Recent

Author Topic: SOLVED: Detect if macOS file is readable  (Read 797 times)

ChrisR

  • Full Member
  • ***
  • Posts: 247
SOLVED: Detect if macOS file is readable
« on: August 29, 2022, 03:40:32 pm »
For GUI applications, a file may be readable based on file permissions, but not readable as it is outside the applications sand box. This is described hear with detection code:
  https://wiki.freepascal.org/macOS_Programming_Tips#Determining_if_a_file_is_readable

However, this detection fails if the file path is longer than 255 bytes
  https://github.com/neurolabusc/surf-ice/issues/35

Are there any comments regarding my suggested code, which handles long file names:

Code: Pascal  [Select][+][-]
  1. function IsReadable (fnm: string): boolean;
  2. //https://wiki.freepascal.org/macOS_Programming_Tips#Determining_if_a_file_is_readable
  3. var
  4.   fs : TFileStream;  
  5.   b: byte;
  6. begin
  7.   result := false;
  8.   if not fileexists(fnm) then begin
  9.     //writeln('File does not exist: '+fnm);
  10.     exit;
  11.   end;
  12.   if FSize(fnm) < 2 then begin
  13.     //writeln('Empty file: '+fnm);
  14.     exit;
  15.   end;
  16.   {$IFDEF Darwin}
  17.   {$I-}
  18.   try
  19.   fs := TFileStream.Create (fnm, fmOpenRead or fmShareDenyWrite);
  20.   except
  21.     //writeln('Unable to open (permissions): '+fnm);
  22.     exit(false);
  23.   end;
  24.   try
  25.     fs.Seek(0, soFromBeginning);
  26.     fs.Read(b, 1);
  27.     //writeln(inttostr(b));
  28.   except
  29.     //writeln('Unable to read: '+fnm);
  30.     exit(false);
  31.   end;
  32.   {$ENDIF}
  33.   result := true;
  34. end;
  35.  
« Last Edit: September 02, 2022, 08:31:21 pm by ChrisR »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2048
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Detect if macOS file is readable
« Reply #1 on: August 30, 2022, 02:40:02 pm »
You forgot to free the created object.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2048
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Detect if macOS file is readable
« Reply #2 on: August 30, 2022, 03:25:02 pm »
tested :
Code: Pascal  [Select][+][-]
  1. function IsReadable(const AFilename: string): Boolean;
  2. var
  3.   fs: TFileStream;
  4.   b : Byte;
  5. begin
  6.   // whatever happen, it is wrong :-)
  7.   Result := False;
  8.   try
  9.     // try connect to target
  10.     fs := TFileStream.Create(AFilename, fmOpenRead or fmShareDenyWrite);
  11.     try
  12.       // ensure that what we want to read is available
  13.       if (fs.Size >= SizeOf(Byte)) then
  14.         begin
  15.           try
  16.             // turn compiler warning off
  17.             b := 0;
  18.             // jump to offset
  19.             fs.Position := 0;
  20.             // compare if the size we want to test is given back
  21.             Result := (SizeOf(Byte) = fs.Read(b, SizeOf(Byte)));
  22.           except
  23.             // something went wrong at reading
  24.           end;
  25.         end;
  26.     finally
  27.       // free the object
  28.       fs.Free;
  29.     end;
  30.   except
  31.     // something went wrong on accessing or in general
  32.     // file not found
  33.     // access error like forbid, no permission etc...
  34.   end;
  35. end;
  36.  

added explanation.
« Last Edit: August 30, 2022, 04:55:29 pm by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

ChrisR

  • Full Member
  • ***
  • Posts: 247
Re: SOLVED: Detect if macOS file is readable
« Reply #3 on: September 02, 2022, 08:32:33 pm »
Thanks for the elegant code!

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2048
  • Fifty shades of code.
    • Delphi & FreePascal
Re: SOLVED: Detect if macOS file is readable
« Reply #4 on: September 03, 2022, 08:04:36 am »
Thanks for the elegant code!
You are very welcome!
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

 

TinyPortal © 2005-2018