1
0
mirror of https://github.com/salvadordf/CEF4Delphi.git synced 2025-09-30 21:28:55 +02:00

Fixed issue #569. Multiple fixes to bugs in temporary directory cleaning code. Thanks to AlexAT.

This commit is contained in:
Salvador Díaz Fau
2025-09-17 16:05:21 +02:00
parent 435cfe8cae
commit 9533fc2257
4 changed files with 78 additions and 27 deletions

View File

@@ -321,12 +321,14 @@ end;
procedure CreateGlobalCEFApp;
begin
GlobalCEFApp := TCefApplication.Create;
GlobalCEFApp.cache := 'cache';
GlobalCEFApp.RootCache := 'RootCache';
GlobalCEFApp.EnablePrintPreview := True;
GlobalCEFApp.EnableGPU := True;
GlobalCEFApp.LogFile := 'debug.log';
GlobalCEFApp.LogSeverity := LOGSEVERITY_INFO;
GlobalCEFApp.UncaughtExceptionStackSize := 50;
//GlobalCEFApp.DeleteCache := True;
//GlobalCEFApp.DeleteCookies := True;
GlobalCEFApp.OnUncaughtException := GlobalCEFApp_OnUncaughtException;
end;

View File

@@ -3208,7 +3208,7 @@ begin
try
TempFiles.Add('Cookies');
TempFiles.Add('Cookies-journal');
TempFiles.Add('LocalPrefs.json');
TempFiles.Add('Local State');
DeleteDirContents(aDirectory, TempFiles);
finally
@@ -3288,7 +3288,7 @@ begin
TempNewDir := TempOldDir + '_' + inttostr(i);
until not(DirectoryExists(TempNewDir));
if RenameFile(TempOldDir, TempNewDir) then
if TryRenameDir(TempOldDir, TempNewDir) then
begin
if aKeepCookies then
MoveCookiesDB(TempNewDir, TempOldDir);
@@ -4983,7 +4983,8 @@ begin
{$IFDEF DELPHI14_UP}
TDirectory.Delete(FDirectory, True);
{$ELSE}
if DeleteDirContents(FDirectory) then RemoveDir(FDirectory);
if DeleteDirContents(FDirectory) then
TryRemoveDir(FDirectory);
{$ENDIF}
except
on e : exception do

View File

@@ -800,6 +800,11 @@ procedure LogicalToDevice(var aRect : TCEFRect; const aDeviceScaleFactor : doubl
function GetScreenDPI : integer;
function GetDeviceScaleFactor : single;
function TryRemoveDir(const aDirectory : string): boolean;
function TryDeleteFile(const aFileName : string): boolean;
function TryRenameDir(const aOldName, aNewName : string): boolean;
function TryRenameFile(const aOldName, aNewName : string): boolean;
function DeleteDirContents(const aDirectory : string; const aExcludeFiles : TStringList = nil) : boolean;
function DeleteFileList(const aFileList : TStringList) : boolean;
function MoveFileList(const aFileList : TStringList; const aSrcDirectory, aDstDirectory : string) : boolean;
@@ -3202,6 +3207,54 @@ begin
{$ENDIF}
end;
function TryRemoveDir(const aDirectory : string): boolean;
begin
Result := False;
try
if DirectoryExists(aDirectory) then
Result := RemoveDir(aDirectory);
except
on e : exception do
if CustomExceptionHandler('TryRemoveDir', e) then raise;
end;
end;
function TryDeleteFile(const aFileName : string): boolean;
begin
Result := False;
try
if FileExists(aFileName) then
Result := DeleteFile(aFileName);
except
on e : exception do
if CustomExceptionHandler('TryDeleteFile', e) then raise;
end;
end;
function TryRenameFile(const aOldName, aNewName : string): boolean;
begin
Result := False;
try
if FileExists(aOldName) and not(FileExists(aNewName)) then
Result := RenameFile(aOldName, aNewName);
except
on e : exception do
if CustomExceptionHandler('TryRenameFile', e) then raise;
end;
end;
function TryRenameDir(const aOldName, aNewName : string): boolean;
begin
Result := False;
try
if DirectoryExists(aOldName) and not(DirectoryExists(aNewName)) then
Result := RenameFile(aOldName, aNewName);
except
on e : exception do
if CustomExceptionHandler('TryRenameDir', e) then raise;
end;
end;
function DeleteDirContents(const aDirectory : string; const aExcludeFiles : TStringList) : boolean;
var
TempRec : TSearchRec;
@@ -3223,7 +3276,7 @@ begin
if (TempRec.Name <> '.') and (TempRec.Name <> '..') then
begin
if DeleteDirContents(TempPath, aExcludeFiles) then
Result := ((TempRec.Name = 'Network') or RemoveDir(TempPath)) and Result
Result := TryRemoveDir(TempPath) and Result
else
Result := False;
end;
@@ -3233,13 +3286,13 @@ begin
begin
TempIdx := aExcludeFiles.IndexOf(TempRec.Name);
Result := ((TempIdx >= 0) or
((TempIdx < 0) and DeleteFile(TempPath))) and
((TempIdx < 0) and TryDeleteFile(TempPath))) and
Result;
end
else
Result := DeleteFile(TempPath) and Result;
Result := TryDeleteFile(TempPath) and Result;
until (FindNext(TempRec) <> 0) or not(Result);
until (FindNext(TempRec) <> 0);
finally
FindClose(TempRec);
end;
@@ -3255,24 +3308,19 @@ var
begin
Result := False;
try
if (aFileList <> nil) then
begin
i := 0;
TempCount := 0;
if (aFileList <> nil) then
begin
i := 0;
TempCount := 0;
while (i < aFileList.Count) do
begin
if FileExists(aFileList[i]) and DeleteFile(aFileList[i]) then inc(TempCount);
inc(i);
end;
while (i < aFileList.Count) do
begin
if TryDeleteFile(aFileList[i]) then inc(TempCount);
inc(i);
end;
Result := (aFileList.Count = TempCount);
end;
except
on e : exception do
if CustomExceptionHandler('DeleteFileList', e) then raise;
end;
Result := (aFileList.Count = TempCount);
end;
end;
function MoveFileList(const aFileList : TStringList; const aSrcDirectory, aDstDirectory : string) : boolean;
@@ -3287,7 +3335,7 @@ begin
(length(aSrcDirectory) > 0) and
(length(aDstDirectory) > 0) and
DirectoryExists(aSrcDirectory) and
(DirectoryExists(aDstDirectory) or CreateDir(aDstDirectory)) then
(DirectoryExists(aDstDirectory) or ForceDirectories(aDstDirectory)) then
begin
i := 0;
TempCount := 0;
@@ -3297,7 +3345,7 @@ begin
TempSrcPath := IncludeTrailingPathDelimiter(aSrcDirectory) + aFileList[i];
TempDstPath := IncludeTrailingPathDelimiter(aDstDirectory) + aFileList[i];
if FileExists(TempSrcPath) and RenameFile(TempSrcPath, TempDstPath) then inc(TempCount);
if TryRenameFile(TempSrcPath, TempDstPath) then inc(TempCount);
inc(i);
end;

View File

@@ -2,7 +2,7 @@
"UpdateLazPackages" : [
{
"ForceNotify" : true,
"InternalVersion" : 785,
"InternalVersion" : 786,
"Name" : "cef4delphi_lazarus.lpk",
"Version" : "139.0.40"
}