2 * Copyright (c) 2016, EMC / Isilon Storage Division
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
15 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/fcntl.h>
38 struct fopencookie_thunk {
40 cookie_io_functions_t foc_io;
43 static int _fopencookie_read(void *, char *, int);
44 static int _fopencookie_write(void *, const char *, int);
45 static fpos_t _fopencookie_seek(void *, fpos_t, int);
46 static int _fopencookie_close(void *);
49 fopencookie(void *cookie, const char *mode, cookie_io_functions_t io_funcs)
51 int (*readfn)(void *, char *, int);
52 int (*writefn)(void *, const char *, int);
53 struct fopencookie_thunk *thunk;
57 if ((flags = __sflags(mode, &oflags)) == 0)
60 thunk = malloc(sizeof(*thunk));
64 thunk->foc_cookie = cookie;
65 thunk->foc_io = io_funcs;
67 readfn = _fopencookie_read;
68 writefn = _fopencookie_write;
71 else if (flags == __SRD)
74 fp = funopen(thunk, readfn, writefn, _fopencookie_seek,
81 if ((oflags & O_APPEND) != 0)
88 _fopencookie_read(void *cookie, char *buf, int size)
90 struct fopencookie_thunk *thunk;
94 /* Reads from a stream with NULL read return EOF. */
95 if (thunk->foc_io.read == NULL)
98 return ((int)thunk->foc_io.read(thunk->foc_cookie, buf, (size_t)size));
102 _fopencookie_write(void *cookie, const char *buf, int size)
104 struct fopencookie_thunk *thunk;
108 /* Writes to a stream with NULL write discard data. */
109 if (thunk->foc_io.write == NULL)
112 return ((int)thunk->foc_io.write(thunk->foc_cookie, buf,
117 _fopencookie_seek(void *cookie, fpos_t offset, int whence)
119 struct fopencookie_thunk *thunk;
129 /* fopencookie(3) only allows these three seek modes. */
137 * If seek is NULL, it is not possible to perform seek operations on
140 if (thunk->foc_io.seek == NULL) {
145 off64 = (off64_t)offset;
146 res = thunk->foc_io.seek(thunk->foc_cookie, &off64, whence);
150 return ((fpos_t)off64);
154 _fopencookie_close(void *cookie)
156 struct fopencookie_thunk *thunk;
161 if (thunk->foc_io.close != NULL)
162 ret = thunk->foc_io.close(thunk->foc_cookie);