Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android FileObserver passing undocumented event 32768

Tags:

android

I am using FileObserver to observe a directory for changes. The process works fine 90% of the time, but occasionally it fails.

Here is a working example of logcat:

04-23 21:12:03.873: V/ItemObserver(1663): Setting up new item observer for item 2
04-23 21:12:04.374: I/ItemObserver(1663): Received item event for item 2, event: 256, file: batch.get.47
04-23 21:12:07.866: I/ItemObserver(1663): Received item event for item 2, event: 512, file: batch.get.47
04-23 21:12:07.873: I/ItemObserver(1663): Received item event for item 2, event: 512, file: item.xml
04-23 21:12:07.883: I/ItemObserver(1663): Received item event for item 2, event: 256, file: item.xml
04-23 21:12:08.033: I/ItemObserver(1663): Received item event for item 2, event: 8, file: item.xml

Here is a failed example:

04-23 22:08:09.403: V/ItemObserver(1751): Setting up new item observer for item 2
04-23 22:08:09.813: I/ItemObserver(1751): Received item event for item 2, event: 256, file: batch.get.52
04-23 22:08:09.954: I/ItemObserver(1751): Received item event for item 2, event: 32768, file: null

Once I get the 32768 event with a null file, everything stops. I've checked the source for FileObserver and searched for inotify 32768 and can't find where this is referenced anywhere.

The code to set up the observer is as follows:

itemDirObserver = new FileObserver(getItemsCache().getProcessedItemDir(itemId).getPath(), 
FileObserver.CLOSE_WRITE | FileObserver.CREATE | FileObserver.DELETE) {
  @Override
  public void onEvent(int event, final String file) {
    itemDirChanged(event, file);
  }
};
itemDirObserver.startWatching();

The code for the logcat is:

public synchronized void itemDirChanged(int event, String file) {
  Log.i(LOG, "Received item event for item " + itemId + ", event: " + event + ", file: " + file);
  switch (event) {
<snip>

Any idea what 32768 and null file signifies?

like image 558
mvsjes2 Avatar asked Apr 24 '12 19:04

mvsjes2


3 Answers

Thanks to this answer.

The event codes are listed here.

32768 in particular is this:

#define IN_IGNORED 0x00008000 /* File was ignored */

like image 123
dokkaebi Avatar answered Nov 15 '22 17:11

dokkaebi


I too was suffering from occasional failures.

I discovered a big gotcha with the Android FileObserver: you mustn't have two FileObservers watching the same folder in your application.

If you call StopWatching on one FileObserver, any other FileObserver that is watching the same folder will also stop watching.

like image 32
Andrew Shepherd Avatar answered Nov 15 '22 17:11

Andrew Shepherd


I had the problem that my FileObserver gets 32768 event and stops working. I desperately tried to understand how to fix this (without recreating the FileObserver) for a couple of days.

First I found that although I have a hard reference to my FileObserver this event (32768) can be triggered by garbage collection (when I forced it through DDMS).

Eventually I found that there was another FileObserver in my program to the same folder. As soon as I deleted it everything started working.

Do anybody knows if it is legal to have several observers to the same directory? I couldn't find any information about it

like image 27
Eugene Avatar answered Nov 15 '22 18:11

Eugene