subsys/fs: add implementation helper module

File system API functions that operate on paths are passed both the
absolute path including the mount point prefix, and the mount point
within which the path lies.

Unfortunately it's not entirely trivial to convert an arbitrary path
within the file system space to an absolute path within its mount point,
because the path may be to the mount point itself and not end with a
directory separator.  The effect is that a file system implementation
like nffs may be given an empty path when "/" is required.

Add an implementation module that does this transformation and use it to
transform paths within each filesystem wrapper.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
This commit is contained in:
Peter A. Bigot 2019-07-21 17:06:54 -05:00 committed by Carles Cufí
parent 60bf4da4aa
commit 312f05eaef
4 changed files with 87 additions and 19 deletions

View file

@ -6,7 +6,8 @@ if(CONFIG_FILE_SYSTEM)
#zephyr_link_interface_ifdef(CONFIG_FILE_SYSTEM_NFFS NFFS) #zephyr_link_interface_ifdef(CONFIG_FILE_SYSTEM_NFFS NFFS)
zephyr_library() zephyr_library()
zephyr_library_sources(fs.c) zephyr_library_include_directories(${CMAKE_CURRENT_SOURCE_DIR})
zephyr_library_sources(fs.c fs_impl.c)
zephyr_library_sources_ifdef(CONFIG_FAT_FILESYSTEM_ELM fat_fs.c) zephyr_library_sources_ifdef(CONFIG_FAT_FILESYSTEM_ELM fat_fs.c)
zephyr_library_sources_ifdef(CONFIG_FILE_SYSTEM_NFFS nffs_fs.c) zephyr_library_sources_ifdef(CONFIG_FILE_SYSTEM_NFFS nffs_fs.c)
zephyr_library_sources_ifdef(CONFIG_FILE_SYSTEM_SHELL shell.c) zephyr_library_sources_ifdef(CONFIG_FILE_SYSTEM_SHELL shell.c)

22
subsys/fs/fs_impl.c Normal file
View file

@ -0,0 +1,22 @@
/*
* Copyright (c) 2019 Peter Bigot Consulting, LLC
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <fs/fs.h>
#include "fs_impl.h"
const char *fs_impl_strip_prefix(const char *path,
const struct fs_mount_t *mp)
{
static const char *const root = "/";
if ((path == NULL) || (mp == NULL)) {
return path;
}
path += mp->mountp_len;
return *path ? path : root;
}

36
subsys/fs/fs_impl.h Normal file
View file

@ -0,0 +1,36 @@
/*
* Copyright (c) 2019 Peter Bigot Consulting, LLC
*
* SPDX-License-Identifier: Apache-2.0
*/
/* Utility functions for use by filesystem implementations. */
#ifndef ZEPHYR_SUBSYS_FS_FS_IMPL_H_
#define ZEPHYR_SUBSYS_FS_FS_IMPL_H_
#include <fs.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Strip the mount point prefix from a path.
*
* @param path an absolute path beginning with a mount point.
*
* @param mp a pointer to the mount point within which @p path is found
*
* @return the absolute path within the mount point. Behavior is
* undefined if @p path does not start with the mount point prefix.
*/
const char *fs_impl_strip_prefix(const char *path,
const struct fs_mount_t *mp);
#ifdef __cplusplus
}
#endif
#endif /* ZEPHYR_SUBSYS_FS_FS_IMPL_H_ */

View file

@ -17,6 +17,8 @@
#include <nffs/os.h> #include <nffs/os.h>
#include <nffs/nffs.h> #include <nffs/nffs.h>
#include "fs_impl.h"
#define NFFS_MAX_FILE_NAME 256 #define NFFS_MAX_FILE_NAME 256
/* /*
@ -231,7 +233,9 @@ static int inode_to_dirent(struct nffs_inode_entry *inode,
static int nffs_open(struct fs_file_t *zfp, const char *file_name) static int nffs_open(struct fs_file_t *zfp, const char *file_name)
{ {
int rc, match_len; int rc;
file_name = fs_impl_strip_prefix(file_name, zfp->mp);
k_mutex_lock(&nffs_lock, K_FOREVER); k_mutex_lock(&nffs_lock, K_FOREVER);
@ -242,8 +246,7 @@ static int nffs_open(struct fs_file_t *zfp, const char *file_name)
return -ENODEV; return -ENODEV;
} }
match_len = strlen(zfp->mp->mnt_point); rc = nffs_file_open((struct nffs_file **)&zfp->filep, file_name,
rc = nffs_file_open((struct nffs_file **)&zfp->filep, &file_name[match_len],
FS_ACCESS_READ | FS_ACCESS_WRITE); FS_ACCESS_READ | FS_ACCESS_WRITE);
k_mutex_unlock(&nffs_lock); k_mutex_unlock(&nffs_lock);
@ -269,12 +272,13 @@ static int nffs_close(struct fs_file_t *zfp)
static int nffs_unlink(struct fs_mount_t *mountp, const char *path) static int nffs_unlink(struct fs_mount_t *mountp, const char *path)
{ {
int rc, match_len; int rc;
path = fs_impl_strip_prefix(path, mountp);
k_mutex_lock(&nffs_lock, K_FOREVER); k_mutex_lock(&nffs_lock, K_FOREVER);
match_len = strlen(mountp->mnt_point); rc = nffs_path_unlink(path);
rc = nffs_path_unlink(&path[match_len]);
k_mutex_unlock(&nffs_lock); k_mutex_unlock(&nffs_lock);
@ -394,7 +398,9 @@ static int nffs_sync(struct fs_file_t *zfp)
static int nffs_mkdir(struct fs_mount_t *mountp, const char *path) static int nffs_mkdir(struct fs_mount_t *mountp, const char *path)
{ {
int rc, match_len; int rc;
path = fs_impl_strip_prefix(path, mountp);
k_mutex_lock(&nffs_lock, K_FOREVER); k_mutex_lock(&nffs_lock, K_FOREVER);
@ -403,8 +409,7 @@ static int nffs_mkdir(struct fs_mount_t *mountp, const char *path)
return -ENODEV; return -ENODEV;
} }
match_len = strlen(mountp->mnt_point); rc = nffs_path_new_dir(path, NULL);
rc = nffs_path_new_dir(&path[match_len], NULL);
k_mutex_unlock(&nffs_lock); k_mutex_unlock(&nffs_lock);
@ -413,7 +418,9 @@ static int nffs_mkdir(struct fs_mount_t *mountp, const char *path)
static int nffs_opendir(struct fs_dir_t *zdp, const char *path) static int nffs_opendir(struct fs_dir_t *zdp, const char *path)
{ {
int rc, match_len; int rc;
path = fs_impl_strip_prefix(path, zdp->mp);
k_mutex_lock(&nffs_lock, K_FOREVER); k_mutex_lock(&nffs_lock, K_FOREVER);
@ -424,8 +431,7 @@ static int nffs_opendir(struct fs_dir_t *zdp, const char *path)
return -ENODEV; return -ENODEV;
} }
match_len = strlen(zdp->mp->mnt_point); rc = nffs_dir_open(path, (struct nffs_dir **)&zdp->dirp);
rc = nffs_dir_open(&path[match_len], (struct nffs_dir **)&zdp->dirp);
k_mutex_unlock(&nffs_lock); k_mutex_unlock(&nffs_lock);
@ -479,12 +485,13 @@ static int nffs_stat(struct fs_mount_t *mountp,
struct nffs_path_parser parser; struct nffs_path_parser parser;
struct nffs_inode_entry *parent; struct nffs_inode_entry *parent;
struct nffs_inode_entry *inode; struct nffs_inode_entry *inode;
int rc, match_len; int rc;
path = fs_impl_strip_prefix(path, mountp);
k_mutex_lock(&nffs_lock, K_FOREVER); k_mutex_lock(&nffs_lock, K_FOREVER);
match_len = strlen(mountp->mnt_point); nffs_path_parser_new(&parser, path);
nffs_path_parser_new(&parser, &path[match_len]);
rc = nffs_path_find(&parser, &inode, &parent); rc = nffs_path_find(&parser, &inode, &parent);
if (rc == 0) { if (rc == 0) {
@ -510,7 +517,10 @@ static int nffs_statvfs(struct fs_mount_t *mountp,
static int nffs_rename(struct fs_mount_t *mountp, const char *from, static int nffs_rename(struct fs_mount_t *mountp, const char *from,
const char *to) const char *to)
{ {
int rc, match_len; int rc;
from = fs_impl_strip_prefix(from, mountp);
to = fs_impl_strip_prefix(to, mountp);
k_mutex_lock(&nffs_lock, K_FOREVER); k_mutex_lock(&nffs_lock, K_FOREVER);
@ -519,8 +529,7 @@ static int nffs_rename(struct fs_mount_t *mountp, const char *from,
return -ENODEV; return -ENODEV;
} }
match_len = strlen(mountp->mnt_point); rc = nffs_path_rename(from, to);
rc = nffs_path_rename(&from[match_len], &to[match_len]);
k_mutex_unlock(&nffs_lock); k_mutex_unlock(&nffs_lock);