Files
palemoon27/xpcom/io/nsLocalFileCommon.cpp
T
roytam1 16aec83f00 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1175600. Add getRelativePath/setRelativePath to nsIFile. r=froydnj (93515cbda)
- bug 1171124 - Swap some XP_MACOSX for XP_DARWIN in nsLocalFileUnix. r=froydnj (1928918ac)
- Bug 1192230 - clean up reference-counting in xpcom/; r=erahm (11528cd9f)
- missing bit of Bug 1138293 - Use malloc/free/realloc/calloc (365fb9991)
- Bug 1175601. Make nsIFile.getRelativeDescriptor work with paths with aths with arbitrary numbers of segments, not just 32. r=froydnj (a4b5ea11d)
- Bug 1175600 followup. Add getRelativePath to ye othere nsIFile implementatione so as to de-bust ye olde CLOSED TREE (b23dfee9e)
- Bug 1159604 - Use fallible allocations in nsLinebreakConverter.cpp (as the code expects). r=smaug (498f30023)
- Bug 1139547 - Fix unsequenced variable modmification/access error. r=botond,bsmedberg (a8460ecff)
- Bug 1167380 - Mark (Read/Write)SegmentsState::m(Source/Sink) as MOZ_NON_OWNING_REF and mark as MOZ_STACK_CLASS. r=froydnj (a1f2dc2ae)
- Bug 1137464 - add missing include for nsICloneableInputStream.h to nsStreamUtils.cpp; r=bkelly (dddd1831c)
- Bug 1026761 - CID 749761: nsAStreamCopier::Process can use sourceCondition, sinkCondition uninitialized. r=froydnj (a728f2b3e)
- Bug 1189423 - part 1 - Add MOZ_COUNT_CTOR/DTOR for nsXPTCStubBase. r=froydnj (208b578bb)
- Bug 1189423 - part 2 - Remove superfluous |new| result check. r=froydnj (b58d8b1fd)
- Bug 1151506 - Null-check the count parameter in GetConstantCount. r=froydnj (6e89f6635)
- Bug 1171603 - Better size check in nsTSubstring::ReplacePrep. r=ehsan (f17bb9c07)
- Bug 603201 - Enable primitive receivers in [[Get]]. r=jorendorff (24e6b04e2)
- revert/align malloc functions to bug 1156317 21 April 2015 (89de8cbad)
- Bug 1081260 - Update the malloc counters if we successfully recover from OOM; r=jonco (c3a03f041)
- Bug 1196210 - Fix an incomplete assertion in the nursery; r=jonco (84140599a)
- Bug 1198826 - Increment (plain) gcNumber on all GCs, r=terrence (a45b9d65c)
- Bug 1184578 - Fix the nursery profiling print statement; r=sfink (f03accbe4)
- Bug 1188878 - Ensure RematerializedFrames are cleared from Debugger instances on Ion bailout failure. (r=jandem) (0cad2e549)
- pointer style (96793b3b3)
- some leftover (7685205e9)
- pointer style (5dc480bd4)
- Bug 1145781 - Unlazify functions when getting their debug scopes. (r=jimb) (f86c2a1b6)
- Bug 1148388 - Handle lost accesses in missing Debugger scopes for block objects. (r=jimb) (9c1734834)
- Bug 1118865 - Relax assertion in DebugScopeProxy::isMagicMissingArgumentsValue. (r=nbp) (7b46b3929)
- Bug 1193046 - Clear prevUpToDate on younger frames when toggling frame debuggeeness off->on. (r=jimb) (f93459582)
- Bug 1188334 - Fix this one weird case with creating debug block scopes of 0-variable block scopes that come from deprecated let exprs inside generators. (r=jimb) (69cc9afe9)
- code style (dc626020e)
- Bug 1135703 - Ensure that lastProfilingFrame gets set appropriately on ALL JitActivations when profiling is turned on or off. r=shu (64665fe96)
- missing init, taken from TFF (dc9544999)
- pointer style (84478dba2)
- cleanup (6e2dd6ae3)
- reposition due to misspatch (738769d65)
- Bug 1191758 - Rework JS::FormatStackDump() to fix OOM handling r=terrence (680e5f8d2)
- Bug 1195545 - Add instruction reordering pass to IonMonkey, r=sunfish. (ba1441975)
2022-01-12 10:00:34 +08:00

329 lines
8.0 KiB
C++

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