server: Introduce hash sort for ls command.
authorAndre Noll <maan@tuebingen.mpg.de>
Mon, 29 May 2017 17:53:47 +0000 (19:53 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Sun, 11 Jun 2017 09:54:23 +0000 (11:54 +0200)
This allows to specify -s=hash (or -s=h) as options to the ls server
command to sort the list of audio files by the SHA1 hash of the
file content.

We already had the LS_SORT_BY_HASH enum constant but the feature was
never implemented. It's easy to do though, so here it is.

aft.c
m4/lls/server_cmd.suite.m4

diff --git a/aft.c b/aft.c
index 4a8124481b551b71c584a9483400420f270949d0..a548afbce8c99e167a932617358c1e0b4339ff6b 100644 (file)
--- a/aft.c
+++ b/aft.c
@@ -1104,6 +1104,12 @@ out:
        return ret;
 }
 
        return ret;
 }
 
+static int ls_hash_compare(const void *a, const void *b)
+{
+       struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
+       return memcmp(d1->hash, d2->hash, HASH_SIZE);
+}
+
 static int ls_audio_format_compare(const void *a, const void *b)
 {
        struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
 static int ls_audio_format_compare(const void *a, const void *b)
 {
        struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
@@ -1221,6 +1227,8 @@ static int sort_matching_paths(struct ls_options *options)
                compar = ls_duration_compare; break;
        case LS_SORT_BY_AUDIO_FORMAT:
                compar = ls_audio_format_compare; break;
                compar = ls_duration_compare; break;
        case LS_SORT_BY_AUDIO_FORMAT:
                compar = ls_audio_format_compare; break;
+       case LS_SORT_BY_HASH:
+               compar = ls_hash_compare; break;
        default:
                return -E_BAD_SORT;
        }
        default:
                return -E_BAD_SORT;
        }
@@ -1369,7 +1377,6 @@ out:
        return ret;
 }
 
        return ret;
 }
 
-/* TODO: flags -h (sort by hash) */
 static int com_ls(struct command_context *cc, struct lls_parse_result *lpr)
 {
        const struct lls_command *cmd = SERVER_CMD_CMD_PTR(LS);
 static int com_ls(struct command_context *cc, struct lls_parse_result *lpr)
 {
        const struct lls_command *cmd = SERVER_CMD_CMD_PTR(LS);
@@ -1435,6 +1442,8 @@ static int com_ls(struct command_context *cc, struct lls_parse_result *lpr)
                        opts->sorting = LS_SORT_BY_DURATION;
                else if (!strcmp(val, "a") || !strcmp(val, "audio-format"))
                        opts->sorting = LS_SORT_BY_AUDIO_FORMAT;
                        opts->sorting = LS_SORT_BY_DURATION;
                else if (!strcmp(val, "a") || !strcmp(val, "audio-format"))
                        opts->sorting = LS_SORT_BY_AUDIO_FORMAT;
+               else if (!strcmp(val, "h") || !strcmp(val, "hash"))
+                       opts->sorting = LS_SORT_BY_HASH;
                else {
                        ret = -E_AFT_SYNTAX;
                        goto out;
                else {
                        ret = -E_AFT_SYNTAX;
                        goto out;
index e2bd162d19b5161a38c9b404607842a0115c4a48..5ac7f16afa6888f97488e8c5d4dd6a7db0d94a64 100644 (file)
@@ -284,6 +284,8 @@ aux_info_prefix = Permissions:
 
                        audio-format (a)
 
 
                        audio-format (a)
 
+                       hash (h)
+
                        If --sort is not given, path sort is implied.
                [/help]
 
                        If --sort is not given, path sort is implied.
                [/help]