仅监视目录中的新文件
我想监视 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
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
解决方案是将上次访问时间存储在全局变量中,并使用
scandir()
过滤器选择最新文件:int cmp_mtime( const struct dirent** lentry, const struct dirent* *rentry )
:(*lentry)->d_name
(通过路径扩展,但这只是一个细节)ltime = statbuf.st_mtime;
rtime = statbuf.st_mtime;
if ( ltime < rtime ) return - 1;
else if ( ltime > rtime ) return 1;
return 0;
int 选择器( const struct dirent*entry )
:entry->d_name
(通过路径扩展,但这只是一个细节)stat.st_mtime > lastseen
then return 1 else return 0Main:
lastseen
scandir(directory, &entries,selector, cmp_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 )
:(*lentry)->d_name
(extended by path, but that's a detail only)ltime = statbuf.st_mtime;
(*rentry)->d_name
(extended by path, but that's a detail only)rtime = statbuf.st_mtime;
if ( ltime < rtime ) return -1;
else if ( ltime > rtime ) return 1;
return 0;
int selector( const struct dirent* entry )
:entry->d_name
(extended by path, but that's a detail only)stat.st_mtime > lastseen
then return 1 else return 0Main:
lastseen
scandir( directory, &entries, selector, cmp_mtime );
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.
在 MacOS X 上有一个文件监控 API 和提供的示例代码显示了如何查找哪些文件已更改。
On MacOS X there is a file monitoring API and the provided sample code shows how to find which files have changed.
您可以使用FAM - 文件更改监视器来实现此目的。
You could use FAM - File Alteration Monitor for this.
一种技巧可能是为已处理的文件设置存档位。
伊迪丝说:
如果其他答案没有任何帮助,您可以使用 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.
正在研究 Solaris 上的同一主题,并找到了
watchdir
应用程序的示例,该应用程序可在脚本中使用,其中将执行阻塞等待,直到监视目录上发生某些情况。
链接: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 inwhich will perform a blocking wait until something happens on the watched directory.
Link: https://solarisrants.wordpress.com/2013/07/24/solaris-file-event-notification/
我知道您正在寻求 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.