TWRP MTP: make sure inotify_fd is set

Originally the inotify_init() call was part of a separate thread
which would occasionally allow the readDirs function to run and
start trying to add watches before inotify_fd was init'ed properly
and result in the add watch failing. This patch set relocates the
inotify_init call to happen outside and before starting the
separate thread to ensure that inotify_fd is set properly.

Change-Id: I8748c23473b60b57887df9d692834f3d2b249802
diff --git a/mtp/MtpStorage.cpp b/mtp/MtpStorage.cpp
index 821a788..319be09 100755
--- a/mtp/MtpStorage.cpp
+++ b/mtp/MtpStorage.cpp
@@ -53,6 +53,7 @@
 {
 	MTPI("MtpStorage id: %d path: %s\n", id, filePath);
 	inotify_thread = 0;
+	inotify_fd = -1;
 	sendEvents = false;
 	handleCurrentlySending = 0;
 	use_mutex = true;
@@ -127,9 +128,15 @@
 	mtpmap[0] = new Tree(0, 0, "");
 	MTPD("MtpStorage::createDB DONE\n");
 	if (use_mutex) {
-		MTPD("Starting inotify thread\n");
 		sendEvents = true;
-		inotify_thread = inotify();
+		MTPD("inotify_init\n");
+		inotify_fd = inotify_init();
+		if (inotify_fd < 0) {
+			MTPE("Can't run inotify_init for mtp server: %s\n", strerror(errno));
+		} else {
+			MTPD("Starting inotify thread\n");
+			inotify_thread = inotify();
+		}
 	} else {
 		MTPD("NOT starting inotify thread\n");
 	}
@@ -566,6 +573,10 @@
 }
 
 int MtpStorage::addInotify(Tree* tree) {
+	if (inotify_fd < 0) {
+		MTPE("inotify_fd not set or error: %i\n", inotify_fd);
+		return -1;
+	}
 	std::string path = getNodePath(tree);
 	MTPD("adding inotify for tree %x, dir: %s\n", tree, path.c_str());
 	int wd = inotify_add_watch(inotify_fd, path.c_str(), WATCH_FLAGS);
@@ -659,13 +670,7 @@
 	#define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16) )
 	char buf[EVENT_BUF_LEN];
 
-	MTPD("inotify thread: inotify_init\n");
-	inotify_fd = inotify_init();
-
-	if (inotify_fd < 0) {
-		MTPE("Can't run inotify for mtp server: %s\n", strerror(errno));
-		return -1;
-	}
+	MTPD("inotify thread starting.\n");
 
 	while (true) {
 		int i = 0;