我正在試圖製作一個監視一個文件夾的程序,並檢查內部的任何文件是否修改了其內容或權限。UNIX文件信息
我的代碼是在這裏:
void verifyChanges(char *directory, int duration, int interval, char *logfile, bool lastModified, bool changedPermissions){
//Definição de variáveis
int i, j;
int timeint = 0;
char * initialFileList[MAX_LIST_SIZE];
char * finalFileList[MAX_LIST_SIZE];
struct stat initialStats[MAX_STRUCT_SIZE];
struct stat finalStats[MAX_STRUCT_SIZE];
bool found;
FILE *log = fopen(logfile, "a");
while(timeint <= (duration*SECONDS)){
int initialFileNr = getFileNameStats(directory, initialFileList, initialStats);
sleep(interval);
int finalFileNr = getFileNameStats(directory, finalFileList, finalStats);
//Check file names of finalFileList thas does not appear in initialFileList
for (i = 0; i < finalFileNr; i++){
found = false;
for (j = 0; j < initialFileNr; j++){
if(strcmp(finalFileList[i], initialFileList[j]) == 0){
found = true;
break;
}
}
if(!found){
char *time = formatTime(finalStats[i].st_mtime);
char *time_tok = strtok(time, " ");
time_tok = strtok(0, " ");
time_tok = strtok(0, " ");
time_tok = strtok(0, " ");
//This fprintf does not print the filename. What is wrong??
fprintf(log, "%-15s %-8s %-51s CRE\n", finalFileList[i], time_tok, directory);
fflush(log);
printf("New File Created!!!!!\n");
}
}
//Same as befor, but this time searching for deleted files
for(i = 0; i < initialFileNr; i++){
found = false;
for(j = 0; j < finalFileNr; j++){
if(strcmp(initialFileList[i], finalFileList[j]) == 0){
found = true;
break;
}
}
if(!found){
char *time = formatTime(initialStats[i].st_mtime);
char *time_tok = strtok(time, " ");
time_tok = strtok(0, " ");
time_tok = strtok(0, " ");
time_tok = strtok(0, " ");
fprintf(log, "%-15s %-8s %-51s DEL\n", initialFileList[i], time_tok, directory);
fflush(log);
printf("Deleted!!!!!\n");
}
}
//At last, checking if common files on first and second list was modified
for(i = 0; i < initialFileNr; i++){
for(j = 0; j < finalFileNr; j++){
if(srtcmp(initialFileList[i], finalFileList[j]) == 0){
//checking content changes
if((initialStats[i].st_mtime != finalStats[j].st_mtime) && lastModified){
char *time = formatTime(finalStats[j].st_mtime);
char *time_tok = strtok(time, " ");
time_tok = strtok(0, " ");
time_tok = strtok(0, " ");
time_tok = strtok(0, " ");
fprintf(log, "%-15s %-8s %-51s EDI\n", finalFileList[j], time_tok, directory);
fflush(log);
printf("Changed Content!!!!!\n");
}
//Verificar alterações às permissões
if((initialStats[i].st_mode != finalStats[j].st_mode) && changedPermissions){
char *time = formatTime(finalStats[j].st_mtime);
char *time_tok = strtok(time, " ");
time_tok = strtok(0, " ");
time_tok = strtok(0, " ");
time_tok = strtok(0, " ");
fprintf(log, "%-15s %-8s %-51s PER\n", finalFileList[j], time_tok, directory);
fflush(log);
printf("Changed Permissionss!!!!!\n");
}
}
}
}
timeint += interval;
}
fclose(log);
}
我知道,此代碼不會有好的表現,但現在,我要的是爲它工作。我有一個其他的功能,獲得該文件夾內的文件名稱和統計資料,但該功能正常工作。
當我嘗試刪除一個文件夾的名稱比該文件夾中的某個其他文件(字母順序)更低時,程序告訴我已經創建了一個文件,而不是將其刪除。我懷疑這可能是一個索引問題,但我不知道它在哪裏。
在此先感謝您的幫助!
P.S.
另一個函數,即獲取文件名和統計信息。這裏有什麼不對嗎?
int getFileNameStats(char *directory, char *fileList[], struct stat stats[]){
int i = 0;
DIR *dirp;
struct dirent *direntp;
struct stat stat_buf;
char fileName[MAX_DIR_SIZE];
dirp = opendir(directory);
while ((direntp = readdir(dirp)) != NULL)
{
sprintf(fileName, "%s/%s", directory, direntp->d_name);
if (lstat(fileName, &stat_buf)==-1){
perror(fileName);
exit(3);
}
if(strcmp(direntp->d_name,".") && strcmp(direntp->d_name,"..")){
if (S_ISREG(stat_buf.st_mode)){
fileList[i] = (char *) malloc(MAX_NAME_SIZE*sizeof(char));
fileList[i] = direntp->d_name;
stats[i] = stat_buf;
printf("%-25s - regular\n", fileList[i]);
i++;
}
}
}
closedir(dirp);
return i;
}
還沒有經歷過您的代碼,但如果這是Linux,我認爲[inotify](http://www.linuxjournal.com/article/8478)比你的空閒計時器+基於輪詢的方法更好。 – 2011-04-17 19:52:26
也許你可以在英文中添加一些註釋,說明哪些行不正確? – BMitch 2011-04-17 20:00:53
我添加了一些評論並翻譯了其他人。還添加了其他功能,其中問題可能是... – 2011-04-17 20:28:53