diff --git a/xpcom/obsolete/nsFileSpec.cpp b/xpcom/obsolete/nsFileSpec.cpp index 9be29eb903b..0bc073a3337 100644 --- a/xpcom/obsolete/nsFileSpec.cpp +++ b/xpcom/obsolete/nsFileSpec.cpp @@ -909,34 +909,22 @@ void nsFileSpec::MakeUnique(const char* inSuggestedLeafName) void nsFileSpec::MakeUnique() //---------------------------------------------------------------------------------------- { - if (!Exists()) - return; - - char* leafName = GetLeafName(); - if (!leafName) - return; - - char* lastDot = strrchr(leafName, '.'); - char* suffix = ""; - if (lastDot) + // XXX: updated path starts empty. In case of error this will cause + // any callers to fail badly, but that seems better than letting them + // re-use the default name which has failed to be unique. + nsCAutoString path; + nsCOMPtr localFile; + NS_NewNativeLocalFile(nsDependentCString(*this), PR_TRUE, getter_AddRefs(localFile)); + if (localFile) { - suffix = nsCRT::strdup(lastDot); // include '.' - *lastDot = '\0'; // strip suffix and dot. + nsresult rv = localFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600); + if (NS_SUCCEEDED(rv)) + localFile->GetNativePath(path); } - const int kMaxRootLength - = nsFileSpecHelpers::kMaxCoreLeafNameLength - strlen(suffix) - 1; - if ((int)strlen(leafName) > (int)kMaxRootLength) - leafName[kMaxRootLength] = '\0'; - for (short indx = 1; indx < 1000 && Exists(); indx++) - { - // start with "Picture-1.jpg" after "Picture.jpg" exists - char newName[nsFileSpecHelpers::kMaxFilenameLength + 1]; - sprintf(newName, "%s-%d%s", leafName, indx, suffix); - SetLeafName(newName); - } - if (*suffix) - nsCRT::free(suffix); - nsCRT::free(leafName); + + NS_WARN_IF_FALSE(!path.IsEmpty(), "MakeUnique() failed!"); + *this = path.get(); // reset the filepath to point to the unique location + } // nsFileSpec::MakeUnique //---------------------------------------------------------------------------------------- diff --git a/xpcom/obsolete/nsFileSpec.h b/xpcom/obsolete/nsFileSpec.h index 53fa6c1a18b..e7f20c3e9ed 100644 --- a/xpcom/obsolete/nsFileSpec.h +++ b/xpcom/obsolete/nsFileSpec.h @@ -460,9 +460,6 @@ class NS_COM_OBSOLETE nsFileSpec void operator += (const char* inRelativeUnixPath); - void MakeUnique(); - void MakeUnique(const char* inSuggestedLeafName); - PRBool IsDirectory() const; // More stringent than Exists() PRBool IsFile() const; // More stringent than Exists() @@ -476,6 +473,10 @@ class NS_COM_OBSOLETE nsFileSpec // Creation and deletion of objects. These can modify the disk. //-------------------------------------------------- + // For security reasons, these create the file. + void MakeUnique(); + void MakeUnique(const char* inSuggestedLeafName); + // Called for the spec of an alias. Modifies the spec to // point to the original. Sets mError. nsresult ResolveSymlink(PRBool& wasSymlink);