How to avoid race condition when checking if file exists and then creating it?

十年热恋 提交于 2020-01-01 17:09:25

问题


I'm thinking of corner cases in my code and I can't figure out how to avoid problem when you check if file exists, and if it does not, you create a file with that filename. The code approximately looks like this:

// 1
status = stat(filename);
if (!status) {
  // 2
  create_file(filename);
}

Between the call to 1 and 2 another process could create the filename. How to avoid this problem and is there a general solution to this type of problems? They happen often in systems programming.


回答1:


You're supposed to create the file anyway, and let the OS know whether or not you want a new file to be created in the process if the file doesn't already exist. You shouldn't perform a separate check before.




回答2:


This is what the O_EXCL | O_CREAT flags to open() were designed for:

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. If O_EXCL and O_CREAT are set, and path names a symbolic link, open() shall fail and set errno to [EEXIST], regardless of the contents of the symbolic link. If O_EXCL is set and O_CREAT is not set, the result is undefined.

So:

fd = open(FILENAME, O_EXCL | O_CREAT | O_RDWR);
if (fd <0) { /* file exists or there were problems like permissions */
    fprintf(stderr, "open() failed: \"%s\"\n", strerror(errno));
    abort();
}
 /* file was newly created */


来源:https://stackoverflow.com/questions/24712383/how-to-avoid-race-condition-when-checking-if-file-exists-and-then-creating-it

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