Factoring out code from (lms_process's) lms_process().
authorGustavo Lima Chaves <glima@profusion.mobi>
Mon, 15 Sep 2008 22:11:47 +0000 (19:11 -0300)
committerGustavo Lima Chaves <glima@profusion.mobi>
Thu, 18 Sep 2008 14:10:15 +0000 (11:10 -0300)
One of the created functions will manage the choice between
single process/original version mode of operation of LMS.

src/lib/lightmediascanner_private.h
src/lib/lightmediascanner_process.c

index 8f3edc7..17df4ac 100644 (file)
@@ -48,11 +48,11 @@ struct fds {
 
 /* info to be carried along lms_process() and lms_check() */
 struct pinfo {
+    lms_t *lms;
+    pid_t child;
     struct fds master;
     struct fds slave;
     struct pollfd poll;
-    lms_t *lms;
-    pid_t child;
 };
 
 struct parser {
index 5f9cf9c..6541b35 100644 (file)
@@ -895,61 +895,39 @@ _process_dir(struct pinfo *pinfo, int base, char *path, const char *name)
     return r;
 }
 
-/**
- * Process the given directory.
- *
- * This will add or update media found in the given directory or its children.
- *
- * @param lms previously allocated Light Media Scanner instance.
- * @param top_path top directory to scan.
- *
- * @return On success 0 is returned.
- */
-int
-lms_process(lms_t *lms, const char *top_path)
+static int
+_lms_process_check_valid(lms_t *lms, const char *path)
 {
-    struct pinfo pinfo;
-    int r, len;
-    char path[PATH_SIZE], *bname;
-
-    if (!lms) {
-        r = -1;
-        goto end;
-    }
+    if (!lms)
+        return -1;
 
-    if (!top_path) {
-        r = -2;
-        goto end;
-    }
+    if (!path)
+        return -2;
 
     if (lms->is_processing) {
         fprintf(stderr, "ERROR: is already processing.\n");
-        r = -3;
-        goto end;
+        return -3;
     }
 
     if (!lms->parsers) {
         fprintf(stderr, "ERROR: no plugins registered.\n");
-        r = -4;
-        goto end;
+        return -4;
     }
 
-    pinfo.lms = lms;
-
-    if (lms_create_pipes(&pinfo) != 0) {
-        r = -5;
-        goto end;
-    }
+    return 0;
+}
 
-    if (lms_create_slave(&pinfo, _slave_work) != 0) {
-        r = -6;
-        goto close_pipes;
-    }
+static int
+_process_trigger(struct pinfo *pinfo, const char *top_path)
+{
+    char path[PATH_SIZE], *bname;
+    lms_t *lms = pinfo->lms;
+    int len;
+    int r;
 
     if (realpath(top_path, path) == NULL) {
         perror("realpath");
-        r = -7;
-        goto finish_slave;
+        return -1;
     }
 
     /* search '/' backwards, split dirname and basename, note realpath usage */
@@ -959,18 +937,53 @@ lms_process(lms_t *lms, const char *top_path)
     bname = strdup(path + len);
     if (bname == NULL) {
         perror("strdup");
-        r = -8;
-        goto finish_slave;
+        return -2;
     }
 
     lms->is_processing = 1;
     lms->stop_processing = 0;
-    r = _process_dir(&pinfo, len, path, bname);
+    r = _process_dir(pinfo, len, path, bname);
     lms->is_processing = 0;
     lms->stop_processing = 0;
     free(bname);
 
-  finish_slave:
+    return r;
+}
+
+/**
+ * Process the given directory.
+ *
+ * This will add or update media found in the given directory or its children.
+ *
+ * @param lms previously allocated Light Media Scanner instance.
+ * @param top_path top directory to scan.
+ *
+ * @return On success 0 is returned.
+ */
+int
+lms_process(lms_t *lms, const char *top_path)
+{
+    struct pinfo pinfo;
+    int r, len;
+
+    r = _lms_process_check_valid(lms, top_path);
+    if (r < 0)
+        return r;
+
+    pinfo.lms = lms;
+
+    if (lms_create_pipes(&pinfo) != 0) {
+        r = -1;
+        goto end;
+    }
+
+    if (lms_create_slave(&pinfo, _slave_work) != 0) {
+        r = -2;
+        goto close_pipes;
+    }
+
+    r = _process_trigger(&pinfo, top_path);
+
     lms_finish_slave(&pinfo, _master_send_finish);
   close_pipes:
     lms_close_pipes(&pinfo);