带有用户提供的目录的 Glob?
我正在编写一个程序,该程序应该搜索用户提供的目录,以便查找该目录中在给定时间的一天内被访问、修改或更改的所有文件。我有两个明确的问题,也许还有另一个问题。
第一个问题是我只能让程序进行浅层搜索,它不会查找任何子目录。我确信这与我连接到目录缓冲区的内容有关(现在是.)。第二个问题是它并没有搜索每个文件,尽管它确实查看了大多数文件 - 我认为这又回到了问题一。第三个“问题”是,当我检查每个文件的访问时间时,它们似乎都是相同的(尽管我在更改/修改时间时没有这个问题)。我通过 VM 在 Ubuntu 上运行,如果这可能会影响访问时间。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <glob.h>
/* Function that checks if specified file was accessed, modified, or
changed within a day of the specified time. */
void checkFile(long long time, char * fileName) {
struct stat *s;
time_t accessTime;
time_t modTime;
time_t changeTime;
s = malloc(sizeof(struct stat));
if(stat(fileName,s) == 0) {
accessTime = s->st_atime;
modTime = s->st_mtime;
changeTime = s->st_ctime;
if((time - accessTime) <= 86400 && (time - accessTime) >= -86400)
printf("%s\n",fileName);
else if((time - modTime) <= 86400 && (time - modTime) >= -86400)
printf("%s\n",fileName);
else if((time - changeTime) <= 86400 && (time - changeTime) >= -86400)
printf("%s\n",fileName);
}
free(s);
}
void searchDirectory(long long time, glob_t globbuf) {
if(globbuf.gl_pathc == 0)
printf("there were no matching files");
else {
int i;
for(i = 0; i < globbuf.gl_pathc; i++)
checkFile(time,globbuf.gl_pathv[i]);
}
}
int main(int argc, char** argv) {
long long time = atol(argv[1]);
char * buf = argv[2];
strcat(buf,"*.*");
glob_t globbuf;
glob(buf, 0, NULL, &globbuf);
searchDirectory(time,globbuf);
globfree(&globbuf);
return 0;
}
感谢您抽出时间!
I am writing a program that should search through a directory that the user supplies in order to find all files in said directory that were accessed, modified, or changed within a day of a given time. I am having two definite problem and perhaps another one.
The first problem is that I can only get the program to do shallow searches, it won't look through any subdirectories. I am sure it has to do with what I concatenate to the directory buffer ( right now it is .). The second problem is that it is not searching every file, though it does look at most of them - I think this goes back to problem one though. The third "problem" is that when I check the access time of each file, it seems as though they are all the same (though I don't have this problem with changed/modified time). I am running on Ubuntu through VM, if that might be affecting the access times.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <glob.h>
/* Function that checks if specified file was accessed, modified, or
changed within a day of the specified time. */
void checkFile(long long time, char * fileName) {
struct stat *s;
time_t accessTime;
time_t modTime;
time_t changeTime;
s = malloc(sizeof(struct stat));
if(stat(fileName,s) == 0) {
accessTime = s->st_atime;
modTime = s->st_mtime;
changeTime = s->st_ctime;
if((time - accessTime) <= 86400 && (time - accessTime) >= -86400)
printf("%s\n",fileName);
else if((time - modTime) <= 86400 && (time - modTime) >= -86400)
printf("%s\n",fileName);
else if((time - changeTime) <= 86400 && (time - changeTime) >= -86400)
printf("%s\n",fileName);
}
free(s);
}
void searchDirectory(long long time, glob_t globbuf) {
if(globbuf.gl_pathc == 0)
printf("there were no matching files");
else {
int i;
for(i = 0; i < globbuf.gl_pathc; i++)
checkFile(time,globbuf.gl_pathv[i]);
}
}
int main(int argc, char** argv) {
long long time = atol(argv[1]);
char * buf = argv[2];
strcat(buf,"*.*");
glob_t globbuf;
glob(buf, 0, NULL, &globbuf);
searchDirectory(time,globbuf);
globfree(&globbuf);
return 0;
}
Thanks for your time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该
...因为“buf”是指向操作系统提供的字符串的指针 - 您不知道该缓冲区是否足够大以容纳您要添加的额外文本。您可以分配一个大缓冲区,将
argv[2]
的内容复制到其中,然后附加"*.*"
,但为了真正安全,您应该确定长度argv[2] 并确保缓冲区足够大。您可以使用
struct stat
结构的st_mode
成员来确定文件是否是目录(检查它是否等于S_IFDIR
)。如果是,您可以将其设为当前目录,并按照 jonsca 的建议,再次调用searchDirectory
函数。但是,当使用递归时,您通常需要确保递归深度有限制,否则堆栈可能会溢出。这是一种“深度优先搜索”。我更喜欢的解决方案是使用队列进行“广度优先搜索”:基本上将您的第一个 glob 推到列表的开头,然后重复从该列表中取出第一项并搜索它,将新目录添加到列表的末尾边走边列出,直到列表为空。在评估这样的程序时,老师喜欢为那些不太容易破坏堆栈的程序奖励额外的分数:)
PS我猜访问时间问题是虚拟机/文件系统/等不兼容,而不是你的错。
You should not
...since 'buf' is a pointer to a string provided by the os - you don't know if that buffer is large enough to hold the extra text you are adding. You could allocate a large buffer, copy the contents of
argv[2]
into it and then append"*.*"
, but to be really safe you should determine the length ofargv[2
] and ensure your buffer is large enough.You can use the
st_mode
member of thestruct stat
structure to determine if the file is a directory (check if it equalsS_IFDIR
). If it is, you could make it the current directory and as jonsca suggested, call yoursearchDirectory
function again. But when using recursion you usually want to ensure you have a limit on the depth of recursion, or you can overflow the stack. This is a kind of 'depth first search'. The solution I prefer is to do a 'breadth first search' using a queue: basically push your first glob onto the start of a list, then repeatedly take the first item off that list and search it, adding new directories to the end of the list as you go, until the list is empty.When evaluating programs like this, teachers love to award extra points for those that don't blow their stack too easily :)
P.S. I'm guessing the access time issue is a VM/filesystem/etc incompatibility and not your fault.