gpt4 book ai didi

c - 标准要求重命名是原子的吗?

转载 作者:太空狗 更新时间:2023-10-29 15:03:33 33 4
gpt4 key购买 nike

在我试图回答这个问题时出现了一个有趣的问题:

Is mv atomic on my fs?

rename() 函数要求按照标准是原子的吗?

POSIX standard for rename 的“基本原理”部分状态:

This rename() function is equivalent for regular files to that defined by the ISO C standard. Its inclusion here expands that definition to include actions on directories and specifies behavior when the new parameter names a file that already exists. That specification requires that the action of the function be atomic.

但是,latest publicly-available ISO C Standard rename 部分的全部内容如下:

7.21.4.2 The rename function

Synopsis

#include <stdio.h>
int rename(const char *old, const char *new);

Description

The rename function causes the file whose name is the string pointed to by old to be henceforth known by the name given by the string pointed to by new. The file named old is no longer accessible by that name. If a file named by the string pointed to by new exists prior to the call to the rename function, the behavior is implementation-defined.

Returns

The rename function returns zero if the operation succeeds, nonzero if it fails, in which case if the file existed previously it is still known by its original name.

在 ISO C 标准的 rename() 部分中,对于任何类型的原子性都没有任何明确的要求。

我编写过许多依赖于 rename() 显然特定于实现的原子性的程序,我曾假设原子性是一项要求,但对 C 标准中的缺失感到惊讶。

但是 POSIX 标准规定 ISO C 标准要求 rename() 是原子的。

解释?

最佳答案

您对 rename() 的 POSIX 标准的引用来自(非规范的)“基本原理”部分。主要条目——实际的规范 Material ——开始于:

For rename(): [CX] [Option Start] 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. [Option End]

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] [Option Start] 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.

[Option End]

条目的所有其余部分都在 [CX] 内(C 扩展)标记并讨论其他特殊行为。

你引用的理由是:

This rename() function is equivalent for regular files to that defined by the ISO C standard. Its inclusion here expands that definition to include actions on directories and specifies behavior when the new parameter names a file that already exists. That specification requires that the action of the function be atomic.

最后一句中提到的“那个规范”是扩展定义,包括对目录的操作规范和“当新参数命名一个已经存在的文件时”,而不是 C 标准中的规范,其中,如您所见,它没有提及任何关于原子性的内容(非常合理;有些系统可以支持 C 和 rename(),但无法支持 POSIX 更严格的原子性要求)。

我看到这正是 T.C 提出的论点在他们的 comment — 我同意 T.C.

关于c - 标准要求重命名是原子的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40538241/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com