From 14ebd7760481660519221b93fdea61b3900c86d2 Mon Sep 17 00:00:00 2001 From: "conrad%ingress.com" Date: Mon, 17 Jul 2000 15:03:05 +0000 Subject: [PATCH] 1. Added Get/SetPersistentDescriptor. Use this instead of GetPath/InitWithPath. 2. It is now possible to pass nsnull for the path param to NS_NewLocalFile(). This allows one to create an unspecified file. r = dougt git-svn-id: svn://10.0.0.236/trunk@74318 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/xpcom/io/nsLocalFileMac.cpp | 64 ++++++++++++++++++++++++++-- mozilla/xpcom/io/nsLocalFileUnix.cpp | 24 +++++++++-- mozilla/xpcom/io/nsLocalFileWin.cpp | 24 +++++++++-- 3 files changed, 100 insertions(+), 12 deletions(-) diff --git a/mozilla/xpcom/io/nsLocalFileMac.cpp b/mozilla/xpcom/io/nsLocalFileMac.cpp index 25cbbb88b2f..1457f432b56 100644 --- a/mozilla/xpcom/io/nsLocalFileMac.cpp +++ b/mozilla/xpcom/io/nsLocalFileMac.cpp @@ -35,6 +35,7 @@ #include "prerror.h" #include "pprio.h" // Include this rather than prio.h so we get def of PR_ImportFile #include "prmem.h" +#include "plbase64.h" #include "FullPath.h" #include "FileCopy.h" @@ -2063,6 +2064,59 @@ nsLocalFile::GetDirectoryEntries(nsISimpleEnumerator * *entries) return NS_OK; } +NS_IMETHODIMP +nsLocalFile::GetPersistentDescriptor(char * *aPersistentDescriptor) +{ + NS_ENSURE_ARG_POINTER(aPersistentDescriptor); + *aPersistentDescriptor = nsnull; + + nsresult rv = ResolveAndStat( PR_TRUE ); + if ( NS_FAILED( rv ) ) + return rv; + + AliasHandle aliasH; + OSErr err = ::NewAlias(nil, &mTargetSpec, &aliasH); + if (err != noErr) + return MacErrorMapper(err); + + PRUint32 bytes = ::GetHandleSize((Handle) aliasH); + HLock((Handle) aliasH); + char* buf = PL_Base64Encode((const char*)*aliasH, bytes, nsnull); // Passing nsnull for dest makes NULL-term string + ::DisposeHandle((Handle) aliasH); + NS_ENSURE_TRUE(buf, NS_ERROR_OUT_OF_MEMORY); + + *aPersistentDescriptor = buf; + + return NS_OK; +} + +NS_IMETHODIMP +nsLocalFile::SetPersistentDescriptor(const char * aPersistentDescriptor) +{ + NS_ENSURE_ARG(aPersistentDescriptor); + + nsresult rv; + + PRUint32 dataSize = nsCRT::strlen(aPersistentDescriptor); + char* decodedData = PL_Base64Decode((const char*)aPersistentDescriptor, dataSize, nsnull); + // Cast to an alias record and resolve. + AliasHandle aliasH = nsnull; + if (::PtrToHand(decodedData, &(Handle)aliasH, (dataSize * 3) / 4) != noErr) + rv = NS_ERROR_OUT_OF_MEMORY; + PR_Free(decodedData); + NS_ENSURE_SUCCESS(rv, rv); + + Boolean changed; + FSSpec resolvedSpec; + OSErr err; + if ((err = ::ResolveAlias(nsnull, aliasH, &resolvedSpec, &changed)) != noErr) + rv = MacErrorMapper(err); + DisposeHandle((Handle) aliasH); + NS_ENSURE_SUCCESS(rv, rv); + + return InitWithFSSpec(&resolvedSpec); +} + #pragma mark - // a stack-based, exception safe class for an AEDesc @@ -2649,10 +2703,12 @@ NS_NewLocalFile(const char* path, PRBool followLinks, nsILocalFile* *result) file->SetFollowLinks(followLinks); - nsresult rv = file->InitWithPath(path); - if (NS_FAILED(rv)) { - NS_RELEASE(file); - return rv; + if (path) { + nsresult rv = file->InitWithPath(path); + if (NS_FAILED(rv)) { + NS_RELEASE(file); + return rv; + } } *result = file; return NS_OK; diff --git a/mozilla/xpcom/io/nsLocalFileUnix.cpp b/mozilla/xpcom/io/nsLocalFileUnix.cpp index 0aebaa86ae1..5f96ad47e00 100644 --- a/mozilla/xpcom/io/nsLocalFileUnix.cpp +++ b/mozilla/xpcom/io/nsLocalFileUnix.cpp @@ -1328,6 +1328,20 @@ nsLocalFile::Load(PRLibrary **_retval) return NS_OK; } +NS_IMETHODIMP +nsLocalFile::GetPersistentDescriptor(char * *aPersistentDescriptor) +{ + NS_ENSURE_ARG_POINTER(aPersistentDescriptor); + return GetPath(aPersistentDescriptor); +} + +NS_IMETHODIMP +nsLocalFile::SetPersistentDescriptor(const char * aPersistentDescriptor) +{ + NS_ENSURE_ARG(aPersistentDescriptor); + return InitWithPath(aPersistentDescriptor); +} + nsresult NS_NewLocalFile(const char* path, PRBool followSymlinks, nsILocalFile* *result) { @@ -1336,10 +1350,12 @@ NS_NewLocalFile(const char* path, PRBool followSymlinks, nsILocalFile* *result) return NS_ERROR_OUT_OF_MEMORY; NS_ADDREF(file); - nsresult rv = file->InitWithPath(path); - if (NS_FAILED(rv)) { - NS_RELEASE(file); - return rv; + if (path) { + nsresult rv = file->InitWithPath(path); + if (NS_FAILED(rv)) { + NS_RELEASE(file); + return rv; + } } *result = file; return NS_OK; diff --git a/mozilla/xpcom/io/nsLocalFileWin.cpp b/mozilla/xpcom/io/nsLocalFileWin.cpp index ca77c3e7132..6067037aa51 100644 --- a/mozilla/xpcom/io/nsLocalFileWin.cpp +++ b/mozilla/xpcom/io/nsLocalFileWin.cpp @@ -1835,6 +1835,20 @@ nsLocalFile::GetDirectoryEntries(nsISimpleEnumerator * *entries) return NS_OK; } +NS_IMETHODIMP +nsLocalFile::GetPersistentDescriptor(char * *aPersistentDescriptor) +{ + NS_ENSURE_ARG_POINTER(aPersistentDescriptor); + return GetPath(aPersistentDescriptor); +} + +NS_IMETHODIMP +nsLocalFile::SetPersistentDescriptor(const char * aPersistentDescriptor) +{ + NS_ENSURE_ARG(aPersistentDescriptor); + return InitWithPath(aPersistentDescriptor); +} + nsresult NS_NewLocalFile(const char* path, PRBool followLinks, nsILocalFile* *result) @@ -1846,10 +1860,12 @@ NS_NewLocalFile(const char* path, PRBool followLinks, nsILocalFile* *result) file->SetFollowLinks(followLinks); - nsresult rv = file->InitWithPath(path); - if (NS_FAILED(rv)) { - NS_RELEASE(file); - return rv; + if (path) { + nsresult rv = file->InitWithPath(path); + if (NS_FAILED(rv)) { + NS_RELEASE(file); + return rv; + } } *result = file;