删除文件在读取目录中READDIR() [英] Delete files while reading directory with readdir()
问题描述
我的code是这样的:
My code is something like this:
DIR* pDir = opendir("/path/to/my/dir");
struct dirent pFile = NULL;
while ((pFile = readdir())) {
// Check if it is a .zip file
if (subrstr(pFile->d_name,".zip") {
// It is a .zip file, delete it, and the matching log file
char zipname[200];
snprintf(zipname, sizeof(zipname), "/path/to/my/dir/%s", pFile->d_name);
unlink(zipname);
char* logname = subsstr(zipname, 0, strlen(pFile->d_name)-4); // Strip of .zip
logname = appendstring(&logname, ".log"); // Append .log
unlink(logname);
}
closedir(pDir);
(此code是未经考验的,纯粹的例子)
(this code is untested and purely an example)
问题的关键是:是否允许删除一个文件目录中,同时通过与目录循环readdir的()?
或将READDIR()仍然发现删除.log文件?
The point is: Is it allowed to delete a file in a directory while looping through the directory with readdir()? Or will readdir() still find the deleted .log file?
推荐答案
引用自 POSIX readdir的:
如果一个文件被去除或添加到
后的最新的目录
调用执行opendir()或rewinddir()
无论后续调用READDIR()
返回该文件的条目
不确定的。
If a file is removed from or added to the directory after the most recent call to opendir() or rewinddir(), whether a subsequent call to readdir() returns an entry for that file is unspecified.
所以,我的猜测是...这要看情况。
So, my guess is ... it depends.
这取决于OS的不同,一天中的时间,对添加的文件的相对顺序/删除,...
It depends on the OS, on the time of day, on the relative order of the files added/deleted, ...
和,作为进一步的点,时间之间的 READDIR()
函数返回,并尝试取消链接()
该文件,其他进程可能已经删除该文件,你的取消链接()
失败。
And, as a further point, between the time the readdir()
function returns and you try to unlink()
the file, some other process could have deleted that file and your unlink()
fails.
的修改的
我用此程序进行测试:
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
int main(void) {
struct dirent *de;
DIR *dd;
/* create files `one.zip` and `one.log` before entering the readdir() loop */
printf("creating `one.log` and `one.zip`\n");
system("touch one.log"); /* assume it worked */
system("touch one.zip"); /* assume it worked */
dd = opendir("."); /* assume it worked */
while ((de = readdir(dd)) != NULL) {
printf("found %s\n", de->d_name);
if (strstr(de->d_name, ".zip")) {
char logname[1200];
size_t i;
if (*de->d_name == 'o') {
/* create `two.zip` and `two.log` when the program finds `one.zip` */
printf("creating `two.zip` and `two.log`\n");
system("touch two.zip"); /* assume it worked */
system("touch two.log"); /* assume it worked */
}
printf("unlinking %s\n", de->d_name);
if (unlink(de->d_name)) perror("unlink");
strcpy(logname, de->d_name);
i = strlen(logname);
logname[i-3] = 'l';
logname[i-2] = 'o';
logname[i-1] = 'g';
printf("unlinking %s\n", logname);
if (unlink(logname)) perror("unlink");
}
}
closedir(dd); /* assume it worked */
return 0;
}
在我的电脑上, READDIR()
查找删除的文件并没有找到之间建立执行opendir文件()
和 READDIR()
。但也可以是另一台计算机上的不同;它可能是,如果我编译使用不同的选项在我的电脑上的不同;它可能是不同的,如果我升级内核; ...
On my computer, readdir()
finds deleted files and does not find files created between opendir()
and readdir()
. But it may be different on another computer; it may be different on my computer if I compile with different options; it may be different if I upgrade the kernel; ...
这篇关于删除文件在读取目录中READDIR()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!