LCL: moved ReadAllLinks to LazUtils

git-svn-id: trunk@44577 -
This commit is contained in:
mattias 2014-04-01 10:24:50 +00:00
parent 13b4f9aa2d
commit 877c4d50b9
7 changed files with 79 additions and 84 deletions

View File

@ -390,11 +390,15 @@ begin
Result := LazFileUtils.FileIsWritable(AFilename); Result := LazFileUtils.FileIsWritable(AFilename);
end; end;
function ReadAllLinks(const Filename: string; ExceptionOnError: boolean
): string;
begin
Result:=LazFileUtils.ReadAllLinks(Filename,ExceptionOnError);
end;
function TryReadAllLinks(const Filename: string): string; function TryReadAllLinks(const Filename: string): string;
begin begin
Result:=ReadAllLinks(Filename,false); Result:=LazFileUtils.TryReadAllLinks(Filename);
if Result='' then
Result:=Filename;
end; end;
{------------------------------------------------------------------------------ {------------------------------------------------------------------------------

View File

@ -67,9 +67,8 @@ function FileIsSymlink(const AFilename: string): boolean; inline;
function FileIsHardLink(const AFilename: string): boolean; inline; function FileIsHardLink(const AFilename: string): boolean; inline;
function FileSize(const Filename: string): int64; overload; inline; function FileSize(const Filename: string): int64; overload; inline;
function GetFileDescription(const AFilename: string): string; inline; function GetFileDescription(const AFilename: string): string; inline;
function ReadAllLinks(const Filename: string; function ReadAllLinks(const Filename: string; ExceptionOnError: boolean): string; inline;
ExceptionOnError: boolean): string; // if a link is broken returns '' function TryReadAllLinks(const Filename: string): string; inline;
function TryReadAllLinks(const Filename: string): string; // if a link is broken returns Filename
// directories // directories
function DirPathExists(const FileName: String): Boolean; inline; function DirPathExists(const FileName: String): Boolean; inline;
@ -262,7 +261,7 @@ uses
{$IFDEF windows} {$IFDEF windows}
Windows; Windows;
{$ELSE} {$ELSE}
Unix, BaseUnix; Unix;
{$ENDIF} {$ENDIF}
{$I fileutil.inc} {$I fileutil.inc}

View File

@ -103,7 +103,9 @@ Function FileCreateUtf8(Const FileName : String; ShareMode : Integer; Rights : C
function FileSizeUtf8(const Filename: string): int64; function FileSizeUtf8(const Filename: string): int64;
function GetFileDescription(const AFilename: string): string; function GetFileDescription(const AFilename: string): string;
function ReadAllLinks(const Filename: string;
ExceptionOnError: boolean): string; // if a link is broken returns ''
function TryReadAllLinks(const Filename: string): string; // if a link is broken returns Filename
function GetAppConfigDirUTF8(Global: Boolean; Create: boolean = false): string; function GetAppConfigDirUTF8(Global: Boolean; Create: boolean = false): string;
function GetAppConfigFileUTF8(Global: Boolean; SubDir: boolean = false; function GetAppConfigFileUTF8(Global: Boolean; SubDir: boolean = false;
@ -984,6 +986,12 @@ begin
Result := DoForceDirectories(GetForcedPathDelims(Dir)); Result := DoForceDirectories(GetForcedPathDelims(Dir));
end; end;
function TryReadAllLinks(const Filename: string): string;
begin
Result:=ReadAllLinks(Filename,false);
if Result='' then
Result:=Filename;
end;
procedure InvalidateFileStateCache(const Filename: string); procedure InvalidateFileStateCache(const Filename: string);
begin begin

View File

@ -1,61 +1,6 @@
{%MainUnit fileutil.pas} {%MainUnit fileutil.pas}
{------------------------------------------------------------------------------
function ReadAllLinks(const Filename: string;
ExceptionOnError: boolean): string;
------------------------------------------------------------------------------}
function ReadAllLinks(const Filename: string;
ExceptionOnError: boolean): string;
var
LinkFilename: string;
AText: string;
Depth: Integer;
begin
Result:=Filename;
Depth:=0;
while Depth<12 do begin
inc(Depth);
LinkFilename:=FpReadLink(Result);
if LinkFilename='' then begin
AText:='"'+Filename+'"';
case fpGetErrno() of
ESysEAcces:
AText:='read access denied for '+AText;
ESysENoEnt:
AText:='a directory component in '+AText
+' does not exist or is a dangling symlink';
ESysENotDir:
AText:='a directory component in '+AText+' is not a directory';
ESysENoMem:
AText:='insufficient memory';
ESysELoop:
AText:=AText+' has a circular symbolic link';
else
// not a symbolic link, just a regular file
exit;
end;
if (not ExceptionOnError) then begin
Result:='';
exit;
end;
raise EFOpenError.Create(AText);
end else begin
if not FilenameIsAbsolute(LinkFilename) then
Result:=ExpandFileNameUTF8(ExtractFilePath(Result)+LinkFilename)
else
Result:=LinkFilename;
end;
end;
// probably an endless loop
if ExceptionOnError then
raise EFOpenError.Create('too many links, maybe an endless loop.')
else
Result:='';
end;
function ExtractShortPathNameUTF8(const FileName: String): String; function ExtractShortPathNameUTF8(const FileName: String): String;
begin begin
Result:=SysToUTF8(SysUtils.ExtractShortPathName(UTF8ToSys(FileName))); Result:=SysToUTF8(SysUtils.ExtractShortPathName(UTF8ToSys(FileName)));

View File

@ -68,6 +68,59 @@ begin
Result := st.st_size; Result := st.st_size;
end; end;
{------------------------------------------------------------------------------
function ReadAllLinks(const Filename: string;
ExceptionOnError: boolean): string;
------------------------------------------------------------------------------}
function ReadAllLinks(const Filename: string;
ExceptionOnError: boolean): string;
var
LinkFilename: string;
AText: string;
Depth: Integer;
begin
Result:=Filename;
Depth:=0;
while Depth<12 do begin
inc(Depth);
LinkFilename:=FpReadLink(Result);
if LinkFilename='' then begin
AText:='"'+Filename+'"';
case fpGetErrno() of
ESysEAcces:
AText:='read access denied for '+AText;
ESysENoEnt:
AText:='a directory component in '+AText
+' does not exist or is a dangling symlink';
ESysENotDir:
AText:='a directory component in '+AText+' is not a directory';
ESysENoMem:
AText:='insufficient memory';
ESysELoop:
AText:=AText+' has a circular symbolic link';
else
// not a symbolic link, just a regular file
exit;
end;
if (not ExceptionOnError) then begin
Result:='';
exit;
end;
raise EFOpenError.Create(AText);
end else begin
if not FilenameIsAbsolute(LinkFilename) then
Result:=ExpandFileNameUTF8(ExtractFilePath(Result)+LinkFilename)
else
Result:=LinkFilename;
end;
end;
// probably an endless loop
if ExceptionOnError then
raise EFOpenError.Create('too many links, maybe an endless loop.')
else
Result:='';
end;
function CreateDirUTF8(const NewDir: String): Boolean; function CreateDirUTF8(const NewDir: String): Boolean;
begin begin
Result:=SysUtils.CreateDir(UTF8ToSys(NewDir)); Result:=SysUtils.CreateDir(UTF8ToSys(NewDir));

View File

@ -1,20 +1,5 @@
{%MainUnit fileutil.pas} {%MainUnit fileutil.pas}
{------------------------------------------------------------------------------
function ReadAllLinks(const Filename: string;
ExceptionOnError: boolean): string;
------------------------------------------------------------------------------}
function ReadAllLinks(const Filename: string;
ExceptionOnError: boolean): string;
begin
Result:=Filename;
end;
function ExtractShortPathNameUTF8(const FileName: String): String; function ExtractShortPathNameUTF8(const FileName: String): String;
var var
lPathSize: DWORD; lPathSize: DWORD;

View File

@ -139,6 +139,13 @@ begin
Result := _GetAppConfigFileUTF8(Global, SubDir, CreateDir); Result := _GetAppConfigFileUTF8(Global, SubDir, CreateDir);
end; end;
function ReadAllLinks(const Filename: string;
ExceptionOnError: boolean): string;
begin
// not supported under Windows
Result:=Filename;
end;
// ******** Start of AnsiString specific implementations ************ // ******** Start of AnsiString specific implementations ************
{$ifndef WinCE} {$ifndef WinCE}
@ -734,16 +741,12 @@ end;
// ******** End of WideString specific implementations ************ // ******** End of WideString specific implementations ************
function FilenameIsAbsolute(const TheFilename: string):boolean; function FilenameIsAbsolute(const TheFilename: string):boolean;
begin begin
Result:=FilenameIsWinAbsolute(TheFilename); Result:=FilenameIsWinAbsolute(TheFilename);
end; end;
function ExpandFileNameUtf8(const FileName: string; {const} BaseDir: String = ''): String; function ExpandFileNameUtf8(const FileName: string; {const} BaseDir: String = ''): String;
var var
IsAbs, StartsWithRoot, CanUseBaseDir : Boolean; IsAbs, StartsWithRoot, CanUseBaseDir : Boolean;
@ -1031,8 +1034,6 @@ begin
end; end;
procedure InitLazFileUtils; procedure InitLazFileUtils;
begin begin
{$ifndef WinCE} {$ifndef WinCE}