Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2010-06

SetFileTime in Linux?

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

bobby

Hi @all,

Is there in Linux any function to set file time (created/modified)? I need it to set the file/directory time at unpacking ZIP archives. In Windows I use the following function:

function SetFileDateTime(FileName: UTF8string; DT: longint): boolean;
var
   hDir: THandle;
   ftCreation: TFiletime;
begin
   {$IFDEF windows}
   Result := False;
   hDir   := CreateFile(PChar(FileName), GENERIC_READ or GENERIC_WRITE,
     0, nil, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
   if hDir  INVALID_HANDLE_VALUE then
   begin
     try
       DosDateTimeToFileTime(LongRec(DT).Hi, LongRec(DT).Lo, ftCreation);
       Result := SetFileTime(hDir, @ftCreation, @ftCreation, @ftCreation);
     finally
       CloseHandle(hDir);
     end;
   end
   else
   {$ENDIF}
     Result := False;
end;

Reply by email

Andreas Schneider
to bobby
bobby wrote
Hi @all, Is there in Linux any function to set file time (created/modified)? I need it to set the file/directory time at unpacking ZIP archives.

As it happens, I had to use such a function for my patch engine ... all necessary functions and structs are in unit libc.
My code to use them is:

function SetFileTimesHelper(const FileName: string; const DateTime: TDateTime;
Times: TFileTimes): Boolean;
var
  FileTime: Integer;
  StatBuf: TStatBuf64;
  TimeBuf: utimbuf;
begin
  Result := False;
  FileTime := DateTimeToFileDate(DateTime);
  if lstat64(PChar(FileName), @statBuf) = 0 then
  begin
    TimeBuf.actime := StatBuf.st_atime;
    TimeBuf.modtime := StatBuf.st_mtime;
    case Times of
      ftLastAccess:
        TimeBuf.actime := FileTime;
      ftLastWrite:
        TimeBuf.modtime := FileTime;
    end;
    Result := utime(PChar(FileName), @TimeBuf) = 0;
  end;
end;

(As you see, there is no Creation Time as in Windows. Only Access and Modified.)

Best Regards,
Andreas.

Reply by email

Michael Van Canneyt
to Andreas Schneider

The libc unit is deprecated. Also, it only works on i386.

You can use the fpUtime function from the baseunix unit.

See
http://www.freepascal.org/docs-html/rtl/baseunix/fputime.html

Michael.

Andreas Schneider wrote 45 lines
Am Freitag 25 Juni 2010, 17:55:38 schrieb bobby: > Hi @all, > > Is there in Linux any function to set file time (created/modified)? > I need it to set the file/directory time at unpacking ZIP archives. As it happens, I had to use such a function for my patch engine ... all necessary functions and structs are in unit libc. My code to use them is: function SetFileTimesHelper(const FileName: string; const DateTime: TDateTime; Times: TFileTimes): Boolean; var FileTime: Integer; StatBuf: TStatBuf64; TimeBuf: utimbuf; begin Result := False; FileTime := DateTimeToFileDate(DateTime); if lstat64(PChar(FileName), @statBuf) = 0 then begin TimeBuf.actime := StatBuf.st_atime; TimeBuf.modtime := StatBuf.st_mtime; case Times of ftLastAccess: TimeBuf.actime := FileTime; ftLastWrite: TimeBuf.modtime := FileTime; end; Result := utime(PChar(FileName), @TimeBuf) = 0; end; end; (As you see, there is no Creation Time as in Windows. Only Access and Modified.) Best Regards, Andreas. -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email