jameslooi
jameslooi

Reputation: 1

TCL help: How to check for unmount/bad disks before read file

I need help here. I have list of directory/file path and my program will read through every one. Somehow one of the directory is unmount/bad disks and cause my program hang over there when I'm try to open the file using command below.

catch {set directory_fid [open $filePath r]}

So, how can I check the directory status before I'm reading/open the file? I want to skip that file if no response for certain time and continue to read next file.

*file isdir $dir is not working as well

*There is no response when i'm using ls -dir in Unix also.

Upvotes: 0

Views: 180

Answers (1)

Brad Lanam
Brad Lanam

Reputation: 5723

Before you start down this path, I would review your requirements and see if there's any easier way to handle this. It would be better to fix the mounts so that they don't cause a hang condition if an access attempt is made.

The main problem is that for the directories you are checking, you need to know the corresponding mount point. If you don't know the mount point, it's hard to tell whether the directory you want to check will cause any hangs when you try to access it.

First, you would have to parse /etc/fstab and get a list of possible filesystem mount points (Assumption, Linux system -- if not Linux, there will be an equivalent file).

Second, to see what is currently mounted you need the di Tcl extension (wiki page) (or main page w/download links). (*). Using this extension, you can get a list of mounted filesystems.

# the load only needs to be done once...
set ext [info sharedlibextension]
set lfn [file normalize [file join [file dirname [info script]] diskspace$ext]]
load $lfn
# there are various options that can be passed to the `diskspace`
# command that will change which filesystems are listed.
set fsdata [diskspace -f {}] 
set fslist [dict keys $fsdata]

Now you have a list of possible mount points, and you know which are mounted.

Third, you need to figure out which mount point corresponds to the directory you want to check. For example, if you have:

/user/bll/source/stuff.c

You need to check for /user/bll/source, then /user/bll, then /user, then / as possible mount points.

There's a huge assumption here that the file or any of its parent directories are not symlinked to another place.

Once you determine the probable mount point, you can check if it is mounted:

if { $mountpoint in $fslist } {
  ...
} else {
  # better skip this one, the probable mount point is not mounted.
}

As you can see, this is a lot of work. It's fragile. Better to fix the mounts so they don't hang.

(*) I wrote di and the di Tcl extension. This is a portable solution. You can of course use exec to run df or mount, but there are other issues (parsing, portability, determining which filesystems to use) if you use the more manual method.

Upvotes: 1

Related Questions