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 807d72137f5aef3c1862c3812d630915a7951e05 Author: zhengyu16 <[email protected]> AuthorDate: Tue Aug 26 05:13:00 2025 -0400 archivefs: rename archivefs to zipfs Rename archivefs to zipfs, and replace the minizip based zipfs with it. FS_ZIPFS now reads zip files with libarchive, the formats come from FS_ZIPFS_FORMAT_*, and FS_ZIPFS_BUFFER_SIZE replaces ZIPFS_SEEK_BUFSIZE. The fs/zipfs/zlib link is replaced by fs/zipfs/libarchive, and ARCHIVEFS_MAGIC is removed. 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: zhengyu16 <[email protected]> Signed-off-by: tengshuangshuang <[email protected]> --- fs/Kconfig | 1 - fs/Makefile | 1 - fs/archivefs/CMakeLists.txt | 27 -- fs/archivefs/Kconfig | 86 ---- fs/archivefs/Make.defs | 30 -- fs/archivefs/archivefs.c | 767 --------------------------------- fs/mount/fs_mount.c | 9 +- fs/zipfs/CMakeLists.txt | 4 +- fs/zipfs/Kconfig | 80 +++- fs/zipfs/Make.defs | 6 +- fs/{archivefs => zipfs}/libarchive | 0 fs/zipfs/zip_vfs.c | 842 +++++++++++++++++++++---------------- fs/zipfs/zlib | 1 - include/sys/statfs.h | 1 - 14 files changed, 556 insertions(+), 1299 deletions(-) diff --git a/fs/Kconfig b/fs/Kconfig index 22451655eca..07af9f4c8f1 100644 --- a/fs/Kconfig +++ b/fs/Kconfig @@ -237,7 +237,6 @@ 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 b2348287572..316ffaadcf8 100644 --- a/fs/Makefile +++ b/fs/Makefile @@ -46,7 +46,6 @@ 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 deleted file mode 100644 index 5f17eae141e..00000000000 --- a/fs/archivefs/CMakeLists.txt +++ /dev/null @@ -1,27 +0,0 @@ -# ############################################################################## -# 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 deleted file mode 100644 index 62168914a18..00000000000 --- a/fs/archivefs/Kconfig +++ /dev/null @@ -1,86 +0,0 @@ -# -# 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. - -config FS_ARCHIVEFS_FORMAT_ALL - bool "support archive all format" - default n - ---help--- - All format support will increase binary size. -if !FS_ARCHIVEFS_FORMAT_ALL -config FS_ARCHIVEFS_FORMAT_ZIP - bool "support archive xar format" - default y - -config FS_ARCHIVEFS_FORMAT_7ZIP - bool "support archive 7zip format" - default n - -config FS_ARCHIVEFS_FORMAT_AR - bool "support archive ar format" - default n - -config FS_ARCHIVEFS_FORMAT_CAB - bool "support archive cab format" - default n - -config FS_ARCHIVEFS_FORMAT_CPIO - bool "support archive cpio format" - default n - -config FS_ARCHIVEFS_FORMAT_EMPTY - bool "support archive empty format" - default n - -config FS_ARCHIVEFS_FORMAT_ISO9660 - bool "support archive iso9660 format" - default n - -config FS_ARCHIVEFS_FORMAT_LHA - bool "support archive lha format" - default n - -config FS_ARCHIVEFS_FORMAT_MTREE - bool "support archive mtree format" - default n - -config FS_ARCHIVEFS_FORMAT_RAR - bool "support archive rar format" - default n - -config FS_ARCHIVEFS_FORMAT_RAR_V5 - bool "support archive rar_v5 format" - default n - -config FS_ARCHIVEFS_FORMAT_RAW - bool "support archive raw format" - default n - -config FS_ARCHIVEFS_FORMAT_TAR - bool "support archive raw format" - default n - -config FS_ARCHIVEFS_FORMAT_WARC - bool "support archive warc format" - default n - -config FS_ARCHIVEFS_FORMAT_XAR - bool "support archive xar format" - default n - -endif - -endif diff --git a/fs/archivefs/Make.defs b/fs/archivefs/Make.defs deleted file mode 100644 index b014d24fa4f..00000000000 --- a/fs/archivefs/Make.defs +++ /dev/null @@ -1,30 +0,0 @@ -############################################################################ -# 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 deleted file mode 100644 index c95f5b967a2..00000000000 --- a/fs/archivefs/archivefs.c +++ /dev/null @@ -1,767 +0,0 @@ -/**************************************************************************** - * 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, /* readv */ - NULL, /* writev */ - 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); -#ifdef CONFIG_FS_ARCHIVEFS_FORMAT_ALL - 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; - } -#else -# ifdef CONFIG_FS_ARCHIVEFS_FORMAT_ZIP - ret = archivefs_convert_result(archive_read_support_format_zip(newp->a)); - if (ret < 0) - { - ferr("ERROR: archive_read_support_format_zip() failed\n"); - goto err_with_lock; - } -# endif - -# ifdef CONFIG_FS_ARCHIVEFS_FORMAT_7ZIP - ret = archivefs_convert_result(archive_read_support_format_7zip(newp->a)); - if (ret < 0) - { - ferr("ERROR: archive_read_support_format_7zip() failed\n"); - goto err_with_lock; - } -# endif - -# ifdef CONFIG_FS_ARCHIVEFS_FORMAT_AR - ret = archivefs_convert_result(archive_read_support_format_ar(newp->a)); - if (ret < 0) - { - ferr("ERROR: archive_read_support_format_ar() failed\n"); - goto err_with_lock; - } -# endif - -# ifdef CONFIG_FS_ARCHIVEFS_FORMAT_CAB - ret = archivefs_convert_result(archive_read_support_format_cab(newp->a)); - if (ret < 0) - { - ferr("ERROR: archive_read_support_format_cab() failed\n"); - goto err_with_lock; - } -# endif - -# ifdef CONFIG_FS_ARCHIVEFS_FORMAT_CPIO - ret = archivefs_convert_result(archive_read_support_format_cpio(newp->a)); - if (ret < 0) - { - ferr("ERROR: archive_read_support_format_cpio() failed\n"); - goto err_with_lock; - } -# endif - -# ifdef CONFIG_FS_ARCHIVEFS_FORMAT_EMPTY - ret = archivefs_convert_result(archive_read_support_format_empty(newp->a)); - if (ret < 0) - { - ferr("ERROR: archive_read_support_format_empty() failed\n"); - goto err_with_lock; - } -# endif - -# ifdef CONFIG_FS_ARCHIVEFS_FORMAT_ISO9660 - ret = archive_read_support_format_iso9660(newp->a); - ret = archivefs_convert_result(ret); - if (ret < 0) - { - ferr("ERROR: archive_read_support_format_iso9660() failed\n"); - goto err_with_lock; - } -# endif - -# ifdef CONFIG_FS_ARCHIVEFS_FORMAT_LHA - ret = archivefs_convert_result(archive_read_support_format_lha(newp->a)); - if (ret < 0) - { - ferr("ERROR: archive_read_support_format_lha() failed\n"); - goto err_with_lock; - } -# endif - -# ifdef CONFIG_FS_ARCHIVEFS_FORMAT_MTREE - ret = archivefs_convert_result(archive_read_support_format_mtree(newp->a)); - if (ret < 0) - { - ferr("ERROR: archive_read_support_format_mtree() failed\n"); - goto err_with_lock; - } -# endif - -# ifdef CONFIG_FS_ARCHIVEFS_FORMAT_RAR - ret = archivefs_convert_result(archive_read_support_format_rar(newp->a)); - if (ret < 0) - { - ferr("ERROR: archive_read_support_format_rar() failed\n"); - goto err_with_lock; - } -# endif - -# ifdef CONFIG_FS_ARCHIVEFS_FORMAT_RAR_V5 - ret = archivefs_convert_result(archive_read_support_format_rar5(newp->a)); - if (ret < 0) - { - ferr("ERROR: archive_read_support_format_rar5() failed\n"); - goto err_with_lock; - } -# endif - -# ifdef CONFIG_FS_ARCHIVEFS_FORMAT_RAW - ret = archivefs_convert_result(archive_read_support_format_raw(newp->a)); - if (ret < 0) - { - ferr("ERROR: archive_read_support_format_raw() failed\n"); - goto err_with_lock; - } -# endif - -# ifdef CONFIG_FS_ARCHIVEFS_FORMAT_TAR - ret = archivefs_convert_result(archive_read_support_format_tar(newp->a)); - if (ret < 0) - { - ferr("ERROR: archive_read_support_format_tar() failed\n"); - goto err_with_lock; - } -# endif - -# ifdef CONFIG_FS_ARCHIVEFS_FORMAT_WARC - ret = archivefs_convert_result(archive_read_support_format_warc(newp->a)); - if (ret < 0) - { - ferr("ERROR: archive_read_support_format_warc() failed\n"); - goto err_with_lock; - } -# endif - -# ifdef CONFIG_FS_ARCHIVEFS_FORMAT_XAR - ret = archivefs_convert_result(archive_read_support_format_xar(newp->a)); - if (ret < 0) - { - ferr("ERROR: archive_read_support_format_xar() failed\n"); - goto err_with_lock; - } -# endif -#endif - - 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/mount/fs_mount.c b/fs/mount/fs_mount.c index d8a537f295c..a64e14ba603 100644 --- a/fs/mount/fs_mount.c +++ b/fs/mount/fs_mount.c @@ -74,8 +74,7 @@ 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_ARCHIVEFS) + defined(CONFIG_FS_RPMSGFS) || defined(CONFIG_FS_V9FS) # define NODFS_SUPPORT #endif @@ -200,9 +199,6 @@ 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[] = { @@ -241,9 +237,6 @@ 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/fs/zipfs/CMakeLists.txt b/fs/zipfs/CMakeLists.txt index 401d84d04b8..d317db3909e 100644 --- a/fs/zipfs/CMakeLists.txt +++ b/fs/zipfs/CMakeLists.txt @@ -24,8 +24,6 @@ if(NOT CONFIG_DISABLE_MOUNTPOINT) target_sources(fs PRIVATE zip_vfs.c) - target_include_directories( - fs PRIVATE ${CMAKE_CURRENT_LIST_DIR}/zlib/zlib/contrib/minizip - ${CMAKE_CURRENT_LIST_DIR}/zlib/zlib) + target_include_directories(fs PRIVATE libarchive/libarchive/libarchive) endif() endif() diff --git a/fs/zipfs/Kconfig b/fs/zipfs/Kconfig index 6d0137554ee..a5b57240972 100644 --- a/fs/zipfs/Kconfig +++ b/fs/zipfs/Kconfig @@ -25,15 +25,83 @@ # config FS_ZIPFS - bool "ZIPFS File System" + bool "ZIP File System" default n if FS_ZIPFS -config ZIPFS_SEEK_BUFSIZE - int "zipfs seek buffer size" - default 256 +config FS_ZIPFS_BUFFER_SIZE + int "Buffer size" + default 32768 ---help--- - this option will influences seek speed + libarchive requires a buffer to read and write data. -endif # FS_ZIPFS +config FS_ZIPFS_FORMAT_ALL + bool "support zip all format" + default n + ---help--- + All format support will increase binary size. +if !FS_ZIPFS_FORMAT_ALL +config FS_ZIPFS_FORMAT_ZIP + bool "support zip xar format" + default y + +config FS_ZIPFS_FORMAT_7ZIP + bool "support zip 7zip format" + default n + +config FS_ZIPFS_FORMAT_AR + bool "support zip ar format" + default n + +config FS_ZIPFS_FORMAT_CAB + bool "support zip cab format" + default n + +config FS_ZIPFS_FORMAT_CPIO + bool "support zip cpio format" + default n + +config FS_ZIPFS_FORMAT_EMPTY + bool "support zip empty format" + default n + +config FS_ZIPFS_FORMAT_ISO9660 + bool "support zip iso9660 format" + default n + +config FS_ZIPFS_FORMAT_LHA + bool "support zip lha format" + default n + +config FS_ZIPFS_FORMAT_MTREE + bool "support zip mtree format" + default n + +config FS_ZIPFS_FORMAT_RAR + bool "support zip rar format" + default n + +config FS_ZIPFS_FORMAT_RAR_V5 + bool "support zip rar_v5 format" + default n + +config FS_ZIPFS_FORMAT_RAW + bool "support zip raw format" + default n + +config FS_ZIPFS_FORMAT_TAR + bool "support zip raw format" + default n + +config FS_ZIPFS_FORMAT_WARC + bool "support zip warc format" + default n + +config FS_ZIPFS_FORMAT_XAR + bool "support zip xar format" + default n + +endif + +endif diff --git a/fs/zipfs/Make.defs b/fs/zipfs/Make.defs index 8b4d60ba834..6ea7c73e57a 100644 --- a/fs/zipfs/Make.defs +++ b/fs/zipfs/Make.defs @@ -21,12 +21,10 @@ ############################################################################ ifeq ($(CONFIG_FS_ZIPFS),y) -# Files required for ZIPFS file system support -CFLAGS += ${INCDIR_PREFIX}zipfs/zlib/zlib/contrib/minizip -CFLAGS += ${INCDIR_PREFIX}zipfs/zlib/zlib +CFLAGS += ${INCDIR_PREFIX}zipfs/libarchive/libarchive/libarchive + CSRCS += zip_vfs.c -# Include ZIPFS build support DEPPATH += --dep-path zipfs VPATH += :zipfs diff --git a/fs/archivefs/libarchive b/fs/zipfs/libarchive similarity index 100% rename from fs/archivefs/libarchive rename to fs/zipfs/libarchive diff --git a/fs/zipfs/zip_vfs.c b/fs/zipfs/zip_vfs.c index b6e0201b76e..5c03fd197d0 100644 --- a/fs/zipfs/zip_vfs.c +++ b/fs/zipfs/zip_vfs.c @@ -24,111 +24,96 @@ * Included Files ****************************************************************************/ -#include <assert.h> -#include <errno.h> +#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 <sys/stat.h> +#include <unistd.h> #include <sys/statfs.h> -#include <nuttx/mutex.h> -#include <nuttx/kmalloc.h> -#include <nuttx/fs/fs.h> -#include <nuttx/fs/ioctl.h> -#include <unzip.h> +#include <archive_entry.h> +#include <archive.h> + +#include <nuttx/fs/fs.h> +#include <nuttx/mutex.h> #include "fs_heap.h" +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + /**************************************************************************** * Private Types ****************************************************************************/ -struct zipfs_dir_s +struct zipfs_priv_s { - struct fs_dirent_s base; + FAR struct archive_entry *entry; + FAR struct archive *a; + + /* callback data */ + + struct file file; + char buffer[CONFIG_FS_ZIPFS_BUFFER_SIZE]; + + /* seek data */ + + FAR char *seekbuf; mutex_t lock; - unzFile uf; - bool last; }; -struct zipfs_mountpt_s +struct zipfs_dir_s { - char abspath[1]; -}; - -struct zipfs_file_s -{ - unzFile uf; - mutex_t lock; - FAR char *seekbuf; - char relpath[1]; + struct fs_dirent_s dir; + FAR struct zipfs_priv_s *priv; }; /**************************************************************************** * Private Function Prototypes ****************************************************************************/ -static voidpf zipfs_real_open(voidpf opaque, FAR const void *filename, - int mode); -static uLong zipfs_real_read(voidpf opaque, voidpf stream, FAR void *buf, - uLong size); -static long zipfs_real_seek(voidpf opaque, voidpf stream, ZPOS64_T offset, - int origin); -static ZPOS64_T zipfs_real_tell(voidpf opaque, voidpf stream); -static int zipfs_real_close(voidpf opaque, voidpf stream); -static int zipfs_real_error(voidpf opaque, voidpf stream); - -static int zipfs_open(FAR struct file *filep, FAR const char *relpath, - int oflags, mode_t mode); -static int zipfs_close(FAR struct file *filep); +static int zipfs_open(FAR struct file *filep, FAR const char *relpath, + int oflags, mode_t mode); +static int zipfs_close(FAR struct file *filep); static ssize_t zipfs_read(FAR struct file *filep, FAR char *buffer, size_t buflen); -static off_t zipfs_seek(FAR struct file *filep, off_t offset, - int whence); -static int zipfs_dup(FAR const struct file *oldp, - FAR struct file *newp); -static int zipfs_fstat(FAR const struct file *filep, - FAR struct stat *buf); -static int zipfs_opendir(FAR struct inode *mountpt, - FAR const char *relpath, - FAR struct fs_dirent_s **dir); -static int zipfs_closedir(FAR struct inode *mountpt, - FAR struct fs_dirent_s *dir); -static int zipfs_readdir(FAR struct inode *mountpt, - FAR struct fs_dirent_s *dir, - FAR struct dirent *entry); -static int zipfs_rewinddir(FAR struct inode *mountpt, - FAR struct fs_dirent_s *dir); -static int zipfs_bind(FAR struct inode *driver, - FAR const void *data, FAR void **handle); -static int zipfs_unbind(FAR void *handle, FAR struct inode **driver, - unsigned int flags); -static int zipfs_statfs(FAR struct inode *mountpt, - FAR struct statfs *buf); -static int zipfs_stat(FAR struct inode *mountpt, - FAR const char *relpath, FAR struct stat *buf); +static off_t zipfs_seek(FAR struct file *filep, off_t offset, + int whence); +static int zipfs_dup(FAR const struct file *oldp, + FAR struct file *newp); +static int zipfs_fstat(FAR const struct file *filep, + FAR struct stat *buf); +static int zipfs_opendir(FAR struct inode *mountpt, + FAR const char *relpath, + FAR struct fs_dirent_s **dir); +static int zipfs_closedir(FAR struct inode *mountpt, + FAR struct fs_dirent_s *dir); +static int zipfs_readdir(FAR struct inode *mountpt, + FAR struct fs_dirent_s *dir, + FAR struct dirent *entry); +static int zipfs_rewinddir(FAR struct inode *mountpt, + FAR struct fs_dirent_s *dir); +static int zipfs_statfs(FAR struct inode *mountpt, + FAR struct statfs *buf); +static int zipfs_stat(FAR struct inode *mountpt, + FAR const char *relpath, + FAR struct stat *buf); +static int zipfs_bind(FAR struct inode *driver, FAR const void *data, + FAR void **handle); +static int zipfs_unbind(FAR void *handle, FAR struct inode **driver, + unsigned int flags); /**************************************************************************** * Private Data ****************************************************************************/ -static zlib_filefunc64_def zipfs_real_ops = -{ - zipfs_real_open, - zipfs_real_read, - NULL, - zipfs_real_tell, - zipfs_real_seek, - zipfs_real_close, - zipfs_real_error, - NULL -}; - -/**************************************************************************** - * Public Data - ****************************************************************************/ - const struct mountpt_operations g_zipfs_operations = { zipfs_open, /* open */ @@ -142,7 +127,6 @@ const struct mountpt_operations g_zipfs_operations = NULL, /* poll */ NULL, /* readv */ NULL, /* writev */ - NULL, /* sync */ zipfs_dup, /* dup */ zipfs_fstat, /* fstat */ @@ -169,214 +153,404 @@ const struct mountpt_operations g_zipfs_operations = * Private Functions ****************************************************************************/ -static voidpf zipfs_real_open(voidpf opaque, FAR const void *filename, - int mode) +static int zipfs_convert_result(int ret) { - FAR struct file *filep; - int ret; - - filep = fs_heap_malloc(sizeof(struct file)); - if (filep == NULL) + switch (ret) { - return NULL; - } + case ARCHIVE_RETRY: + return -EAGAIN; + case ARCHIVE_WARN: + return -ENOEXEC; + case ARCHIVE_FAILED: + return -EINVAL; + case ARCHIVE_FATAL: + return -EPERM; - ret = file_open(filep, filename, O_RDONLY); - if (ret < 0) - { - fs_heap_free(filep); - return NULL; + default: + return ret; } - - return filep; } -static uLong zipfs_real_read(voidpf opaque, voidpf stream, - FAR void *buf, uLong size) +static int zipfs_close_cb(FAR struct archive *a, FAR void *client_data) { - return file_read(stream, buf, size); -} + FAR struct zipfs_priv_s *priv = client_data; -static ZPOS64_T zipfs_real_tell(voidpf opaque, voidpf stream) -{ - return file_seek(stream, 0, SEEK_CUR); + return file_close(&priv->file); } -static long zipfs_real_seek(voidpf opaque, voidpf stream, ZPOS64_T offset, - int origin) +static la_ssize_t zipfs_read_cb(FAR struct archive *a, + FAR void *client_data, + FAR const void **buff) { - int ret; - - ret = file_seek(stream, offset, origin); - return ret >= 0 ? 0 : ret; -} + FAR struct zipfs_priv_s *priv = client_data; + ssize_t ret; -static int zipfs_real_close(voidpf opaque, voidpf stream) -{ - int ret; + ret = file_read(&priv->file, priv->buffer, + CONFIG_FS_ZIPFS_BUFFER_SIZE); + if (ret <= 0) + { + return ret; + } - ret = file_close(stream); - fs_heap_free(stream); + *buff = priv->buffer; return ret; } -static int zipfs_real_error(voidpf opaque, voidpf stream) +static la_int64_t zipfs_seek_cb(FAR struct archive *a, + FAR void *client_data, + la_int64_t offset, int whence) { - return OK; + FAR struct zipfs_priv_s *priv = client_data; + + return file_seek(&priv->file, offset, whence); } -static int zipfs_convert_result(int ziperr) +static off_t zipfs_skip(FAR struct zipfs_priv_s *priv, off_t amount) { - switch (ziperr) - { - case UNZ_END_OF_LIST_OF_FILE: - return -ENOENT; - case UNZ_CRCERROR: - ferr("zipfs file crc error\n"); - return -ESTALE; - case UNZ_INTERNALERROR: - return -EPERM; - case UNZ_BADZIPFILE: - return -EBADF; - case UNZ_PARAMERROR: - return -EINVAL; - default: - return ziperr; + off_t next = 0; + + if (priv->seekbuf == NULL) + { + priv->seekbuf = fs_heap_malloc(CONFIG_FS_ZIPFS_BUFFER_SIZE); + if (priv->seekbuf == NULL) + { + return -ENOMEM; + } } + + while (next < amount) + { + off_t remain = amount - next; + + if (remain > CONFIG_FS_ZIPFS_BUFFER_SIZE) + { + remain = CONFIG_FS_ZIPFS_BUFFER_SIZE; + } + + remain = archive_read_data(priv->a, priv->seekbuf, remain); + remain = zipfs_convert_result(remain); + if (remain <= 0) + { + return next ? next : remain; + } + + next += remain; + } + + return next; } -static int zipfs_open(FAR struct file *filep, FAR const char *relpath, - int oflags, mode_t mode) +int zipfs_new(FAR const char *abspath, FAR const char *relpath, + FAR struct zipfs_priv_s **priv) { - FAR struct zipfs_mountpt_s *fs = filep->f_inode->i_private; - FAR struct zipfs_file_s *fp; - int ret; + FAR struct zipfs_priv_s *newp; + int ret = -ENOMEM; - DEBUGASSERT(fs != NULL); + newp = fs_heap_zalloc(sizeof(struct zipfs_priv_s)); + if (newp == NULL) + { + return ret; + } - fp = fs_heap_malloc(sizeof(*fp) + strlen(relpath)); - if (fp == NULL) + newp->a = archive_read_new(); + if (newp->a == NULL) { - return -ENOMEM; + ferr("ERROR: archive_read_new() failed\n"); + goto err_with_priv; } - nxmutex_init(&fp->lock); + nxmutex_init(&newp->lock); +#ifdef CONFIG_FS_ZIPFS_FORMAT_ALL + ret = zipfs_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; + } +#else +# ifdef CONFIG_FS_ZIPFS_FORMAT_ZIP + ret = zipfs_convert_result(archive_read_support_format_zip(newp->a)); + if (ret < 0) + { + ferr("ERROR: archive_read_support_format_zip() failed\n"); + goto err_with_lock; + } +# endif - fp->uf = unzOpen2_64(fs->abspath, &zipfs_real_ops); - if (fp->uf == NULL) +# ifdef CONFIG_FS_ZIPFS_FORMAT_7ZIP + ret = zipfs_convert_result(archive_read_support_format_7zip(newp->a)); + if (ret < 0) { - ret = -EINVAL; - goto err_with_mutex; + ferr("ERROR: archive_read_support_format_7zip() failed\n"); + goto err_with_lock; } +# endif - ret = zipfs_convert_result(unzLocateFile(fp->uf, relpath, 0)); +# ifdef CONFIG_FS_ZIPFS_FORMAT_AR + ret = zipfs_convert_result(archive_read_support_format_ar(newp->a)); if (ret < 0) { - goto err_with_zip; + ferr("ERROR: archive_read_support_format_ar() failed\n"); + goto err_with_lock; } +# endif - ret = zipfs_convert_result(unzOpenCurrentFile(fp->uf)); +# ifdef CONFIG_FS_ZIPFS_FORMAT_CAB + ret = zipfs_convert_result(archive_read_support_format_cab(newp->a)); if (ret < 0) { - goto err_with_zip; + ferr("ERROR: archive_read_support_format_cab() failed\n"); + goto err_with_lock; } +# endif - if (ret == OK) +# ifdef CONFIG_FS_ZIPFS_FORMAT_CPIO + ret = zipfs_convert_result(archive_read_support_format_cpio(newp->a)); + if (ret < 0) { - fp->seekbuf = NULL; - strcpy(fp->relpath, relpath); - filep->f_priv = fp; + ferr("ERROR: archive_read_support_format_cpio() failed\n"); + goto err_with_lock; } - else +# endif + +# ifdef CONFIG_FS_ZIPFS_FORMAT_EMPTY + ret = zipfs_convert_result(archive_read_support_format_empty(newp->a)); + if (ret < 0) { -err_with_zip: - unzClose(fp->uf); -err_with_mutex: - nxmutex_destroy(&fp->lock); - fs_heap_free(fp); + ferr("ERROR: archive_read_support_format_empty() failed\n"); + goto err_with_lock; } +# endif - return ret; -} +# ifdef CONFIG_FS_ZIPFS_FORMAT_ISO9660 + ret = archive_read_support_format_iso9660(newp->a); + ret = zipfs_convert_result(ret); + if (ret < 0) + { + ferr("ERROR: archive_read_support_format_iso9660() failed\n"); + goto err_with_lock; + } +# endif -static int zipfs_close(FAR struct file *filep) -{ - FAR struct zipfs_file_s *fp = filep->f_priv; - int ret; - int ret1; +# ifdef CONFIG_FS_ZIPFS_FORMAT_LHA + ret = zipfs_convert_result(archive_read_support_format_lha(newp->a)); + if (ret < 0) + { + ferr("ERROR: archive_read_support_format_lha() failed\n"); + goto err_with_lock; + } +# endif - /* If read done, will check crc in unzCloseCurrentFile */ +# ifdef CONFIG_FS_ZIPFS_FORMAT_MTREE + ret = zipfs_convert_result(archive_read_support_format_mtree(newp->a)); + if (ret < 0) + { + ferr("ERROR: archive_read_support_format_mtree() failed\n"); + goto err_with_lock; + } +# endif - ret = zipfs_convert_result(unzCloseCurrentFile(fp->uf)); - ret1 = zipfs_convert_result(unzClose(fp->uf)); +# ifdef CONFIG_FS_ZIPFS_FORMAT_RAR + ret = zipfs_convert_result(archive_read_support_format_rar(newp->a)); + if (ret < 0) + { + ferr("ERROR: archive_read_support_format_rar() failed\n"); + goto err_with_lock; + } +# endif - nxmutex_destroy(&fp->lock); - fs_heap_free(fp->seekbuf); - fs_heap_free(fp); - return ret < 0 ? ret : ret1; -} +# ifdef CONFIG_FS_ZIPFS_FORMAT_RAR_V5 + ret = zipfs_convert_result(archive_read_support_format_rar5(newp->a)); + if (ret < 0) + { + ferr("ERROR: archive_read_support_format_rar5() failed\n"); + goto err_with_lock; + } +# endif -static ssize_t zipfs_read(FAR struct file *filep, FAR char *buffer, - size_t buflen) -{ - FAR struct zipfs_file_s *fp = filep->f_priv; - ssize_t ret; +# ifdef CONFIG_FS_ZIPFS_FORMAT_RAW + ret = zipfs_convert_result(archive_read_support_format_raw(newp->a)); + if (ret < 0) + { + ferr("ERROR: archive_read_support_format_raw() failed\n"); + goto err_with_lock; + } +# endif - nxmutex_lock(&fp->lock); - ret = zipfs_convert_result(unzReadCurrentFile(fp->uf, buffer, buflen)); - if (ret > 0) +# ifdef CONFIG_FS_ZIPFS_FORMAT_TAR + ret = zipfs_convert_result(archive_read_support_format_tar(newp->a)); + if (ret < 0) { - filep->f_pos += ret; + ferr("ERROR: archive_read_support_format_tar() failed\n"); + goto err_with_lock; } +# endif - nxmutex_unlock(&fp->lock); - return ret; -} +# ifdef CONFIG_FS_ZIPFS_FORMAT_WARC + ret = zipfs_convert_result(archive_read_support_format_warc(newp->a)); + if (ret < 0) + { + ferr("ERROR: archive_read_support_format_warc() failed\n"); + goto err_with_lock; + } +# endif -static off_t zipfs_skip(FAR struct zipfs_file_s *fp, off_t amount) -{ - off_t next = 0; +# ifdef CONFIG_FS_ZIPFS_FORMAT_XAR + ret = zipfs_convert_result(archive_read_support_format_xar(newp->a)); + if (ret < 0) + { + ferr("ERROR: archive_read_support_format_xar() failed\n"); + goto err_with_lock; + } +# endif +#endif - if (fp->seekbuf == NULL) + ret = file_open(&newp->file, abspath, O_RDONLY); + if (ret < 0) { - fp->seekbuf = fs_heap_malloc(CONFIG_ZIPFS_SEEK_BUFSIZE); - if (fp->seekbuf == NULL) - { - return -ENOMEM; - } + ferr("ERROR: file_open() failed\n"); + goto err_with_lock; } - while (next < amount) + archive_read_set_seek_callback(newp->a, zipfs_seek_cb); + ret = archive_read_open(newp->a, newp, NULL, + zipfs_read_cb, zipfs_close_cb); + ret = zipfs_convert_result(ret); + if (ret < 0) { - off_t remain = amount - next; + 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; + } - if (remain > CONFIG_ZIPFS_SEEK_BUFSIZE) + while (true) + { + ret = archive_read_next_header2(newp->a, newp->entry); + ret = zipfs_convert_result(ret); + if (ret < 0) { - remain = CONFIG_ZIPFS_SEEK_BUFSIZE; + break; } - remain = unzReadCurrentFile(fp->uf, fp->seekbuf, remain); - remain = zipfs_convert_result(remain); - if (remain <= 0) + if (strcmp(archive_entry_pathname(newp->entry), relpath) == 0) { - return next ? next : remain; + *priv = newp; + return OK; } + } - next += remain; + 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 zipfs_free(FAR struct zipfs_priv_s *priv) +{ + if (priv->seekbuf != NULL) + { + fs_heap_free(priv->seekbuf); } - return next; + 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 zipfs_stats_common(FAR struct zipfs_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; +} + +/**************************************************************************** + * Zipfs methods + ****************************************************************************/ + +static int zipfs_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 zipfs_priv_s *priv; + int ret; + + ret = zipfs_new(abspath, relpath, &priv); + if (ret < 0) + { + return ret; + } + + filep->f_priv = priv; + return OK; +} + +static int zipfs_close(FAR struct file *filep) +{ + FAR struct zipfs_priv_s *priv = filep->f_priv; + + zipfs_free(priv); + return OK; +} + +static ssize_t zipfs_read(FAR struct file *filep, FAR char *buffer, + size_t buflen) +{ + FAR struct zipfs_priv_s *priv = filep->f_priv; + ssize_t ret; + + nxmutex_lock(&priv->lock); + ret = zipfs_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 zipfs_seek(FAR struct file *filep, off_t offset, int whence) { - FAR struct zipfs_mountpt_s *fs = filep->f_inode->i_private; - FAR struct zipfs_file_s *fp = filep->f_priv; - unz_file_info64 file_info; + FAR const char *abspath = filep->f_inode->i_private; + FAR struct zipfs_priv_s *priv = filep->f_priv; + FAR struct zipfs_priv_s *newp; off_t ret = 0; - nxmutex_lock(&fp->lock); + nxmutex_lock(&priv->lock); switch (whence) { case SEEK_SET: @@ -385,15 +559,7 @@ static off_t zipfs_seek(FAR struct file *filep, off_t offset, offset += filep->f_pos; break; case SEEK_END: - ret = unzGetCurrentFileInfo64(fp->uf, &file_info, - NULL, 0, NULL, 0, NULL, 0); - ret = zipfs_convert_result(ret); - if (ret < 0) - { - goto err_with_lock; - } - - offset += file_info.uncompressed_size; + offset += archive_entry_size(priv->entry); break; default: ret = -EINVAL; @@ -406,214 +572,135 @@ static off_t zipfs_seek(FAR struct file *filep, off_t offset, } else if (filep->f_pos > offset) { - ret = zipfs_convert_result(unzClose(fp->uf)); - if (ret < 0) - { - goto err_with_lock; - } - - fp->uf = unzOpen2_64(fs->abspath, &zipfs_real_ops); - if (fp->uf == NULL) - { - ret = -EINVAL; - goto err_with_lock; - } - - ret = zipfs_convert_result(unzLocateFile(fp->uf, fp->relpath, 0)); - if (ret < 0) - { - goto err_with_lock; - } - - ret = zipfs_convert_result(unzOpenCurrentFile(fp->uf)); + ret = zipfs_new(abspath, archive_entry_pathname(priv->entry), + &newp); if (ret < 0) { goto err_with_lock; } + nxmutex_unlock(&priv->lock); + zipfs_free(priv); + priv = newp; + nxmutex_lock(&priv->lock); + filep->f_priv = newp; filep->f_pos = 0; } - ret = zipfs_skip(fp, offset - filep->f_pos); + ret = zipfs_skip(priv, offset - filep->f_pos); if (ret < 0) { goto err_with_lock; } - if (ret >= 0) - { - filep->f_pos += ret; - } + filep->f_pos += ret; err_with_lock: - nxmutex_unlock(&fp->lock); + nxmutex_unlock(&priv->lock); return ret < 0 ? ret : filep->f_pos; } static int zipfs_dup(FAR const struct file *oldp, FAR struct file *newp) { - FAR struct zipfs_file_s *fp; + FAR struct zipfs_priv_s *priv = oldp->f_priv; + FAR const char *relpath = archive_entry_pathname(priv->entry); - fp = oldp->f_priv; - return zipfs_open(newp, fp->relpath, oldp->f_oflags, 0); -} - -static int zipfs_stat_common(unzFile uf, FAR struct stat *buf) -{ - unz_file_info64 file_info; - int ret; - - memset(buf, 0, sizeof(struct stat)); - - ret = unzGetCurrentFileInfo64(uf, &file_info, NULL, 0, - NULL, 0, NULL, 0); - ret = zipfs_convert_result(ret); - if (ret >= 0) - { - buf->st_size = file_info.uncompressed_size; - buf->st_mode = S_IFREG | 0555; - } - - return ret; + return zipfs_open(newp, relpath, oldp->f_oflags, 0); } static int zipfs_fstat(FAR const struct file *filep, FAR struct stat *buf) { - FAR struct zipfs_file_s *fp = filep->f_priv; + FAR struct zipfs_priv_s *priv = filep->f_priv; - return zipfs_stat_common(fp->uf, buf); + return zipfs_stats_common(priv, buf); } -static int zipfs_opendir(FAR struct inode *mountpt, FAR const char *relpath, +static int zipfs_opendir(FAR struct inode *mountpt, + FAR const char *relpath, FAR struct fs_dirent_s **dir) { - FAR struct zipfs_mountpt_s *fs = mountpt->i_private; - FAR struct zipfs_dir_s *zdir; - - DEBUGASSERT(fs != NULL); + FAR const char *abspath = mountpt->i_private; + FAR struct zipfs_dir_s *adir; + int ret; - zdir = fs_heap_malloc(sizeof(*zdir)); - if (zdir == NULL) + adir = fs_heap_zalloc(sizeof(struct zipfs_dir_s)); + if (adir == NULL) { return -ENOMEM; } - nxmutex_init(&zdir->lock); - - zdir->uf = unzOpen2_64(fs->abspath, &zipfs_real_ops); - if (zdir->uf == NULL) + ret = zipfs_new(abspath, NULL, &adir->priv); + if (ret < 0) { - nxmutex_destroy(&zdir->lock); - fs_heap_free(zdir); - return -EINVAL; + fs_heap_free(adir); + return ret; } - zdir->last = false; - *dir = &zdir->base; + *dir = &adir->dir; return OK; } static int zipfs_closedir(FAR struct inode *mountpt, FAR struct fs_dirent_s *dir) { - FAR struct zipfs_dir_s *zdir = (FAR struct zipfs_dir_s *)dir; - int ret; + FAR struct zipfs_dir_s *adir = (FAR struct zipfs_dir_s *)dir; - zdir = (FAR struct zipfs_dir_s *)dir; - ret = zipfs_convert_result(unzClose(zdir->uf)); - nxmutex_destroy(&zdir->lock); - fs_heap_free(zdir); - return ret; + zipfs_free(adir->priv); + fs_heap_free(adir); + return OK; } static int zipfs_readdir(FAR struct inode *mountpt, FAR struct fs_dirent_s *dir, FAR struct dirent *entry) { - FAR struct zipfs_dir_s *zdir = (FAR struct zipfs_dir_s *)dir; - unz_file_info64 file_info; + FAR struct zipfs_dir_s *adir = (FAR struct zipfs_dir_s *)dir; + FAR struct zipfs_priv_s *priv = adir->priv; int ret; - nxmutex_lock(&zdir->lock); - ret = unzGetCurrentFileInfo64(zdir->uf, - &file_info, - entry->d_name, - NAME_MAX, NULL, 0, NULL, 0); - + nxmutex_lock(&priv->lock); + ret = archive_read_next_header2(priv->a, priv->entry); ret = zipfs_convert_result(ret); if (ret < 0) { - goto err_with_lock; + goto err; } - ret = zipfs_convert_result(unzGoToNextFile(zdir->uf)); - if (ret == -ENOENT) + if (ret == ARCHIVE_EOF) { - if (zdir->last == false) - { - ret = OK; - zdir->last = true; - } + ret = EOF; + goto err; } -err_with_lock: - nxmutex_unlock(&zdir->lock); + strlcpy(entry->d_name, archive_entry_pathname(priv->entry), + sizeof(entry->d_name)); +err: + nxmutex_unlock(&priv->lock); return ret; } static int zipfs_rewinddir(FAR struct inode *mountpt, FAR struct fs_dirent_s *dir) { - FAR struct zipfs_dir_s *zdir = (FAR struct zipfs_dir_s *)dir; - int ret; - - nxmutex_lock(&zdir->lock); - zdir->last = false; - ret = zipfs_convert_result(unzGoToFirstFile(zdir->uf)); - nxmutex_unlock(&zdir->lock); - return ret; -} + FAR struct zipfs_dir_s *adir = (FAR struct zipfs_dir_s *)dir; + FAR struct zipfs_priv_s *priv = adir->priv; -static int zipfs_bind(FAR struct inode *driver, FAR const void *data, - FAR void **handle) -{ - FAR struct zipfs_mountpt_s *fs; - unzFile uf; - - if (data == NULL) - { - return -ENODEV; - } - - fs = fs_heap_zalloc(sizeof(struct zipfs_mountpt_s) + strlen(data)); - if (fs == NULL) + 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; } - uf = unzOpen2_64(data, &zipfs_real_ops); - if (uf == NULL) - { - fs_heap_free(fs); - return -EINVAL; - } - - unzClose(uf); - strcpy(fs->abspath, data); - *handle = fs; - - return OK; -} - -static int zipfs_unbind(FAR void *handle, FAR struct inode **driver, - unsigned int flags) -{ - fs_heap_free(handle); + nxmutex_unlock(&priv->lock); return OK; } -static int zipfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) +static int zipfs_statfs(FAR struct inode *mountpt, + FAR struct statfs *buf) { buf->f_type = ZIPFS_MAGIC; buf->f_namelen = NAME_MAX; @@ -621,38 +708,65 @@ static int zipfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf) } static int zipfs_stat(FAR struct inode *mountpt, - FAR const char *relpath, FAR struct stat *buf) + FAR const char *relpath, + FAR struct stat *buf) { - FAR struct zipfs_mountpt_s *fs; - unzFile uf; + FAR const char *abspath = mountpt->i_private; + FAR struct zipfs_priv_s *priv; int ret; - /* Sanity checks */ - - DEBUGASSERT(mountpt && mountpt->i_private); - if (relpath[0] == 0) { buf->st_mode = S_IFDIR | 0555; return OK; } - fs = mountpt->i_private; - uf = unzOpen2_64(fs->abspath, &zipfs_real_ops); - if (uf == NULL) + ret = zipfs_new(abspath, relpath, &priv); + if (ret < 0) { - return -EINVAL; + return ret; + } + + ret = zipfs_stats_common(priv, buf); + zipfs_free(priv); + return ret; +} + +static int zipfs_bind(FAR struct inode *driver, FAR const void *data, + FAR void **handle) +{ + FAR struct zipfs_priv_s *priv; + FAR char *abspath; + int ret; + + if (data == NULL) + { + return -ENODEV; } - ret = zipfs_convert_result(unzLocateFile(uf, relpath, 0)); + /* Try access to zip file */ + + ret = zipfs_new(data, NULL, &priv); if (ret < 0) { - unzClose(uf); return ret; } - ret = zipfs_stat_common(uf, buf); + zipfs_free(priv); + abspath = fs_heap_zalloc(PATH_MAX); + if (abspath == NULL) + { + return -ENOMEM; + } - unzClose(uf); - return ret; + *handle = abspath; + strlcpy(abspath, data, PATH_MAX); + return OK; +} + +static int zipfs_unbind(FAR void *handle, FAR struct inode **driver, + unsigned int flags) +{ + fs_heap_free(handle); + return OK; } diff --git a/fs/zipfs/zlib b/fs/zipfs/zlib deleted file mode 120000 index 8ab5e157160..00000000000 --- a/fs/zipfs/zlib +++ /dev/null @@ -1 +0,0 @@ -../../../apps/system/zlib \ No newline at end of file diff --git a/include/sys/statfs.h b/include/sys/statfs.h index 14f44320cba..81e80af5417 100644 --- a/include/sys/statfs.h +++ b/include/sys/statfs.h @@ -105,7 +105,6 @@ #define ZIPFS_MAGIC 0x504b #define XIPFS_MAGIC 0x58495046 #define V9FS_MAGIC 0x01021997 -#define ARCHIVEFS_MAGIC 0x504c #endif /* NUTTX_NO_EXTENSIONS */
