Malus has asked for the wisdom of the Perl Monks concerning the following question:

Most knowledgeable Monks,

I am trying to figure out whether a file is local or remote. I don't even need to know where it is coming from. However, I am seeking a pure perl solution (without calling external commands like df) and without installing another module.

Any help or comments are greatly appreciated.

Thanks,
Malus

Replies are listed 'Best First'.
Re: file remote or local
by jbrugger (Parson) on Jan 23, 2006 at 20:55 UTC
    Huh?
    I don't see what you want. How is the file presented to the program? as an argument? by a list of files in any folder the program reads?
    Next, why would you care to know?, how is it going to help you?
    Perhaps it's an idea to give some more information, so one of us can help you with a possible solution you're looking for.
    On this moment, i can only say, look at the filename, if it's something like //foo/bar it's probably 'remote', but that would not be taken for granted on a linux system where a misspelled // is just / (the root).

    Next, i'd be careful with files that i don't even know where they come from...

    Update
    Ah i understand now. Indeed as stated below use stat.
    as stated there, you can see:
    0 dev device number of filesystem 1 ino inode number 2 mode file mode (type and permissions) 3 nlink number of (hard) links to the file 4 uid numeric user ID of file's owner 5 gid numeric group ID of file's owner 6 rdev the device identifier (special files only) 7 size total size of file, in bytes 8 atime last access time in seconds since the epoch 9 mtime last modify time in seconds since the epoch 10 ctime inode change time in seconds since the epoch (*) 11 blksize preferred block size for file system I/O 12 blocks actual number of blocks allocated
    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
      It sounds like what he wants is a way to look at a file and determine whether or not that file is a local file or one that is NFS mounted. Since he made reference to "df", I would assume that he's using a *NIX OS.
Re: file remote or local
by explorer (Chaplain) on Jan 23, 2006 at 22:07 UTC
    Try
    $inode = (stat("/file/to/check"))[1]; print $inode;
    The NFS files have big i-node numbers regarding local files...
      You really want to check dev which is  $dev = (stat("/file/to/check"))[0];. Compare the value with the value for the remote filesystem's mountpoint.
        And you can get a list of mountpoints from /etc/mtab, figure out their type from the 3rd column, stat the filesystem root for the ones you determine are remote, then see if the device number from the above stat call is one of the remote ones.
        On our solaris systems local filesystems have a device like 800000xh and remote like 45c14xxh. I've not checked all and your experience may vary.

        x in examples is varying data.