简体   繁体   中英

*OSX/UNIX* Testing if a file is on a locally mounted drive vs. AFP/SMB mounted?

I'm working on an app that automates multi-machine processing of a dataset.

From a master computer (192.168.1.2), the user picks a file/folder to be processed.
The exact filepath is then shared with all the slave computers on a LAN network.

As long as the file is on a local drive, everything is fine and the shared filepath looks like :

afp://192.168.1.2/Volumes/LOCAL_DRIVE/Projects/file.zip

But if the user picks a file stored on an AFP mounted drive (a NAS for example), I'm unable to retrieve a complete filepath.


So for I'm able to get the mounted filepath :

/Volumes/NAS/Documents/file.zip

And I can get a list of mounted drives :

mbp:~ myself$ mount
/dev/disk0s2 on / (hfs, local, journaled)
devfs on /dev (devfs, local, nobrowse)
/dev/disk1s2 on /Volumes/LOCAL_DRIVE (hfs, local, journaled)
map -hosts on /net (autofs, nosuid, automounted, nobrowse)
map auto_home on /home (autofs, automounted, nobrowse)
localhost:/ndtfxrIYDV1dU5kiwHMwAy on /Volumes/MobileBackups (mtmfs, nosuid, read-only, nobrowse)
//AdminNas@NAS._afpovertcp._tcp.local/NAS on /Volumes/NAS (afpfs, nodev, nosuid, mounted by myself)
//AdminUser@Remote_MacPro._afpovertcp._tcp.local/DATA on /Volumes/DATA (afpfs, nodev, nosuid, mounted by myself)  

I'm seeking help to parse theses infos to :

  • test if a file is AFP-mounted or not
  • if true, extract an URL (afp://NAS._afpovertcp._tcp.local/NAS/Documents/file.zip)

Any clues ?

+Bonus point : retrieve the IP adress of the network volume !

Based on this link

You should be able to work something out from the following (untested).

#include <sys/param.h>
#include <sys/mount.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
  struct statfs buf;
  if (statvfs("/tmp", &buf) == 0){
    printf("filesystem typeid: %d\n", buf.f_type);
    printf("filesystem type: %s\n", buf.f_fstypename);
  }
  return 0;
}

Presumably, if it's not a local filesystem it won't be HFS.

Problem solved !

I managed to extract the mount point using df , then tested it against a list of AFP mounted volumes sourced with mount . The whole thing being cleaned and managed using grep and awk .
Was a good learning exercise.

So this allows to :
- test if the file is local or AFP-mounted (remote)
- deliver a filepath accordingly

#!bin/bash

## Input as arguments, only one file or folder, with absolute filepath including "/Volumes/..."


## Get a nice $AFP_list of AFP-mounted volumes
AFP_list=$(mount |grep "afpfs" |awk {'print $1'})

## Get $base_volume of our input file
base_volume=$(df -PH "$1" |grep "%" |awk {'print $1'})


## Check if $base_volume is part of $AFP_list
if grep -q "$base_volume" <(echo "$AFP_list"); then
## If TRUE, deliver the network volume filepath
    echo "afp:$base_volume"
## If FALSE, deliver the original filepath
else
    echo "$1"
fi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM