2014-05-05 10:30:43 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07: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/. */
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
|
|
|
|
#include "nsLocalFile.h" // includes platform-specific headers
|
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsPrintfCString.h"
|
|
|
|
#include "nsCRT.h"
|
2008-01-09 13:10:06 -08:00
|
|
|
#include "nsNativeCharsetUtils.h"
|
|
|
|
#include "nsUTF8Utils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
2012-08-17 19:43:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
void
|
|
|
|
NS_StartupLocalFile()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
nsLocalFile::GlobalInit();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
void
|
|
|
|
NS_ShutdownLocalFile()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
nsLocalFile::GlobalShutdown();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-04-27 05:57:34 -07:00
|
|
|
#if !defined(MOZ_WIDGET_COCOA) && !defined(XP_WIN)
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::InitWithFile(nsIFile* aFile)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!aFile)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
nsAutoCString path;
|
|
|
|
aFile->GetNativePath(path);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (path.IsEmpty()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
return InitWithNativePath(path);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define kMaxFilenameLength 255
|
|
|
|
#define kMaxExtensionLength 100
|
2008-01-09 13:10:06 -08:00
|
|
|
#define kMaxSequenceNumberLength 5 // "-9999"
|
|
|
|
// requirement: kMaxExtensionLength < kMaxFilenameLength - kMaxSequenceNumberLength
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::CreateUnique(uint32_t aType, uint32_t aAttributes)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv;
|
|
|
|
bool longName;
|
2008-01-09 13:10:06 -08:00
|
|
|
|
|
|
|
#ifdef XP_WIN
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString pathName, leafName, rootName, suffix;
|
|
|
|
rv = GetPath(pathName);
|
2008-01-09 13:10:06 -08:00
|
|
|
#else
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoCString pathName, leafName, rootName, suffix;
|
|
|
|
rv = GetNativePath(pathName);
|
2008-01-09 13:10:06 -08:00
|
|
|
#endif
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
longName = (pathName.Length() + kMaxSequenceNumberLength >
|
|
|
|
kMaxFilenameLength);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!longName) {
|
|
|
|
rv = Create(aType, aAttributes);
|
|
|
|
if (rv != NS_ERROR_FILE_ALREADY_EXISTS) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2008-01-09 13:10:06 -08:00
|
|
|
|
|
|
|
#ifdef XP_WIN
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = GetLeafName(leafName);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
const int32_t lastDot = leafName.RFindChar(char16_t('.'));
|
2008-01-09 13:10:06 -08:00
|
|
|
#else
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = GetNativeLeafName(leafName);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
const int32_t lastDot = leafName.RFindChar('.');
|
2008-01-09 13:10:06 -08:00
|
|
|
#endif
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (lastDot == kNotFound) {
|
2014-05-05 10:30:43 -07:00
|
|
|
rootName = leafName;
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
suffix = Substring(leafName, lastDot); // include '.'
|
|
|
|
rootName = Substring(leafName, 0, lastDot); // strip suffix and dot
|
|
|
|
}
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (longName) {
|
2014-05-05 10:30:43 -07:00
|
|
|
int32_t 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.
|
2014-05-14 13:15:46 -07:00
|
|
|
if (maxRootLength < 2) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2010-05-02 01:28:26 -07:00
|
|
|
|
2008-01-09 13:10:06 -08:00
|
|
|
#ifdef XP_WIN
|
2014-05-05 10:30:43 -07:00
|
|
|
// 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);
|
2008-01-09 13:10:06 -08:00
|
|
|
#else
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_IsNativeUTF8()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
// ensure that we don't cut the name in mid-UTF8-character
|
|
|
|
// (assume the name is valid UTF8 to begin with)
|
2014-05-14 13:15:46 -07:00
|
|
|
while (UTF8traits::isInSeq(rootName[maxRootLength])) {
|
2014-05-05 10:30:43 -07:00
|
|
|
--maxRootLength;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
// Another check to avoid ending up with an empty leaf name.
|
2014-05-14 13:15:46 -07:00
|
|
|
if (maxRootLength == 0 && suffix.IsEmpty()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2008-01-09 13:10:06 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
rootName.SetLength(maxRootLength);
|
|
|
|
SetNativeLeafName(rootName + suffix);
|
|
|
|
#endif
|
2014-05-14 13:15:46 -07:00
|
|
|
nsresult rv = Create(aType, aAttributes);
|
|
|
|
if (rv != NS_ERROR_FILE_ALREADY_EXISTS) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
for (int indx = 1; indx < 10000; ++indx) {
|
2014-05-05 10:30:43 -07:00
|
|
|
// start with "Picture-1.jpg" after "Picture.jpg" exists
|
2008-01-09 13:10:06 -08:00
|
|
|
#ifdef XP_WIN
|
2014-05-05 10:30:43 -07:00
|
|
|
SetLeafName(rootName +
|
|
|
|
NS_ConvertASCIItoUTF16(nsPrintfCString("-%d", indx)) +
|
|
|
|
suffix);
|
2008-01-09 13:10:06 -08:00
|
|
|
#else
|
2014-05-05 10:30:43 -07:00
|
|
|
SetNativeLeafName(rootName + nsPrintfCString("-%d", indx) + suffix);
|
2008-01-09 13:10:06 -08:00
|
|
|
#endif
|
2014-05-14 13:15:46 -07:00
|
|
|
rv = Create(aType, aAttributes);
|
|
|
|
if (NS_SUCCEEDED(rv) || rv != NS_ERROR_FILE_ALREADY_EXISTS) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// The disk is full, sort of
|
|
|
|
return NS_ERROR_FILE_TOO_BIG;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-02-10 14:57:01 -08:00
|
|
|
#if defined(XP_WIN)
|
2014-01-04 07:02:17 -08:00
|
|
|
static const char16_t 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 11:10:24 -08:00
|
|
|
#elif defined(XP_UNIX)
|
2014-01-04 07:02:17 -08:00
|
|
|
static const char16_t kPathSeparatorChar = '/';
|
2007-03-22 10:30:00 -07:00
|
|
|
#else
|
|
|
|
#error Need to define file path separator for your platform
|
|
|
|
#endif
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
static int32_t
|
|
|
|
SplitPath(char16_t* aPath, char16_t** aNodeArray, int32_t aArrayLen)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (*aPath == 0) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return 0;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
char16_t** nodePtr = aNodeArray;
|
|
|
|
if (*aPath == kPathSeparatorChar) {
|
|
|
|
aPath++;
|
|
|
|
}
|
|
|
|
*nodePtr++ = aPath;
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
for (char16_t* cp = aPath; *cp != 0; ++cp) {
|
2014-05-05 10:30:43 -07:00
|
|
|
if (*cp == kPathSeparatorChar) {
|
|
|
|
*cp++ = 0;
|
2014-05-14 13:15:46 -07:00
|
|
|
if (*cp == 0) {
|
2014-05-05 10:30:43 -07:00
|
|
|
break;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
if (nodePtr - aNodeArray >= aArrayLen) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return -1;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
*nodePtr++ = cp;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2014-05-14 13:15:46 -07:00
|
|
|
return nodePtr - aNodeArray;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetRelativeDescriptor(nsIFile* aFromFile, nsACString& aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!aFromFile)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
const int32_t kMaxNodesInPath = 32;
|
|
|
|
|
|
|
|
//
|
2014-05-14 13:15:46 -07:00
|
|
|
// aResult will be UTF-8 encoded
|
2014-05-05 10:30:43 -07:00
|
|
|
//
|
|
|
|
|
|
|
|
nsresult rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
aResult.Truncate(0);
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
nsAutoString thisPath, fromPath;
|
2014-05-14 13:15:46 -07:00
|
|
|
char16_t* thisNodes[kMaxNodesInPath];
|
|
|
|
char16_t* fromNodes[kMaxNodesInPath];
|
|
|
|
int32_t thisNodeCnt, fromNodeCnt, nodeIndex;
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
rv = GetPath(thisPath);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
rv = aFromFile->GetPath(fromPath);
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
// get raw pointer to mutable string buffer
|
2014-05-14 13:15:46 -07:00
|
|
|
char16_t* thisPathPtr;
|
|
|
|
thisPath.BeginWriting(thisPathPtr);
|
|
|
|
char16_t* fromPathPtr;
|
|
|
|
fromPath.BeginWriting(fromPathPtr);
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
thisNodeCnt = SplitPath(thisPathPtr, thisNodes, kMaxNodesInPath);
|
|
|
|
fromNodeCnt = SplitPath(fromPathPtr, fromNodes, kMaxNodesInPath);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (thisNodeCnt < 0 || fromNodeCnt < 0) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2014-08-25 12:17:15 -07:00
|
|
|
for (nodeIndex = 0; nodeIndex < thisNodeCnt &&
|
|
|
|
nodeIndex < fromNodeCnt; ++nodeIndex) {
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef XP_WIN
|
2014-08-25 12:17:15 -07:00
|
|
|
if (_wcsicmp(char16ptr_t(thisNodes[nodeIndex]),
|
|
|
|
char16ptr_t(fromNodes[nodeIndex]))) {
|
2014-05-05 10:30:43 -07:00
|
|
|
break;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#else
|
2014-05-14 13:15:46 -07:00
|
|
|
if (nsCRT::strcmp(thisNodes[nodeIndex], fromNodes[nodeIndex])) {
|
2014-05-05 10:30:43 -07:00
|
|
|
break;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t branchIndex = nodeIndex;
|
2014-05-14 13:15:46 -07:00
|
|
|
for (nodeIndex = branchIndex; nodeIndex < fromNodeCnt; ++nodeIndex) {
|
|
|
|
aResult.AppendLiteral("../");
|
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
for (nodeIndex = branchIndex; nodeIndex < thisNodeCnt; nodeIndex++) {
|
|
|
|
NS_ConvertUTF16toUTF8 nodeStr(thisNodes[nodeIndex]);
|
2014-05-14 13:15:46 -07:00
|
|
|
aResult.Append(nodeStr);
|
|
|
|
if (nodeIndex + 1 < thisNodeCnt) {
|
|
|
|
aResult.Append('/');
|
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::SetRelativeDescriptor(nsIFile* aFromFile,
|
|
|
|
const nsACString& aRelativeDesc)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
NS_NAMED_LITERAL_CSTRING(kParentDirStr, "../");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsCOMPtr<nsIFile> targetFile;
|
2014-05-14 13:15:46 -07:00
|
|
|
nsresult rv = aFromFile->Clone(getter_AddRefs(targetFile));
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
//
|
2014-05-14 13:15:46 -07:00
|
|
|
// aRelativeDesc is UTF-8 encoded
|
2014-05-05 10:30:43 -07:00
|
|
|
//
|
|
|
|
|
|
|
|
nsCString::const_iterator strBegin, strEnd;
|
2014-05-14 13:15:46 -07:00
|
|
|
aRelativeDesc.BeginReading(strBegin);
|
|
|
|
aRelativeDesc.EndReading(strEnd);
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
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));
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
if (!parentDir) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
targetFile = parentDir;
|
|
|
|
|
|
|
|
nodeBegin = nodeEnd;
|
|
|
|
pos = nodeEnd;
|
|
|
|
nodeEnd = strEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeBegin = nodeEnd = pos;
|
|
|
|
while (nodeEnd != strEnd) {
|
|
|
|
FindCharInReadable('/', nodeEnd, strEnd);
|
|
|
|
targetFile->Append(NS_ConvertUTF8toUTF16(Substring(nodeBegin, nodeEnd)));
|
2014-05-14 13:15:46 -07:00
|
|
|
if (nodeEnd != strEnd) { // If there's more left in the string, inc over the '/' nodeEnd is on.
|
2014-05-05 10:30:43 -07:00
|
|
|
++nodeEnd;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
nodeBegin = nodeEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
return InitWithFile(targetFile);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|