当我试图回答这个问题时,出现了一个有趣的问题:
mv 在我的 fs 上是原子的吗? https://unix.stackexchange.com/questions/322038/is-mv-atomic-on-my-fs/322074#322074
Is the rename()
功能required按照标准是原子的?
的“理由”部分POSIX 标准rename http://pubs.opengroup.org/onlinepubs/9699919799/functions/rename.html states:
This rename()
功能与常规文件等效
由 ISO C 标准定义。它包含在这里扩展了
定义包括对目录的操作并指定行为
当新参数命名一个已经存在的文件时。那
规范要求函数的操作是原子的。
但是,最新公开的 ISO C 标准 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf部分关于rename
全文指出:
7.21.4.2rename
功能
Synopsis
#include <stdio.h>
int rename(const char *old, const char *new);
描述
The rename
函数导致文件名是由 指向的字符串old
从此以后以这个名字为人所知
由指向的字符串给出new
。该文件名为old
没有
可以通过该名称更方便地访问。如果由指向的字符串命名的文件
到通过new
在调用之前存在rename
函数,则
行为是实现定义的。
Returns
The rename
如果操作成功则函数返回零,如果失败则返回非零,在这种情况下如果文件存在
此前,它仍以其原始名称而闻名。
对于任何类型的原子性都没有任何类型的明确要求rename()
ISO C 标准的部分。
编写了许多依赖于明显特定于实现的原子性的程序rename()
,我原以为原子性是一项要求,但对 C 标准中的缺乏感到惊讶。
但POSIX标准说ISO C标准要求rename()
成为原子的。
解释?
您对 POSIX 标准的引用rename() http://pubs.opengroup.org/onlinepubs/9699919799/functions/rename.html来自(非规范性)“基本原理”部分。主条目——实际的规范材料——开始于:
For rename(): [CX] The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of POSIX.1-2008 defers to the ISO C standard.
The rename()
function shall change the name of a file. The old
argument points to the pathname of the file to be renamed. The new
argument points to the new pathname of the file. [CX] If the new
argument does not resolve to an existing directory entry for a file of type directory and the new
argument contains at least one non-<slash>
character and ends with one or more trailing <slash>
characters after all symbolic links have been processed, rename()
shall fail.
…
所有其余的条目都在[CX]
(C 扩展)标签并讨论其他特殊行为。
你引用的理由是:
This rename()
常规文件的功能与 ISO C 标准定义的功能等效。此处的包含扩展了该定义,以包括对目录的操作,并指定新参数命名已存在的文件时的行为。该规范要求函数的操作是原子的。
最后一句中提到的“该规范”是扩展定义,其中包括对目录的操作规范和“当新参数命名已存在的文件时”,而不是 C 标准中的规范,正如您所观察到的,没有提及任何关于原子性的内容(非常合理;有可以支持 C 和rename()
无法支持 POSIX 更严格的原子性要求)。
我发现这正是T.C https://stackoverflow.com/users/2756719/t-c在他们的comment https://stackoverflow.com/questions/40538241/is-rename-required-by-standard-to-be-atomic#comment68316147_40538241— 我同意 T.C.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)