LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-19-2006, 10:55 AM   #1
bujecas
Member
 
Registered: Oct 2004
Location: Portugal
Distribution: Debian, Slackware
Posts: 78

Rep: Reputation: 20
using select with directory descriptor


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.
 
Old 06-19-2006, 04:02 PM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
First off: why doesn't your solution work? That's actually quite simple: what select does is see if there's any input available. But since you have the directory on the disk, there's always input available.

What you want to test instead is whether or not the date of last change is altered.
Demo:
Code:
$ ls -ld .
drwxr-xr-x 84 jonas jonas 7400 2006-05-18 13:43 .
$ mkdir non-existent
$ ls -ld .
drwxr-xr-x 84 jonas jonas 7400 2006-06-19 22:55 .
If you want to do that from C, look at stat(2).
 
Old 06-20-2006, 10:47 AM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.
Think about `dnotify' and `inotify'. dnotify stands for 'directory notify' -- if directory contents changed, you will get a signal. Look at linux-2.N.M/Documentation/dnotify.txt (kernel sources).
inotify -- another file change notification system. It's newer than dnotify and "therefore" -- better
Unfortunately this stuff don't allow you to determine which process is changing filesystem. I try to solve such problem with `ptrace' -- debugging system call. In another points of view /[d|i]notify/ is the best solution I think.

Good luck.

Last edited by firstfire; 06-20-2006 at 10:49 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
No longer able to select files from my user home directory oily_rags SUSE / openSUSE 4 11-06-2005 01:39 PM
ShellScrpit : select the last directory paraiso Linux - Newbie 2 05-10-2005 04:37 PM
gtk directory select MylesCLin Programming 1 12-16-2004 05:54 PM
Using Select to make a simple directory menu why doesn't it work ctrimble Programming 5 06-07-2004 01:21 AM
Redhat crash - boot directory empty "dub2 bad descriptor" errors jjohnston62 Red Hat 3 11-14-2003 05:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration