inotify in android

来源:互联网 发布:怀化广电网络 编辑:程序博客网 时间:2024/05/17 00:09

android_util_FileObserver.cpp  uses inotify to notice the changes of appointed files , and report these changes to FileObserver.


android_os_fileobserver_init()  call inotify_init  to create an inotify instance.  a file descriptor is returned to the caller,

the caller will pass it  in android_os_fileobserver_startWatching, android_os_fileobserver_stopWatching etc. functions.



static jint android_os_fileobserver_startWatching(JNIEnv* env, jobject object, jint fd, jstring pathString, jint mask): 


The function call inotify _add_watch(fd, pathname, mask)  to  watch the inode pointed to by pathname.

inotify _add_watch returns a unique watch descriptor to the inode pointed by the pathname.

notes: the parameter fd is a file descriptor created by  android_os_fileobserver_init,  inotify instance.


static void android_os_fileobserver_stopWatching(JNIEnv* env, jobject object, jint fd, jint wfd):

the function call inotify_rm_watch to cancel a watch on the watch descriptor wfd in an inotify instance appointed by  fd .


static void android_os_fileobserver_observe(JNIEnv* env, jobject object, jint fd):


This function  enters into a dead loop, it  continously read event information from the  inotify instance appointed by fd.

The function do not return unless encounter an error when reading and the error is not EINTR.

after read events, the events is splited to single event, then call method_onEvent( it is a global variant be metioned later)  to send the event to FileObserver.



int register_android_os_FileObserver(JNIEnv* env):

this funtion will get method id of    "android/os/FileObserver$ObserverThread/onEvent",

and save the method id in method_onEvent,

and register all methods to androidruntime metioned above .

from what the function do , we should guess the function is called very early, 

in fact the function's address  is declared in array  gRegJNI[] (static const RegJNIRec ),

each gRegJNI's element  is called at androidRuntime's startup. (in function startreg)





原创粉丝点击