2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2001, 2002
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Conrad Carlen <ccarlen@netscape.com>
|
|
|
|
* Jungshik Shin <jshin@mailaps.org>
|
|
|
|
* Asaf Romano <mozilla.mano@sent.com>
|
|
|
|
* Mark Mentovai <mark@moxienet.com>
|
2009-01-15 14:57:43 -08:00
|
|
|
* Josh Aas <josh@mozilla.com>
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsLocalFile.h"
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
|
2008-02-29 15:24:53 -08:00
|
|
|
#include "nsObjCExceptions.h"
|
2009-03-07 18:59:40 -08:00
|
|
|
#include "nsAutoPtr.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsIDirectoryEnumerator.h"
|
|
|
|
#include "nsISimpleEnumerator.h"
|
|
|
|
#include "nsITimelineService.h"
|
2009-01-18 12:14:14 -08:00
|
|
|
#include "nsTArray.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "plbase64.h"
|
|
|
|
#include "prmem.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
|
|
|
|
#include "nsTraceRefcntImpl.h"
|
|
|
|
|
|
|
|
#include <Carbon/Carbon.h>
|
2008-02-29 15:24:53 -08:00
|
|
|
#import <Cocoa/Cocoa.h>
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
2009-04-29 23:53:43 -07:00
|
|
|
#include <sys/statvfs.h>
|
2007-03-22 10:30:00 -07:00
|
|
|
#include <stdlib.h>
|
2009-04-23 19:22:20 -07:00
|
|
|
#include <utime.h>
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-12 13:04:25 -07:00
|
|
|
#define CHECK_INIT() \
|
2007-03-22 10:30:00 -07:00
|
|
|
PR_BEGIN_MACRO \
|
2009-04-09 17:46:49 -07:00
|
|
|
if (!mBaseURL) \
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_NOT_INITIALIZED; \
|
|
|
|
PR_END_MACRO
|
|
|
|
|
|
|
|
static nsresult MacErrorMapper(OSErr inErr);
|
|
|
|
static void CopyUTF8toUTF16NFC(const nsACString& aSrc, nsAString& aResult);
|
2009-07-29 21:47:06 -07:00
|
|
|
static nsresult CFStringReftoUTF8(CFStringRef aInStrRef, nsACString& aOutStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-27 02:54:43 -07:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark [FSRef operator==]
|
|
|
|
|
|
|
|
bool operator==(const FSRef& lhs, const FSRef& rhs)
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
|
|
|
|
|
|
|
return (::FSCompareFSRefs(&lhs, &rhs) == noErr);
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(false);
|
|
|
|
}
|
2009-04-30 17:01:06 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark [StFollowLinksState]
|
|
|
|
|
|
|
|
class StFollowLinksState
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
StFollowLinksState(nsLocalFile& aFile) :
|
|
|
|
mFile(aFile)
|
|
|
|
{
|
|
|
|
mFile.GetFollowLinks(&mSavedState);
|
|
|
|
}
|
|
|
|
|
|
|
|
StFollowLinksState(nsLocalFile& aFile, PRBool followLinksState) :
|
|
|
|
mFile(aFile)
|
|
|
|
{
|
|
|
|
mFile.GetFollowLinks(&mSavedState);
|
|
|
|
mFile.SetFollowLinks(followLinksState);
|
|
|
|
}
|
|
|
|
|
|
|
|
~StFollowLinksState()
|
|
|
|
{
|
|
|
|
mFile.SetFollowLinks(mSavedState);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsLocalFile& mFile;
|
|
|
|
PRBool mSavedState;
|
|
|
|
};
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark [nsDirEnumerator]
|
|
|
|
|
2009-09-27 02:54:43 -07:00
|
|
|
class nsDirEnumerator : public nsISimpleEnumerator,
|
|
|
|
public nsIDirectoryEnumerator
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-09-27 02:54:43 -07:00
|
|
|
public:
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
nsDirEnumerator() :
|
|
|
|
mIterator(nsnull),
|
|
|
|
mFSRefsArray(nsnull),
|
|
|
|
mArrayCnt(0), mArrayIndex(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Init(nsILocalFileMac* parent)
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
|
|
|
NS_ENSURE_ARG(parent);
|
|
|
|
|
|
|
|
OSErr err;
|
|
|
|
nsresult rv;
|
|
|
|
FSRef parentRef;
|
|
|
|
|
|
|
|
rv = parent->GetFSRef(&parentRef);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
mFSRefsArray = (FSRef *)nsMemory::Alloc(sizeof(FSRef)
|
|
|
|
* kRequestCountPerIteration);
|
|
|
|
if (!mFSRefsArray)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
err = ::FSOpenIterator(&parentRef, kFSIterateFlat, &mIterator);
|
|
|
|
if (err != noErr)
|
|
|
|
return MacErrorMapper(err);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD HasMoreElements(PRBool *result)
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
|
|
|
if (!mIterator || !mFSRefsArray) {
|
|
|
|
*result = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mNext == nsnull) {
|
|
|
|
if (mArrayIndex >= mArrayCnt) {
|
|
|
|
ItemCount actualCnt;
|
|
|
|
OSErr err = ::FSGetCatalogInfoBulk(mIterator,
|
|
|
|
kRequestCountPerIteration,
|
|
|
|
&actualCnt,
|
|
|
|
nsnull,
|
|
|
|
kFSCatInfoNone,
|
|
|
|
nsnull,
|
|
|
|
mFSRefsArray,
|
|
|
|
nsnull,
|
|
|
|
nsnull);
|
|
|
|
|
|
|
|
if (err == noErr || err == errFSNoMoreItems) {
|
|
|
|
mArrayCnt = actualCnt;
|
|
|
|
mArrayIndex = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mArrayIndex < mArrayCnt) {
|
|
|
|
nsRefPtr<nsLocalFile> newFile = new nsLocalFile;
|
|
|
|
if (!newFile)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
FSRef fsRef = mFSRefsArray[mArrayIndex];
|
|
|
|
if (NS_FAILED(newFile->InitWithFSRef(&fsRef)))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
mArrayIndex++;
|
|
|
|
mNext = newFile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = mNext != nsnull;
|
|
|
|
if (!*result)
|
|
|
|
Close();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD GetNext(nsISupports **result)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
|
|
|
*result = nsnull;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
PRBool hasMore;
|
|
|
|
rv = HasMoreElements(&hasMore);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
*result = mNext; // might return nsnull
|
|
|
|
NS_IF_ADDREF(*result);
|
|
|
|
|
|
|
|
mNext = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD GetNextFile(nsIFile **result)
|
|
|
|
{
|
|
|
|
*result = nsnull;
|
|
|
|
PRBool hasMore = PR_FALSE;
|
|
|
|
nsresult rv = HasMoreElements(&hasMore);
|
|
|
|
if (NS_FAILED(rv) || !hasMore)
|
|
|
|
return rv;
|
|
|
|
*result = mNext;
|
|
|
|
NS_IF_ADDREF(*result);
|
|
|
|
mNext = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Close()
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
|
|
|
if (mIterator) {
|
|
|
|
::FSCloseIterator(mIterator);
|
|
|
|
mIterator = nsnull;
|
|
|
|
}
|
|
|
|
if (mFSRefsArray) {
|
|
|
|
nsMemory::Free(mFSRefsArray);
|
|
|
|
mFSRefsArray = nsnull;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~nsDirEnumerator()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// According to Apple doc, request the number of objects
|
|
|
|
// per call that will fit in 4 VM pages.
|
|
|
|
enum {
|
|
|
|
kRequestCountPerIteration = ((4096 * 4) / sizeof(FSRef))
|
|
|
|
};
|
|
|
|
|
|
|
|
nsCOMPtr<nsILocalFileMac> mNext;
|
|
|
|
|
|
|
|
FSIterator mIterator;
|
|
|
|
FSRef *mFSRefsArray;
|
|
|
|
PRInt32 mArrayCnt, mArrayIndex;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS2(nsDirEnumerator, nsISimpleEnumerator, nsIDirectoryEnumerator)
|
|
|
|
|
2009-09-27 02:54:43 -07:00
|
|
|
const char kPathSepChar = '/';
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark [CTORs/DTOR]
|
|
|
|
|
|
|
|
nsLocalFile::nsLocalFile() :
|
2009-07-29 21:47:06 -07:00
|
|
|
mBaseURL(NULL),
|
2009-06-10 22:23:57 -07:00
|
|
|
mFollowLinks(PR_TRUE)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-07-29 21:47:06 -07:00
|
|
|
mPath[0] = '\0';
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsLocalFile::nsLocalFile(const nsLocalFile& src) :
|
2009-07-29 21:47:06 -07:00
|
|
|
mBaseURL(NULL),
|
2009-06-10 22:23:57 -07:00
|
|
|
mFollowLinks(src.mFollowLinks)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-07-29 21:47:06 -07:00
|
|
|
SetBaseURL(src.mBaseURL);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsLocalFile::~nsLocalFile()
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2009-04-09 17:46:49 -07:00
|
|
|
if (mBaseURL)
|
|
|
|
::CFRelease(mBaseURL);
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark [nsISupports]
|
|
|
|
|
2008-02-01 18:07:49 -08:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS4(nsLocalFile,
|
|
|
|
nsILocalFileMac,
|
|
|
|
nsILocalFile,
|
|
|
|
nsIFile,
|
|
|
|
nsIHashable)
|
|
|
|
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_IMETHODIMP nsLocalFile::nsLocalFileConstructor(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aInstancePtr);
|
|
|
|
NS_ENSURE_NO_AGGREGATION(outer);
|
|
|
|
|
|
|
|
nsLocalFile* inst = new nsLocalFile();
|
|
|
|
if (inst == NULL)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsresult rv = inst->QueryInterface(aIID, aInstancePtr);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
delete inst;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark [nsIFile]
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::Append(const nsAString& aNode)
|
|
|
|
{
|
|
|
|
return AppendNative(NS_ConvertUTF16toUTF8(aNode));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::AppendNative(const nsACString& aNode)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsACString::const_iterator start, end;
|
|
|
|
aNode.BeginReading(start);
|
|
|
|
aNode.EndReading(end);
|
|
|
|
if (FindCharInReadable(kPathSepChar, start, end))
|
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
|
|
|
|
|
|
|
CFStringRef nodeStrRef = ::CFStringCreateWithCString(kCFAllocatorDefault,
|
|
|
|
PromiseFlatCString(aNode).get(),
|
|
|
|
kCFStringEncodingUTF8);
|
|
|
|
if (nodeStrRef) {
|
|
|
|
CFURLRef newRef = ::CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,
|
2009-04-09 17:46:49 -07:00
|
|
|
mBaseURL, nodeStrRef, PR_FALSE);
|
2007-03-22 10:30:00 -07:00
|
|
|
::CFRelease(nodeStrRef);
|
|
|
|
if (newRef) {
|
2009-06-10 22:23:57 -07:00
|
|
|
SetBaseURL(newRef);
|
2007-03-22 10:30:00 -07:00
|
|
|
::CFRelease(newRef);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::Normalize()
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
char resolved_path[PATH_MAX] = "";
|
2009-07-29 21:47:06 -07:00
|
|
|
if (!realpath(mPath, resolved_path))
|
2007-03-22 10:30:00 -07:00
|
|
|
return NSRESULT_FOR_ERRNO();
|
|
|
|
|
|
|
|
// Need to know whether we're a directory to create a new CFURLRef
|
|
|
|
PRBool isDirectory;
|
|
|
|
nsresult rv = IsDirectory(&isDirectory);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = NS_ERROR_FAILURE;
|
2009-06-10 22:23:57 -07:00
|
|
|
CFStringRef pathStrRef = ::CFStringCreateWithCString(kCFAllocatorDefault,
|
|
|
|
resolved_path,
|
|
|
|
kCFStringEncodingUTF8);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (pathStrRef) {
|
|
|
|
CFURLRef newURLRef =
|
|
|
|
::CFURLCreateWithFileSystemPath(kCFAllocatorDefault, pathStrRef,
|
|
|
|
kCFURLPOSIXPathStyle, isDirectory);
|
|
|
|
if (newURLRef) {
|
2009-06-10 22:23:57 -07:00
|
|
|
SetBaseURL(newURLRef);
|
2007-03-22 10:30:00 -07:00
|
|
|
::CFRelease(newURLRef);
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
::CFRelease(pathStrRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::Create(PRUint32 type, PRUint32 permissions)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (type != NORMAL_FILE_TYPE && type != DIRECTORY_TYPE)
|
|
|
|
return NS_ERROR_FILE_UNKNOWN_TYPE;
|
|
|
|
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-01-18 12:14:14 -08:00
|
|
|
nsTArray<nsString> nonExtantNodes;
|
2009-04-09 17:46:49 -07:00
|
|
|
CFURLRef pathURLRef = mBaseURL;
|
2007-03-22 10:30:00 -07:00
|
|
|
FSRef pathFSRef;
|
|
|
|
CFStringRef leafStrRef = nsnull;
|
2009-07-29 21:47:06 -07:00
|
|
|
nsAutoTArray<UniChar, NAME_MAX> buffer;
|
2007-03-22 10:30:00 -07:00
|
|
|
Boolean success;
|
|
|
|
|
|
|
|
// Work backwards through the path to find the last node which
|
|
|
|
// exists. Place the nodes which don't exist in an array and we'll
|
|
|
|
// create those below.
|
|
|
|
while ((success = ::CFURLGetFSRef(pathURLRef, &pathFSRef)) == false) {
|
|
|
|
leafStrRef = ::CFURLCopyLastPathComponent(pathURLRef);
|
|
|
|
if (!leafStrRef)
|
|
|
|
break;
|
|
|
|
CFIndex leafLen = ::CFStringGetLength(leafStrRef);
|
2007-09-18 16:12:06 -07:00
|
|
|
if (!buffer.SetLength(leafLen + 1))
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
2007-09-18 16:12:06 -07:00
|
|
|
::CFStringGetCharacters(leafStrRef, CFRangeMake(0, leafLen), buffer.Elements());
|
|
|
|
buffer[leafLen] = '\0';
|
2009-01-18 12:14:14 -08:00
|
|
|
nonExtantNodes.AppendElement(nsString(nsDependentString(buffer.Elements())));
|
2007-03-22 10:30:00 -07:00
|
|
|
::CFRelease(leafStrRef);
|
|
|
|
leafStrRef = nsnull;
|
|
|
|
|
|
|
|
// Get the parent of the leaf for the next go round
|
|
|
|
CFURLRef parent = ::CFURLCreateCopyDeletingLastPathComponent(NULL, pathURLRef);
|
|
|
|
if (!parent)
|
|
|
|
break;
|
2009-04-09 17:46:49 -07:00
|
|
|
if (pathURLRef != mBaseURL)
|
2007-03-22 10:30:00 -07:00
|
|
|
::CFRelease(pathURLRef);
|
|
|
|
pathURLRef = parent;
|
|
|
|
}
|
2009-04-09 17:46:49 -07:00
|
|
|
if (pathURLRef != mBaseURL)
|
2007-03-22 10:30:00 -07:00
|
|
|
::CFRelease(pathURLRef);
|
|
|
|
if (leafStrRef != nsnull)
|
|
|
|
::CFRelease(leafStrRef);
|
|
|
|
if (!success)
|
|
|
|
return NS_ERROR_FAILURE;
|
2009-01-18 12:14:14 -08:00
|
|
|
PRInt32 nodesToCreate = PRInt32(nonExtantNodes.Length());
|
2007-03-22 10:30:00 -07:00
|
|
|
if (nodesToCreate == 0)
|
|
|
|
return NS_ERROR_FILE_ALREADY_EXISTS;
|
|
|
|
|
|
|
|
OSErr err;
|
|
|
|
nsAutoString nextNodeName;
|
|
|
|
for (PRInt32 i = nodesToCreate - 1; i > 0; i--) {
|
2009-01-18 12:14:14 -08:00
|
|
|
nextNodeName = nonExtantNodes[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
err = ::FSCreateDirectoryUnicode(&pathFSRef,
|
|
|
|
nextNodeName.Length(),
|
|
|
|
(const UniChar *)nextNodeName.get(),
|
|
|
|
kFSCatInfoNone,
|
|
|
|
nsnull, &pathFSRef, nsnull, nsnull);
|
|
|
|
if (err != noErr)
|
|
|
|
return MacErrorMapper(err);
|
|
|
|
}
|
2009-01-18 12:14:14 -08:00
|
|
|
nextNodeName = nonExtantNodes[0];
|
2007-03-22 10:30:00 -07:00
|
|
|
if (type == NORMAL_FILE_TYPE) {
|
|
|
|
err = ::FSCreateFileUnicode(&pathFSRef,
|
|
|
|
nextNodeName.Length(),
|
|
|
|
(const UniChar *)nextNodeName.get(),
|
|
|
|
kFSCatInfoNone,
|
|
|
|
nsnull, nsnull, nsnull);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
err = ::FSCreateDirectoryUnicode(&pathFSRef,
|
|
|
|
nextNodeName.Length(),
|
|
|
|
(const UniChar *)nextNodeName.get(),
|
|
|
|
kFSCatInfoNone,
|
|
|
|
nsnull, nsnull, nsnull, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
return MacErrorMapper(err);
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetLeafName(nsAString& aLeafName)
|
|
|
|
{
|
|
|
|
nsCAutoString nativeString;
|
|
|
|
nsresult rv = GetNativeLeafName(nativeString);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
CopyUTF8toUTF16NFC(nativeString, aLeafName);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::SetLeafName(const nsAString& aLeafName)
|
|
|
|
{
|
|
|
|
return SetNativeLeafName(NS_ConvertUTF16toUTF8(aLeafName));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetNativeLeafName(nsACString& aNativeLeafName)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
2009-04-09 17:46:49 -07:00
|
|
|
CFStringRef leafStrRef = ::CFURLCopyLastPathComponent(mBaseURL);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (leafStrRef) {
|
|
|
|
rv = CFStringReftoUTF8(leafStrRef, aNativeLeafName);
|
|
|
|
::CFRelease(leafStrRef);
|
|
|
|
}
|
2008-02-29 15:24:53 -08:00
|
|
|
return rv;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::SetNativeLeafName(const nsACString& aNativeLeafName)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
2009-04-09 17:46:49 -07:00
|
|
|
CFURLRef parentURLRef = ::CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorDefault, mBaseURL);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (parentURLRef) {
|
|
|
|
CFStringRef nodeStrRef = ::CFStringCreateWithCString(kCFAllocatorDefault,
|
|
|
|
PromiseFlatCString(aNativeLeafName).get(),
|
|
|
|
kCFStringEncodingUTF8);
|
|
|
|
|
|
|
|
if (nodeStrRef) {
|
|
|
|
CFURLRef newURLRef = ::CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,
|
|
|
|
parentURLRef, nodeStrRef, PR_FALSE);
|
|
|
|
if (newURLRef) {
|
2009-06-10 22:23:57 -07:00
|
|
|
SetBaseURL(newURLRef);
|
2007-03-22 10:30:00 -07:00
|
|
|
::CFRelease(newURLRef);
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
::CFRelease(nodeStrRef);
|
|
|
|
}
|
|
|
|
::CFRelease(parentURLRef);
|
|
|
|
}
|
|
|
|
return rv;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::CopyTo(nsIFile *newParentDir, const nsAString& newName)
|
|
|
|
{
|
|
|
|
return CopyInternal(newParentDir, newName, PR_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::CopyToNative(nsIFile *newParentDir, const nsACString& newName)
|
|
|
|
{
|
|
|
|
return CopyInternal(newParentDir, NS_ConvertUTF8toUTF16(newName), PR_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::CopyToFollowingLinks(nsIFile *newParentDir, const nsAString& newName)
|
|
|
|
{
|
|
|
|
return CopyInternal(newParentDir, newName, PR_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::CopyToFollowingLinksNative(nsIFile *newParentDir, const nsACString& newName)
|
|
|
|
{
|
|
|
|
return CopyInternal(newParentDir, NS_ConvertUTF8toUTF16(newName), PR_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::MoveTo(nsIFile *newParentDir, const nsAString& newName)
|
|
|
|
{
|
|
|
|
return MoveToNative(newParentDir, NS_ConvertUTF16toUTF8(newName));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::MoveToNative(nsIFile *newParentDir, const nsACString& newName)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
StFollowLinksState followLinks(*this, PR_FALSE);
|
|
|
|
|
|
|
|
PRBool isDirectory;
|
|
|
|
nsresult rv = IsDirectory(&isDirectory);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
// Build the destination path.
|
|
|
|
nsCOMPtr<nsIFile> parentDir = newParentDir;
|
|
|
|
if (!parentDir) {
|
|
|
|
if (newName.IsEmpty())
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
rv = GetParent(getter_AddRefs(parentDir));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PRBool exists;
|
|
|
|
rv = parentDir->Exists(&exists);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (!exists) {
|
|
|
|
rv = parentDir->Create(nsIFile::DIRECTORY_TYPE, 0777);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCAutoString destPath;
|
|
|
|
rv = parentDir->GetNativePath(destPath);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (!newName.IsEmpty())
|
|
|
|
destPath.Append(NS_LITERAL_CSTRING("/") + newName);
|
|
|
|
else {
|
|
|
|
nsCAutoString leafName;
|
|
|
|
rv = GetNativeLeafName(leafName);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
destPath.Append(NS_LITERAL_CSTRING("/") + leafName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the move.
|
2009-07-29 21:47:06 -07:00
|
|
|
if (rename(mPath, destPath.get()) != 0) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (errno == EXDEV) {
|
|
|
|
// Can't move across volume (device) boundaries. Copy and remove.
|
|
|
|
rv = CopyToNative(parentDir, newName);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// Permit removal failure.
|
|
|
|
Remove(PR_TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rv = NSRESULT_FOR_ERRNO();
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update |this| to refer to the moved file.
|
|
|
|
CFURLRef newBaseRef =
|
|
|
|
::CFURLCreateFromFileSystemRepresentation(NULL, (UInt8*)destPath.get(),
|
|
|
|
destPath.Length(), isDirectory);
|
|
|
|
if (!newBaseRef)
|
|
|
|
return NS_ERROR_FAILURE;
|
2009-06-10 22:23:57 -07:00
|
|
|
SetBaseURL(newBaseRef);
|
2007-03-22 10:30:00 -07:00
|
|
|
::CFRelease(newBaseRef);
|
|
|
|
|
|
|
|
return rv;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::Remove(PRBool recursive)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-06-10 22:23:57 -07:00
|
|
|
// if we're a symlink, never remove target
|
2007-03-22 10:30:00 -07:00
|
|
|
StFollowLinksState followLinks(*this, PR_FALSE);
|
|
|
|
|
|
|
|
PRBool isDirectory;
|
|
|
|
nsresult rv = IsDirectory(&isDirectory);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (recursive && isDirectory) {
|
2009-02-11 20:26:59 -08:00
|
|
|
CFURLRef urlRef;
|
|
|
|
rv = GetCFURL(&urlRef);
|
|
|
|
if (NS_SUCCEEDED(rv) && urlRef) {
|
|
|
|
NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
|
|
|
|
BOOL removeSuccess = [[NSFileManager defaultManager] removeFileAtPath:[(NSURL*)urlRef path] handler:nil];
|
|
|
|
[ap release];
|
|
|
|
::CFRelease(urlRef);
|
|
|
|
rv = removeSuccess ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
int status;
|
|
|
|
if (isDirectory)
|
2009-07-29 21:47:06 -07:00
|
|
|
status = rmdir(mPath);
|
2007-03-22 10:30:00 -07:00
|
|
|
else
|
2009-07-29 21:47:06 -07:00
|
|
|
status = unlink(mPath);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
rv = NSRESULT_FOR_ERRNO();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-03-06 10:32:57 -08:00
|
|
|
// Only send back permissions bits: maybe we want to send back the whole
|
|
|
|
// mode_t to permit checks against other file types?
|
|
|
|
#define NORMALIZE_PERMS(mode) ((mode)& (S_IRWXU | S_IRWXG | S_IRWXO))
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP nsLocalFile::GetPermissions(PRUint32 *aPermissions)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aPermissions);
|
2009-03-06 10:32:57 -08:00
|
|
|
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2009-03-06 10:32:57 -08:00
|
|
|
|
2009-06-23 05:55:59 -07:00
|
|
|
struct STAT buf;
|
|
|
|
nsresult rv = FillStatBufferInternal(&buf);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return NSRESULT_FOR_ERRNO();
|
2008-02-29 15:24:53 -08:00
|
|
|
|
2009-06-23 05:55:59 -07:00
|
|
|
*aPermissions = NORMALIZE_PERMS(buf.st_mode);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::SetPermissions(PRUint32 aPermissions)
|
|
|
|
{
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2009-03-06 10:32:57 -08:00
|
|
|
|
2009-07-29 21:47:06 -07:00
|
|
|
if (chmod(mPath, aPermissions) < 0)
|
2009-06-23 05:55:59 -07:00
|
|
|
return NSRESULT_FOR_ERRNO();
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetPermissionsOfLink(PRUint32 *aPermissionsOfLink)
|
|
|
|
{
|
|
|
|
NS_ERROR("NS_ERROR_NOT_IMPLEMENTED");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::SetPermissionsOfLink(PRUint32 aPermissionsOfLink)
|
|
|
|
{
|
|
|
|
NS_ERROR("NS_ERROR_NOT_IMPLEMENTED");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetLastModifiedTime(PRInt64 *aLastModifiedTime)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aLastModifiedTime);
|
2009-04-23 19:22:20 -07:00
|
|
|
|
|
|
|
nsCAutoString path;
|
|
|
|
nsresult rv = GetPathInternal(path);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
2009-04-23 19:22:20 -07:00
|
|
|
PRFileInfo64 info;
|
|
|
|
if (PR_GetFileInfo64(path.get(), &info) != PR_SUCCESS)
|
|
|
|
return NSRESULT_FOR_ERRNO();
|
|
|
|
PRInt64 modTime = PRInt64(info.modifyTime);
|
|
|
|
if (modTime == 0)
|
|
|
|
*aLastModifiedTime = 0;
|
|
|
|
else
|
|
|
|
*aLastModifiedTime = modTime / PRInt64(PR_USEC_PER_MSEC);
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::SetLastModifiedTime(PRInt64 aLastModifiedTime)
|
|
|
|
{
|
2009-04-23 19:22:20 -07:00
|
|
|
nsCAutoString path;
|
|
|
|
nsresult rv = GetPathInternal(path);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2009-04-23 19:22:20 -07:00
|
|
|
int result;
|
|
|
|
if (aLastModifiedTime != 0) {
|
|
|
|
struct STAT statBuf;
|
2009-06-10 22:23:57 -07:00
|
|
|
nsresult rv = FillStatBufferInternal(&statBuf);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-23 19:22:20 -07:00
|
|
|
struct utimbuf ut;
|
|
|
|
ut.actime = statBuf.st_atime;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-23 19:22:20 -07:00
|
|
|
// convert milliseconds to seconds since the unix epoch
|
|
|
|
ut.modtime = (time_t)(PRFloat64(aLastModifiedTime) / PR_MSEC_PER_SEC);
|
|
|
|
result = utime(path.get(), &ut);
|
|
|
|
} else {
|
|
|
|
result = utime(path.get(), nsnull);
|
|
|
|
}
|
|
|
|
return NSRESULT_FOR_RETURN(result);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetLastModifiedTimeOfLink(PRInt64 *aLastModifiedTimeOfLink)
|
|
|
|
{
|
2009-04-23 19:22:20 -07:00
|
|
|
NS_ENSURE_ARG(aLastModifiedTimeOfLink);
|
|
|
|
|
|
|
|
nsCAutoString path;
|
|
|
|
nsresult rv = GetPathInternal(path);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
struct STAT sbuf;
|
|
|
|
if (LSTAT(path.get(), &sbuf) == -1)
|
|
|
|
return NSRESULT_FOR_ERRNO();
|
|
|
|
*aLastModifiedTimeOfLink = PRInt64(sbuf.st_mtime) * PRInt64(PR_MSEC_PER_SEC);
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-04-23 19:22:20 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP nsLocalFile::SetLastModifiedTimeOfLink(PRInt64 aLastModifiedTimeOfLink)
|
|
|
|
{
|
2009-04-23 19:22:20 -07:00
|
|
|
return SetLastModifiedTime(aLastModifiedTimeOfLink);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetFileSize(PRInt64 *aFileSize)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aFileSize);
|
|
|
|
*aFileSize = 0;
|
2009-05-01 21:52:21 -07:00
|
|
|
|
2009-07-29 21:47:06 -07:00
|
|
|
struct STAT buf;
|
|
|
|
nsresult rv = FillStatBufferInternal(&buf);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
2009-05-01 21:52:21 -07:00
|
|
|
if (!S_ISDIR(buf.st_mode))
|
|
|
|
*aFileSize = (PRInt64)buf.st_size;
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::SetFileSize(PRInt64 aFileSize)
|
|
|
|
{
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-04 23:39:04 -07:00
|
|
|
off_t size = (off_t)aFileSize;
|
2009-07-29 21:47:06 -07:00
|
|
|
if (truncate(mPath, size) == -1)
|
2009-07-04 23:39:04 -07:00
|
|
|
return NSRESULT_FOR_ERRNO();
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetFileSizeOfLink(PRInt64 *aFileSizeOfLink)
|
|
|
|
{
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(aFileSizeOfLink);
|
|
|
|
|
|
|
|
StFollowLinksState followLinks(*this, PR_FALSE);
|
|
|
|
return GetFileSize(aFileSizeOfLink);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetTarget(nsAString& aTarget)
|
|
|
|
{
|
2008-02-26 01:57:06 -08:00
|
|
|
nsCAutoString nativeString;
|
|
|
|
nsresult rv = GetNativeTarget(nativeString);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
CopyUTF8toUTF16NFC(nativeString, aTarget);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetNativeTarget(nsACString& aNativeTarget)
|
|
|
|
{
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2008-02-29 15:24:53 -08:00
|
|
|
|
2009-09-22 10:37:30 -07:00
|
|
|
aNativeTarget.Truncate();
|
|
|
|
|
|
|
|
struct STAT symStat;
|
|
|
|
if (LSTAT(mPath, &symStat) == -1)
|
|
|
|
return NSRESULT_FOR_ERRNO();
|
|
|
|
|
|
|
|
if (!S_ISLNK(symStat.st_mode))
|
2009-06-10 22:23:57 -07:00
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
2009-09-22 10:37:30 -07:00
|
|
|
|
|
|
|
PRInt32 size = (PRInt32)symStat.st_size;
|
|
|
|
char *target = (char *)nsMemory::Alloc(size + 1);
|
|
|
|
if (!target)
|
2009-06-10 22:23:57 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2009-09-22 10:37:30 -07:00
|
|
|
|
|
|
|
if (readlink(mPath, target, (size_t)size) < 0) {
|
|
|
|
nsMemory::Free(target);
|
|
|
|
return NSRESULT_FOR_ERRNO();
|
|
|
|
}
|
|
|
|
target[size] = '\0';
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIFile> self(this);
|
|
|
|
PRInt32 maxLinks = 40;
|
|
|
|
while (PR_TRUE) {
|
|
|
|
if (maxLinks-- == 0) {
|
|
|
|
rv = NS_ERROR_FILE_UNRESOLVABLE_SYMLINK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target[0] != '/') {
|
|
|
|
nsCOMPtr<nsIFile> parent;
|
|
|
|
if (NS_FAILED(rv = self->GetParent(getter_AddRefs(parent))))
|
|
|
|
break;
|
|
|
|
nsCOMPtr<nsILocalFile> localFile(do_QueryInterface(parent, &rv));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break;
|
|
|
|
if (NS_FAILED(rv = localFile->AppendRelativeNativePath(nsDependentCString(target))))
|
|
|
|
break;
|
|
|
|
if (NS_FAILED(rv = localFile->GetNativePath(aNativeTarget)))
|
|
|
|
break;
|
|
|
|
self = parent;
|
|
|
|
} else {
|
|
|
|
aNativeTarget = target;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsPromiseFlatCString &flatRetval = PromiseFlatCString(aNativeTarget);
|
|
|
|
|
|
|
|
// Any failure in testing the current target we'll just interpret
|
|
|
|
// as having reached our destiny.
|
|
|
|
if (LSTAT(flatRetval.get(), &symStat) == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// And of course we're done if it isn't a symlink.
|
|
|
|
if (!S_ISLNK(symStat.st_mode))
|
|
|
|
break;
|
|
|
|
|
|
|
|
PRInt32 newSize = (PRInt32)symStat.st_size;
|
|
|
|
if (newSize > size) {
|
|
|
|
char *newTarget = (char *)nsMemory::Realloc(target, newSize + 1);
|
|
|
|
if (!newTarget) {
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
target = newTarget;
|
|
|
|
size = newSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32 linkLen = readlink(flatRetval.get(), target, size);
|
|
|
|
if (linkLen == -1) {
|
|
|
|
rv = NSRESULT_FOR_ERRNO();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
target[linkLen] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
nsMemory::Free(target);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
aNativeTarget.Truncate();
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetPath(nsAString& aPath)
|
|
|
|
{
|
|
|
|
nsCAutoString nativeString;
|
|
|
|
nsresult rv = GetNativePath(nativeString);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
CopyUTF8toUTF16NFC(nativeString, aPath);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetNativePath(nsACString& aNativePath)
|
|
|
|
{
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-29 21:47:06 -07:00
|
|
|
aNativePath.Assign(mPath);
|
2008-02-29 15:24:53 -08:00
|
|
|
|
2009-07-29 21:47:06 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::Exists(PRBool *_retval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
2009-05-01 18:32:38 -07:00
|
|
|
|
|
|
|
struct STAT buf;
|
2009-07-29 21:47:06 -07:00
|
|
|
*_retval = (STAT(mPath, &buf) == 0);
|
2009-05-01 18:32:38 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::IsWritable(PRBool *_retval)
|
|
|
|
{
|
2009-02-11 20:26:59 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-02-11 20:26:59 -08:00
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
*_retval = PR_FALSE;
|
|
|
|
|
|
|
|
NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
|
|
|
|
// don't bother resolving, this always traverses symbolic links
|
2009-04-09 17:46:49 -07:00
|
|
|
*_retval = (PRBool)[[NSFileManager defaultManager] isWritableFileAtPath:[(NSURL*)mBaseURL path]];
|
2009-02-11 20:26:59 -08:00
|
|
|
[ap release];
|
2008-02-29 15:24:53 -08:00
|
|
|
|
2009-02-11 20:26:59 -08:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::IsReadable(PRBool *_retval)
|
|
|
|
{
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-02-11 20:26:59 -08:00
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
*_retval = PR_FALSE;
|
|
|
|
|
|
|
|
PRUint32 permissions;
|
|
|
|
nsresult rv = GetPermissions(&permissions);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
*_retval = ((permissions & S_IRUSR) != 0);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::IsExecutable(PRBool *_retval)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
*_retval = PR_FALSE;
|
|
|
|
|
|
|
|
FSRef fsRef;
|
|
|
|
nsresult rv = GetFSRefInternal(fsRef);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
LSRequestedInfo theInfoRequest = kLSRequestAllInfo;
|
|
|
|
LSItemInfoRecord theInfo;
|
|
|
|
if (::LSCopyItemInfoForRef(&fsRef, theInfoRequest, &theInfo) == noErr) {
|
|
|
|
if ((theInfo.flags & kLSItemInfoIsApplication) != 0)
|
|
|
|
*_retval = PR_TRUE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::IsHidden(PRBool *_retval)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-11-02 13:15:40 -08:00
|
|
|
CHECK_INIT();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
*_retval = PR_FALSE;
|
2009-11-02 13:15:40 -08:00
|
|
|
|
|
|
|
// If the leaf name begins with a '.', consider it invisible
|
|
|
|
nsAutoString name;
|
|
|
|
nsresult rv = GetLeafName(name);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2009-11-02 13:15:40 -08:00
|
|
|
if (name.Length() >= 1 && Substring(name, 0, 1).EqualsLiteral("."))
|
2007-03-22 10:30:00 -07:00
|
|
|
*_retval = PR_TRUE;
|
2009-11-02 13:15:40 -08:00
|
|
|
|
|
|
|
LSItemInfoRecord itemInfo;
|
|
|
|
LSCopyItemInfoForURL(mBaseURL, kLSRequestBasicFlagsOnly, &itemInfo);
|
|
|
|
*_retval = !!(itemInfo.flags & kLSItemInfoIsInvisible);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::IsDirectory(PRBool *_retval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
*_retval = PR_FALSE;
|
2009-06-10 22:23:57 -07:00
|
|
|
|
|
|
|
struct STAT buf;
|
|
|
|
nsresult rv = FillStatBufferInternal(&buf);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
2009-06-10 22:23:57 -07:00
|
|
|
*_retval = S_ISDIR(buf.st_mode);
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::IsFile(PRBool *_retval)
|
|
|
|
{
|
2009-05-02 15:14:47 -07:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
*_retval = PR_FALSE;
|
2009-06-10 22:23:57 -07:00
|
|
|
|
|
|
|
struct STAT buf;
|
|
|
|
nsresult rv = FillStatBufferInternal(&buf);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2009-06-10 22:23:57 -07:00
|
|
|
|
|
|
|
*_retval = S_ISREG(buf.st_mode);
|
|
|
|
|
2009-05-02 12:54:08 -07:00
|
|
|
return NS_OK;
|
2009-05-02 15:14:47 -07:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::IsSymlink(PRBool *_retval)
|
|
|
|
{
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_ENSURE_ARG(_retval);
|
|
|
|
*_retval = PR_FALSE;
|
|
|
|
|
2009-02-09 10:37:32 -08:00
|
|
|
struct stat symStat;
|
2009-07-29 21:47:06 -07:00
|
|
|
if (lstat(mPath, &symStat) < 0)
|
2009-02-09 10:37:32 -08:00
|
|
|
return NSRESULT_FOR_ERRNO();
|
|
|
|
*_retval = S_ISLNK(symStat.st_mode);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::IsSpecial(PRBool *_retval)
|
|
|
|
{
|
2009-06-10 22:23:57 -07:00
|
|
|
NS_ERROR("NS_ERROR_NOT_IMPLEMENTED");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::Clone(nsIFile **_retval)
|
|
|
|
{
|
|
|
|
// Just copy-construct ourselves
|
|
|
|
*_retval = new nsLocalFile(*this);
|
|
|
|
if (!*_retval)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(*_retval);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::Equals(nsIFile *inFile, PRBool *_retval)
|
|
|
|
{
|
2009-07-08 08:26:07 -07:00
|
|
|
NS_ENSURE_ARG(inFile);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
*_retval = PR_FALSE;
|
2009-07-08 08:26:07 -07:00
|
|
|
|
|
|
|
nsCAutoString inPath;
|
|
|
|
nsresult rv = inFile->GetNativePath(inPath);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2009-07-29 21:47:06 -07:00
|
|
|
*_retval = !strcmp(mPath, inPath.get());
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::Contains(nsIFile *inFile, PRBool recur, PRBool *_retval)
|
|
|
|
{
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
*_retval = PR_FALSE;
|
|
|
|
|
|
|
|
PRBool isDir;
|
|
|
|
nsresult rv = IsDirectory(&isDir);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (!isDir)
|
|
|
|
return NS_OK; // must be a dir to contain someone
|
|
|
|
|
|
|
|
nsCAutoString thisPath, inPath;
|
|
|
|
if (NS_FAILED(GetNativePath(thisPath)) || NS_FAILED(inFile->GetNativePath(inPath)))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
size_t thisPathLen = thisPath.Length();
|
|
|
|
if ((inPath.Length() > thisPathLen + 1) && (strncasecmp(thisPath.get(), inPath.get(), thisPathLen) == 0)) {
|
|
|
|
// Now make sure that the |inFile|'s path has a separator at thisPathLen,
|
|
|
|
// and there's at least one more character after that.
|
|
|
|
if (inPath[thisPathLen] == kPathSepChar)
|
|
|
|
*_retval = PR_TRUE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetParent(nsIFile * *aParent)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aParent);
|
|
|
|
*aParent = nsnull;
|
|
|
|
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// If it can be determined without error that a file does not
|
|
|
|
// have a parent, return nsnull for the parent and NS_OK as the result.
|
|
|
|
// See bug 133617.
|
|
|
|
nsresult rv = NS_OK;
|
2009-04-09 17:46:49 -07:00
|
|
|
CFURLRef parentURLRef = ::CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorDefault, mBaseURL);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (parentURLRef) {
|
|
|
|
// If the parent path is longer than file's path then
|
|
|
|
// CFURLCreateCopyDeletingLastPathComponent must have simply added
|
|
|
|
// two dots at the end - in this case indicate that there is no parent.
|
|
|
|
// See bug 332389.
|
2009-04-09 17:46:49 -07:00
|
|
|
CFStringRef path = ::CFURLGetString(mBaseURL);
|
2007-03-22 10:30:00 -07:00
|
|
|
CFStringRef newPath = ::CFURLGetString(parentURLRef);
|
|
|
|
if (::CFStringGetLength(newPath) < ::CFStringGetLength(path)) {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
2009-03-07 18:59:40 -08:00
|
|
|
nsRefPtr<nsLocalFile> newFile = new nsLocalFile;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (newFile) {
|
|
|
|
rv = newFile->InitWithCFURL(parentURLRef);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
NS_ADDREF(*aParent = newFile);
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
::CFRelease(parentURLRef);
|
|
|
|
}
|
|
|
|
return rv;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetDirectoryEntries(nsISimpleEnumerator **aDirectoryEntries)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aDirectoryEntries);
|
|
|
|
*aDirectoryEntries = nsnull;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
PRBool isDir;
|
|
|
|
rv = IsDirectory(&isDir);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (!isDir)
|
|
|
|
return NS_ERROR_FILE_NOT_DIRECTORY;
|
|
|
|
|
|
|
|
nsDirEnumerator* dirEnum = new nsDirEnumerator;
|
|
|
|
if (dirEnum == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(dirEnum);
|
|
|
|
rv = dirEnum->Init(this);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(dirEnum);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
*aDirectoryEntries = dirEnum;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark [nsILocalFile]
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::InitWithPath(const nsAString& filePath)
|
|
|
|
{
|
|
|
|
return InitWithNativePath(NS_ConvertUTF16toUTF8(filePath));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::InitWithNativePath(const nsACString& filePath)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCAutoString fixedPath;
|
|
|
|
if (Substring(filePath, 0, 2).EqualsLiteral("~/")) {
|
|
|
|
nsCOMPtr<nsIFile> homeDir;
|
|
|
|
nsCAutoString homePath;
|
|
|
|
nsresult rv = NS_GetSpecialDirectory(NS_OS_HOME_DIR,
|
|
|
|
getter_AddRefs(homeDir));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = homeDir->GetNativePath(homePath);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
fixedPath = homePath + Substring(filePath, 1, filePath.Length() - 1);
|
|
|
|
}
|
|
|
|
else if (filePath.IsEmpty() || filePath.First() != '/')
|
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
|
|
|
else
|
|
|
|
fixedPath.Assign(filePath);
|
|
|
|
|
|
|
|
// A path with consecutive '/'s which are not between
|
|
|
|
// nodes crashes CFURLGetFSRef(). Consecutive '/'s which
|
|
|
|
// are between actual nodes are OK. So, convert consecutive
|
|
|
|
// '/'s to a single one.
|
|
|
|
fixedPath.ReplaceSubstring("//", "/");
|
|
|
|
|
|
|
|
// On 10.2, huge paths also crash CFURLGetFSRef()
|
|
|
|
if (fixedPath.Length() > PATH_MAX)
|
|
|
|
return NS_ERROR_FILE_NAME_TOO_LONG;
|
|
|
|
|
|
|
|
CFStringRef pathAsCFString;
|
|
|
|
CFURLRef pathAsCFURL;
|
|
|
|
|
|
|
|
pathAsCFString = ::CFStringCreateWithCString(nsnull, fixedPath.get(), kCFStringEncodingUTF8);
|
|
|
|
if (!pathAsCFString)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
pathAsCFURL = ::CFURLCreateWithFileSystemPath(nsnull, pathAsCFString, kCFURLPOSIXPathStyle, PR_FALSE);
|
|
|
|
if (!pathAsCFURL) {
|
|
|
|
::CFRelease(pathAsCFString);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2009-06-10 22:23:57 -07:00
|
|
|
SetBaseURL(pathAsCFURL);
|
2007-03-22 10:30:00 -07:00
|
|
|
::CFRelease(pathAsCFURL);
|
|
|
|
::CFRelease(pathAsCFString);
|
|
|
|
return NS_OK;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::InitWithFile(nsILocalFile *aFile)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG(aFile);
|
|
|
|
|
|
|
|
nsCOMPtr<nsILocalFileMac> aFileMac(do_QueryInterface(aFile));
|
|
|
|
if (!aFileMac)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
CFURLRef urlRef;
|
|
|
|
nsresult rv = aFileMac->GetCFURL(&urlRef);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
rv = InitWithCFURL(urlRef);
|
|
|
|
::CFRelease(urlRef);
|
|
|
|
return rv;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetFollowLinks(PRBool *aFollowLinks)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aFollowLinks);
|
|
|
|
|
|
|
|
*aFollowLinks = mFollowLinks;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::SetFollowLinks(PRBool aFollowLinks)
|
|
|
|
{
|
2009-06-10 22:23:57 -07:00
|
|
|
mFollowLinks = aFollowLinks;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::OpenNSPRFileDesc(PRInt32 flags, PRInt32 mode, PRFileDesc **_retval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
|
|
|
|
nsCAutoString path;
|
|
|
|
nsresult rv = GetPathInternal(path);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
*_retval = PR_Open(path.get(), flags, mode);
|
|
|
|
if (! *_retval)
|
|
|
|
return NS_ErrorAccordingToNSPR();
|
|
|
|
|
2009-03-19 13:56:05 -07:00
|
|
|
if (flags & DELETE_ON_CLOSE) {
|
|
|
|
PR_Delete(path.get());
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::OpenANSIFileDesc(const char *mode, FILE **_retval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
|
|
|
|
nsCAutoString path;
|
|
|
|
nsresult rv = GetPathInternal(path);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
*_retval = fopen(path.get(), mode);
|
|
|
|
if (! *_retval)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::Load(PRLibrary **_retval)
|
|
|
|
{
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
|
|
|
|
NS_TIMELINE_START_TIMER("PR_LoadLibrary");
|
|
|
|
|
|
|
|
nsCAutoString path;
|
|
|
|
nsresult rv = GetPathInternal(path);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
|
|
|
nsTraceRefcntImpl::SetActivityIsLegal(PR_FALSE);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
*_retval = PR_LoadLibrary(path.get());
|
|
|
|
|
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
|
|
|
nsTraceRefcntImpl::SetActivityIsLegal(PR_TRUE);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_TIMELINE_STOP_TIMER("PR_LoadLibrary");
|
|
|
|
NS_TIMELINE_MARK_TIMER1("PR_LoadLibrary", path.get());
|
|
|
|
|
|
|
|
if (!*_retval)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetDiskSpaceAvailable(PRInt64 *aDiskSpaceAvailable)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aDiskSpaceAvailable);
|
2009-04-29 23:53:43 -07:00
|
|
|
|
|
|
|
nsCAutoString path;
|
|
|
|
nsresult rv = GetPathInternal(path);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2009-04-29 23:53:43 -07:00
|
|
|
|
|
|
|
struct STATVFS fs_buf;
|
|
|
|
if (STATVFS(path.get(), &fs_buf) < 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
// minus one block for fuzz
|
|
|
|
*aDiskSpaceAvailable = (PRInt64)fs_buf.f_frsize * (fs_buf.f_bavail - 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::AppendRelativePath(const nsAString& relativeFilePath)
|
|
|
|
{
|
|
|
|
return AppendRelativeNativePath(NS_ConvertUTF16toUTF8(relativeFilePath));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::AppendRelativeNativePath(const nsACString& relativeFilePath)
|
|
|
|
{
|
|
|
|
if (relativeFilePath.IsEmpty())
|
|
|
|
return NS_OK;
|
|
|
|
// No leading '/'
|
|
|
|
if (relativeFilePath.First() == '/')
|
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
|
|
|
|
|
|
|
// Parse the nodes and call Append() for each
|
|
|
|
nsACString::const_iterator nodeBegin, pathEnd;
|
|
|
|
relativeFilePath.BeginReading(nodeBegin);
|
|
|
|
relativeFilePath.EndReading(pathEnd);
|
|
|
|
nsACString::const_iterator nodeEnd(nodeBegin);
|
|
|
|
|
|
|
|
while (nodeEnd != pathEnd) {
|
|
|
|
FindCharInReadable(kPathSepChar, nodeEnd, pathEnd);
|
|
|
|
nsresult rv = AppendNative(Substring(nodeBegin, nodeEnd));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (nodeEnd != pathEnd) // If there's more left in the string, inc over the '/' nodeEnd is on.
|
|
|
|
++nodeEnd;
|
|
|
|
nodeBegin = nodeEnd;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetPersistentDescriptor(nsACString& aPersistentDescriptor)
|
|
|
|
{
|
2009-11-10 11:46:16 -08:00
|
|
|
return GetNativePath(aPersistentDescriptor);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::SetPersistentDescriptor(const nsACString& aPersistentDescriptor)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aPersistentDescriptor.IsEmpty())
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
// Support pathnames as user-supplied descriptors if they begin with '/'
|
|
|
|
// or '~'. These characters do not collide with the base64 set used for
|
|
|
|
// encoding alias records.
|
|
|
|
char first = aPersistentDescriptor.First();
|
|
|
|
if (first == '/' || first == '~')
|
|
|
|
return InitWithNativePath(aPersistentDescriptor);
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
PRUint32 dataSize = aPersistentDescriptor.Length();
|
|
|
|
char* decodedData = PL_Base64Decode(PromiseFlatCString(aPersistentDescriptor).get(), dataSize, nsnull);
|
|
|
|
if (!decodedData) {
|
|
|
|
NS_ERROR("SetPersistentDescriptor was given bad data");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cast to an alias record and resolve.
|
|
|
|
AliasRecord aliasHeader = *(AliasPtr)decodedData;
|
2009-01-15 14:57:43 -08:00
|
|
|
PRInt32 aliasSize = ::GetAliasSizeFromPtr(&aliasHeader);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aliasSize > ((PRInt32)dataSize * 3) / 4) { // be paranoid about having too few data
|
|
|
|
PR_Free(decodedData);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the now-decoded data into the Handle.
|
|
|
|
// The size of the decoded data is 3/4 the size of the encoded data. See plbase64.h
|
|
|
|
Handle newHandle = nsnull;
|
|
|
|
if (::PtrToHand(decodedData, &newHandle, aliasSize) != noErr)
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
PR_Free(decodedData);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
Boolean changed;
|
|
|
|
FSRef resolvedFSRef;
|
|
|
|
OSErr err = ::FSResolveAlias(nsnull, (AliasHandle)newHandle, &resolvedFSRef, &changed);
|
|
|
|
|
|
|
|
rv = MacErrorMapper(err);
|
|
|
|
DisposeHandle(newHandle);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
return InitWithFSRef(&resolvedFSRef);
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::Reveal()
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-01-15 14:57:43 -08:00
|
|
|
BOOL success = NO;
|
|
|
|
|
|
|
|
CFURLRef urlRef;
|
|
|
|
if (NS_SUCCEEDED(GetCFURL(&urlRef)) && urlRef) {
|
|
|
|
NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
|
|
|
|
success = [[NSWorkspace sharedWorkspace] selectFile:[(NSURL*)urlRef path] inFileViewerRootedAtPath:@""];
|
|
|
|
[ap release];
|
|
|
|
|
|
|
|
::CFRelease(urlRef);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-01-15 14:57:43 -08:00
|
|
|
|
|
|
|
return (success ? NS_OK : NS_ERROR_FAILURE);
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::Launch()
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-01-15 14:57:43 -08:00
|
|
|
BOOL success = NO;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-01-15 14:57:43 -08:00
|
|
|
CFURLRef urlRef;
|
|
|
|
if (NS_SUCCEEDED(GetCFURL(&urlRef)) && urlRef) {
|
|
|
|
NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
|
|
|
|
success = [[NSWorkspace sharedWorkspace] openURL:(NSURL*)urlRef];
|
|
|
|
[ap release];
|
|
|
|
|
|
|
|
::CFRelease(urlRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (success ? NS_OK : NS_ERROR_FAILURE);
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark [nsILocalFileMac]
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::InitWithCFURL(CFURLRef aCFURL)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aCFURL);
|
|
|
|
|
2009-06-10 22:23:57 -07:00
|
|
|
SetBaseURL(aCFURL);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::InitWithFSRef(const FSRef *aFSRef)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG(aFSRef);
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
CFURLRef newURLRef = ::CFURLCreateFromFSRef(kCFAllocatorDefault, aFSRef);
|
|
|
|
if (newURLRef) {
|
2009-06-10 22:23:57 -07:00
|
|
|
SetBaseURL(newURLRef);
|
2007-03-22 10:30:00 -07:00
|
|
|
::CFRelease(newURLRef);
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
return rv;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::InitToAppWithCreatorCode(OSType aAppCreator)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
FSRef fsRef;
|
|
|
|
OSErr err = ::LSFindApplicationForInfo(aAppCreator, nsnull, nsnull, &fsRef, nsnull);
|
|
|
|
if (err != noErr)
|
|
|
|
return MacErrorMapper(err);
|
|
|
|
return InitWithFSRef(&fsRef);
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetCFURL(CFURLRef *_retval)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2009-06-10 22:23:57 -07:00
|
|
|
|
|
|
|
if (mFollowLinks) {
|
|
|
|
nsCAutoString nativePath;
|
|
|
|
nsresult rv = GetNativeTarget(nativePath);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// This is a symlink if we can get a target path.
|
|
|
|
// We can assume this is not a directory if we know it is a symlink.
|
|
|
|
// I'll say this anyway because it is so dangerous: Do not call IsDirectory
|
|
|
|
// here because that ultimately ends up calling GetCFURL and you get a loop.
|
|
|
|
CFURLRef targetURL = ::CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault,
|
|
|
|
(UInt8*)nativePath.BeginWriting(),
|
|
|
|
nativePath.Length(),
|
|
|
|
false);
|
|
|
|
if (!targetURL)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
*_retval = targetURL;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
::CFRetain(mBaseURL);
|
|
|
|
*_retval = mBaseURL;
|
|
|
|
|
|
|
|
return NS_OK;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetFSRef(FSRef *_retval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
return GetFSRefInternal(*_retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetFSSpec(FSSpec *_retval)
|
2009-02-11 20:26:59 -08:00
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
FSRef fsRef;
|
|
|
|
nsresult rv = GetFSRefInternal(fsRef);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2009-02-11 20:26:59 -08:00
|
|
|
OSErr err = ::FSGetCatalogInfo(&fsRef, kFSCatInfoNone, nsnull, nsnull, _retval, nsnull);
|
2007-03-22 10:30:00 -07:00
|
|
|
return MacErrorMapper(err);
|
|
|
|
}
|
|
|
|
return rv;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetFileSizeWithResFork(PRInt64 *aFileSizeWithResFork)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aFileSizeWithResFork);
|
|
|
|
|
|
|
|
FSRef fsRef;
|
|
|
|
nsresult rv = GetFSRefInternal(fsRef);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
FSCatalogInfo catalogInfo;
|
|
|
|
OSErr err = ::FSGetCatalogInfo(&fsRef, kFSCatInfoDataSizes + kFSCatInfoRsrcSizes,
|
|
|
|
&catalogInfo, nsnull, nsnull, nsnull);
|
|
|
|
if (err != noErr)
|
|
|
|
return MacErrorMapper(err);
|
|
|
|
|
|
|
|
*aFileSizeWithResFork = catalogInfo.dataLogicalSize + catalogInfo.rsrcLogicalSize;
|
|
|
|
return NS_OK;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetFileType(OSType *aFileType)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aFileType);
|
2009-02-11 20:26:59 -08:00
|
|
|
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-02-11 20:26:59 -08:00
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
|
2009-04-09 17:46:49 -07:00
|
|
|
NSDictionary* dict = [[NSFileManager defaultManager] fileAttributesAtPath:[(NSURL*)mBaseURL path] traverseLink:YES];
|
2009-02-11 20:26:59 -08:00
|
|
|
NSNumber* typeNum = (NSNumber*)[dict objectForKey:NSFileHFSTypeCode];
|
|
|
|
if (typeNum) {
|
|
|
|
*aFileType = [typeNum unsignedLongValue];
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
[ap release];
|
|
|
|
|
|
|
|
return rv;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::SetFileType(OSType aFileType)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2009-02-11 20:26:59 -08:00
|
|
|
|
|
|
|
NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
|
|
|
|
NSDictionary* dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:aFileType] forKey:NSFileHFSTypeCode];
|
2009-04-09 17:46:49 -07:00
|
|
|
BOOL success = [[NSFileManager defaultManager] changeFileAttributes:dict atPath:[(NSURL*)mBaseURL path]];
|
2009-02-11 20:26:59 -08:00
|
|
|
[ap release];
|
|
|
|
return (success ? NS_OK : NS_ERROR_FAILURE);
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::GetFileCreator(OSType *aFileCreator)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aFileCreator);
|
2009-02-11 20:26:59 -08:00
|
|
|
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2009-02-11 20:26:59 -08:00
|
|
|
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
|
2009-04-09 17:46:49 -07:00
|
|
|
NSDictionary* dict = [[NSFileManager defaultManager] fileAttributesAtPath:[(NSURL*)mBaseURL path] traverseLink:YES];
|
2009-02-11 20:26:59 -08:00
|
|
|
id creatorNum = (NSNumber*)[dict objectForKey:NSFileHFSCreatorCode];
|
|
|
|
if (creatorNum) {
|
|
|
|
*aFileCreator = [creatorNum unsignedLongValue];
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
[ap release];
|
|
|
|
|
|
|
|
return rv;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::SetFileCreator(OSType aFileCreator)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2009-02-11 20:26:59 -08:00
|
|
|
|
|
|
|
NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
|
|
|
|
NSDictionary* dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:aFileCreator] forKey:NSFileHFSCreatorCode];
|
2009-04-09 17:46:49 -07:00
|
|
|
BOOL success = [[NSFileManager defaultManager] changeFileAttributes:dict atPath:[(NSURL*)mBaseURL path]];
|
2009-02-11 20:26:59 -08:00
|
|
|
[ap release];
|
|
|
|
return (success ? NS_OK : NS_ERROR_FAILURE);
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::LaunchWithDoc(nsILocalFile *aDocToLoad, PRBool aLaunchInBackground)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
PRBool isExecutable;
|
|
|
|
nsresult rv = IsExecutable(&isExecutable);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (!isExecutable)
|
|
|
|
return NS_ERROR_FILE_EXECUTION_FAILED;
|
|
|
|
|
|
|
|
FSRef appFSRef, docFSRef;
|
|
|
|
rv = GetFSRefInternal(appFSRef);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (aDocToLoad) {
|
|
|
|
nsCOMPtr<nsILocalFileMac> macDoc = do_QueryInterface(aDocToLoad);
|
|
|
|
rv = macDoc->GetFSRef(&docFSRef);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
LSLaunchFlags theLaunchFlags = kLSLaunchDefaults;
|
|
|
|
LSLaunchFSRefSpec thelaunchSpec;
|
|
|
|
|
|
|
|
if (aLaunchInBackground)
|
|
|
|
theLaunchFlags |= kLSLaunchDontSwitch;
|
|
|
|
memset(&thelaunchSpec, 0, sizeof(LSLaunchFSRefSpec));
|
|
|
|
|
|
|
|
thelaunchSpec.appRef = &appFSRef;
|
|
|
|
if (aDocToLoad) {
|
|
|
|
thelaunchSpec.numDocs = 1;
|
|
|
|
thelaunchSpec.itemRefs = &docFSRef;
|
|
|
|
}
|
|
|
|
thelaunchSpec.launchFlags = theLaunchFlags;
|
|
|
|
|
|
|
|
OSErr err = ::LSOpenFromRefSpec(&thelaunchSpec, NULL);
|
|
|
|
if (err != noErr)
|
|
|
|
return MacErrorMapper(err);
|
|
|
|
|
|
|
|
return NS_OK;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::OpenDocWithApp(nsILocalFile *aAppToOpenWith, PRBool aLaunchInBackground)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv;
|
|
|
|
OSErr err;
|
|
|
|
|
|
|
|
FSRef docFSRef, appFSRef;
|
|
|
|
rv = GetFSRefInternal(docFSRef);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (aAppToOpenWith) {
|
|
|
|
nsCOMPtr<nsILocalFileMac> appFileMac = do_QueryInterface(aAppToOpenWith, &rv);
|
|
|
|
if (!appFileMac)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
PRBool isExecutable;
|
|
|
|
rv = appFileMac->IsExecutable(&isExecutable);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (!isExecutable)
|
|
|
|
return NS_ERROR_FILE_EXECUTION_FAILED;
|
|
|
|
|
|
|
|
rv = appFileMac->GetFSRef(&appFSRef);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
OSType fileCreator;
|
|
|
|
rv = GetFileCreator(&fileCreator);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
err = ::LSFindApplicationForInfo(fileCreator, nsnull, nsnull, &appFSRef, nsnull);
|
|
|
|
if (err != noErr)
|
|
|
|
return MacErrorMapper(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
LSLaunchFlags theLaunchFlags = kLSLaunchDefaults;
|
|
|
|
LSLaunchFSRefSpec thelaunchSpec;
|
|
|
|
|
|
|
|
if (aLaunchInBackground)
|
|
|
|
theLaunchFlags |= kLSLaunchDontSwitch;
|
|
|
|
memset(&thelaunchSpec, 0, sizeof(LSLaunchFSRefSpec));
|
|
|
|
|
|
|
|
thelaunchSpec.appRef = &appFSRef;
|
|
|
|
thelaunchSpec.numDocs = 1;
|
|
|
|
thelaunchSpec.itemRefs = &docFSRef;
|
|
|
|
thelaunchSpec.launchFlags = theLaunchFlags;
|
|
|
|
|
|
|
|
err = ::LSOpenFromRefSpec(&thelaunchSpec, NULL);
|
|
|
|
if (err != noErr)
|
|
|
|
return MacErrorMapper(err);
|
|
|
|
|
|
|
|
return NS_OK;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::IsPackage(PRBool *_retval)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG(_retval);
|
|
|
|
*_retval = PR_FALSE;
|
|
|
|
|
2008-08-26 02:17:13 -07:00
|
|
|
CFURLRef urlRef;
|
|
|
|
if (NS_SUCCEEDED(GetCFURL(&urlRef)) && urlRef) {
|
|
|
|
*_retval = [[NSWorkspace sharedWorkspace] isFilePackageAtPath:[(NSURL *)urlRef path]];
|
|
|
|
::CFRelease(urlRef);
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-08-26 02:17:13 -07:00
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::GetBundleDisplayName(nsAString& outBundleName)
|
|
|
|
{
|
|
|
|
PRBool isPackage = PR_FALSE;
|
|
|
|
nsresult rv = IsPackage(&isPackage);
|
|
|
|
if (NS_FAILED(rv) || !isPackage)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsAutoString name;
|
|
|
|
rv = GetLeafName(name);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
PRInt32 length = name.Length();
|
|
|
|
if (Substring(name, length - 4, length).EqualsLiteral(".app")) {
|
|
|
|
// 4 characters in ".app"
|
|
|
|
outBundleName = Substring(name, 0, length - 4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
outBundleName = name;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::GetBundleIdentifier(nsACString& outBundleIdentifier)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
CFURLRef urlRef;
|
|
|
|
if (NS_SUCCEEDED(GetCFURL(&urlRef))) {
|
|
|
|
CFBundleRef bundle = ::CFBundleCreate(NULL, urlRef);
|
|
|
|
if (bundle) {
|
|
|
|
CFStringRef bundleIdentifier = ::CFBundleGetIdentifier(bundle);
|
|
|
|
if (bundleIdentifier)
|
|
|
|
rv = CFStringReftoUTF8(bundleIdentifier, outBundleIdentifier);
|
|
|
|
|
|
|
|
::CFRelease(bundle);
|
|
|
|
}
|
|
|
|
::CFRelease(urlRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark [Protected Methods]
|
|
|
|
|
2009-06-10 22:23:57 -07:00
|
|
|
nsresult nsLocalFile::SetBaseURL(CFURLRef aCFURLRef)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG(aCFURLRef);
|
2009-06-10 22:23:57 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
::CFRetain(aCFURLRef);
|
2009-04-09 17:46:49 -07:00
|
|
|
if (mBaseURL)
|
|
|
|
::CFRelease(mBaseURL);
|
|
|
|
mBaseURL = aCFURLRef;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-11-13 04:22:28 -08:00
|
|
|
if (!::CFURLGetFileSystemRepresentation(mBaseURL, NO, (UInt8*)mPath, PATH_MAX))
|
2009-07-29 21:47:06 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-06-10 22:23:57 -07:00
|
|
|
nsresult nsLocalFile::GetFSRefInternal(FSRef& aFSRef)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2009-06-10 22:23:57 -07:00
|
|
|
|
|
|
|
return (::CFURLGetFSRef(mBaseURL, &aFSRef) ? NS_OK : NS_ERROR_FAILURE);
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-06-10 22:23:57 -07:00
|
|
|
nsresult nsLocalFile::GetPathInternal(nsACString& path)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2008-09-19 12:50:41 -07:00
|
|
|
|
2009-06-10 22:23:57 -07:00
|
|
|
CFURLRef urlRef;
|
|
|
|
nsresult rv = GetCFURL(&urlRef);
|
2009-03-07 18:59:40 -08:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2009-04-23 19:22:20 -07:00
|
|
|
path.SetLength(PATH_MAX);
|
2009-06-10 22:23:57 -07:00
|
|
|
if (path.Length() != (unsigned int)PATH_MAX) {
|
|
|
|
::CFRelease(urlRef);
|
2009-04-23 19:22:20 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2009-06-10 22:23:57 -07:00
|
|
|
}
|
|
|
|
Boolean gotPath = ::CFURLGetFileSystemRepresentation(urlRef, true, (UInt8*)path.BeginWriting(), PATH_MAX);
|
|
|
|
::CFRelease(urlRef);
|
|
|
|
|
|
|
|
return (gotPath ? NS_OK : NS_ERROR_FAILURE);
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsLocalFile::CopyInternal(nsIFile* aParentDir,
|
|
|
|
const nsAString& newName,
|
|
|
|
PRBool followLinks)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
StFollowLinksState srcFollowState(*this, followLinks);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
OSErr err;
|
2009-02-11 20:26:59 -08:00
|
|
|
FSRef srcFSRef;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
rv = GetFSRefInternal(srcFSRef);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> newParentDir = aParentDir;
|
|
|
|
|
|
|
|
if (!newParentDir) {
|
|
|
|
if (newName.IsEmpty())
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
rv = GetParent(getter_AddRefs(newParentDir));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If newParentDir does not exist, create it
|
|
|
|
PRBool exists;
|
|
|
|
rv = newParentDir->Exists(&exists);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (!exists) {
|
|
|
|
rv = newParentDir->Create(nsIFile::DIRECTORY_TYPE, 0777);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
FSRef destFSRef;
|
|
|
|
nsCOMPtr<nsILocalFileMac> newParentDirMac(do_QueryInterface(newParentDir));
|
|
|
|
if (!newParentDirMac)
|
|
|
|
return NS_ERROR_NO_INTERFACE;
|
|
|
|
rv = newParentDirMac->GetFSRef(&destFSRef);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2009-02-15 16:20:27 -08:00
|
|
|
CFStringRef destNameStr = NULL;
|
|
|
|
if (newName.Length() > 0) {
|
|
|
|
destNameStr = ::CFStringCreateWithCharacters(kCFAllocatorDefault,
|
|
|
|
PromiseFlatString(newName).get(),
|
|
|
|
newName.Length());
|
|
|
|
}
|
2009-02-11 20:26:59 -08:00
|
|
|
err = ::FSCopyObjectSync(&srcFSRef, &destFSRef, destNameStr, NULL, kFSFileOperationDefaultOptions);
|
2009-02-15 16:20:27 -08:00
|
|
|
if (destNameStr)
|
|
|
|
::CFRelease(destNameStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return MacErrorMapper(err);
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-06-10 22:23:57 -07:00
|
|
|
nsresult nsLocalFile::FillStatBufferInternal(struct STAT *statBuffer)
|
|
|
|
{
|
2009-07-12 13:04:25 -07:00
|
|
|
CHECK_INIT();
|
2009-06-10 22:23:57 -07:00
|
|
|
|
2009-07-29 21:47:06 -07:00
|
|
|
if (STAT(mPath, statBuffer) == -1) {
|
|
|
|
if (LSTAT(mPath, statBuffer) == -1)
|
2009-06-10 22:23:57 -07:00
|
|
|
return NSRESULT_FOR_ERRNO();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// nsIHashable
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::Equals(nsIHashable* aOther, PRBool *aResult)
|
|
|
|
{
|
2009-07-08 08:26:07 -07:00
|
|
|
nsCOMPtr<nsIFile> otherFile(do_QueryInterface(aOther));
|
|
|
|
if (!otherFile) {
|
|
|
|
*aResult = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Equals(otherFile, aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::GetHashCode(PRUint32 *aResult)
|
|
|
|
{
|
2008-02-29 15:24:53 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-04-09 17:46:49 -07:00
|
|
|
CFStringRef pathStrRef = ::CFURLCopyFileSystemPath(mBaseURL, kCFURLPOSIXPathStyle);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCAutoString path;
|
|
|
|
CFStringReftoUTF8(pathStrRef, path);
|
2007-07-24 13:19:03 -07:00
|
|
|
::CFRelease(pathStrRef);
|
2007-03-22 10:30:00 -07:00
|
|
|
*aResult = HashString(path);
|
|
|
|
return NS_OK;
|
2008-02-29 15:24:53 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark [Global Functions]
|
|
|
|
|
|
|
|
void nsLocalFile::GlobalInit()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsLocalFile::GlobalShutdown()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult NS_NewLocalFile(const nsAString& path, PRBool followLinks, nsILocalFile* *result)
|
|
|
|
{
|
|
|
|
nsLocalFile* file = new nsLocalFile;
|
|
|
|
if (file == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(file);
|
|
|
|
|
|
|
|
file->SetFollowLinks(followLinks);
|
|
|
|
|
|
|
|
if (!path.IsEmpty()) {
|
|
|
|
nsresult rv = file->InitWithPath(path);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(file);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*result = file;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult NS_NewNativeLocalFile(const nsACString& path, PRBool followLinks, nsILocalFile **result)
|
|
|
|
{
|
|
|
|
return NS_NewLocalFile(NS_ConvertUTF8toUTF16(path), followLinks, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult NS_NewLocalFileWithFSRef(const FSRef* aFSRef, PRBool aFollowLinks, nsILocalFileMac** result)
|
|
|
|
{
|
|
|
|
nsLocalFile* file = new nsLocalFile();
|
|
|
|
if (file == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(file);
|
|
|
|
|
|
|
|
file->SetFollowLinks(aFollowLinks);
|
|
|
|
|
|
|
|
nsresult rv = file->InitWithFSRef(aFSRef);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(file);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
*result = file;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-04-09 17:54:11 -07:00
|
|
|
nsresult NS_NewLocalFileWithCFURL(const CFURLRef aURL, PRBool aFollowLinks, nsILocalFileMac** result)
|
|
|
|
{
|
|
|
|
nsLocalFile* file = new nsLocalFile();
|
|
|
|
if (file == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(file);
|
|
|
|
|
|
|
|
file->SetFollowLinks(aFollowLinks);
|
|
|
|
|
|
|
|
nsresult rv = file->InitWithCFURL(aURL);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(file);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
*result = file;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-11-28 10:37:40 -08:00
|
|
|
#pragma mark -
|
|
|
|
#pragma mark [Static Functions]
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static nsresult MacErrorMapper(OSErr inErr)
|
|
|
|
{
|
|
|
|
nsresult outErr;
|
|
|
|
|
|
|
|
switch (inErr)
|
|
|
|
{
|
|
|
|
case noErr:
|
|
|
|
outErr = NS_OK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case fnfErr:
|
2007-10-14 20:05:20 -07:00
|
|
|
case afpObjectNotFound:
|
|
|
|
case afpDirNotFound:
|
2007-03-22 10:30:00 -07:00
|
|
|
outErr = NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case dupFNErr:
|
2007-10-14 20:05:20 -07:00
|
|
|
case afpObjectExists:
|
2007-03-22 10:30:00 -07:00
|
|
|
outErr = NS_ERROR_FILE_ALREADY_EXISTS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case dskFulErr:
|
2007-10-14 20:05:20 -07:00
|
|
|
case afpDiskFull:
|
2007-03-22 10:30:00 -07:00
|
|
|
outErr = NS_ERROR_FILE_DISK_FULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case fLckdErr:
|
2007-10-14 20:05:20 -07:00
|
|
|
case afpVolLocked:
|
2007-03-22 10:30:00 -07:00
|
|
|
outErr = NS_ERROR_FILE_IS_LOCKED;
|
|
|
|
break;
|
|
|
|
|
2007-10-14 20:05:20 -07:00
|
|
|
case afpAccessDenied:
|
|
|
|
outErr = NS_ERROR_FILE_ACCESS_DENIED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case afpDirNotEmpty:
|
|
|
|
outErr = NS_ERROR_FILE_DIR_NOT_EMPTY;
|
|
|
|
break;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Can't find good map for some
|
|
|
|
case bdNamErr:
|
|
|
|
outErr = NS_ERROR_FAILURE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
outErr = NS_ERROR_FAILURE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return outErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert a UTF-8 string to a UTF-16 string while normalizing to
|
|
|
|
// Normalization Form C (composed Unicode). We need this because
|
|
|
|
// Mac OS X file system uses NFD (Normalization Form D : decomposed Unicode)
|
|
|
|
// while most other OS', server-side programs usually expect NFC.
|
|
|
|
static void CopyUTF8toUTF16NFC(const nsACString& aSrc, nsAString& aResult)
|
|
|
|
{
|
2009-05-01 09:59:33 -07:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-01 09:59:33 -07:00
|
|
|
const nsAFlatCString &inFlatSrc = PromiseFlatCString(aSrc);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-01 09:59:33 -07:00
|
|
|
// The number of 16bit code units in a UTF-16 string will never be
|
|
|
|
// larger than the number of bytes in the corresponding UTF-8 string.
|
|
|
|
CFMutableStringRef inStr = ::CFStringCreateMutable(NULL, inFlatSrc.Length());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-01 09:59:33 -07:00
|
|
|
if (!inStr) {
|
|
|
|
CopyUTF8toUTF16(aSrc, aResult);
|
|
|
|
return;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-01 09:59:33 -07:00
|
|
|
::CFStringAppendCString(inStr, inFlatSrc.get(), kCFStringEncodingUTF8);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-01 09:59:33 -07:00
|
|
|
::CFStringNormalize(inStr, kCFStringNormalizationFormC);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-01 09:59:33 -07:00
|
|
|
CFIndex length = ::CFStringGetLength(inStr);
|
|
|
|
const UniChar* chars = ::CFStringGetCharactersPtr(inStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-01 09:59:33 -07:00
|
|
|
if (chars) {
|
|
|
|
aResult.Assign(chars, length);
|
|
|
|
}
|
|
|
|
else {
|
2009-07-29 21:47:06 -07:00
|
|
|
nsAutoTArray<UniChar, PATH_MAX> buffer;
|
2009-05-01 09:59:33 -07:00
|
|
|
if (!buffer.SetLength(length)) {
|
|
|
|
CopyUTF8toUTF16(aSrc, aResult);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
else {
|
2009-05-01 09:59:33 -07:00
|
|
|
::CFStringGetCharacters(inStr, ::CFRangeMake(0, length), buffer.Elements());
|
|
|
|
aResult.Assign(buffer.Elements(), length);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-05-01 09:59:33 -07:00
|
|
|
}
|
|
|
|
::CFRelease(inStr);
|
2008-02-29 15:24:53 -08:00
|
|
|
|
2009-05-01 09:59:33 -07:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-07-29 21:47:06 -07:00
|
|
|
|
|
|
|
nsresult CFStringReftoUTF8(CFStringRef aInStrRef, nsACString& aOutStr)
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
|
|
|
// first see if the conversion would succeed and find the length of the result
|
|
|
|
CFIndex usedBufLen, inStrLen = ::CFStringGetLength(aInStrRef);
|
|
|
|
CFIndex charsConverted = ::CFStringGetBytes(aInStrRef, CFRangeMake(0, inStrLen),
|
|
|
|
kCFStringEncodingUTF8, 0, PR_FALSE,
|
|
|
|
NULL, 0, &usedBufLen);
|
|
|
|
if (charsConverted == inStrLen) {
|
|
|
|
// all characters converted, do the actual conversion
|
|
|
|
aOutStr.SetLength(usedBufLen);
|
|
|
|
if (aOutStr.Length() != (unsigned int)usedBufLen)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
UInt8 *buffer = (UInt8*)aOutStr.BeginWriting();
|
|
|
|
::CFStringGetBytes(aInStrRef, CFRangeMake(0, inStrLen), kCFStringEncodingUTF8,
|
|
|
|
0, false, buffer, usedBufLen, &usedBufLen);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
}
|