Bug 571193. Move Mac OS X to UNIX filesystem code shared with Linux. Also fixes bug 506812, bug 528447, bug 530188. r=benwa sr=bsmedberg CLOSED TREE

This commit is contained in:
Josh Aas 2010-07-16 18:35:59 -04:00
parent fe8a477f84
commit 6a6d3c8ee5
10 changed files with 704 additions and 2447 deletions

View File

@ -1507,22 +1507,17 @@ XRE_GetBinaryPath(const char* argv0, nsILocalFile* *aResult)
if (!appBundle)
return NS_ERROR_FAILURE;
CFURLRef bundleURL = CFBundleCopyExecutableURL(appBundle);
if (!bundleURL)
CFURLRef executableURL = CFBundleCopyExecutableURL(appBundle);
if (!executableURL)
return NS_ERROR_FAILURE;
FSRef fileRef;
if (!CFURLGetFSRef(bundleURL, &fileRef)) {
CFRelease(bundleURL);
return NS_ERROR_FAILURE;
}
rv = lfm->InitWithFSRef(&fileRef);
CFRelease(bundleURL);
rv = lfm->InitWithCFURL(executableURL);
CFRelease(executableURL);
if (NS_FAILED(rv))
return rv;
// Callers expect a normalized path.
lfm->Normalize();
#elif defined(XP_UNIX)
struct stat fileStat;
char exePath[MAXPATHLEN];

60
xpcom/io/CocoaFileUtils.h Normal file
View File

@ -0,0 +1,60 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
// vim:set ts=2 sts=2 sw=2 et cin:
/* ***** 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
* Mozilla Corporation.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Josh Aas <josh@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of 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 ***** */
// This namespace contains methods with Obj-C/Cocoa implementations. The header
// is C/C++ for inclusion in C/C++-only files.
#ifndef CocoaFileUtils_h_
#define CocoaFileUtils_h_
#include "nscore.h"
#include <CoreFoundation/CoreFoundation.h>
namespace CocoaFileUtils {
nsresult RevealFileInFinder(CFURLRef url);
nsresult OpenURL(CFURLRef url);
nsresult GetFileCreatorCode(CFURLRef url, OSType *creatorCode);
nsresult SetFileCreatorCode(CFURLRef url, OSType creatorCode);
nsresult GetFileTypeCode(CFURLRef url, OSType *typeCode);
nsresult SetFileTypeCode(CFURLRef url, OSType typeCode);
} // namespace CocoaFileUtils
#endif

153
xpcom/io/CocoaFileUtils.mm Normal file
View File

@ -0,0 +1,153 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
// vim:set ts=2 sts=2 sw=2 et cin:
/* ***** 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
* Mozilla Corporation.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Josh Aas <josh@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of 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 "CocoaFileUtils.h"
#include <Cocoa/Cocoa.h>
#include "nsObjCExceptions.h"
#include "nsDebug.h"
namespace CocoaFileUtils {
nsresult RevealFileInFinder(CFURLRef url)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
NS_ENSURE_ARG_POINTER(url);
NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
BOOL success = [[NSWorkspace sharedWorkspace] selectFile:[(NSURL*)url path] inFileViewerRootedAtPath:@""];
[ap release];
return (success ? NS_OK : NS_ERROR_FAILURE);
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
nsresult OpenURL(CFURLRef url)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
NS_ENSURE_ARG_POINTER(url);
NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
BOOL success = [[NSWorkspace sharedWorkspace] openURL:(NSURL*)url];
[ap release];
return (success ? NS_OK : NS_ERROR_FAILURE);
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
nsresult GetFileCreatorCode(CFURLRef url, OSType *creatorCode)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
NS_ENSURE_ARG_POINTER(url);
NS_ENSURE_ARG_POINTER(creatorCode);
nsresult rv = NS_ERROR_FAILURE;
NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
NSDictionary* dict = [[NSFileManager defaultManager] fileAttributesAtPath:[(NSURL*)url path] traverseLink:YES];
NSNumber* creatorNum = (NSNumber*)[dict objectForKey:NSFileHFSCreatorCode];
if (creatorNum) {
*creatorCode = [creatorNum unsignedLongValue];
rv = NS_OK;
}
[ap release];
return rv;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
nsresult SetFileCreatorCode(CFURLRef url, OSType creatorCode)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
NS_ENSURE_ARG_POINTER(url);
NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
NSDictionary* dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:creatorCode] forKey:NSFileHFSCreatorCode];
BOOL success = [[NSFileManager defaultManager] setAttributes:dict ofItemAtPath:[(NSURL*)url path] error:nil];
[ap release];
return (success ? NS_OK : NS_ERROR_FAILURE);
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
nsresult GetFileTypeCode(CFURLRef url, OSType *typeCode)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
NS_ENSURE_ARG_POINTER(url);
NS_ENSURE_ARG_POINTER(typeCode);
nsresult rv = NS_ERROR_FAILURE;
NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
NSDictionary* dict = [[NSFileManager defaultManager] fileAttributesAtPath:[(NSURL*)url path] traverseLink:YES];
NSNumber* typeNum = (NSNumber*)[dict objectForKey:NSFileHFSTypeCode];
if (typeNum) {
*typeCode = [typeNum unsignedLongValue];
rv = NS_OK;
}
[ap release];
return rv;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
nsresult SetFileTypeCode(CFURLRef url, OSType typeCode)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
NS_ENSURE_ARG_POINTER(url);
NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
NSDictionary* dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:typeCode] forKey:NSFileHFSTypeCode];
BOOL success = [[NSFileManager defaultManager] setAttributes:dict ofItemAtPath:[(NSURL*)url path] error:nil];
[ap release];
return (success ? NS_OK : NS_ERROR_FAILURE);
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
} // namespace CocoaFileUtils

View File

@ -77,6 +77,12 @@ CPPSRCS = \
SpecialSystemDirectory.cpp \
$(NULL)
ifeq (cocoa,$(MOZ_WIDGET_TOOLKIT))
CMMSRCS += \
CocoaFileUtils.mm \
$(NULL)
endif
ifndef MOZ_NO_FAST_LOAD
CPPSRCS += \
nsFastLoadFile.cpp \
@ -87,15 +93,11 @@ endif
ifeq ($(MOZ_WIDGET_TOOLKIT),os2)
CPPSRCS += nsLocalFileOS2.cpp
else
ifeq ($(MOZ_WIDGET_TOOLKIT),cocoa)
CMMSRCS = nsLocalFileOSX.mm
else
ifeq ($(MOZ_WIDGET_TOOLKIT),windows)
CPPSRCS += nsLocalFileWin.cpp
else
CPPSRCS += nsLocalFileUnix.cpp
endif # windows
endif # mac
endif # OS2
EXPORTS = \
@ -118,15 +120,11 @@ EXPORTS = \
ifeq ($(MOZ_WIDGET_TOOLKIT),os2)
EXPORTS += nsLocalFileOS2.h
else
ifeq ($(MOZ_WIDGET_TOOLKIT),cocoa)
EXPORTS += nsLocalFileOSX.h
else
ifeq ($(MOZ_WIDGET_TOOLKIT),windows)
EXPORTS += nsLocalFileWin.h
else
EXPORTS += nsLocalFileUnix.h
endif # windows
endif # cocoa
endif # os2
XPIDLSRCS = \

View File

@ -50,7 +50,7 @@
[ptr] native FSRefPtr(FSRef);
native CFURLRef(CFURLRef);
[scriptable, uuid(86D685E5-EE3F-405A-B521-446529DB82E5)]
[scriptable, uuid(DE4C75BE-D42B-4F8C-95D9-284C83CF29A4)]
interface nsILocalFileMac : nsILocalFile
{
/**
@ -78,18 +78,6 @@ interface nsILocalFileMac : nsILocalFile
*/
[noscript] void initWithFSRef([const] in FSRefPtr aFSRef);
/**
* initToAppWithCreatorCode
*
* Init this object to point to an application having the given
* creator code. If this app is missing, this will fail. It will first
* look for running application with the given creator.
*
* @param aAppCreator the signature of the app
*
*/
[noscript] void initToAppWithCreatorCode(in OSType aAppCreator);
/**
* getCFURL
*
@ -149,12 +137,7 @@ interface nsILocalFileMac : nsILocalFile
*
*/
readonly attribute PRInt64 fileSizeWithResFork;
/**
* Use with SetFileType() to specify the signature of current process
*/
const unsigned long CURRENT_PROCESS_CREATOR = 0x8000000;
/**
* fileType, creator
*

View File

@ -72,8 +72,6 @@
#ifdef XP_WIN
#include "nsLocalFileWin.h"
#elif defined(XP_MACOSX)
#include "nsLocalFileOSX.h"
#elif defined(XP_UNIX) || defined(XP_BEOS)
#include "nsLocalFileUnix.h"
#elif defined(XP_OS2)

View File

@ -1,112 +0,0 @@
/* -*- 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>
*
* 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 ***** */
#ifndef nsLocalFileMac_h_
#define nsLocalFileMac_h_
#include "nsILocalFileMac.h"
#include "nsString.h"
#include "nsIHashable.h"
class nsDirEnumerator;
// Mac OS X 10.4 does not have stat64/lstat64
#if defined(HAVE_STAT64) && defined(HAVE_LSTAT64) && (MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4)
#define STAT stat64
#define LSTAT lstat64
#else
#define STAT stat
#define LSTAT lstat
#endif
// Mac OS X 10.4 does not have statvfs64
#if defined(HAVE_STATVFS64) && (MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4)
#define STATVFS statvfs64
#else
#define STATVFS statvfs
#endif
// The native charset of this implementation is UTF-8. The Unicode used by the
// Mac OS file system is decomposed, so "Native" versions of these routines will
// always use decomposed Unicode (NFD). Their "non-Native" counterparts are
// intended to be simple wrappers which call the "Native" version and convert
// between UTF-8 and UTF-16. All the work is done on the "Native" side except
// for the conversion to NFC (composed Unicode) done in "non-Native" getters.
class NS_COM nsLocalFile : public nsILocalFileMac,
public nsIHashable
{
friend class nsDirEnumerator;
public:
NS_DEFINE_STATIC_CID_ACCESSOR(NS_LOCAL_FILE_CID)
nsLocalFile();
static nsresult nsLocalFileConstructor(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr);
NS_DECL_ISUPPORTS
NS_DECL_NSIFILE
NS_DECL_NSILOCALFILE
NS_DECL_NSILOCALFILEMAC
NS_DECL_NSIHASHABLE
public:
static void GlobalInit();
static void GlobalShutdown();
private:
~nsLocalFile();
protected:
nsLocalFile(const nsLocalFile& src);
nsresult SetBaseURL(CFURLRef aCFURLRef); // retains aCFURLRef
nsresult GetFSRefInternal(FSRef& aFSRef);
nsresult GetPathInternal(nsACString& path); // Returns path respecting mFollowLinks
nsresult CopyInternal(nsIFile* newParentDir,
const nsAString& newName,
PRBool followLinks);
nsresult FillStatBufferInternal(struct STAT *statBuffer);
protected:
CFURLRef mBaseURL; // The FS object we represent
char mPath[PATH_MAX]; // POSIX path, UTF-8, NULL terminated
PRPackedBool mFollowLinks;
};
#endif // nsLocalFileMac_h_

File diff suppressed because it is too large Load Diff

View File

@ -46,7 +46,7 @@
* ***** END LICENSE BLOCK ***** */
/**
* Implementation of nsIFile for ``Unixy'' systems.
* Implementation of nsIFile for "unixy" systems.
*/
#include <sys/types.h>
@ -92,6 +92,11 @@
#include "nsIGnomeVFSService.h"
#endif
#ifdef XP_MACOSX
#include <Carbon/Carbon.h>
#include "CocoaFileUtils.h"
#endif
#if (MOZ_PLATFORM_MAEMO == 5)
#include <glib.h>
#include <hildon-uri.h>
@ -260,10 +265,18 @@ nsLocalFile::nsLocalFile(const nsLocalFile& other)
{
}
NS_IMPL_THREADSAFE_ISUPPORTS3(nsLocalFile,
nsIFile,
#ifdef XP_MACOSX
NS_IMPL_THREADSAFE_ISUPPORTS4(nsLocalFile,
nsILocalFileMac,
nsILocalFile,
nsIFile,
nsIHashable)
#else
NS_IMPL_THREADSAFE_ISUPPORTS3(nsLocalFile,
nsILocalFile,
nsIFile,
nsIHashable)
#endif
nsresult
nsLocalFile::nsLocalFileConstructor(nsISupports *outer,
@ -1287,7 +1300,7 @@ nsLocalFile::GetParent(nsIFile **aParent)
{
CHECK_mPath();
NS_ENSURE_ARG_POINTER(aParent);
*aParent = nsnull;
*aParent = nsnull;
// if '/' we are at the top of the volume, return null
if (mPath.Equals("/"))
@ -1299,7 +1312,7 @@ nsLocalFile::GetParent(nsIFile **aParent)
// find the last significant slash in buffer
slashp = strrchr(buffer, '/');
NS_ASSERTION(slashp, "non-canonical mPath?");
NS_ASSERTION(slashp, "non-canonical path?");
if (!slashp)
return NS_ERROR_FILE_INVALID_PATH;
@ -1640,7 +1653,6 @@ nsLocalFile::GetNativeTarget(nsACString &_retval)
return rv;
}
/* attribute PRBool followLinks; */
NS_IMETHODIMP
nsLocalFile::GetFollowLinks(PRBool *aFollowLinks)
{
@ -1773,6 +1785,14 @@ nsLocalFile::Reveal()
else
return gnomevfs->ShowURIForInput(dirPath);
}
#elif defined(XP_MACOSX)
CFURLRef url;
if (NS_SUCCEEDED(GetCFURL(&url))) {
nsresult rv = CocoaFileUtils::RevealFileInFinder(url);
::CFRelease(url);
return rv;
}
return NS_ERROR_FAILURE;
#else
return NS_ERROR_FAILURE;
#endif
@ -1823,6 +1843,14 @@ nsLocalFile::Launch()
fileUri.Append(mPath);
mozilla::AndroidBridge* bridge = mozilla::AndroidBridge::Bridge();
return bridge->OpenUriExternal(fileUri, type) ? NS_OK : NS_ERROR_FAILURE;
#elif defined(XP_MACOSX)
CFURLRef url;
if (NS_SUCCEEDED(GetCFURL(&url))) {
nsresult rv = CocoaFileUtils::OpenURL(url);
::CFRelease(url);
return rv;
}
return NS_ERROR_FAILURE;
#else
return NS_ERROR_FAILURE;
#endif
@ -1837,6 +1865,8 @@ NS_NewNativeLocalFile(const nsACString &path, PRBool followSymlinks, nsILocalFil
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(file);
file->SetFollowLinks(followSymlinks);
if (!path.IsEmpty()) {
nsresult rv = file->InitWithNativePath(path);
if (NS_FAILED(rv)) {
@ -1974,3 +2004,429 @@ void
nsLocalFile::GlobalShutdown()
{
}
// nsILocalFileMac
#ifdef XP_MACOSX
static nsresult MacErrorMapper(OSErr inErr)
{
nsresult outErr;
switch (inErr)
{
case noErr:
outErr = NS_OK;
break;
case fnfErr:
case afpObjectNotFound:
case afpDirNotFound:
outErr = NS_ERROR_FILE_NOT_FOUND;
break;
case dupFNErr:
case afpObjectExists:
outErr = NS_ERROR_FILE_ALREADY_EXISTS;
break;
case dskFulErr:
case afpDiskFull:
outErr = NS_ERROR_FILE_DISK_FULL;
break;
case fLckdErr:
case afpVolLocked:
outErr = NS_ERROR_FILE_IS_LOCKED;
break;
case afpAccessDenied:
outErr = NS_ERROR_FILE_ACCESS_DENIED;
break;
case afpDirNotEmpty:
outErr = NS_ERROR_FILE_DIR_NOT_EMPTY;
break;
// Can't find good map for some
case bdNamErr:
outErr = NS_ERROR_FAILURE;
break;
default:
outErr = NS_ERROR_FAILURE;
break;
}
return outErr;
}
static nsresult CFStringReftoUTF8(CFStringRef aInStrRef, nsACString& aOutStr)
{
// 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_IMETHODIMP
nsLocalFile::InitWithCFURL(CFURLRef aCFURL)
{
UInt8 path[PATH_MAX];
if (::CFURLGetFileSystemRepresentation(aCFURL, false, path, PATH_MAX)) {
nsDependentCString nativePath((char*)path);
return InitWithNativePath(nativePath);
}
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsLocalFile::InitWithFSRef(const FSRef *aFSRef)
{
NS_ENSURE_ARG(aFSRef);
CFURLRef newURLRef = ::CFURLCreateFromFSRef(kCFAllocatorDefault, aFSRef);
if (newURLRef) {
nsresult rv = InitWithCFURL(newURLRef);
::CFRelease(newURLRef);
return rv;
}
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsLocalFile::GetCFURL(CFURLRef *_retval)
{
CHECK_mPath();
PRBool isDir;
IsDirectory(&isDir);
*_retval = ::CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault,
(UInt8*)mPath.get(),
mPath.Length(),
isDir);
return (*_retval ? NS_OK : NS_ERROR_FAILURE);
}
NS_IMETHODIMP
nsLocalFile::GetFSRef(FSRef *_retval)
{
NS_ENSURE_ARG_POINTER(_retval);
nsresult rv = NS_ERROR_FAILURE;
CFURLRef url = NULL;
if (NS_SUCCEEDED(GetCFURL(&url))) {
if (::CFURLGetFSRef(url, _retval)) {
rv = NS_OK;
}
::CFRelease(url);
}
return rv;
}
NS_IMETHODIMP
nsLocalFile::GetFSSpec(FSSpec *_retval)
{
NS_ENSURE_ARG_POINTER(_retval);
FSRef fsRef;
nsresult rv = GetFSRef(&fsRef);
if (NS_SUCCEEDED(rv)) {
OSErr err = ::FSGetCatalogInfo(&fsRef, kFSCatInfoNone, nsnull, nsnull, _retval, nsnull);
return MacErrorMapper(err);
}
return rv;
}
NS_IMETHODIMP
nsLocalFile::GetFileSizeWithResFork(PRInt64 *aFileSizeWithResFork)
{
NS_ENSURE_ARG_POINTER(aFileSizeWithResFork);
FSRef fsRef;
nsresult rv = GetFSRef(&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;
}
NS_IMETHODIMP
nsLocalFile::GetFileType(OSType *aFileType)
{
CFURLRef url;
if (NS_SUCCEEDED(GetCFURL(&url))) {
nsresult rv = CocoaFileUtils::GetFileTypeCode(url, aFileType);
::CFRelease(url);
return rv;
}
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsLocalFile::SetFileType(OSType aFileType)
{
CFURLRef url;
if (NS_SUCCEEDED(GetCFURL(&url))) {
nsresult rv = CocoaFileUtils::SetFileTypeCode(url, aFileType);
::CFRelease(url);
return rv;
}
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsLocalFile::GetFileCreator(OSType *aFileCreator)
{
CFURLRef url;
if (NS_SUCCEEDED(GetCFURL(&url))) {
nsresult rv = CocoaFileUtils::GetFileCreatorCode(url, aFileCreator);
::CFRelease(url);
return rv;
}
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsLocalFile::SetFileCreator(OSType aFileCreator)
{
CFURLRef url;
if (NS_SUCCEEDED(GetCFURL(&url))) {
nsresult rv = CocoaFileUtils::SetFileCreatorCode(url, aFileCreator);
::CFRelease(url);
return rv;
}
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsLocalFile::LaunchWithDoc(nsILocalFile *aDocToLoad, PRBool aLaunchInBackground)
{
PRBool isExecutable;
nsresult rv = IsExecutable(&isExecutable);
if (NS_FAILED(rv))
return rv;
if (!isExecutable)
return NS_ERROR_FILE_EXECUTION_FAILED;
FSRef appFSRef, docFSRef;
rv = GetFSRef(&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;
}
NS_IMETHODIMP
nsLocalFile::OpenDocWithApp(nsILocalFile *aAppToOpenWith, PRBool aLaunchInBackground)
{
FSRef docFSRef;
nsresult rv = GetFSRef(&docFSRef);
if (NS_FAILED(rv))
return rv;
if (!aAppToOpenWith) {
OSErr err = ::LSOpenFSRef(&docFSRef, NULL);
return MacErrorMapper(err);
}
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;
FSRef appFSRef;
rv = appFileMac->GetFSRef(&appFSRef);
if (NS_FAILED(rv))
return rv;
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;
OSErr err = ::LSOpenFromRefSpec(&thelaunchSpec, NULL);
if (err != noErr)
return MacErrorMapper(err);
return NS_OK;
}
NS_IMETHODIMP
nsLocalFile::IsPackage(PRBool *_retval)
{
NS_ENSURE_ARG(_retval);
*_retval = PR_FALSE;
CFURLRef url;
nsresult rv = GetCFURL(&url);
if (NS_SUCCEEDED(rv)) {
CFBundleRef bundle = ::CFBundleCreate(kCFAllocatorDefault, url);
if (bundle) {
*_retval = PR_TRUE;
::CFRelease(bundle);
}
}
return rv;
}
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)
{
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;
}
NS_IMETHODIMP nsLocalFile::InitWithFile(nsILocalFile *aFile)
{
NS_ENSURE_ARG(aFile);
nsCAutoString nativePath;
nsresult rv = aFile->GetNativePath(nativePath);
if (NS_FAILED(rv))
return rv;
return InitWithNativePath(nativePath);
}
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;
}
nsresult
NS_NewLocalFileWithCFURL(const CFURLRef aURL, PRBool aFollowLinks, nsILocalFileMac** result)
{
nsLocalFile* file = new nsLocalFile();
if (!file)
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;
}
#endif

View File

@ -53,6 +53,9 @@
#include "nsReadableUtils.h"
#include "nsIHashable.h"
#include "nsIClassInfoImpl.h"
#ifdef XP_MACOSX
#include "nsILocalFileMac.h"
#endif
/**
* we need these for statfs()
@ -101,7 +104,12 @@
#endif
class NS_COM nsLocalFile : public nsILocalFile,
class NS_COM nsLocalFile :
#ifdef XP_MACOSX
public nsILocalFileMac,
#else
public nsILocalFile,
#endif
public nsIHashable
{
public:
@ -111,16 +119,12 @@ public:
static nsresult nsLocalFileConstructor(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr);
// nsISupports
NS_DECL_ISUPPORTS
// nsIFile
NS_DECL_NSIFILE
// nsILocalFile
NS_DECL_NSILOCALFILE
// nsIHashable
#ifdef XP_MACOSX
NS_DECL_NSILOCALFILEMAC
#endif
NS_DECL_NSIHASHABLE
public:
@ -132,8 +136,8 @@ private:
~nsLocalFile() {}
protected:
// This stat cache holds the *last stat* - it does not invalidate.
// Call "FillStatCache" whenever you want to stat our file.
// This stat cache holds the *last stat* - it does not invalidate.
// Call "FillStatCache" whenever you want to stat our file.
struct STAT mCachedStat;
nsCString mPath;