1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsLocalFile.h" // includes platform-specific headers
11 #include "nsReadableUtils.h"
12 #include "nsPrintfCString.h"
14 #include "nsNativeCharsetUtils.h"
15 #include "nsUTF8Utils.h"
17 #include "nsLocalFileCommon.h"
23 // Extensions that should be considered 'executable', ie will not allow users
24 // to open immediately without first saving to disk, and potentially provoke
25 // other warnings. PLEASE read the longer comment in
26 // toolkit/components/reputationservice/ApplicationReputation.cpp
27 // before modifying this list!
28 // If you update this list, make sure to update the length of sExecutableExts
29 // in nsLocalFileCommmon.h.
31 const char* const sExecutableExts
[] = {
33 ".accda", // MS Access database
34 ".accdb", // MS Access database
35 ".accde", // MS Access database
36 ".accdr", // MS Access database
38 ".ade", // access project extension
40 ".afploc", // Apple Filing Protocol Location
41 ".air", // Adobe AIR installer
42 ".app", // executable application
43 ".application", // from bug 348763
44 ".appref-ms", // ClickOnce link
48 ".atloc", // Appletalk Location
51 ".cer", // Signed certificate file
58 ".diagcab", // Windows archive
60 ".fileloc", // Apple finder internet location data file
61 ".ftploc", // Apple FTP Location
62 ".fxp", // FoxPro compiled app
65 ".inetloc", // Apple finder internet location data file
69 ".jar", // java application bundle
76 ".mad", // Access Module Shortcut
78 ".mag", // Access Diagram Shortcut
79 ".mam", // Access Macro Shortcut
80 ".maq", // Access Query Shortcut
81 ".mar", // Access Report Shortcut
82 ".mas", // Access Stored Procedure
83 ".mat", // Access Table Shortcut
84 ".mau", // Media Attachment Unit
85 ".mav", // Access View Shortcut
86 ".maw", // Access Data Access Page
87 ".mda", // Access Add-in, MDA Access 2 Workgroup
90 ".mdt", // Access Add-in Data
91 ".mdw", // Access Workgroup Information
92 ".mdz", // Access Wizard Template
94 ".msh", // Microsoft Shell
95 ".msh1", // Microsoft Shell
96 ".msh1xml", // Microsoft Shell
97 ".msh2", // Microsoft Shell
98 ".msh2xml", // Microsoft Shell
99 ".mshxml", // Microsoft Shell
105 ".ops", // Office Profile Settings
108 ".plg", // Developer Studio Build Log
109 ".prf", // windows system file
113 ".scf", // Windows explorer command
116 ".settingcontent-ms",
127 ".vsmacros", // Visual Studio .NET Binary-based Macro Project
137 ".webloc", // MacOS website location file
142 ".xll" // MS Excel dynamic link library
146 #if !defined(MOZ_WIDGET_COCOA) && !defined(XP_WIN)
148 nsLocalFile::InitWithFile(nsIFile
* aFile
) {
149 if (NS_WARN_IF(!aFile
)) {
150 return NS_ERROR_INVALID_ARG
;
154 aFile
->GetNativePath(path
);
155 if (path
.IsEmpty()) {
156 return NS_ERROR_INVALID_ARG
;
158 return InitWithNativePath(path
);
162 #define kMaxFilenameLength 255
163 #define kMaxExtensionLength 100
164 #define kMaxSequenceNumberLength 5 // "-9999"
165 // requirement: kMaxExtensionLength <
166 // kMaxFilenameLength - kMaxSequenceNumberLength
169 nsLocalFile::CreateUnique(uint32_t aType
, uint32_t aAttributes
) {
174 nsAutoString pathName
, leafName
, rootName
, suffix
;
175 rv
= GetPath(pathName
);
177 nsAutoCString pathName
, leafName
, rootName
, suffix
;
178 rv
= GetNativePath(pathName
);
184 auto FailedBecauseExists
= [&](nsresult aRv
) {
185 if (aRv
== NS_ERROR_FILE_ACCESS_DENIED
) {
187 return NS_SUCCEEDED(Exists(&exists
)) && exists
;
189 return aRv
== NS_ERROR_FILE_ALREADY_EXISTS
;
193 (pathName
.Length() + kMaxSequenceNumberLength
> kMaxFilenameLength
);
195 rv
= Create(aType
, aAttributes
);
196 if (!FailedBecauseExists(rv
)) {
202 rv
= GetLeafName(leafName
);
207 const int32_t lastDot
= leafName
.RFindChar(char16_t('.'));
209 rv
= GetNativeLeafName(leafName
);
214 const int32_t lastDot
= leafName
.RFindChar('.');
217 if (lastDot
== kNotFound
) {
220 suffix
= Substring(leafName
, lastDot
); // include '.'
221 rootName
= Substring(leafName
, 0, lastDot
); // strip suffix and dot
225 int32_t maxRootLength
=
226 (kMaxFilenameLength
- (pathName
.Length() - leafName
.Length()) -
227 suffix
.Length() - kMaxSequenceNumberLength
);
229 // We cannot create an item inside a directory whose name is too long.
230 // Also, ensure that at least one character remains after we truncate
231 // the root name, as we don't want to end up with an empty leaf name.
232 if (maxRootLength
< 2) {
233 return NS_ERROR_FILE_UNRECOGNIZED_PATH
;
237 // ensure that we don't cut the name in mid-UTF16-character
238 rootName
.SetLength(NS_IS_LOW_SURROGATE(rootName
[maxRootLength
])
241 SetLeafName(rootName
+ suffix
);
243 if (NS_IsNativeUTF8()) {
244 // ensure that we don't cut the name in mid-UTF8-character
245 // (assume the name is valid UTF8 to begin with)
246 while (UTF8traits::isInSeq(rootName
[maxRootLength
])) {
250 // Another check to avoid ending up with an empty leaf name.
251 if (maxRootLength
== 0 && suffix
.IsEmpty()) {
252 return NS_ERROR_FILE_UNRECOGNIZED_PATH
;
256 rootName
.SetLength(maxRootLength
);
257 SetNativeLeafName(rootName
+ suffix
);
259 nsresult rvCreate
= Create(aType
, aAttributes
);
260 if (!FailedBecauseExists(rvCreate
)) {
265 for (int indx
= 1; indx
< 10000; ++indx
) {
266 // start with "Picture-1.jpg" after "Picture.jpg" exists
268 SetLeafName(rootName
+
269 NS_ConvertASCIItoUTF16(nsPrintfCString("-%d", indx
)) + suffix
);
271 SetNativeLeafName(rootName
+ nsPrintfCString("-%d", indx
) + suffix
);
273 rv
= Create(aType
, aAttributes
);
274 if (NS_SUCCEEDED(rv
) || !FailedBecauseExists(rv
)) {
279 // The disk is full, sort of
280 return NS_ERROR_FILE_TOO_BIG
;
284 static const char16_t kPathSeparatorChar
= '\\';
285 #elif defined(XP_UNIX)
286 static const char16_t kPathSeparatorChar
= '/';
288 # error Need to define file path separator for your platform
291 static void SplitPath(char16_t
* aPath
, nsTArray
<char16_t
*>& aNodeArray
) {
296 if (*aPath
== kPathSeparatorChar
) {
299 aNodeArray
.AppendElement(aPath
);
301 for (char16_t
* cp
= aPath
; *cp
!= 0; ++cp
) {
302 if (*cp
== kPathSeparatorChar
) {
307 aNodeArray
.AppendElement(cp
);
313 nsLocalFile::GetRelativeDescriptor(nsIFile
* aFromFile
, nsACString
& aResult
) {
314 if (NS_WARN_IF(!aFromFile
)) {
315 return NS_ERROR_INVALID_ARG
;
319 // aResult will be UTF-8 encoded
325 nsAutoString thisPath
, fromPath
;
326 AutoTArray
<char16_t
*, 32> thisNodes
;
327 AutoTArray
<char16_t
*, 32> fromNodes
;
329 rv
= GetPath(thisPath
);
333 rv
= aFromFile
->GetPath(fromPath
);
338 // get raw pointer to mutable string buffer
339 char16_t
* thisPathPtr
= thisPath
.BeginWriting();
340 char16_t
* fromPathPtr
= fromPath
.BeginWriting();
342 SplitPath(thisPathPtr
, thisNodes
);
343 SplitPath(fromPathPtr
, fromNodes
);
347 nodeIndex
< thisNodes
.Length() && nodeIndex
< fromNodes
.Length();
350 if (_wcsicmp(char16ptr_t(thisNodes
[nodeIndex
]),
351 char16ptr_t(fromNodes
[nodeIndex
]))) {
355 if (nsCRT::strcmp(thisNodes
[nodeIndex
], fromNodes
[nodeIndex
])) {
361 size_t branchIndex
= nodeIndex
;
362 for (nodeIndex
= branchIndex
; nodeIndex
< fromNodes
.Length(); ++nodeIndex
) {
363 aResult
.AppendLiteral("../");
365 StringJoinAppend(aResult
, "/"_ns
, mozilla::Span
{thisNodes
}.From(branchIndex
),
366 [](nsACString
& dest
, const auto& thisNode
) {
367 // XXX(Bug 1682869) We wouldn't need to reconstruct a
368 // nsDependentString here if SplitPath already returned
369 // nsDependentString. In fact, it seems SplitPath might be
370 // replaced by ParseString?
371 AppendUTF16toUTF8(nsDependentString
{thisNode
}, dest
);
378 nsLocalFile::SetRelativeDescriptor(nsIFile
* aFromFile
,
379 const nsACString
& aRelativeDesc
) {
380 constexpr auto kParentDirStr
= "../"_ns
;
382 nsCOMPtr
<nsIFile
> targetFile
;
383 nsresult rv
= aFromFile
->Clone(getter_AddRefs(targetFile
));
389 // aRelativeDesc is UTF-8 encoded
392 nsCString::const_iterator strBegin
, strEnd
;
393 aRelativeDesc
.BeginReading(strBegin
);
394 aRelativeDesc
.EndReading(strEnd
);
396 nsCString::const_iterator
nodeBegin(strBegin
), nodeEnd(strEnd
);
397 nsCString::const_iterator
pos(strBegin
);
399 nsCOMPtr
<nsIFile
> parentDir
;
400 while (FindInReadable(kParentDirStr
, nodeBegin
, nodeEnd
)) {
401 rv
= targetFile
->GetParent(getter_AddRefs(parentDir
));
406 return NS_ERROR_FILE_UNRECOGNIZED_PATH
;
408 targetFile
= parentDir
;
415 nodeBegin
= nodeEnd
= pos
;
416 while (nodeEnd
!= strEnd
) {
417 FindCharInReadable('/', nodeEnd
, strEnd
);
418 targetFile
->Append(NS_ConvertUTF8toUTF16(Substring(nodeBegin
, nodeEnd
)));
419 if (nodeEnd
!= strEnd
) { // If there's more left in the string, inc over
420 // the '/' nodeEnd is on.
426 return InitWithFile(targetFile
);
430 nsLocalFile::GetRelativePath(nsIFile
* aFromFile
, nsACString
& aResult
) {
431 return GetRelativeDescriptor(aFromFile
, aResult
);
435 nsLocalFile::SetRelativePath(nsIFile
* aFromFile
,
436 const nsACString
& aRelativePath
) {
437 return SetRelativeDescriptor(aFromFile
, aRelativePath
);