Portable file accessibility queries

Started by rickyboy, November 05, 2007, 08:27:05 AM

Previous topic - Next topic

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

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



; 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


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

(getuid) => 501




Check if a file is a link


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


Get file permissions
; 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


(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