LazUtils: control how CopyFile works using flags. Issue #21854, modified from a patch from Takeda Matsuki

git-svn-id: trunk@37060 -
This commit is contained in:
juha 2012-04-28 13:02:07 +00:00
parent c53c098c48
commit 9fcb610a8e
2 changed files with 34 additions and 15 deletions

View File

@ -939,19 +939,8 @@ begin
and (CompareFilenames(ExpDir,LeftStr(ExpFile,p))=0);
end;
{------------------------------------------------------------------------------
function CopyFile(const SrcFilename, DestFilename: string): boolean;
------------------------------------------------------------------------------}
function CopyFile(const SrcFilename, DestFilename: string): boolean;
begin
Result := CopyFile(SrcFilename, DestFilename, false);
end;
{------------------------------------------------------------------------------
function CopyFile(const SrcFilename, DestFilename: string PreserveTime:
boolean): boolean;
------------------------------------------------------------------------------}
function CopyFile(const SrcFilename, DestFilename: String; PreserveTime: Boolean): Boolean;
function CopyFile(const SrcFilename, DestFilename: String;
Flags: TCopyFileFlags=[cffOverwriteFile]): Boolean;
var
SrcFS: TFileStreamUTF8;
DestFS: TFileStreamUTF8;
@ -959,13 +948,19 @@ begin
try
SrcFS := TFileStreamUTF8.Create(SrcFilename, fmOpenRead or fmShareDenyWrite);
try
if (not (cffOverwriteFile in Flags)) and FileExistsUTF8(DestFileName) then
exit(False);
if (cffCreateDestDirectory in Flags)
and (not DirectoryExistsUTF8(ExtractFilePath(DestFileName)))
and (not ForceDirectoriesUTF8(ExtractFilePath(DestFileName))) then
exit(False);
DestFS := TFileStreamUTF8.Create(DestFilename, fmCreate);
try
DestFS.CopyFrom(SrcFS, SrcFS.Size);
finally
DestFS.Free;
end;
if PreserveTime then
if (cffPreserveTime in Flags) then
FileSetDateUTF8(DestFilename, FileGetDate(SrcFS.Handle));
finally
SrcFS.Free;
@ -974,6 +969,18 @@ begin
except
Result := False;
end;
end;
function CopyFile(const SrcFilename, DestFilename: string; PreserveTime: Boolean): boolean;
// Deparecated because Flags parameter can used for the same thing.
var
Flags: TCopyFileFlags;
begin
if PreserveTime then
Flags:=[cffPreserveTime]
else
Flags:=[];
Result := CopyFile(SrcFilename, DestFilename, Flags);
end;
function GetTempFilename(const Directory, Prefix: string): string;

View File

@ -171,10 +171,22 @@ function FindAllFiles(const SearchPath: String; SearchMask: String = '';
function FindAllDirectories(const SearchPath: string;
SearchSubDirs: Boolean = True): TStringList;
// file copy
type
TCopyFileFlag = (
cffOverwriteFile,
cffCreateDestDirectory,
cffPreserveTime
);
TCopyFileFlags = set of TCopyFileFlag;
// file actions
function ReadFileToString(const Filename: string): string;
function CopyFile(const SrcFilename, DestFilename: string): boolean;
function CopyFile(const SrcFilename, DestFilename: string;
Flags: TCopyFileFlags=[cffOverwriteFile]): boolean;
function CopyFile(const SrcFilename, DestFilename: string; PreserveTime: boolean): boolean;
deprecated {$IFDEF VER2_5}'use cffPreserveTime in Flags parameter instead'{$ENDIF};
function GetTempFilename(const Directory, Prefix: string): string;
// basic functions similar to the RTL but working with UTF-8 instead of the