From ba0a963f0a31848e7f622c45c56fa6ebf0b67057 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sun, 15 May 2016 18:53:50 +0200 Subject: [PATCH] server: Convert com_add() to lopsub. In the callback structure for the add command, replace the flags field by the serialized callback result. This way the callback has access to the full parse result structure. --- afs.cmd | 23 ------ afs.h | 1 + aft.c | 159 ++++++++++++++++++------------------- m4/lls/server_cmd.suite.m4 | 39 +++++++++ 4 files changed, 116 insertions(+), 106 deletions(-) diff --git a/afs.cmd b/afs.cmd index 76b5f4dc..7028d1bb 100644 --- a/afs.cmd +++ b/afs.cmd @@ -3,29 +3,6 @@ SF: afs.c aft.c attribute.c SN: list of afs commands TM: mood lyr img pl --- -N: add -P: AFS_READ | AFS_WRITE -D: Add or update audio files. -U: add [-a] [-l] [-f] [-v] path... -H: Each path must be absolute and refer to either an audio file, or a -H: directory. In case of a directory, all audio files in that directory -H: are added recursively. Only absolute paths are accepted. -H: -H: Options: -H: -H: -a Add all files. The default is to add only files ending in a -H: known suffix for a supported audio format. -H: -H: -l Add files lazily. If the path already exists in the -H: database, skip this file. This operation is really cheap. Useful -H: to update large directories after some files have been added or -H: deleted. -H: -H: -f Force adding/updating. Recompute the audio format handler data -H: even if a file with the same path and the same hash value exists. -H: -H: -v Verbose mode. Print what is being done. ---- N: init P: AFS_READ | AFS_WRITE D: Initialize the osl tables for the audio file selector. diff --git a/afs.h b/afs.h index 2f9d7e5f..a3135033 100644 --- a/afs.h +++ b/afs.h @@ -163,6 +163,7 @@ struct afs_callback_arg { struct osl_object query; /** Will be written on band SBD_OUTPUT, fully buffered. */ struct para_buffer pbout; + struct lls_parse_result *lpr; }; /** diff --git a/aft.c b/aft.c index 38f47fd8..628f98fb 100644 --- a/aft.c +++ b/aft.c @@ -13,6 +13,7 @@ #include #include +#include "server_cmd.lsg.h" #include "para.h" #include "error.h" #include "crypt.h" @@ -1523,8 +1524,8 @@ enum com_add_buffer_offsets { CAB_AFHI_OFFSET_POS = 0, /** Start of the chunk table (if present). */ CAB_CHUNKS_OFFSET_POS = 4, - /** Flags given to the add command. */ - CAB_FLAGS_OFFSET = 8, + /** Start of the (serialized) lopsub parse result. */ + CAB_LPR_OFFSET = 8, /** Audio format id. */ CAB_AUDIO_FORMAT_ID_OFFSET = 12, /** The hash of the audio file being added. */ @@ -1541,31 +1542,32 @@ enum com_add_buffer_offsets { * handler info won't be stored in the buffer. */ static void save_add_callback_buffer(unsigned char *hash, const char *path, - struct afh_info *afhi, uint32_t flags, + struct afh_info *afhi, const char *slpr, size_t slpr_size, uint8_t audio_format_num, struct osl_object *obj) { size_t path_len = strlen(path) + 1; size_t afhi_size = sizeof_afhi_buf(afhi); size_t size = CAB_PATH_OFFSET + path_len + afhi_size - + sizeof_chunk_table(afhi); + + sizeof_chunk_table(afhi) + slpr_size; char *buf = para_malloc(size); uint32_t pos; + assert(size <= ~(uint32_t)0); + write_u8(buf + CAB_AUDIO_FORMAT_ID_OFFSET, audio_format_num); + memcpy(buf + CAB_HASH_OFFSET, hash, HASH_SIZE); + strcpy(buf + CAB_PATH_OFFSET, path); pos = CAB_PATH_OFFSET + path_len; write_u32(buf + CAB_AFHI_OFFSET_POS, pos); save_afhi(afhi, buf + pos); pos += afhi_size; - write_u32(buf + CAB_CHUNKS_OFFSET_POS, pos); - if (afhi) + if (afhi) { save_chunk_table(afhi, buf + pos); - - write_u32(buf + CAB_FLAGS_OFFSET, flags); - write_u8(buf + CAB_AUDIO_FORMAT_ID_OFFSET, audio_format_num); - - memcpy(buf + CAB_HASH_OFFSET, hash, HASH_SIZE); - strcpy(buf + CAB_PATH_OFFSET, path); - + pos += sizeof_chunk_table(afhi); + } + write_u32(buf + CAB_LPR_OFFSET, pos); + memcpy(buf + pos, slpr, slpr_size); + assert(pos + slpr_size == size); obj->data = buf; obj->size = size; } @@ -1621,18 +1623,6 @@ Notes: */ -/** Flags passed to the add command. */ -enum com_add_flags { - /** Skip paths that exist already. */ - ADD_FLAG_LAZY = 1, - /** Force adding. */ - ADD_FLAG_FORCE = 2, - /** Print what is being done. */ - ADD_FLAG_VERBOSE = 4, - /** Try to add files with unknown suffixes. */ - ADD_FLAG_ALL = 8, -}; - static int com_add_callback(struct afs_callback_arg *aca) { char *buf = aca->query.data, *path; @@ -1643,9 +1633,16 @@ static int com_add_callback(struct afs_callback_arg *aca) char asc[2 * HASH_SIZE + 1]; int ret; char afsi_buf[AFSI_SIZE]; - uint32_t flags = read_u32(buf + CAB_FLAGS_OFFSET); + char *slpr = buf + read_u32(buf + CAB_LPR_OFFSET); struct afs_info default_afsi = {.last_played = 0}; uint16_t afhi_offset, chunks_offset; + const struct lls_command *cmd = SERVER_CMD_CMD_PTR(ADD); + const struct lls_opt_result *r_f, *r_v; + + ret = lls_deserialize_parse_result(slpr, cmd, &aca->lpr); + assert(ret >= 0); + r_f = SERVER_CMD_OPT_RESULT(ADD, FORCE, aca->lpr); + r_v = SERVER_CMD_OPT_RESULT(ADD, VERBOSE, aca->lpr); hash = (unsigned char *)buf + CAB_HASH_OFFSET; hash_to_asc(hash, asc); @@ -1663,8 +1660,8 @@ static int com_add_callback(struct afs_callback_arg *aca) ret = find_path_brother(path, &pb); if (ret < 0) goto out; - if (hs && pb && hs == pb && !(flags & ADD_FLAG_FORCE)) { - if (flags & ADD_FLAG_VERBOSE) + if (hs && pb && hs == pb && !lls_opt_given(r_f)) { + if (lls_opt_given(r_v)) para_printf(&aca->pbout, "ignoring duplicate\n"); ret = 1; goto out; @@ -1672,7 +1669,7 @@ static int com_add_callback(struct afs_callback_arg *aca) if (hs && hs != pb) { struct osl_object obj; if (pb) { /* hs trumps pb, remove pb */ - if (flags & ADD_FLAG_VERBOSE) + if (lls_opt_given(r_v)) para_printf(&aca->pbout, "removing %s\n", path); ret = afs_event(AUDIO_FILE_REMOVE, &aca->pbout, pb); if (ret < 0) @@ -1683,7 +1680,7 @@ static int com_add_callback(struct afs_callback_arg *aca) pb = NULL; } /* file rename, update hs' path */ - if (flags & ADD_FLAG_VERBOSE) { + if (lls_opt_given(r_v)) { ret = osl(osl_get_object(audio_file_table, hs, AFTCOL_PATH, &obj)); if (ret < 0) @@ -1698,7 +1695,7 @@ static int com_add_callback(struct afs_callback_arg *aca) ret = afs_event(AUDIO_FILE_RENAME, &aca->pbout, hs); if (ret < 0) goto out; - if (!(flags & ADD_FLAG_FORCE)) + if (!lls_opt_given(r_f)) goto out; } /* no hs or force mode, child must have sent afhi */ @@ -1719,18 +1716,18 @@ static int com_add_callback(struct afs_callback_arg *aca) if (ret < 0) goto out; hash_to_asc(old_hash, old_asc); - if (flags & ADD_FLAG_VERBOSE) + if (lls_opt_given(r_v)) para_printf(&aca->pbout, "file change: %s -> %s\n", old_asc, asc); - ret = osl_update_object(audio_file_table, pb, AFTCOL_HASH, - &objs[AFTCOL_HASH]); + ret = osl(osl_update_object(audio_file_table, pb, AFTCOL_HASH, + &objs[AFTCOL_HASH])); if (ret < 0) goto out; } if (hs || pb) { /* (hs != NULL and pb != NULL) implies hs == pb */ struct osl_row *row = pb? pb : hs; /* update afhi and chunk_table */ - if (flags & ADD_FLAG_VERBOSE) + if (lls_opt_given(r_v)) para_printf(&aca->pbout, "updating afhi and chunk table\n"); ret = osl(osl_update_object(audio_file_table, row, AFTCOL_AFHI, @@ -1742,12 +1739,10 @@ static int com_add_callback(struct afs_callback_arg *aca) if (ret < 0) goto out; ret = afs_event(AFHI_CHANGE, &aca->pbout, row); - if (ret < 0) - goto out; goto out; } /* new entry, use default afsi */ - if (flags & ADD_FLAG_VERBOSE) + if (lls_opt_given(r_v)) para_printf(&aca->pbout, "new file\n"); default_afsi.last_played = time(NULL) - 365 * 24 * 60 * 60; default_afsi.audio_format_id = read_u8(buf + CAB_AUDIO_FORMAT_ID_OFFSET); @@ -1762,15 +1757,20 @@ static int com_add_callback(struct afs_callback_arg *aca) out: if (ret < 0) para_printf(&aca->pbout, "could not add %s\n", path); + lls_free_parse_result(aca->lpr, cmd); return ret; } -/** Used by com_add(). */ +/* Used by com_add(). */ struct private_add_data { - /** The pointer passed to the original command handler. */ + /* The pointer passed to the original command handler. */ struct command_context *cc; - /** The given add flags. */ - uint32_t flags; + /* Contains the flags given at the command line. */ + struct lls_parse_result *lpr; + /* Serialized lopsub parse result. */ + char *slpr; + /* Number of bytes. */ + size_t slpr_size; }; static int path_brother_callback(struct afs_callback_arg *aca) @@ -1815,9 +1815,13 @@ static int add_one_audio_file(const char *path, void *private_data) struct osl_row *pb = NULL, *hs = NULL; /* path brother/hash sister */ struct osl_object map, obj = {.data = NULL}, query; unsigned char hash[HASH_SIZE]; + bool a_given = SERVER_CMD_OPT_GIVEN(ADD, ALL, pad->lpr); + bool f_given = SERVER_CMD_OPT_GIVEN(ADD, FORCE, pad->lpr); + bool l_given = SERVER_CMD_OPT_GIVEN(ADD, LAZY, pad->lpr); + bool v_given = SERVER_CMD_OPT_GIVEN(ADD, VERBOSE, pad->lpr); ret = guess_audio_format(path); - if (ret < 0 && !(pad->flags & ADD_FLAG_ALL)) { + if (ret < 0 && !a_given) { ret = 0; goto out_free; } @@ -1828,8 +1832,8 @@ static int add_one_audio_file(const char *path, void *private_data) if (ret < 0 && ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND)) goto out_free; ret = 1; - if (pb && (pad->flags & ADD_FLAG_LAZY)) { /* lazy is really cheap */ - if (pad->flags & ADD_FLAG_VERBOSE) + if (pb && l_given) { /* lazy is really cheap */ + if (v_given) send_ret = send_sb_va(&pad->cc->scc, SBD_OUTPUT, "lazy-ignore: %s\n", path); goto out_free; @@ -1849,8 +1853,8 @@ static int add_one_audio_file(const char *path, void *private_data) goto out_unmap; /* Return success if we already know this file. */ ret = 1; - if (pb && hs && hs == pb && !(pad->flags & ADD_FLAG_FORCE)) { - if (pad->flags & ADD_FLAG_VERBOSE) + if (pb && hs && hs == pb && !f_given) { + if (v_given) send_ret = send_sb_va(&pad->cc->scc, SBD_OUTPUT, "%s exists, not forcing update\n", path); goto out_unmap; @@ -1859,7 +1863,7 @@ static int add_one_audio_file(const char *path, void *private_data) * We won't recalculate the audio format info and the chunk table if * there is a hash sister and FORCE was not given. */ - if (!hs || (pad->flags & ADD_FLAG_FORCE)) { + if (!hs || f_given) { ret = compute_afhi(path, map.data, map.size, fd, &afhi); if (ret < 0) goto out_unmap; @@ -1868,13 +1872,14 @@ static int add_one_audio_file(const char *path, void *private_data) } munmap(map.data, map.size); close(fd); - if (pad->flags & ADD_FLAG_VERBOSE) { + if (v_given) { send_ret = send_sb_va(&pad->cc->scc, SBD_OUTPUT, "adding %s\n", path); if (send_ret < 0) goto out_free; } - save_add_callback_buffer(hash, path, afhi_ptr, pad->flags, format_num, &obj); + save_add_callback_buffer(hash, path, afhi_ptr, pad->slpr, + pad->slpr_size, format_num, &obj); /* Ask afs to consider this entry for adding. */ ret = send_callback_request(com_add_callback, &obj, afs_cb_result_handler, pad->cc); @@ -1893,46 +1898,30 @@ out_free: return send_ret; } -int com_add(struct command_context *cc) +static int com_add(struct command_context *cc, struct lls_parse_result *lpr) { int i, ret; - struct private_add_data pad = {.cc = cc, .flags = 0}; + struct private_add_data pad = {.cc = cc, .lpr = lpr}; + const struct lls_command *cmd = SERVER_CMD_CMD_PTR(ADD); + unsigned num_inputs; + char *errctx; - for (i = 1; i < cc->argc; i++) { - const char *arg = cc->argv[i]; - if (arg[0] != '-') - break; - if (!strcmp(arg, "--")) { - i++; - break; - } - if (!strcmp(arg, "-a")) { - pad.flags |= ADD_FLAG_ALL; - continue; - } - if (!strcmp(arg, "-l")) { - pad.flags |= ADD_FLAG_LAZY; - continue; - } - if (!strcmp(arg, "-f")) { - pad.flags |= ADD_FLAG_FORCE; - continue; - } - if (!strcmp(arg, "-v")) { - pad.flags |= ADD_FLAG_VERBOSE; - continue; - } + ret = lls(lls_check_arg_count(lpr, 1, INT_MAX, &errctx)); + if (ret < 0) { + send_errctx(cc, errctx); + return ret; } - if (cc->argc <= i) - return -E_AFT_SYNTAX; - for (; i < cc->argc; i++) { + ret = lls_serialize_parse_result(lpr, cmd, &pad.slpr, &pad.slpr_size); + assert(ret >= 0); + num_inputs = lls_num_inputs(lpr); + for (i = 0; i < num_inputs; i++) { char *path; - ret = verify_path(cc->argv[i], &path); + ret = verify_path(lls_input(i, lpr), &path); if (ret < 0) { ret = send_sb_va(&cc->scc, SBD_ERROR_LOG, "%s: %s\n", - cc->argv[i], para_strerror(-ret)); + lls_input(i, lpr), para_strerror(-ret)); if (ret < 0) - return ret; + goto out; continue; } if (ret == 1) /* directory */ @@ -1948,8 +1937,12 @@ int com_add(struct command_context *cc) } free(path); } - return 1; + ret = 1; +out: + free(pad.slpr); + return ret; } +EXPORT_SERVER_CMD_HANDLER(add); /** * Flags used by the touch command. diff --git a/m4/lls/server_cmd.suite.m4 b/m4/lls/server_cmd.suite.m4 index 0585b5e2..e5bdcede 100644 --- a/m4/lls/server_cmd.suite.m4 +++ b/m4/lls/server_cmd.suite.m4 @@ -13,6 +13,45 @@ aux_info_prefix = Permissions: granted to the user. [/introduction] +[subcommand add] + purpose = add or update audio files + non-opts-name = path... + aux_info = AFS_READ | AFS_WRITE + [description] + Each path must be absolute and refer to either an audio file or a + directory. In case of a directory, all audio files in that directory + are added recursively. Note that the given paths refer to files or + directories on the host on which para_server is running. + [/description] + [option all] + short_opt = a + summary = add all files + [help] + The default is to add only files ending in a known suffix for a + supported audio format. + [/help] + [option lazy] + short_opt = l + summary = add files lazily + [help] + If the path already exists in the database, skip this file. This + operation is really cheap. Useful to update large directories after + some files have been added. + [/help] + [option force] + short_opt = f + summary = force adding/updating + [help] + Recompute the audio format handler data even if a file with the same + path and the same hash value exists. + [/help] + [option verbose] + short_opt = v + summary = enable verbose mode + [help] + Print what is being done. + [/help] + [subcommand ff] purpose = jump N seconds forward or backward synopsis = n[-] -- 2.30.2