Bug 212123. Fix temp file creation in nsFileSpec. r+sr=darin,a=dveditz

This commit is contained in:
roc+%cs.cmu.edu 2005-03-09 00:41:56 +00:00
Родитель 76e83267c4
Коммит a9737587bb
2 изменённых файлов: 16 добавлений и 7 удалений

Просмотреть файл

@ -49,6 +49,7 @@
#include "nsCOMPtr.h"
#include "nsIServiceManager.h"
#include "nsILocalFile.h"
#include "nsIFileStream.h"
#include <string.h>
#include <stdio.h>
@ -909,8 +910,11 @@ void nsFileSpec::MakeUnique(const char* inSuggestedLeafName)
void nsFileSpec::MakeUnique()
//----------------------------------------------------------------------------------------
{
if (!Exists())
return;
nsCOMPtr<nsISupports> file;
nsresult rv = NS_NewIOFileStream(getter_AddRefs(file), *this,
PR_WRONLY | PR_CREATE_FILE | PR_EXCL | PR_TRUNCATE, 0600);
if (NS_SUCCEEDED(rv))
return; // the stream will be closed automatically
char* leafName = GetLeafName();
if (!leafName)
@ -927,12 +931,16 @@ void nsFileSpec::MakeUnique()
= nsFileSpecHelpers::kMaxCoreLeafNameLength - strlen(suffix) - 1;
if ((int)strlen(leafName) > (int)kMaxRootLength)
leafName[kMaxRootLength] = '\0';
for (short indx = 1; indx < 1000 && Exists(); indx++)
for (short indx = 1; indx < 1000; 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);
rv = NS_NewIOFileStream(getter_AddRefs(file), *this,
PR_WRONLY | PR_CREATE_FILE | PR_EXCL | PR_TRUNCATE, 0600);
if (NS_SUCCEEDED(rv))
break;
}
if (*suffix)
nsCRT::free(suffix);

Просмотреть файл

@ -460,10 +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()
PRBool Exists() const;
@ -476,6 +472,11 @@ 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);