C写的代码来监控文件系统事件,按照一定格式输入,PC机端再解析:

android 本地代码如下:(编译成 /system/bin/inotify ,并记得push 到android phone中),

因为脚本是这么调用的: adb shell "/system/bin/inotify <file_you_wanna_monitor>"

当然编译出来的名字不一定要是inotify,但是你至少要保证两边一致:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdint.h>#include <fcntl.h>#include <unistd.h>#include <sys/ioctl.h>#include <sys/inotify.h>#include <errno.h>#include <cutils/properties.h>struct wd_name {        int wd;        char * name;};#define FNUM 4#define WD_NUM 1struct wd_name wd_array[WD_NUM];char * event_array[] = {        "File was accessed",        "File was modified",        "File attributes were changed",        "writtable file closed",        "Unwrittable file closed",        "File was opened",        "File was moved from X",        "File was moved to Y",        "Subfile was created",        "Subfile was deleted",        "Self was deleted",        "Self was moved",        "",        "Backing fs was unmounted",        "Event queued overflowed",        "File was ignored"};#define EVENT_NUM 16#define MAX_BUF_SIZE 1024#define MAX_PATH_LEN 255void main (int argc, char *argv[]){        int fd;        int wd;        char buffer[1024];        char * offset = NULL;        struct inotify_event * event;        int len, tmp_len;        char strbuf[16];        int i = 0;        int j=0;        if (argc!=2)        {                printf("error parameters , please try again.\n");                exit(-1);        }        fd = inotify_init();        if (fd < 0)         {                printf("Fail to initialize inotify.\n");                exit(-1);        }        for (i=0; i<WD_NUM; i++)         {            wd_array[i].name = argv[i+1];            wd = inotify_add_watch(fd, wd_array[i].name, IN_MOVED_TO | IN_CREATE|IN_ACCESS|IN_MODIFY|IN_ATTRIB |  \                                           IN_CLOSE_WRITE | IN_CLOSE_NOWRITE |IN_OPEN |IN_MOVED_FROM|IN_DELETE);            printf("i  wanna  add watch for %s to monitor.\n", wd_array[i].name);            if (wd < 0)            {                printf("Can't add watch for %s.\n", wd_array[i].name);                 exit(-1);            }             wd_array[i].wd = wd;          }        while(len = read(fd, buffer, MAX_BUF_SIZE))        {                offset = buffer;                event = (struct inotify_event *)buffer;                while (((char *)event - buffer) < len)                 {                        if (event->mask & IN_ISDIR)                         {                                memcpy(strbuf, "Direcotory", 11);                        }                        else                         {                                memcpy(strbuf, "File", 5);                        }                        for (i=0; i<WD_NUM; i++)                        {                                if (event->wd != wd_array[i].wd) continue;                                //printf("Object name: %s\n", wd_array[i].name);                                break;                        }                        for (j=0; j<EVENT_NUM; j++)                         {                                if (event_array[j][0] == '\0') continue;                                if (event->mask & (1<<j))                                 {                                        //printf("Event: %s\n", event_array[j]);                                        break;                                }                        }                        // added by yongming.i for keyword for monitor android filesystem       printf("inotifybegin:%s:%s:%s:%s\r\n",strbuf,event->name,wd_array[i].name,event_array[j]);                        tmp_len = sizeof(struct inotify_event) + event->len;                        event = (struct inotify_event *)(offset + tmp_len);                        offset += tmp_len;                }        }}


更多相关文章

  1. 记录我发现的第一个关于 Google 的 Bug
  2. 从源码角度理解HandlerThread和IntentService
  3. android ListView 异步加载图片
  4. Android(安卓)简易音乐动态相册(多种动画)
  5. 《Android(安卓)9编程通俗演义》代码下载
  6. android-轻松监听来电和去电
  7. android 一个应用去获取另一个应用assets下面的资源通过框架代码
  8. 学习深入理解android第一章
  9. Android遍历所有文件夹和子目录搜索文件

随机推荐

  1. 《GB18306-2015》PDF下载
  2. 《GB50555-2010》PDF下载
  3. 《GB/T50786-2012》PDF下载
  4. 《GB50411-2019》PDF下载
  5. 《GB/T50001-2017》PDF下载
  6. 《GB/T27947-2020》PDF下载
  7. JS与内存-初
  8. 0525实战作业
  9. 【记账小程序】收入和支出首页展示与数字
  10. 0601实战作业