Browse Source

Create configuration directory if needed.

If gfsec-split is to create a configuration file in the "standard"
configuration directory ($XDG_CONFIG_HOME/gfsecret), make sure
that directory exists.

Closes #6
master
Damien Goutte-Gattat 2 years ago
parent
commit
9e21851af6
  1. 15
      src/gfsec-split.c
  2. 41
      src/util.c
  3. 3
      src/util.h

15
src/gfsec-split.c

@ -225,11 +225,20 @@ transform_config_pathname(char **cfg, const char *secret)
if ( base ) {
const char *env_info;
char *config_dir = NULL;
if ( (env_info = getenv("XDG_CONFIG_HOME")) && *env_info )
xasprintf(cfg, "%s/gfsecret/%s.conf", env_info, base);
if ( (env_info = getenv("XDG_CONFIG_HOME")) && *env_info)
xasprintf(&config_dir, "%s/gfsecret", env_info);
else if ( (env_info = getenv("HOME")) && *env_info )
xasprintf(cfg, "%s/.config/gfsecret/%s.conf", env_info, base);
xasprintf(&config_dir, "%s/.config/gfsecret", env_info);
if ( config_dir ) {
if ( makedir(config_dir, 0700) == -1 )
err(EXIT_FAILURE, "Cannot create configuration directory");
xasprintf(cfg, "%s/%s.conf", config_dir, base);
free(config_dir);
}
else
xasprintf(cfg, "%s.conf", base);

41
src/util.c

@ -169,3 +169,44 @@ get_file_basename(const char *path)
return xstrndup(path, n);
}
/**
* Creates a directory and its parents.
* This function creates the specified directory. Parent directories
* are created as needed.
*
* @param path The pathname of the directory to create.
* @param mode The permissions of the directory, as in creat(2).
*
* @return 0 if the directory was successfully created (or if it
* already existed), or -1 if an error occured.
*/
int
makedir(const char *path, int mode)
{
char *p, q;
int ret;
struct stat st_buf;
assert(path != NULL);
for ( p = (char *)(path + 1), ret = 0; *(p - 1) && ret == 0; p++ ) {
if ( *p == '/' || *p == '\0' ) {
q = *p, *p = '\0';
if ( stat(path, &st_buf) == -1 ) {
/* Path does not exist, attempt to create it. */
ret = mkdir(path, mode);
}
else if ( ! S_ISDIR(st_buf.st_mode) ) {
/* Path exists but is not a directory. */
errno = ENOTDIR;
ret = -1;
}
*p = q;
}
}
return ret;
}

3
src/util.h

@ -41,6 +41,9 @@ write_file(const char *, const unsigned char *, size_t);
char *
get_file_basename(const char *);
int
makedir(const char *, int);
#ifdef __cplusplus
}
#endif

Loading…
Cancel
Save