开发者

UNIX file information

开发者 https://www.devze.com 2023-02-27 03:19 出处:网络
I\'m trying to make a program that monitors one folder, and checks if any files within had their content or permissions modified.

I'm trying to make a program that monitors one folder, and checks if any files within had their content or permissions modified.

My code is here:

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

I know that this code does not have good performance, but for now, what I want is for it to work. I have one other function that obtains files names and stats inside that folder, but that function is working properly.

When I try to delete one folder whose name is lower (alphabetical order) than some other file inside that folder, program tells me that I have created one file, instead of deleting it. I'm suspicious that this could be a problem of indexes, but I don't know where it is.

Thanks in advance for every help!

P.S.

The other function, that obtains file name and stats. Is something wrong here?

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


You can't compare strings like this:

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

Use strcmp instead:

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

Just be sure all of your strings are null terminated, or use strncmp.

The reason your code says you created a file is that none of the pointers in the finalFileList match any of the pointers in the initialFileList.


You did not follow the advice in the previous answer correctly. You did this:

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

instead of this:

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

Please read documentation for strcmp() carefully. If the two strings match, a zero is returned, not 1.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号