Skip to content

Commit

Permalink
Fix: -c command line option cleans up the config dir #1245
Browse files Browse the repository at this point in the history
  • Loading branch information
jcorporation committed Apr 3, 2024
1 parent e1860fb commit c18bfc0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This is a small bugfix release.

### Changelog

- Fix: mympd-config - cleanup before writing new values #1245
- Fix: `-c` command line option cleans up the config dir #1245

***

Expand Down
1 change: 0 additions & 1 deletion cli_tools/mympd-config/mympd-config
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ read_config() {
}

apply_config() {
find "${CONFIG_DIR}" -maxdepth 1 -type f -delete
if [ "$MYMPD_SYSTEMD" -eq 1 ]
then
systemd-run -p DynamicUser=yes -p StateDirectory=mympd -p CacheDirectory=mympd \
Expand Down
32 changes: 32 additions & 0 deletions src/lib/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "src/lib/utility.h"
#include "src/lib/validate.h"

#include <dirent.h>
#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
Expand Down Expand Up @@ -149,6 +151,36 @@ void mympd_config_defaults(struct t_config *config) {
FREE_SDS(album_group_tag_str);
}

/**
* Removes all files from the config directory
* @param config pointer to config struct
* @return bool true on success, else false
*/
bool mympd_config_rm(struct t_config *config) {
errno = 0;
sds filepath = sdscatfmt(sdsempty(), "%S/%s", config->workdir, DIR_WORK_CONFIG);
DIR *config_dir = opendir(filepath);
if (config_dir == NULL) {
MYMPD_LOG_ERROR(NULL, "Error opening directory \"%s\"", filepath);
MYMPD_LOG_ERRNO(NULL, errno);
FREE_SDS(filepath);
return false;
}

struct dirent *next_file;
while ((next_file = readdir(config_dir)) != NULL ) {
if (next_file->d_type != DT_REG) {
continue;
}
sdsclear(filepath);
filepath = sdscatfmt(filepath, "%S/%s/%s", config->workdir, DIR_WORK_CONFIG, next_file->d_name);
rm_file(filepath);
}
closedir(config_dir);
FREE_SDS(filepath);
return true;
}

/**
* Reads or writes the config from the /var/lib/mympd/config directory
* @param config pointer to config struct
Expand Down
1 change: 1 addition & 0 deletions src/lib/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
void mympd_config_defaults_initial(struct t_config *config);
void mympd_config_defaults(struct t_config *config);
void *mympd_config_free(struct t_config *config);
bool mympd_config_rm(struct t_config *config);
bool mympd_config_rw(struct t_config *config, bool write);
void mympd_autoconf(struct t_config *config);
bool mympd_version_set(sds workdir);
Expand Down
1 change: 1 addition & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ int main(int argc, char **argv) {
//bootstrap - write config files and exit
if (config->bootstrap == true) {
if (drop_privileges(config->user, startup_uid) == true &&
mympd_config_rm(config) == true &&
mympd_config_rw(config, true) == true &&
create_certificates(config) == true)
{
Expand Down

0 comments on commit c18bfc0

Please sign in to comment.