Switch to strings for all the core option parsing
authorSteve McIntyre <steve@einval.com>
Wed, 29 Jun 2011 17:20:02 +0000 (18:20 +0100)
committerSteve McIntyre <steve@einval.com>
Wed, 29 Jun 2011 17:20:02 +0000 (18:20 +0100)
Cleans up the macro handling in mount_opts and fm_mount_opts, ready
for adding config file support. The values of the two numberic config
options (cachesize and num_threads) are now split out into separate
file-global variables. We parse the strings once the fuse lib has
given them to us, then use the numeric variables from then on.

Allows us to use more user-friendly inputs like cachesize=10G (etc.)
easily in the program.

C/fuse-music.c

index e420db8..402100a 100644 (file)
@@ -72,6 +72,8 @@ format_t formats[] =
 #define DIR_TRIGGER 2
 
 static fmt_e format_index = OUTFMT_OGG;
+static long long cachesize_value = CACHE_DEFAULT_SIZE;
+static long num_threads_value = 1;
 char *quality = NULL;
 FMDB *db_state = NULL;
 
@@ -112,24 +114,20 @@ struct thread_work
 };
 struct thread_work global_work;
 
-#define MOUNT_OPT_CHAR(n)      char *n
-#define MOUNT_OPT_LLD(n)       long long n
-#define MOUNT_OPT_INT(n)       int n
-#define FUSE_MOUNT_OPT_CHAR(n) {"--"#n"=%s", offsetof(struct mount_opts, n), 1}
-#define FUSE_MOUNT_OPT_LLD(n)  {"--"#n"=%lld", offsetof(struct mount_opts, n), 1}
-#define FUSE_MOUNT_OPT_INT(n)  {"--"#n"=%d", offsetof(struct mount_opts, n), 1}
+#define MOUNT_OPT(n)      char *n
+#define FUSE_MOUNT_OPT(n) {"--"#n"=%s", offsetof(struct mount_opts, n), 1}
 
 struct mount_opts
 {
-    MOUNT_OPT_CHAR(config_file);    
-    MOUNT_OPT_CHAR(basedir);    
-    MOUNT_OPT_CHAR(cachedir);
-    MOUNT_OPT_CHAR(db_file);
-    MOUNT_OPT_CHAR(format);
-    MOUNT_OPT_CHAR(quality);
-    MOUNT_OPT_CHAR(logfile);
-    MOUNT_OPT_LLD(cachesize);
-    MOUNT_OPT_INT(num_threads);
+    MOUNT_OPT(config_file);    
+    MOUNT_OPT(basedir);    
+    MOUNT_OPT(cachedir);
+    MOUNT_OPT(db_file);
+    MOUNT_OPT(format);
+    MOUNT_OPT(quality);
+    MOUNT_OPT(logfile);
+    MOUNT_OPT(cachesize);
+    MOUNT_OPT(num_threads);
 };
 struct mount_opts mo;
 
@@ -143,15 +141,15 @@ struct
 pthread_once_t once_control = PTHREAD_ONCE_INIT;
 
 static const struct fuse_opt fm_mount_opts[] = {    
-    FUSE_MOUNT_OPT_CHAR(config_file),
-    FUSE_MOUNT_OPT_CHAR(basedir),
-    FUSE_MOUNT_OPT_CHAR(cachedir),
-    FUSE_MOUNT_OPT_CHAR(db_file),
-    FUSE_MOUNT_OPT_CHAR(format),
-    FUSE_MOUNT_OPT_CHAR(quality),
-    FUSE_MOUNT_OPT_CHAR(logfile),
-    FUSE_MOUNT_OPT_LLD(cachesize),
-    FUSE_MOUNT_OPT_INT(num_threads),
+    FUSE_MOUNT_OPT(config_file),
+    FUSE_MOUNT_OPT(basedir),
+    FUSE_MOUNT_OPT(cachedir),
+    FUSE_MOUNT_OPT(db_file),
+    FUSE_MOUNT_OPT(format),
+    FUSE_MOUNT_OPT(quality),
+    FUSE_MOUNT_OPT(logfile),
+    FUSE_MOUNT_OPT(cachesize),
+    FUSE_MOUNT_OPT(num_threads),
     FUSE_OPT_END
 };
 
@@ -169,19 +167,19 @@ static void init_threads(void)
     pthread_mutex_init(&dir_state.lock, &attr);
     pthread_mutex_init(&global_work.lock, &attr);
     pthread_cond_init(&global_work.cv, NULL);
-    bg_thread = malloc(mo.num_threads * sizeof(pthread_t));
+    bg_thread = malloc(num_threads_value * sizeof(pthread_t));
     if (!bg_thread)
     {
         MAINLOG0((logfile, "%s: Failed to malloc thread space for thread descriptors\n", __func__));
         db_close(db_state);
     }
-    threadcount = malloc(mo.num_threads * sizeof(int));
+    threadcount = malloc(num_threads_value * sizeof(int));
     if (!threadcount)
     {
         MAINLOG0((logfile, "%s: Failed to malloc thread space for thread numbers\n", __func__));
         db_close(db_state);
     }
-    for (i = 0; i < mo.num_threads; i++)
+    for (i = 0; i < num_threads_value; i++)
     {
         threadcount[i] = i;
         error = pthread_create(&bg_thread[i], NULL, bg_handler, &threadcount[i]);
@@ -643,7 +641,7 @@ static int enqueue_encode_file(const char *flac_path, encode_keep_e keep)
 
     pthread_mutex_unlock(&global_work.lock);
 
-    if (mo.num_threads > 1)
+    if (num_threads_value > 1)
     {
         tmp_path = strdup(flac_path);
         current_dir = dirname(tmp_path);
@@ -677,7 +675,7 @@ static int enqueue_encode_file(const char *flac_path, encode_keep_e keep)
         }
         pthread_mutex_unlock(&dir_state.lock);
         free(tmp_path);
-    } /* mo.num_threads > 1 */
+    } /* num_threads_value > 1 */
  
     pthread_mutex_lock(&global_work.lock);
     MAINLOG1((logfile, "%s: signal, %d files in queue\n", dbgname, global_work.num_todo));
@@ -1071,8 +1069,6 @@ int main(int argc, char *argv[])
 
     logfile = stderr;
 
-    mo.cachesize = CACHE_DEFAULT_SIZE;
-    mo.num_threads = 1;
     fuse_opt_parse(&args, &mo, fm_mount_opts, NULL);
 
     if (mo.logfile)
@@ -1104,10 +1100,31 @@ int main(int argc, char *argv[])
         return EINVAL;
     }
 
-    if (mo.cachesize < CACHE_MIN_SIZE)
+    /* Convert strings into useful integers */
+    if (mo.cachesize)
+    {
+        error = parse_string_to_long_long(mo.cachesize, &cachesize_value);
+        if (error)
+        {
+            MAINLOG0((logfile, "Unable to parse cachesize value: %s\n", mo.cachesize));
+            return EINVAL;
+        }
+    }
+    
+    if (mo.num_threads)
+    {
+        error = parse_string_to_long(mo.cachesize, &num_threads_value);
+        if (error)
+        {
+            MAINLOG0((logfile, "Unable to parse num_threads value: %s\n", mo.num_threads));
+            return EINVAL;
+        }
+    }
+
+    if (cachesize_value < CACHE_MIN_SIZE)
     {
         MAINLOG0((logfile, "Cache size %lld is too small; minimum allowed is %lld\n",
-                  mo.cachesize, CACHE_MIN_SIZE));
+                  cachesize_value, CACHE_MIN_SIZE));
         return EINVAL;
     }
 
@@ -1144,7 +1161,7 @@ int main(int argc, char *argv[])
         return 1;
     }
 
-    error = cache_init(db_state, mo.cachedir, mo.cachesize);
+    error = cache_init(db_state, mo.cachedir, cachesize_value);
     if (error)
     {
         MAINLOG0((logfile, "Failed to init cache %s, error %d\n", mo.cachedir, error));