X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=aft.c;h=ac7d9a1a34c69412242bef6631841238f07d8aea;hp=82724c3d10ca578bde9e662803571c5fee333e49;hb=f51c95fa0edaaf651387456e716899433a0c579c;hpb=727ae07c46980a5115f9085bee6c72ef3109c8bb diff --git a/aft.c b/aft.c index 82724c3d..ac7d9a1a 100644 --- a/aft.c +++ b/aft.c @@ -205,40 +205,38 @@ static int verify_dotfile(const char *rest) return 1; } -static int verify_path(const char *path, char **resolved_path) +static int verify_path(const char *orig_path, char **resolved_path) { - const char *orig_path = path; char c; const char prefix[] = AFS_AUDIO_FILE_DIR "/"; + const char *path = orig_path; const size_t prefix_len = strlen(prefix); - PARA_DEBUG_LOG("path: %s\n", path); c = *path++; if (!c) - return -E_BAD_PATH; + goto bad_path; while (c) { if (c == '/') { c = *path++; switch (c) { default: continue; - case '/': case '\0': - break; + case '/': /* double slash */ + goto bad_path; case '.': - if (verify_dotfile(path) > 0) - continue; + if (verify_dotfile(path) < 0) + goto bad_path; } - *resolved_path = NULL; - return -E_BAD_PATH; } c = *path++; } - if (*orig_path == '/') - *resolved_path = prefix_path("", 0, orig_path); - else + if (*orig_path != '/') *resolved_path = prefix_path(prefix, prefix_len, orig_path); - PARA_DEBUG_LOG("resolved: %s\n", *resolved_path); - return *resolved_path? 1: -E_BAD_PATH; + else + *resolved_path = para_strdup(orig_path); + return 1; +bad_path: + return -E_BAD_PATH; } enum afhi_offsets { @@ -288,7 +286,7 @@ static unsigned sizeof_chunk_info_buf(struct audio_format_info *afhi) } /** The offsets of the data contained in the AFTCOL_CHUNKS column. */ -enum chunk_info_offsets { +enum chunk_info_offsets{ /** The total number of chunks (4 bytes). */ CHUNKS_TOTAL_OFFSET = 0, /** The length of the audio file header (4 bytes). */ @@ -1394,7 +1392,6 @@ static int hash_sister_callback(const struct osl_object *query, return 1; } - static int add_one_audio_file(const char *arg, const void *private_data) { int ret; @@ -1402,15 +1399,15 @@ static int add_one_audio_file(const char *arg, const void *private_data) const struct private_add_data *pad = private_data; struct audio_format_info afhi, *afhi_ptr = NULL; struct osl_row *pb = NULL, *hs = NULL; /* path brother/hash sister */ - struct osl_object map, obj, query, result; - char *path; + struct osl_object map, obj = {.data = NULL}, query, result; + char *path = NULL; HASH_TYPE hash[HASH_SIZE]; afhi.header_offset = 0; afhi.header_len = 0; ret = verify_path(arg, &path); if (ret < 0) - return ret; + goto out_free; query.data = path; query.size = strlen(path) + 1; ret = send_callback_request(path_brother_callback, &query, &result); @@ -1471,13 +1468,13 @@ out_unmap: munmap(map.data, map.size); out_free: if (ret < 0) - send_va_buffer(pad->fd, "failed to add %s (%s)\n", path, - PARA_STRERROR(-ret)); + send_va_buffer(pad->fd, "failed to add %s (%s)\n", path? + path : arg, PARA_STRERROR(-ret)); free(obj.data); free(path); if (afhi_ptr) free(afhi_ptr->chunk_table); - return ret; + return 1; /* it's not an error if not all files could be added */ } int com_add(int fd, int argc, char * const * const argv) @@ -1511,15 +1508,21 @@ int com_add(int fd, int argc, char * const * const argv) if (argc <= i) return -E_AFT_SYNTAX; for (; i < argc; i++) { - ret = stat(argv[i], &statbuf); + char *path = para_strdup(argv[i]); + size_t len = strlen(path); + while (len > 1 && path[--len] == '/') + path[len] = '\0'; + ret = stat(path, &statbuf); if (ret < 0) - return -E_AFS_STAT; - if (S_ISDIR(statbuf.st_mode)) { - ret = for_each_file_in_dir(argv[i], - add_one_audio_file, &pad); - continue; - } - ret = add_one_audio_file(argv[i], &pad); + PARA_NOTICE_LOG("failed to stat %s (%s)", path, + strerror(errno)); + else + if (S_ISDIR(statbuf.st_mode)) + for_each_file_in_dir(path, add_one_audio_file, + &pad); + else + add_one_audio_file(path, &pad); + free(path); } ret = 1; return ret;