]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - aft.c
First draft of the libosl patch series.
[paraslash.git] / aft.c
diff --git a/aft.c b/aft.c
index 3283dda82d309769fa569d7ab6b32b299d368b4f..12424c40e0dda04400698c6e14d71e69387ccfd9 100644 (file)
--- a/aft.c
+++ b/aft.c
@@ -218,12 +218,27 @@ enum audio_file_table_columns {
        NUM_AFT_COLUMNS
 };
 
+/**
+ * Compare two osl objects pointing to hash values.
+ *
+ * \param obj1 Pointer to the first hash object.
+ * \param obj2 Pointer to the second hash object.
+ *
+ * \return The values required for an osl compare function.
+ *
+ * \sa osl_compare_func, uint32_compare().
+ */
+int aft_hash_compare(const struct osl_object *obj1, const struct osl_object *obj2)
+{
+       return hash_compare((HASH_TYPE *)obj1->data, (HASH_TYPE *)obj2->data);
+}
+
 static struct osl_column_description aft_cols[] = {
        [AFTCOL_HASH] = {
                .storage_type = OSL_MAPPED_STORAGE,
                .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
                .name = "hash",
-               .compare_function = osl_hash_compare,
+               .compare_function = aft_hash_compare,
                .data_size = HASH_SIZE
        },
        [AFTCOL_PATH] = {
@@ -980,12 +995,66 @@ void make_empty_status_items(char *buf)
        );
 }
 
+static void fixup_taginfo(char *begin, char *end)
+{
+       char *p = begin;
+
+       for (;;) {
+               p = strchr(p, '\n');
+               if (!p)
+                       break;
+               if (p >= end - 1)
+                       break;
+               *p = ' ';
+               p++;
+       }
+}
+
+/* crap, remove this ASAP. */
+static int fixup_info_string(char *info_string)
+{
+       char *t1, *t2, *end;
+
+       if (strncmp(info_string, "audio_file_info:", 16))
+               return -ERRNO_TO_PARA_ERROR(EINVAL);
+       t1 = strstr(info_string, "\ntaginfo1:");
+       if (!t1)
+               return -ERRNO_TO_PARA_ERROR(EINVAL);
+       t2 = strstr(info_string, "\ntaginfo2: ");
+       if (!t2)
+               return -ERRNO_TO_PARA_ERROR(EINVAL);
+
+       end = t2 + strlen(t2) + 1;
+       fixup_taginfo(info_string + 16, t1);
+       fixup_taginfo(t1 + 10, t2);
+       fixup_taginfo(t2 + 10, end);
+
+       if (t1 - info_string < 80 && t2 - t1 < 80 && end - t2 < 80)
+               return 0;
+       if (t1 - info_string >= 80) {
+               memmove(info_string + 80, t1, end - t1);
+               t1 = info_string + 80;
+               t2 -= t1 - info_string - 80;
+               end -= t1 - info_string - 80;
+       }
+       if (t2 - t1 >= 80) {
+               memmove(t1 + 80, t2, end - t2);
+               end -= t2 - t1 - 80;
+               t2 = t1 + 80;
+       }
+       if (end - t2 >= 80) {
+               t2[78] = '\n';
+               t2[79] = '\0';
+       }
+       return 1;
+}
+
 static int make_status_items(struct audio_file_data *afd,
                struct afs_info *afsi, char *path, long score,
                HASH_TYPE *hash)
 {
        struct ls_data d = {
-               .afhi = afd->afhi, /* struct copy */
+               .afhi = afd->afhi,
                .afsi = *afsi,
                .path = path,
                .score = score,
@@ -999,6 +1068,12 @@ static int make_status_items(struct audio_file_data *afd,
        time_t current_time;
        int ret;
 
+       ret = fixup_info_string(afd->afhi.info_string);
+       if (ret < 0) {
+               PARA_WARNING_LOG("ignoring invalid tag info\n");
+               afd->afhi.info_string[0] = '\0';
+       } else if (ret)
+               PARA_NOTICE_LOG("truncated overlong tag info\n");
        time(&current_time);
        ret = print_list_item(&d, &opts, &pb, current_time); /* frees info string */
        afd->afhi.info_string = NULL;
@@ -1622,7 +1697,7 @@ static void com_add_callback(int fd, const struct osl_object *query)
        PARA_INFO_LOG("request to add %s\n", path);
        hs = find_hash_sister(hash);
        ret = aft_get_row_of_path(path, &pb);
-       if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
+       if (ret < 0 && ret != -E_OSL_RB_KEY_NOT_FOUND)
                goto out;
        if (hs && pb && hs == pb && !(flags & ADD_FLAG_FORCE)) {
                if (flags & ADD_FLAG_VERBOSE)
@@ -1785,7 +1860,7 @@ static int add_one_audio_file(const char *path, void *private_data)
        query.size = strlen(path) + 1;
        ret = send_callback_request(path_brother_callback, &query,
                get_row_pointer_from_result, &pb);
-       if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
+       if (ret < 0 && ret != -E_OSL_RB_KEY_NOT_FOUND)
                goto out_free;
        ret = 1;
        if (pb && (pad->flags & ADD_FLAG_LAZY)) { /* lazy is really cheap */