日常應(yīng)用中艾杏,常常會(huì)遇到以下場(chǎng)景韧衣,監(jiān)控文件夾A,若文件夾中的B文件發(fā)生變化,則執(zhí)行C命令畅铭。Linux下可以通過inotify完成該功能氏淑。
自從Linux kernel 2.6.13起,inotify以作為內(nèi)核的一部份硕噩,同時(shí)需要glibc 2.4以上版本假残。
1. 相關(guān)函數(shù)
inotify_init() - 創(chuàng)建一個(gè)inotify實(shí)例
inotify_add_watch(int fd, const char *pathname, uint32_t mask) - 加入文件或目錄到inotify進(jìn)行監(jiān)測(cè)
inotify_rm_watch(int fd, int wd) - 移除一個(gè)watcher
2. 相關(guān)結(jié)構(gòu)
? ? ? ? struct inotify_event {
? ? ? ? ? ? ? int? ? ? wd;? ? ? /* Watch descriptor */
? ? ? ? ? ? ? uint32_t mask;? ? /* Mask of events */
? ? ? ? ? ? ? uint32_t cookie;? /* Unique cookie associating related
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? events (for rename(2)) */
? ? ? ? ? ? ? uint32_t len;? ? ? /* Size of name field */
? ? ? ? ? ? ? char? ? name[];? /* Optional null-terminated name */
? ? ? ? ? };
3. Mask
適用于 inotify_add_watch mask 與 read 返回的inotify_event中mask
IN_ACCESS文件被訪問
IN_ATTRIB文件屬性發(fā)生變化
IN_CLOSE_WRITE以write方式打開文件并關(guān)閉
IN_CLOSE_NOWRITE以非write方式打開文件并關(guān)閉
IN_CREATE文件或目錄被創(chuàng)建
IN_DELETE文件或目錄被刪除(被監(jiān)測(cè)的文件夾A中B文件被刪除)
IN_DELETE_SELF被監(jiān)測(cè)的文件或目錄被刪除(被監(jiān)測(cè)的文件夾A被刪除)
IN_MODIFY文件被修改
IN_MOVE_SELF被監(jiān)測(cè)的文件或目錄移動(dòng)
IN_MOVED_FROM文件移出被監(jiān)測(cè)的目錄
IN_MOVED_TO文件移入被監(jiān)測(cè)的目錄
IN_OPEN文件被打開
上述flag的集合
IN_ALL_EVENTS以上所有flag的集合
IN_MOVEIN_MOVED_TO|IN_MOVED_FROM
IN_CLOSEIN_CLOSE_WRITE|IN_CLOSE_NOWRITE
不常用的flag
IN_DONT_FOLLOW不follow符號(hào)鏈接 (since 2.6.15)
IN_EXCL_UNLINK當(dāng)文件從監(jiān)測(cè)目中unlink后,則不再報(bào)告該文件的相關(guān)event榴徐,比如監(jiān)控/tmp使用 (since 2.6.36)
IN_MASK_ADD追打MASK到被監(jiān)測(cè)的pathname
IN_ONESHOT只監(jiān)測(cè)一次
IN_ONLYDIR只監(jiān)測(cè)目錄
僅由read返回
IN_IGNOREDinotify_rm_watch守问,文件被刪除或者文件系統(tǒng)被umount
IN_ISDIR發(fā)生事件的是一個(gè)目錄
IN_Q_OVERFLOWEvent隊(duì)列溢出
IN_UNMOUNT文件系統(tǒng)unmount
4. 例子
用途:監(jiān)測(cè)指定文件或目錄(或不指定則為當(dāng)前目錄)的一切動(dòng)作。
使用:inotify [文件或目錄]
[cpp]?view plain?copy
#include???
#include???
#include???
#include???
#include???
#include???
#include???
#define?ERROR(text)?error(1,?errno,?"%s",?text)??
struct?EventMask?{??
int????????flag;??
const?char?*name;??
};??
int?freadsome(void?*dest,?size_t?remain,?FILE?*file)??
{??
char?*offset?=?(char*)dest;??
while?(remain)?{??
int?n?=?fread(offset,?1,?remain,?file);??
if?(n?==?0)?{??
return?-1;??
????????}??
????????remain?-=?n;??
????????offset?+=?n;??
????}??
return?0;??
}??
//http://www.ibm.com/developerworks/cn/linux/l-inotify/??
//http://www.jiangmiao.org/blog/2179.html??
int?main(int?argc,?char?*argv[])??
{??
const?char?*target;??
if?(argc?==?1)?{??
target?=".";??
????}??
else?{??
????????target?=?argv[1];??
????}??
struct?EventMask?event_masks[]?=?{??
{?IN_ACCESS,"IN_ACCESS"?},????
{?IN_ATTRIB,"IN_ATTRIB"?},????
{?IN_CLOSE_WRITE,"IN_CLOSE_WRITE"?},????
{?IN_CLOSE_NOWRITE,"IN_CLOSE_NOWRITE"?},????
{?IN_CREATE,"IN_CREATE"?},????
{?IN_DELETE,"IN_DELETE"?},????
{?IN_DELETE_SELF,"IN_DELETE_SELF"?},????
{?IN_MODIFY,"IN_MODIFY"?},????
{?IN_MOVE_SELF,"IN_MOVE_SELF"?},????
{?IN_MOVED_FROM,"IN_MOVED_FROM"?},????
{?IN_MOVED_TO,"IN_MOVED_TO"?},????
{?IN_OPEN,"IN_OPEN"?},????
{?IN_DONT_FOLLOW,"IN_DONT_FOLLOW"?},????
//{?IN_EXCL_UNLINK,?"IN_EXCL_UNLINK"?},????
{?IN_MASK_ADD,"IN_MASK_ADD"?},????
{?IN_ONESHOT,"IN_ONESHOT"?},????
{?IN_ONLYDIR,"IN_ONLYDIR"?},????
{?IN_IGNORED,"IN_IGNORED"?},????
{?IN_ISDIR,"IN_ISDIR"?},????
{?IN_Q_OVERFLOW,"IN_Q_OVERFLOW"?},????
{?IN_UNMOUNT,"IN_UNMOUNT"?},????
????};??
int?monitor?=?inotify_init();??
if?(-1?==?monitor)?{??
ERROR("monitor");??
????}??
int?watcher?=?inotify_add_watch(monitor,?target,?IN_ALL_EVENTS);??
if?(-1?==?watcher)?{??
ERROR("inotify_add_watch?error");??
????}??
FILE?*monitor_file?=?fdopen(monitor,?"r");??
char?last_name[1024];??
char?name[1024];??
/*?event:inotify_event?->?name:char[event.len]?*/??
while?(true)?{??
struct?inotify_event?event;??
if?(-1?==?freadsome(&event,?sizeof(event),?monitor_file))?{??
ERROR("freadsome?error");??
????????}??
if?(event.len)?{??
????????????freadsome(name,?event.len,?monitor_file);??
????????}??
else?{??
sprintf(name,"FD:?%d\n",?event.wd);??
????????}??
if?(strcmp(name,?last_name)?!=?0)?{??
????????????puts(name);??
????????????strcpy(last_name,?name);??
????????}??
/*?顯示event的mask的含義?*/??
int?i?=?0;??
for?(i?=?0;?i?<?sizeof(event_masks)?/?sizeof(struct?EventMask);?++i)?{??
if?(event.mask?&?event_masks[i].flag)?{??
printf("\t%s\n",?event_masks[i].name);??
????????????}??
????????}??
????}??
return?0;??
}??
Linux系統(tǒng)中提供了一套intotify的機(jī)制來監(jiān)視文件系統(tǒng)的事件坑资,比如創(chuàng)建文件耗帕,打開,關(guān)閉等等袱贮。利用這一機(jī)制仿便,我們可以很容易寫出監(jiān)控目錄變化的工具。更何況更有了一個(gè)inotify_tools的開源工程呢攒巍。inotify_tools對(duì)inotify的API做了進(jìn)一步的封裝嗽仪,更加方便使用。
下面的例子來源于inotify_tools的示例代碼柒莉,但是做了一些改進(jìn)闻坚。inotify_tools監(jiān)視一個(gè)目錄時(shí),可以得到該目錄變化的信息兢孝。但是在該目錄下窿凤,創(chuàng)建一個(gè)子目錄后,子目錄中的變化跨蟹,inotify_tools不能獲取雳殊。所以我添加了動(dòng)態(tài)監(jiān)控目錄的代碼。
#include?
#include?
#include?
#include?
enum?{
ACTION_NULL_WD,
ACTION_ADD_WD,
ACTION_DEL_WD,
};
int?main(int?argc,?const?char?**argv)
{
int?err?=?0;
if?(!inotifytools_initialize())?{
printf("inotifytools_initialize failedn");
goto?error;
}
inotifytools_initialize_stats();
const?char?*monitor_path?=?".";
if?(argc?>?1)?{
monitor_path?=?argv[1];
}
printf("Monitor dir(%s)n",?monitor_path);
if?(!inotifytools_watch_recursively(monitor_path,?IN_ALL_EVENTS))?{
printf("inotifytools_watch_recursively failedn");
goto?error;
}
inotifytools_set_printf_timefmt("%F %T");
struct inotify_event?*?event?=?inotifytools_next_event(-1);
char path[256];
while?(event)?{
inotifytools_printf(?event,?"%T %w%f %en"?);
if?(IN_ISDIR&event->mask)?{
int?action?=?ACTION_NULL_WD;
if?((IN_DELETE|IN_DELETE_SELF|IN_MOVED_FROM)&event->mask)?{
action?=?ACTION_DEL_WD;
snprintf(path,?sizeof(path),?"%s%s",
inotifytools_filename_from_wd(event->wd),
event->name);
printf("Remove path(%s) from wdn",?path);
}?else?if?(((IN_CREATE|IN_MOVED_TO)&event->mask)?&&?(IN_ISDIR&event->mask))?{
action?=?ACTION_ADD_WD;
snprintf(path,?sizeof(path),?"%s%s",
inotifytools_filename_from_wd(event->wd),
event->name);
printf("Add path(%s) into wdn",?path);
}
if?(ACTION_ADD_WD?==?action)?{
if?(!inotifytools_watch_recursively(path,?IN_ALL_EVENTS))?{
printf("inotifytools_watch_recursively failedn");
goto?error;
}
}?else?if?(ACTION_DEL_WD?==?action)?{
if?(!inotifytools_remove_watch_by_wd(event->wd))?{
printf("inotifytools_remove_watch_by_wd failed. event->wd(%d)n",?event->wd);
goto?error;
}
}
}
event?=?inotifytools_next_event(-1);
}
printf("Exitn");
return 0;
error:
err?=?inotifytools_error();
printf("Error(%d)n",?err);
return?-1;
}
代碼很簡(jiǎn)單窗轩。就是在獲得事件以后夯秃,檢查是否為目錄。如果是目錄痢艺,則需要進(jìn)行動(dòng)態(tài)監(jiān)控的檢查仓洼。如果是創(chuàng)建動(dòng)作,那么就添加新的監(jiān)控堤舒。如果是刪除動(dòng)作色建,就去掉已有的監(jiān)控wd。