我想使用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 bysource
(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 intarget
.
您已在应用程序中切换源和目标。
关于c++ - 如何使用c中的mount函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42157914/