仅监视目录中的新文件

发布于 2024-09-30 18:37:25 字数 741 浏览 11 评论 0原文

我想监视 C 应用程序中新文件的目录。但是,我对修改的文件不感兴趣,只对新文件感兴趣。目前我正在使用 readdir/stat 来实现此目的:

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;
    }
  }
}

知道如何在不保留文件列表的情况下检测 Linux AND Solaris 10 上新创建的文件吗?

干杯,

马丁。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

九厘米的零° 2024-10-07 18:37:25

gamin 为许多 *nixes 提供了围绕系统相关文件通知 api 的抽象,它包含在许多 Linux 发行版默认情况下。

对于 Linux,您可以使用 Linux 特定的 inotify API。

Win32 通过 FindFirstChangeNotification 有类似的 API

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

開玄 2024-10-07 18:37:25

解决方案是将上次访问时间存储在全局变量中,并使用 scandir() 过滤器选择最新文件:

int cmp_mtime( const struct dirent** lentry, const struct dirent* *rentry ):

  1. Stat (*lentry)->d_name (通过路径扩展,但这只是一个细节)
  2. ltime = statbuf.st_mtime;
  3. Stat < code>(*rentry)->d_name (通过路径扩展,但这只是一个细节)
  4. rtime = statbuf.st_mtime;
  5. if ( ltime < rtime ) return - 1;
  6. else if ( ltime > rtime ) return 1;
  7. return 0;

int 选择器( const struct dirent*entry ) :

  1. Stat entry->d_name (通过路径扩展,但这只是一个细节)
  2. 如果不是普通文件则返回 0
  3. 如果 stat.st_mtime > lastseen then return 1 else return 0

Main:

  1. 初始化全局时间变量 lastseen
  2. scandir(directory, &entries,selector, cmp_mtime );
  3. 处理条目列表
  4. lastseen := 列表中最后一个条目的 mtime

The solution is to store the last access time in a global variable and pick the latest files with a filter to scandir():

int cmp_mtime( const struct dirent** lentry, const struct dirent** rentry ):

  1. Stat (*lentry)->d_name (extended by path, but that's a detail only)
  2. ltime = statbuf.st_mtime;
  3. Stat (*rentry)->d_name (extended by path, but that's a detail only)
  4. rtime = statbuf.st_mtime;
  5. if ( ltime < rtime ) return -1;
  6. else if ( ltime > rtime ) return 1;
  7. return 0;

int selector( const struct dirent* entry ):

  1. Stat entry->d_name (extended by path, but that's a detail only)
  2. If not normal file then return 0
  3. If stat.st_mtime > lastseen then return 1 else return 0

Main:

  1. Init global time variable lastseen
  2. scandir( directory, &entries, selector, cmp_mtime );
  3. Process list of entries
  4. lastseen := mtime of last entry in list
硬不硬你别怂 2024-10-07 18:37:25

Solaris 10 可能没有更好的方法来与 dtrace 命令或 libdtrace 进行外部连接(不推荐)。在基于 SunOS 5.11 的操作系统(例如:OpenSolaris、Solaris 11 Express...)上,您只需使用 文件事件通知框架

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.

回梦 2024-10-07 18:37:25

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

和我恋爱吧 2024-10-07 18:37:25

您可以使用FAM - 文件更改监视器来实现此目的。

You could use FAM - File Alteration Monitor for this.

孤者何惧 2024-10-07 18:37:25

一种技巧可能是为已处理的文件设置存档位。

伊迪丝说:
如果其他答案没有任何帮助,您可以使用 chmod() 代替。

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.

泪痕残 2024-10-07 18:37:25

正在研究 Solaris 上的同一主题,并找到了 watchdir 应用程序的示例,该应用程序可在脚本中使用,其中

watchdir /foo/bar

将执行阻塞等待,直到监视目录上发生某些情况。

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

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/

神魇的王 2024-10-07 18:37:25

我知道您正在寻求 C 的解决方案,但实际上 Java(现在)为此提供了一个跨平台类。您可以在此处阅读更多相关信息。另请参阅 WatchService 类 的文档这是Java文件更改通知能力的中心类。

也许有一些文档说明 Java 如何在各种平台上实现这一点?

据我所知,Java 在 Linux、Solaris 和 Windows 上使用操作系统的本机文件更改通知 API,此外还实现了一种基于轮询的回退方法,该方法可以在任何平台上工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文