diff options
author | David Härdeman <david@hardeman.nu> | 2020-07-12 14:04:40 +0200 |
---|---|---|
committer | David Härdeman <david@hardeman.nu> | 2020-07-12 14:04:40 +0200 |
commit | 6586ea650597ae0563c4234a7658499ce1e0117b (patch) | |
tree | be99b63c29a21b2492a35d0bc9cd2ade93388405 /minecctl | |
parent | 714c267f5e4af98fd986c3563fc0e8e82a3dae1f (diff) |
Teach minecproxy to use same dirs as minecctl, step 1
Diffstat (limited to 'minecctl')
-rw-r--r-- | minecctl/misc-commands.c | 9 | ||||
-rw-r--r-- | minecctl/misc.c | 69 | ||||
-rw-r--r-- | minecctl/misc.h | 14 | ||||
-rw-r--r-- | minecctl/server.c | 46 |
4 files changed, 11 insertions, 127 deletions
diff --git a/minecctl/misc-commands.c b/minecctl/misc-commands.c index db9b937..3ad9792 100644 --- a/minecctl/misc-commands.c +++ b/minecctl/misc-commands.c @@ -69,6 +69,7 @@ static bool write_cfg_file(int dfd, const char *name, static bool find_user_service(int xfd, const char *service) { _cleanup_close_ int dfd = -1; + _cleanup_free_ char *dpath = NULL; /* FIXME: Make this a macro, make paths #defines */ char sub_path[STRLEN("systemd/user/") + strlen(service) + 1]; char etc_path[STRLEN("/etc/systemd/user/") + strlen(service) + 1]; @@ -98,11 +99,11 @@ static bool find_user_service(int xfd, const char *service) return true; } - dfd = open_xdg_data_dir(false); + dfd = open_xdg_data_dir(false, &dpath); if (dfd >= 0) { if (faccessat(dfd, sub_path, R_OK, 0) == 0) { info("User service %s already installed in " - "$XDG_DATA_HOME/systemd/user/", service); + "%s/systemd/user/", service, dpath); return true; } } @@ -187,7 +188,7 @@ bool do_init(_unused_ struct cfg *cfg) _cleanup_close_ int xdfd = -1; _cleanup_close_ int mdfd = -1; - xcfd = open_xdg_cfg_dir(true); + xcfd = open_xdg_cfg_dir(true, NULL); if (xcfd < 0) return false; @@ -215,7 +216,7 @@ bool do_init(_unused_ struct cfg *cfg) ___examples_minecproxy_service_len)) return false; - xdfd = open_xdg_data_dir(true); + xdfd = open_xdg_data_dir(true, NULL); if (xdfd < 0) return false; diff --git a/minecctl/misc.c b/minecctl/misc.c index 90189dc..72c711a 100644 --- a/minecctl/misc.c +++ b/minecctl/misc.c @@ -8,81 +8,12 @@ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> -#include <pwd.h> #include <errno.h> #include "shared/utils.h" #include "misc.h" #include "minecctl.h" -static const char *get_homedir() -{ - const char *e; - struct passwd *passwd; - uid_t uid; - - e = getenv("HOME"); - if (e && e[0] == '/') - return e; - - uid = getuid(); - if (uid == 0) - return "/root"; - - passwd = getpwuid(uid); - if (passwd && passwd->pw_dir[0] == '/') - return passwd->pw_dir; - - return NULL; -} - -int open_subdir(int dfd, const char *subdir, bool nofail) -{ - int sfd; - - if (nofail && mkdirat(dfd, subdir, 0777) < 0 && errno != EEXIST) { - error("Unable to create subdirectory %s: %m", subdir); - return -1; - } - - sfd = openat(dfd, subdir, O_PATH | O_CLOEXEC | O_DIRECTORY); - if (sfd < 0 && nofail) - error("Unable to open subdirectory %s: %m", subdir); - - return sfd; -} - -int open_xdg_dir(const char *envname, const char *altpath, bool nofail) -{ - const char *e; - const char *h; - int dfd, hfd; - - e = getenv(envname); - if (e && e[0] == '/') { - dfd = open(e, O_PATH | O_CLOEXEC | O_DIRECTORY); - if (dfd < 0) - error("Unable to open $%s(%s): %m", envname, e); - return dfd; - } - - h = get_homedir(); - if (!h) { - error("Unable to determine home directory"); - return -1; - } - - hfd = open(h, O_PATH | O_CLOEXEC | O_DIRECTORY); - if (hfd < 0) { - error("Unable to open $HOME(%s): %m", h); - return -1; - } - - dfd = open_subdir(hfd, altpath, nofail); - close(hfd); - return dfd; -} - /* FIXME: Can be shared */ void set_use_colors() { diff --git a/minecctl/misc.h b/minecctl/misc.h index 02c01ea..3182ef8 100644 --- a/minecctl/misc.h +++ b/minecctl/misc.h @@ -2,20 +2,6 @@ #ifndef foomischfoo #define foomischfoo -int open_subdir(int dfd, const char *subdir, bool nofail); - -int open_xdg_dir(const char *envname, const char *altpath, bool nofail); - -static inline int open_xdg_data_dir(bool nofail) -{ - return open_xdg_dir("XDG_DATA_HOME", ".local/share", nofail); -} - -static inline int open_xdg_cfg_dir(bool nofail) -{ - return open_xdg_dir("XDG_CONFIG_HOME", ".config", nofail); -} - void set_use_colors(); char **strv_copy(char *const *strv); diff --git a/minecctl/server.c b/minecctl/server.c index 3acc537..e50e10c 100644 --- a/minecctl/server.c +++ b/minecctl/server.c @@ -9,63 +9,29 @@ #include "minecctl.h" #include "server.h" #include "misc.h" -#include "config.h" #define INVALID(msg) do { *error = (msg); return false; } while(0) -static DIR *__open_dir(int (*base_dir)(bool nofail), const char *fallback) -{ - _cleanup_close_ int xfd = -1; - _cleanup_close_ int mfd = -1; - DIR *dir; - - /* First, attempt per-user config dir... */ - xfd = base_dir(false); - if (xfd < 0) - goto fallback; - - mfd = openat(xfd, "minecproxy", O_CLOEXEC | O_DIRECTORY | O_RDONLY); - if (mfd < 0) - goto fallback; - - dir = fdopendir(mfd); - if (dir) - mfd = -1; - return dir; - - /* ...and fallback on the system dir */ -fallback: - return opendir(fallback); -} - -static inline DIR *open_cfg_dir() -{ - return __open_dir(open_xdg_cfg_dir, DEFAULT_CFG_DIR); -} - -static inline DIR *open_data_dir() -{ - return __open_dir(open_xdg_data_dir, DEFAULT_DATA_DIR); -} - void server_load_all_known(struct cfg *cfg) { struct dirent *dent; + _cleanup_free_ char *cpath = NULL; + _cleanup_free_ char *dpath = NULL; if (cfg->server_list_loaded) return; cfg->server_list_loaded = true; - cfg->cfg_dir = open_cfg_dir(); + cfg->cfg_dir = open_cfg_dir(NULL, &cpath); if (!cfg->cfg_dir) { - error("Failed to open config directory"); + error("Failed to open config directory %s", cpath); return; } - cfg->data_dir = open_data_dir(); + cfg->data_dir = open_data_dir(NULL, &dpath); if (!cfg->data_dir) { - error("Failed to open server directory"); + error("Failed to open server directory %s", dpath); return; } |