linux開發 檔案監控 inotify

2021-04-20 21:33:47 字數 1600 閱讀 6250

從linux 2.6.13開始引入inotify來監控檔案,代替舊的dnotify。因此舊版本的glibc並不提供inotify系統呼叫的封裝。

標頭檔案 sys/inotify

int inotify_init()

建立乙個inotify例項,並返回乙個引用inotify例項檔案描述符,檔案描述符使用close()關閉

int inotify_add_watch(int fd, const char *pathname, uint32_t mask);

新增或修改乙個監控pathname的mask事件的watch到fd的watch列表中,也就是乙個inotify例項可監控多個目錄或檔案

fd: inotify例項的檔案描述符

pathname: 監控的路徑

mask: 監控的型別掩碼

一共有三類掩碼,檔案的事件,用來控制inotify_add_watch的掩碼,除了檔案事件read可能返回的事件

檔案事件

in_access

in_attrib

in_close_write

in_close_nowrite

in_create

in_delete

in_delete_self

in_modify

in_move_self

in_moved_from

in_moved_to

in_open

in_all_events

in_move = in_move_from | in_move_to

in_close = in_close_nowrite | in_close_write

新增watch是可以使用的mask

in_dont_follow

in_mask_add

in_oneshot

in_onlydir

可能被read返回的mask

in_ignored

in_isdir

in_q_overflow

in_unmount

返回值: 成功返回乙個非負整形,代表watch描述符,失敗返回-1並且設定errno

int inotify_rm_watch(int fd, uint32_t wd);

從inotify例項的watch列表中刪除watch

返回值: 成功 = 0 失敗 = -1

使用read監控檔案操作事件

read將返回乙個或多個struct inotify_event

struct inotify_event

當緩衝區過小時,返回值以來核心版本 2.6.21返回0,之後版本返回einval

inotify還有三個proc檔案系統介面,用來控制inotify消耗的核心記憶體,也可通過sysctl修改

/proc/sys/fs/inotify/max_queued_events 最大事件佇列

/proc/sys/fs/inotify/max_user_instances 使用者最多能建立inotify例項數

/proc/sys/fs/inotify/max_user_watches 每個例項可以擁有的watch數

windows phone開發 檔案操作

下面將分別介紹以上兩種方式如何來實現的檔案的訪問的 首先將乙個頁面進行如下布局 首先,建立資料夾,在進行檔案操作之前不要忘記了新增命名空間 private void button1 click object sender,routedeventargs e if storagefile.direct...

IOS開發 檔案路徑

1.開發平台路徑 developer platforms 此路徑下一般有三個目錄,分別是mac電腦 模擬器 iphone真機 macosx.platform iphonesimulator.platform iphoneos.platform 每個目錄下都有乙個 developer usr bin目...

程式開發檔案儲存相關

這類檔案當我們刪除應用之後,還應該保留在手機上的,例如拍照的 不應該隨著刪除應用而被刪除掉。對於這類檔案,android給我們提供了特定的目錄,這些目錄都是以directory開頭的,例如 directory music directory pictures.訪問這些資料夾有兩種方式 第一種 fil...