나는 주어진 시간의 하루 안에 액세스, 수정 또는 변경된 디렉토리의 모든 파일을 찾기 위해 사용자가 제공하는 디렉토리를 검색해야하는 프로그램을 작성 중입니다. 나는 두 가지 확실한 문제와 아마도 또 다른 문제를 안고 있습니다.사용자가 제공 한 디렉토리가있는 Glob?
첫 번째 문제는 얕은 검색을 수행하는 프로그램 만 가져올 수 있으며 하위 디렉토리를 조사하지 않는다는 것입니다. 디렉터리 버퍼에 연결 한 것과 관련이 있다고 확신합니다 (지금은 입니다.). 두 번째 문제는 대부분의 파일을 살펴 봤지만 모든 파일을 검색하지 않는다는 것입니다. 문제는이 파일로 돌아갑니다. 세 번째 "문제"는 각 파일의 액세스 시간을 확인할 때 모든 파일이 동일한 것처럼 보입니다 (변경되었거나 수정 된 시간에이 문제가 없음에도 불구하고). 액세스 시간에 영향을 줄 수 있다면 VM을 통해 우분투에서 실행 중입니다.
#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;
}
감사합니다.
유익한 답변을 보내 주셔서 감사합니다. –