Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Programming
»
Operating Systems
»
macOS / Mac OS X
»
SOLVED: Detect if macOS file is readable
Free Pascal
Website
Downloads
Wiki
Documentation
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Documentation (RTL/FCL/LCL)
Bugtracker
CCR Bugs
IRC channel
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
How to: create DLL file f...
by
440bx
[
Today
at 12:42:21 pm]
Access violation when ope...
by
Чебурашка
[
Today
at 12:27:34 pm]
Database standards OR Am ...
by
egsuh
[
Today
at 12:02:06 pm]
Step-into the field sette...
by
Martin_fr
[
Today
at 11:31:48 am]
Access violation when re-...
by
Chris Osborne
[
Today
at 11:31:25 am]
AI, NLP and CAI: Text Gen...
by
Dzandaa
[
Today
at 11:03:26 am]
Who is Indy mattias?
by
jollytall
[
Today
at 11:01:45 am]
dwindows for Android
by
PierceNg
[
Today
at 10:54:44 am]
create system unit from s...
by
Laksen
[
Today
at 10:53:57 am]
how to get class and meth...
by
cdbc
[
Today
at 10:52:22 am]
[Solved] Find child contr...
by
Joanna
[
Today
at 09:53:43 am]
FpDebug unexpected Assemb...
by
Marc
[
Today
at 08:46:38 am]
Offical launch of the 1 B...
by
Handoko
[
Today
at 07:59:07 am]
Big Numbers Math
by
iLya2IK
[
Today
at 07:13:20 am]
Lazarus for Windows on aa...
by
dbannon
[
Today
at 07:02:55 am]
BUG REPORT: Unable to gen...
by
dbannon
[
Today
at 06:29:49 am]
what to do if my target M...
by
Laksen
[
Today
at 12:49:12 am]
For-in loop over constant...
by
ASerge
[
Today
at 12:36:09 am]
Converting a string/index...
by
TRon
[April 18, 2024, 11:56:37 pm]
exclude ALL packages from...
by
PascalDragon
[April 18, 2024, 11:33:29 pm]
Demoscene The Champs Crac...
by
KodeZwerg
[April 18, 2024, 11:29:50 pm]
Poll: Watches and Display...
by
440bx
[April 18, 2024, 10:55:14 pm]
Have anyone develop UEFI ...
by
PascalDragon
[April 18, 2024, 10:14:15 pm]
The weird Rewrite bug
by
PascalDragon
[April 18, 2024, 09:58:53 pm]
Assign (textfile) not com...
by
PascalDragon
[April 18, 2024, 09:51:50 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: SOLVED: Detect if macOS file is readable (Read 801 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]
[+]
[-]
function
IsReadable
(
fnm
:
string
)
:
boolean
;
//https://wiki.freepascal.org/macOS_Programming_Tips#Determining_if_a_file_is_readable
var
fs
:
TFileStream
;
b
:
byte
;
begin
result
:
=
false
;
if
not
fileexists
(
fnm
)
then
begin
//writeln('File does not exist: '+fnm);
exit
;
end
;
if
FSize
(
fnm
)
<
2
then
begin
//writeln('Empty file: '+fnm);
exit
;
end
;
{$IFDEF Darwin}
{$I-}
try
fs
:
=
TFileStream
.
Create
(
fnm
,
fmOpenRead
or
fmShareDenyWrite
)
;
except
//writeln('Unable to open (permissions): '+fnm);
exit
(
false
)
;
end
;
try
fs
.
Seek
(
0
,
soFromBeginning
)
;
fs
.
Read
(
b
,
1
)
;
//writeln(inttostr(b));
except
//writeln('Unable to read: '+fnm);
exit
(
false
)
;
end
;
{$ENDIF}
result
:
=
true
;
end
;
«
Last Edit: September 02, 2022, 08:31:21 pm by ChrisR
»
Logged
KodeZwerg
Hero Member
Posts: 2066
Fifty shades of code.
Re: Detect if macOS file is readable
«
Reply #1 on:
August 30, 2022, 02:40:02 pm »
You forgot to free the created object.
Logged
«
Last Edit:
Tomorrow
at 31:76:97 xm by
KodeZwerg
»
KodeZwerg
Hero Member
Posts: 2066
Fifty shades of code.
Re: Detect if macOS file is readable
«
Reply #2 on:
August 30, 2022, 03:25:02 pm »
tested :
Code: Pascal
[Select]
[+]
[-]
function
IsReadable
(
const
AFilename
:
string
)
:
Boolean
;
var
fs
:
TFileStream
;
b
:
Byte
;
begin
// whatever happen, it is wrong :-)
Result
:
=
False
;
try
// try connect to target
fs
:
=
TFileStream
.
Create
(
AFilename
,
fmOpenRead
or
fmShareDenyWrite
)
;
try
// ensure that what we want to read is available
if
(
fs
.
Size
>
=
SizeOf
(
Byte
)
)
then
begin
try
// turn compiler warning off
b
:
=
0
;
// jump to offset
fs
.
Position
:
=
0
;
// compare if the size we want to test is given back
Result
:
=
(
SizeOf
(
Byte
)
=
fs
.
Read
(
b
,
SizeOf
(
Byte
)
)
)
;
except
// something went wrong at reading
end
;
end
;
finally
// free the object
fs
.
Free
;
end
;
except
// something went wrong on accessing or in general
// file not found
// access error like forbid, no permission etc...
end
;
end
;
added explanation.
«
Last Edit: August 30, 2022, 04:55:29 pm by KodeZwerg
»
Logged
«
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!
Logged
KodeZwerg
Hero Member
Posts: 2066
Fifty shades of code.
Re: SOLVED: Detect if macOS file is readable
«
Reply #4 on:
September 03, 2022, 08:04:36 am »
Quote from: ChrisR on September 02, 2022, 08:32:33 pm
Thanks for the elegant code!
You are very welcome!
Logged
«
Last Edit:
Tomorrow
at 31:76:97 xm by
KodeZwerg
»
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Programming
»
Operating Systems
»
macOS / Mac OS X
»
SOLVED: Detect if macOS file is readable
TinyPortal
© 2005-2018