【问题标题】:UNIX file informationUNIX 文件信息
【发布时间】:2011-04-17 19:48:32
【问题描述】:

我正在尝试制作一个程序来监视一个文件夹,并检查其中的任何文件是否修改了其内容或权限。

我的代码在这里:

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);
}

我知道这段代码没有很好的性能,但现在,我想要的是让它工作。我还有另一个函数可以获取该文件夹内的文件名和统计信息,但该函数工作正常。

当我尝试删除一个名称低于该文件夹内其他文件的文件夹(按字母顺序排列)时,程序告诉我我创建了一个文件,而不是删除它。我怀疑这可能是索引的问题,但我不知道它在哪里。

提前感谢您的每一个帮助!

附言

另一个函数,获取文件名和统计信息。这里有什么问题吗?

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 比你的空闲计时器 + 基于轮询的方法更好。
  • 或许您可以添加一些英文的 cmets 解释哪些行运行不正确?
  • 我添加了一些 cmets 并翻译了其他的。还添加了其他可能出现问题的功能...

标签: c linux unix monitoring


【解决方案1】:

你不能像这样比较字符串:

char * initialFileList[MAX_LIST_SIZE];
char * finalFileList[MAX_LIST_SIZE];
....
if(finalFileList[i] == initialFileList[j]){

改用strcmp

if(strcmp(finalFileList[i], initialFileList[j]) == 0){

请确保您的所有字符串都以 null 结尾,或使用 strncmp

您的代码说您创建了一个文件的原因是finalFileList 中的任何指针都不匹配initialFileList 中的任何指针。

【讨论】:

  • 我改成strcmp但结果完全一样...问题可能出在其他地方。我不知道为什么 finalList 中的指针在初始时没有任何匹配。我不会更改这些列表...
【解决方案2】:

您没有正确遵循上一个答案中的建议。你这样做了:

if(strcmp(finalFileList[i], initialFileList[j]))

而不是这个:

if(strcmp(finalFileList[i], initialFileList[j]) == 0)

请仔细阅读 strcmp() 的文档。如果两个字符串匹配,则返回零,而不是 1。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 2010-12-16
    • 2012-09-17
    • 2015-12-11
    • 2013-07-23
    • 1970-01-01
    • 2013-02-14
    相关资源
    最近更新 更多