日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

inotify监听文件夹的变动

發布時間:2024/4/14 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 inotify监听文件夹的变动 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
inotify只能監控單層目錄變化,不能監控子目錄中的變化情況。
如果需要監控子目錄,需要在調用inotify_add_watch(int fd, char *dir, int mask):int建立監控時,遞歸建立子目錄的監控,偽代碼如下
void addwatch(int fd, char *dir, int mask)
{
wd = inotify_add_watch(fd, dir, mask);
向目錄集合加入(wd, dir);
for (dir下所有的子目錄subdir)
addwatch(fd, subdir, mask);
}
這樣就可以得到一個目錄集合,其中每一個wd對應一個子目錄。
當你調用read獲取信息時,可以得到一個下面的結構體
struct inotify_event
{
int wd; /* Watch descriptor. */
uint32_t mask; /* Watch mask. */
uint32_t cookie; /* Cookie to synchronize two events. */
uint32_t len; /* Length (including NULs) of name. */
char name __flexarr; /* Name. */
};
其中,通過event->wd和剛才記錄的目錄集合可以知道變動的具體子目錄。
event->name為具體的文件名稱。
event->name是一個char name[0]形式的樁指針,具體的name占據的長度可以由event->len得出

我的監控部分代碼如下:
enum {EVENT_SIZE = sizeof(struct inotify_event)};
enum {BUF_SIZE = (EVENT_SIZE + 16) << 10};
void watch_mon(int fd)
{
int i, length;
void *buf;
struct inotify_event *event;
buf = malloc(BUF_SIZE);

while ((length = read(fd, buf, BUF_SIZE)) >= 0)
{
i = 0;
while (i < length)
{
event = buf + i;
if (event->len)
具體處理函數(event);
i += EVENT_SIZE + event->len;
}
}
close(fd);
exit(1);
}
在你的具體處理函數中,通過wd辨識子目錄,通過name辨識文件

這是利用C++STLmap寫的一個范例,可以監視當前目錄下(含子目錄)的變化,創建,刪除過程(新建立的目錄不能監視,只能通過監視到創建新目錄的事件后重新初始化監視表)
新版1.1.0,可以監視創建的子目錄,方法是,當do_action探測到新目錄創建的動作時,調用inotify_add_watch追加新的監視
/*
Copyright (C) 2010-2011 LIU An (SuperHacker@china.com.cn)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/inotify.h>
#include <errno.h>
#include <dirent.h>

#include <map>
#include <string>
using namespace std;

void addwatch(int, char*, int);
static int filter_action(uint32_t mask);
int watch_init(int mask, char *root);
void addwatch(int fd, char *dir, int mask);
static void do_action(int fd, struct inotify_event *event);
void watch_mon(int fd);
static void send_mess(char *name, char *act, int ewd);
void append_dir(int fd, struct inotify_event *event, int mask);

map<int, string> dirset;

enum{MASK = IN_MODIFY | IN_CREATE | IN_DELETE};

int main(int argc, char **argv)
{
int fd;
if (argc != 2)
{
fprintf(stderr, "Usage: %s dir\n", argv[0]);
exit(1);
}

fd = watch_init(MASK, argv[1]);
watch_mon(fd);

return 0;
}

int watch_init(int mask, char *root)
{
int i, fd;

if ((fd = inotify_init()) < 0)
perror("inotify_init");
addwatch(fd, root, mask);
return fd;
}

void addwatch(int fd, char *dir, int mask)
{
int wd;
char subdir[512];
DIR *odir;
struct dirent *dent;

if ((odir = opendir(dir)) == NULL)
{
perror("fail to open root dir");
exit(1);
}
wd = inotify_add_watch(fd, dir, mask);
dirset.insert(make_pair(wd, string(dir)));

errno = 0;
while ((dent = readdir(odir)) != NULL)
{
if (strcmp(dent->d_name, ".") == 0
|| strcmp(dent->d_name, "..") == 0)
continue;
if (dent->d_type == DT_DIR)
{
sprintf(subdir, "%s/%s", dir, dent->d_name);
addwatch(fd, subdir, mask);
}
}

if (errno != 0)
{
perror("fail to read dir");
exit(1);
}

closedir (odir);
}

enum {EVENT_SIZE = sizeof(struct inotify_event)};
enum {BUF_SIZE = (EVENT_SIZE + 16) << 10};

void watch_mon(int fd)
{
int i, length;
void *buf;
struct inotify_event *event;
buf = malloc(BUF_SIZE);

while ((length = read(fd, buf, BUF_SIZE)) >= 0)
{
i = 0;
while (i < length)
{
event = (struct inotify_event*)(buf + i);
if (event->len)
do_action(fd, event);
i += EVENT_SIZE + event->len;
}
}
close(fd);
exit(1);
}

static char action[][10] =
{
"modified",
"accessed",
"created",
"removed"
};

enum{NEWDIR = IN_CREATE | IN_ISDIR};

static void do_action(int fd, struct inotify_event *event)
{
int ia, i;

if ((ia = filter_action(event->mask)) < 0)
return;

if ((event->mask & NEWDIR) == NEWDIR)
append_dir(fd, event, MASK);

send_mess(event->name, action[ia], event->wd);
}

void append_dir(int fd, struct inotify_event *event, int mask)
{
char ndir[512];
int wd;

sprintf(ndir, "%s/%s", dirset.find(event->wd)->second.c_str(),
event->name);
wd = inotify_add_watch(fd, ndir, mask);
dirset.insert(make_pair(wd, string(ndir)));
}

static int filter_action(uint32_t mask)
{
if (mask & IN_MODIFY)
return 0;
if (mask & IN_ACCESS)
return 1;
if (mask & IN_CREATE)
return 2;
if (mask & IN_DELETE)
return 3;
return -1;
}

static void send_mess(char *name, char *act, int ewd)
{
char format[] = "%s was %s.\n";
char file[512];

sprintf(file, "%s/%s", dirset.find(ewd)->second.c_str(), name);

printf(format, file, act);
}

轉載于:https://www.cnblogs.com/keepsimple/p/3416598.html

總結

以上是生活随笔為你收集整理的inotify监听文件夹的变动的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲精品久久久蜜桃网尤妮丝 | 黑人大群体交免费视频 | 黄色录像毛片 | 亚洲97视频| 成年人免费网站视频 | 91精品国产综合久久香蕉922 | 成人免费影视网站 | 99国产精品久久久久久久成人热 | 亚洲国产97在线精品一区 | 亚洲黄色影视 | 视频在线国产 | 在线不卡av电影 | 日韩aa视频 | aaa成人 | 欧美少妇xxxxx| av资源在线 | 日本一区三区 | 亚欧在线播放 | 欧美日韩专区 | 日韩网红少妇无码视频香港 | 校花被c到呻吟求饶 | 亚洲av综合av一区二区三区 | 黄色片子看看 | 成人精品久久久午夜福利 | www.男女| 性色欲网站人妻丰满中文久久不卡 | 少妇流白浆| 蜜桃精品成人影片 | 精品日韩久久 | 免费人成视频在线 | www.色综合| 999色综合 | 动漫美女被吸乳奶动漫视频 | 久久久久久久99 | 99久久久精品 | 国产做爰xxxⅹ久久久精华液 | 超碰人人国产 | 91九色精品 | 午夜寂寞视频 | 国产精品二区一区二区aⅴ 一卡二卡三卡在线观看 | 理想之城连续剧40集免费播放 | 中文 欧美 日韩 | 亚洲先锋影音 | 美国少妇在线观看免费 | 中国精品久久 | 中文字幕日韩在线播放 | 国内自拍青青草 | 精品国产a线一区二区三区东京热 | 国产在线观看一区 | 久久婷香| 国产老女人乱淫免费 | 成人在线综合网 | 可以免费看的黄色 | 国产成人一区二区 | www.99热| 日本涩涩网| 日韩不卡一二三 | 亚洲永久无码精品一区二区 | 夜夜久久久 | 国产成人77亚洲精品www | 激情综合五月 | 91射| 久久性爱视频网站 | 亚洲国产片| 欧美在线视频一区二区 | 日韩精品视频在线 | 亚洲成人欧美 | 琪琪av在线| 欧美三p | 欧美午夜在线观看 | 日本护士取精xxxxxhd | 日本不卡一二三 | 波兰性xxxxx极品hd | 国产成人综合自拍 | 国产伦精品一区二区三 | 亚洲精品久久久乳夜夜欧美 | 爱情岛亚洲首页论坛 | 免费激情av | 麻豆蜜桃在线观看 | 欧美激情中文字幕 | 老妇女玩小男生毛片 | 色又色| 九九热在线免费观看 | 久在线观看视频 | 日韩欧美精品久久 | 亚洲成年人 | 1024精品一区二区三区日韩 | 夜夜爽日日澡人人添 | 91麻豆精品视频 | 欧美成人久久久免费播放 | 男人的亚洲天堂 | 午夜福利电影一区二区 | 国产精品一区二区久久久 | 亚洲精品区 | 深夜在线| 久久精品免费一区二区 | 成人在线激情 | 国产免费无码一区二区 | 日韩经典在线观看 |