diff --git a/src/alias.c b/src/alias.c index f97005dc..093cd981 100644 --- a/src/alias.c +++ b/src/alias.c @@ -162,14 +162,25 @@ struct list *get_alias_definitions(void) struct list *iteru = NULL; struct list *system_alias_files = NULL; struct list *user_alias_files = NULL; + struct list *tmp_list = NULL; /* get sorted system and user filenames */ - string_or_die(&path, "%s/%s", globals.path_prefix, SYSTEM_ALIAS_PATH); - system_alias_files = get_dir_files_sorted(path); + path = sys_path_join("%s/%s", globals.path_prefix, SYSTEM_ALIAS_PATH); + tmp_list = sys_ls(path); + for (iters = tmp_list; iters; iters = iters->next) { + system_alias_files = list_prepend_data(system_alias_files, str_or_die("%s/%s", path, (char *)iters->data)); + } + system_alias_files = list_sort(system_alias_files, str_cmp_wrapper); + list_free_list_and_data(tmp_list, free); FREE(path); - string_or_die(&path, "%s/%s", globals.path_prefix, USER_ALIAS_PATH); - user_alias_files = get_dir_files_sorted(path); + path = sys_path_join("%s/%s", globals.path_prefix, USER_ALIAS_PATH); + tmp_list = sys_ls(path); + for (iters = tmp_list; iters; iters = iters->next) { + user_alias_files = list_prepend_data(user_alias_files, str_or_die("%s/%s", path, (char *)iters->data)); + } + user_alias_files = list_sort(user_alias_files, str_cmp_wrapper); + list_free_list_and_data(tmp_list, free); FREE(path); /* get a combined list with user files overriding system files */ diff --git a/src/bundle.c b/src/bundle.c index 7956c654..b17ffb60 100644 --- a/src/bundle.c +++ b/src/bundle.c @@ -238,3 +238,30 @@ void track_bundle(const char *bundle_name) { track_bundle_in_statedir(bundle_name, globals.state_dir); } + +static char *get_bundles_dir(void) +{ + return sys_path_join("%s/%s", globals.path_prefix, BUNDLES_DIR); +} + +struct list *bundle_list_tracked(void) +{ + struct list *bundles = NULL; + char *tracking_dir = get_tracking_dir(); + + bundles = sys_ls(tracking_dir); + FREE(tracking_dir); + + return bundles; +} + +struct list *bundle_list_installed(void) +{ + struct list *bundles = NULL; + char *bundles_dir = get_bundles_dir(); + + bundles = sys_ls(bundles_dir); + FREE(bundles_dir); + + return bundles; +} diff --git a/src/bundle.h b/src/bundle.h index 142d9b22..add3cc41 100644 --- a/src/bundle.h +++ b/src/bundle.h @@ -62,6 +62,16 @@ void track_bundle_in_statedir(const char *bundle_name, const char *state_dir); */ void track_bundle(const char *bundle_name); +/** + * @brief Returns the list of tracked bundles. + */ +struct list *bundle_list_tracked(void); + +/** + * @brief Returns the list of installed bundles. + */ +struct list *bundle_list_installed(void); + #ifdef __cplusplus } #endif diff --git a/src/bundle_list.c b/src/bundle_list.c index 3b6eeda1..48df44af 100644 --- a/src/bundle_list.c +++ b/src/bundle_list.c @@ -167,8 +167,8 @@ static bool parse_options(int argc, char **argv) */ static enum swupd_code list_local_bundles(int version) { + enum swupd_code ret_code = SWUPD_OK; char *name; - char *path = NULL; struct list *bundles = NULL; struct list *item = NULL; struct manifest *MoM = NULL; @@ -183,23 +183,19 @@ static enum swupd_code list_local_bundles(int version) } } - string_or_die(&path, "%s/%s", globals.path_prefix, BUNDLES_DIR); - - errno = 0; - bundles = get_dir_files_sorted(path); - if (!bundles && errno) { - error("couldn't open bundles directory"); - FREE(path); - return SWUPD_COULDNT_LIST_DIR; + bundles = bundle_list_installed(); + if (!bundles) { + ret_code = SWUPD_COULDNT_LIST_DIR; + goto out; } + bundles = list_sort(bundles, str_cmp_wrapper); progress_next_step("list_bundles", PROGRESS_UNDEFINED); - info("Installed bundles:\n"); item = bundles; while (item) { if (MoM) { - bundle_manifest = mom_search_bundle(MoM, sys_basename((char *)item->data)); + bundle_manifest = mom_search_bundle(MoM, (char *)item->data); } if (bundle_manifest) { name = get_printable_bundle_name(bundle_manifest->filename, bundle_manifest->is_experimental, cmdline_option_status && is_installed_bundle(bundle_manifest->filename), cmdline_option_status && is_tracked_bundle(bundle_manifest->filename)); @@ -208,7 +204,7 @@ static enum swupd_code list_local_bundles(int version) FREE(name); } else { info(" - "); - print("%s\n", sys_basename((char *)item->data)) + print("%s\n", (char *)item->data); } count++; item = item->next; @@ -217,10 +213,10 @@ static enum swupd_code list_local_bundles(int version) list_free_list_and_data(bundles, free); - FREE(path); +out: manifest_free(MoM); - return SWUPD_OK; + return ret_code; } /* Return recursive list of included bundles */ diff --git a/src/lib/sys.c b/src/lib/sys.c index ccd9ca7e..9df5df80 100644 --- a/src/lib/sys.c +++ b/src/lib/sys.c @@ -245,11 +245,7 @@ int rm_rf(const char *file) return run_command_quiet("/bin/rm", "-rf", file, NULL); } -/* Get a list of files in a directory sorted by filename - * with their fullpath, returns NULL on error (errno set by - * opendir). - */ -struct list *get_dir_files_sorted(char *path) +struct list *sys_ls(char *path) { DIR *dir = NULL; struct dirent *ent = NULL; @@ -267,7 +263,7 @@ struct list *get_dir_files_sorted(char *path) if (ent->d_name[0] == '.') { continue; } - string_or_die(&name, "%s/%s", path, ent->d_name); + string_or_die(&name, "%s", ent->d_name); files = list_prepend_data(files, name); } @@ -277,7 +273,7 @@ struct list *get_dir_files_sorted(char *path) } (void)closedir(dir); - return list_sort(files, str_cmp_wrapper); + return files; } bool sys_file_exists(const char *filename) diff --git a/src/lib/sys.h b/src/lib/sys.h index 8afb82e0..2efdbd82 100644 --- a/src/lib/sys.h +++ b/src/lib/sys.h @@ -89,10 +89,8 @@ int mkdir_p(const char *dir); */ int rm_rf(const char *file); -/** - * @brief Return a list of all files in a directory, sorted lexicographically - */ -struct list *get_dir_files_sorted(char *path); +/* @brief Return a list of files in a directory */ +struct list *sys_ls(char *path); /** * @brief Checks if a file exists in the filesystem.