xref: /netbsd-src/lib/librefuse/refuse/buf.h (revision ce847e0a0bdc988b3b1af277e09170bc78742161)
1 /* $NetBSD: buf.h,v 1.1 2022/01/22 07:54:57 pho Exp $ */
2 
3 /*
4  * Copyright (c) 2021 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote
16  *    products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 #if !defined(_FUSE_BUF_H_)
32 #define _FUSE_BUF_H_
33 
34 #include <sys/types.h>
35 
36 /* Data buffer API, appeared on FUSE 2.9. */
37 
38 #if !defined(FUSE_H_)
39 #  error Do not include this header directly. Include <fuse.h> instead.
40 #endif
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 enum fuse_buf_flags {
47 	/* The buffer is actually an fd: .mem is invalid but .fd and
48 	 * .pos have valid values. */
49 	FUSE_BUF_IS_FD		= (1 << 1),
50 	/* The fd is seekable. */
51 	FUSE_BUF_FD_SEEK	= (1 << 2),
52 	/* Keep reading from/writing to the fd until reaching EOF. */
53 	FUSE_BUF_FD_RETRY	= (1 << 3),
54 };
55 enum fuse_buf_copy_flags {
56 	/* These flags are always ignored because splice(2) is a
57 	 * Linux-specific syscall and there are no alternatives on
58 	 * NetBSD atm. */
59 	FUSE_BUF_NO_SPLICE		= (1 << 1),
60 	FUSE_BUF_FORCE_SPLICE		= (1 << 2),
61 	FUSE_BUF_SPLICE_MOVE		= (1 << 3),
62 	FUSE_BUF_SPLICE_NONBLOCK	= (1 << 4),
63 };
64 struct fuse_buf {
65 	size_t			size;
66 	enum fuse_buf_flags	flags;
67 	void			*mem;
68 	int			fd;
69 	off_t			pos;
70 };
71 struct fuse_bufvec {
72 	/* The number of buffers in the vector. */
73 	size_t		count;
74 	/* The index of the current buffer in the vector. */
75 	size_t		idx;
76 	/* The current offset in the current buffer. */
77 	size_t		off;
78 	/* The vector of buffers. */
79 	struct fuse_buf	buf[1];
80 };
81 #define FUSE_BUFVEC_INIT(size_)				\
82 	((struct fuse_bufvec) {				\
83 		/* .count = */ 1,			\
84 		/* .idx = */ 0,				\
85 		/* .off = */ 0,				\
86 		/* .buf = */ {				\
87 			/* [0] = */ {			\
88 				/* .size = */ (size_)	\
89 				/* .flags = */ (enum fuse_buf_flags)0,	\
90 				/* .mem = */ NULL,	\
91 				/* .fd = */ -1,		\
92 				/* .pos = */ 0,		\
93 			}				\
94 		}					\
95 	})
96 
97 /* Get the total size of data in a buffer vector. */
98 size_t fuse_buf_size(const struct fuse_bufvec *bufv);
99 
100 /* Copy data from one buffer vector to another, and return the number
101  * of octets that have been copied. */
102 ssize_t fuse_buf_copy(struct fuse_bufvec *dstv, struct fuse_bufvec *srcv,
103 					  enum fuse_buf_copy_flags flags);
104 
105 #ifdef __cplusplus
106 }
107 #endif
108 
109 #endif
110