移动文件 - ç [英] Moving a File - C

查看:122
本文介绍了移动文件 - ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 平台:Debian的喘息3.2.0-4-686-PAE

  • 编者:GCC(Debian的4.7.2-5)4.7.2(code ::块)

我想从一个位置移动文件到另一个。没有什么复杂的像移动到不同的驱动器或不同的文件系统。我知道,标准的方式来做到这将是简单地复制该文件,然后删除原始。但我想preserving文件的所有权,模式,最后访问/修改等的一些方法。我假设我将要复制的文件,然后编辑新文件的所有权,模式等事后但我不知道如何做到这一点。

I want to move a file from one location to another. Nothing complex like moving to different drives or to different file systems. I know the "standard" way to do this would be simply copying the file and then removing the original. But I want some way of preserving the file's ownership, mode, last access/modification, etc. . I am assuming that I will have to copy the file and then edit the new file's ownership, mode, etc. afterwards but I have no idea how to do this.

推荐答案

如果您无法使用重命名(2)系统调用(例如,因为源和目标是不同的文件系统),你必须查询源文件的大小,权限和其他元数据与的 STAT(2);使用阅读(2)复制数据,的write(2)(使用几千字节的缓冲区)的open(2),的关闭(2)并使用 CHMOD(2)中的元数据, CHOWN(2),的 UTIME(2)。您可能还关心复制属性使用 getxattr(2),的setxattr(2),的listxattr(2)

If you cannot use the rename(2) syscall (e.g. because source and target are on different filesystem), you have to query the size, permission and other metadata of the source file with stat(2); copy the data using read(2), write(2) (using a buffer of several kilobytes), open(2), close(2) and the metadata using chmod(2), chown(2), utime(2). You might also care about copying attributes using getxattr(2), setxattr(2), listxattr(2)

如果源和目标都在不同的文件系统,没有办法,使移动原子,并避免竞争条件(因此,使用的rename(2)是preferable出现,因为它根据其手册页是原子)。源文件总是可以(由另一个进程)在移动操作修改...

And if the source and target are on different filesystems, there is no way to make the move atomic and avoid race conditions (So using rename(2) is preferable when possible, because it is atomic according to its man page). The source file can always be modified (by another process) during the move operations...

高级Linux编程了解(也尝试 strace的一些 MV 命令来了解它在做什么)。

Read Advanced Linux Programming for more (and also try to strace some mv command to understand what it is doing).

这篇关于移动文件 - ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆