mirror of
https://github.com/versity/scoutfs.git
synced 2026-09-06 08:06:56 +00:00
Add get_path()
Implement a fallback mechanism for opening paths to a filesystem. If explicitly given, use that. If env var is set, use that. Otherwise, use current working directory. Use wordexp to expand ~, $HOME, etc. Signed-off-by: Andy Grover <agrover@versity.com>
This commit is contained in:
+1
-1
@@ -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)"
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <wordexp.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user