2008-01-10 00:10:06 +03:00
|
|
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2000-04-20 09:44:03 +04:00
|
|
|
#include "nsIServiceManager.h"
|
2000-04-22 01:44:23 +04:00
|
|
|
|
2000-12-09 07:11:05 +03:00
|
|
|
#include "nsLocalFile.h" // includes platform-specific headers
|
2000-04-20 09:44:03 +04:00
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2002-04-09 18:12:24 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2002-04-27 09:33:09 +04:00
|
|
|
#include "nsPrintfCString.h"
|
2002-05-15 22:55:21 +04:00
|
|
|
#include "nsCRT.h"
|
2008-01-10 00:10:06 +03:00
|
|
|
#include "nsNativeCharsetUtils.h"
|
|
|
|
#include "nsUTF8Utils.h"
|
2001-06-22 02:02:47 +04:00
|
|
|
|
2004-05-29 19:37:44 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
2012-08-18 06:43:00 +04:00
|
|
|
|
2000-12-09 07:11:05 +03:00
|
|
|
void NS_StartupLocalFile()
|
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
nsLocalFile::GlobalInit();
|
2000-12-09 07:11:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void NS_ShutdownLocalFile()
|
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
nsLocalFile::GlobalShutdown();
|
2000-12-09 07:11:05 +03:00
|
|
|
}
|
|
|
|
|
2010-04-27 16:57:34 +04:00
|
|
|
#if !defined(MOZ_WIDGET_COCOA) && !defined(XP_WIN)
|
2002-04-09 18:12:24 +04:00
|
|
|
NS_IMETHODIMP
|
2012-04-04 15:04:37 +04:00
|
|
|
nsLocalFile::InitWithFile(nsIFile *aFile)
|
2002-04-09 18:12:24 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aFile);
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
nsCAutoString path;
|
2002-05-08 03:07:19 +04:00
|
|
|
aFile->GetNativePath(path);
|
2002-04-27 09:33:09 +04:00
|
|
|
if (path.IsEmpty())
|
2003-01-13 09:26:19 +03:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2002-05-08 03:07:19 +04:00
|
|
|
return InitWithNativePath(path);
|
2002-04-09 18:12:24 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-09-25 16:23:39 +04:00
|
|
|
#define kMaxFilenameLength 255
|
2005-02-19 10:44:59 +03:00
|
|
|
#define kMaxExtensionLength 100
|
2008-01-10 00:10:06 +03:00
|
|
|
#define kMaxSequenceNumberLength 5 // "-9999"
|
|
|
|
// requirement: kMaxExtensionLength < kMaxFilenameLength - kMaxSequenceNumberLength
|
2000-07-13 03:31:31 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-12-08 02:06:51 +03:00
|
|
|
nsLocalFile::CreateUnique(PRUint32 type, PRUint32 attributes)
|
2000-07-13 03:31:31 +04:00
|
|
|
{
|
2008-01-10 00:10:06 +03:00
|
|
|
nsresult rv;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool longName;
|
2008-01-10 00:10:06 +03:00
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
nsAutoString pathName, leafName, rootName, suffix;
|
|
|
|
rv = GetPath(pathName);
|
|
|
|
#else
|
|
|
|
nsCAutoString pathName, leafName, rootName, suffix;
|
|
|
|
rv = GetNativePath(pathName);
|
|
|
|
#endif
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
longName = (pathName.Length() + kMaxSequenceNumberLength >
|
|
|
|
kMaxFilenameLength);
|
|
|
|
if (!longName)
|
|
|
|
{
|
|
|
|
rv = Create(type, attributes);
|
|
|
|
if (rv != NS_ERROR_FILE_ALREADY_EXISTS)
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
rv = GetLeafName(leafName);
|
|
|
|
if (NS_FAILED(rv))
|
2005-02-19 10:44:59 +03:00
|
|
|
return rv;
|
2000-07-13 03:31:31 +04:00
|
|
|
|
2008-01-10 00:10:06 +03:00
|
|
|
const PRInt32 lastDot = leafName.RFindChar(PRUnichar('.'));
|
|
|
|
#else
|
2002-04-27 09:33:09 +04:00
|
|
|
rv = GetNativeLeafName(leafName);
|
2005-02-19 10:44:59 +03:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2000-07-13 03:31:31 +04:00
|
|
|
|
2008-01-10 00:10:06 +03:00
|
|
|
const PRInt32 lastDot = leafName.RFindChar('.');
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (lastDot == kNotFound)
|
|
|
|
{
|
|
|
|
rootName = leafName;
|
|
|
|
}
|
|
|
|
else
|
2000-07-13 03:31:31 +04:00
|
|
|
{
|
2008-01-10 00:10:06 +03:00
|
|
|
suffix = Substring(leafName, lastDot); // include '.'
|
|
|
|
rootName = Substring(leafName, 0, lastDot); // strip suffix and dot
|
2000-07-13 03:31:31 +04:00
|
|
|
}
|
|
|
|
|
2008-01-10 00:10:06 +03:00
|
|
|
if (longName)
|
|
|
|
{
|
2010-05-02 12:28:26 +04:00
|
|
|
PRInt32 maxRootLength = (kMaxFilenameLength -
|
|
|
|
(pathName.Length() - leafName.Length()) -
|
|
|
|
suffix.Length() - kMaxSequenceNumberLength);
|
|
|
|
|
|
|
|
// We cannot create an item inside a directory whose name is too long.
|
|
|
|
// Also, ensure that at least one character remains after we truncate
|
|
|
|
// the root name, as we don't want to end up with an empty leaf name.
|
|
|
|
if (maxRootLength < 2)
|
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
|
|
|
|
2008-01-10 00:10:06 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
// ensure that we don't cut the name in mid-UTF16-character
|
|
|
|
rootName.SetLength(NS_IS_LOW_SURROGATE(rootName[maxRootLength]) ?
|
|
|
|
maxRootLength - 1 : maxRootLength);
|
|
|
|
SetLeafName(rootName + suffix);
|
|
|
|
#else
|
|
|
|
if (NS_IsNativeUTF8())
|
2010-05-02 12:28:26 +04:00
|
|
|
{
|
2008-01-10 00:10:06 +03:00
|
|
|
// ensure that we don't cut the name in mid-UTF8-character
|
2010-05-02 12:28:26 +04:00
|
|
|
// (assume the name is valid UTF8 to begin with)
|
2008-01-10 00:10:06 +03:00
|
|
|
while (UTF8traits::isInSeq(rootName[maxRootLength]))
|
|
|
|
--maxRootLength;
|
2010-05-02 12:28:26 +04:00
|
|
|
|
|
|
|
// Another check to avoid ending up with an empty leaf name.
|
|
|
|
if (maxRootLength == 0 && suffix.IsEmpty())
|
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
|
|
|
}
|
|
|
|
|
2008-01-10 00:10:06 +03:00
|
|
|
rootName.SetLength(maxRootLength);
|
|
|
|
SetNativeLeafName(rootName + suffix);
|
|
|
|
#endif
|
|
|
|
nsresult rv = Create(type, attributes);
|
|
|
|
if (rv != NS_ERROR_FILE_ALREADY_EXISTS)
|
|
|
|
return rv;
|
|
|
|
}
|
2000-07-13 03:31:31 +04:00
|
|
|
|
2005-02-19 10:44:59 +03:00
|
|
|
for (int indx = 1; indx < 10000; indx++)
|
2000-07-13 03:31:31 +04:00
|
|
|
{
|
|
|
|
// start with "Picture-1.jpg" after "Picture.jpg" exists
|
2008-01-10 00:10:06 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
SetLeafName(rootName +
|
|
|
|
NS_ConvertASCIItoUTF16(nsPrintfCString("-%d", indx)) +
|
|
|
|
suffix);
|
|
|
|
#else
|
|
|
|
SetNativeLeafName(rootName + nsPrintfCString("-%d", indx) + suffix);
|
|
|
|
#endif
|
2000-07-13 03:31:31 +04:00
|
|
|
rv = Create(type, attributes);
|
|
|
|
if (NS_SUCCEEDED(rv) || rv != NS_ERROR_FILE_ALREADY_EXISTS)
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The disk is full, sort of
|
|
|
|
return NS_ERROR_FILE_TOO_BIG;
|
|
|
|
}
|
|
|
|
|
2006-11-16 12:10:09 +03:00
|
|
|
#if defined(XP_WIN) || defined(XP_OS2)
|
2002-05-08 03:07:19 +04:00
|
|
|
static const PRUnichar kPathSeparatorChar = '\\';
|
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 22:10:24 +03:00
|
|
|
#elif defined(XP_UNIX)
|
2002-05-08 03:07:19 +04:00
|
|
|
static const PRUnichar kPathSeparatorChar = '/';
|
2002-04-09 18:12:24 +04:00
|
|
|
#else
|
|
|
|
#error Need to define file path separator for your platform
|
|
|
|
#endif
|
|
|
|
|
2002-05-08 03:07:19 +04:00
|
|
|
static PRInt32 SplitPath(PRUnichar *path, PRUnichar **nodeArray, PRInt32 arrayLen)
|
2002-04-09 18:12:24 +04:00
|
|
|
{
|
|
|
|
if (*path == 0)
|
|
|
|
return 0;
|
|
|
|
|
2002-05-08 03:07:19 +04:00
|
|
|
PRUnichar **nodePtr = nodeArray;
|
2002-04-09 18:12:24 +04:00
|
|
|
if (*path == kPathSeparatorChar)
|
|
|
|
path++;
|
|
|
|
*nodePtr++ = path;
|
|
|
|
|
2002-05-08 03:07:19 +04:00
|
|
|
for (PRUnichar *cp = path; *cp != 0; cp++) {
|
2002-04-09 18:12:24 +04:00
|
|
|
if (*cp == kPathSeparatorChar) {
|
|
|
|
*cp++ = 0;
|
2004-06-08 09:03:59 +04:00
|
|
|
if (*cp == 0)
|
|
|
|
break;
|
|
|
|
if (nodePtr - nodeArray >= arrayLen)
|
|
|
|
return -1;
|
|
|
|
*nodePtr++ = cp;
|
2002-04-09 18:12:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodePtr - nodeArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-04-04 15:04:37 +04:00
|
|
|
nsLocalFile::GetRelativeDescriptor(nsIFile *fromFile, nsACString& _retval)
|
2002-04-09 18:12:24 +04:00
|
|
|
{
|
2004-04-28 16:53:20 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(fromFile);
|
2002-04-09 18:12:24 +04:00
|
|
|
const PRInt32 kMaxNodesInPath = 32;
|
2002-05-08 03:07:19 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// _retval will be UTF-8 encoded
|
|
|
|
//
|
2002-04-09 18:12:24 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
_retval.Truncate(0);
|
|
|
|
|
2002-05-08 03:07:19 +04:00
|
|
|
nsAutoString thisPath, fromPath;
|
|
|
|
PRUnichar *thisNodes[kMaxNodesInPath], *fromNodes[kMaxNodesInPath];
|
2002-04-09 18:12:24 +04:00
|
|
|
PRInt32 thisNodeCnt, fromNodeCnt, nodeIndex;
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
rv = GetPath(thisPath);
|
2002-04-09 18:12:24 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2002-04-27 09:33:09 +04:00
|
|
|
rv = fromFile->GetPath(fromPath);
|
|
|
|
if (NS_FAILED(rv))
|
2002-04-09 18:12:24 +04:00
|
|
|
return rv;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
// get raw pointer to mutable string buffer
|
|
|
|
PRUnichar *thisPathPtr; thisPath.BeginWriting(thisPathPtr);
|
|
|
|
PRUnichar *fromPathPtr; fromPath.BeginWriting(fromPathPtr);
|
2002-04-09 18:12:24 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
thisNodeCnt = SplitPath(thisPathPtr, thisNodes, kMaxNodesInPath);
|
|
|
|
fromNodeCnt = SplitPath(fromPathPtr, fromNodes, kMaxNodesInPath);
|
2002-04-27 09:33:09 +04:00
|
|
|
if (thisNodeCnt < 0 || fromNodeCnt < 0)
|
2002-04-09 18:12:24 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2004-05-29 19:37:44 +04:00
|
|
|
for (nodeIndex = 0; nodeIndex < thisNodeCnt && nodeIndex < fromNodeCnt; ++nodeIndex) {
|
|
|
|
#ifdef XP_WIN
|
|
|
|
if (_wcsicmp(thisNodes[nodeIndex], fromNodes[nodeIndex]))
|
|
|
|
break;
|
|
|
|
#else
|
2003-12-19 21:13:42 +03:00
|
|
|
if (nsCRT::strcmp(thisNodes[nodeIndex], fromNodes[nodeIndex]))
|
2002-04-09 18:12:24 +04:00
|
|
|
break;
|
2004-05-29 19:37:44 +04:00
|
|
|
#endif
|
2002-04-09 18:12:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32 branchIndex = nodeIndex;
|
|
|
|
for (nodeIndex = branchIndex; nodeIndex < fromNodeCnt; nodeIndex++)
|
2004-06-17 04:13:25 +04:00
|
|
|
_retval.AppendLiteral("../");
|
2002-04-09 18:12:24 +04:00
|
|
|
for (nodeIndex = branchIndex; nodeIndex < thisNodeCnt; nodeIndex++) {
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_ConvertUTF16toUTF8 nodeStr(thisNodes[nodeIndex]);
|
2002-04-27 09:33:09 +04:00
|
|
|
_retval.Append(nodeStr);
|
2002-04-09 18:12:24 +04:00
|
|
|
if (nodeIndex + 1 < thisNodeCnt)
|
|
|
|
_retval.Append('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-04-04 15:04:37 +04:00
|
|
|
nsLocalFile::SetRelativeDescriptor(nsIFile *fromFile, const nsACString& relativeDesc)
|
2002-04-09 18:12:24 +04:00
|
|
|
{
|
|
|
|
NS_NAMED_LITERAL_CSTRING(kParentDirStr, "../");
|
2002-01-09 23:03:46 +03:00
|
|
|
|
2002-04-09 18:12:24 +04:00
|
|
|
nsCOMPtr<nsIFile> targetFile;
|
|
|
|
nsresult rv = fromFile->Clone(getter_AddRefs(targetFile));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2002-05-08 03:07:19 +04:00
|
|
|
//
|
|
|
|
// relativeDesc is UTF-8 encoded
|
|
|
|
//
|
|
|
|
|
2002-04-09 18:12:24 +04:00
|
|
|
nsCString::const_iterator strBegin, strEnd;
|
|
|
|
relativeDesc.BeginReading(strBegin);
|
|
|
|
relativeDesc.EndReading(strEnd);
|
|
|
|
|
|
|
|
nsCString::const_iterator nodeBegin(strBegin), nodeEnd(strEnd);
|
|
|
|
nsCString::const_iterator pos(strBegin);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> parentDir;
|
|
|
|
while (FindInReadable(kParentDirStr, nodeBegin, nodeEnd)) {
|
|
|
|
rv = targetFile->GetParent(getter_AddRefs(parentDir));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2005-06-29 21:25:24 +04:00
|
|
|
if (!parentDir)
|
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
2002-04-09 18:12:24 +04:00
|
|
|
targetFile = parentDir;
|
2001-11-16 05:09:13 +03:00
|
|
|
|
2002-04-09 18:12:24 +04:00
|
|
|
nodeBegin = nodeEnd;
|
|
|
|
pos = nodeEnd;
|
|
|
|
nodeEnd = strEnd;
|
|
|
|
}
|
2001-11-16 05:09:13 +03:00
|
|
|
|
2002-04-09 18:12:24 +04:00
|
|
|
nodeBegin = nodeEnd = pos;
|
|
|
|
while (nodeEnd != strEnd) {
|
|
|
|
FindCharInReadable('/', nodeEnd, strEnd);
|
2006-02-03 17:18:39 +03:00
|
|
|
targetFile->Append(NS_ConvertUTF8toUTF16(Substring(nodeBegin, nodeEnd)));
|
2002-04-09 18:12:24 +04:00
|
|
|
if (nodeEnd != strEnd) // If there's more left in the string, inc over the '/' nodeEnd is on.
|
|
|
|
++nodeEnd;
|
|
|
|
nodeBegin = nodeEnd;
|
|
|
|
}
|
2001-11-16 05:09:13 +03:00
|
|
|
|
2012-04-04 15:04:37 +04:00
|
|
|
return InitWithFile(targetFile);
|
2002-04-09 18:12:24 +04:00
|
|
|
}
|