Functions to list installed and tracked bundles

This commit adds a couple of functions:
- bundle_list_track():  list the bundles that are tracked in the system
- bundle_list_installed(): list the bundles installed in the system

Signed-off-by: Castulo Martinez <castulo.martinez@intel.com>
This commit is contained in:
Castulo Martinez
2020-05-05 10:37:44 -07:00
committed by Castulo J. Martinez
parent d29865f8a9
commit b27e51356d
6 changed files with 67 additions and 29 deletions
+15 -4
View File
@@ -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 */
+27
View File
@@ -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;
}
+10
View File
@@ -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
+10 -14
View File
@@ -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 */
+3 -7
View File
@@ -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)
+2 -4
View File
@@ -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.