(* Tux Commander - UEngines - Basic engines (abstract, local) Copyright (C) 2007 Tomas Bzatek Check for updates on tuxcmd.sourceforge.net This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *) unit UEngines; interface uses glib2, gdk2, Classes, ULibc; const ERRException = -1; ERRNoAccess = -2; ERRCreateLink = -3; ERRCopyMove = -4; ERRRemove = -5; ERRMkDIr = -6; omRead = 0; omWrite = 1; omAppend = 2; ConfDefaultDirCreationMask = 755; type PDataItem = ^TDataItem; TDataItem = record FName: PChar; // ANSI FDisplayName: PChar; // always-valid UTF-8 LnkPointTo: PChar; // ANSI ColumnData: array[0..9] of PChar; Size: cuLongLong; PackedSize: Int64; UpDir: boolean; Mode, UID, GID: cuLong; IsDir, IsLnk, IsBlk, IsChr, IsFIFO, IsSock: boolean; Selected, IsDotFile, IsExecutable: boolean; atime, mtime, ctime: time_t; Icon: Pointer; ItemColor: PGdkColor; end; PDataItemSL = ^TDataItemSL; TDataItemSL = record DataItem: PDataItem; Stage1: boolean; Level: word; ADestination: PChar; ForceMove, IsOnRO: boolean; end; TEngineProgressFunc = function (Sender: Pointer; BytesDone: Int64): boolean; cdecl; // Return False to break the copy process TEngineErrorFunc = function (Sender: Pointer; ErrorType, ErrorNum: integer; FileName: string): boolean; cdecl; // Return TEngineFileDes = pointer; TPanelEngine = class private BreakProcessingType: integer; public ParentEngine: TPanelEngine; LastHighlightItem, SavePath: string; constructor Create; destructor Destroy; override; function GetListing(List: TList; const APath: string; AddDotFiles, FollowSymlinks, AddFullPath: boolean): integer; virtual; abstract; // Returns errorcode function GetFileInfo(const APath: string; FollowSymlinks, AddFullPath: boolean): PDataItem; virtual; abstract; function ChangeDir(const NewPath: string): integer; virtual; abstract; // Returns errorcode function GetPath: string; virtual; abstract; procedure SetPath(Value: string); virtual; abstract; function GetDirSize(const APath: string): Int64; virtual; abstract; // Returns size or 0 if fails procedure BreakProcessing(ProcessingKind: integer); virtual; abstract; // 1 = GetDirSize, 2 = GetListing function FileExists(const FileName: string; FollowSymlinks: boolean): boolean; virtual; abstract; function DirectoryExists(const FileName: string; FollowSymlinks: boolean): boolean; virtual; abstract; procedure GetFileSystemInfo(const APath: string; var FSSize, FSFree: Int64; var FSName: string); virtual; abstract; function IsOnROMedium(const FileName: string): boolean; virtual; abstract; function FileCanRun(const FileName: string): boolean; virtual; abstract; // Operations function MakeDir(const NewDir: string): integer; virtual; abstract; // Returns errorcode function Remove(const APath: string): integer; virtual; abstract; // Returns errorcode function MakeSymLink(const NewFileName, PointTo: string): integer; virtual; abstract; // Returns errorcode function Chmod(const FileName: string; Mode: cuLong): integer; virtual; abstract; // Returns errorcode function Chown(const FileName: string; UID, GID: cuLong): integer; virtual; abstract; // Returns errorcode function RenameFile(const SourceFile, DestFile: string): integer; virtual; abstract; // Returns errorcode function ChangeTimes(const APath: string; mtime, atime: time_t): integer; virtual; abstract; // Returns errorcode // Copy-related routines function GetBlockSize: guint32; virtual; abstract; procedure SetBlockSize(Value: guint32); virtual; abstract; function CopyFileIn(Sender: Pointer; const SourceFile, DestFile: string; ProgressFunc: TEngineProgressFunc; ErrorFunc: TEngineErrorFunc; Append: boolean): boolean; virtual; abstract; // returns True if file is successfully copied function CopyFileOut(Sender: Pointer; const SourceFile, DestFile: string; ProgressFunc: TEngineProgressFunc; ErrorFunc: TEngineErrorFunc; Append: boolean): boolean; virtual; abstract; // returns True if file is successfully copied function IsOnSameFS(const Path1, Path2: string; FollowSymlinks: boolean): boolean; virtual; abstract; function TwoSameFiles(const Path1, Path2: string; FollowSymlinks: boolean): boolean; virtual; abstract; // Separate file read/write routines, not supported on most backends function OpenFile(const APath: string; Mode: integer; var Error: integer): TEngineFileDes; virtual; abstract; // Returns filedescriptor function ReadFile(const FileDescriptor: TEngineFileDes; Buffer: Pointer; ABlockSize: integer; var Error: integer): integer; virtual; abstract; // Returns number of bytes read function WriteFile(const FileDescriptor: TEngineFileDes; Buffer: Pointer; BytesCount: integer; var Error: integer): integer; virtual; abstract; // Returns number of bytes written function CloseFile(const FileDescriptor: TEngineFileDes): integer; virtual; abstract; // Returns errorcode function FileSeek(const FileDescriptor: TEngineFileDes; const AbsoluteOffset: Int64; var Error: integer): Int64; virtual; abstract; // Returns errorcode published property Path: string read GetPath write SetPath; property BlockSize: guint32 read GetBlockSize write SetBlockSize; end; TLocalTreeEngine = class(TPanelEngine) private FPath: string; FBlockSize: guint32; public constructor Create; destructor Destroy; override; function GetListing(List: TList; const APath: string; AddDotFiles, FollowSymlinks, AddFullPath: boolean): integer; override; function GetFileInfo(const APath: string; FollowSymlinks, AddFullPath: boolean): PDataItem; override; function ChangeDir(const NewPath: string): integer; override; function GetPath: string; override; procedure SetPath(Value: string); override; function GetDirSize(const APath: string): Int64; override; procedure BreakProcessing(ProcessingKind: integer); override; function FileExists(const FileName: string; FollowSymlinks: boolean): boolean; override; function DirectoryExists(const FileName: string; FollowSymlinks: boolean): boolean; override; procedure GetFileSystemInfo(const APath: string; var FSSize, FSFree: Int64; var FSName: string); override; function IsOnROMedium(const FileName: string): boolean; override; function FileCanRun(const FileName: string): boolean; override; function MakeDir(const NewDir: string): integer; override; function Remove(const APath: string): integer; override; function MakeSymLink(const NewFileName, PointTo: string): integer; override; function Chmod(const FileName: string; Mode: cuLong): integer; override; function Chown(const FileName: string; UID, GID: cuLong): integer; override; function RenameFile(const SourceFile, DestFile: string): integer; override; function ChangeTimes(const APath: string; mtime, atime: time_t): integer; override; function GetBlockSize: guint32; override; procedure SetBlockSize(Value: guint32); override; function CopyFileIn(Sender: Pointer; const SourceFile, DestFile: string; ProgressFunc: TEngineProgressFunc; ErrorFunc: TEngineErrorFunc; Append: boolean): boolean; override; function CopyFileOut(Sender: Pointer; const SourceFile, DestFile: string; ProgressFunc: TEngineProgressFunc; ErrorFunc: TEngineErrorFunc; Append: boolean): boolean; override; function IsOnSameFS(const Path1, Path2: string; FollowSymlinks: boolean): boolean; override; function TwoSameFiles(const Path1, Path2: string; FollowSymlinks: boolean): boolean; override; function OpenFile(const APath: string; Mode: integer; var Error: integer): TEngineFileDes; override; function ReadFile(const FileDescriptor: TEngineFileDes; Buffer: Pointer; ABlockSize: integer; var Error: integer): integer; override; function WriteFile(const FileDescriptor: TEngineFileDes; Buffer: Pointer; BytesCount: integer; var Error: integer): integer; override; function CloseFile(const FileDescriptor: TEngineFileDes): integer; override; function FileSeek(const FileDescriptor: TEngineFileDes; const AbsoluteOffset: Int64; var Error: integer): Int64; override; // Local extra functions function CopyFile(Sender: Pointer; const SourceFile, DestFile: string; ProgressFunc: TEngineProgressFunc; ErrorFunc: TEngineErrorFunc; Append: boolean): boolean; published property Path; property BlockSize; end; procedure FreeDataItem(DataItem: PDataItemSL); overload; procedure FreeDataItem(DataItem: PDataItem); overload; function DuplicateDataItem(DataItem: PDataItem): PDataItem; overload; function DuplicateDataItem(DataItem: PDataItemSL): PDataItemSL; overload; implementation uses SysUtils, UCoreUtils, UGnome; (********************************************************************************************************************************) constructor TPanelEngine.Create; begin inherited Create; BreakProcessingType := 0; ParentEngine := nil; // By default it is a top-level engine (local) LastHighlightItem := ''; end; destructor TPanelEngine.Destroy; begin inherited Destroy; end; (********************************************************************************************************************************) constructor TLocalTreeEngine.Create; begin inherited Create; FPath := '/'; FBlockSize := 65536; end; destructor TLocalTreeEngine.Destroy; begin inherited Destroy; end; function TLocalTreeEngine.GetPath: string; begin Result := FPath; end; procedure TLocalTreeEngine.SetPath(Value: string); begin if Value <> FPath then begin FPath := Value; end; end; function TLocalTreeEngine.GetBlockSize: guint32; begin Result := FBlockSize; end; procedure TLocalTreeEngine.SetBlockSize(Value: guint32); begin if Value <> FBlockSize then begin FBlockSize := Value; end; end; function TLocalTreeEngine.GetListing(List: TList; const APath: string; AddDotFiles, FollowSymlinks, AddFullPath: boolean): integer; var Item: PDataItem; Handle: PDIR; DirEnt: PDirent64; Buf: PChar; begin Result := 0; try if libc_chdir(PChar(APath)) <> 0 then begin Result := errno; DebugMsg(['*** TLocalTreeEngine.GetListing(APath=', APath, '): chdir error: ', strerror(Result)]); Exit; end; Handle := opendir(PChar(APath)); if Handle = nil then begin DebugMsg(['*** TLocalTreeEngine.GetListing(APath=', APath, '): opendir() handle == NULL: ', strerror(errno)]); Result := ERRNoAccess; Exit; end; repeat DirEnt := readdir64(Handle); if (DirEnt <> nil) and (DirEnt^.d_name[0] <> #0) then begin Buf := PChar(@DirEnt^.d_name[0]); if (Buf <> '.') and (Buf <> '..') and (strlen(Buf) > 0) and (AddDotFiles or (Buf[0] <> '.')) then begin Item := GetFileInfo(IncludeTrailingPathDelimiter(APath) + string(Buf), FollowSymlinks, AddFullPath); List.Add(Item); end; end; until DirEnt = nil; // TODO: check errno? closedir(Handle); except on E: Exception do begin Result := ERRException; DebugMsg(['*** TLocalTreeEngine.GetListing(APath=', APath, ') -Exception: ', E.Message]); Exit; end; end; end; function TLocalTreeEngine.GetFileInfo(const APath: string; FollowSymlinks, AddFullPath: boolean): PDataItem; var Item: PDataItem; StatBuf: Pstat64; LnkBuf: array[0..65535] of char; i: integer; begin StatBuf := malloc(sizeof(Tstat64)); memset(StatBuf, 0, sizeof(Tstat64)); if lstat64(PChar(APath), StatBuf) <> 0 then begin DebugMsg(['*** TLocalTreeEngine.GetFileInfo(APath=', APath, '): Error reading file via lstat64: ', strerror(errno)]); libc_free(StatBuf); Result := nil; Exit; end; Item := malloc(sizeof(TDataItem)); memset(Item, 0, sizeof(TDataItem)); Item^.UpDir := False; Item^.LnkPointTo := nil; Item^.Selected := False; if AddFullPath then Item^.FName := strdup(PChar(APath)) else Item^.FName := strdup(PChar(ExtractFileName(APath))); Item^.FDisplayName := StrToUTF8(Item^.FName); Item^.Mode := StatBuf^.st_mode; Item^.IsDotFile := (Length(ExtractFileName(APath)) > 0) and (ExtractFileName(APath)[1] = '.'); Item^.IsExecutable := (StatBuf^.st_mode and S_IXUSR) = S_IXUSR; Item^.IsDir := __S_ISTYPE(StatBuf^.st_mode, __S_IFDIR); Item^.IsLnk := __S_ISTYPE(StatBuf^.st_mode, __S_IFLNK); Item^.IsBlk := __S_ISTYPE(StatBuf^.st_mode, __S_IFBLK); Item^.IsChr := __S_ISTYPE(StatBuf^.st_mode, __S_IFCHR); Item^.IsFIFO := __S_ISTYPE(StatBuf^.st_mode, __S_IFIFO); Item^.IsSock := __S_ISTYPE(StatBuf^.st_mode, __S_IFSOCK); Item^.mtime := StatBuf^.st_mtime; Item^.atime := StatBuf^.st_atime; Item^.ctime := StatBuf^.st_ctime; Item^.UID := StatBuf^.st_uid; Item^.GID := StatBuf^.st_gid; Item^.Size := StatBuf^.st_size; Item^.PackedSize := -1; libc_free(StatBuf); if Item^.IsLnk then begin i := readlink(PChar(APath), LnkBuf, sizeof(LnkBuf)); if i >= 0 then Item^.LnkPointTo := g_strdup(@LnkBuf[0]); if FollowSymlinks then begin StatBuf := malloc(sizeof(Tstat64)); memset(StatBuf, 0, sizeof(Tstat64)); if stat64(PChar(APath), StatBuf) = 0 then begin Item^.IsDir := __S_ISTYPE(StatBuf^.st_mode, __S_IFDIR); Item^.Mode := StatBuf^.st_mode; Item^.Size := StatBuf^.st_size; end; libc_free(StatBuf); end; end; Result := Item; end; function TLocalTreeEngine.ChangeDir(const NewPath: string): integer; var APath: string; Handle : PDIR; begin try APath := IncludeTrailingPathDelimiter(NewPath); if libc_chdir(PChar(APath)) <> 0 then begin Result := errno; Exit; end; Handle := opendir(PChar(APath)); if not Assigned(Handle) then begin Result := ERRNoAccess; Exit; end; { if not Assigned(readdir(Handle)) then begin Result := ERRNoAccess; Exit; end; } if closedir(Handle) <> 0 then begin Result := ERRNoAccess; Exit; end; Result := 0; except on E: Exception do begin Result := ERRException; DebugMsg(['*** TLocalTreeEngine.ChangeDir(APath=', APath, ') -Exception: ', E.Message]); Exit; end; end; end; (********************************************************************************************************************************) function TLocalTreeEngine.MakeDir(const NewDir: string): integer; begin // DebugMsg(['(II) TLocalTreeEngine.MakeDir: begin']); Result := __mkdir(PChar(NewDir), OctalToAttr(ConfDefaultDirCreationMask)); // DebugMsg(['(II) TLocalTreeEngine.MakeDir: Result = ', Result]); // if Result <> 0 then Result := errno; if Result <> 0 then try if Self.DirectoryExists(NewDir, False) or (g_mkdir_with_parents(PChar(NewDir), OctalToAttr(ConfDefaultDirCreationMask)) <> 0) {ForceDirectories(NewDir))} then Result := errno else Result := 0; except Result := -1; DebugMsg(['(II) TLocalTreeEngine.MakeDir: Exception']); end; // DebugMsg(['(II) TLocalTreeEngine.MakeDir: end']); end; (********************************************************************************************************************************) function TLocalTreeEngine.GetDirSize(const APath: string): Int64; function InternalGetDirSize(APath: string): Int64; var Handle: PDIR; DirEnt: PDirent64; StatBuf: Pstat64; Buf: PChar; begin Result := 0; try if BreakProcessingType = 1 then Exit; APath := IncludeTrailingPathDelimiter(APath); if libc_chdir(PChar(APath)) <> 0 then Exit; Handle := opendir(PChar(APath)); if not Assigned(Handle) then Exit; repeat DirEnt := readdir64(Handle); if DirEnt <> nil then begin Buf := PChar(@DirEnt^.d_name[0]); if (strlen(Buf) > 0) and (Buf <> '.') and (Buf <> '..') then begin StatBuf := malloc(sizeof(Tstat64)); memset(StatBuf, 0, sizeof(Tstat64)); if lstat64(Buf, StatBuf) = 0 then begin if __S_ISTYPE(StatBuf^.st_mode, __S_IFDIR) then begin Inc(Result, InternalGetDirSize(APath + string(Buf))); libc_chdir(PChar(APath)); end else Inc(Result, StatBuf^.st_size); end; libc_free(StatBuf); end; end; until DirEnt = nil; closedir(Handle); except on E: Exception do begin Result := 0; DebugMsg(['*** TLocalTreeEngine.GetDirSize(APath=', APath, ') -Exception: ', E.Message]); end; end; end; begin try BreakProcessingType := 0; Result := InternalGetDirSize(APath); finally BreakProcessingType := 0; end; end; (********************************************************************************************************************************) function TLocalTreeEngine.Remove(const APath: string): integer; begin Result := libc_remove(PChar(ExcludeTrailingPathDelimiter(APath))); if Result <> 0 then Result := errno; end; (********************************************************************************************************************************) function TLocalTreeEngine.CopyFileIn(Sender: Pointer; const SourceFile, DestFile: string; ProgressFunc: TEngineProgressFunc; ErrorFunc: TEngineErrorFunc; Append: boolean): boolean; begin Result := CopyFile(Sender, SourceFile, DestFile, ProgressFunc, ErrorFunc, Append); end; function TLocalTreeEngine.CopyFileOut(Sender: Pointer; const SourceFile, DestFile: string; ProgressFunc: TEngineProgressFunc; ErrorFunc: TEngineErrorFunc; Append: boolean): boolean; begin Result := CopyFile(Sender, SourceFile, DestFile, ProgressFunc, ErrorFunc, Append); end; function TLocalTreeEngine.CopyFile(Sender: Pointer; const SourceFile, DestFile: string; ProgressFunc: TEngineProgressFunc; ErrorFunc: TEngineErrorFunc; Append: boolean): boolean; var fsrc, fdest: PFILE; BytesDone, BytesRead: Int64; // offset: __off_t; // Used due to sendfile bug while copying from NTFS and some 2.6.x kernels function OldCopyRoutine: boolean; var Buffer: Pointer; BytesWritten: Int64; Res: boolean; begin Result := False; Res := True; try // DebugMsg(['*** Using old copy function due to bug in sendfile']); // WriteLn('x1'); Buffer := malloc(FBlockSize); // WriteLn('x2'); if Buffer = nil then begin ErrorFunc(Sender, 1, errno, SourceFile); // Memory allocation failed // libc_free(Buffer); Exit; end; memset(Buffer, 0, FBlockSize); // WriteLn('x3'); while feof(fsrc) = 0 do begin // WriteLn('x4'); BytesRead := fread(Buffer, 1, FBlockSize, fsrc); if (BytesRead < FBlockSize) and (feof(fsrc) = 0) then begin Res := ErrorFunc(Sender, 6, errno, SourceFile); // Cannot read from source file Break; end; // WriteLn('x5'); BytesWritten := fwrite(Buffer, 1, BytesRead, fdest); if BytesWritten < BytesRead then begin Res := ErrorFunc(Sender, 7, ferror(fdest), DestFile); // Cannot write to source file Break; end; Inc(BytesDone, BytesRead); // WriteLn('x6'); try if Assigned(ProgressFunc) and (not ProgressFunc(Sender, BytesDone)) then begin Res := False; Break; end; except on E: Exception do DebugMsg(['*** ProgressFunc ! Exception raised in TLocalTreeEngine.CopyFile.OldCopyRoutine(Sender=', DWord(Sender), ', SourceFile=', SourceFile, ', DestFile=', DestFile, '): (', E.ClassName, '): ', E.Message]); end; // WriteLn('x7'); end; // WriteLn('x8'); libc_free(Buffer); // WriteLn('x9'); Result := Res; except on E: Exception do DebugMsg(['*** Exception raised in TLocalTreeEngine.CopyFile.OldCopyRoutine(Sender=', DWord(Sender), ', SourceFile=', SourceFile, ', DestFile=', DestFile, '): (', E.ClassName, '): ', E.Message]); end; end; (* function NewCopyRoutine: boolean; var Res: boolean; StatBuf: Pstat64; begin try Res := True; repeat DebugMsg(['Copy(sendfile): offset = ', offset, ', BytesDone = ', BytesDone, ', ftell(fsrc) = ', ftell(fsrc)]); BytesRead := sendfile(fileno(fdest), fileno(fsrc), offset, FBlockSize); if BytesRead = -1 then begin if errno = EINVAL then begin Result := OldCopyRoutine; Exit; end else Res := ErrorFunc(Sender, 6, errno, SourceFile); // Cannot read from source file Break; end; Inc(BytesDone, BytesRead); if Assigned(ProgressFunc) and (not ProgressFunc(Sender, BytesDone)) then begin Res := False; Break; end; until BytesRead < FBlockSize; StatBuf := malloc(sizeof(Tstat64)); memset(StatBuf, 0, sizeof(Tstat64)); if lstat64(PChar(SourceFile), StatBuf) <> 0 then begin DebugMsg(['*** TLocalTreeEngine.CopyFile.NewCopyRoutine(Sender=', DWord(Sender), ', SourceFile=', SourceFile, ', DestFile=', DestFile, '): Error reading file via lstat64: ', strerror(errno)]); Res := ErrorFunc(Sender, 6, errno, SourceFile); end else if (BytesDone < StatBuf^.st_size) and Res then Res := ErrorFunc(Sender, 6, errno, SourceFile); libc_free(StatBuf); Result := Res; except on E: Exception do DebugMsg(['*** Exception raised in TLocalTreeEngine.CopyFile.NewCopyRoutine(Sender=', DWord(Sender), ', SourceFile=', SourceFile, ', DestFile=', DestFile, '): (', E.ClassName, '): ', E.Message]); end; end; *) begin Result := False; try fsrc := fopen64(PChar(SourceFile), 'r'); if fsrc = nil then begin ErrorFunc(Sender, 2, errno, SourceFile); // Cannot open source file Exit; end; if Append then fdest := fopen64(PChar(DestFile), 'a') else fdest := fopen64(PChar(DestFile), 'w'); if fdest = nil then begin fclose(fsrc); ErrorFunc(Sender, 3, errno, DestFile); // Cannot open target file Exit; end; BytesDone := 0; // offset := 0; Result := OldCopyRoutine; if fclose(fdest) <> 0 then begin fclose(fsrc); ErrorFunc(Sender, 4, errno, DestFile); // Cannot close target file Exit; end; if fclose(fsrc) <> 0 then begin ErrorFunc(Sender, 5, errno, SourceFile); // Cannot close source file Exit; end; except on E: Exception do DebugMsg(['*** Exception raised in TLocalTreeEngine.CopyFile(Sender=', DWord(Sender), ', SourceFile=', SourceFile, ', DestFile=', DestFile, '): (', E.ClassName, '): ', E.Message]); end; // DebugMsg(['(II) TLocalTreeEngine.CopyFile: finished']); end; (********************************************************************************************************************************) function TLocalTreeEngine.FileExists(const FileName: string; FollowSymlinks: boolean): boolean; var st: Pstat64; begin st := malloc(sizeof(Tstat64)); memset(st, 0, sizeof(Tstat64)); if not FollowSymlinks then Result := lstat64(PChar(FileName), st) = 0 else Result := stat64(PChar(FileName), st) = 0; libc_free(st); end; (********************************************************************************************************************************) function TLocalTreeEngine.DirectoryExists(const FileName: string; FollowSymlinks: boolean): boolean; var st: Pstat64; begin st := malloc(sizeof(Tstat64)); memset(st, 0, sizeof(Tstat64)); if not FollowSymlinks then Result := lstat64(PChar(FileName), st) = 0 else Result := stat64(PChar(FileName), st) = 0; Result := Result and S_ISDIR(st^.st_mode); libc_free(st); end; (********************************************************************************************************************************) function TLocalTreeEngine.MakeSymLink(const NewFileName, PointTo: string): integer; begin // DebugMsg(['TLocalTreeEngine.MakeSymLink(NewFileName = "', NewFileName, '", PointTo = "', PointTo, '"']); Result := symlink(PChar(PointTo), PChar(NewFileName)); if Result <> 0 then Result := errno; end; (********************************************************************************************************************************) function TLocalTreeEngine.Chmod(const FileName: string; Mode: cuLong): integer; begin Result := libc_chmod(PChar(FileName), Mode); if Result <> 0 then Result := errno; end; (********************************************************************************************************************************) function TLocalTreeEngine.Chown(const FileName: string; UID, GID: cuLong): integer; begin Result := libc_chown(PChar(FileName), UID, GID); if Result <> 0 then Result := errno; end; (********************************************************************************************************************************) procedure TLocalTreeEngine.BreakProcessing(ProcessingKind: integer); begin BreakProcessingType := ProcessingKind; end; (********************************************************************************************************************************) function TLocalTreeEngine.IsOnSameFS(const Path1, Path2: string; FollowSymlinks: boolean): boolean; var FStat1, FStat2: Pstat64; Res1, Res2: integer; begin // DebugMsg(['** TLocalTreeEngine.IsOnSameFS("', Path1, '", "', Path2, '")']); Result := False; // Default fallback result (forces copy + delete) FStat1 := malloc(sizeof(Tstat64)); FStat2 := malloc(sizeof(Tstat64)); memset(FStat1, 0, sizeof(Tstat64)); memset(FStat2, 0, sizeof(Tstat64)); if FollowSymlinks then Res1 := stat64(PChar(Path1), FStat1) else Res1 := lstat64(PChar(Path1), FStat1); if Res1 <> 0 then DebugMsg(['** TLocalTreeEngine.IsOnSameFS: stat(', Path1, ') error: ', strerror(errno)]); if FollowSymlinks then Res2 := stat64(PChar(Path2), FStat2) else Res2 := lstat64(PChar(Path2), FStat2); if Res2 <> 0 then DebugMsg(['** TLocalTreeEngine.IsOnSameFS: stat(', Path2, ') error: ', strerror(errno)]); if (Res1 = 0) and (Res2 = 0) then Result := FStat1^.st_dev = FStat2^.st_dev; libc_free(FStat1); libc_free(FStat2); // DebugMsg(['** TLocalTreeEngine.IsOnSameFS("', Path1, '", "', Path2, '") Result = ', Result]); end; (********************************************************************************************************************************) function TLocalTreeEngine.TwoSameFiles(const Path1, Path2: string; FollowSymlinks: boolean): boolean; var FStat1, FStat2: Pstat64; Res1, Res2: integer; begin Result := False; FStat1 := malloc(sizeof(Tstat64)); FStat2 := malloc(sizeof(Tstat64)); memset(FStat1, 0, sizeof(Tstat64)); memset(FStat2, 0, sizeof(Tstat64)); if FollowSymlinks then Res1 := stat64(PChar(Path1), FStat1) else Res1 := lstat64(PChar(Path1), FStat1); if Res1 <> 0 then DebugMsg(['** TLocalTreeEngine.TwoSameFiles: stat(', Path1, ') error: ', strerror(errno)]); if FollowSymlinks then Res2 := stat64(PChar(Path2), FStat2) else Res2 := lstat64(PChar(Path2), FStat2); if Res2 <> 0 then DebugMsg(['** TLocalTreeEngine.TwoSameFiles: stat(', Path2, ') error: ', strerror(errno)]); if (Res1 = 0) and (Res2 = 0) then Result := FStat1^.st_ino = FStat2^.st_ino; libc_free(FStat1); libc_free(FStat2); end; (********************************************************************************************************************************) function TLocalTreeEngine.RenameFile(const SourceFile, DestFile: string): integer; begin Result := libc_rename(PChar(SourceFile), PChar(DestFile)); if Result <> 0 then Result := errno; end; (********************************************************************************************************************************) function TLocalTreeEngine.ChangeTimes(const APath: string; mtime, atime: time_t): integer; var timebuf: Putimbuf; begin Result := errno; try timebuf := malloc(sizeof(Tutimbuf)); memset(timebuf, 0, sizeof(Tutimbuf)); timebuf^.actime := atime; timebuf^.modtime := mtime; Result := utime(PChar(APath), timebuf); if Result <> 0 then Result := errno; libc_free(timebuf); except on E: Exception do DebugMsg(['*** Exception raised in TLocalTreeEngine.ChangeTimes(APath=', APath, '): (', E.ClassName, '): ', E.Message]); end; end; (********************************************************************************************************************************) procedure TLocalTreeEngine.GetFileSystemInfo(const APath: string; var FSSize, FSFree: Int64; var FSName: string); var Stat: Pstatfs64; fd: PFILE; mntent: Pmntent; mntdev: PChar; FoundLength: integer; Buffer: array[0..31] of char; begin FSSize := 0; FSFree := 0; FSName := ''; try Stat := malloc(sizeof(Tstatfs64)); memset(Stat, 0, sizeof(Tstatfs64)); if statfs64(PChar(APath), Stat) <> 0 then Exit; FSSize := Stat^.f_bsize * Stat^.f_blocks; FSFree := Stat^.f_bsize * Stat^.f_bfree; fd := setmntent(_PATH_MOUNTED, 'r'); if fd = nil then Exit; // Get mount name FoundLength := 0; mntdev := nil; mntent := getmntent(fd); while mntent <> nil do begin if (Pos(mntent^.mnt_dir, APath) = 1) and (Length(String(mntent^.mnt_dir)) > FoundLength) then begin FoundLength := Length(String(mntent^.mnt_dir)); FSName := String(mntent^.mnt_dir); mntdev := mntent^.mnt_fsname; end; mntent := getmntent(fd); end; endmntent(fd); // if it is CD-ROM, read ISO9660 label if Stat^.f_type = $9660 then begin { ISOFS_SUPER_MAGIC } if Assigned(mntdev) and (mntdev <> '') then begin fd := fopen(mntdev, 'r'); if fd <> nil then begin if fseek(fd, 32808, SEEK_SET) = 0 then if fread(@Buffer[0], 1, 32, fd) <> 0 then FSName := Trim(String(Buffer)); fclose(fd); end; end; end; libc_free(Stat); except on E: Exception do DebugMsg(['*** Exception raised in TLocalTreeEngine.GetFileSystemInfo(APath=', APath, '): (', E.ClassName, '): ', E.Message]); end; end; (********************************************************************************************************************************) function TLocalTreeEngine.OpenFile(const APath: string; Mode: integer; var Error: integer): TEngineFileDes; var m: PChar; begin Error := 0; case Mode of omRead: m := 'r'; omWrite: m := 'w'; omAppend: m := 'a'; else m := 'r'; end; Result := fopen64(PChar(APath), m); if Result = nil then Error := errno; end; (********************************************************************************************************************************) function TLocalTreeEngine.ReadFile(const FileDescriptor: TEngineFileDes; Buffer: Pointer; ABlockSize: integer; var Error: integer): integer; begin Error := 0; Result := fread(Buffer, 1, ABlockSize, FileDescriptor); if (Result = 0) and (feof(FileDescriptor) = 0) then Error := errno; end; (********************************************************************************************************************************) function TLocalTreeEngine.WriteFile(const FileDescriptor: TEngineFileDes; Buffer: Pointer; BytesCount: integer; var Error: integer): integer; begin Error := 0; { Result := __write(fileno(FileDescriptor), Buffer^, BytesCount); if Result < BytesCount then Error := errno; } Result := fwrite(Buffer, 1, BytesCount, FileDescriptor); if Result < BytesCount then Error := ferror(FileDescriptor); end; (********************************************************************************************************************************) function TLocalTreeEngine.CloseFile(const FileDescriptor: TEngineFileDes): integer; begin Result := fclose(FileDescriptor); if Result <> 0 then Result := errno; end; (********************************************************************************************************************************) function TLocalTreeEngine.FileSeek(const FileDescriptor: TEngineFileDes; const AbsoluteOffset: Int64; var Error: integer): Int64; begin Error := 0; Result := fseeko64(FileDescriptor, AbsoluteOffset, SEEK_SET); if Result = -1 then Error := errno; end; (********************************************************************************************************************************) function TLocalTreeEngine.IsOnROMedium(const FileName: string): boolean; var Stat: Pstatfs64; begin Result := False; try Stat := malloc(sizeof(Tstatfs64)); memset(Stat, 0, sizeof(Tstatfs64)); if statfs64(PChar(FileName), Stat) = 0 then Result := (Stat^.f_type = $9660); { ISOFS_SUPER_MAGIC } libc_free(Stat); except on E: Exception do DebugMsg(['*** TLocalTreeEngine.IsOnROMedium(FileName=', FileName, ') -Exception: ', E.Message]); end; end; (********************************************************************************************************************************) function TLocalTreeEngine.FileCanRun(const FileName: string): boolean; begin Result := access(PChar(FileName), R_OK or X_OK) = 0; end; (********************************************************************************************************************************) (********************************************************************************************************************************) procedure FreeDataItem(DataItem: PDataItem); var i : integer; begin try if DataItem <> nil then begin with DataItem^ do begin if FName <> nil then libc_free(FName); if FDisplayName <> nil then libc_free(FDisplayName); if LnkPointTo <> nil then libc_free(LnkPointTo); for i := 0 to Length(ColumnData) - 1 do if ColumnData[i] <> nil then libc_free(ColumnData[i]); end; libc_free(DataItem); end; except on E: Exception do DebugMsg(['*** FreeDataItem: Exception: ', E.Message]); end; end; procedure FreeDataItem(DataItem: PDataItemSL); begin try if DataItem <> nil then begin with DataItem^ do begin if ADestination <> nil then libc_free(ADestination); FreeDataItem(DataItem); end; libc_free(DataItem); end; except on E: Exception do DebugMsg(['*** FreeDataItem: Exception: ', E.Message]); end; end; function DuplicateDataItem(DataItem: PDataItem): PDataItem; var NewDataItem: PDataItem; i: integer; begin NewDataItem := malloc(sizeof(TDataItem)); memcpy(NewDataItem, DataItem, sizeof(TDataItem)); NewDataItem^.FName := g_strdup(DataItem^.FName); NewDataItem^.FDisplayName := g_strdup(DataItem^.FDisplayName); NewDataItem^.LnkPointTo := g_strdup(DataItem^.LnkPointTo); for i := 0 to Length(DataItem^.ColumnData) - 1 do NewDataItem^.ColumnData[i] := g_strdup(DataItem^.ColumnData[i]); Result := NewDataItem; end; function DuplicateDataItem(DataItem: PDataItemSL): PDataItemSL; var NewDataItem: PDataItemSL; begin NewDataItem := malloc(sizeof(TDataItemSL)); memcpy(NewDataItem, DataItem, sizeof(TDataItemSL)); NewDataItem^.ADestination := g_strdup(DataItem^.ADestination); NewDataItem^.DataItem := DuplicateDataItem(DataItem^.DataItem); Result := NewDataItem; end; end.