atomic file creation on Linux?

て烟熏妆下的殇ゞ 提交于 2019-12-10 10:44:22

问题


I need to create a file if it does not exist, in a way that another process trying to create this file would fail. I need the file be considered "created" even before the creating process finished writing the actual data to it.

I read about O_EXCL flag to open(), so it seems that the solution exists, I have a few questions however:

  1. do you have experience with this technique? How good is it? (I guess I can't have a DB-level atomicity, but but good enough is... well, enough)
  2. should I immediately close the file after open() so that it is considered created, and then reopen it for writing?
  3. are there any subtleties to be aware of?

回答1:


The open() man page says your method may fail on NFS.

From the section on O_EXCL:

When used with O_CREAT, if the file already exists it is an error and the open() will fail. In this context, a symbolic link exists, regardless of where it points to. O_EXCL is broken on NFS file systems; programs which rely on it for performing locking tasks will contain a race condition.

And it suggests a more general solution:

The solution for performing atomic file locking using a lockfile is to create a unique file on the same file system (e.g., incorporating hostname and pid), use link(2) to make a link to the lockfile. If link() returns 0, the lock is successful. Otherwise, use stat(2) on the unique file to check if its link count has increased to 2, in which case the lock is also successful.

See the "Using Files as Locks" section of this Web page for more details on the various issues and approaches.




回答2:


POSIX says:

If O_CREAT and O_EXCL are set, open() shall fail if the file exists. The check for the existence of the file and the creation of the file if it does not exist shall be atomic with respect to other threads executing open() naming the same filename in the same directory with O_EXCL and O_CREAT set.

So other processes using O_EXCL will consider it opened as soon as it is created.



来源:https://stackoverflow.com/questions/5210945/atomic-file-creation-on-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!