diff --git a/file_path_special.c b/file_path_special.c index 8a9b60dadc..349359790d 100644 --- a/file_path_special.c +++ b/file_path_special.c @@ -80,7 +80,7 @@ void fill_pathname_expand_special(char *out_path, application_dir[0] = '\0'; fill_pathname_application_path(application_dir, sizeof(application_dir)); - path_basedir(application_dir); + path_basedir_wrapper(application_dir); src_size = strlcpy(out_path, application_dir, size); retro_assert(src_size < size); @@ -122,7 +122,7 @@ void fill_pathname_abbreviate_special(char *out_path, notations [2] = NULL; fill_pathname_application_path(application_dir, sizeof(application_dir)); - path_basedir(application_dir); + path_basedir_wrapper(application_dir); for (i = 0; candidates[i]; i++) { @@ -458,3 +458,79 @@ void fill_pathname_application_special(char *s, size_t len, enum application_spe break; } } + +/** + * fill_short_pathname_representation: + * @out_rep : output representation + * @in_path : input path + * @size : size of output representation + * + * Generates a short representation of path. It should only + * be used for displaying the result; the output representation is not + * binding in any meaningful way (for a normal path, this is the same as basename) + * In case of more complex URLs, this should cut everything except for + * the main image file. + * + * E.g.: "/path/to/game.img" -> game.img + * "/path/to/myarchive.7z#folder/to/game.img" -> game.img + */ +void fill_short_pathname_representation_wrapper(char* out_rep, + const char *in_path, size_t size) +{ + char path_short[PATH_MAX_LENGTH]; +#ifdef HAVE_COMPRESSION + char *last_slash = NULL; +#endif + + path_short[0] = '\0'; + + fill_pathname(path_short, path_basename(in_path), "", + sizeof(path_short)); + +#ifdef HAVE_COMPRESSION + last_slash = find_last_slash(path_short); + if (last_slash != NULL) + { + /* We handle paths like: + * /path/to/file.7z#mygame.img + * short_name: mygame.img: + * + * We check whether something is actually + * after the hash to avoid going over the buffer. + */ + retro_assert(strlen(last_slash) > 1); + strlcpy(out_rep, last_slash + 1, size); + return; + } +#endif + + fill_short_pathname_representation(out_rep, in_path, size); +} + +/** + * path_basedir: + * @path : path + * + * Extracts base directory by mutating path. + * Keeps trailing '/'. + **/ +void path_basedir_wrapper(char *path) +{ + char *last = NULL; + if (strlen(path) < 2) + return; + +#ifdef HAVE_COMPRESSION + /* We want to find the directory with the archive in basedir. */ + last = (char*)path_get_archive_delim(path); + if (last) + *last = '\0'; +#endif + + last = find_last_slash(path); + + if (last) + last[1] = '\0'; + else + snprintf(path, 3, ".%s", path_default_slash()); +} diff --git a/file_path_special.h b/file_path_special.h index fbc6e47848..521866143a 100644 --- a/file_path_special.h +++ b/file_path_special.h @@ -100,6 +100,33 @@ enum application_special_type APPLICATION_SPECIAL_DIRECTORY_ASSETS_ZARCH_ICONS }; +/** + * fill_short_pathname_representation: + * @out_rep : output representation + * @in_path : input path + * @size : size of output representation + * + * Generates a short representation of path. It should only + * be used for displaying the result; the output representation is not + * binding in any meaningful way (for a normal path, this is the same as basename) + * In case of more complex URLs, this should cut everything except for + * the main image file. + * + * E.g.: "/path/to/game.img" -> game.img + * "/path/to/myarchive.7z#folder/to/game.img" -> game.img + */ +void fill_short_pathname_representation_wrapper(char* out_rep, + const char *in_path, size_t size); + +/** + * path_basedir: + * @path : path + * + * Extracts base directory by mutating path. + * Keeps trailing '/'. + **/ +void path_basedir_wrapper(char *path); + const char *file_path_str(enum file_path_enum enum_idx); bool fill_pathname_application_data(char *s, size_t len); diff --git a/libretro-common/file/file_path.c b/libretro-common/file/file_path.c index ca2ae1756c..a58e7d3a78 100644 --- a/libretro-common/file/file_path.c +++ b/libretro-common/file/file_path.c @@ -477,13 +477,6 @@ void path_basedir(char *path) if (strlen(path) < 2) return; -#ifdef HAVE_COMPRESSION - /* We want to find the directory with the archive in basedir. */ - last = (char*)path_get_archive_delim(path); - if (last) - *last = '\0'; -#endif - last = find_last_slash(path); if (last) @@ -721,32 +714,13 @@ void fill_short_pathname_representation(char* out_rep, const char *in_path, size_t size) { char path_short[PATH_MAX_LENGTH]; -#ifdef HAVE_COMPRESSION - char *last_slash = NULL; -#endif path_short[0] = '\0'; fill_pathname(path_short, path_basename(in_path), "", sizeof(path_short)); -#ifdef HAVE_COMPRESSION - last_slash = find_last_slash(path_short); - if (last_slash != NULL) - { - /* We handle paths like: - * /path/to/file.7z#mygame.img - * short_name: mygame.img: - * - * We check whether something is actually - * after the hash to avoid going over the buffer. - */ - retro_assert(strlen(last_slash) > 1); - strlcpy(out_rep, last_slash + 1, size); - } - else -#endif - strlcpy(out_rep, path_short, size); + strlcpy(out_rep, path_short, size); } void fill_short_pathname_representation_noext(char* out_rep, diff --git a/menu/cbs/menu_cbs_ok.c b/menu/cbs/menu_cbs_ok.c index aaf198a5e4..ba16d33d66 100644 --- a/menu/cbs/menu_cbs_ok.c +++ b/menu/cbs/menu_cbs_ok.c @@ -2370,7 +2370,7 @@ static void cb_generic_download(void *task_data, transf->path, sizeof(output_path)); /* Make sure the directory exists */ - path_basedir(output_path); + path_basedir_wrapper(output_path); if (!path_mkdir(output_path)) { diff --git a/paths.c b/paths.c index 64b9b916cc..08ba73fd79 100644 --- a/paths.c +++ b/paths.c @@ -237,7 +237,7 @@ void path_set_basename(const char *path) * directory then and the name of srm and states are meaningful. * */ - path_basedir(path_main_basename); + path_basedir_wrapper(path_main_basename); fill_pathname_dir(path_main_basename, path, "", sizeof(path_main_basename)); #endif diff --git a/tasks/task_content.c b/tasks/task_content.c index 94e31c3db7..57db1866cd 100644 --- a/tasks/task_content.c +++ b/tasks/task_content.c @@ -1047,7 +1047,7 @@ static void update_firmware_status(void) else { strlcpy(s, path_get(RARCH_PATH_CONTENT) ,sizeof(s)); - path_basedir(s); + path_basedir_wrapper(s); firmware_info.directory.system = s; } diff --git a/tasks/task_decompress.c b/tasks/task_decompress.c index 9250fa4011..709270d65a 100644 --- a/tasks/task_decompress.c +++ b/tasks/task_decompress.c @@ -24,6 +24,7 @@ #include #include "tasks_internal.h" +#include "../file_path_special.h" #include "../verbosity.h" #include "../msg_hash.h" @@ -98,7 +99,7 @@ static int file_decompressed(const char *name, const char *valid_exts, /* Make directory */ fill_pathname_join(path, dec->target_dir, name, sizeof(path)); - path_basedir(path); + path_basedir_wrapper(path); if (!path_mkdir(path)) goto error;