C - 如何将文件从cifs mount移动到本地hdd

sen*_*aze 1 c linux smb cifs

当我尝试将文件从cifs挂载重命名为本地路径(将文件从服务器移动到本地hdd)时,我得到-1.我可以删除文件,我可以添加新文件,我只是不能使用rename()函数来做到这一点.该程序以root用户身份运行,cifs mount中的用户拥有该共享和服务器上本地文件系统的完全权限.

服务器:Windows XP SP3 x32

本地:Ubuntu 13.04 x64

smb坐骑:

sudo mount -t cifs -o username=admin_account,password=<passw> \
  //server/share /local/mount/point
Run Code Online (Sandbox Code Playgroud)

C代码:

void
function moveFile(char *fname){
  char *base;
  base = basename(fname);
  char newF[strlen(getSaveDir()) + strlen(base)];
  sprintf(newF,"%s%s", getSaveDir(), base);
  int result;
  result = rename(fname, newF);
  if( result == 0 ) {
    printf("Moved file: %s to %s", fname, newF);
  } else {
    printf("There was an error moving %s to %s (ID: %d)", fname, newF, result);
    //TODO figure out better fix than this
    remove(fname);
  }
}
Run Code Online (Sandbox Code Playgroud)

nos*_*nos 7

rename()仅适用于同一设备,它只是更改其名称(或将名称"移动"到另一个目录).rename()无法将文件数据从一个位置移动到另一个位置.

如果要复制或移动文件,则需要自己完成:

  • 打开源文件和目标文件
  • 从源文件中读取(),在循环中写入目标文件直到结束.
  • unlink()源文件(仅当你要移动它时.)