UPD: GetTempName function

This commit is contained in:
Alexander Koblov 2022-06-22 18:30:01 +03:00
commit ea7eb6972c
7 changed files with 22 additions and 16 deletions

View file

@ -139,10 +139,10 @@ function FileIsReadOnly(iAttr: TFileAttrs): Boolean; inline;
The directories in this path are not created if they don't exist.
If it is empty then the system temporary directory is used.
For example:
If PathPrefix is '/tmp/myfile' then files '/tmp/myfileXXXXXX' are tried.
If PathPrefix is '/tmp/myfile' then files '/tmp/myfile~XXXXXX.tmp' are tried.
The path '/tmp' must already exist.)
}
function GetTempName(PathPrefix: String): String;
function GetTempName(PathPrefix: String; Extension: String = 'tmp'): String;
(* File mapping/unmapping routines *)
{en
@ -613,7 +613,7 @@ begin
end;
{$ENDIF}
function GetTempName(PathPrefix: String): String;
function GetTempName(PathPrefix: String; Extension: String): String;
const
MaxTries = 100;
var
@ -623,12 +623,18 @@ begin
if PathPrefix = '' then
PathPrefix := GetTempDir
else begin
FileName:= ExtractFileName(PathPrefix);
FileName:= ExtractOnlyFileName(PathPrefix);
PathPrefix:= ExtractFilePath(PathPrefix);
// Generated file name should be less the maximum file name length
PathPrefix:= ExtractFilePath(PathPrefix) + UTF8Copy(FileName, 1, 48);
if (Length(FileName) > 0) then PathPrefix += UTF8Copy(FileName, 1, 48) + '~';
end;
if (Length(Extension) > 0) then
begin
if (not StrBegins(Extension, ExtensionSeparator)) then
Extension := ExtensionSeparator + Extension;
end;
repeat
Result := PathPrefix + IntToStr(System.Random(MaxInt)); // or use CreateGUID()
Result := PathPrefix + IntToStr(System.Random(MaxInt)) + Extension;
Inc(TryNumber);
if TryNumber = MaxTries then
Exit('');

View file

@ -213,7 +213,7 @@ var
begin
repeat
bRetry := False;
NewName:= GetTempName(ExtractFilePath(FileName)) + '.tmp';
NewName:= GetTempName(ExtractFilePath(FileName));
Result := RenameFileUAC(FileName, NewName);
if not Result then
bRetry := HandleError(Format(rsMsgErrRename, [FileName, NewName]));

View file

@ -2344,7 +2344,7 @@ var
begin
AFileList := TStringListEx.Create;
AFileName := GetTempFolderDeletableAtTheEnd;
AFileName := GetTempName(AFileName) + '.txt';
AFileName := GetTempName(AFileName, 'txt');
if FNames.Count > 0 then
AFileList.Assign(FNames)
else
@ -2378,7 +2378,7 @@ begin
try
for iIndexFile := 0 to pred(FFiles.Count) do
AFileList.Add(FreshText(iIndexFile));
sFileName := GetTempName(GetTempFolderDeletableAtTheEnd) + '.txt';
sFileName := GetTempName(GetTempFolderDeletableAtTheEnd, 'txt');
try
AFileList.SaveToFile(sFileName);
try
@ -2483,7 +2483,7 @@ begin
begin
NewName := AFile.Name;
// Generate temp file name, save file index as extension
AFile.FullPath := GetTempName(FFiles[I].Path) + ExtensionSeparator + IntToStr(I);
AFile.FullPath := GetTempName(FFiles[I].Path, IntToStr(I));
TempFiles.AddObject(NewName, AFile.Clone);
end;

View file

@ -593,7 +593,7 @@ begin
SplitCmdLine(CustomFile, Value, Args);
CustomFile:= 'dc_' + ExtractFileName(Value) + '_';
CustomFile:= UserDataDir + PathDelim + CustomFile;
CustomFile:= GetTempName(CustomFile) + '.desktop';
CustomFile:= GetTempName(CustomFile, 'desktop');
try
DesktopFile:= TIniFileEx.Create(CustomFile, fmCreate or fmOpenReadWrite);
try

View file

@ -2368,7 +2368,7 @@ begin
if bMakeViaCopy then
begin
Directory := GetTempName(GetTempFolderDeletableAtTheEnd);
Directory := GetTempName(GetTempFolderDeletableAtTheEnd, EmptyStr);
if not mbForceDirectory(IncludeTrailingBackslash(Directory) + sPath) then
begin
MessageDlg(mbSysErrorMessage(GetLastOSError), mtError, [mbOK], 0);

View file

@ -358,7 +358,7 @@ type
FileList: TFileStreamEx;
LineEndingA: ansistring = LineEnding;
begin
Result := GetTempName(GetTempFolderDeletableAtTheEnd + 'Filelist') + '.lst';
Result := GetTempName(GetTempFolderDeletableAtTheEnd + 'Filelist', 'lst');
try
FileList := TFileStreamEx.Create(Result, fmCreate);
try
@ -480,7 +480,7 @@ type
sTmpFilename, sShellCmdLine: string;
Process: TProcessUTF8;
begin
sTmpFilename := GetTempName(GetTempFolderDeletableAtTheEnd) + '.tmp';
sTmpFilename := GetTempName(GetTempFolderDeletableAtTheEnd);
//sShellCmdLine := Copy(state.sSubParam, 3, length(state.sSubParam)-2) + ' > ' + QuoteStr(sTmpFilename);
sShellCmdLine := state.sSubParam + ' > ' + QuoteStr(sTmpFilename);
Process := TProcessUTF8.Create(nil);
@ -991,7 +991,7 @@ begin
iCount := Posex('?>', sParams, iStart) - iStart;
if (iStart <> 0) and (iCount >= 0) then
begin
sTmpFile := GetTempName(GetTempFolderDeletableAtTheEnd) + '.tmp';
sTmpFile := GetTempName(GetTempFolderDeletableAtTheEnd);
sShellCmdLine := Copy(sParams, iStart, iCount) + ' > ' + QuoteStr(sTmpFile);
Process := TProcessUTF8.Create(nil);
try

View file

@ -751,7 +751,7 @@ begin
Exit(pdrFailed);
end;
Directory := GetTempName(GetTempFolderDeletableAtTheEnd);
Directory := GetTempName(GetTempFolderDeletableAtTheEnd, EmptyStr);
if not mbForceDirectory(Directory) then
begin
MessageDlg(mbSysErrorMessage(GetLastOSError), mtError, [mbOK], 0);