diff --git a/utils/src/dev.h b/utils/src/dev.h index 83dfffb4..f7017da4 100644 --- a/utils/src/dev.h +++ b/utils/src/dev.h @@ -1,7 +1,7 @@ #ifndef _DEV_H_ #define _DEV_H_ -#define BASE_SIZE_FMT "%.2f %s" +#define BASE_SIZE_FMT "%.2f%s" #define BASE_SIZE_ARGS(sz) size_flt(sz, 1), size_str(sz, 1) #define SIZE_FMT "%llu (%.2f %s)" diff --git a/utils/src/util.c b/utils/src/util.c new file mode 100644 index 00000000..dfa948ff --- /dev/null +++ b/utils/src/util.c @@ -0,0 +1,70 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "util.h" + +static int open_path(char *path, int flags) +{ + wordexp_t exp_result; + int ret; + + ret = wordexp(path, &exp_result, WRDE_NOCMD | WRDE_SHOWERR | WRDE_UNDEF); + if (ret) { + fprintf(stderr, "wordexp() failure for \"%s\": %d\n", path, ret); + ret = -EINVAL; + goto out; + } + + ret = open(exp_result.we_wordv[0], flags); + if (ret < 0) { + ret = -errno; + fprintf(stderr, "failed to open '%s': %s (%d)\n", + path, strerror(errno), errno); + } + +out: + wordfree(&exp_result); + + return ret; +} + +/* + * 1. if path option given, use that + * 2. if env var, use that + * 3. if cwd is in a scoutfs fs, use that + * 4. else error + */ +int get_path(char *path, int flags) +{ + char *env_path; + char *cur_dir_path; + int ret; + + if (path) + return open_path(path, flags); + + env_path = getenv("SCOUTFS_PATH"); + if (env_path) + return open_path(path, flags); + + cur_dir_path = get_current_dir_name(); + if (!cur_dir_path) { + ret = -errno; + return ret; + } + + ret = open_path(cur_dir_path, flags); + free(cur_dir_path); + + // TODO: check this is within a scoutfs mount? + + return ret; +} diff --git a/utils/src/util.h b/utils/src/util.h index 637fdf5b..c70ddbb5 100644 --- a/utils/src/util.h +++ b/utils/src/util.h @@ -111,4 +111,6 @@ static inline int memcmp_lens(const void *a, int a_len, return memcmp(a, b, len) ?: a_len - b_len; } +int get_path(char *path, int flags); + #endif