监视新文件的目录只 [英] Monitor directory for new files only
本文介绍了监视新文件的目录只的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想监视新文件的目录从C应用程序。不过,我不感兴趣修改的文件,只有在新的文件。目前我使用READDIR / STAT用于这一目的:
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;
}
}
}
任何想法,我怎么能检测到新创建的文件 Linux和Solaris 10 没有保留的文件列表?
干杯,
马丁。
推荐答案
解决方案是上次访问时间存储在一个全局变量,并选择与过滤器的最新文件, SCANDIR()
:
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(常量结构的dirent ** lentry,常量结构的dirent ** rentry)
:
- 统计<$ C $ C>(* lentry) - GT; d_name (由路径扩展,但是这是一个细节只)
-
= LTIME statbuf.st_mtime;
- 统计<$ C $ C>(* rentry) - GT; d_name (由路径扩展,但是这是一个细节只)
-
RTIME = statbuf.st_mtime;
-
如果(LTIME&LT; RTIME)返回-1;
-
否则如果(LTIME&GT; RTIME)返回1;
-
返回0;
- Stat
(*lentry)->d_name
(extended by path, but that's a detail only) ltime = statbuf.st_mtime;
- Stat
(*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选择器(常量结构的dirent *进入)
:
- 统计<$ C $ C>入门&GT; d_name (由路径扩展,但是这是一个细节只)
- 如果不正常的文件则返回0
- 如果
stat.st_mtime&GT; lastseen
则返回1,否则返回0
- Stat
entry->d_name
(extended by path, but that's a detail only) - If not normal file then return 0
- If
stat.st_mtime > lastseen
then return 1 else return 0
主营:
- 初始化全局时间变量
lastseen
-
SCANDIR(目录,和放大器;条目,选择,cmp_mtime);
- 项的进程列表
- lastseen:最后一个条目的列表=的mtime
这篇关于监视新文件的目录只的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文