From cfb76ff6eecec15636f182009cda50ea0c654c60 Mon Sep 17 00:00:00 2001 From: Evan Green Date: Fri, 7 Dec 2018 16:03:43 -0800 Subject: [PATCH] storage: Allow specifying the storage directory Enable the specification of storage paths other than /boot, using a new -o command line argument. Getoptify the command line arguments for better processing. --- rmtfs.c | 23 ++++++++++++++++++++--- rmtfs.h | 2 +- storage.c | 24 +++++++++++++++++------- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/rmtfs.c b/rmtfs.c index c190d17..aa8e09a 100644 --- a/rmtfs.c +++ b/rmtfs.c @@ -448,15 +448,32 @@ static int run_rmtfs(void) int main(int argc, char **argv) { int ret; + int option; + const char *storage_root = NULL; - if (argc == 2 && strcmp(argv[1], "-v") == 0) - dbgprintf_enabled = true; + while ((option = getopt(argc, argv, "o:v")) != -1) { + switch (option) { + /* -o sets the directory where EFS images are stored. */ + case 'o': + storage_root = optarg; + break; + + /* -v is for verbose */ + case 'v': + dbgprintf_enabled = 1; + break; + + case '?': + fprintf(stderr, "Unknown option: -%c\n", option); + return 1; + } + } rmem = rmtfs_mem_open(); if (!rmem) return 1; - ret = storage_open(); + ret = storage_open(storage_root); if (ret) { fprintf(stderr, "failed to initialize storage system\n"); goto close_rmtfs_mem; diff --git a/rmtfs.h b/rmtfs.h index fdd600c..7ab8733 100644 --- a/rmtfs.h +++ b/rmtfs.h @@ -23,7 +23,7 @@ void rmtfs_mem_free(struct rmtfs_mem *rmem); ssize_t rmtfs_mem_read(struct rmtfs_mem *rmem, unsigned long phys_address, void *buf, ssize_t len); ssize_t rmtfs_mem_write(struct rmtfs_mem *rmem, unsigned long phys_address, const void *buf, ssize_t len); -int storage_open(void); +int storage_open(const char *storage_root); int storage_get(unsigned node, const char *path); int storage_put(unsigned node, int caller_id); int storage_get_handle(unsigned node, int caller_id); diff --git a/storage.c b/storage.c index 09092a7..b1fa4d4 100644 --- a/storage.c +++ b/storage.c @@ -22,20 +22,25 @@ struct caller { const struct partition *partition; }; +static const char *storage_dir = "/boot"; + static const struct partition partition_table[] = { - { "/boot/modem_fs1", "/boot/modem_fs1" }, - { "/boot/modem_fs2", "/boot/modem_fs2" }, - { "/boot/modem_fsc", "/boot/modem_fsc" }, - { "/boot/modem_fsg", "/boot/modem_fsg" }, + { "/boot/modem_fs1", "modem_fs1" }, + { "/boot/modem_fs2", "modem_fs2" }, + { "/boot/modem_fsc", "modem_fsc" }, + { "/boot/modem_fsg", "modem_fsg" }, {} }; static struct caller caller_handles[MAX_CALLERS]; -int storage_open(void) +int storage_open(const char *storage_root) { int i; + if (storage_root) + storage_dir = storage_root; + for (i = 0; i < MAX_CALLERS; i++) { caller_handles[i].id = i; caller_handles[i].fd = -1; @@ -46,8 +51,10 @@ int storage_open(void) int storage_get(unsigned node, const char *path) { + char *fspath; const struct partition *part; struct caller *caller = NULL; + size_t pathlen; int saved_errno; int fd; int i; @@ -80,11 +87,14 @@ found: return -EBUSY; } - fd = open(part->actual, O_RDWR); + pathlen = strlen(storage_dir) + strlen(part->actual) + 2; + fspath = alloca(pathlen); + snprintf(fspath, pathlen, "%s/%s", storage_dir, part->actual); + fd = open(fspath, O_RDWR); if (fd < 0) { saved_errno = errno; fprintf(stderr, "[storage] failed to open '%s' (requested '%s'): %s\n", - part->actual, part->path, strerror(saved_errno)); + fspath, part->path, strerror(saved_errno)); return -saved_errno; }