mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-04-20 11:59:26 +02:00
LCL: moved ReadAllLinks to LazUtils
git-svn-id: trunk@44577 -
This commit is contained in:
parent
13b4f9aa2d
commit
877c4d50b9
@ -390,11 +390,15 @@ begin
|
||||
Result := LazFileUtils.FileIsWritable(AFilename);
|
||||
end;
|
||||
|
||||
function ReadAllLinks(const Filename: string; ExceptionOnError: boolean
|
||||
): string;
|
||||
begin
|
||||
Result:=LazFileUtils.ReadAllLinks(Filename,ExceptionOnError);
|
||||
end;
|
||||
|
||||
function TryReadAllLinks(const Filename: string): string;
|
||||
begin
|
||||
Result:=ReadAllLinks(Filename,false);
|
||||
if Result='' then
|
||||
Result:=Filename;
|
||||
Result:=LazFileUtils.TryReadAllLinks(Filename);
|
||||
end;
|
||||
|
||||
{------------------------------------------------------------------------------
|
||||
|
@ -67,9 +67,8 @@ function FileIsSymlink(const AFilename: string): boolean; inline;
|
||||
function FileIsHardLink(const AFilename: string): boolean; inline;
|
||||
function FileSize(const Filename: string): int64; overload; inline;
|
||||
function GetFileDescription(const AFilename: string): string; inline;
|
||||
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 ReadAllLinks(const Filename: string; ExceptionOnError: boolean): string; inline;
|
||||
function TryReadAllLinks(const Filename: string): string; inline;
|
||||
|
||||
// directories
|
||||
function DirPathExists(const FileName: String): Boolean; inline;
|
||||
@ -262,7 +261,7 @@ uses
|
||||
{$IFDEF windows}
|
||||
Windows;
|
||||
{$ELSE}
|
||||
Unix, BaseUnix;
|
||||
Unix;
|
||||
{$ENDIF}
|
||||
|
||||
{$I fileutil.inc}
|
||||
|
@ -103,7 +103,9 @@ Function FileCreateUtf8(Const FileName : String; ShareMode : Integer; Rights : C
|
||||
|
||||
function FileSizeUtf8(const Filename: string): int64;
|
||||
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 GetAppConfigFileUTF8(Global: Boolean; SubDir: boolean = false;
|
||||
@ -984,6 +986,12 @@ begin
|
||||
Result := DoForceDirectories(GetForcedPathDelims(Dir));
|
||||
end;
|
||||
|
||||
function TryReadAllLinks(const Filename: string): string;
|
||||
begin
|
||||
Result:=ReadAllLinks(Filename,false);
|
||||
if Result='' then
|
||||
Result:=Filename;
|
||||
end;
|
||||
|
||||
procedure InvalidateFileStateCache(const Filename: string);
|
||||
begin
|
||||
|
@ -1,61 +1,6 @@
|
||||
{%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;
|
||||
begin
|
||||
Result:=SysToUTF8(SysUtils.ExtractShortPathName(UTF8ToSys(FileName)));
|
||||
|
@ -68,6 +68,59 @@ begin
|
||||
Result := st.st_size;
|
||||
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;
|
||||
begin
|
||||
Result:=SysUtils.CreateDir(UTF8ToSys(NewDir));
|
||||
|
@ -1,20 +1,5 @@
|
||||
{%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;
|
||||
var
|
||||
lPathSize: DWORD;
|
||||
|
@ -139,6 +139,13 @@ begin
|
||||
Result := _GetAppConfigFileUTF8(Global, SubDir, CreateDir);
|
||||
end;
|
||||
|
||||
function ReadAllLinks(const Filename: string;
|
||||
ExceptionOnError: boolean): string;
|
||||
begin
|
||||
// not supported under Windows
|
||||
Result:=Filename;
|
||||
end;
|
||||
|
||||
// ******** Start of AnsiString specific implementations ************
|
||||
|
||||
{$ifndef WinCE}
|
||||
@ -734,16 +741,12 @@ end;
|
||||
// ******** End of WideString specific implementations ************
|
||||
|
||||
|
||||
|
||||
|
||||
function FilenameIsAbsolute(const TheFilename: string):boolean;
|
||||
begin
|
||||
Result:=FilenameIsWinAbsolute(TheFilename);
|
||||
end;
|
||||
|
||||
|
||||
|
||||
|
||||
function ExpandFileNameUtf8(const FileName: string; {const} BaseDir: String = ''): String;
|
||||
var
|
||||
IsAbs, StartsWithRoot, CanUseBaseDir : Boolean;
|
||||
@ -1031,8 +1034,6 @@ begin
|
||||
end;
|
||||
|
||||
|
||||
|
||||
|
||||
procedure InitLazFileUtils;
|
||||
begin
|
||||
{$ifndef WinCE}
|
||||
|
Loading…
Reference in New Issue
Block a user