Fix com_add segfault.
[paraslash.git] / aft.c
diff --git a/aft.c b/aft.c
index 1d7680714f05d39e1c0c057aaaf95b412e1626b6..8473bc9b012b275518e056f2631222eaa954b462 100644 (file)
--- a/aft.c
+++ b/aft.c
@@ -126,8 +126,7 @@ static struct osl_column_description aft_cols[] = {
        }
 };
 
-static const struct osl_table_description audio_file_table_desc = {
-       .dir = DATABASE_DIR,
+static struct osl_table_description audio_file_table_desc = {
        .name = "audio_files",
        .num_columns = NUM_AFT_COLUMNS,
        .flags = OSL_LARGE_TABLE,
@@ -982,7 +981,7 @@ static int com_ls_callback(const struct osl_object *query,
                        prepare_ls_row);
        if (ret < 0)
                goto out;
-       ret = opts->num_patterns? -E_NO_MATCH : 1;
+       ret = opts->num_patterns? -E_NO_MATCH : 0;
        if (!opts->num_matching_paths) {
                PARA_NOTICE_LOG("no match, ret: %d\n", ret);
                goto out;
@@ -1005,6 +1004,7 @@ static int com_ls_callback(const struct osl_object *query,
        ret = 1;
 out:
        ls_output->data = b.buf;
+       PARA_NOTICE_LOG("ls_outoute.data: %p\n", ls_output->data);
        ls_output->size = b.size;
        free(opts->data);
        free(opts->data_ptr);
@@ -1124,8 +1124,8 @@ int com_afs_ls(int fd, int argc, char * const * const argv)
        opts.num_patterns = argc - i;
        ret = send_option_arg_callback_request(&query, opts.num_patterns,
                argv + i, com_ls_callback, &ls_output);
-       if (ret >= 0 && ls_output.data) {
-               send_buffer(fd, (char *)ls_output.data);
+       if (ret > 0) {
+               ret = send_buffer(fd, (char *)ls_output.data);
                free(ls_output.data);
        }
        return ret;
@@ -1402,7 +1402,7 @@ static int add_one_audio_file(const char *arg, const void *private_data)
        const struct private_add_data *pad = private_data;
        struct audio_format_info afhi, *afhi_ptr = NULL;
        struct osl_row *pb = NULL, *hs = NULL; /* path brother/hash sister */
-       struct osl_object map, obj, query, result;
+       struct osl_object map, obj = {.data = NULL}, query, result;
        char *path;
        HASH_TYPE hash[HASH_SIZE];
 
@@ -1456,11 +1456,8 @@ static int add_one_audio_file(const char *arg, const void *private_data)
         */
        if (!hs || (pad->flags & ADD_FLAG_FORCE)) {
                ret = compute_afhi(path, map.data, map.size, &afhi);
-               if (ret < 0) {
-                       ret = send_va_buffer(pad->fd,
-                               "audio format of %s not recognized\n", path);
+               if (ret < 0)
                        goto out_unmap;
-               }
                format_num = ret;
                afhi_ptr = &afhi;
        }
@@ -1473,11 +1470,14 @@ static int add_one_audio_file(const char *arg, const void *private_data)
 out_unmap:
        munmap(map.data, map.size);
 out_free:
+       if (ret < 0)
+               send_va_buffer(pad->fd, "failed to add %s (%s)\n", path,
+                       PARA_STRERROR(-ret));
        free(obj.data);
        free(path);
        if (afhi_ptr)
                free(afhi_ptr->chunk_table);
-       return ret;
+       return 1; /* it's not an error if not all files could be added */
 }
 
 int com_add(int fd, int argc, char * const * const argv)
@@ -1517,16 +1517,11 @@ int com_add(int fd, int argc, char * const * const argv)
                if (S_ISDIR(statbuf.st_mode)) {
                        ret = for_each_file_in_dir(argv[i],
                                add_one_audio_file, &pad);
-                       if (ret < 0)
-                               return ret;
                        continue;
                }
                ret = add_one_audio_file(argv[i], &pad);
-               if (ret < 0)
-                       goto out;
        }
        ret = 1;
-out:
        return ret;
 
 }
@@ -1712,16 +1707,18 @@ void aft_shutdown(enum osl_close_flags flags)
 /**
  * Open the audio file table.
  *
- * \param ti Gets initialized by this function
+ * \param ti Gets initialized by this function.
+ * \param db The database directory.
  *
  * \return Positive on success, negative on errors.
  *
  * \sa osl_open_table().
  */
-int aft_init(struct table_info *ti)
+int aft_init(struct table_info *ti, const char *db)
 {
        int ret;
 
+       audio_file_table_desc.dir = db;
        ti->desc = &audio_file_table_desc;
        ret = osl_open_table(ti->desc, &ti->table);
        if (ret >= 0) {