Monitor directory for new files only

2019-04-12 20:13发布

I'd like to monitor a directory for new files from a C app. However, I'm not interested in modified files, only in new files. Currently I'm using readdir/stat for that purpose:

while ( (ent = readdir(dir)) != NULL ) {
  strcpy(path, mon_dir);
  strcat(path, "/");
  strcat(path, ent->d_name);
  if ( stat(path, &statbuf) == -1 ) {
    printf( "Can't stat %s\n", ent->d_name );
    continue;
  }
  if ( S_ISREG(statbuf.st_mode) ) {
    if ( statbuf.st_mtime > *timestamp ) {
      tcomp = localtime( &statbuf.st_mtime );
      strftime( s_date, sizeof(s_date), "%Y%m%d %H:%M:%S", tcomp );
      printf( "%s %s was added\n", s_date, ent->d_name );
      *timestamp = statbuf.st_mtime;
    }
  }
}

Any idea how I can detect newly created files on Linux AND Solaris 10 without keeping a list of files?

Cheers,

Martin.

8条回答
forever°为你锁心
2楼-- · 2019-04-12 20:32

gamin provides an abstraction around system dependant file notification apis for many *nixes , and it's included in many linux distros by default.

For linux, you could use the linux specific inotify api.

Win32 has a similar API via FindFirstChangeNotification

查看更多
Viruses.
3楼-- · 2019-04-12 20:33

On MacOS X there is a file monitoring API and the provided sample code shows how to find which files have changed.

查看更多
地球回转人心会变
4楼-- · 2019-04-12 20:36

There is probably no better way with Solaris 10 outside interfacing with either the dtrace command or libdtrace (not recommended). On SunOS 5.11 based OSes (eg: OpenSolaris, Solaris 11 Express, ...), you can just use the File Event Notification framework.

查看更多
贼婆χ
5楼-- · 2019-04-12 20:38

I know you were asking for a solution from C but in fact Java (now) has a cross-platform class for this. You can read more about it here. Also see documentation for the WatchService class which is the central class of Java's file change notification ability.

Perhaps there's some documentation somewhere as to how Java accomplishes this on the various platforms ?

From the little I understand Java uses the OS's native file change notification API on Linux, Solaris and Windows and then in addition also implements a fallback method which is based on polling and which will work on any platform.

查看更多
ゆ 、 Hurt°
6楼-- · 2019-04-12 20:42

one trick may be to set the archive bit for handled files.

Edith says: if nothing else from the other answeres helpes, you may play with chmod() instead.

查看更多
霸刀☆藐视天下
7楼-- · 2019-04-12 20:45

Was researching the same topic on Solaris and found the example of the watchdir app, to be used from within scripts, as in

watchdir /foo/bar

which will perform a blocking wait until something happens on the watched directory.

Link: https://solarisrants.wordpress.com/2013/07/24/solaris-file-event-notification/

查看更多
登录 后发表回答