1

当我尝试将文件从 cifs 挂载重命名到本地路径(将文件从服务器移动到本地硬盘)时,我得到 -1。我可以删除文件,也可以添加新文件,只是不能使用 rename() 函数来完成。该程序以 root 身份运行,并且 cifs 挂载中的用户对共享和服务器上的本地文件系统具有完全权限。

服务器:Windows XP SP3 x32

本地:Ubuntu 13.04 x64

smb 挂载:

sudo mount -t cifs -o username=admin_account,password=<passw> \
  //server/share /local/mount/point

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);
  }
}
4

2 回答 2

7

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

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

  • 打开源文件和目标文件
  • 从源文件中读取(),循环写入目标文件直到结束。
  • unlink() 源文件(仅当您想移动它时。)
于 2013-08-14T18:00:51.507 回答
3

In all likelihood, if you interrogate errno after rename fails, you will find that it is set to EXDEV.

May I suggest that you add that information or confirm that it is EXDEV.

If you are getting EXDEV, then it is because of the Linux limitation that rename() only works if oldpath and newpath are on the same mounted file system.

From rename(2)

   EXDEV  oldpath and newpath are not on the  same  mounted  file  system.
          (Linux  permits  a file system to be mounted at multiple points,
          but rename() does not work across different mount  points,  even
          if the same file system is mounted on both.)
于 2013-08-14T17:55:00.107 回答