c++ - 如何使用c中的mount函数?

标签 c++ mount

我想使用mount函数来实现NFS。

int mount(const char *source, const char *target,
                 const char *filesystemtype, unsigned long mountflags,
                 const void *data);

我可以使用mount命令来实现它,例如mount 172.16.0.144:/tmp/test/tmp/test。但是当我使用 mount() 函数时,它不起作用。这是我的代码。

#include<sys/mount.h> 
#include<iostream>
#include<errno.h>
#include<fstream>
#include<string.h>
using namespace std;

int main(int argc, char**argv) {
    const char* srcPath = "/tmp/watchman";
    const char* targetPath = "172.16.0.144:/tmp/watchman";
    if (argc == 3) {
        srcPath = argv[1];
        targetPath = argv[2];
        cerr << "reset the src && target path\n";
    } else {
        if (argc != 1) {
            cerr << "wrong input argument!\n";
            return 0;
        }
    }
    cerr << "srcPath = " << srcPath << endl;
    cerr << "target = " << targetPath << endl;
    int ret_val = mount(srcPath, targetPath, "", MS_SHARED, "");
    if (ret_val == 0) {
        cerr << "mount succeed\n";
        string filename = string(srcPath) + "/" + "tmp.txt";
        fstream fin(filename.c_str(), ios::out);
        fin << "there is a write test from client\n";
        fin.close();
        ret_val = umount(srcPath);
        if (ret_val == 0) {
            cerr << "umount succeed \n";
        } else {
            cerr << "umount failed \n";
            printf("%s/n", strerror(errno));
        }
    } else {
        cout<<"ret_val = "<<ret_val<<endl;
        cerr << "mount failed \n";
        cerr << strerror(errno) << endl;
    }
    return 0;
}

printf挂载失败,没有这样的文件或目录。有人可以帮助我吗?请 !!!

最佳答案

如果您read the mount manual page你会看到

mount() attaches the filesystem specified by source (which is often a pathname referring to a device, but can also be the pathname of a directory or file, or a dummy string) to the location (a directory or file) specified by the pathname in target.

您已在应用程序中切换源和目标。

关于c++ - 如何使用c中的mount函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42157914/

相关文章:

c++ - 为什么 HRESULT 0 表示成功?

c++ - 在 C++ 中跨类使用结构体

c++ - 在 Windbg 中转储值和填充

php - 在 phar 中挂载文件夹不起作用

Docker --mount type=tmpfs 具有 'exec' 权限

c++ - 可移植内存搜索功能的返回类型

C++ 从参数参数包创建 vector 或列表

c - 使用 readdir_r 遍历目录树时如何跳过已安装的文件

linux - 不使用 Fuse 挂载到本地目录。

使用C在fuse中创建文件