From be78128f5b0e9605de29724b620c9c54f312281e Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Wed, 19 Oct 2022 21:56:51 +0200 Subject: [PATCH] server: Implement ls --limit. 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 | 28 ++++++++++++---------------- m4/lls/server_cmd.suite.m4 | 10 ++++++++++ 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/aft.c b/aft.c index b4d8c2dd..ff75361d 100644 --- 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(¤t_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); diff --git a/m4/lls/server_cmd.suite.m4 b/m4/lls/server_cmd.suite.m4 index 8200c624..cafdbccf 100644 --- a/m4/lls/server_cmd.suite.m4 +++ b/m4/lls/server_cmd.suite.m4 @@ -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 -- 2.39.2