【问题标题】:*OSX/UNIX* Testing if a file is on a locally mounted drive vs. AFP/SMB mounted?*OSX/UNIX* 测试文件是否在本地安装的驱动器上与 AFP/SMB 安装?
【发布时间】:2014-11-10 22:04:59
【问题描述】:

我正在开发一个可以自动对数据集进行多机处理的应用程序。

从主计算机 (192.168.1.2) 中,用户选择要处理的文件/文件夹。
然后与 LAN 网络上的所有从属计算机共享确切的文件路径。

只要文件在本地驱动器上,一切都很好,共享文件路径如下所示:

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

但如果用户选择存储在 AFP 安装驱动器(例如 NAS)上的文件,我将无法检索完整的文件路径。


所以我可以得到挂载的文件路径:

/Volumes/NAS/Documents/file.zip

我可以获得已安装驱动器的列表:

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)  

我正在寻求帮助以将这些信息解析为:

  • 测试文件是否被 AFP 挂载
  • 如果为真,则提取 URL (afp://NAS._afpovertcp._tcp.local/NAS/Documents/file.zip)

有什么线索吗?

+加分点:检索网络卷的IP地址!

【问题讨论】:

  • 可能是您自己的版本,“stat -f --format=%t /path”可能是一个选项。它提供了一种文件系统类型。您也许可以使用它来确定位置,而无需查看已安装的驱动器列表。
  • 谢谢!我能用stat 做的最好的事情就是提取神秘的“设备号”。示例:mbp:~ myself$ stat -f "%d" /Volumes/NAS 给了我771751941。我仍然想不出一种方法来逆向工程卷+ AFP 地址:(

标签: macos bash unix applescript filepath


【解决方案1】:

基于此link

您应该能够从以下(未经测试)中解决一些问题。

#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;
}

大概,如果它不是本地文件系统,它就不会是 HFS。

【讨论】:

  • 不幸的是,它使用的是 Xcode 库,它在标准客户端机器上不可用。 (我现在甚至无法测试,因为它没有安装在我的笔记本电脑上,明天再试)
  • 无论如何都试试吧,这是一个常见的低级例程,所以应该在那里。该链接指向作为 xcode 文档一部分的文档。默认情况下,该功能应该仍然存在。
  • 编译回声:9: warning: format ‘%ld’ expects type ‘long int’, but argument 2 has type ‘uint32_t’。无论输入是什么,运行结果都是filesystem typeid: 0 filesystem type: 。 (无法调试,因为我必须承认我是 C 中的菜鸟)
  • 将 %ld 更改为 %d 以修复警告。我不明白你最后的评论。
  • 没有更多警告,但它不起作用:filesystem typeid: 0 filesystem type: 一直。
【解决方案2】:

问题解决了!

我设法使用df 提取了挂载点,然后对照以mount 为源的AFP 挂载卷列表对其进行了测试。使用grepawk 清理和管理整个事情。
是一个很好的学习练习。

所以这允许:
- 测试文件是本地文件还是 AFP 挂载(远程)
- 相应地提供文件路径

#!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

【讨论】:

    猜你喜欢
    • 2014-11-02
    • 1970-01-01
    • 2019-05-09
    • 2016-10-26
    • 1970-01-01
    • 2016-09-01
    • 2015-06-02
    • 2020-04-25
    • 1970-01-01
    相关资源
    最近更新 更多