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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user