UNIX 文件信息

发布于 2024-11-01 14:52:59 字数 4048 浏览 2 评论 0原文

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

我的代码在这里:

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

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

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

预先感谢您的每一个帮助!

PS

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感受沵的脚步 2024-11-08 14:52:59

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

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 中的任何指针匹配。

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.

药祭#氼 2024-11-08 14:52:59

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

<代码>
if(strcmp(finalFileList[i],initialFileList[j]))

而不是这个:


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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文