Bug 122892: nsLocalFile::Clone should preserve stat info.

make nsLocalFile* impls use copy constructors for their nsIFile::Clone methods.
This avoids unnecessary |stat| calls inherent in using NS_NewNativeLocalFile.

b=122892, r=dougt, sr=darin, with many thanks to biesi & mkaply for testing on other
plats.
This commit is contained in:
dwitte%stanford.edu 2003-07-18 22:14:16 +00:00
parent 46938f6907
commit 9ae54e162c
8 changed files with 51 additions and 45 deletions

View File

@ -1084,15 +1084,11 @@ nsLocalFile::ResolveAndStat()
NS_IMETHODIMP
nsLocalFile::Clone(nsIFile **file)
{
NS_ENSURE_ARG(file);
*file = nsnull;
// Just copy-construct ourselves
nsCOMPtr<nsILocalFile> localFile = new nsLocalFile(*this);
if (localFile == NULL)
*file = new nsLocalFile(*this);
if (!*file)
return NS_ERROR_OUT_OF_MEMORY;
*file = localFile;
NS_ADDREF(*file);
return NS_OK;

View File

@ -250,6 +250,13 @@ nsLocalFile::nsLocalFile()
MakeDirty();
}
nsLocalFile::nsLocalFile(const nsLocalFile& other)
: mDirty(other.mDirty)
, mWorkingPath(other.mWorkingPath)
, mFileInfo64(other.mFileInfo64)
{
}
nsLocalFile::~nsLocalFile()
{
}
@ -313,18 +320,14 @@ nsLocalFile::Stat()
NS_IMETHODIMP
nsLocalFile::Clone(nsIFile **file)
{
nsresult rv;
// Just copy-construct ourselves
*file = new nsLocalFile(*this);
if (!*file)
return NS_ERROR_OUT_OF_MEMORY;
nsCOMPtr<nsILocalFile> localFile;
rv = NS_NewNativeLocalFile(mWorkingPath, PR_TRUE, getter_AddRefs(localFile));
NS_ADDREF(*file);
if (NS_SUCCEEDED(rv) && localFile)
{
return localFile->QueryInterface(NS_GET_IID(nsIFile), (void**)file);
}
return rv;
return NS_OK;
}
NS_IMETHODIMP

View File

@ -79,6 +79,7 @@ public:
static void GlobalShutdown();
private:
nsLocalFile(const nsLocalFile& other);
// this is the flag which indicates if I can used cached information about the file
PRPackedBool mDirty;

View File

@ -997,13 +997,11 @@ NS_IMETHODIMP nsLocalFile::IsSpecial(PRBool *_retval)
/* nsIFile clone (); */
NS_IMETHODIMP nsLocalFile::Clone(nsIFile **_retval)
{
NS_ENSURE_ARG_POINTER(_retval);
*_retval = nsnull;
nsLocalFile *newFile = new nsLocalFile(*this);
if (!newFile)
return NS_ERROR_OUT_OF_MEMORY;
*_retval = newFile;
// Just copy-construct ourselves
*_retval = new nsLocalFile(*this);
if (!*_retval)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(*_retval);
return NS_OK;

View File

@ -201,6 +201,13 @@ nsLocalFile::nsLocalFile() :
{
}
nsLocalFile::nsLocalFile(const nsLocalFile& other)
: mCachedStat(other.mCachedStat)
, mPath(other.mPath)
, mHaveCachedStat(other.mHaveCachedStat)
{
}
nsLocalFile::~nsLocalFile()
{
}
@ -240,18 +247,13 @@ nsLocalFile::FillStatCache() {
NS_IMETHODIMP
nsLocalFile::Clone(nsIFile **file)
{
NS_ENSURE_ARG(file);
// Just copy-construct ourselves
*file = new nsLocalFile(*this);
if (!*file)
return NS_ERROR_OUT_OF_MEMORY;
nsLocalFile* localFile = new nsLocalFile();
if (!localFile)
return NS_ERROR_OUT_OF_MEMORY;
nsresult rv = localFile->InitWithNativePath(mPath);
if (NS_FAILED(rv))
return rv;
*file = NS_STATIC_CAST(nsIFile *, localFile);
NS_ADDREF(*file);
return NS_OK;
}

View File

@ -87,6 +87,9 @@ public:
static void GlobalInit();
static void GlobalShutdown();
private:
nsLocalFile(const nsLocalFile& other);
protected:
struct stat mCachedStat;
nsCString mPath;

View File

@ -438,6 +438,16 @@ NS_IMPL_THREADSAFE_ISUPPORTS2(nsLocalFile, nsILocalFile, nsIFile)
// nsLocalFile <private>
//-----------------------------------------------------------------------------
nsLocalFile::nsLocalFile(const nsLocalFile& other)
: mDirty(other.mDirty)
, mLastResolution(other.mLastResolution)
, mFollowSymlinks(other.mFollowSymlinks)
, mWorkingPath(other.mWorkingPath)
, mResolvedPath(other.mResolvedPath)
, mFileInfo64(other.mFileInfo64)
{
}
// This function resets any cached information about the file.
void
nsLocalFile::MakeDirty()
@ -693,21 +703,13 @@ nsLocalFile::ResolveAndStat(PRBool resolveTerminal)
NS_IMETHODIMP
nsLocalFile::Clone(nsIFile **file)
{
NS_ENSURE_ARG(file);
*file = nsnull;
// Just copy-construct ourselves
nsLocalFile *localFile = new nsLocalFile(*this);
if (localFile == NULL)
return NS_ERROR_OUT_OF_MEMORY;
*file = new nsLocalFile(*this);
if (!*file)
return NS_ERROR_OUT_OF_MEMORY;
// don't forget to re-initialize mRefCnt
// or the new object will have the old refcnt
localFile->mRefCnt = 0;
*file = localFile;
NS_ADDREF(*file);
return NS_OK;
}

View File

@ -68,6 +68,7 @@ public:
static void GlobalShutdown();
private:
nsLocalFile(const nsLocalFile& other);
// this is the flag which indicates if I can used cached information about the file
PRPackedBool mDirty;