2008-01-09 21:10:06 +00:00
|
|
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00: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 05:44:03 +00:00
|
|
|
#include "nsIServiceManager.h"
|
2000-04-21 21:44:23 +00:00
|
|
|
|
2000-12-09 04:11:05 +00:00
|
|
|
#include "nsLocalFile.h" // includes platform-specific headers
|
2000-04-20 05:44:03 +00:00
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2002-04-09 14:12:24 +00:00
|
|
|
#include "nsReadableUtils.h"
|
2002-04-27 05:33:09 +00:00
|
|
|
#include "nsPrintfCString.h"
|
2002-05-15 18:55:21 +00:00
|
|
|
#include "nsCRT.h"
|
2008-01-09 21:10:06 +00:00
|
|
|
#include "nsNativeCharsetUtils.h"
|
|
|
|
#include "nsUTF8Utils.h"
|
2001-06-21 22:02:47 +00:00
|
|
|
|
2004-05-29 15:37:44 +00:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
2012-08-18 02:43:00 +00:00
|
|
|
|
2000-12-09 04:11:05 +00:00
|
|
|
void NS_StartupLocalFile()
|
|
|
|
{
|
2002-04-27 05:33:09 +00:00
|
|
|
nsLocalFile::GlobalInit();
|
2000-12-09 04:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NS_ShutdownLocalFile()
|
|
|
|
{
|
2002-04-27 05:33:09 +00:00
|
|
|
nsLocalFile::GlobalShutdown();
|
2000-12-09 04:11:05 +00:00
|
|
|
}
|
|
|
|
|
2010-04-27 12:57:34 +00:00
|
|
|
#if !defined(MOZ_WIDGET_COCOA) && !defined(XP_WIN)
|
2002-04-09 14:12:24 +00:00
|
|
|
NS_IMETHODIMP
|
2012-04-04 11:04:37 +00:00
|
|
|
nsLocalFile::InitWithFile(nsIFile *aFile)
|
2002-04-09 14:12:24 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aFile);
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString path;
|
2002-05-07 23:07:19 +00:00
|
|
|
aFile->GetNativePath(path);
|
2002-04-27 05:33:09 +00:00
|
|
|
if (path.IsEmpty())
|
2003-01-13 06:26:19 +00:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2002-05-07 23:07:19 +00:00
|
|
|
return InitWithNativePath(path);
|
2002-04-09 14:12:24 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-09-25 12:23:39 +00:00
|
|
|
#define kMaxFilenameLength 255
|
2005-02-19 07:44:59 +00:00
|
|
|
#define kMaxExtensionLength 100
|
2008-01-09 21:10:06 +00:00
|
|
|
#define kMaxSequenceNumberLength 5 // "-9999"
|
|
|
|
// requirement: kMaxExtensionLength < kMaxFilenameLength - kMaxSequenceNumberLength
|
2000-07-12 23:31:31 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsLocalFile::CreateUnique(uint32_t type, uint32_t attributes)
|
2000-07-12 23:31:31 +00:00
|
|
|
{
|
2008-01-09 21:10:06 +00:00
|
|
|
nsresult rv;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool longName;
|
2008-01-09 21:10:06 +00:00
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
nsAutoString pathName, leafName, rootName, suffix;
|
|
|
|
rv = GetPath(pathName);
|
|
|
|
#else
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString pathName, leafName, rootName, suffix;
|
2008-01-09 21:10:06 +00:00
|
|
|
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 07:44:59 +00:00
|
|
|
return rv;
|
2000-07-12 23:31:31 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
const int32_t lastDot = leafName.RFindChar(PRUnichar('.'));
|
2008-01-09 21:10:06 +00:00
|
|
|
#else
|
2002-04-27 05:33:09 +00:00
|
|
|
rv = GetNativeLeafName(leafName);
|
2005-02-19 07:44:59 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2000-07-12 23:31:31 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
const int32_t lastDot = leafName.RFindChar('.');
|
2008-01-09 21:10:06 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (lastDot == kNotFound)
|
|
|
|
{
|
|
|
|
rootName = leafName;
|
|
|
|
}
|
|
|
|
else
|
2000-07-12 23:31:31 +00:00
|
|
|
{
|
2008-01-09 21:10:06 +00:00
|
|
|
suffix = Substring(leafName, lastDot); // include '.'
|
|
|
|
rootName = Substring(leafName, 0, lastDot); // strip suffix and dot
|
2000-07-12 23:31:31 +00:00
|
|
|
}
|
|
|
|
|
2008-01-09 21:10:06 +00:00
|
|
|
if (longName)
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t maxRootLength = (kMaxFilenameLength -
|
2010-05-02 08:28:26 +00:00
|
|
|
(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-09 21:10:06 +00: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 08:28:26 +00:00
|
|
|
{
|
2008-01-09 21:10:06 +00:00
|
|
|
// ensure that we don't cut the name in mid-UTF8-character
|
2010-05-02 08:28:26 +00:00
|
|
|
// (assume the name is valid UTF8 to begin with)
|
2008-01-09 21:10:06 +00:00
|
|
|
while (UTF8traits::isInSeq(rootName[maxRootLength]))
|
|
|
|
--maxRootLength;
|
2010-05-02 08:28:26 +00: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-09 21:10:06 +00:00
|
|
|
rootName.SetLength(maxRootLength);
|
|
|
|
SetNativeLeafName(rootName + suffix);
|
|
|
|
#endif
|
|
|
|
nsresult rv = Create(type, attributes);
|
|
|
|
if (rv != NS_ERROR_FILE_ALREADY_EXISTS)
|
|
|
|
return rv;
|
|
|
|
}
|
2000-07-12 23:31:31 +00:00
|
|
|
|
2005-02-19 07:44:59 +00:00
|
|
|
for (int indx = 1; indx < 10000; indx++)
|
2000-07-12 23:31:31 +00:00
|
|
|
{
|
|
|
|
// start with "Picture-1.jpg" after "Picture.jpg" exists
|
2008-01-09 21:10:06 +00:00
|
|
|
#ifdef XP_WIN
|
|
|
|
SetLeafName(rootName +
|
|
|
|
NS_ConvertASCIItoUTF16(nsPrintfCString("-%d", indx)) +
|
|
|
|
suffix);
|
|
|
|
#else
|
|
|
|
SetNativeLeafName(rootName + nsPrintfCString("-%d", indx) + suffix);
|
|
|
|
#endif
|
2000-07-12 23:31:31 +00: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 09:10:09 +00:00
|
|
|
#if defined(XP_WIN) || defined(XP_OS2)
|
2002-05-07 23:07:19 +00: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 19:10:24 +00:00
|
|
|
#elif defined(XP_UNIX)
|
2002-05-07 23:07:19 +00:00
|
|
|
static const PRUnichar kPathSeparatorChar = '/';
|
2002-04-09 14:12:24 +00:00
|
|
|
#else
|
|
|
|
#error Need to define file path separator for your platform
|
|
|
|
#endif
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
static int32_t SplitPath(PRUnichar *path, PRUnichar **nodeArray, int32_t arrayLen)
|
2002-04-09 14:12:24 +00:00
|
|
|
{
|
|
|
|
if (*path == 0)
|
|
|
|
return 0;
|
|
|
|
|
2002-05-07 23:07:19 +00:00
|
|
|
PRUnichar **nodePtr = nodeArray;
|
2002-04-09 14:12:24 +00:00
|
|
|
if (*path == kPathSeparatorChar)
|
|
|
|
path++;
|
|
|
|
*nodePtr++ = path;
|
|
|
|
|
2002-05-07 23:07:19 +00:00
|
|
|
for (PRUnichar *cp = path; *cp != 0; cp++) {
|
2002-04-09 14:12:24 +00:00
|
|
|
if (*cp == kPathSeparatorChar) {
|
|
|
|
*cp++ = 0;
|
2004-06-08 05:03:59 +00:00
|
|
|
if (*cp == 0)
|
|
|
|
break;
|
|
|
|
if (nodePtr - nodeArray >= arrayLen)
|
|
|
|
return -1;
|
|
|
|
*nodePtr++ = cp;
|
2002-04-09 14:12:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodePtr - nodeArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-04-04 11:04:37 +00:00
|
|
|
nsLocalFile::GetRelativeDescriptor(nsIFile *fromFile, nsACString& _retval)
|
2002-04-09 14:12:24 +00:00
|
|
|
{
|
2004-04-28 12:53:20 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(fromFile);
|
2012-08-22 15:56:38 +00:00
|
|
|
const int32_t kMaxNodesInPath = 32;
|
2002-05-07 23:07:19 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// _retval will be UTF-8 encoded
|
|
|
|
//
|
2002-04-09 14:12:24 +00:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
_retval.Truncate(0);
|
|
|
|
|
2002-05-07 23:07:19 +00:00
|
|
|
nsAutoString thisPath, fromPath;
|
|
|
|
PRUnichar *thisNodes[kMaxNodesInPath], *fromNodes[kMaxNodesInPath];
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t thisNodeCnt, fromNodeCnt, nodeIndex;
|
2002-04-09 14:12:24 +00:00
|
|
|
|
2002-04-27 05:33:09 +00:00
|
|
|
rv = GetPath(thisPath);
|
2002-04-09 14:12:24 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2002-04-27 05:33:09 +00:00
|
|
|
rv = fromFile->GetPath(fromPath);
|
|
|
|
if (NS_FAILED(rv))
|
2002-04-09 14:12:24 +00:00
|
|
|
return rv;
|
2004-02-19 02:44:03 +00:00
|
|
|
|
|
|
|
// get raw pointer to mutable string buffer
|
|
|
|
PRUnichar *thisPathPtr; thisPath.BeginWriting(thisPathPtr);
|
|
|
|
PRUnichar *fromPathPtr; fromPath.BeginWriting(fromPathPtr);
|
2002-04-09 14:12:24 +00:00
|
|
|
|
2004-02-19 02:44:03 +00:00
|
|
|
thisNodeCnt = SplitPath(thisPathPtr, thisNodes, kMaxNodesInPath);
|
|
|
|
fromNodeCnt = SplitPath(fromPathPtr, fromNodes, kMaxNodesInPath);
|
2002-04-27 05:33:09 +00:00
|
|
|
if (thisNodeCnt < 0 || fromNodeCnt < 0)
|
2002-04-09 14:12:24 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2004-05-29 15:37:44 +00:00
|
|
|
for (nodeIndex = 0; nodeIndex < thisNodeCnt && nodeIndex < fromNodeCnt; ++nodeIndex) {
|
|
|
|
#ifdef XP_WIN
|
|
|
|
if (_wcsicmp(thisNodes[nodeIndex], fromNodes[nodeIndex]))
|
|
|
|
break;
|
|
|
|
#else
|
2013-01-03 23:36:16 +00:00
|
|
|
if (nsCRT::strcmp(thisNodes[nodeIndex], fromNodes[nodeIndex]))
|
2002-04-09 14:12:24 +00:00
|
|
|
break;
|
2004-05-29 15:37:44 +00:00
|
|
|
#endif
|
2002-04-09 14:12:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t branchIndex = nodeIndex;
|
2002-04-09 14:12:24 +00:00
|
|
|
for (nodeIndex = branchIndex; nodeIndex < fromNodeCnt; nodeIndex++)
|
2004-06-17 00:13:25 +00:00
|
|
|
_retval.AppendLiteral("../");
|
2002-04-09 14:12:24 +00:00
|
|
|
for (nodeIndex = branchIndex; nodeIndex < thisNodeCnt; nodeIndex++) {
|
2006-02-03 14:18:39 +00:00
|
|
|
NS_ConvertUTF16toUTF8 nodeStr(thisNodes[nodeIndex]);
|
2002-04-27 05:33:09 +00:00
|
|
|
_retval.Append(nodeStr);
|
2002-04-09 14:12:24 +00:00
|
|
|
if (nodeIndex + 1 < thisNodeCnt)
|
|
|
|
_retval.Append('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-04-04 11:04:37 +00:00
|
|
|
nsLocalFile::SetRelativeDescriptor(nsIFile *fromFile, const nsACString& relativeDesc)
|
2002-04-09 14:12:24 +00:00
|
|
|
{
|
|
|
|
NS_NAMED_LITERAL_CSTRING(kParentDirStr, "../");
|
2002-01-09 20:03:46 +00:00
|
|
|
|
2002-04-09 14:12:24 +00:00
|
|
|
nsCOMPtr<nsIFile> targetFile;
|
|
|
|
nsresult rv = fromFile->Clone(getter_AddRefs(targetFile));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2002-05-07 23:07:19 +00:00
|
|
|
//
|
|
|
|
// relativeDesc is UTF-8 encoded
|
|
|
|
//
|
|
|
|
|
2002-04-09 14:12:24 +00: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 17:25:24 +00:00
|
|
|
if (!parentDir)
|
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
2002-04-09 14:12:24 +00:00
|
|
|
targetFile = parentDir;
|
2001-11-16 02:09:13 +00:00
|
|
|
|
2002-04-09 14:12:24 +00:00
|
|
|
nodeBegin = nodeEnd;
|
|
|
|
pos = nodeEnd;
|
|
|
|
nodeEnd = strEnd;
|
|
|
|
}
|
2001-11-16 02:09:13 +00:00
|
|
|
|
2002-04-09 14:12:24 +00:00
|
|
|
nodeBegin = nodeEnd = pos;
|
|
|
|
while (nodeEnd != strEnd) {
|
|
|
|
FindCharInReadable('/', nodeEnd, strEnd);
|
2006-02-03 14:18:39 +00:00
|
|
|
targetFile->Append(NS_ConvertUTF8toUTF16(Substring(nodeBegin, nodeEnd)));
|
2002-04-09 14:12:24 +00:00
|
|
|
if (nodeEnd != strEnd) // If there's more left in the string, inc over the '/' nodeEnd is on.
|
|
|
|
++nodeEnd;
|
|
|
|
nodeBegin = nodeEnd;
|
|
|
|
}
|
2001-11-16 02:09:13 +00:00
|
|
|
|
2012-04-04 11:04:37 +00:00
|
|
|
return InitWithFile(targetFile);
|
2002-04-09 14:12:24 +00:00
|
|
|
}
|