Directory? versus File?

Q&A's, tips, howto's
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Directory? versus File?

Post by newdep »

Hello Lutz,

Someting funny, perhaps behaviour?
but maybe you can explain it... see below ->

;;; show all dirs in current dir
> (dolist (d (directory ".")) (if (directory? d) (println d)))
.
..
dir1
dir2
dir3
dir4
nil

;; show all files in current dir
> (dolist (d (directory ".")) (if (file? d) (println d)))
.
..
one
two
three
four
..
..
"lastfile"



;; now show all dirs 1 dir back (there are 10 dirs there!)
> (dolist (d (directory "../")) (if (directory? d) (println d)))
.
..
nil

;; or

> (dolist (d (directory "./../")) (if (directory? d) (println d)))
.
..
nil

;;; Now we do the same but with file?
> (dolist (d (directory "../")) (if (file? d) (println d)))
.
..
test1
test2
nil



I notice that 'directory? is linked to the inodes on the filesystem
so its not a problem in 'directory? because a normal
(directory? "/") returns -> true.... so thats oke...


;; here i try to display all dirs in the "root"
> (dolist (d (directory "/")) (if (directory? d) (println d)))
.
..
nil

;; now with file?
> (dolist (d (directory "/")) (if (file? d) (println d)))
.
..
test
nil


But im lost actualy... Because the last 'Directory? shown below
does not exist ;-) still it returns true.. So this implies
that 'directory does not look for the inode index..Or does it?

The "/" is not the "root" but 1 directory back...> (file? "/")
true
> (directory? "/")
true
> (directory? "../../")
true
> (directory? "../../../../../")
true
> (directory? "../../../../../../../../../")
true


Testing the above with 'dolist it could be a list behaviour??? ->

The "." is the current directory
The "./" is also the current directory
The "../../" is 2 directory's back, understoud by File? but not by Directory?

The "/." could be the root? but its not


Please help me out ;-)

Regards, Norman
-- (define? (Cornflakes))

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

Post by Lutz »

file? and directory? both use the 'C' library stat() call.

file? returns true whenever stat() does not return an erro, this includes links, directories, devices, sockets etc. , for links the file referenced is referred.

directory? checks first if its file? than it cheks for the directory flag in the statinfo: S_ISDIR

directory uses the standrad library opendir() and readdir() functions.

There is no further processing beyond using these functions. I suggest reading the man pages for stat(), opendir() and readdir() could give you an answer to your questions.

Lutz

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

Post by Lutz »

No I see what is going on:

when you do

(directory "/") you get all the node names in root. But when you do

(map file? (directory "/")) => you get mosty nil because when you check file? on the pure file names it check against the current directory! if you are not in root but somewhere else you get mostly nil except for "." and "..". Remember that (directory) returns the filenames without the directory path ;-)

Lutz

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

If 'file? would return true on a file that is readable
and 'directory? would return true on file-type directory
then they are both doing it difrently then the manual tells me ;-)

Using 'map (great function!) below gives a very strange output
of my root directory.. I cant explain the extra 'true because I should
actualy be having on most of them readability, thus 'true (using 'file?)...
(But i think the 'map function confuses it all by its return value..)

> (map file? (directory "/"))
(true true nil nil nil nil nil nil nil nil nil nil nil nil nil nil
nil nil nil nil nil nil nil nil nil nil nil nil true nil)

> (map directory? (directory "/"))
(true true nil nil nil nil nil nil nil nil nil nil nil nil nil nil
nil nil nil nil nil nil nil nil nil nil nil nil nil nil)


PS: How is the weather overthere?
I see some stormy pictures passing from Florida...

Regards,
Norman
-- (define? (Cornflakes))

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

Post by Lutz »

when you are doing:

(map file? (directory "/"))

you are not in the root directory, do a:

(change-dir "/")
(map file? (directory "/"))

and they all will be 'true', (directory "/") returns only the names without the paths.

Lutz

ps: fortunately I am out of the hurricane zone on the south-east coast, most of the damage is on the west coast and further north central Florida. where it cam out east again is about 100+ miles north of me.

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

;-)

> (change-dir "/")
true
> (map directory? (directory "/"))
(true true true true true true true true true true true true true
true true true true true nil nil nil nil nil nil true true true
true nil true)
> (map file? (directory "/"))
(true true true true true true true true true true true true true
true true true true true true true true true true true true true
true true true true)
>
-- (define? (Cornflakes))

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

Post by Lutz »

also the file does not have to be readable to be 'true' with file?, any node/permission will do.

Lutz

Locked