From eb9b4853c0502e483cd6c2b298f525be3e185f4d Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Mon, 12 Jun 2017 14:24:51 +0200 Subject: [PATCH] mufs: implement mufs_rmpath() to remove file system nodes --- source/fs/mufs/path.c | 35 +++++++++++++++++++++++++++++++++++ source/fs/mufs/path.h | 1 + 2 files changed, 36 insertions(+) diff --git a/source/fs/mufs/path.c b/source/fs/mufs/path.c index 274c4ce..1e784cb 100644 --- a/source/fs/mufs/path.c +++ b/source/fs/mufs/path.c @@ -67,6 +67,41 @@ int mufs_mkpath(struct mufs *fs, const char *path, unsigned type) return -1; } +int mufs_rmpath(struct mufs *fs, const char *path, unsigned type) +{ + struct flash_dev *dev = fs->dev; + char data[1 << dev->log2_block_size]; + struct mufs_dentry *entry; + struct mufs_tree *tree; + char *s; + uint32_t va, offset, len; + + if (!(s = mufs_abspath(path))) + return -1; + + if (!(tree = resolve_path(fs, s, NULL))) { + free(s); + return -1; + } + + offset = tree->va & ((UINT32_C(1) << dev->log2_block_size) - 1); + va = align(tree->va, dev->log2_block_size); + + if (mufs_tree_read(tree, data, va, 1 << dev->log2_block_size) == 0) + return -1; + + entry = (struct mufs_dentry *)(data + offset); + len = sizeof *entry + entry->path_len; + + memmove(data + offset, data + offset + len, + (UINT32_C(1) << dev->log2_block_size) - offset - len); + + if (mufs_tree_write(tree, data, va, 1 << dev->log2_block_size) == 0) + return -1; + + return 0; +} + int mufs_stat(struct mufs *fs, const char *path, struct mufs_stat *stat) { if (!resolve_path(fs, path, stat)) diff --git a/source/fs/mufs/path.h b/source/fs/mufs/path.h index ea906e8..b6aeddb 100644 --- a/source/fs/mufs/path.h +++ b/source/fs/mufs/path.h @@ -1,3 +1,4 @@ #pragma once int mufs_mkpath(struct mufs *fs, const char *path, unsigned type); +int mufs_rmpath(struct mufs *fs, const char *path, unsigned type);