Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How to determine if a file is located on a remote machine in UNIX

Hi,

At work our application runs on SOLARIS, LINUX, and Windows. I have been tasked with determining on startup if the system is being run across the network.

On Windows, this is simple, get the current working directory and throw it into the function PathIsNetworkPath.

Such a thing does not exist on UNIX systems, as far as I can tell. I see that using the function stat(), I can retrieve the device number that the file resides on (along with a bunch of other info). However, I have been unable to determine where this device is located (locally or remotely), and I have not been able to find a function that given a device number, returns true if remote and false if local...or something similar.

How can I determine if a particular directory or file lives on a remote machine? I can not have any network protocol checks...meaning, the function must succeed for all network filesystem protocols, it can't work for NFS and not for SMB for instance.

Please help!

Thanks in advance!


Comments

  • nugentnugent Member Posts: 87
    can not use the device number (from the stat function) as this is not guaranteed to be the same across system reboots and i am not aware of any function that tells you if the device is local or remote, so you may need to do network protocol checking, based on the example code below.

    you could scan through fstab (or mtab if your system maintains these files) and use the data in the fstab structure. Use the vfstype member of the structure to guess if if is network or not, although the vfstype member can be any string!

    i assume that the vfstype is the same as the string passed to the mount command using the -t option. According to the man page the available options are:

    adfs
    affs
    autofs
    coda
    coherent
    cramfs
    devpts
    efs
    ext
    ext2
    ext3
    hfs
    hpfs
    iso9660
    jfs
    minix
    msdos
    ncpfs
    nfs
    nfs4
    ntfs
    proc
    qnx4
    ramfs
    reiserfs
    romfs
    smbfs
    sysv
    tmpfs
    udf
    ufs
    umsdos
    usbfs
    vfat
    xenix
    xfs
    xiafs


    but as far as i know only the following are network based file systems:
    coda
    ncpfs
    nfs
    nfs4
    smbfs

    here is example source of reading the information from fstab
    [code]
    #include
    #include

    int main()
    {
    struct fstab *fst;
    int fd = setfsent(); /* returns 0 on failure, so should error check this */

    while(1)
    {
    fst = getfsent(); /* returns NULL when no more entries */
    if(fst != NULL)
    {
    /* print some information */
    printf("File: %s
    ", fst->fs_file);
    printf("Spec: %s
    ", fst->fs_spec);
    printf("VFStype: %s

    ", fst->fs_vfstype);
    }
    else
    break;
    }

    endfsent();
    return 0;
    }
    [/code]

    the functions used here are not thread safe.


    ------
    nugent



Sign In or Register to comment.