【发布时间】:2021-05-29 17:12:45
【问题描述】:
在这段代码中,我正在监视一个文件和一个目录。但是我有一个我想解决的问题,那就是要重命名哪个文件或目录的条件是什么?尽管IN_MODIFY 仅在修改文件文本时才适用于该文件。但我想监控文件或目录是否被重命名
是否可以使用inotify 或C?
int main(){
int length, i = 0, fd, wd;
char buffer[EVENT_BUF_LEN];
fd = inotify_init();
if (fd < 0){
perror( "inotify_init" );
}
wd = inotify_add_watch(fd, pathname1, IN_MODIFY);
length = read( fd, buffer, EVENT_BUF_LEN);
if(length < 0){
perror("read");
}
while(i < length){
struct inotify_event *event = ( struct inotify_event *)&buffer[i];
if(event->len){
if(event->mask & IN_MODIFY){
if(event->mask & IN_ISDIR){
if(event->wd == wd){
// if directory name changed. I don't know what condition to put
printf("The directory name '%s' is changed with %s\n", event->name);
}
}
else{
if(event->wd == wd){
// if file name changed. I don't know what condition to put
printf("The file name '%s' is changed with %s\n", event->name);
}
}
}
}
i += EVENT_SIZE + event->len;
}
inotify_rm_watch(fd, wd);
close(fd);
}
【问题讨论】: