Sign in Join

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

Mailing lists / fpc-devel / 2010-11

Question regarding FileSetDate implementation

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Sven Barth

Hello together!

As you might have noticed, I'm working on the NativeNT RTL again and discover some nice FPC_HAS_FEATURE_* and THandle problems on the way. Now I've found another one and I'd like to know how this should be fixed:

FileSetDate in SysUtils (the filename variant) returns a LongInt, but someone thought it was a good idea to return the (invalid) file handle if the FileOpen call fails. Thus I get a compile error, cause my handle type on NativeNT is a Pointer.

The documentation states that a negative value is returned in case of an error. Granted, INVALID_HANDLE_VALUE is negative, but that's no reason to assign the value of the handle there (what if some OS decides that an invalid handle is 42 instead of LongWord(-1)?)

==== source begin ====

   Function FileSetDate (Const FileName : String;Age : Longint) : Longint;
   Var
     fd : THandle;
   begin
     { at least windows requires fmOpenWrite here }
     fd:=FileOpen(FileName,fmOpenWrite);
     If (FdfeInvalidHandle) then
       try
         Result:=FileSetDate(fd,Age);
       finally
         FileClose(fd);
       end
     else
       Result:=fd; // this is the problematic place... should we just
return -1 here?
   end;

==== source end ====

Regards,
Sven

Reply by email

Sergei Gorelkin
to Sven Barth

Sven Barth пишет:

Show quoted text 16 lines
Hello together! As you might have noticed, I'm working on the NativeNT RTL again and discover some nice FPC_HAS_FEATURE_* and THandle problems on the way. Now I've found another one and I'd like to know how this should be fixed: FileSetDate in SysUtils (the filename variant) returns a LongInt, but someone thought it was a good idea to return the (invalid) file handle if the FileOpen call fails. Thus I get a compile error, cause my handle type on NativeNT is a Pointer. The documentation states that a negative value is returned in case of an error. Granted, INVALID_HANDLE_VALUE is negative, but that's no reason to assign the value of the handle there (what if some OS decides that an invalid handle is 42 instead of LongWord(-1)?)

According to Delphi documentation, it should return zero on success, non-zero error code otherwise. Windows handle-based implementation conforms to that. The proper fix would be returning value of GetLastOSError, but it may be not available for all platforms.

Regards,
Sergei

Reply by email

Sergei Gorelkin
to Sergei Gorelkin

Sergei Gorelkin пишет:

Show quoted text 6 lines
According to Delphi documentation, it should return zero on success, non-zero error code otherwise. Windows handle-based implementation conforms to that. The proper fix would be returning value of GetLastOSError, but it may be not available for all platforms.

Fixed in r16318.

Sergei

Reply by email

Sven Barth
to Sergei Gorelkin
Sergei Gorelkin wrote 9 lines
Sergei Gorelkin пишет: > According to Delphi documentation, it should return zero on success, > non-zero error code otherwise. > Windows handle-based implementation conforms to that. The proper fix > would be returning value of GetLastOSError, but it may be not > available for all platforms. > Fixed in r16318.

Thank you for that quick fix :)

Regards,
Sven

Reply by email