如果涉及超过 1 个目录,移动文件是否是原子的?
在 NTFS 中,文件移动是原子的,如下所述 这里:
[...] 如果您在 NTFS 下运行,则文件操作是文件的原子操作 系统级别。就任何更高的代码而言,重命名将在单个操作中发生 很关心。
这是否意味着如果将文件从一个目录移动到另一个目录,移动文件也是原子的?还是只能在同一个目录下?
我假设两个目录位于同一卷上。
In NTFS, file moving is atomic as explained here:
[...] if you are running under NTFS then file operations are atomic at the file
system level. A rename will occur in a single operation as far as any higher code
is concerned.
Does this mean that moving a file is atomic also if it is moved from one directory to another directory? Or only within the same directory?
I'm assuming both directories are located on the same volume.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,它们是原子的。源目录和目标目录在移动期间都被锁定,以防止程序看到传输中的数据。
然而,这只是“失败原子性”。您的程序可能会进行移动,认为它已经移动了东西(并与另一台服务器通信它已经这样做了),但对控制结构本身的修改尚未到达媒体。断电可能会放弃这些更改,但 NTFS 将确保目录的一致性(即不需要 chkdsk)。
后续程序访问将看到移动之前或移动之后的文件,但不会在发生时看到文件。
Yes, they are atomic. Both source and destination directories are locked for the duration of the move, preventing programs from seeing inflight data.
However, this is only "failure atomicity". Your program may do the move, thinking it has moved things (and communicating to another server that it has done so), but the modifications to the control structures themselves hasn't made it to the media. A power loss may discard those changes but NTFS will assure the consistency of the directories (aka, no chkdsk required).
Subsequent program access will either see the files as they are before the move or after the move, but not while-it-is-happening.