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 5710c6866eb88319940f0b15a19d5f09a89624d0 Author: anjiahao <[email protected]> AuthorDate: Wed May 13 15:31:54 2026 +0800 fs/zipfs: support fd= mount option for wzipfs Extend wzipfs with an 'fd=<N>' mount option so callers can stream a ZIP archive directly into an already-open file descriptor without routing through a pseudo-filesystem path. Typical use cases are anonymous pipe(2), connected socket, or stdout. Option semantics: mount -t wzipfs -o src=<path>[,method=...] <mnt> (existing) mount -t wzipfs -o fd=<N>[,method=...] <mnt> (new) src= and fd= are mutually exclusive; giving neither returns -ENODEV, giving both returns -EINVAL. Implementation notes: - fd= uses file_get() to look up the caller's struct file, then file_dup2() to clone it into the mount-private backing. The caller retains its original fd and MUST keep it open until umount(2) returns so that the final close releases any shared inode state (pipe writer count, socket reference, etc.). - Both paths end in file_close(&priv->backing) on unbind. fd= uses the libarchive-owned dup; src= uses file_open() directly on the embedded struct file. - No stat/size check is performed on fd= targets; callers own the backing semantics. The existing non-empty regular file rejection remains for src= only. Signed-off-by: anjiahao <[email protected]> Signed-off-by: tengshuangshuang <[email protected]> --- Documentation/os/filesystem/zipfs.rst | 34 +++++++++++++++++++----- fs/zipfs/wzip_vfs.c | 50 ++++++++++++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 10 deletions(-) diff --git a/Documentation/os/filesystem/zipfs.rst b/Documentation/os/filesystem/zipfs.rst index a0a4c0b7cae..fd6df7d2621 100644 --- a/Documentation/os/filesystem/zipfs.rst +++ b/Documentation/os/filesystem/zipfs.rst @@ -52,15 +52,25 @@ Mount Syntax:: mount -t wzipfs -o src=<backing>[,method=store|deflate] <mountpoint> + mount -t wzipfs -o fd=<N>[,method=store|deflate] <mountpoint> + +Exactly one of ``src=`` or ``fd=`` must be supplied. Behavior: -- If ``<backing>`` does not exist or has zero size, wzipfs streams a new - ZIP archive into it. -- If ``<backing>`` is a regular file with ``st_size > 0``, mount fails with - ``-EEXIST``. Remove or truncate the backing file before mounting to - start a fresh archive. -- FIFO and socket targets are always accepted and stream from offset 0. +- ``src=<backing>`` -- wzipfs opens the path. If ``<backing>`` does not + exist or has zero size, a new ZIP archive is streamed into it. If it + is a regular file with ``st_size > 0``, mount fails with ``-EEXIST``; + remove or truncate the backing file before mounting to start a fresh + archive. FIFO and socket targets are always accepted. +- ``fd=<N>`` -- wzipfs duplicates the caller-provided writable file + descriptor into its own backing. The caller retains the original fd + and MUST keep it open until ``umount(2)`` returns, so that the final + close on that fd (after umount) releases any shared inode state + (e.g. pipe writer count, socket reference). No stat/size check is + performed: callers are responsible for their own backing semantics + (pipe, socket, regular file). Typical use cases are streaming to an + anonymous ``pipe(2)``, to a connected socket, or to ``stdout``. Compression ----------- @@ -139,6 +149,18 @@ Stream to a FIFO (pipe to another process):: echo data > /mnt/zip/data.txt umount /mnt/zip +Stream to a caller-provided fd (anonymous pipe, socket, stdout):: + + int pipefd[2]; + char opts[32]; + + pipe(pipefd); + snprintf(opts, sizeof(opts), "fd=%d", pipefd[1]); + mount(NULL, "/mnt/zip", "wzipfs", 0, opts); + /* write entries through /mnt/zip/... while another task drains pipefd[0] */ + umount("/mnt/zip"); /* closes wzipfs's own dup of the fd */ + close(pipefd[1]); /* final close: delivers EOF to pipefd[0] reader */ + DEFLATE compression:: mount -t wzipfs -o src=/data/out.zip,method=deflate /mnt/zip diff --git a/fs/zipfs/wzip_vfs.c b/fs/zipfs/wzip_vfs.c index 56ccbc3804c..c5f9a01041b 100644 --- a/fs/zipfs/wzip_vfs.c +++ b/fs/zipfs/wzip_vfs.c @@ -155,20 +155,22 @@ static int wzipfs_convert_result(int ret) * * Description: * Parse a NuttX mount "-o" options string. - * Accepted keys: src=<path>, method=store|deflate. + * Accepted keys: src=<path>, fd=<N>, method=store|deflate. * Legacy positional path (no '=' and no ',') is accepted as src. + * src= and fd= are mutually exclusive. * * Returns 0 on success, negative errno on failure. ****************************************************************************/ static int wzipfs_parse_options(FAR const void *data, FAR char *src, size_t len, - FAR bool *deflate) + FAR int *fd, FAR bool *deflate) { FAR const char *p = data; FAR const char *end; src[0] = '\0'; + *fd = -1; *deflate = false; if (p == NULL || *p == '\0') @@ -207,6 +209,20 @@ static int wzipfs_parse_options(FAR const void *data, memcpy(src, p + 4, pl); src[pl] = '\0'; } + else if (end - p > 3 && memcmp(p, "fd=", 3) == 0) + { + FAR char *tail; + long v; + + v = strtol(p + 3, &tail, 0); + if (tail != end || v < 0 || v > INT_MAX) + { + ferr("wzipfs: invalid fd value\n"); + return -EINVAL; + } + + *fd = (int)v; + } else if (end - p > 7 && memcmp(p, "method=", 7) == 0) { FAR const char *val = p + 7; @@ -237,6 +253,17 @@ static int wzipfs_parse_options(FAR const void *data, p = *end ? end + 1 : end; } + if (*fd >= 0 && src[0] != '\0') + { + ferr("wzipfs: src= and fd= are mutually exclusive\n"); + return -EINVAL; + } + + if (*fd < 0 && src[0] == '\0') + { + return -ENODEV; + } + return 0; } @@ -523,11 +550,13 @@ static int wzipfs_bind(FAR struct inode *driver, FAR const void *data, FAR void **handle) { FAR struct wzipfs_priv_s *priv; + FAR struct file *src_filep; char src[PATH_MAX]; bool deflate; int ret; + int fd; - ret = wzipfs_parse_options(data, src, sizeof(src), &deflate); + ret = wzipfs_parse_options(data, src, sizeof(src), &fd, &deflate); if (ret < 0) { return ret; @@ -541,7 +570,20 @@ static int wzipfs_bind(FAR struct inode *driver, FAR const void *data, nxsem_init(&priv->open_sem, 0, 1); - ret = wzipfs_create_backing(priv, src); + if (fd >= 0) + { + ret = file_get(fd, &src_filep); + if (ret >= 0) + { + ret = file_dup2(src_filep, &priv->backing); + file_put(src_filep); + } + } + else + { + ret = wzipfs_create_backing(priv, src); + } + if (ret < 0) { goto err;
