Hello,
I'm trying to write a program that is aware of directory contents changes. I'm using a select in the directory descriptor obtained by dirfd function. My code is something like this:
Code:
int main()
{
fd_set rset;
struct timeval tm;
int fd, ret;
DIR *dfd;
dfd = opendir("directory");
fd = dirfd(dfd);
while(1) {
tm.tv_sec = 1;
tm.tv_usec = 0;
FD_ZERO(&rset);
FD_SET(fd, &rset);
ret = select(fd+1, &rset, NULL, NULL, &tm);
if(ret>0)
printf("Directory contents changed");
}
return 0;
}
My idea is: when I add a file to the directory, the select will detect and do something but with this code the fd is always true even without adding any file to the directory.
Any ideas? Thanks.