This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit e8e9df30b4aa17096cac462f56bbfb6a0e79da9a Author: anjiahao <[email protected]> AuthorDate: Tue Oct 22 13:25:10 2024 +0800 archivefs:support archivefs archivefs is more poweeful zipfs, support more compress format The Make build includes the new directory from fs/Makefile. fs/CMakeLists.txt needs no change, because nuttx_add_subdirectory() adds every subdirectory that has a CMakeLists.txt. [skip-cmake-parity] Assisted-by: Claude Code:claude-sonnet-5.5 Signed-off-by: anjiahao <[email protected]> Signed-off-by: tengshuangshuang <[email protected]> --- fs/Kconfig | 1 + fs/Makefile | 1 + fs/archivefs/CMakeLists.txt | 27 ++ fs/archivefs/Kconfig | 18 ++ fs/archivefs/Make.defs | 30 +++ fs/archivefs/archivefs.c | 627 ++++++++++++++++++++++++++++++++++++++++++++ fs/archivefs/libarchive | 1 + fs/mount/fs_mount.c | 9 +- include/sys/statfs.h | 1 + 9 files changed, 714 insertions(+), 1 deletion(-) diff --git a/fs/Kconfig b/fs/Kconfig index 07af9f4c8f1..22451655eca 100644 --- a/fs/Kconfig +++ b/fs/Kconfig @@ -237,6 +237,7 @@ config FS_MTD_PROXY source "fs/vfs/Kconfig" source "fs/aio/Kconfig" +source "fs/archivefs/Kconfig" source "fs/semaphore/Kconfig" source "fs/event/Kconfig" source "fs/mqueue/Kconfig" diff --git a/fs/Makefile b/fs/Makefile index 316ffaadcf8..b2348287572 100644 --- a/fs/Makefile +++ b/fs/Makefile @@ -46,6 +46,7 @@ include event/Make.defs ifneq ($(CONFIG_DISABLE_MOUNTPOINT),y) +include archivefs/Make.defs include mount/Make.defs include partition/Make.defs include fat/Make.defs diff --git a/fs/archivefs/CMakeLists.txt b/fs/archivefs/CMakeLists.txt new file mode 100644 index 00000000000..5f17eae141e --- /dev/null +++ b/fs/archivefs/CMakeLists.txt @@ -0,0 +1,27 @@ +# ############################################################################## +# fs/archivefs/CMakeLists.txt +# +# Licensed to the Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with this work for +# additional information regarding copyright ownership. The ASF licenses this +# file to you under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy of +# the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. +# +# ############################################################################## +if(NOT CONFIG_DISABLE_MOUNTPOINT) + if(CONFIG_FS_ARCHIVEFS) + + target_sources(fs PRIVATE archivefs.c) + + target_include_directories(fs PRIVATE libarchive/libarchive/libarchive) + endif() +endif() diff --git a/fs/archivefs/Kconfig b/fs/archivefs/Kconfig new file mode 100644 index 00000000000..ebd402ebc54 --- /dev/null +++ b/fs/archivefs/Kconfig @@ -0,0 +1,18 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config FS_ARCHIVEFS + bool "ARCHIVE File System" + default n + +if FS_ARCHIVEFS + +config FS_ARCHIVEFS_BUFFER_SIZE + int "Buffer size" + default 32768 + ---help--- + libarchive requires a buffer to read and write data. + +endif diff --git a/fs/archivefs/Make.defs b/fs/archivefs/Make.defs new file mode 100644 index 00000000000..b014d24fa4f --- /dev/null +++ b/fs/archivefs/Make.defs @@ -0,0 +1,30 @@ +############################################################################ +# fs/archivefs/Make.defs +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +ifeq ($(CONFIG_FS_ARCHIVEFS),y) + +CFLAGS += ${INCDIR_PREFIX}archivefs/libarchive/libarchive/libarchive + +CSRCS += archivefs.c + +DEPPATH += --dep-path archivefs +VPATH += :archivefs + +endif diff --git a/fs/archivefs/archivefs.c b/fs/archivefs/archivefs.c new file mode 100644 index 00000000000..613362596cd --- /dev/null +++ b/fs/archivefs/archivefs.c @@ -0,0 +1,627 @@ +/**************************************************************************** + * fs/archivefs/archivefs.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <debug.h> +#include <dirent.h> +#include <errno.h> +#include <fcntl.h> +#include <sched.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/statfs.h> + +#include <archive_entry.h> +#include <archive.h> + +#include "fs_heap.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct archivefs_priv_s +{ + FAR struct archive_entry *entry; + FAR struct archive *a; + + /* callback data */ + + struct file file; + char buffer[CONFIG_FS_ARCHIVEFS_BUFFER_SIZE]; + + /* seek data */ + + FAR char *seekbuf; + mutex_t lock; +}; + +struct archivefs_dir_s +{ + struct fs_dirent_s dir; + FAR struct archivefs_priv_s *priv; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int archivefs_open(FAR struct file *filep, FAR const char *relpath, + int oflags, mode_t mode); +static int archivefs_close(FAR struct file *filep); +static ssize_t archivefs_read(FAR struct file *filep, FAR char *buffer, + size_t buflen); +static off_t archivefs_seek(FAR struct file *filep, off_t offset, + int whence); +static int archivefs_dup(FAR const struct file *oldp, + FAR struct file *newp); +static int archivefs_fstat(FAR const struct file *filep, + FAR struct stat *buf); +static int archivefs_opendir(FAR struct inode *mountpt, + FAR const char *relpath, + FAR struct fs_dirent_s **dir); +static int archivefs_closedir(FAR struct inode *mountpt, + FAR struct fs_dirent_s *dir); +static int archivefs_readdir(FAR struct inode *mountpt, + FAR struct fs_dirent_s *dir, + FAR struct dirent *entry); +static int archivefs_rewinddir(FAR struct inode *mountpt, + FAR struct fs_dirent_s *dir); +static int archivefs_statfs(FAR struct inode *mountpt, + FAR struct statfs *buf); +static int archivefs_stat(FAR struct inode *mountpt, + FAR const char *relpath, + FAR struct stat *buf); +static int archivefs_bind(FAR struct inode *driver, FAR const void *data, + FAR void **handle); +static int archivefs_unbind(FAR void *handle, FAR struct inode **driver, + unsigned int flags); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +const struct mountpt_operations g_archivefs_operations = +{ + archivefs_open, /* open */ + archivefs_close, /* close */ + archivefs_read, /* read */ + NULL, /* write */ + archivefs_seek, /* seek */ + NULL, /* ioctl */ + NULL, /* mmap */ + NULL, /* truncate */ + NULL, /* poll */ + NULL, /* sync */ + archivefs_dup, /* dup */ + archivefs_fstat, /* fstat */ + NULL, /* fchstat */ + + archivefs_opendir, /* opendir */ + archivefs_closedir, /* closedir */ + archivefs_readdir, /* readdir */ + archivefs_rewinddir, /* rewinddir */ + + archivefs_bind, /* bind */ + archivefs_unbind, /* unbind */ + archivefs_statfs, /* statfs */ + + NULL, /* unlink */ + NULL, /* mkdir */ + NULL, /* rmdir */ + NULL, /* rename */ + archivefs_stat, /* stat */ + NULL /* chstat */ +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int archivefs_convert_result(int ret) +{ + switch (ret) + { + case ARCHIVE_RETRY: + return -EAGAIN; + case ARCHIVE_WARN: + return -ENOEXEC; + case ARCHIVE_FAILED: + return -EINVAL; + case ARCHIVE_FATAL: + return -EPERM; + + default: + return ret; + } +} + +static int archivefs_close_cb(FAR struct archive *a, FAR void *client_data) +{ + FAR struct archivefs_priv_s *priv = client_data; + + return file_close(&priv->file); +} + +static la_ssize_t archivefs_read_cb(FAR struct archive *a, + FAR void *client_data, + FAR const void **buff) +{ + FAR struct archivefs_priv_s *priv = client_data; + ssize_t ret; + + ret = file_read(&priv->file, priv->buffer, + CONFIG_FS_ARCHIVEFS_BUFFER_SIZE); + if (ret <= 0) + { + return ret; + } + + *buff = priv->buffer; + return ret; +} + +static la_int64_t archivefs_seek_cb(FAR struct archive *a, + FAR void *client_data, + la_int64_t offset, int whence) +{ + FAR struct archivefs_priv_s *priv = client_data; + + return file_seek(&priv->file, offset, whence); +} + +static off_t archivefs_skip(FAR struct archivefs_priv_s *priv, off_t amount) +{ + off_t next = 0; + + if (priv->seekbuf == NULL) + { + priv->seekbuf = fs_heap_malloc(CONFIG_FS_ARCHIVEFS_BUFFER_SIZE); + if (priv->seekbuf == NULL) + { + return -ENOMEM; + } + } + + while (next < amount) + { + off_t remain = amount - next; + + if (remain > CONFIG_FS_ARCHIVEFS_BUFFER_SIZE) + { + remain = CONFIG_FS_ARCHIVEFS_BUFFER_SIZE; + } + + remain = archive_read_data(priv->a, priv->seekbuf, remain); + remain = archivefs_convert_result(remain); + if (remain <= 0) + { + return next ? next : remain; + } + + next += remain; + } + + return next; +} + +int archivefs_new(FAR const char *abspath, FAR const char *relpath, + FAR struct archivefs_priv_s **priv) +{ + FAR struct archivefs_priv_s *newp; + int ret = -ENOMEM; + + newp = fs_heap_zalloc(sizeof(struct archivefs_priv_s)); + if (newp == NULL) + { + return ret; + } + + newp->a = archive_read_new(); + if (newp->a == NULL) + { + ferr("ERROR: archive_read_new() failed\n"); + goto err_with_priv; + } + + nxmutex_init(&newp->lock); + ret = archivefs_convert_result(archive_read_support_format_all(newp->a)); + if (ret < 0) + { + ferr("ERROR: archive_read_support_format_all() failed\n"); + goto err_with_lock; + } + + ret = file_open(&newp->file, abspath, O_RDONLY); + if (ret < 0) + { + ferr("ERROR: file_open() failed\n"); + goto err_with_lock; + } + + archive_read_set_seek_callback(newp->a, archivefs_seek_cb); + ret = archive_read_open(newp->a, newp, NULL, + archivefs_read_cb, archivefs_close_cb); + ret = archivefs_convert_result(ret); + if (ret < 0) + { + ferr("ERROR: archive_read_open() failed\n"); + goto err_with_open; + } + + newp->entry = archive_entry_new2(newp->a); + if (newp->entry == NULL) + { + ferr("ERROR: archive_entry_new2() failed\n"); + ret = -ENOMEM; + goto err_with_close; + } + + if (relpath == NULL) + { + /* Open root dir */ + + *priv = newp; + return OK; + } + + while (true) + { + ret = archive_read_next_header2(newp->a, newp->entry); + ret = archivefs_convert_result(ret); + if (ret < 0) + { + break; + } + + if (strcmp(archive_entry_pathname(newp->entry), relpath) == 0) + { + *priv = newp; + return OK; + } + } + + ret = -ENOENT; + archive_entry_free(newp->entry); +err_with_close: + archive_read_close(newp->a); +err_with_open: + file_close(&newp->file); +err_with_lock: + nxmutex_destroy(&newp->lock); + archive_read_free(newp->a); +err_with_priv: + fs_heap_free(newp); + return ret; +} + +static void archivefs_free(FAR struct archivefs_priv_s *priv) +{ + if (priv->seekbuf != NULL) + { + fs_heap_free(priv->seekbuf); + } + + archive_entry_free(priv->entry); + archive_read_close(priv->a); + nxmutex_destroy(&priv->lock); + archive_read_free(priv->a); + fs_heap_free(priv); +} + +static int archivefs_stats_common(FAR struct archivefs_priv_s *priv, + FAR struct stat *buf) +{ + FAR const struct stat *stat = archive_entry_stat(priv->entry); + + if (stat == NULL) + { + return -EINVAL; + } + + memcpy(buf, stat, sizeof(struct stat)); + return OK; +} + +/**************************************************************************** + * Archivefs methods + ****************************************************************************/ + +static int archivefs_open(FAR struct file *filep, FAR const char *relpath, + int oflags, mode_t mode) +{ + FAR const char *abspath = filep->f_inode->i_private; + FAR struct archivefs_priv_s *priv; + int ret; + + ret = archivefs_new(abspath, relpath, &priv); + if (ret < 0) + { + return ret; + } + + filep->f_priv = priv; + return OK; +} + +static int archivefs_close(FAR struct file *filep) +{ + FAR struct archivefs_priv_s *priv = filep->f_priv; + + archivefs_free(priv); + return OK; +} + +static ssize_t archivefs_read(FAR struct file *filep, FAR char *buffer, + size_t buflen) +{ + FAR struct archivefs_priv_s *priv = filep->f_priv; + ssize_t ret; + + nxmutex_lock(&priv->lock); + ret = archivefs_convert_result(archive_read_data(priv->a, buffer, buflen)); + if (ret > 0) + { + filep->f_pos += ret; + } + + nxmutex_unlock(&priv->lock); + return ret; +} + +static off_t archivefs_seek(FAR struct file *filep, off_t offset, + int whence) +{ + FAR const char *abspath = filep->f_inode->i_private; + FAR struct archivefs_priv_s *priv = filep->f_priv; + FAR struct archivefs_priv_s *newp; + off_t ret = 0; + + nxmutex_lock(&priv->lock); + switch (whence) + { + case SEEK_SET: + break; + case SEEK_CUR: + offset += filep->f_pos; + break; + case SEEK_END: + offset += archive_entry_size(priv->entry); + break; + default: + ret = -EINVAL; + goto err_with_lock; + } + + if (filep->f_pos == offset) + { + goto err_with_lock; + } + else if (filep->f_pos > offset) + { + ret = archivefs_new(abspath, archive_entry_pathname(priv->entry), + &newp); + if (ret < 0) + { + goto err_with_lock; + } + + nxmutex_unlock(&priv->lock); + archivefs_free(priv); + priv = newp; + nxmutex_lock(&priv->lock); + filep->f_priv = newp; + filep->f_pos = 0; + } + + ret = archivefs_skip(priv, offset - filep->f_pos); + if (ret < 0) + { + goto err_with_lock; + } + + filep->f_pos += ret; + +err_with_lock: + nxmutex_unlock(&priv->lock); + return ret < 0 ? ret : filep->f_pos; +} + +static int archivefs_dup(FAR const struct file *oldp, FAR struct file *newp) +{ + FAR struct archivefs_priv_s *priv = oldp->f_priv; + FAR const char *relpath = archive_entry_pathname(priv->entry); + + return archivefs_open(newp, relpath, oldp->f_oflags, 0); +} + +static int archivefs_fstat(FAR const struct file *filep, + FAR struct stat *buf) +{ + FAR struct archivefs_priv_s *priv = filep->f_priv; + + return archivefs_stats_common(priv, buf); +} + +static int archivefs_opendir(FAR struct inode *mountpt, + FAR const char *relpath, + FAR struct fs_dirent_s **dir) +{ + FAR const char *abspath = mountpt->i_private; + FAR struct archivefs_dir_s *adir; + int ret; + + adir = fs_heap_zalloc(sizeof(struct archivefs_dir_s)); + if (adir == NULL) + { + return -ENOMEM; + } + + ret = archivefs_new(abspath, NULL, &adir->priv); + if (ret < 0) + { + fs_heap_free(adir); + return ret; + } + + *dir = &adir->dir; + return OK; +} + +static int archivefs_closedir(FAR struct inode *mountpt, + FAR struct fs_dirent_s *dir) +{ + FAR struct archivefs_dir_s *adir = (FAR struct archivefs_dir_s *)dir; + + archivefs_free(adir->priv); + fs_heap_free(adir); + return OK; +} + +static int archivefs_readdir(FAR struct inode *mountpt, + FAR struct fs_dirent_s *dir, + FAR struct dirent *entry) +{ + FAR struct archivefs_dir_s *adir = (FAR struct archivefs_dir_s *)dir; + FAR struct archivefs_priv_s *priv = adir->priv; + int ret; + + nxmutex_lock(&priv->lock); + ret = archive_read_next_header2(priv->a, priv->entry); + ret = archivefs_convert_result(ret); + if (ret < 0) + { + goto err; + } + + if (ret == ARCHIVE_EOF) + { + ret = EOF; + goto err; + } + + strlcpy(entry->d_name, archive_entry_pathname(priv->entry), + sizeof(entry->d_name)); +err: + nxmutex_unlock(&priv->lock); + return ret; +} + +static int archivefs_rewinddir(FAR struct inode *mountpt, + FAR struct fs_dirent_s *dir) +{ + FAR struct archivefs_dir_s *adir = (FAR struct archivefs_dir_s *)dir; + FAR struct archivefs_priv_s *priv = adir->priv; + + nxmutex_lock(&priv->lock); + archive_entry_free(priv->entry); + priv->entry = archive_entry_new2(priv->a); + if (priv->entry == NULL) + { + nxmutex_unlock(&priv->lock); + return -ENOMEM; + } + + nxmutex_unlock(&priv->lock); + return OK; +} + +static int archivefs_statfs(FAR struct inode *mountpt, + FAR struct statfs *buf) +{ + buf->f_type = ARCHIVEFS_MAGIC; + buf->f_namelen = NAME_MAX; + return OK; +} + +static int archivefs_stat(FAR struct inode *mountpt, + FAR const char *relpath, + FAR struct stat *buf) +{ + FAR const char *abspath = mountpt->i_private; + FAR struct archivefs_priv_s *priv; + int ret; + + if (relpath[0] == 0) + { + buf->st_mode = S_IFDIR | 0555; + return OK; + } + + ret = archivefs_new(abspath, relpath, &priv); + if (ret < 0) + { + return ret; + } + + ret = archivefs_stats_common(priv, buf); + archivefs_free(priv); + return ret; +} + +static int archivefs_bind(FAR struct inode *driver, FAR const void *data, + FAR void **handle) +{ + FAR struct archivefs_priv_s *priv; + FAR char *abspath; + int ret; + + if (data == NULL) + { + return -ENODEV; + } + + /* Try access to archive file */ + + ret = archivefs_new(data, NULL, &priv); + if (ret < 0) + { + return ret; + } + + archivefs_free(priv); + abspath = fs_heap_zalloc(PATH_MAX); + if (abspath == NULL) + { + return -ENOMEM; + } + + *handle = abspath; + strlcpy(abspath, data, PATH_MAX); + return OK; +} + +static int archivefs_unbind(FAR void *handle, FAR struct inode **driver, + unsigned int flags) +{ + fs_heap_free(handle); + return OK; +} diff --git a/fs/archivefs/libarchive b/fs/archivefs/libarchive new file mode 120000 index 00000000000..e19d28690f8 --- /dev/null +++ b/fs/archivefs/libarchive @@ -0,0 +1 @@ +../../../apps/system/libarchive \ No newline at end of file diff --git a/fs/mount/fs_mount.c b/fs/mount/fs_mount.c index a64e14ba603..d8a537f295c 100644 --- a/fs/mount/fs_mount.c +++ b/fs/mount/fs_mount.c @@ -74,7 +74,8 @@ defined(CONFIG_FS_TMPFS) || defined(CONFIG_FS_USERFS) || \ defined(CONFIG_FS_CROMFS) || defined(CONFIG_FS_UNIONFS) || \ defined(CONFIG_FS_HOSTFS) || defined(CONFIG_FS_ZIPFS) || \ - defined(CONFIG_FS_RPMSGFS) || defined(CONFIG_FS_V9FS) + defined(CONFIG_FS_RPMSGFS) || defined(CONFIG_FS_V9FS) || \ + defined(CONFIG_FS_ARCHIVEFS) # define NODFS_SUPPORT #endif @@ -199,6 +200,9 @@ extern const struct mountpt_operations g_zipfs_operations; #ifdef CONFIG_FS_V9FS extern const struct mountpt_operations g_v9fs_operations; #endif +#ifdef CONFIG_FS_ARCHIVEFS +extern const struct mountpt_operations g_archivefs_operations; +#endif static const struct fsmap_t g_nonbdfsmap[] = { @@ -237,6 +241,9 @@ static const struct fsmap_t g_nonbdfsmap[] = #endif #ifdef CONFIG_FS_V9FS { "v9fs", &g_v9fs_operations}, +#endif +#ifdef CONFIG_FS_ARCHIVEFS + { "archivefs", &g_archivefs_operations}, #endif { NULL, NULL }, }; diff --git a/include/sys/statfs.h b/include/sys/statfs.h index 81e80af5417..14f44320cba 100644 --- a/include/sys/statfs.h +++ b/include/sys/statfs.h @@ -105,6 +105,7 @@ #define ZIPFS_MAGIC 0x504b #define XIPFS_MAGIC 0x58495046 #define V9FS_MAGIC 0x01021997 +#define ARCHIVEFS_MAGIC 0x504c #endif /* NUTTX_NO_EXTENSIONS */
