]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
server: Implement ls --limit.
authorAndre Noll <maan@tuebingen.mpg.de>
Wed, 19 Oct 2022 19:56:51 +0000 (21:56 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Mon, 15 May 2023 18:55:45 +0000 (20:55 +0200)
The new option is quite easy to implement. The patch actually removes
more lines from aft.c than it adds, but this is just because the code
which loops over all matching files was duplicated across the two
branches of the clause which checks whether --reverse was given. The
branches can easily be combined.

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

diff --git a/aft.c b/aft.c
index b4d8c2dd359bbfbe143c8be75a89b016b0bfe505..ff75361d374b9dea3669d24d819afefaf9f50a52 100644 (file)
--- a/aft.c
+++ b/aft.c
@@ -1365,9 +1365,10 @@ static int com_ls_callback(struct afs_callback_arg *aca)
 {
        const struct lls_command *cmd = SERVER_CMD_CMD_PTR(LS);
        struct ls_options *opts = aca->query.data;
-       int i = 0, ret;
+       int ret;
        time_t current_time;
        const struct lls_opt_result *r_r;
+       uint32_t limit, k, n;
 
        ret = lls_deserialize_parse_result(
                (char *)aca->query.data + sizeof(*opts), cmd, &opts->lpr);
@@ -1382,7 +1383,8 @@ static int com_ls_callback(struct afs_callback_arg *aca)
                        prepare_ls_row));
        if (ret < 0)
                goto out;
-       if (opts->num_matching_paths == 0) {
+       n = opts->num_matching_paths;
+       if (n == 0) {
                ret = lls_num_inputs(opts->lpr) > 0? -E_NO_MATCH : 0;
                goto out;
        }
@@ -1390,20 +1392,14 @@ static int com_ls_callback(struct afs_callback_arg *aca)
        if (ret < 0)
                goto out;
        time(&current_time);
-       if (lls_opt_given(r_r))
-               for (i = opts->num_matching_paths - 1; i >= 0; i--) {
-                       ret = print_list_item(opts->data_ptr[i], opts,
-                               &aca->pbout, current_time);
-                       if (ret < 0)
-                               goto out;
-               }
-       else
-               for (i = 0; i < opts->num_matching_paths; i++) {
-                       ret = print_list_item(opts->data_ptr[i], opts,
-                               &aca->pbout, current_time);
-                       if (ret < 0)
-                               goto out;
-               }
+       limit = SERVER_CMD_UINT32_VAL(LS, LIMIT, opts->lpr);
+       for (k = 0; k < n && (limit == 0 || k < limit); k++) {
+               uint32_t idx = lls_opt_given(r_r)? n - 1 - k : k;
+               ret = print_list_item(opts->data_ptr[idx], opts, &aca->pbout,
+                       current_time);
+               if (ret < 0)
+                       goto out;
+       }
 out:
        lls_free_parse_result(opts->lpr, cmd);
        free(opts->data);
index 8200c6249449f81b5743775f04796462fc912b83..cafdbccf16e8e0511e7722999ca8bcb3a7d254bf 100644 (file)
@@ -222,6 +222,16 @@ m4_include(`com_ll.m4')
                        also given), chunk time and chunk offsets.
 
                [/help]
+       [option limit]
+               short_opt = L
+               summary = list at most this many files
+               arg_type = uint32
+               arg_info = required_arg
+               typestr = num
+               [help]
+                       An argument of zero means "unlimited". This is also the default which
+                       applies if the option is not given.
+               [/help]
        [option basename]
                short_opt = b
                summary = list and match basenames only