VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/io/nsLocalFileOSX.cpp@ 1396

Last change on this file since 1396 was 1396, checked in by vboxsync, 18 years ago

hack to fix RegistryLocationForSpec issues with /path/to/./components that causes IPC_Init to be called twice if doing auto registration.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 70.1 KB
Line 
1/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2001, 2002
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Conrad Carlen <[email protected]>
24 * Jungshik Shin <[email protected]>
25 * Asaf Romano <[email protected]>
26 * Mark Mentovai <[email protected]>
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * either the GNU General Public License Version 2 or later (the "GPL"), or
30 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the GPL or the LGPL are applicable instead
32 * of those above. If you wish to allow use of your version of this file only
33 * under the terms of either the GPL or the LGPL, and not to allow others to
34 * use your version of this file under the terms of the MPL, indicate your
35 * decision by deleting the provisions above and replace them with the notice
36 * and other provisions required by the GPL or the LGPL. If you do not delete
37 * the provisions above, a recipient may use your version of this file under
38 * the terms of any one of the MPL, the GPL or the LGPL.
39 *
40 * ***** END LICENSE BLOCK ***** */
41
42#include "nsLocalFile.h"
43#include "nsDirectoryServiceDefs.h"
44
45#include "nsString.h"
46#include "nsReadableUtils.h"
47#include "nsIDirectoryEnumerator.h"
48#include "nsISimpleEnumerator.h"
49#include "nsITimelineService.h"
50#include "nsVoidArray.h"
51
52#include "plbase64.h"
53#include "prmem.h"
54#include "nsCRT.h"
55#include "nsHashKeys.h"
56
57#include "MoreFilesX.h"
58#include "FSCopyObject.h"
59#include "nsAutoBuffer.h"
60#include "nsTraceRefcntImpl.h"
61
62// Mac Includes
63#include <Carbon/Carbon.h>
64
65// Unix Includes
66#include <unistd.h>
67#include <sys/stat.h>
68#include <stdlib.h>
69
70#if !defined(MAC_OS_X_VERSION_10_4) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4
71#define GetAliasSizeFromRecord(aliasRecord) aliasRecord.aliasSize
72#else
73#define GetAliasSizeFromRecord(aliasRecord) GetAliasSizeFromPtr(&aliasRecord)
74#endif
75
76#define CHECK_mBaseRef() \
77 PR_BEGIN_MACRO \
78 if (!mBaseRef) \
79 return NS_ERROR_NOT_INITIALIZED; \
80 PR_END_MACRO
81
82//*****************************************************************************
83// Static Function Prototypes
84//*****************************************************************************
85
86static nsresult MacErrorMapper(OSErr inErr);
87static OSErr FindRunningAppBySignature(OSType aAppSig, ProcessSerialNumber& outPsn);
88static void CopyUTF8toUTF16NFC(const nsACString& aSrc, nsAString& aResult);
89
90//*****************************************************************************
91// Local Helper Classes
92//*****************************************************************************
93
94#pragma mark -
95#pragma mark [FSRef operator==]
96
97bool operator==(const FSRef& lhs, const FSRef& rhs)
98{
99 return (::FSCompareFSRefs(&lhs, &rhs) == noErr);
100}
101
102#pragma mark -
103#pragma mark [StFollowLinksState]
104
105class StFollowLinksState
106{
107 public:
108 StFollowLinksState(nsLocalFile& aFile) :
109 mFile(aFile)
110 {
111 mFile.GetFollowLinks(&mSavedState);
112 }
113
114 StFollowLinksState(nsLocalFile& aFile, PRBool followLinksState) :
115 mFile(aFile)
116 {
117 mFile.GetFollowLinks(&mSavedState);
118 mFile.SetFollowLinks(followLinksState);
119 }
120
121 ~StFollowLinksState()
122 {
123 mFile.SetFollowLinks(mSavedState);
124 }
125
126 private:
127 nsLocalFile& mFile;
128 PRBool mSavedState;
129};
130
131#pragma mark -
132#pragma mark [nsDirEnumerator]
133
134class nsDirEnumerator : public nsISimpleEnumerator,
135 public nsIDirectoryEnumerator
136{
137 public:
138
139 NS_DECL_ISUPPORTS
140
141 nsDirEnumerator() :
142 mIterator(nsnull),
143 mFSRefsArray(nsnull),
144 mArrayCnt(0), mArrayIndex(0)
145 {
146 }
147
148 nsresult Init(nsILocalFileMac* parent)
149 {
150 NS_ENSURE_ARG(parent);
151
152 OSErr err;
153 nsresult rv;
154 FSRef parentRef;
155
156 rv = parent->GetFSRef(&parentRef);
157 if (NS_FAILED(rv))
158 return rv;
159
160 mFSRefsArray = (FSRef *)nsMemory::Alloc(sizeof(FSRef)
161 * kRequestCountPerIteration);
162 if (!mFSRefsArray)
163 return NS_ERROR_OUT_OF_MEMORY;
164
165 err = ::FSOpenIterator(&parentRef, kFSIterateFlat, &mIterator);
166 if (err != noErr)
167 return MacErrorMapper(err);
168
169 return NS_OK;
170 }
171
172 NS_IMETHOD HasMoreElements(PRBool *result)
173 {
174 if (mNext == nsnull) {
175 if (mArrayIndex >= mArrayCnt) {
176 ItemCount actualCnt;
177 OSErr err = ::FSGetCatalogInfoBulk(mIterator,
178 kRequestCountPerIteration,
179 &actualCnt,
180 nsnull,
181 kFSCatInfoNone,
182 nsnull,
183 mFSRefsArray,
184 nsnull,
185 nsnull);
186
187 if (err == noErr || err == errFSNoMoreItems) {
188 mArrayCnt = actualCnt;
189 mArrayIndex = 0;
190 }
191 }
192 if (mArrayIndex < mArrayCnt) {
193 nsLocalFile *newFile = new nsLocalFile;
194 if (!newFile)
195 return NS_ERROR_OUT_OF_MEMORY;
196 FSRef fsRef = mFSRefsArray[mArrayIndex];
197 if (NS_FAILED(newFile->InitWithFSRef(&fsRef)))
198 return NS_ERROR_FAILURE;
199 mArrayIndex++;
200 mNext = newFile;
201 }
202 }
203 *result = mNext != nsnull;
204 if (!*result)
205 Close();
206 return NS_OK;
207 }
208
209 NS_IMETHOD GetNext(nsISupports **result)
210 {
211 NS_ENSURE_ARG_POINTER(result);
212 *result = nsnull;
213
214 nsresult rv;
215 PRBool hasMore;
216 rv = HasMoreElements(&hasMore);
217 if (NS_FAILED(rv)) return rv;
218
219 *result = mNext; // might return nsnull
220 NS_IF_ADDREF(*result);
221
222 mNext = nsnull;
223 return NS_OK;
224 }
225
226 NS_IMETHOD GetNextFile(nsIFile **result)
227 {
228 *result = nsnull;
229 PRBool hasMore = PR_FALSE;
230 nsresult rv = HasMoreElements(&hasMore);
231 if (NS_FAILED(rv) || !hasMore)
232 return rv;
233 *result = mNext;
234 NS_IF_ADDREF(*result);
235 mNext = nsnull;
236 return NS_OK;
237 }
238
239 NS_IMETHOD Close()
240 {
241 if (mIterator) {
242 ::FSCloseIterator(mIterator);
243 mIterator = nsnull;
244 }
245 if (mFSRefsArray) {
246 nsMemory::Free(mFSRefsArray);
247 mFSRefsArray = nsnull;
248 }
249 return NS_OK;
250 }
251
252 private:
253 ~nsDirEnumerator()
254 {
255 Close();
256 }
257
258 protected:
259 // According to Apple doc, request the number of objects
260 // per call that will fit in 4 VM pages.
261 enum {
262 kRequestCountPerIteration = ((4096 * 4) / sizeof(FSRef))
263 };
264
265 nsCOMPtr<nsILocalFileMac> mNext;
266
267 FSIterator mIterator;
268 FSRef *mFSRefsArray;
269 PRInt32 mArrayCnt, mArrayIndex;
270};
271
272NS_IMPL_ISUPPORTS2(nsDirEnumerator, nsISimpleEnumerator, nsIDirectoryEnumerator)
273
274#pragma mark -
275#pragma mark [StAEDesc]
276
277class StAEDesc: public AEDesc
278{
279public:
280 StAEDesc()
281 {
282 descriptorType = typeNull;
283 dataHandle = nil;
284 }
285
286 ~StAEDesc()
287 {
288 ::AEDisposeDesc(this);
289 }
290};
291
292#define FILENAME_BUFFER_SIZE 512
293
294//*****************************************************************************
295// nsLocalFile
296//*****************************************************************************
297
298const char nsLocalFile::kPathSepChar = '/';
299const PRUnichar nsLocalFile::kPathSepUnichar = '/';
300
301// The HFS+ epoch is Jan. 1, 1904 GMT - differs from HFS in which times were local
302// The NSPR epoch is Jan. 1, 1970 GMT
303// 2082844800 is the difference in seconds between those dates
304const PRInt64 nsLocalFile::kJanuaryFirst1970Seconds = 2082844800LL;
305
306#pragma mark -
307#pragma mark [CTORs/DTOR]
308
309nsLocalFile::nsLocalFile() :
310 mBaseRef(nsnull),
311 mTargetRef(nsnull),
312 mCachedFSRefValid(PR_FALSE),
313 mFollowLinks(PR_TRUE),
314 mFollowLinksDirty(PR_TRUE)
315{
316}
317
318nsLocalFile::nsLocalFile(const nsLocalFile& src) :
319 mBaseRef(src.mBaseRef),
320 mTargetRef(src.mTargetRef),
321 mCachedFSRef(src.mCachedFSRef),
322 mCachedFSRefValid(src.mCachedFSRefValid),
323 mFollowLinks(src.mFollowLinks),
324 mFollowLinksDirty(src.mFollowLinksDirty)
325{
326 // A CFURLRef is immutable so no need to copy, just retain.
327 if (mBaseRef)
328 ::CFRetain(mBaseRef);
329 if (mTargetRef)
330 ::CFRetain(mTargetRef);
331}
332
333nsLocalFile::~nsLocalFile()
334{
335 if (mBaseRef)
336 ::CFRelease(mBaseRef);
337 if (mTargetRef)
338 ::CFRelease(mTargetRef);
339}
340
341
342//*****************************************************************************
343// nsLocalFile::nsISupports
344//*****************************************************************************
345#pragma mark -
346#pragma mark [nsISupports]
347
348NS_IMPL_THREADSAFE_ISUPPORTS4(nsLocalFile,
349 nsILocalFileMac,
350 nsILocalFile,
351 nsIFile,
352 nsIHashable)
353
354NS_METHOD nsLocalFile::nsLocalFileConstructor(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
355{
356 NS_ENSURE_ARG_POINTER(aInstancePtr);
357 NS_ENSURE_NO_AGGREGATION(outer);
358
359 nsLocalFile* inst = new nsLocalFile();
360 if (inst == NULL)
361 return NS_ERROR_OUT_OF_MEMORY;
362
363 nsresult rv = inst->QueryInterface(aIID, aInstancePtr);
364 if (NS_FAILED(rv))
365 {
366 delete inst;
367 return rv;
368 }
369 return NS_OK;
370}
371
372
373//*****************************************************************************
374// nsLocalFile::nsIFile
375//*****************************************************************************
376#pragma mark -
377#pragma mark [nsIFile]
378
379/* void append (in AString node); */
380NS_IMETHODIMP nsLocalFile::Append(const nsAString& aNode)
381{
382 return AppendNative(NS_ConvertUTF16toUTF8(aNode));
383}
384
385/* [noscript] void appendNative (in ACString node); */
386NS_IMETHODIMP nsLocalFile::AppendNative(const nsACString& aNode)
387{
388 // Check we are correctly initialized.
389 CHECK_mBaseRef();
390
391 nsACString::const_iterator start, end;
392 aNode.BeginReading(start);
393 aNode.EndReading(end);
394 if (FindCharInReadable(kPathSepChar, start, end))
395 return NS_ERROR_FILE_UNRECOGNIZED_PATH;
396
397 CFStringRef nodeStrRef = ::CFStringCreateWithCString(kCFAllocatorDefault,
398 PromiseFlatCString(aNode).get(),
399 kCFStringEncodingUTF8);
400 if (nodeStrRef) {
401 CFURLRef newRef = ::CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,
402 mBaseRef, nodeStrRef, PR_FALSE);
403 ::CFRelease(nodeStrRef);
404 if (newRef) {
405 SetBaseRef(newRef);
406 ::CFRelease(newRef);
407 return NS_OK;
408 }
409 }
410 return NS_ERROR_FAILURE;
411}
412
413/* void normalize (); */
414NS_IMETHODIMP nsLocalFile::Normalize()
415{
416 // Check we are correctly initialized.
417 CHECK_mBaseRef();
418
419 // CFURL doesn't doesn't seem to resolve paths containing relative
420 // components, so we'll nick the stdlib code from nsLocalFileUnix
421 UInt8 path[PATH_MAX] = "";
422 Boolean success;
423 success = ::CFURLGetFileSystemRepresentation(mBaseRef, true, path, PATH_MAX);
424 if (!success)
425 return NS_ERROR_FAILURE;
426
427 char resolved_path[PATH_MAX] = "";
428 char *resolved_path_ptr = nsnull;
429 resolved_path_ptr = realpath((char*)path, resolved_path);
430
431 // if there is an error, the return is null.
432 if (!resolved_path_ptr)
433 return NSRESULT_FOR_ERRNO();
434
435 // Need to know whether we're a directory to create a new CFURLRef
436 PRBool isDirectory;
437 nsresult rv = IsDirectory(&isDirectory);
438 NS_ENSURE_SUCCESS(rv, rv);
439
440 rv = NS_ERROR_FAILURE;
441 CFStringRef pathStrRef =
442 ::CFStringCreateWithCString(kCFAllocatorDefault,
443 resolved_path,
444 kCFStringEncodingUTF8);
445 if (pathStrRef) {
446 CFURLRef newURLRef =
447 ::CFURLCreateWithFileSystemPath(kCFAllocatorDefault, pathStrRef,
448 kCFURLPOSIXPathStyle, isDirectory);
449 if (newURLRef) {
450 SetBaseRef(newURLRef);
451 ::CFRelease(newURLRef);
452 rv = NS_OK;
453 }
454 ::CFRelease(pathStrRef);
455 }
456
457 return rv;
458}
459
460/* void create (in unsigned long type, in unsigned long permissions); */
461NS_IMETHODIMP nsLocalFile::Create(PRUint32 type, PRUint32 permissions)
462{
463 if (type != NORMAL_FILE_TYPE && type != DIRECTORY_TYPE)
464 return NS_ERROR_FILE_UNKNOWN_TYPE;
465
466 // Check we are correctly initialized.
467 CHECK_mBaseRef();
468
469 nsStringArray nonExtantNodes;
470 CFURLRef pathURLRef = mBaseRef;
471 FSRef pathFSRef;
472 CFStringRef leafStrRef = nsnull;
473 nsAutoBuffer<UniChar, FILENAME_BUFFER_SIZE> buffer;
474 Boolean success;
475
476 // Work backwards through the path to find the last node which
477 // exists. Place the nodes which don't exist in an array and we'll
478 // create those below.
479 while ((success = ::CFURLGetFSRef(pathURLRef, &pathFSRef)) == false) {
480 leafStrRef = ::CFURLCopyLastPathComponent(pathURLRef);
481 if (!leafStrRef)
482 break;
483 CFIndex leafLen = ::CFStringGetLength(leafStrRef);
484 if (!buffer.EnsureElemCapacity(leafLen + 1))
485 break;
486 ::CFStringGetCharacters(leafStrRef, CFRangeMake(0, leafLen), buffer.get());
487 buffer.get()[leafLen] = '\0';
488 nonExtantNodes.AppendString(nsString(nsDependentString(buffer.get())));
489 ::CFRelease(leafStrRef);
490 leafStrRef = nsnull;
491
492 // Get the parent of the leaf for the next go round
493 CFURLRef parent = ::CFURLCreateCopyDeletingLastPathComponent(NULL, pathURLRef);
494 if (!parent)
495 break;
496 if (pathURLRef != mBaseRef)
497 ::CFRelease(pathURLRef);
498 pathURLRef = parent;
499 }
500 if (pathURLRef != mBaseRef)
501 ::CFRelease(pathURLRef);
502 if (leafStrRef != nsnull)
503 ::CFRelease(leafStrRef);
504 if (!success)
505 return NS_ERROR_FAILURE;
506 PRInt32 nodesToCreate = nonExtantNodes.Count();
507 if (nodesToCreate == 0)
508 return NS_ERROR_FILE_ALREADY_EXISTS;
509
510 OSErr err;
511 nsAutoString nextNodeName;
512 for (PRInt32 i = nodesToCreate - 1; i > 0; i--) {
513 nonExtantNodes.StringAt(i, nextNodeName);
514 err = ::FSCreateDirectoryUnicode(&pathFSRef,
515 nextNodeName.Length(),
516 (const UniChar *)nextNodeName.get(),
517 kFSCatInfoNone,
518 nsnull, &pathFSRef, nsnull, nsnull);
519 if (err != noErr)
520 return MacErrorMapper(err);
521 }
522 nonExtantNodes.StringAt(0, nextNodeName);
523 if (type == NORMAL_FILE_TYPE) {
524 err = ::FSCreateFileUnicode(&pathFSRef,
525 nextNodeName.Length(),
526 (const UniChar *)nextNodeName.get(),
527 kFSCatInfoNone,
528 nsnull, nsnull, nsnull);
529 }
530 else {
531 err = ::FSCreateDirectoryUnicode(&pathFSRef,
532 nextNodeName.Length(),
533 (const UniChar *)nextNodeName.get(),
534 kFSCatInfoNone,
535 nsnull, nsnull, nsnull, nsnull);
536 }
537
538 return MacErrorMapper(err);
539}
540
541/* attribute AString leafName; */
542NS_IMETHODIMP nsLocalFile::GetLeafName(nsAString& aLeafName)
543{
544 nsCAutoString nativeString;
545 nsresult rv = GetNativeLeafName(nativeString);
546 if (NS_FAILED(rv))
547 return rv;
548 CopyUTF8toUTF16NFC(nativeString, aLeafName);
549 return NS_OK;
550}
551
552NS_IMETHODIMP nsLocalFile::SetLeafName(const nsAString& aLeafName)
553{
554 return SetNativeLeafName(NS_ConvertUTF16toUTF8(aLeafName));
555}
556
557/* [noscript] attribute ACString nativeLeafName; */
558NS_IMETHODIMP nsLocalFile::GetNativeLeafName(nsACString& aNativeLeafName)
559{
560 // Check we are correctly initialized.
561 CHECK_mBaseRef();
562
563 nsresult rv = NS_ERROR_FAILURE;
564 CFStringRef leafStrRef = ::CFURLCopyLastPathComponent(mBaseRef);
565 if (leafStrRef) {
566 rv = CFStringReftoUTF8(leafStrRef, aNativeLeafName);
567 ::CFRelease(leafStrRef);
568 }
569 return rv;
570}
571
572NS_IMETHODIMP nsLocalFile::SetNativeLeafName(const nsACString& aNativeLeafName)
573{
574 // Check we are correctly initialized.
575 CHECK_mBaseRef();
576
577 nsresult rv = NS_ERROR_FAILURE;
578 CFURLRef parentURLRef = ::CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorDefault, mBaseRef);
579 if (parentURLRef) {
580 CFStringRef nodeStrRef = ::CFStringCreateWithCString(kCFAllocatorDefault,
581 PromiseFlatCString(aNativeLeafName).get(),
582 kCFStringEncodingUTF8);
583
584 if (nodeStrRef) {
585 CFURLRef newURLRef = ::CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,
586 parentURLRef, nodeStrRef, PR_FALSE);
587 if (newURLRef) {
588 SetBaseRef(newURLRef);
589 ::CFRelease(newURLRef);
590 rv = NS_OK;
591 }
592 ::CFRelease(nodeStrRef);
593 }
594 ::CFRelease(parentURLRef);
595 }
596 return rv;
597}
598
599/* void copyTo (in nsIFile newParentDir, in AString newName); */
600NS_IMETHODIMP nsLocalFile::CopyTo(nsIFile *newParentDir, const nsAString& newName)
601{
602 return CopyInternal(newParentDir, newName, PR_FALSE);
603}
604
605/* [noscrpit] void CopyToNative (in nsIFile newParentDir, in ACString newName); */
606NS_IMETHODIMP nsLocalFile::CopyToNative(nsIFile *newParentDir, const nsACString& newName)
607{
608 return CopyInternal(newParentDir, NS_ConvertUTF8toUTF16(newName), PR_FALSE);
609}
610
611/* void copyToFollowingLinks (in nsIFile newParentDir, in AString newName); */
612NS_IMETHODIMP nsLocalFile::CopyToFollowingLinks(nsIFile *newParentDir, const nsAString& newName)
613{
614 return CopyInternal(newParentDir, newName, PR_TRUE);
615}
616
617/* [noscript] void copyToFollowingLinksNative (in nsIFile newParentDir, in ACString newName); */
618NS_IMETHODIMP nsLocalFile::CopyToFollowingLinksNative(nsIFile *newParentDir, const nsACString& newName)
619{
620 return CopyInternal(newParentDir, NS_ConvertUTF8toUTF16(newName), PR_TRUE);
621}
622
623/* void moveTo (in nsIFile newParentDir, in AString newName); */
624NS_IMETHODIMP nsLocalFile::MoveTo(nsIFile *newParentDir, const nsAString& newName)
625{
626 return MoveToNative(newParentDir, NS_ConvertUTF16toUTF8(newName));
627}
628
629/* [noscript] void moveToNative (in nsIFile newParentDir, in ACString newName); */
630NS_IMETHODIMP nsLocalFile::MoveToNative(nsIFile *newParentDir, const nsACString& newName)
631{
632 // Check we are correctly initialized.
633 CHECK_mBaseRef();
634
635 StFollowLinksState followLinks(*this, PR_FALSE);
636
637 PRBool isDirectory;
638 nsresult rv = IsDirectory(&isDirectory);
639 if (NS_FAILED(rv))
640 return rv;
641
642 // Get the source path.
643 nsCAutoString srcPath;
644 rv = GetNativePath(srcPath);
645 if (NS_FAILED(rv))
646 return rv;
647
648 // Build the destination path.
649 nsCOMPtr<nsIFile> parentDir = newParentDir;
650 if (!parentDir) {
651 if (newName.IsEmpty())
652 return NS_ERROR_INVALID_ARG;
653 rv = GetParent(getter_AddRefs(parentDir));
654 if (NS_FAILED(rv))
655 return rv;
656 }
657 else {
658 PRBool exists;
659 rv = parentDir->Exists(&exists);
660 if (NS_FAILED(rv))
661 return rv;
662 if (!exists) {
663 rv = parentDir->Create(nsIFile::DIRECTORY_TYPE, 0777);
664 if (NS_FAILED(rv))
665 return rv;
666 }
667 }
668
669 nsCAutoString destPath;
670 rv = parentDir->GetNativePath(destPath);
671 if (NS_FAILED(rv))
672 return rv;
673
674 if (!newName.IsEmpty())
675 destPath.Append(NS_LITERAL_CSTRING("/") + newName);
676 else {
677 nsCAutoString leafName;
678 rv = GetNativeLeafName(leafName);
679 if (NS_FAILED(rv))
680 return rv;
681 destPath.Append(NS_LITERAL_CSTRING("/") + leafName);
682 }
683
684 // Perform the move.
685 if (rename(srcPath.get(), destPath.get()) != 0) {
686 if (errno == EXDEV) {
687 // Can't move across volume (device) boundaries. Copy and remove.
688 rv = CopyToNative(parentDir, newName);
689 if (NS_SUCCEEDED(rv)) {
690 // Permit removal failure.
691 Remove(PR_TRUE);
692 }
693 }
694 else
695 rv = NSRESULT_FOR_ERRNO();
696
697 if (NS_FAILED(rv))
698 return rv;
699 }
700
701 // Update |this| to refer to the moved file.
702 CFURLRef newBaseRef =
703 ::CFURLCreateFromFileSystemRepresentation(NULL, (UInt8*)destPath.get(),
704 destPath.Length(), isDirectory);
705 if (!newBaseRef)
706 return NS_ERROR_FAILURE;
707 SetBaseRef(newBaseRef);
708 ::CFRelease(newBaseRef);
709
710 return rv;
711}
712
713/* void remove (in boolean recursive); */
714NS_IMETHODIMP nsLocalFile::Remove(PRBool recursive)
715{
716 // Check we are correctly initialized.
717 CHECK_mBaseRef();
718
719 // XXX If we're an alias, never remove target
720 StFollowLinksState followLinks(*this, PR_FALSE);
721
722 PRBool isDirectory;
723 nsresult rv = IsDirectory(&isDirectory);
724 if (NS_FAILED(rv))
725 return rv;
726
727 if (recursive && isDirectory) {
728 FSRef fsRef;
729 rv = GetFSRefInternal(fsRef);
730 if (NS_FAILED(rv))
731 return rv;
732
733 // Call MoreFilesX to do a recursive removal.
734 OSStatus err = ::FSDeleteContainer(&fsRef);
735 rv = MacErrorMapper(err);
736 }
737 else {
738 nsCAutoString path;
739 rv = GetNativePath(path);
740 if (NS_FAILED(rv))
741 return rv;
742
743 const char* pathPtr = path.get();
744 int status;
745 if (isDirectory)
746 status = rmdir(pathPtr);
747 else
748 status = unlink(pathPtr);
749
750 if (status != 0)
751 rv = NSRESULT_FOR_ERRNO();
752 }
753
754 mCachedFSRefValid = PR_FALSE;
755 return rv;
756}
757
758/* attribute unsigned long permissions; */
759NS_IMETHODIMP nsLocalFile::GetPermissions(PRUint32 *aPermissions)
760{
761 NS_ENSURE_ARG_POINTER(aPermissions);
762
763 FSRef fsRef;
764 nsresult rv = GetFSRefInternal(fsRef);
765 if (NS_FAILED(rv))
766 return rv;
767
768 FSCatalogInfo catalogInfo;
769 OSErr err = ::FSGetCatalogInfo(&fsRef, kFSCatInfoPermissions, &catalogInfo,
770 nsnull, nsnull, nsnull);
771 if (err != noErr)
772 return MacErrorMapper(err);
773 FSPermissionInfo *permPtr = (FSPermissionInfo*)catalogInfo.permissions;
774 *aPermissions = permPtr->mode;
775 return NS_OK;
776}
777
778NS_IMETHODIMP nsLocalFile::SetPermissions(PRUint32 aPermissions)
779{
780 FSRef fsRef;
781 nsresult rv = GetFSRefInternal(fsRef);
782 if (NS_FAILED(rv))
783 return rv;
784
785 FSCatalogInfo catalogInfo;
786 OSErr err = ::FSGetCatalogInfo(&fsRef, kFSCatInfoPermissions, &catalogInfo,
787 nsnull, nsnull, nsnull);
788 if (err != noErr)
789 return MacErrorMapper(err);
790 FSPermissionInfo *permPtr = (FSPermissionInfo*)catalogInfo.permissions;
791 permPtr->mode = (UInt16)aPermissions;
792 err = ::FSSetCatalogInfo(&fsRef, kFSCatInfoPermissions, &catalogInfo);
793 return MacErrorMapper(err);
794}
795
796/* attribute unsigned long permissionsOfLink; */
797NS_IMETHODIMP nsLocalFile::GetPermissionsOfLink(PRUint32 *aPermissionsOfLink)
798{
799 NS_ERROR("NS_ERROR_NOT_IMPLEMENTED");
800 return NS_ERROR_NOT_IMPLEMENTED;
801}
802
803NS_IMETHODIMP nsLocalFile::SetPermissionsOfLink(PRUint32 aPermissionsOfLink)
804{
805 NS_ERROR("NS_ERROR_NOT_IMPLEMENTED");
806 return NS_ERROR_NOT_IMPLEMENTED;
807}
808
809/* attribute PRInt64 lastModifiedTime; */
810NS_IMETHODIMP nsLocalFile::GetLastModifiedTime(PRInt64 *aLastModifiedTime)
811{
812 // Check we are correctly initialized.
813 CHECK_mBaseRef();
814
815 NS_ENSURE_ARG_POINTER(aLastModifiedTime);
816
817 FSRef fsRef;
818 nsresult rv = GetFSRefInternal(fsRef);
819 if (NS_FAILED(rv))
820 return rv;
821
822 FSCatalogInfo catalogInfo;
823 OSErr err = ::FSGetCatalogInfo(&fsRef, kFSCatInfoContentMod, &catalogInfo,
824 nsnull, nsnull, nsnull);
825 if (err != noErr)
826 return MacErrorMapper(err);
827 *aLastModifiedTime = HFSPlustoNSPRTime(catalogInfo.contentModDate);
828 return NS_OK;
829}
830
831NS_IMETHODIMP nsLocalFile::SetLastModifiedTime(PRInt64 aLastModifiedTime)
832{
833 // Check we are correctly initialized.
834 CHECK_mBaseRef();
835
836 OSErr err;
837 nsresult rv;
838 FSRef fsRef;
839 FSCatalogInfo catalogInfo;
840
841 rv = GetFSRefInternal(fsRef);
842 if (NS_FAILED(rv))
843 return rv;
844
845 FSRef parentRef;
846 PRBool notifyParent;
847
848 /* Get the node flags, the content modification date and time, and the parent ref */
849 err = ::FSGetCatalogInfo(&fsRef, kFSCatInfoNodeFlags + kFSCatInfoContentMod,
850 &catalogInfo, NULL, NULL, &parentRef);
851 if (err != noErr)
852 return MacErrorMapper(err);
853
854 /* Notify the parent if this is a file */
855 notifyParent = (0 == (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask));
856
857 NSPRtoHFSPlusTime(aLastModifiedTime, catalogInfo.contentModDate);
858 err = ::FSSetCatalogInfo(&fsRef, kFSCatInfoContentMod, &catalogInfo);
859 if (err != noErr)
860 return MacErrorMapper(err);
861
862 /* Send a notification for the parent of the file, or for the directory */
863 err = FNNotify(notifyParent ? &parentRef : &fsRef, kFNDirectoryModifiedMessage, kNilOptions);
864 if (err != noErr)
865 return MacErrorMapper(err);
866
867 return NS_OK;
868}
869
870/* attribute PRInt64 lastModifiedTimeOfLink; */
871NS_IMETHODIMP nsLocalFile::GetLastModifiedTimeOfLink(PRInt64 *aLastModifiedTimeOfLink)
872{
873 NS_ERROR("NS_ERROR_NOT_IMPLEMENTED");
874 return NS_ERROR_NOT_IMPLEMENTED;
875}
876NS_IMETHODIMP nsLocalFile::SetLastModifiedTimeOfLink(PRInt64 aLastModifiedTimeOfLink)
877{
878 NS_ERROR("NS_ERROR_NOT_IMPLEMENTED");
879 return NS_ERROR_NOT_IMPLEMENTED;
880}
881
882/* attribute PRInt64 fileSize; */
883NS_IMETHODIMP nsLocalFile::GetFileSize(PRInt64 *aFileSize)
884{
885 NS_ENSURE_ARG_POINTER(aFileSize);
886 *aFileSize = 0;
887
888 FSRef fsRef;
889 nsresult rv = GetFSRefInternal(fsRef);
890 if (NS_FAILED(rv))
891 return rv;
892
893 FSCatalogInfo catalogInfo;
894 OSErr err = ::FSGetCatalogInfo(&fsRef, kFSCatInfoNodeFlags + kFSCatInfoDataSizes, &catalogInfo,
895 nsnull, nsnull, nsnull);
896 if (err != noErr)
897 return MacErrorMapper(err);
898
899 // FSGetCatalogInfo can return a bogus size for directories sometimes, so only
900 // rely on the answer for files
901 if ((catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) == 0)
902 *aFileSize = catalogInfo.dataLogicalSize;
903 return NS_OK;
904}
905
906NS_IMETHODIMP nsLocalFile::SetFileSize(PRInt64 aFileSize)
907{
908 // Check we are correctly initialized.
909 CHECK_mBaseRef();
910
911 FSRef fsRef;
912 nsresult rv = GetFSRefInternal(fsRef);
913 if (NS_FAILED(rv))
914 return rv;
915
916 SInt16 refNum;
917 OSErr err = ::FSOpenFork(&fsRef, 0, nsnull, fsWrPerm, &refNum);
918 if (err != noErr)
919 return MacErrorMapper(err);
920 err = ::FSSetForkSize(refNum, fsFromStart, aFileSize);
921 ::FSCloseFork(refNum);
922
923 return MacErrorMapper(err);
924}
925
926/* readonly attribute PRInt64 fileSizeOfLink; */
927NS_IMETHODIMP nsLocalFile::GetFileSizeOfLink(PRInt64 *aFileSizeOfLink)
928{
929 // Check we are correctly initialized.
930 CHECK_mBaseRef();
931
932 NS_ENSURE_ARG_POINTER(aFileSizeOfLink);
933
934 StFollowLinksState followLinks(*this, PR_FALSE);
935 return GetFileSize(aFileSizeOfLink);
936}
937
938/* readonly attribute AString target; */
939NS_IMETHODIMP nsLocalFile::GetTarget(nsAString& aTarget)
940{
941 NS_ERROR("NS_ERROR_NOT_IMPLEMENTED");
942 return NS_ERROR_NOT_IMPLEMENTED;
943}
944
945/* [noscript] readonly attribute ACString nativeTarget; */
946NS_IMETHODIMP nsLocalFile::GetNativeTarget(nsACString& aNativeTarget)
947{
948 NS_ERROR("NS_ERROR_NOT_IMPLEMENTED");
949 return NS_ERROR_NOT_IMPLEMENTED;
950}
951
952/* readonly attribute AString path; */
953NS_IMETHODIMP nsLocalFile::GetPath(nsAString& aPath)
954{
955 nsCAutoString nativeString;
956 nsresult rv = GetNativePath(nativeString);
957 if (NS_FAILED(rv))
958 return rv;
959 CopyUTF8toUTF16NFC(nativeString, aPath);
960 return NS_OK;
961}
962
963/* [noscript] readonly attribute ACString nativePath; */
964NS_IMETHODIMP nsLocalFile::GetNativePath(nsACString& aNativePath)
965{
966 // Check we are correctly initialized.
967 CHECK_mBaseRef();
968
969 nsresult rv = NS_ERROR_FAILURE;
970 CFStringRef pathStrRef = ::CFURLCopyFileSystemPath(mBaseRef, kCFURLPOSIXPathStyle);
971 if (pathStrRef) {
972 rv = CFStringReftoUTF8(pathStrRef, aNativePath);
973 ::CFRelease(pathStrRef);
974 }
975 return rv;
976}
977
978/* boolean exists (); */
979NS_IMETHODIMP nsLocalFile::Exists(PRBool *_retval)
980{
981 // Check we are correctly initialized.
982 CHECK_mBaseRef();
983
984 NS_ENSURE_ARG_POINTER(_retval);
985 *_retval = PR_FALSE;
986
987 FSRef fsRef;
988 if (NS_SUCCEEDED(GetFSRefInternal(fsRef, PR_TRUE))) {
989 *_retval = PR_TRUE;
990 }
991
992 return NS_OK;
993}
994
995/* boolean isWritable (); */
996NS_IMETHODIMP nsLocalFile::IsWritable(PRBool *_retval)
997{
998 // Check we are correctly initialized.
999 CHECK_mBaseRef();
1000
1001 NS_ENSURE_ARG_POINTER(_retval);
1002 *_retval = PR_FALSE;
1003
1004 FSRef fsRef;
1005 nsresult rv = GetFSRefInternal(fsRef);
1006 if (NS_FAILED(rv))
1007 return rv;
1008 if (::FSCheckLock(&fsRef) == noErr) {
1009 PRUint32 permissions;
1010 rv = GetPermissions(&permissions);
1011 if (NS_FAILED(rv))
1012 return rv;
1013 *_retval = ((permissions & S_IWUSR) != 0);
1014 }
1015 return NS_OK;
1016}
1017
1018/* boolean isReadable (); */
1019NS_IMETHODIMP nsLocalFile::IsReadable(PRBool *_retval)
1020{
1021 // Check we are correctly initialized.
1022 CHECK_mBaseRef();
1023
1024 NS_ENSURE_ARG_POINTER(_retval);
1025 *_retval = PR_FALSE;
1026
1027 PRUint32 permissions;
1028 nsresult rv = GetPermissions(&permissions);
1029 if (NS_FAILED(rv))
1030 return rv;
1031 *_retval = ((permissions & S_IRUSR) != 0);
1032 return NS_OK;
1033}
1034
1035/* boolean isExecutable (); */
1036NS_IMETHODIMP nsLocalFile::IsExecutable(PRBool *_retval)
1037{
1038 // Check we are correctly initialized.
1039 CHECK_mBaseRef();
1040
1041 NS_ENSURE_ARG_POINTER(_retval);
1042 *_retval = PR_FALSE;
1043
1044 FSRef fsRef;
1045 nsresult rv = GetFSRefInternal(fsRef);
1046 if (NS_FAILED(rv))
1047 return rv;
1048
1049 LSRequestedInfo theInfoRequest = kLSRequestAllInfo;
1050 LSItemInfoRecord theInfo;
1051 if (::LSCopyItemInfoForRef(&fsRef, theInfoRequest, &theInfo) == noErr) {
1052 if ((theInfo.flags & kLSItemInfoIsApplication) != 0)
1053 *_retval = PR_TRUE;
1054 }
1055 return NS_OK;
1056}
1057
1058/* boolean isHidden (); */
1059NS_IMETHODIMP nsLocalFile::IsHidden(PRBool *_retval)
1060{
1061 NS_ENSURE_ARG_POINTER(_retval);
1062 *_retval = PR_FALSE;
1063
1064 FSRef fsRef;
1065 nsresult rv = GetFSRefInternal(fsRef);
1066 if (NS_FAILED(rv))
1067 return rv;
1068
1069 FSCatalogInfo catalogInfo;
1070 HFSUniStr255 leafName;
1071 OSErr err = ::FSGetCatalogInfo(&fsRef, kFSCatInfoFinderInfo, &catalogInfo,
1072 &leafName, nsnull, nsnull);
1073 if (err != noErr)
1074 return MacErrorMapper(err);
1075
1076 FileInfo *fInfoPtr = (FileInfo *)(catalogInfo.finderInfo); // Finder flags are in the same place whether we use FileInfo or FolderInfo
1077 if ((fInfoPtr->finderFlags & kIsInvisible) != 0) {
1078 *_retval = PR_TRUE;
1079 }
1080 else {
1081 // If the leaf name begins with a '.', consider it invisible
1082 if (leafName.length >= 1 && leafName.unicode[0] == UniChar('.'))
1083 *_retval = PR_TRUE;
1084 }
1085 return NS_OK;
1086}
1087
1088/* boolean isDirectory (); */
1089NS_IMETHODIMP nsLocalFile::IsDirectory(PRBool *_retval)
1090{
1091 NS_ENSURE_ARG_POINTER(_retval);
1092 *_retval = PR_FALSE;
1093
1094 FSRef fsRef;
1095 nsresult rv = GetFSRefInternal(fsRef, PR_FALSE);
1096 if (NS_FAILED(rv))
1097 return rv;
1098
1099 FSCatalogInfo catalogInfo;
1100 OSErr err = ::FSGetCatalogInfo(&fsRef, kFSCatInfoNodeFlags, &catalogInfo,
1101 nsnull, nsnull, nsnull);
1102 if (err != noErr)
1103 return MacErrorMapper(err);
1104 *_retval = ((catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) != 0);
1105 return NS_OK;
1106}
1107
1108/* boolean isFile (); */
1109NS_IMETHODIMP nsLocalFile::IsFile(PRBool *_retval)
1110{
1111 NS_ENSURE_ARG_POINTER(_retval);
1112 *_retval = PR_FALSE;
1113
1114 FSRef fsRef;
1115 nsresult rv = GetFSRefInternal(fsRef, PR_FALSE);
1116 if (NS_FAILED(rv))
1117 return rv;
1118
1119 FSCatalogInfo catalogInfo;
1120 OSErr err = ::FSGetCatalogInfo(&fsRef, kFSCatInfoNodeFlags, &catalogInfo,
1121 nsnull, nsnull, nsnull);
1122 if (err != noErr)
1123 return MacErrorMapper(err);
1124 *_retval = ((catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) == 0);
1125 return NS_OK;
1126}
1127
1128/* boolean isSymlink (); */
1129NS_IMETHODIMP nsLocalFile::IsSymlink(PRBool *_retval)
1130{
1131 // Check we are correctly initialized.
1132 CHECK_mBaseRef();
1133
1134 NS_ENSURE_ARG(_retval);
1135 *_retval = PR_FALSE;
1136
1137 // Check we are correctly initialized.
1138 CHECK_mBaseRef();
1139
1140 FSRef fsRef;
1141 if (::CFURLGetFSRef(mBaseRef, &fsRef)) {
1142 Boolean isAlias, isFolder;
1143 if (::FSIsAliasFile(&fsRef, &isAlias, &isFolder) == noErr)
1144 *_retval = isAlias;
1145 }
1146 return NS_OK;
1147}
1148
1149/* boolean isSpecial (); */
1150NS_IMETHODIMP nsLocalFile::IsSpecial(PRBool *_retval)
1151{
1152 NS_ERROR("NS_ERROR_NOT_IMPLEMENTED");
1153 return NS_ERROR_NOT_IMPLEMENTED;
1154}
1155
1156/* nsIFile clone (); */
1157NS_IMETHODIMP nsLocalFile::Clone(nsIFile **_retval)
1158{
1159 // Just copy-construct ourselves
1160 *_retval = new nsLocalFile(*this);
1161 if (!*_retval)
1162 return NS_ERROR_OUT_OF_MEMORY;
1163
1164 NS_ADDREF(*_retval);
1165
1166 return NS_OK;
1167}
1168
1169/* boolean equals (in nsIFile inFile); */
1170NS_IMETHODIMP nsLocalFile::Equals(nsIFile *inFile, PRBool *_retval)
1171{
1172 return EqualsInternal(inFile, PR_TRUE, _retval);
1173}
1174
1175nsresult
1176nsLocalFile::EqualsInternal(nsISupports* inFile, PRBool aUpdateCache,
1177 PRBool *_retval)
1178{
1179 NS_ENSURE_ARG_POINTER(_retval);
1180 *_retval = PR_FALSE;
1181
1182 nsCOMPtr<nsILocalFileMac> inMacFile(do_QueryInterface(inFile));
1183 if (!inFile)
1184 return NS_OK;
1185
1186 nsLocalFile* inLF =
1187 NS_STATIC_CAST(nsLocalFile*, (nsILocalFileMac*) inMacFile);
1188
1189 // If both exist, compare FSRefs
1190 FSRef thisFSRef, inFSRef;
1191 nsresult rv1 = GetFSRefInternal(thisFSRef, aUpdateCache);
1192 nsresult rv2 = inLF->GetFSRefInternal(inFSRef, aUpdateCache);
1193 if (NS_SUCCEEDED(rv1) && NS_SUCCEEDED(rv2)) {
1194 *_retval = (thisFSRef == inFSRef);
1195 return NS_OK;
1196 }
1197 // If one exists and the other doesn't, not equal
1198 if (rv1 != rv2)
1199 return NS_OK;
1200
1201 // Arg, we have to get their paths and compare
1202 nsCAutoString thisPath, inPath;
1203 if (NS_FAILED(GetNativePath(thisPath)))
1204 return NS_ERROR_FAILURE;
1205 if (NS_FAILED(inMacFile->GetNativePath(inPath)))
1206 return NS_ERROR_FAILURE;
1207 *_retval = thisPath.Equals(inPath);
1208
1209 return NS_OK;
1210}
1211
1212/* boolean contains (in nsIFile inFile, in boolean recur); */
1213NS_IMETHODIMP nsLocalFile::Contains(nsIFile *inFile, PRBool recur, PRBool *_retval)
1214{
1215 // Check we are correctly initialized.
1216 CHECK_mBaseRef();
1217
1218 NS_ENSURE_ARG_POINTER(_retval);
1219 *_retval = PR_FALSE;
1220
1221 PRBool isDir;
1222 nsresult rv = IsDirectory(&isDir);
1223 if (NS_FAILED(rv))
1224 return rv;
1225 if (!isDir)
1226 return NS_OK; // must be a dir to contain someone
1227
1228 nsCAutoString thisPath, inPath;
1229 if (NS_FAILED(GetNativePath(thisPath)) || NS_FAILED(inFile->GetNativePath(inPath)))
1230 return NS_ERROR_FAILURE;
1231 size_t thisPathLen = thisPath.Length();
1232 if ((inPath.Length() > thisPathLen + 1) && (strncasecmp(thisPath.get(), inPath.get(), thisPathLen) == 0)) {
1233 // Now make sure that the |inFile|'s path has a separator at thisPathLen,
1234 // and there's at least one more character after that.
1235 if (inPath[thisPathLen] == kPathSepChar)
1236 *_retval = PR_TRUE;
1237 }
1238 return NS_OK;
1239}
1240
1241/* readonly attribute nsIFile parent; */
1242NS_IMETHODIMP nsLocalFile::GetParent(nsIFile * *aParent)
1243{
1244 NS_ENSURE_ARG_POINTER(aParent);
1245 *aParent = nsnull;
1246
1247 // Check we are correctly initialized.
1248 CHECK_mBaseRef();
1249
1250 nsLocalFile *newFile = nsnull;
1251
1252 // If it can be determined without error that a file does not
1253 // have a parent, return nsnull for the parent and NS_OK as the result.
1254 // See bug 133617.
1255 nsresult rv = NS_OK;
1256 CFURLRef parentURLRef = ::CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorDefault, mBaseRef);
1257 if (parentURLRef) {
1258 rv = NS_ERROR_FAILURE;
1259 newFile = new nsLocalFile;
1260 if (newFile) {
1261 rv = newFile->InitWithCFURL(parentURLRef);
1262 if (NS_SUCCEEDED(rv)) {
1263 NS_ADDREF(*aParent = newFile);
1264 rv = NS_OK;
1265 }
1266 }
1267 ::CFRelease(parentURLRef);
1268 }
1269 return rv;
1270}
1271
1272/* readonly attribute nsISimpleEnumerator directoryEntries; */
1273NS_IMETHODIMP nsLocalFile::GetDirectoryEntries(nsISimpleEnumerator **aDirectoryEntries)
1274{
1275 NS_ENSURE_ARG_POINTER(aDirectoryEntries);
1276 *aDirectoryEntries = nsnull;
1277
1278 nsresult rv;
1279 PRBool isDir;
1280 rv = IsDirectory(&isDir);
1281 if (NS_FAILED(rv))
1282 return rv;
1283 if (!isDir)
1284 return NS_ERROR_FILE_NOT_DIRECTORY;
1285
1286 nsDirEnumerator* dirEnum = new nsDirEnumerator;
1287 if (dirEnum == nsnull)
1288 return NS_ERROR_OUT_OF_MEMORY;
1289 NS_ADDREF(dirEnum);
1290 rv = dirEnum->Init(this);
1291 if (NS_FAILED(rv)) {
1292 NS_RELEASE(dirEnum);
1293 return rv;
1294 }
1295 *aDirectoryEntries = dirEnum;
1296
1297 return NS_OK;
1298}
1299
1300
1301//*****************************************************************************
1302// nsLocalFile::nsILocalFile
1303//*****************************************************************************
1304#pragma mark -
1305#pragma mark [nsILocalFile]
1306
1307/* void initWithPath (in AString filePath); */
1308NS_IMETHODIMP nsLocalFile::InitWithPath(const nsAString& filePath)
1309{
1310 return InitWithNativePath(NS_ConvertUTF16toUTF8(filePath));
1311}
1312
1313/* [noscript] void initWithNativePath (in ACString filePath); */
1314NS_IMETHODIMP nsLocalFile::InitWithNativePath(const nsACString& filePath)
1315{
1316 nsCAutoString fixedPath;
1317 if (Substring(filePath, 0, 2).EqualsLiteral("~/")) {
1318 nsCOMPtr<nsIFile> homeDir;
1319 nsCAutoString homePath;
1320 nsresult rv = NS_GetSpecialDirectory(NS_OS_HOME_DIR,
1321 getter_AddRefs(homeDir));
1322 NS_ENSURE_SUCCESS(rv, rv);
1323 rv = homeDir->GetNativePath(homePath);
1324 NS_ENSURE_SUCCESS(rv, rv);
1325
1326 fixedPath = homePath + Substring(filePath, 1, filePath.Length() - 1);
1327 }
1328 else if (filePath.IsEmpty() || filePath.First() != '/')
1329 return NS_ERROR_FILE_UNRECOGNIZED_PATH;
1330 else
1331 fixedPath.Assign(filePath);
1332
1333 // A path with consecutive '/'s which are not between
1334 // nodes crashes CFURLGetFSRef(). Consecutive '/'s which
1335 // are between actual nodes are OK. So, convert consecutive
1336 // '/'s to a single one.
1337 fixedPath.ReplaceSubstring("//", "/");
1338
1339#if 1 // bird: hack to fix RegistryLocationForSpec issues with /path/to/./components
1340 fixedPath.ReplaceSubstring("/./", "/");
1341 size_t len = fixedPath.Length();
1342 while (len > 2)
1343 {
1344 size_t choplen = 0;
1345 if (!strcmp(fixedPath.get() + len - 2, "/."))
1346 choplen = 2;
1347 else if (!strcmp(fixedPath.get() + len - 1, "/"))
1348 choplen = 1;
1349 else
1350 break;
1351 fixedPath = StringHead(fixedPath, len - choplen);
1352 }
1353#endif
1354
1355 // On 10.2, huge paths also crash CFURLGetFSRef()
1356 if (fixedPath.Length() > PATH_MAX)
1357 return NS_ERROR_FILE_NAME_TOO_LONG;
1358
1359 CFStringRef pathAsCFString;
1360 CFURLRef pathAsCFURL;
1361
1362 pathAsCFString = ::CFStringCreateWithCString(nsnull, fixedPath.get(), kCFStringEncodingUTF8);
1363 if (!pathAsCFString)
1364 return NS_ERROR_FAILURE;
1365 pathAsCFURL = ::CFURLCreateWithFileSystemPath(nsnull, pathAsCFString, kCFURLPOSIXPathStyle, PR_FALSE);
1366 if (!pathAsCFURL) {
1367 ::CFRelease(pathAsCFString);
1368 return NS_ERROR_FAILURE;
1369 }
1370 SetBaseRef(pathAsCFURL);
1371 ::CFRelease(pathAsCFURL);
1372 ::CFRelease(pathAsCFString);
1373 return NS_OK;
1374}
1375
1376/* void initWithFile (in nsILocalFile aFile); */
1377NS_IMETHODIMP nsLocalFile::InitWithFile(nsILocalFile *aFile)
1378{
1379 NS_ENSURE_ARG(aFile);
1380
1381 nsCOMPtr<nsILocalFileMac> aFileMac(do_QueryInterface(aFile));
1382 if (!aFileMac)
1383 return NS_ERROR_UNEXPECTED;
1384 CFURLRef urlRef;
1385 nsresult rv = aFileMac->GetCFURL(&urlRef);
1386 if (NS_FAILED(rv))
1387 return rv;
1388 rv = InitWithCFURL(urlRef);
1389 ::CFRelease(urlRef);
1390 return rv;
1391}
1392
1393/* attribute PRBool followLinks; */
1394NS_IMETHODIMP nsLocalFile::GetFollowLinks(PRBool *aFollowLinks)
1395{
1396 NS_ENSURE_ARG_POINTER(aFollowLinks);
1397
1398 *aFollowLinks = mFollowLinks;
1399 return NS_OK;
1400}
1401
1402NS_IMETHODIMP nsLocalFile::SetFollowLinks(PRBool aFollowLinks)
1403{
1404 if (aFollowLinks != mFollowLinks) {
1405 mFollowLinks = aFollowLinks;
1406 UpdateTargetRef();
1407 }
1408 return NS_OK;
1409}
1410
1411/* [noscript] PRFileDescStar openNSPRFileDesc (in long flags, in long mode); */
1412NS_IMETHODIMP nsLocalFile::OpenNSPRFileDesc(PRInt32 flags, PRInt32 mode, PRFileDesc **_retval)
1413{
1414 NS_ENSURE_ARG_POINTER(_retval);
1415
1416 nsCAutoString path;
1417 nsresult rv = GetPathInternal(path);
1418 if (NS_FAILED(rv))
1419 return rv;
1420
1421 *_retval = PR_Open(path.get(), flags, mode);
1422 if (! *_retval)
1423 return NS_ErrorAccordingToNSPR();
1424
1425 return NS_OK;
1426}
1427
1428/* [noscript] FILE openANSIFileDesc (in string mode); */
1429NS_IMETHODIMP nsLocalFile::OpenANSIFileDesc(const char *mode, FILE **_retval)
1430{
1431 NS_ENSURE_ARG_POINTER(_retval);
1432
1433 nsCAutoString path;
1434 nsresult rv = GetPathInternal(path);
1435 if (NS_FAILED(rv))
1436 return rv;
1437
1438 *_retval = fopen(path.get(), mode);
1439 if (! *_retval)
1440 return NS_ERROR_FAILURE;
1441
1442 return NS_OK;
1443}
1444
1445/* [noscript] PRLibraryStar load (); */
1446NS_IMETHODIMP nsLocalFile::Load(PRLibrary **_retval)
1447{
1448 // Check we are correctly initialized.
1449 CHECK_mBaseRef();
1450
1451 NS_ENSURE_ARG_POINTER(_retval);
1452
1453 NS_TIMELINE_START_TIMER("PR_LoadLibrary");
1454
1455 nsCAutoString path;
1456 nsresult rv = GetPathInternal(path);
1457 if (NS_FAILED(rv))
1458 return rv;
1459
1460#ifdef NS_BUILD_REFCNT_LOGGING
1461 nsTraceRefcntImpl::SetActivityIsLegal(PR_FALSE);
1462#endif
1463
1464 *_retval = PR_LoadLibrary(path.get());
1465
1466#ifdef NS_BUILD_REFCNT_LOGGING
1467 nsTraceRefcntImpl::SetActivityIsLegal(PR_TRUE);
1468#endif
1469
1470 NS_TIMELINE_STOP_TIMER("PR_LoadLibrary");
1471 NS_TIMELINE_MARK_TIMER1("PR_LoadLibrary", path.get());
1472
1473 if (!*_retval)
1474 return NS_ERROR_FAILURE;
1475
1476 return NS_OK;
1477}
1478
1479/* readonly attribute PRInt64 diskSpaceAvailable; */
1480NS_IMETHODIMP nsLocalFile::GetDiskSpaceAvailable(PRInt64 *aDiskSpaceAvailable)
1481{
1482 // Check we are correctly initialized.
1483 CHECK_mBaseRef();
1484
1485 NS_ENSURE_ARG_POINTER(aDiskSpaceAvailable);
1486
1487 FSRef fsRef;
1488 nsresult rv = GetFSRefInternal(fsRef);
1489 if (NS_FAILED(rv))
1490 return rv;
1491
1492 OSErr err;
1493 FSCatalogInfo catalogInfo;
1494 err = ::FSGetCatalogInfo(&fsRef, kFSCatInfoVolume, &catalogInfo,
1495 nsnull, nsnull, nsnull);
1496 if (err != noErr)
1497 return MacErrorMapper(err);
1498
1499 FSVolumeInfo volumeInfo;
1500 err = ::FSGetVolumeInfo(catalogInfo.volume, 0, nsnull, kFSVolInfoSizes,
1501 &volumeInfo, nsnull, nsnull);
1502 if (err != noErr)
1503 return MacErrorMapper(err);
1504
1505 *aDiskSpaceAvailable = volumeInfo.freeBytes;
1506 return NS_OK;
1507}
1508
1509/* void appendRelativePath (in AString relativeFilePath); */
1510NS_IMETHODIMP nsLocalFile::AppendRelativePath(const nsAString& relativeFilePath)
1511{
1512 return AppendRelativeNativePath(NS_ConvertUTF16toUTF8(relativeFilePath));
1513}
1514
1515/* [noscript] void appendRelativeNativePath (in ACString relativeFilePath); */
1516NS_IMETHODIMP nsLocalFile::AppendRelativeNativePath(const nsACString& relativeFilePath)
1517{
1518 if (relativeFilePath.IsEmpty())
1519 return NS_OK;
1520 // No leading '/'
1521 if (relativeFilePath.First() == '/')
1522 return NS_ERROR_FILE_UNRECOGNIZED_PATH;
1523
1524 // Parse the nodes and call Append() for each
1525 nsACString::const_iterator nodeBegin, pathEnd;
1526 relativeFilePath.BeginReading(nodeBegin);
1527 relativeFilePath.EndReading(pathEnd);
1528 nsACString::const_iterator nodeEnd(nodeBegin);
1529
1530 while (nodeEnd != pathEnd) {
1531 FindCharInReadable(kPathSepChar, nodeEnd, pathEnd);
1532 nsresult rv = AppendNative(Substring(nodeBegin, nodeEnd));
1533 if (NS_FAILED(rv))
1534 return rv;
1535 if (nodeEnd != pathEnd) // If there's more left in the string, inc over the '/' nodeEnd is on.
1536 ++nodeEnd;
1537 nodeBegin = nodeEnd;
1538 }
1539 return NS_OK;
1540}
1541
1542/* attribute ACString persistentDescriptor; */
1543NS_IMETHODIMP nsLocalFile::GetPersistentDescriptor(nsACString& aPersistentDescriptor)
1544{
1545 FSRef fsRef;
1546 nsresult rv = GetFSRefInternal(fsRef);
1547 if (NS_FAILED(rv))
1548 return rv;
1549
1550 AliasHandle aliasH;
1551 OSErr err = ::FSNewAlias(nsnull, &fsRef, &aliasH);
1552 if (err != noErr)
1553 return MacErrorMapper(err);
1554
1555 PRUint32 bytes = ::GetHandleSize((Handle) aliasH);
1556 ::HLock((Handle) aliasH);
1557 // Passing nsnull for dest makes NULL-term string
1558 char* buf = PL_Base64Encode((const char*)*aliasH, bytes, nsnull);
1559 ::DisposeHandle((Handle) aliasH);
1560 NS_ENSURE_TRUE(buf, NS_ERROR_OUT_OF_MEMORY);
1561
1562 aPersistentDescriptor = buf;
1563 PR_Free(buf);
1564
1565 return NS_OK;
1566}
1567
1568NS_IMETHODIMP nsLocalFile::SetPersistentDescriptor(const nsACString& aPersistentDescriptor)
1569{
1570 if (aPersistentDescriptor.IsEmpty())
1571 return NS_ERROR_INVALID_ARG;
1572
1573 // Support pathnames as user-supplied descriptors if they begin with '/'
1574 // or '~'. These characters do not collide with the base64 set used for
1575 // encoding alias records.
1576 char first = aPersistentDescriptor.First();
1577 if (first == '/' || first == '~')
1578 return InitWithNativePath(aPersistentDescriptor);
1579
1580 nsresult rv = NS_OK;
1581
1582 PRUint32 dataSize = aPersistentDescriptor.Length();
1583 char* decodedData = PL_Base64Decode(PromiseFlatCString(aPersistentDescriptor).get(), dataSize, nsnull);
1584 if (!decodedData) {
1585 NS_ERROR("SetPersistentDescriptor was given bad data");
1586 return NS_ERROR_FAILURE;
1587 }
1588
1589 // Cast to an alias record and resolve.
1590 AliasRecord aliasHeader = *(AliasPtr)decodedData;
1591 PRInt32 aliasSize = GetAliasSizeFromRecord(aliasHeader);
1592 if (aliasSize > (dataSize * 3) / 4) { // be paranoid about having too few data
1593 PR_Free(decodedData);
1594 return NS_ERROR_FAILURE;
1595 }
1596
1597 // Move the now-decoded data into the Handle.
1598 // The size of the decoded data is 3/4 the size of the encoded data. See plbase64.h
1599 Handle newHandle = nsnull;
1600 if (::PtrToHand(decodedData, &newHandle, aliasSize) != noErr)
1601 rv = NS_ERROR_OUT_OF_MEMORY;
1602 PR_Free(decodedData);
1603 if (NS_FAILED(rv))
1604 return rv;
1605
1606 Boolean changed;
1607 FSRef resolvedFSRef;
1608 OSErr err = ::FSResolveAlias(nsnull, (AliasHandle)newHandle, &resolvedFSRef, &changed);
1609
1610 rv = MacErrorMapper(err);
1611 DisposeHandle(newHandle);
1612 if (NS_FAILED(rv))
1613 return rv;
1614
1615 return InitWithFSRef(&resolvedFSRef);
1616}
1617
1618/* void reveal (); */
1619NS_IMETHODIMP nsLocalFile::Reveal()
1620{
1621 FSRef fsRefToReveal;
1622 AppleEvent aeEvent = {0, nil};
1623 AppleEvent aeReply = {0, nil};
1624 StAEDesc aeDirDesc, listElem, myAddressDesc, fileList;
1625 OSErr err;
1626 ProcessSerialNumber process;
1627
1628 nsresult rv = GetFSRefInternal(fsRefToReveal);
1629 if (NS_FAILED(rv))
1630 return rv;
1631
1632 err = ::FindRunningAppBySignature ('MACS', process);
1633 if (err == noErr) {
1634 err = ::AECreateDesc(typeProcessSerialNumber, (Ptr)&process, sizeof(process), &myAddressDesc);
1635 if (err == noErr) {
1636 // Create the FinderEvent
1637 err = ::AECreateAppleEvent(kAEMiscStandards, kAEMakeObjectsVisible, &myAddressDesc,
1638 kAutoGenerateReturnID, kAnyTransactionID, &aeEvent);
1639 if (err == noErr) {
1640 // Create the file list
1641 err = ::AECreateList(nil, 0, false, &fileList);
1642 if (err == noErr) {
1643 FSSpec fsSpecToReveal;
1644 err = ::FSRefMakeFSSpec(&fsRefToReveal, &fsSpecToReveal);
1645 if (err == noErr) {
1646 err = ::AEPutPtr(&fileList, 0, typeFSS, &fsSpecToReveal, sizeof(FSSpec));
1647 if (err == noErr) {
1648 err = ::AEPutParamDesc(&aeEvent, keyDirectObject, &fileList);
1649 if (err == noErr) {
1650 err = ::AESend(&aeEvent, &aeReply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, nil, nil);
1651 if (err == noErr)
1652 ::SetFrontProcess(&process);
1653 }
1654 }
1655 }
1656 }
1657 }
1658 }
1659 }
1660
1661 return NS_OK;
1662}
1663
1664/* void launch (); */
1665NS_IMETHODIMP nsLocalFile::Launch()
1666{
1667 FSRef fsRef;
1668 nsresult rv = GetFSRefInternal(fsRef);
1669 if (NS_FAILED(rv))
1670 return rv;
1671
1672 OSErr err = ::LSOpenFSRef(&fsRef, NULL);
1673 return MacErrorMapper(err);
1674}
1675
1676
1677//*****************************************************************************
1678// nsLocalFile::nsILocalFileMac
1679//*****************************************************************************
1680#pragma mark -
1681#pragma mark [nsILocalFileMac]
1682
1683/* void initWithCFURL (in CFURLRef aCFURL); */
1684NS_IMETHODIMP nsLocalFile::InitWithCFURL(CFURLRef aCFURL)
1685{
1686 NS_ENSURE_ARG(aCFURL);
1687
1688 SetBaseRef(aCFURL);
1689 return NS_OK;
1690}
1691
1692/* void initWithFSRef ([const] in FSRefPtr aFSRef); */
1693NS_IMETHODIMP nsLocalFile::InitWithFSRef(const FSRef *aFSRef)
1694{
1695 NS_ENSURE_ARG(aFSRef);
1696 nsresult rv = NS_ERROR_FAILURE;
1697
1698 CFURLRef newURLRef = ::CFURLCreateFromFSRef(kCFAllocatorDefault, aFSRef);
1699 if (newURLRef) {
1700 SetBaseRef(newURLRef);
1701 ::CFRelease(newURLRef);
1702 rv = NS_OK;
1703 }
1704 return rv;
1705}
1706
1707/* void initWithFSSpec ([const] in FSSpecPtr aFileSpec); */
1708NS_IMETHODIMP nsLocalFile::InitWithFSSpec(const FSSpec *aFileSpec)
1709{
1710 NS_ENSURE_ARG(aFileSpec);
1711
1712 FSRef fsRef;
1713 OSErr err = ::FSpMakeFSRef(aFileSpec, &fsRef);
1714 if (err == noErr)
1715 return InitWithFSRef(&fsRef);
1716 else if (err == fnfErr) {
1717 CInfoPBRec pBlock;
1718 FSSpec parentDirSpec;
1719
1720 memset(&pBlock, 0, sizeof(CInfoPBRec));
1721 parentDirSpec.name[0] = 0;
1722 pBlock.dirInfo.ioVRefNum = aFileSpec->vRefNum;
1723 pBlock.dirInfo.ioDrDirID = aFileSpec->parID;
1724 pBlock.dirInfo.ioNamePtr = (StringPtr)parentDirSpec.name;
1725 pBlock.dirInfo.ioFDirIndex = -1; //get info on parID
1726 err = ::PBGetCatInfoSync(&pBlock);
1727 if (err != noErr)
1728 return MacErrorMapper(err);
1729
1730 parentDirSpec.vRefNum = aFileSpec->vRefNum;
1731 parentDirSpec.parID = pBlock.dirInfo.ioDrParID;
1732 err = ::FSpMakeFSRef(&parentDirSpec, &fsRef);
1733 if (err != noErr)
1734 return MacErrorMapper(err);
1735 HFSUniStr255 unicodeName;
1736 err = ::HFSNameGetUnicodeName(aFileSpec->name, kTextEncodingUnknown, &unicodeName);
1737 if (err != noErr)
1738 return MacErrorMapper(err);
1739 nsresult rv = InitWithFSRef(&fsRef);
1740 if (NS_FAILED(rv))
1741 return rv;
1742 return Append(nsDependentString(unicodeName.unicode, unicodeName.length));
1743 }
1744 return MacErrorMapper(err);
1745}
1746
1747/* void initToAppWithCreatorCode (in OSType aAppCreator); */
1748NS_IMETHODIMP nsLocalFile::InitToAppWithCreatorCode(OSType aAppCreator)
1749{
1750 FSRef fsRef;
1751 OSErr err = ::LSFindApplicationForInfo(aAppCreator, nsnull, nsnull, &fsRef, nsnull);
1752 if (err != noErr)
1753 return MacErrorMapper(err);
1754 return InitWithFSRef(&fsRef);
1755}
1756
1757/* CFURLRef getCFURL (); */
1758NS_IMETHODIMP nsLocalFile::GetCFURL(CFURLRef *_retval)
1759{
1760 NS_ENSURE_ARG_POINTER(_retval);
1761 CFURLRef whichURLRef = mFollowLinks ? mTargetRef : mBaseRef;
1762 if (whichURLRef)
1763 ::CFRetain(whichURLRef);
1764 *_retval = whichURLRef;
1765 return whichURLRef ? NS_OK : NS_ERROR_FAILURE;
1766}
1767
1768/* FSRef getFSRef (); */
1769NS_IMETHODIMP nsLocalFile::GetFSRef(FSRef *_retval)
1770{
1771 NS_ENSURE_ARG_POINTER(_retval);
1772 return GetFSRefInternal(*_retval);
1773}
1774
1775/* FSSpec getFSSpec (); */
1776NS_IMETHODIMP nsLocalFile::GetFSSpec(FSSpec *_retval)
1777{
1778 NS_ENSURE_ARG_POINTER(_retval);
1779
1780 // Check we are correctly initialized.
1781 CHECK_mBaseRef();
1782
1783 OSErr err;
1784 FSRef fsRef;
1785 nsresult rv = GetFSRefInternal(fsRef);
1786 if (NS_SUCCEEDED(rv)) {
1787 // If the leaf node exists, things are simple.
1788 err = ::FSGetCatalogInfo(&fsRef, kFSCatInfoNone,
1789 nsnull, nsnull, _retval, nsnull);
1790 return MacErrorMapper(err);
1791 }
1792 else if (rv == NS_ERROR_FILE_NOT_FOUND) {
1793 // If the parent of the leaf exists, make an FSSpec from that.
1794 CFURLRef parentURLRef = ::CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorDefault, mBaseRef);
1795 if (!parentURLRef)
1796 return NS_ERROR_FAILURE;
1797
1798 err = fnfErr;
1799 if (::CFURLGetFSRef(parentURLRef, &fsRef)) {
1800 FSCatalogInfo catalogInfo;
1801 if ((err = ::FSGetCatalogInfo(&fsRef,
1802 kFSCatInfoVolume + kFSCatInfoNodeID + kFSCatInfoTextEncoding,
1803 &catalogInfo, nsnull, nsnull, nsnull)) == noErr) {
1804 nsAutoString leafName;
1805 if (NS_SUCCEEDED(GetLeafName(leafName))) {
1806 Str31 hfsName;
1807 if ((err = ::UnicodeNameGetHFSName(leafName.Length(),
1808 leafName.get(),
1809 catalogInfo.textEncodingHint,
1810 catalogInfo.nodeID == fsRtDirID,
1811 hfsName)) == noErr)
1812 err = ::FSMakeFSSpec(catalogInfo.volume, catalogInfo.nodeID, hfsName, _retval);
1813 }
1814 }
1815 }
1816 ::CFRelease(parentURLRef);
1817 rv = MacErrorMapper(err);
1818 }
1819 return rv;
1820}
1821
1822/* readonly attribute PRInt64 fileSizeWithResFork; */
1823NS_IMETHODIMP nsLocalFile::GetFileSizeWithResFork(PRInt64 *aFileSizeWithResFork)
1824{
1825 NS_ENSURE_ARG_POINTER(aFileSizeWithResFork);
1826
1827 FSRef fsRef;
1828 nsresult rv = GetFSRefInternal(fsRef);
1829 if (NS_FAILED(rv))
1830 return rv;
1831
1832 FSCatalogInfo catalogInfo;
1833 OSErr err = ::FSGetCatalogInfo(&fsRef, kFSCatInfoDataSizes + kFSCatInfoRsrcSizes,
1834 &catalogInfo, nsnull, nsnull, nsnull);
1835 if (err != noErr)
1836 return MacErrorMapper(err);
1837
1838 *aFileSizeWithResFork = catalogInfo.dataLogicalSize + catalogInfo.rsrcLogicalSize;
1839 return NS_OK;
1840}
1841
1842/* attribute OSType fileType; */
1843NS_IMETHODIMP nsLocalFile::GetFileType(OSType *aFileType)
1844{
1845 NS_ENSURE_ARG_POINTER(aFileType);
1846
1847 FSRef fsRef;
1848 nsresult rv = GetFSRefInternal(fsRef);
1849 if (NS_FAILED(rv))
1850 return rv;
1851
1852 FinderInfo fInfo;
1853 OSErr err = ::FSGetFinderInfo(&fsRef, &fInfo, nsnull, nsnull);
1854 if (err != noErr)
1855 return MacErrorMapper(err);
1856 *aFileType = fInfo.file.fileType;
1857 return NS_OK;
1858}
1859
1860NS_IMETHODIMP nsLocalFile::SetFileType(OSType aFileType)
1861{
1862 FSRef fsRef;
1863 nsresult rv = GetFSRefInternal(fsRef);
1864 if (NS_FAILED(rv))
1865 return rv;
1866
1867 OSErr err = ::FSChangeCreatorType(&fsRef, 0, aFileType);
1868 return MacErrorMapper(err);
1869}
1870
1871/* attribute OSType fileCreator; */
1872NS_IMETHODIMP nsLocalFile::GetFileCreator(OSType *aFileCreator)
1873{
1874 NS_ENSURE_ARG_POINTER(aFileCreator);
1875
1876 FSRef fsRef;
1877 nsresult rv = GetFSRefInternal(fsRef);
1878 if (NS_FAILED(rv))
1879 return rv;
1880
1881 FinderInfo fInfo;
1882 OSErr err = ::FSGetFinderInfo(&fsRef, &fInfo, nsnull, nsnull);
1883 if (err != noErr)
1884 return MacErrorMapper(err);
1885 *aFileCreator = fInfo.file.fileCreator;
1886 return NS_OK;
1887}
1888
1889NS_IMETHODIMP nsLocalFile::SetFileCreator(OSType aFileCreator)
1890{
1891 FSRef fsRef;
1892 nsresult rv = GetFSRefInternal(fsRef);
1893 if (NS_FAILED(rv))
1894 return rv;
1895
1896 OSErr err = ::FSChangeCreatorType(&fsRef, aFileCreator, 0);
1897 return MacErrorMapper(err);
1898}
1899
1900/* void setFileTypeAndCreatorFromMIMEType (in string aMIMEType); */
1901NS_IMETHODIMP nsLocalFile::SetFileTypeAndCreatorFromMIMEType(const char *aMIMEType)
1902{
1903 // XXX - This should be cut from the API. Would create an evil dependency.
1904 NS_ERROR("NS_ERROR_NOT_IMPLEMENTED");
1905 return NS_ERROR_NOT_IMPLEMENTED;
1906}
1907
1908/* void setFileTypeAndCreatorFromExtension (in string aExtension); */
1909NS_IMETHODIMP nsLocalFile::SetFileTypeAndCreatorFromExtension(const char *aExtension)
1910{
1911 // XXX - This should be cut from the API. Would create an evil dependency.
1912 NS_ERROR("NS_ERROR_NOT_IMPLEMENTED");
1913 return NS_ERROR_NOT_IMPLEMENTED;
1914}
1915
1916/* void launchWithDoc (in nsILocalFile aDocToLoad, in boolean aLaunchInBackground); */
1917NS_IMETHODIMP nsLocalFile::LaunchWithDoc(nsILocalFile *aDocToLoad, PRBool aLaunchInBackground)
1918{
1919 PRBool isExecutable;
1920 nsresult rv = IsExecutable(&isExecutable);
1921 if (NS_FAILED(rv))
1922 return rv;
1923 if (!isExecutable)
1924 return NS_ERROR_FILE_EXECUTION_FAILED;
1925
1926 FSRef appFSRef, docFSRef;
1927 rv = GetFSRefInternal(appFSRef);
1928 if (NS_FAILED(rv))
1929 return rv;
1930
1931 if (aDocToLoad) {
1932 nsCOMPtr<nsILocalFileMac> macDoc = do_QueryInterface(aDocToLoad);
1933 rv = macDoc->GetFSRef(&docFSRef);
1934 if (NS_FAILED(rv))
1935 return rv;
1936 }
1937
1938 LSLaunchFlags theLaunchFlags = kLSLaunchDefaults;
1939 LSLaunchFSRefSpec thelaunchSpec;
1940
1941 if (aLaunchInBackground)
1942 theLaunchFlags |= kLSLaunchDontSwitch;
1943 memset(&thelaunchSpec, 0, sizeof(LSLaunchFSRefSpec));
1944
1945 thelaunchSpec.appRef = &appFSRef;
1946 if (aDocToLoad) {
1947 thelaunchSpec.numDocs = 1;
1948 thelaunchSpec.itemRefs = &docFSRef;
1949 }
1950 thelaunchSpec.launchFlags = theLaunchFlags;
1951
1952 OSErr err = ::LSOpenFromRefSpec(&thelaunchSpec, NULL);
1953 if (err != noErr)
1954 return MacErrorMapper(err);
1955
1956 return NS_OK;
1957}
1958
1959/* void openDocWithApp (in nsILocalFile aAppToOpenWith, in boolean aLaunchInBackground); */
1960NS_IMETHODIMP nsLocalFile::OpenDocWithApp(nsILocalFile *aAppToOpenWith, PRBool aLaunchInBackground)
1961{
1962 nsresult rv;
1963 OSErr err;
1964
1965 FSRef docFSRef, appFSRef;
1966 rv = GetFSRefInternal(docFSRef);
1967 if (NS_FAILED(rv))
1968 return rv;
1969
1970 if (aAppToOpenWith) {
1971 nsCOMPtr<nsILocalFileMac> appFileMac = do_QueryInterface(aAppToOpenWith, &rv);
1972 if (!appFileMac)
1973 return rv;
1974
1975 PRBool isExecutable;
1976 rv = appFileMac->IsExecutable(&isExecutable);
1977 if (NS_FAILED(rv))
1978 return rv;
1979 if (!isExecutable)
1980 return NS_ERROR_FILE_EXECUTION_FAILED;
1981
1982 rv = appFileMac->GetFSRef(&appFSRef);
1983 if (NS_FAILED(rv))
1984 return rv;
1985 }
1986 else {
1987 OSType fileCreator;
1988 rv = GetFileCreator(&fileCreator);
1989 if (NS_FAILED(rv))
1990 return rv;
1991
1992 err = ::LSFindApplicationForInfo(fileCreator, nsnull, nsnull, &appFSRef, nsnull);
1993 if (err != noErr)
1994 return MacErrorMapper(err);
1995 }
1996
1997 LSLaunchFlags theLaunchFlags = kLSLaunchDefaults;
1998 LSLaunchFSRefSpec thelaunchSpec;
1999
2000 if (aLaunchInBackground)
2001 theLaunchFlags |= kLSLaunchDontSwitch;
2002 memset(&thelaunchSpec, 0, sizeof(LSLaunchFSRefSpec));
2003
2004 thelaunchSpec.appRef = &appFSRef;
2005 thelaunchSpec.numDocs = 1;
2006 thelaunchSpec.itemRefs = &docFSRef;
2007 thelaunchSpec.launchFlags = theLaunchFlags;
2008
2009 err = ::LSOpenFromRefSpec(&thelaunchSpec, NULL);
2010 if (err != noErr)
2011 return MacErrorMapper(err);
2012
2013 return NS_OK;
2014}
2015
2016/* boolean isPackage (); */
2017NS_IMETHODIMP nsLocalFile::IsPackage(PRBool *_retval)
2018{
2019 NS_ENSURE_ARG(_retval);
2020 *_retval = PR_FALSE;
2021
2022 FSRef fsRef;
2023 nsresult rv = GetFSRefInternal(fsRef);
2024 if (NS_FAILED(rv))
2025 return rv;
2026
2027 FSCatalogInfo catalogInfo;
2028 OSErr err = ::FSGetCatalogInfo(&fsRef, kFSCatInfoNodeFlags + kFSCatInfoFinderInfo,
2029 &catalogInfo, nsnull, nsnull, nsnull);
2030 if (err != noErr)
2031 return MacErrorMapper(err);
2032 if ((catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) != 0) {
2033 FileInfo *fInfoPtr = (FileInfo *)(catalogInfo.finderInfo);
2034 if ((fInfoPtr->finderFlags & kHasBundle) != 0) {
2035 *_retval = PR_TRUE;
2036 }
2037 else {
2038 // Folders ending with ".app" are also considered to
2039 // be packages, even if the top-level folder doesn't have bundle set
2040 nsCAutoString name;
2041 if (NS_SUCCEEDED(rv = GetNativeLeafName(name))) {
2042 const char *extPtr = strrchr(name.get(), '.');
2043 if (extPtr) {
2044 if ((nsCRT::strcasecmp(extPtr, ".app") == 0))
2045 *_retval = PR_TRUE;
2046 }
2047 }
2048 }
2049 }
2050 return NS_OK;
2051}
2052
2053NS_IMETHODIMP
2054nsLocalFile::GetBundleDisplayName(nsAString& outBundleName)
2055{
2056 PRBool isPackage = PR_FALSE;
2057 nsresult rv = IsPackage(&isPackage);
2058 if (NS_FAILED(rv) || !isPackage)
2059 return NS_ERROR_FAILURE;
2060
2061 nsAutoString name;
2062 rv = GetLeafName(name);
2063 if (NS_FAILED(rv))
2064 return rv;
2065
2066 PRInt32 length = name.Length();
2067 if (Substring(name, length - 4, length).EqualsLiteral(".app")) {
2068 // 4 characters in ".app"
2069 outBundleName = Substring(name, 0, length - 4);
2070 }
2071 else
2072 outBundleName = name;
2073
2074 return NS_OK;
2075}
2076
2077NS_IMETHODIMP
2078nsLocalFile::GetBundleIdentifier(nsACString& outBundleIdentifier)
2079{
2080 nsresult rv = NS_ERROR_FAILURE;
2081
2082 CFURLRef urlRef;
2083 if (NS_SUCCEEDED(GetCFURL(&urlRef))) {
2084 CFBundleRef bundle = ::CFBundleCreate(NULL, urlRef);
2085 if (bundle) {
2086 CFStringRef bundleIdentifier = ::CFBundleGetIdentifier(bundle);
2087 if (bundleIdentifier)
2088 rv = CFStringReftoUTF8(bundleIdentifier, outBundleIdentifier);
2089
2090 ::CFRelease(bundle);
2091 }
2092 ::CFRelease(urlRef);
2093 }
2094
2095 return rv;
2096}
2097
2098
2099//*****************************************************************************
2100// nsLocalFile Methods
2101//*****************************************************************************
2102#pragma mark -
2103#pragma mark [Protected Methods]
2104
2105nsresult nsLocalFile::SetBaseRef(CFURLRef aCFURLRef)
2106{
2107 NS_ENSURE_ARG(aCFURLRef);
2108
2109 ::CFRetain(aCFURLRef);
2110 if (mBaseRef)
2111 ::CFRelease(mBaseRef);
2112 mBaseRef = aCFURLRef;
2113
2114 mFollowLinksDirty = PR_TRUE;
2115 UpdateTargetRef();
2116 mCachedFSRefValid = PR_FALSE;
2117 return NS_OK;
2118}
2119
2120nsresult nsLocalFile::UpdateTargetRef()
2121{
2122 // Check we are correctly initialized.
2123 CHECK_mBaseRef();
2124
2125 if (mFollowLinksDirty) {
2126 if (mTargetRef) {
2127 ::CFRelease(mTargetRef);
2128 mTargetRef = nsnull;
2129 }
2130 if (mFollowLinks) {
2131 mTargetRef = mBaseRef;
2132 ::CFRetain(mTargetRef);
2133
2134 FSRef fsRef;
2135 if (::CFURLGetFSRef(mBaseRef, &fsRef)) {
2136 Boolean targetIsFolder, wasAliased;
2137 if (FSResolveAliasFile(&fsRef, true /*resolveAliasChains*/,
2138 &targetIsFolder, &wasAliased) == noErr && wasAliased) {
2139 ::CFRelease(mTargetRef);
2140 mTargetRef = CFURLCreateFromFSRef(NULL, &fsRef);
2141 if (!mTargetRef)
2142 return NS_ERROR_FAILURE;
2143 }
2144 }
2145 mFollowLinksDirty = PR_FALSE;
2146 }
2147 }
2148 return NS_OK;
2149}
2150
2151nsresult nsLocalFile::GetFSRefInternal(FSRef& aFSRef, PRBool bForceUpdateCache)
2152{
2153 if (bForceUpdateCache || !mCachedFSRefValid) {
2154 mCachedFSRefValid = PR_FALSE;
2155 CFURLRef whichURLRef = mFollowLinks ? mTargetRef : mBaseRef;
2156 NS_ENSURE_TRUE(whichURLRef, NS_ERROR_NULL_POINTER);
2157 if (::CFURLGetFSRef(whichURLRef, &mCachedFSRef))
2158 mCachedFSRefValid = PR_TRUE;
2159 }
2160 if (mCachedFSRefValid) {
2161 aFSRef = mCachedFSRef;
2162 return NS_OK;
2163 }
2164 // CFURLGetFSRef only returns a Boolean for success,
2165 // so we have to assume what the error was. This is
2166 // the only probable cause.
2167 return NS_ERROR_FILE_NOT_FOUND;
2168}
2169
2170nsresult nsLocalFile::GetPathInternal(nsACString& path)
2171{
2172 nsresult rv = NS_ERROR_FAILURE;
2173
2174 CFURLRef whichURLRef = mFollowLinks ? mTargetRef : mBaseRef;
2175 NS_ENSURE_TRUE(whichURLRef, NS_ERROR_NULL_POINTER);
2176
2177 CFStringRef pathStrRef = ::CFURLCopyFileSystemPath(whichURLRef, kCFURLPOSIXPathStyle);
2178 if (pathStrRef) {
2179 rv = CFStringReftoUTF8(pathStrRef, path);
2180 ::CFRelease(pathStrRef);
2181 }
2182 return rv;
2183}
2184
2185nsresult nsLocalFile::CopyInternal(nsIFile* aParentDir,
2186 const nsAString& newName,
2187 PRBool followLinks)
2188{
2189 // Check we are correctly initialized.
2190 CHECK_mBaseRef();
2191
2192 StFollowLinksState srcFollowState(*this, followLinks);
2193
2194 nsresult rv;
2195 OSErr err;
2196 FSRef srcFSRef, newFSRef;
2197
2198 rv = GetFSRefInternal(srcFSRef);
2199 if (NS_FAILED(rv))
2200 return rv;
2201
2202 nsCOMPtr<nsIFile> newParentDir = aParentDir;
2203
2204 if (!newParentDir) {
2205 if (newName.IsEmpty())
2206 return NS_ERROR_INVALID_ARG;
2207 rv = GetParent(getter_AddRefs(newParentDir));
2208 if (NS_FAILED(rv))
2209 return rv;
2210 }
2211
2212 // If newParentDir does not exist, create it
2213 PRBool exists;
2214 rv = newParentDir->Exists(&exists);
2215 if (NS_FAILED(rv))
2216 return rv;
2217 if (!exists) {
2218 rv = newParentDir->Create(nsIFile::DIRECTORY_TYPE, 0777);
2219 if (NS_FAILED(rv))
2220 return rv;
2221 }
2222
2223 FSRef destFSRef;
2224 nsCOMPtr<nsILocalFileMac> newParentDirMac(do_QueryInterface(newParentDir));
2225 if (!newParentDirMac)
2226 return NS_ERROR_NO_INTERFACE;
2227 rv = newParentDirMac->GetFSRef(&destFSRef);
2228 if (NS_FAILED(rv))
2229 return rv;
2230
2231 err =
2232 ::FSCopyObject(&srcFSRef, &destFSRef, newName.Length(),
2233 newName.Length() ? PromiseFlatString(newName).get() : NULL,
2234 0, kFSCatInfoNone, false, false, NULL, NULL, &newFSRef);
2235
2236 return MacErrorMapper(err);
2237}
2238
2239const PRInt64 kMillisecsPerSec = 1000LL;
2240const PRInt64 kUTCDateTimeFractionDivisor = 65535LL;
2241
2242PRInt64 nsLocalFile::HFSPlustoNSPRTime(const UTCDateTime& utcTime)
2243{
2244 // Start with seconds since Jan. 1, 1904 GMT
2245 PRInt64 result = ((PRInt64)utcTime.highSeconds << 32) + (PRInt64)utcTime.lowSeconds;
2246 // Subtract to convert to NSPR epoch of 1970
2247 result -= kJanuaryFirst1970Seconds;
2248 // Convert to millisecs
2249 result *= kMillisecsPerSec;
2250 // Convert the fraction to millisecs and add it
2251 result += ((PRInt64)utcTime.fraction * kMillisecsPerSec) / kUTCDateTimeFractionDivisor;
2252
2253 return result;
2254}
2255
2256void nsLocalFile::NSPRtoHFSPlusTime(PRInt64 nsprTime, UTCDateTime& utcTime)
2257{
2258 PRInt64 fraction = nsprTime % kMillisecsPerSec;
2259 PRInt64 seconds = (nsprTime / kMillisecsPerSec) + kJanuaryFirst1970Seconds;
2260 utcTime.highSeconds = (UInt16)((PRUint64)seconds >> 32);
2261 utcTime.lowSeconds = (UInt32)seconds;
2262 utcTime.fraction = (UInt16)((fraction * kUTCDateTimeFractionDivisor) / kMillisecsPerSec);
2263}
2264
2265nsresult nsLocalFile::CFStringReftoUTF8(CFStringRef aInStrRef, nsACString& aOutStr)
2266{
2267 nsresult rv = NS_ERROR_FAILURE;
2268 CFIndex usedBufLen, inStrLen = ::CFStringGetLength(aInStrRef);
2269 CFIndex charsConverted = ::CFStringGetBytes(aInStrRef, CFRangeMake(0, inStrLen),
2270 kCFStringEncodingUTF8, 0, PR_FALSE, nsnull, 0, &usedBufLen);
2271 if (charsConverted == inStrLen) {
2272#if 0 /* bird: too new? */
2273 aOutStr.SetLength(usedBufLen);
2274 if (aOutStr.Length() != usedBufLen)
2275 return NS_ERROR_OUT_OF_MEMORY;
2276 UInt8 *buffer = (UInt8*) aOutStr.BeginWriting();
2277
2278 ::CFStringGetBytes(aInStrRef, CFRangeMake(0, inStrLen),
2279 kCFStringEncodingUTF8, 0, false, buffer, usedBufLen, &usedBufLen);
2280 rv = NS_OK;
2281#else
2282 nsAutoBuffer<UInt8, FILENAME_BUFFER_SIZE> buffer;
2283 if (buffer.EnsureElemCapacity(usedBufLen + 1)) {
2284 ::CFStringGetBytes(aInStrRef, CFRangeMake(0, inStrLen),
2285 kCFStringEncodingUTF8, 0, false, buffer.get(), usedBufLen, &usedBufLen);
2286 buffer.get()[usedBufLen] = '\0';
2287 aOutStr.Assign(nsDependentCString((char*)buffer.get()));
2288 rv = NS_OK;
2289 }
2290#endif
2291 }
2292 return rv;
2293}
2294
2295// nsIHashable
2296
2297NS_IMETHODIMP
2298nsLocalFile::Equals(nsIHashable* aOther, PRBool *aResult)
2299{
2300 return EqualsInternal(aOther, PR_FALSE, aResult);
2301}
2302
2303NS_IMETHODIMP
2304nsLocalFile::GetHashCode(PRUint32 *aResult)
2305{
2306 CFStringRef pathStrRef = ::CFURLCopyFileSystemPath(mBaseRef, kCFURLPOSIXPathStyle);
2307 nsCAutoString path;
2308 CFStringReftoUTF8(pathStrRef, path);
2309 *aResult = HashString(path);
2310 return NS_OK;
2311}
2312
2313//*****************************************************************************
2314// Global Functions
2315//*****************************************************************************
2316#pragma mark -
2317#pragma mark [Global Functions]
2318
2319void nsLocalFile::GlobalInit()
2320{
2321}
2322
2323void nsLocalFile::GlobalShutdown()
2324{
2325}
2326
2327nsresult NS_NewLocalFile(const nsAString& path, PRBool followLinks, nsILocalFile* *result)
2328{
2329 nsLocalFile* file = new nsLocalFile;
2330 if (file == nsnull)
2331 return NS_ERROR_OUT_OF_MEMORY;
2332 NS_ADDREF(file);
2333
2334 file->SetFollowLinks(followLinks);
2335
2336 if (!path.IsEmpty()) {
2337 nsresult rv = file->InitWithPath(path);
2338 if (NS_FAILED(rv)) {
2339 NS_RELEASE(file);
2340 return rv;
2341 }
2342 }
2343 *result = file;
2344 return NS_OK;
2345}
2346
2347nsresult NS_NewNativeLocalFile(const nsACString& path, PRBool followLinks, nsILocalFile **result)
2348{
2349 return NS_NewLocalFile(NS_ConvertUTF8toUTF16(path), followLinks, result);
2350}
2351
2352nsresult NS_NewLocalFileWithFSSpec(const FSSpec* inSpec, PRBool followLinks, nsILocalFileMac **result)
2353{
2354 nsLocalFile* file = new nsLocalFile();
2355 if (file == nsnull)
2356 return NS_ERROR_OUT_OF_MEMORY;
2357 NS_ADDREF(file);
2358
2359 file->SetFollowLinks(followLinks);
2360
2361 nsresult rv = file->InitWithFSSpec(inSpec);
2362 if (NS_FAILED(rv)) {
2363 NS_RELEASE(file);
2364 return rv;
2365 }
2366 *result = file;
2367 return NS_OK;
2368}
2369
2370nsresult NS_NewLocalFileWithFSRef(const FSRef* aFSRef, PRBool aFollowLinks, nsILocalFileMac** result)
2371{
2372 nsLocalFile* file = new nsLocalFile();
2373 if (file == nsnull)
2374 return NS_ERROR_OUT_OF_MEMORY;
2375 NS_ADDREF(file);
2376
2377 file->SetFollowLinks(aFollowLinks);
2378
2379 nsresult rv = file->InitWithFSRef(aFSRef);
2380 if (NS_FAILED(rv)) {
2381 NS_RELEASE(file);
2382 return rv;
2383 }
2384 *result = file;
2385 return NS_OK;
2386}
2387
2388//*****************************************************************************
2389// Static Functions
2390//*****************************************************************************
2391
2392static nsresult MacErrorMapper(OSErr inErr)
2393{
2394 nsresult outErr;
2395
2396 switch (inErr)
2397 {
2398 case noErr:
2399 outErr = NS_OK;
2400 break;
2401
2402 case fnfErr:
2403 outErr = NS_ERROR_FILE_NOT_FOUND;
2404 break;
2405
2406 case dupFNErr:
2407 outErr = NS_ERROR_FILE_ALREADY_EXISTS;
2408 break;
2409
2410 case dskFulErr:
2411 outErr = NS_ERROR_FILE_DISK_FULL;
2412 break;
2413
2414 case fLckdErr:
2415 outErr = NS_ERROR_FILE_IS_LOCKED;
2416 break;
2417
2418 // Can't find good map for some
2419 case bdNamErr:
2420 outErr = NS_ERROR_FAILURE;
2421 break;
2422
2423 default:
2424 outErr = NS_ERROR_FAILURE;
2425 break;
2426 }
2427 return outErr;
2428}
2429
2430static OSErr FindRunningAppBySignature(OSType aAppSig, ProcessSerialNumber& outPsn)
2431{
2432 ProcessInfoRec info;
2433 OSErr err = noErr;
2434
2435 outPsn.highLongOfPSN = 0;
2436 outPsn.lowLongOfPSN = kNoProcess;
2437
2438 while (PR_TRUE)
2439 {
2440 err = ::GetNextProcess(&outPsn);
2441 if (err == procNotFound)
2442 break;
2443 if (err != noErr)
2444 return err;
2445 info.processInfoLength = sizeof(ProcessInfoRec);
2446 info.processName = nil;
2447 info.processAppSpec = nil;
2448 err = ::GetProcessInformation(&outPsn, &info);
2449 if (err != noErr)
2450 return err;
2451
2452 if (info.processSignature == aAppSig)
2453 return noErr;
2454 }
2455 return procNotFound;
2456}
2457
2458// Convert a UTF-8 string to a UTF-16 string while normalizing to
2459// Normalization Form C (composed Unicode). We need this because
2460// Mac OS X file system uses NFD (Normalization Form D : decomposed Unicode)
2461// while most other OS', server-side programs usually expect NFC.
2462
2463typedef void (*UnicodeNormalizer) (CFMutableStringRef, CFStringNormalizationForm);
2464static void CopyUTF8toUTF16NFC(const nsACString& aSrc, nsAString& aResult)
2465{
2466 static PRBool sChecked = PR_FALSE;
2467 static UnicodeNormalizer sUnicodeNormalizer = NULL;
2468
2469 // CFStringNormalize was not introduced until Mac OS 10.2
2470 if (!sChecked) {
2471 CFBundleRef carbonBundle =
2472 CFBundleGetBundleWithIdentifier(CFSTR("com.apple.Carbon"));
2473 if (carbonBundle)
2474 sUnicodeNormalizer = (UnicodeNormalizer)
2475 ::CFBundleGetFunctionPointerForName(carbonBundle,
2476 CFSTR("CFStringNormalize"));
2477 sChecked = PR_TRUE;
2478 }
2479
2480 if (!sUnicodeNormalizer) { // OS X 10.2 or earlier
2481 CopyUTF8toUTF16(aSrc, aResult);
2482 return;
2483 }
2484
2485 const nsAFlatCString &inFlatSrc = PromiseFlatCString(aSrc);
2486
2487 // The number of 16bit code units in a UTF-16 string will never be
2488 // larger than the number of bytes in the corresponding UTF-8 string.
2489 CFMutableStringRef inStr =
2490 ::CFStringCreateMutable(NULL, inFlatSrc.Length());
2491
2492 if (!inStr) {
2493 CopyUTF8toUTF16(aSrc, aResult);
2494 return;
2495 }
2496
2497 ::CFStringAppendCString(inStr, inFlatSrc.get(), kCFStringEncodingUTF8);
2498
2499 sUnicodeNormalizer(inStr, kCFStringNormalizationFormC);
2500
2501 CFIndex length = CFStringGetLength(inStr);
2502 const UniChar* chars = CFStringGetCharactersPtr(inStr);
2503
2504 if (chars)
2505 aResult.Assign(chars, length);
2506 else {
2507 nsAutoBuffer<UniChar, FILENAME_BUFFER_SIZE> buffer;
2508 if (!buffer.EnsureElemCapacity(length))
2509 CopyUTF8toUTF16(aSrc, aResult);
2510 else {
2511 CFStringGetCharacters(inStr, CFRangeMake(0, length), buffer.get());
2512 aResult.Assign(buffer.get(), length);
2513 }
2514 }
2515 CFRelease(inStr);
2516}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette