Portable file accessibility queries

For the Compleat Fan
Locked
rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Portable file accessibility queries

Post by rickyboy »

Hi there everyone!

What is The Way, or your preferred way, of querying the file system for information about the accessibility of a file? I was looking for a portable way to do this also.

For instance, a function file-readable? which given a filename returns true or false depending on if the current process can read the file or not. I was also looking for a file-writable? function which would be defined similarly.

Has anyone made any progress on this? Sorry if we've talked about this before. Thanks for any help.

--Rick
(λx. x x) (λx. x x)

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

here some random notes for this topic (some only applicable to UNIX/Mac OSX)

Code: Select all

; Access rights are in the mode byte (the second)

(file-info "newlisp.c") 
=> (131038 33188 0 501 501 1194273030 1193797570 1194035314)


Get the current uid

Code: Select all

(import "/usr/lib/libc.dylib" "getuid")

(getuid) => 501

Check if a file is a link

Code: Select all

(define (link? fname) 
    (= 0x2000 (& (file-info fname 1) 0x2000)))
Get file permissions

Code: Select all

; masking right bit and checking the user-id and group-id
; could be used to define 'file-readable?' etc

(& 0xFFF ((file-info "newlisp.c") 1)) => 420

; 420 is 644 in octal which would be:

((parse ((exec "ls -l newlisp.c") 0) " ") 0) => "-rw-r--r--"
Change file permissions

Code: Select all

(import "/usr/lib/libc.dylib" "chmod")

(write-file "junk.txt" "just-testing")

(chmod "junk.txt" 0755)

> !ls -l junk.txt
-rwxr-xr-x  1 lutz  lutz  12 Nov  5 12:01 junk.txt
> 
Lutz

Locked