mufs: generalise mufs_create() and mufs_mkdir() into mufs_mkpath()
This commit is contained in:
parent
1c736c3324
commit
a65cdbfedf
5 changed files with 76 additions and 100 deletions
|
@ -10,6 +10,7 @@
|
|||
#include <fs/mufs.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include <fs/mufs.h>
|
||||
|
||||
#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)
|
||||
|
|
68
source/fs/mufs/path.c
Normal file
68
source/fs/mufs/path.c
Normal file
|
@ -0,0 +1,68 @@
|
|||
#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, 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;
|
||||
}
|
3
source/fs/mufs/path.h
Normal file
3
source/fs/mufs/path.h
Normal file
|
@ -0,0 +1,3 @@
|
|||
#pragma once
|
||||
|
||||
int mufs_mkpath(struct mufs *fs, const char *path, unsigned type);
|
Loading…
Add table
Add a link
Reference in a new issue