Mkn
Mkn

Reputation: 638

Command glob : type

Here my command :

foreach fic [glob -nocomplain -dir $dir -types {f d r} *] {
    set infofile [list [file tail $fic] [file mtime $fic] [file atime $fic]]
    # ...
}

Only I have an error : couldn't read directory "/Users/..." permission denied...
My solution is to add this command : file readable

foreach fic [glob -nocomplain -dir $dir -types {f d} *] {
    if {![file readable $fic]} continue
    set infofile [list [file tail $fic] [file mtime $fic] [file atime $fic]]
    # ...
}

I thought when I added the r -type this kind of error did not appear.
It’s a misunderstanding of the documentation ?

Upvotes: 0

Views: 330

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137627

Permissions on Windows are complex, to the point where you can only be really sure that you've got permission to read a file immediately after you've successfully opened it for reading. The indications from glob and file readable are not definitive. This is the case on other operating systems, and in any case there's a race condition: the user could change the permissions between checking with file readable and calling some other operation. Because of that, while you can use glob -type r, you should not rely on it. It simply can't be guaranteed to be correct.

The fix for this? Handle errors from calls properly.

foreach fic [glob -nocomplain -dir $dir -types {f d r} *] {
    try {
        # More efficient than calling [file mtime] and [file atime] separately
        file stat $fic data
    } on error {} {
        # Couldn't actually handle the file. Ignore
        continue
    }
    set infofile [list [file tail $fic] $data(mtime) $data(atime)]
    # ...
}

Upvotes: 1

Related Questions