【问题标题】:How to mount a directory via afp using mount(2)?如何使用 mount(2) 通过 afp 挂载目录?
【发布时间】:2017-10-27 19:08:48
【问题描述】:

我想挂载一个位于我网络上另一台主机上的目录。到目前为止,我已经通过afp 成功地做到了这一点,通过系统调用使用mount(8),如下所示:

std::string syscmd = "mount -v -t afp -r \"afp://user:password@host/dir\" \"/tmp/foo/bar\"";
FILE *fd;
if(!(fd = popen(syscmd.c_str(), "r"))) {
    std::cout << "oops. popen() failed." << std::endl;
    exit(1);
}    

但我想直接使用函数调用进行挂载,而不需要使用popen() 调用 shell 的额外开销。我不知道如何使用具有此签名的mount(2) 来执行此操作:

 int mount(const char *type, const char *dir, int flags, void *data);

data 应该是什么?手册页没有详细解释这一点。例如,它说:

Data 是指向包含特定类型的结构的指针 挂载的论据。这些参数结构的格式是 每个文件系统的手册页中都有描述。

它所指的手册页在哪里?我还缺少其他一些文档吗?谁能指出我一个简单的工作示例来说明 mount(2) 在 afp 上的使用?有没有更好的方法来做到这一点?

【问题讨论】:

  • 我在任何地方都找不到它的手册页。我的猜测是它只需要一个指向 URL 的指针。
  • 也许在启用系统调用跟踪的情况下运行 mount_afp 以查看它如何调用 mount()
  • @Barmar 我尝试了char data[]="afp://user:password@host/dir"; mount("hfs","/tmp/foo/",MNT_RDONLY,data);,但这给出了错误 14(“错误地址”)。我尝试使用呼叫跟踪 (dtruss) 运行 mount_afp,但在数千行输出中找不到任何有用的信息。

标签: c++ macos mount afp


【解决方案1】:

您有理由要求通过mount(2) 完成吗?

Apple 提供了来自 NetFS 框架的 NetFSMountURLSync() 函数来挂载网络文件系统。可悲的是,唯一的文档是头文件,所以我无法链接到它,但这是相关声明:

/*
 * Given a URL that refers to a file server, connect to that server
 * and mount stuff.
 *
 * If the URL just specifies a server and you can't just mount the
 * "root directory" of the server, the user will be prompted with
 * a window to let them select one or more items to mount from that
 * server, otherwise whatever item the URL specifies to mount will
 * be mounted.
 *
 * If the mountpath is provided it will be used as the mount point.
 * If the mountpath is set to NULL, a default mount point will be used.
 *
 * If the user and passwd are set, they will override any user name
 * or password that may be set in the URL. These calls go through the NetAuth agent.
 * If the URL doesn't specify a password, and one is needed, the
 * user will be prompted with a window requesting password.
 *
 * Options can be provided for the session open and the mount itself.
 * If the mount is successful, the POSIX path to each mountpoint is
 * returned as a CFStringRef in mountpoints.
 *
 * If the return value is zero the mount has succeeded.
 *
 * A positive non-zero return value represents an errno value
 * (see /usr/include/sys/errno.h).  For instance, a missing mountpoint
 * error will be returned as ENOENT (2).
 *
 * A negative non-zero return value represents an OSStatus error.
 * For instance, error -128 is userCanceledErr, returned when a mount
 * operation is canceled by the user. These OSStatus errors are
 * extended to include:
 *
 *  from this header:
 *  ENETFSPWDNEEDSCHANGE        -5045
 *  ENETFSPWDPOLICY         -5046
 *  ENETFSACCOUNTRESTRICTED     -5999
 *  ENETFSNOSHARESAVAIL     -5998
 *  ENETFSNOAUTHMECHSUPP        -5997
 *  ENETFSNOPROTOVERSSUPP       -5996
 *
 *  from <NetAuth/NetAuthErrors.h>
 *  kNetAuthErrorInternal       -6600
 *  kNetAuthErrorMountFailed    -6602
 *  kNetAuthErrorNoSharesAvailable  -6003
 *  kNetAuthErrorGuestNotSupported  -6004
 *  kNetAuthErrorAlreadyClosed  -6005
 *
 */
int
NetFSMountURLSync(
    CFURLRef url,               // URL to mount, e.g. nfs://server/path
    CFURLRef mountpath,         // Path for the mountpoint
    CFStringRef user,           // Auth user name (overrides URL)
    CFStringRef passwd,             // Auth password (overrides URL)
    CFMutableDictionaryRef open_options,    // Options for session open (see below)
    CFMutableDictionaryRef mount_options,   // Options for mounting (see below)
    CFArrayRef *mountpoints)        // Array of mountpoints
    __OSX_AVAILABLE_STARTING(__MAC_10_8, __IPHONE_NA);

如果对您的用例有用,还有一个异步版本。

this answer 所示,如果您想指定挂载点,则需要使用kNetFSMountAtMountDirKey 选项。

【讨论】:

  • 这是一个有趣的想法。我以前没有使用过 OS X 框架。我主要习惯于 C++ 标准库和通过MacPorts 获得的各种开源库。除了习惯 Apple 的长 CamelCaseVariableNames 之外,深入研究 OS X 框架是否存在任何重大的概念挑战?学习曲线陡峭?
  • 嗯,macOS 上有很多不同的框架。不过,NetFS 似乎真的只依赖于 Core Foundation。应该不会太差。要学习的主要内容是内存管理规则和它们所基于的命名约定。见thisthis
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-10
  • 1970-01-01
  • 2011-01-15
  • 1970-01-01
  • 1970-01-01
  • 2010-11-09
  • 1970-01-01
相关资源
最近更新 更多