2017-02-10 13 views
0

NFSを実装するためにmount関数を使いたいと思っています。cからマウント機能を使用する方法?

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

私は、コマンド例えば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のマウントに失敗しました。そのようなファイルまたはディレクトリはありません。誰でも私を助けることができますか?お願いします !!!

+0

正しいタグを使用してください。これはC++ではなく、Cです。 – Olaf

答えて

1

あなたが

mount()は、多くの場合、デバイスを参照するパス名ですsourceで指定されたファイルシステムを(添付していることがわかりますread the mount manual page、だけでなく、ディレクトリやファイルのパス名することができた場合、またはダミー文字列)を、targetのパス名で指定された場所(ディレクトリまたはファイル)にコピーします。

アプリケーションでソースとターゲットを切り替えました。

+0

私はそれを試していますが、それでも動作しません。あなたは私にその事例を教えてもらえますか?私はそれを非常に混乱させています。 –

+0

@CloriaD '/ tmp/watchman'ディレクトリは存在しますか?マウントの対象として使用するには、それが存在する必要があります。 –

+0

はい、もちろん存在します。 NFSサーバーとローカルの両方に/ tmp/watchmanフォルダーが存在します。だから、あなたはこの例題を実行して良い結果を得ていますか?お願い助けて。 –

関連する問題