com_add(): Write messages to the client rather than to the log.
[paraslash.git] / aft.c
diff --git a/aft.c b/aft.c
index ce84ef0a47c822256ab52283c6259e0e787c9e37..431bedb63c1c1bb58a632aa055eeaf8a98198ce7 100644 (file)
--- a/aft.c
+++ b/aft.c
@@ -1372,7 +1372,7 @@ afhi <=> force or no HS
 /* TODO: change log messages so that they get written to the result buffer */
 
 static int com_add_callback(const struct osl_object *query,
-               __a_unused struct osl_object *result)
+               struct osl_object *result)
 {
        char *buf = query->data, *path;
        struct osl_row *pb, *aft_row;
@@ -1384,6 +1384,7 @@ static int com_add_callback(const struct osl_object *query,
        char afsi_buf[AFSI_SIZE];
        uint32_t flags = read_u32(buf + AFTROW_FLAGS_OFFSET);
        struct afs_info default_afsi = {.last_played = 0};
+       struct para_buffer msg = {.buf = NULL};
 
        hash = (HASH_TYPE *)buf + AFTROW_HASH_OFFSET;
        hash_to_asc(hash, asc);;
@@ -1401,29 +1402,30 @@ static int com_add_callback(const struct osl_object *query,
                return ret;
        if (hs && pb && hs == pb && !(flags & ADD_FLAG_FORCE)) {
                if (flags & ADD_FLAG_VERBOSE)
-                       PARA_NOTICE_LOG("ignoring duplicate %p\n", path);
-               return 1;
+                       para_printf(&msg, "ignoring duplicate\n");
+               ret = 1;
+               goto out;
        }
        if (hs && hs != pb) {
                struct osl_object obj;
                if (pb) { /* hs trumps pb, remove pb */
                        if (flags & ADD_FLAG_VERBOSE)
-                               PARA_NOTICE_LOG("removing path brother\n");
+                               para_printf(&msg, "removing path brother\n");
                        ret = osl_del_row(audio_file_table, pb);
                        if (ret < 0)
-                               return ret;
+                               goto out;
                        pb = NULL;
                }
                /* file rename, update hs' path */
                ret = osl_get_object(audio_file_table, hs, AFTCOL_PATH, &obj);
                if (flags & ADD_FLAG_VERBOSE)
-                       PARA_NOTICE_LOG("rename %s -> %s\n", (char *)obj.data, path);
+                       para_printf(&msg, "renamed from %s\n", (char *)obj.data);
                ret = osl_update_object(audio_file_table, hs, AFTCOL_PATH,
                        &objs[AFTCOL_PATH]);
                if (ret < 0)
-                       return ret;
+                       goto out;
                if (!(flags & ADD_FLAG_FORCE))
-                       return ret;
+                       goto out;
        }
        /* no hs or force mode, child must have sent afhi */
        uint16_t afhi_offset = read_u16(buf + AFTROW_AFHI_OFFSET_POS);
@@ -1431,8 +1433,9 @@ static int com_add_callback(const struct osl_object *query,
 
        objs[AFTCOL_AFHI].data = buf + afhi_offset;
        objs[AFTCOL_AFHI].size = chunks_offset - afhi_offset;
+       ret = -E_NO_AFHI;
        if (!objs[AFTCOL_AFHI].size) /* "impossible" */
-               return -E_NO_AFHI;
+               goto out;
        objs[AFTCOL_CHUNKS].data = buf + chunks_offset;
        objs[AFTCOL_CHUNKS].size = query->size - chunks_offset;
        if (pb && !hs) { /* update pb's hash */
@@ -1440,35 +1443,32 @@ static int com_add_callback(const struct osl_object *query,
                HASH_TYPE *old_hash;
                ret = get_hash_of_row(pb, &old_hash);
                if (ret < 0)
-                       return ret;
+                       goto out;
                hash_to_asc(old_hash, old_asc);
                if (flags & ADD_FLAG_VERBOSE)
-                       PARA_NOTICE_LOG("file change: %s %s -> %s\n", path,
+                       para_printf(&msg, "file change: %s -> %s\n",
                                old_asc, asc);
                ret = osl_update_object(audio_file_table, pb, AFTCOL_HASH,
                        &objs[AFTCOL_HASH]);
                if (ret < 0)
-                       return ret;
+                       goto out;
        }
        if (hs || pb) { /* (hs != NULL and pb != NULL) implies hs == pb */
                const struct osl_row *row = pb? pb : hs;
                /* update afhi and chunk_table */
                if (flags & ADD_FLAG_VERBOSE)
-                       PARA_DEBUG_LOG("updating audio format handler info (%zd bytes)\n",
-                               objs[AFTCOL_AFHI].size);
+                       para_printf(&msg, "updating afhi and chunk table\n");
                ret = osl_update_object(audio_file_table, row, AFTCOL_AFHI,
                        &objs[AFTCOL_AFHI]);
                if (ret < 0)
-                       return ret;
-               if (flags & ADD_FLAG_VERBOSE)
-                       PARA_DEBUG_LOG("updating chunk table\n");
+                       goto out;
                ret = osl_update_object(audio_file_table, row, AFTCOL_CHUNKS,
                        &objs[AFTCOL_CHUNKS]);
-               if (ret < 0)
-                       return ret;
-               return mood_update_audio_file(row, NULL);
+               goto out;
        }
        /* new entry, use default afsi */
+       if (flags & ADD_FLAG_VERBOSE)
+               para_printf(&msg, "new file\n");
        default_afsi.last_played = time(NULL) - 365 * 24 * 60 * 60;
        default_afsi.audio_format_id = read_u8(buf + AFTROW_AUDIO_FORMAT_OFFSET);
 
@@ -1476,9 +1476,15 @@ static int com_add_callback(const struct osl_object *query,
        objs[AFTCOL_AFSI].size = AFSI_SIZE;
        save_afsi(&default_afsi, &objs[AFTCOL_AFSI]);
        ret = osl_add_and_get_row(audio_file_table, objs, &aft_row);
+out:
        if (ret < 0)
-               return ret;
-       return mood_update_audio_file(aft_row, NULL);
+               para_printf(&msg, "%s\n", PARA_STRERROR(-ret));
+       if (!msg.buf)
+               return 0;
+       result->data = msg.buf;
+       result->size = msg.size;
+       return 1;
+       // mood_update_audio_file(aft_row, NULL);
 }
 
 struct private_add_data {
@@ -1517,7 +1523,7 @@ static int hash_sister_callback(const struct osl_object *query,
 
 static int add_one_audio_file(const char *path, const void *private_data)
 {
-       int ret;
+       int ret, ret2;
        uint8_t format_num = -1;
        const struct private_add_data *pad = private_data;
        struct audio_format_info afhi, *afhi_ptr = NULL;
@@ -1528,13 +1534,8 @@ static int add_one_audio_file(const char *path, const void *private_data)
        afhi.header_offset = 0;
        afhi.header_len = 0;
        ret = guess_audio_format(path);
-       if (ret < 0 && !(pad->flags & ADD_FLAG_ALL)) {
-               if (pad->flags & ADD_FLAG_VERBOSE)
-                       ret = send_va_buffer(pad->fd, "%s: %s\n",
-                               PARA_STRERROR(-ret), path);
-               ret = 1;
+       if (ret < 0 && !(pad->flags & ADD_FLAG_ALL))
                goto out_free;
-       }
        query.data = (char *)path;
        query.size = strlen(path) + 1;
        ret = send_callback_request(path_brother_callback, &query, &result);
@@ -1571,7 +1572,7 @@ static int add_one_audio_file(const char *path, const void *private_data)
        if (pb && hs && hs == pb && (!(pad->flags & ADD_FLAG_FORCE))) {
                if (pad->flags & ADD_FLAG_VERBOSE)
                        ret = send_va_buffer(pad->fd,
-                               "not forcing update: %s\n", path);
+                               "%s exists, not forcing update\n", path);
                goto out_unmap;
        }
        /*
@@ -1593,7 +1594,13 @@ static int add_one_audio_file(const char *path, const void *private_data)
        munmap(map.data, map.size);
        save_audio_file_info(hash, path, afhi_ptr, pad->flags, format_num, &obj);
        /* Ask afs to consider this entry for adding. */
-       ret = send_callback_request(com_add_callback, &obj, NULL);
+       ret = send_callback_request(com_add_callback, &obj, &result);
+       if (result.data && result.size) {
+               ret2 = send_va_buffer(pad->fd, "%s", (char *)result.data);
+               free(result.data);
+               if (ret >= 0 && ret2 < 0)
+                       ret = ret2;
+       }
        goto out_free;
 
 out_unmap: