J'écris un programme par inotify pour vérifier le changement de fichier dans la boucle pour saisir toute modification à cela. mais je pense que cela peut être fait mieux. quelqu'un peut-il écrire ce code mieux?optimiser un programme avec inotify
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <linux/inotify.h>
#include <sys/select.h>
#define EVENT_SIZE (sizeof (struct inotify_event))
#define EVENT_BUF_LEN (1024 * (EVENT_SIZE + 16))
int event_check (int fd)
{
fd_set rfds;
FD_ZERO (&rfds);
FD_SET (fd, &rfds);
/* Wait until an event happens or we get interrupted
by a signal that we catch */
return select (FD_SETSIZE, &rfds, NULL, NULL, NULL);
}
int main()
{
int length, i = 0;
int fd;
int wd;
while(1){
i=0;
fd = inotify_init();
if (fd < 0) {
perror("inotify_init");
}
wd = inotify_add_watch(fd, "/tmp/test", IN_CLOSE_WRITE);
if (event_check (fd) > 0)
{
char buffer[EVENT_BUF_LEN];
int count = 0;
length = read(fd, buffer, EVENT_BUF_LEN);
if (length < 0) {
perror("read");
}
while (i < length) { struct inotify_event *event = (struct inotify_event *) &buffer[ i ];
printf("New file %s Editted.\n", event->name);
i += EVENT_SIZE + event->len;
}
}
}
inotify_rm_watch(fd, wd);
close(fd);
}
Pouvez-vous définir ce que vous voulez dire quand vous dites que vous voulez que le code soit meilleur? Mieux de quelles manières? –
Puis-je l'écrire mieux? Très probable. Y a-t-il quelque chose en particulier que tu voulais? –
Simplement réindenter le code correctement le rendrait "meilleur". – bstpierre