You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
213 lines
4.1 KiB
213 lines
4.1 KiB
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <flash.h>
|
|
#include <macros.h>
|
|
|
|
#include <fs/mufs.h>
|
|
|
|
#include "dir.h"
|
|
#include "path.h"
|
|
#include "tree.h"
|
|
|
|
int mufs_mkpath(struct mufs *fs, const char *path, struct mufs_tree *subtree,
|
|
unsigned type)
|
|
{
|
|
struct mufs_stat stat;
|
|
struct mufs_dirent dirent;
|
|
struct mufs_tree *tree;
|
|
char *s, *root, *name;
|
|
size_t len;
|
|
int ret;
|
|
|
|
if (!(s = mufs_abspath(path)))
|
|
return -1;
|
|
|
|
/* TODO: already exists. */
|
|
if (resolve_path(fs, s, NULL, NULL) == 0) {
|
|
free(s);
|
|
return 0;
|
|
}
|
|
|
|
root = s;
|
|
name = strrchr(s, '/');
|
|
*name++ = '\0';
|
|
|
|
if (*root == '\0')
|
|
root = "/";
|
|
|
|
if (*name == '\0')
|
|
goto err_free_s;
|
|
|
|
if (resolve_path(fs, root, &tree, &stat) < 0 || stat.type != MUFS_DIR)
|
|
goto err_free_s;
|
|
|
|
memset(&dirent, 0, sizeof dirent);
|
|
|
|
len = min(strlen(name), sizeof(dirent.path) - 1);
|
|
memcpy(dirent.path, name, len);
|
|
dirent.path[len] = '\0';
|
|
dirent.type = type;
|
|
|
|
dirent.tree.fs = fs;
|
|
|
|
if (subtree) {
|
|
dirent.tree.file_size = subtree->file_size;
|
|
dirent.tree.root = subtree->root;
|
|
dirent.tree.depth = subtree->depth;
|
|
}
|
|
|
|
tree->file_size = find_dirent_size(fs, tree, tree->file_size);
|
|
|
|
if (!is_aligned(tree->file_size, fs->dev->log2_block_size) &&
|
|
write_dirent(tree, tree->file_size, &dirent) > 0) {
|
|
mufs_del_tree(tree);
|
|
free(s);
|
|
return 0;
|
|
}
|
|
|
|
ret = write_dirent(tree, align_up(tree->file_size,
|
|
fs->dev->log2_block_size), &dirent);
|
|
mufs_del_tree(tree);
|
|
free(s);
|
|
|
|
return ret;
|
|
|
|
err_free_s:
|
|
free(s);
|
|
return -1;
|
|
}
|
|
|
|
static int remove_page(struct mufs_tree *tree, uint32_t va)
|
|
{
|
|
struct mufs *fs = tree->fs;
|
|
struct flash_dev *dev = fs->dev;
|
|
uint32_t last_va, page;
|
|
|
|
last_va = align(tree->file_size - 1, dev->log2_block_size);
|
|
|
|
if (mufs_lookup_page(tree, &page, last_va) < 0)
|
|
return -1;
|
|
|
|
mufs_free_page(tree, va);
|
|
|
|
if (va != last_va) {
|
|
mufs_unmap_page(tree, last_va);
|
|
mufs_map_page(tree, va, page);
|
|
}
|
|
|
|
tree->file_size = last_va;
|
|
|
|
return mufs_sync_tree(tree);
|
|
}
|
|
|
|
int mufs_rmpath(struct mufs *fs, const char *path)
|
|
{
|
|
struct flash_dev *dev = fs->dev;
|
|
char data[1 << dev->log2_block_size];
|
|
struct mufs_dentry *entry, *next;
|
|
struct mufs_dirent ent;
|
|
struct mufs_tree *tree;
|
|
struct mufs_dir *dir;
|
|
char *s, *name;
|
|
uint32_t va, offset, len;
|
|
int ret;
|
|
|
|
if (!(s = mufs_abspath(path)))
|
|
return -1;
|
|
|
|
name = strrchr(s, '/');
|
|
*name++ = '\0';
|
|
|
|
if (resolve_path(fs, s, &tree, NULL) < 0)
|
|
goto err_free_s;
|
|
|
|
if (!(dir = mufs_opendir(fs, s)))
|
|
goto err_del_tree;
|
|
|
|
for (va = dir->va; (ret = mufs_readdir(dir, &ent)) == 0; va = dir->va) {
|
|
if (strcmp(ent.path, name) == 0)
|
|
break;
|
|
}
|
|
|
|
mufs_closedir(dir);
|
|
|
|
if (ret < 0)
|
|
goto err_del_tree;
|
|
|
|
offset = va & ((UINT32_C(1) << dev->log2_block_size) - 1);
|
|
va = align(va, dev->log2_block_size);
|
|
|
|
if (mufs_tree_read(tree, data, va, 1 << dev->log2_block_size) == 0)
|
|
goto err_del_tree;
|
|
|
|
entry = (struct mufs_dentry *)(data + offset);
|
|
len = sizeof *entry + entry->path_len;
|
|
next = (struct mufs_dentry *)(data + offset + len);
|
|
|
|
if (!offset && (len == (UINT32_C(1) << dev->log2_block_size) ||
|
|
!next->type)) {
|
|
ret = remove_page(tree, va);
|
|
mufs_del_tree(tree);
|
|
free(s);
|
|
|
|
return ret;
|
|
}
|
|
|
|
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)
|
|
goto err_del_tree;
|
|
|
|
mufs_del_tree(tree);
|
|
free(s);
|
|
return 0;
|
|
|
|
err_del_tree:
|
|
mufs_del_tree(tree);
|
|
err_free_s:
|
|
free(s);
|
|
return -1;
|
|
}
|
|
|
|
int mufs_stat(struct mufs *fs, const char *path, struct mufs_stat *stat)
|
|
{
|
|
return resolve_path(fs, path, NULL, stat);
|
|
}
|
|
|
|
int mufs_rename(struct mufs *fs, const char *old, const char *new)
|
|
{
|
|
struct mufs_stat stat;
|
|
struct mufs_tree *subtree;
|
|
char *path;
|
|
unsigned type;
|
|
int ret;
|
|
|
|
if (!(path = mufs_abspath(old)))
|
|
return -1;
|
|
|
|
ret = resolve_path(fs, path, &subtree, &stat);
|
|
free(path);
|
|
|
|
if (ret < 0)
|
|
goto err_del_subtree;
|
|
|
|
type = stat.type;
|
|
|
|
if (mufs_mkpath(fs, new, subtree, type) < 0)
|
|
goto err_del_subtree;
|
|
|
|
if (mufs_rmpath(fs, old) < 0)
|
|
goto err_del_subtree;
|
|
|
|
mufs_del_tree(subtree);
|
|
return 0;
|
|
|
|
err_del_subtree:
|
|
mufs_del_tree(subtree);
|
|
return -1;
|
|
}
|
|
|