|
| 1 | +// |
| 2 | +// CBLBlob+FILE.cc |
| 3 | +// |
| 4 | +// Copyright © 2021 Couchbase. All rights reserved. |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | + |
| 19 | +#include "CBLBlob+FILE.h" |
| 20 | +#include "CBLLog.h" |
| 21 | + |
| 22 | +using namespace std; |
| 23 | +using namespace fleece; |
| 24 | + |
| 25 | + |
| 26 | +/* |
| 27 | + There are two nonstandard APIs in <stdio.h> for opening a `FILE*` with custom read/write/seek |
| 28 | + behavior: |
| 29 | + - Apple platforms and BSD have `funopen`. |
| 30 | + - GNU's libc (Linux) has a similar API called `fopencookie`. |
| 31 | + - Sadly, Windows does not support this :( |
| 32 | +
|
| 33 | + The two functions, and the callbacks they use, have slightly different parameter types and |
| 34 | + semantics. Since `fopencookie`s callbacks have more sensible types, we've implemented those |
| 35 | + and then added some `funopen`-compatible wrapper functions. |
| 36 | +
|
| 37 | + `funopen` callback error reporting is consistent: |
| 38 | + > All user I/O functions can report an error by returning -1. Additionally, |
| 39 | + > all of the functions should set the external variable errno appropriately |
| 40 | + > if an error occurs. |
| 41 | +
|
| 42 | + `fopencookie`s man page doesn't mention setting `errno`, but presumably it's allowed. |
| 43 | + The return values are somewhat inconsistent in that the write callback is supposed to return |
| 44 | + 0, not -1, on error. (Even though the man page itself shows an example that returns -1...) |
| 45 | +
|
| 46 | + References: |
| 47 | + <https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/funopen.3.html> |
| 48 | + <https://www.freebsd.org/cgi/man.cgi?query=funopen> |
| 49 | + <https://man7.org/linux/man-pages/man3/fopencookie.3.html> |
| 50 | + */ |
| 51 | + |
| 52 | + |
| 53 | +#ifndef _MSC_VER |
| 54 | + |
| 55 | +#ifdef __APPLE__ // ...or BSD... |
| 56 | + #define USE_FUNOPEN |
| 57 | +#endif |
| 58 | + |
| 59 | + |
| 60 | +static inline int withErrno(int err) { |
| 61 | + errno = err; |
| 62 | + return -1; |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +#pragma mark - STDIO READ CALLBACKS: |
| 67 | + |
| 68 | + |
| 69 | +static ssize_t readFn_cookie(void *cookie, char *dst, size_t len) noexcept { |
| 70 | + int result = CBLBlobReader_Read((CBLBlobReadStream*)cookie, dst, len, nullptr); |
| 71 | + if (result < 0) |
| 72 | + return withErrno(EIO); |
| 73 | + return result; |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +_cbl_unused |
| 78 | +static int readFn_fun(void *cookie, char *dst, int len) { |
| 79 | + if (len < 0) |
| 80 | + return withErrno(EINVAL); |
| 81 | + return int(readFn_cookie(cookie, dst, len)); |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +static int seekFn_cookie(void *cookie, int64_t *offset, int mode) noexcept { |
| 86 | + CBLSeekBase base; |
| 87 | + switch (mode) { |
| 88 | + case SEEK_SET: base = kCBLSeekModeFromStart; break; |
| 89 | + case SEEK_CUR: base = kCBLSeekModeRelative; break; |
| 90 | + case SEEK_END: base = kCBLSeekModeFromEnd; break; |
| 91 | + default: return withErrno(EINVAL); |
| 92 | + } |
| 93 | + int64_t newOffset = CBLBlobReader_Seek((CBLBlobReadStream*)cookie, *offset, base, nullptr); |
| 94 | + if (newOffset < 0) |
| 95 | + return withErrno(EINVAL); |
| 96 | + *offset = newOffset; |
| 97 | + return 0; |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +_cbl_unused |
| 102 | +static fpos_t seekFn_fun(void *cookie, fpos_t pos, int mode) noexcept { |
| 103 | + return (seekFn_cookie(cookie, &pos, mode) == 0) ? pos : -1; |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | +static int closeReaderFn(void *cookie) noexcept { |
| 108 | + CBLBlobReader_Close((CBLBlobReadStream*)cookie); |
| 109 | + return 0; |
| 110 | +} |
| 111 | + |
| 112 | + |
| 113 | +#pragma mark - STDIO WRITE CALLBACKS: |
| 114 | + |
| 115 | + |
| 116 | +static ssize_t writeFn_cookie(void *cookie, const char *src, size_t len) noexcept { |
| 117 | + // "the write function should return the number of bytes copied from buf, or 0 on error. |
| 118 | + // (The function must not return a negative value.)" --Linux man page |
| 119 | + if (len == 0) { |
| 120 | + errno = EINVAL; |
| 121 | + return 0; |
| 122 | + } |
| 123 | + if (!CBLBlobWriter_Write((CBLBlobWriteStream*)cookie, src, len, nullptr)) { |
| 124 | + errno = EIO; |
| 125 | + return 0; |
| 126 | + } |
| 127 | + return len; |
| 128 | +} |
| 129 | + |
| 130 | + |
| 131 | +_cbl_unused |
| 132 | +static int writeFn_fun(void *cookie, const char *src, int len) noexcept { |
| 133 | + if (len < 0) |
| 134 | + return withErrno(EINVAL); |
| 135 | + if (len == 0) |
| 136 | + return 0; |
| 137 | + auto bytesWritten = writeFn_cookie(cookie, src, len); |
| 138 | + return bytesWritten > 0 ? int(bytesWritten) : -1; |
| 139 | +} |
| 140 | + |
| 141 | + |
| 142 | +// Coordinator between `closeWriterFn` and `CBLBlobWriter_CreateFILE`. |
| 143 | +// (It's thread-local to avoid race conditions if multiple threads create blobs at once.) |
| 144 | +__thread static CBLBlobWriteStream** sPutStreamHereOnClose = nullptr; |
| 145 | + |
| 146 | + |
| 147 | +static int closeWriterFn(void *cookie) noexcept { |
| 148 | + if (sPutStreamHereOnClose) { |
| 149 | + // Instead of actually closing, copy the pointer to the blob write stream where the |
| 150 | + // `CBLBlob_CreateWithFILE` function can retrieve it. |
| 151 | + *sPutStreamHereOnClose = (CBLBlobWriteStream*)cookie; |
| 152 | + } else { |
| 153 | + // If our secret pointer is NULL, then `CBLBlobWriter_CreateFILE` isn't being called, |
| 154 | + // so the app must just be calling `fclose` itself to cancel creating a blob. |
| 155 | + CBLBlobWriter_Close((CBLBlobWriteStream*)cookie); |
| 156 | + } |
| 157 | + return 0; |
| 158 | +} |
| 159 | + |
| 160 | + |
| 161 | +static CBLBlobWriteStream* closeFILEAndRecoverStream(FILE *f) { |
| 162 | + // There's no stdio API to recover the "cookie" value from a custom `FILE*`, so how are we |
| 163 | + // going to get the `CBLBlobWriteStream*` back? |
| 164 | + // Kludgy solution: the "close" callback (`closeWriterFn`) stores the cookie into a variable |
| 165 | + // pointed to by the static pointer `sPutStreamHereOnClose`, so after calling `fclose` |
| 166 | + // -- which we need to do anyway to flush the buffer -- our variable will be set. |
| 167 | + // If it wasn't, it means the caller passed in a `FILE*` we didn't open, which is an error. |
| 168 | + CBLBlobWriteStream *stream = nullptr; |
| 169 | + sPutStreamHereOnClose = &stream; |
| 170 | + fclose(f); |
| 171 | + sPutStreamHereOnClose = nullptr; |
| 172 | + return stream; |
| 173 | +} |
| 174 | + |
| 175 | + |
| 176 | +#pragma mark - API FUNCTIONS: |
| 177 | + |
| 178 | + |
| 179 | +FILE* CBLBlob_OpenAsFILE(CBLBlob* blob, CBLError* outError) noexcept { |
| 180 | + auto stream = CBLBlob_OpenContentStream(blob, outError); |
| 181 | + if (!stream) |
| 182 | + return nullptr; |
| 183 | +#ifdef USE_FUNOPEN |
| 184 | + return funopen(stream, &readFn_fun, nullptr, &seekFn_fun, &closeReaderFn); |
| 185 | +#else |
| 186 | + return fopencookie(stream, "r", {&readfn_cookie, nullptr, &seekfn_cookie, &closeReaderFn}); |
| 187 | +#endif |
| 188 | +} |
| 189 | + |
| 190 | + |
| 191 | +FILE* _cbl_nullable CBLBlobWriter_CreateFILE(CBLDatabase* db, CBLError* outError) noexcept { |
| 192 | + CBLBlobWriteStream *stream = CBLBlobWriter_Create(db, outError); |
| 193 | + if (!stream) |
| 194 | + return nullptr; |
| 195 | +#ifdef USE_FUNOPEN |
| 196 | + FILE *f = funopen(stream, nullptr, writeFn_fun, nullptr, closeWriterFn); |
| 197 | +#else |
| 198 | + FILE *f = fopencookie(stream, "w", {nullptr, &writefn_cookie, nullptr, closeWriterFn}); |
| 199 | +#endif |
| 200 | + if (!f) |
| 201 | + CBLBlobWriter_Close(stream); |
| 202 | + return f; |
| 203 | +} |
| 204 | + |
| 205 | + |
| 206 | +CBLBlob* CBLBlob_CreateWithFILE(FLString contentType, FILE* file) noexcept { |
| 207 | + CBLBlobWriteStream *stream = closeFILEAndRecoverStream(file); |
| 208 | + if (!stream) { |
| 209 | + CBL_LogMessage(kCBLLogDomainDatabase, kCBLLogError, |
| 210 | + FLSTR("CBLBlob_CreateWithFILE was called with a FILE* not opened by" |
| 211 | + " CBLBlobWriter_CreateFILE")); |
| 212 | + return nullptr; |
| 213 | + } |
| 214 | + return CBLBlob_CreateWithStream(contentType, stream); |
| 215 | +} |
| 216 | + |
| 217 | +#endif // _MSC_VER |
0 commit comments