fuse_buf_copy: check if buffers are the same
[fuse.git] / lib / buffer.c
blobcb734a3c72763d4bd9612b42c5767d0e6c11adfd
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2010 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU LGPLv2.
6 See the file COPYING.LIB
7 */
9 #define _GNU_SOURCE
11 #include "fuse_i.h"
12 #include "fuse_lowlevel.h"
13 #include <string.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <assert.h>
18 size_t fuse_buf_size(const struct fuse_bufvec *bufv)
20 size_t i;
21 size_t size = 0;
23 for (i = 0; i < bufv->count; i++) {
24 if (bufv->buf[i].size == SIZE_MAX)
25 size = SIZE_MAX;
26 else
27 size += bufv->buf[i].size;
30 return size;
33 static size_t min_size(size_t s1, size_t s2)
35 return s1 < s2 ? s1 : s2;
38 static ssize_t fuse_buf_write(const struct fuse_buf *dst, size_t dst_off,
39 const struct fuse_buf *src, size_t src_off,
40 size_t len)
42 ssize_t res = 0;
43 size_t copied = 0;
45 while (len) {
46 if (dst->flags & FUSE_BUF_FD_SEEK) {
47 res = pwrite(dst->fd, src->mem + src_off, len,
48 dst->pos + dst_off);
49 } else {
50 res = write(dst->fd, src->mem + src_off, len);
52 if (res == -1) {
53 if (!copied)
54 return -errno;
55 break;
57 if (res == 0)
58 break;
60 copied += res;
61 if (!(dst->flags & FUSE_BUF_FD_RETRY))
62 break;
64 src_off += res;
65 dst_off += res;
66 len -= res;
69 return copied;
72 static ssize_t fuse_buf_read(const struct fuse_buf *dst, size_t dst_off,
73 const struct fuse_buf *src, size_t src_off,
74 size_t len)
76 ssize_t res = 0;
77 size_t copied = 0;
79 while (len) {
80 if (src->flags & FUSE_BUF_FD_SEEK) {
81 res = pread(src->fd, dst->mem + dst_off, len,
82 src->pos + src_off);
83 } else {
84 res = read(src->fd, dst->mem + dst_off, len);
86 if (res == -1) {
87 if (!copied)
88 return -errno;
89 break;
91 if (res == 0)
92 break;
94 copied += res;
95 if (!(src->flags & FUSE_BUF_FD_RETRY))
96 break;
98 dst_off += res;
99 src_off += res;
100 len -= res;
103 return copied;
106 static ssize_t fuse_buf_fd_to_fd(const struct fuse_buf *dst, size_t dst_off,
107 const struct fuse_buf *src, size_t src_off,
108 size_t len)
110 char buf[4096];
111 struct fuse_buf tmp = {
112 .size = sizeof(buf),
113 .flags = 0,
115 ssize_t res;
116 size_t copied = 0;
118 tmp.mem = buf;
120 while (len) {
121 size_t this_len = min_size(tmp.size, len);
122 size_t read_len;
124 res = fuse_buf_read(&tmp, 0, src, src_off, this_len);
125 if (res < 0) {
126 if (!copied)
127 return res;
128 break;
130 if (res == 0)
131 break;
133 read_len = res;
134 res = fuse_buf_write(dst, dst_off, &tmp, 0, read_len);
135 if (res < 0) {
136 if (!copied)
137 return res;
138 break;
140 if (res == 0)
141 break;
143 copied += res;
145 if (res < this_len)
146 break;
148 dst_off += res;
149 src_off += res;
150 len -= res;
153 return copied;
156 static ssize_t fuse_buf_splice(const struct fuse_buf *dst, size_t dst_off,
157 const struct fuse_buf *src, size_t src_off,
158 size_t len, enum fuse_buf_copy_flags flags)
160 int splice_flags = 0;
161 off_t *srcpos = NULL;
162 off_t *dstpos = NULL;
163 off_t srcpos_val;
164 off_t dstpos_val;
165 ssize_t res;
166 size_t copied = 0;
168 if (flags & FUSE_BUF_SPLICE_MOVE)
169 splice_flags |= SPLICE_F_MOVE;
170 if (flags & FUSE_BUF_SPLICE_NONBLOCK)
171 splice_flags |= SPLICE_F_NONBLOCK;
173 if (src->flags & FUSE_BUF_FD_SEEK) {
174 srcpos_val = src->pos + src_off;
175 srcpos = &srcpos_val;
177 if (dst->flags & FUSE_BUF_FD_SEEK) {
178 dstpos_val = dst->pos + dst_off;
179 dstpos = &dstpos_val;
182 while (len) {
183 res = splice(src->fd, srcpos, dst->fd, dstpos, len,
184 splice_flags);
185 if (res == -1) {
186 if (copied)
187 break;
189 if (errno != EINVAL || (flags & FUSE_BUF_FORCE_SPLICE))
190 return -errno;
192 /* Maybe splice is not supported for this combination */
193 return fuse_buf_fd_to_fd(dst, dst_off, src, src_off,
194 len);
196 if (res == 0)
197 break;
199 copied += res;
200 if (!(src->flags & FUSE_BUF_FD_RETRY) &&
201 !(dst->flags & FUSE_BUF_FD_RETRY)) {
202 break;
205 len -= res;
208 return copied;
212 static ssize_t fuse_buf_copy_one(const struct fuse_buf *dst, size_t dst_off,
213 const struct fuse_buf *src, size_t src_off,
214 size_t len, enum fuse_buf_copy_flags flags)
216 int src_is_fd = src->flags & FUSE_BUF_IS_FD;
217 int dst_is_fd = dst->flags & FUSE_BUF_IS_FD;
219 if (!src_is_fd && !dst_is_fd) {
220 void *dstmem = dst->mem + dst_off;
221 void *srcmem = src->mem + src_off;
223 if (dstmem != srcmem) {
224 if (dstmem + len <= srcmem || srcmem + len <= dstmem)
225 memcpy(dstmem, srcmem, len);
226 else
227 memmove(dstmem, srcmem, len);
230 return len;
231 } else if (!src_is_fd) {
232 return fuse_buf_write(dst, dst_off, src, src_off, len);
233 } else if (!dst_is_fd) {
234 return fuse_buf_read(dst, dst_off, src, src_off, len);
235 } else if (flags & FUSE_BUF_NO_SPLICE) {
236 return fuse_buf_fd_to_fd(dst, dst_off, src, src_off, len);
237 } else {
238 return fuse_buf_splice(dst, dst_off, src, src_off, len, flags);
242 static const struct fuse_buf *fuse_bufvec_current(struct fuse_bufvec *bufv)
244 if (bufv->idx < bufv->count)
245 return &bufv->buf[bufv->idx];
246 else
247 return NULL;
250 static int fuse_bufvec_advance(struct fuse_bufvec *bufv, size_t len)
252 const struct fuse_buf *buf = fuse_bufvec_current(bufv);
254 bufv->off += len;
255 assert(bufv->off <= buf->size);
256 if (bufv->off == buf->size) {
257 assert(bufv->idx < bufv->count);
258 bufv->idx++;
259 if (bufv->idx == bufv->count)
260 return 0;
261 bufv->off = 0;
263 return 1;
266 ssize_t fuse_buf_copy(struct fuse_bufvec *dstv, struct fuse_bufvec *srcv,
267 enum fuse_buf_copy_flags flags)
269 size_t copied = 0;
271 if (dstv == srcv)
272 return fuse_buf_size(dstv);
274 for (;;) {
275 const struct fuse_buf *src = fuse_bufvec_current(srcv);
276 const struct fuse_buf *dst = fuse_bufvec_current(dstv);
277 size_t src_len;
278 size_t dst_len;
279 size_t len;
280 ssize_t res;
282 if (src == NULL || dst == NULL)
283 break;
285 src_len = src->size - srcv->off;
286 dst_len = dst->size - dstv->off;
287 len = min_size(src_len, dst_len);
289 res = fuse_buf_copy_one(dst, dstv->off, src, srcv->off, len, flags);
290 if (res < 0) {
291 if (!copied)
292 return res;
293 break;
295 copied += res;
297 if (!fuse_bufvec_advance(srcv, res) ||
298 !fuse_bufvec_advance(dstv, res))
299 break;
301 if (res < len)
302 break;
305 return copied;