Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/librefuse/refuse lib/librefuse: Implement data buffer AP...



details:   https://anonhg.NetBSD.org/src/rev/1f7bc4d040a4
branches:  trunk
changeset: 359759:1f7bc4d040a4
user:      pho <pho%NetBSD.org@localhost>
date:      Sat Jan 22 07:54:56 2022 +0000

description:
lib/librefuse: Implement data buffer API appeared on FUSE 2.9

diffstat:

 distrib/sets/lists/comp/mi        |    3 +-
 lib/librefuse/TODO                |    3 +-
 lib/librefuse/fuse.h              |    3 +-
 lib/librefuse/refuse/Makefile.inc |    4 +-
 lib/librefuse/refuse/buf.c        |  320 ++++++++++++++++++++++++++++++++++++++
 lib/librefuse/refuse/buf.h        |  109 ++++++++++++
 6 files changed, 438 insertions(+), 4 deletions(-)

diffs (truncated from 502 to 300 lines):

diff -r f0f59e886d59 -r 1f7bc4d040a4 distrib/sets/lists/comp/mi
--- a/distrib/sets/lists/comp/mi        Sat Jan 22 07:53:05 2022 +0000
+++ b/distrib/sets/lists/comp/mi        Sat Jan 22 07:54:56 2022 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: mi,v 1.2405 2022/01/22 07:53:05 pho Exp $
+#      $NetBSD: mi,v 1.2406 2022/01/22 07:54:56 pho Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 ./etc/mtree/set.comp                           comp-sys-root
@@ -3092,6 +3092,7 @@
 ./usr/include/readline.h                       comp-obsolete           obsolete
 ./usr/include/readline/history.h               comp-c-include
 ./usr/include/readline/readline.h              comp-c-include
+./usr/include/refuse/buf.h                     comp-refuse-include
 ./usr/include/refuse/session.h                 comp-refuse-include
 ./usr/include/regex.h                          comp-c-include
 ./usr/include/regexp.h                         comp-c-include
diff -r f0f59e886d59 -r 1f7bc4d040a4 lib/librefuse/TODO
--- a/lib/librefuse/TODO        Sat Jan 22 07:53:05 2022 +0000
+++ b/lib/librefuse/TODO        Sat Jan 22 07:54:56 2022 +0000
@@ -1,4 +1,4 @@
-       $NetBSD: TODO,v 1.4 2016/11/16 16:11:42 pho Exp $
+       $NetBSD: TODO,v 1.5 2022/01/22 07:54:57 pho Exp $
 
 To Do
 =====
@@ -11,6 +11,7 @@
 do proper implementations of dirfillers
 statfs - some fuse file systems want struct statfs and we only have
          statvfs available natively
+Support data buffers appeared on FUSE 2.9 (struct fuse_buf).
 
 Done
 ====
diff -r f0f59e886d59 -r 1f7bc4d040a4 lib/librefuse/fuse.h
--- a/lib/librefuse/fuse.h      Sat Jan 22 07:53:05 2022 +0000
+++ b/lib/librefuse/fuse.h      Sat Jan 22 07:54:56 2022 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: fuse.h,v 1.25 2022/01/22 07:53:06 pho Exp $ */
+/* $NetBSD: fuse.h,v 1.26 2022/01/22 07:54:57 pho Exp $ */
 
 /*
  * Copyright © 2007 Alistair Crooks.  All rights reserved.
@@ -30,6 +30,7 @@
 #ifndef FUSE_H_
 #define FUSE_H_        20211204
 
+#include <refuse/buf.h>
 #include <refuse/session.h>
 #include <sys/cdefs.h>
 #include <sys/stat.h>
diff -r f0f59e886d59 -r 1f7bc4d040a4 lib/librefuse/refuse/Makefile.inc
--- a/lib/librefuse/refuse/Makefile.inc Sat Jan 22 07:53:05 2022 +0000
+++ b/lib/librefuse/refuse/Makefile.inc Sat Jan 22 07:54:56 2022 +0000
@@ -1,7 +1,9 @@
-# $NetBSD: Makefile.inc,v 1.1 2022/01/22 07:53:06 pho Exp $
+# $NetBSD: Makefile.inc,v 1.2 2022/01/22 07:54:57 pho Exp $
 
 .PATH: ${.CURDIR}/refuse
 
+SRCS+= buf.c
 SRCS+= session.c
 
+INCS+= refuse/buf.h
 INCS+= refuse/session.h
diff -r f0f59e886d59 -r 1f7bc4d040a4 lib/librefuse/refuse/buf.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/librefuse/refuse/buf.c        Sat Jan 22 07:54:56 2022 +0000
@@ -0,0 +1,320 @@
+/* $NetBSD: buf.c,v 1.1 2022/01/22 07:54:57 pho Exp $ */
+
+/*
+ * Copyright (c) 2021 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote
+ *    products derived from this software without specific prior written
+ *    permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#if !defined(lint)
+__RCSID("$NetBSD: buf.c,v 1.1 2022/01/22 07:54:57 pho Exp $");
+#endif /* !lint */
+
+#include <assert.h>
+#include <errno.h>
+#include <fuse_internal.h>
+#include <machine/vmparam.h> /* for PAGE_SIZE */
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/param.h> /* for MIN(a, b) */
+#include <unistd.h>
+
+size_t
+fuse_buf_size(const struct fuse_bufvec *bufv) {
+    size_t i;
+    size_t total = 0;
+
+    for (i = 0; i < bufv->count; i++) {
+        total += bufv->buf[i].size;
+    }
+
+    return total;
+}
+
+/* Return the pointer to the current buffer in a buffer vector, or
+ * NULL if we have reached the end of the vector. */
+static const struct fuse_buf*
+fuse_buf_current(const struct fuse_bufvec *bufv) {
+    if (bufv->idx < bufv->count)
+        return &bufv->buf[bufv->idx];
+    else
+        return NULL;
+}
+
+/* Copy data from one fd to a memory buffer, and return the number of
+ * octets that have been copied, or -1 on failure. */
+static ssize_t
+fuse_buf_read_fd_to_mem(const struct fuse_buf *dst, size_t dst_off,
+                        const struct fuse_buf *src, size_t src_off,
+                        size_t len) {
+    ssize_t total = 0;
+
+    while (len > 0) {
+        ssize_t n_read;
+
+        if (src->flags & FUSE_BUF_FD_SEEK)
+            n_read = pread(src->fd, (uint8_t*)dst->mem + dst_off, len,
+                           src->pos + (off_t)src_off);
+        else
+            n_read = read(src->fd, (uint8_t*)dst->mem + dst_off, len);
+
+        if (n_read == -1) {
+            if (errno == EINTR)
+                continue;
+            else if (total == 0)
+                return -1;
+            else
+                /* The last pread(2) or read(2) failed but we have
+                 * already copied some data. */
+                break;
+        }
+        else if (n_read == 0) {
+            /* Reached EOF */
+            break;
+        }
+        else {
+            total   += n_read;
+            dst_off += (size_t)n_read;
+            src_off += (size_t)n_read;
+            len     -= (size_t)n_read;
+
+            if (src->flags & FUSE_BUF_FD_RETRY)
+                continue;
+        }
+    }
+
+    return total;
+}
+
+/* Copy data from one memory buffer to an fd, and return the number of
+ * octets that have been copied, or -1 on failure. */
+static ssize_t
+fuse_buf_write_mem_to_fd(const struct fuse_buf *dst, size_t dst_off,
+                         const struct fuse_buf *src, size_t src_off,
+                         size_t len) {
+    ssize_t total = 0;
+
+    while (len > 0) {
+        ssize_t n_wrote;
+
+        if (dst->flags & FUSE_BUF_FD_SEEK)
+            n_wrote = pwrite(dst->fd, (uint8_t*)src->mem + src_off, len,
+                             dst->pos + (off_t)dst_off);
+        else
+            n_wrote = write(dst->fd, (uint8_t*)src->mem + src_off, len);
+
+        if (n_wrote == -1) {
+            if (errno == EINTR)
+                continue;
+            else if (total == 0)
+                return -1;
+            else
+                /* The last pwrite(2) or write(2) failed but we have
+                 * already copied some data. */
+                break;
+        }
+        else if (n_wrote == 0) {
+            break;
+        }
+        else {
+            total   += n_wrote;
+            dst_off += (size_t)n_wrote;
+            src_off += (size_t)n_wrote;
+            len     -= (size_t)n_wrote;
+
+            if (dst->flags & FUSE_BUF_FD_RETRY)
+                continue;
+        }
+    }
+
+    return total;
+}
+
+/* Copy data from one fd to another, and return the number of octets
+ * that have been copied, or -1 on failure. */
+static ssize_t
+fuse_buf_copy_fd_to_fd(const struct fuse_buf *dst, size_t dst_off,
+                       const struct fuse_buf *src, size_t src_off,
+                       size_t len) {
+    ssize_t total = 0;
+    struct fuse_buf tmp;
+
+    tmp.size  = PAGE_SIZE;
+    tmp.flags = (enum fuse_buf_flags)0;
+    tmp.mem   = malloc(tmp.size);
+
+    if (tmp.mem == NULL) {
+        return -1;
+    }
+
+    while (len) {
+        size_t n_to_read = MIN(tmp.size, len);
+        ssize_t n_read;
+        ssize_t n_wrote;
+
+        n_read = fuse_buf_read_fd_to_mem(&tmp, 0, src, src_off, n_to_read);
+        if (n_read == -1) {
+            if (total == 0) {
+                free(tmp.mem);
+                return -1;
+            }
+            else {
+                /* We have already copied some data. */
+                break;
+            }
+        }
+        else if (n_read == 0) {
+            /* Reached EOF */
+            break;
+        }
+
+        n_wrote = fuse_buf_write_mem_to_fd(dst, dst_off, &tmp, 0, (size_t)n_read);
+        if (n_wrote == -1) {
+            if (total == 0) {
+                free(tmp.mem);
+                return -1;
+            }
+            else {
+                /* We have already copied some data. */
+                break;
+            }
+        }
+        else if (n_wrote == 0) {
+            break;
+        }
+
+        total   += n_wrote;
+        dst_off += (size_t)n_wrote;
+        src_off += (size_t)n_wrote;
+        len     -= (size_t)n_wrote;
+
+        if (n_wrote < n_read)
+            /* Now we have some data that were read but couldn't be
+             * written, and can't do anything about it. */
+            break;
+    }
+
+    free(tmp.mem);
+    return total;
+}
+
+/* Copy data from one buffer to another, and return the number of
+ * octets that have been copied. */
+static ssize_t
+fuse_buf_copy_one(const struct fuse_buf *dst, size_t dst_off,
+                  const struct fuse_buf *src, size_t src_off,



Home | Main Index | Thread Index | Old Index