From a65cdbfedf5813154e8e99b3b4c6a137916786c8 Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Sun, 11 Jun 2017 19:27:53 +0200 Subject: [PATCH] mufs: generalise mufs_create() and mufs_mkdir() into mufs_mkpath() --- Makefile | 1 + source/fs/mufs/dir.c | 52 ++------------------------------------- source/fs/mufs/file.c | 52 ++------------------------------------- source/fs/mufs/path.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ source/fs/mufs/path.h | 3 +++ 5 files changed, 76 insertions(+), 100 deletions(-) create mode 100644 source/fs/mufs/path.c create mode 100644 source/fs/mufs/path.h diff --git a/Makefile b/Makefile index b00d018..ad8246b 100644 --- a/Makefile +++ b/Makefile @@ -26,6 +26,7 @@ obj-y += source/core/flash.o obj-y += source/fs/mufs/block.o obj-y += source/fs/mufs/dir.o obj-y += source/fs/mufs/file.o +obj-y += source/fs/mufs/path.o obj-y += source/fs/mufs/super.o obj-y += source/fs/mufs/tree.o diff --git a/source/fs/mufs/dir.c b/source/fs/mufs/dir.c index 643eb1e..6b477d5 100644 --- a/source/fs/mufs/dir.c +++ b/source/fs/mufs/dir.c @@ -10,6 +10,7 @@ #include #include "dir.h" +#include "path.h" #include "tree.h" struct mufs_dir { @@ -280,54 +281,5 @@ int mufs_readdir(struct mufs_dir *dir, struct mufs_dirent *dirent) int mufs_mkdir(struct mufs *fs, const char *path) { - struct mufs_dirent dirent; - struct mufs_tree *tree; - size_t len; - char *s; - char *name; - - if (!path || *path == '\0') - return -1; - - if (!(s = mufs_abspath(path))) - return -1; - - /* TODO: already exists. */ - if (resolve_path(fs, s)) { - free(s); - return 0; - } - - if (!(name = strrchr(s, '/'))) { - name = s; - s = "/"; - } else { - *name++ = '\0'; - } - - if (*name == '\0') - return -1; - - if (!(tree = resolve_path(fs, s))) - return -1; - - len = min(strlen(name), sizeof(dirent.path) - 1); - memcpy(dirent.path, name, len); - dirent.path[len] = '\0'; - dirent.type = MUFS_DIR; - - dirent.tree.fs = fs; - dirent.tree.file_size = 0; - dirent.tree.root = 0; - dirent.tree.depth = 0; - - if (!is_aligned(tree->file_size, fs->dev->log2_block_size) && - write_dirent(tree, tree->file_size, &dirent) > 0) - return 0; - - if (write_dirent(tree, align_up(tree->file_size, - fs->dev->log2_block_size), &dirent) > 0) - return 0; - - return -1; + return mufs_mkpath(fs, path, MUFS_DIR); } diff --git a/source/fs/mufs/file.c b/source/fs/mufs/file.c index 4786b3c..baca57f 100644 --- a/source/fs/mufs/file.c +++ b/source/fs/mufs/file.c @@ -9,6 +9,7 @@ #include #include "dir.h" +#include "path.h" #include "tree.h" struct mufs_file { @@ -19,56 +20,7 @@ struct mufs_file { int mufs_create(struct mufs *fs, const char *path) { - struct mufs_dirent dirent; - struct mufs_tree *tree; - size_t len; - char *s; - char *name; - - if (!path || *path == '\0') - return -1; - - if (!(s = mufs_abspath(path))) - return -1; - - /* TODO: already exists. */ - if (resolve_path(fs, s)) { - free(s); - return 0; - } - - if (!(name = strrchr(s, '/'))) { - name = s; - s = "/"; - } else { - *name++ = '\0'; - } - - if (*name == '\0') - return -1; - - if (!(tree = resolve_path(fs, s))) - return -1; - - len = min(strlen(name), sizeof(dirent.path) - 1); - memcpy(dirent.path, name, len); - dirent.path[len] = '\0'; - dirent.type = MUFS_FILE; - - dirent.tree.fs = fs; - dirent.tree.file_size = 0; - dirent.tree.root = 0; - dirent.tree.depth = 0; - - if (!is_aligned(tree->file_size, fs->dev->log2_block_size) && - write_dirent(tree, tree->file_size, &dirent) > 0) - return 0; - - if (write_dirent(tree, align_up(tree->file_size, - fs->dev->log2_block_size), &dirent) > 0) - return 0; - - return -1; + return mufs_mkpath(fs, path, MUFS_FILE); } struct mufs_file *mufs_open(struct mufs *fs, const char *path) diff --git a/source/fs/mufs/path.c b/source/fs/mufs/path.c new file mode 100644 index 0000000..054185a --- /dev/null +++ b/source/fs/mufs/path.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include "dir.h" +#include "path.h" +#include "tree.h" + +int mufs_mkpath(struct mufs *fs, const char *path, unsigned type) +{ + struct mufs_dirent dirent; + struct mufs_tree *tree; + size_t len; + char *s; + char *name; + + if (!path || *path == '\0') + return -1; + + if (!(s = mufs_abspath(path))) + return -1; + + /* TODO: already exists. */ + if (resolve_path(fs, s)) { + free(s); + return 0; + } + + if (!(name = strrchr(s, '/'))) { + name = s; + s = "/"; + } else { + *name++ = '\0'; + } + + if (*name == '\0') + return -1; + + if (!(tree = resolve_path(fs, s))) + return -1; + + len = min(strlen(name), sizeof(dirent.path) - 1); + memcpy(dirent.path, name, len); + dirent.path[len] = '\0'; + dirent.type = type; + + dirent.tree.fs = fs; + dirent.tree.file_size = 0; + dirent.tree.root = 0; + dirent.tree.depth = 0; + + if (!is_aligned(tree->file_size, fs->dev->log2_block_size) && + write_dirent(tree, tree->file_size, &dirent) > 0) + return 0; + + if (write_dirent(tree, align_up(tree->file_size, + fs->dev->log2_block_size), &dirent) > 0) + return 0; + + return -1; +} diff --git a/source/fs/mufs/path.h b/source/fs/mufs/path.h new file mode 100644 index 0000000..ea906e8 --- /dev/null +++ b/source/fs/mufs/path.h @@ -0,0 +1,3 @@ +#pragma once + +int mufs_mkpath(struct mufs *fs, const char *path, unsigned type);