| 1 |
/* |
| 2 |
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2020 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 14 янв. 2018 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <lsp-plug.in/fmt/lspc/ChunkAccessor.h> |
| 23 |
#include <lsp-plug.in/common/debug.h> |
| 24 |
|
| 25 |
#include <errno.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <unistd.h> |
| 28 |
|
| 29 |
#if defined(PLATFORM_WINDOWS) |
| 30 |
#include <windows.h> |
| 31 |
|
| 32 |
#define FD_INVALID(fd) ((fd) == INVALID_HANDLE_VALUE) |
| 33 |
#else |
| 34 |
#define FD_INVALID(fd) ((fd) < 0) |
| 35 |
#endif /* PLATFORM_WINDOWS */ |
| 36 |
|
| 37 |
namespace lsp |
| 38 |
{ |
| 39 |
namespace lspc |
| 40 |
{ |
| 41 |
status_t Resource::acquire() |
| 42 |
{ |
| 43 |
if (FD_INVALID(fd)) |
| 44 |
return STATUS_CLOSED; |
| 45 |
++refs; |
| 46 |
return STATUS_OK; |
| 47 |
} |
| 48 |
|
| 49 |
status_t Resource::release() |
| 50 |
{ |
| 51 |
if (FD_INVALID(fd)) |
| 52 |
return STATUS_CLOSED; |
| 53 |
if ((--refs) <= 0) |
| 54 |
{ |
| 55 |
#if defined(PLATFORM_WINDOWS) |
| 56 |
CloseHandle(fd); |
| 57 |
fd = INVALID_HANDLE_VALUE; |
| 58 |
#else |
| 59 |
close(fd); |
| 60 |
fd = -1; |
| 61 |
#endif /* PLATFORM_WINDOWS */ |
| 62 |
} |
| 63 |
|
| 64 |
return STATUS_OK; |
| 65 |
} |
| 66 |
|
| 67 |
status_t Resource::allocate(uint32_t *id) |
| 68 |
{ |
| 69 |
uint32_t cid = chunk_id + 1; |
| 70 |
if (cid == 0) |
| 71 |
return STATUS_OVERFLOW; |
| 72 |
*id = chunk_id = cid; |
| 73 |
return STATUS_OK; |
| 74 |
} |
| 75 |
|
| 76 |
status_t Resource::write(const void *buf, size_t count) |
| 77 |
{ |
| 78 |
if (FD_INVALID(fd)) |
| 79 |
return STATUS_CLOSED; |
| 80 |
|
| 81 |
// Write data at the end of file |
| 82 |
const uint8_t *bptr = static_cast<const uint8_t *>(buf); |
| 83 |
while (count > 0) |
| 84 |
{ |
| 85 |
#if defined(PLATFORM_WINDOWS) |
| 86 |
DWORD written = 0; |
| 87 |
if (!WriteFile(fd, bptr, count, &written, NULL)) |
| 88 |
{ |
| 89 |
DWORD error = GetLastError(); |
| 90 |
if (error != ERROR_IO_PENDING) |
| 91 |
{ |
| 92 |
lsp_trace("Error write: GetLastError()=%d", int(error)); |
| 93 |
return STATUS_IO_ERROR; |
| 94 |
} |
| 95 |
written = 0; |
| 96 |
} |
| 97 |
#else |
| 98 |
errno = 0; |
| 99 |
|
| 100 |
ssize_t written = pwrite(fd, bptr, count, length); |
| 101 |
if (written < ssize_t(count)) |
| 102 |
{ |
| 103 |
int error = errno; |
| 104 |
if (error != 0) |
| 105 |
{ |
| 106 |
lsp_trace("Error write: errno=%d", error); |
| 107 |
return STATUS_IO_ERROR; |
| 108 |
} |
| 109 |
} |
| 110 |
#endif /* PLATFORM_WINDOWS */ |
| 111 |
|
| 112 |
bptr += written; |
| 113 |
length += written; |
| 114 |
count -= written; |
| 115 |
} |
| 116 |
|
| 117 |
return STATUS_OK; |
| 118 |
} |
| 119 |
|
| 120 |
ssize_t Resource::read(wsize_t pos, void *buf, size_t count) |
| 121 |
{ |
| 122 |
if (FD_INVALID(fd)) |
| 123 |
return -STATUS_CLOSED; |
| 124 |
|
| 125 |
// Write data at the end of file |
| 126 |
uint8_t *bptr = static_cast<uint8_t *>(buf); |
| 127 |
ssize_t total = 0; |
| 128 |
|
| 129 |
#if defined(PLATFORM_WINDOWS) |
| 130 |
LARGE_INTEGER set_pos; |
| 131 |
LARGE_INTEGER seek_pos; |
| 132 |
|
| 133 |
set_pos.QuadPart = pos; |
| 134 |
if (!SetFilePointerEx(fd, set_pos, &seek_pos, FILE_BEGIN)) |
| 135 |
return -STATUS_IO_ERROR; |
| 136 |
else if (seek_pos.QuadPart != set_pos.QuadPart) |
| 137 |
return -STATUS_IO_ERROR; |
| 138 |
#endif /* PLATFORM_WINDOWS */ |
| 139 |
|
| 140 |
while (count > 0) |
| 141 |
{ |
| 142 |
#if defined(PLATFORM_WINDOWS) |
| 143 |
DWORD read = 0; |
| 144 |
if (!ReadFile(fd, bptr, count, &read, NULL)) |
| 145 |
{ |
| 146 |
DWORD error = GetLastError(); |
| 147 |
if (error != ERROR_IO_PENDING) |
| 148 |
return -STATUS_IO_ERROR; |
| 149 |
read = 0; |
| 150 |
} |
| 151 |
#else |
| 152 |
ssize_t read = pread(fd, bptr, count, pos); |
| 153 |
#endif /* PLATFORM_WINDOWS */ |
| 154 |
if (read < 0) |
| 155 |
{ |
| 156 |
int error = errno; |
| 157 |
switch (error) |
| 158 |
{ |
| 159 |
case EISDIR: return -STATUS_IS_DIRECTORY; |
| 160 |
case EPERM: return -STATUS_PERMISSION_DENIED; |
| 161 |
case EINTR: continue; |
| 162 |
case EIO: return -STATUS_IO_ERROR; |
| 163 |
case EBADF: return -STATUS_BAD_STATE; |
| 164 |
case EINVAL: return -STATUS_BAD_STATE; |
| 165 |
case EAGAIN: break; |
| 166 |
default: return -STATUS_IO_ERROR; |
| 167 |
} |
| 168 |
|
| 169 |
return total; |
| 170 |
} |
| 171 |
else if (ssize_t(read) < ssize_t(count)) |
| 172 |
return total; |
| 173 |
|
| 174 |
bptr += read; |
| 175 |
count -= read; |
| 176 |
total += read; |
| 177 |
} |
| 178 |
|
| 179 |
return total; |
| 180 |
} |
| 181 |
|
| 182 |
ChunkAccessor::ChunkAccessor(Resource *fd, uint32_t magic) |
| 183 |
{ |
| 184 |
pBuffer = NULL; |
| 185 |
pFile = fd; |
| 186 |
set_error((pFile != NULL) ? pFile->acquire() : STATUS_BAD_STATE); |
| 187 |
nMagic = magic; |
| 188 |
nBufSize = (pFile != NULL) ? pFile->bufsize : 0; |
| 189 |
nBufPos = 0; |
| 190 |
nUID = 0; |
| 191 |
|
| 192 |
if (nBufSize > 0) |
| 193 |
{ |
| 194 |
// Adjust buffer size |
| 195 |
if (nBufSize < MIN_BUF_SIZE) |
| 196 |
nBufSize = MIN_BUF_SIZE; |
| 197 |
|
| 198 |
// Allocate buffer |
| 199 |
pBuffer = static_cast<uint8_t *>(malloc(nBufSize)); |
| 200 |
if (pBuffer == NULL) |
| 201 |
{ |
| 202 |
set_error(STATUS_NO_MEM); |
| 203 |
return; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
set_error(STATUS_OK); |
| 208 |
} |
| 209 |
|
| 210 |
ChunkAccessor::~ChunkAccessor() |
| 211 |
{ |
| 212 |
do_close(); |
| 213 |
} |
| 214 |
|
| 215 |
status_t ChunkAccessor::do_close() |
| 216 |
{ |
| 217 |
if (pBuffer != NULL) |
| 218 |
{ |
| 219 |
free(pBuffer); |
| 220 |
pBuffer = NULL; |
| 221 |
} |
| 222 |
if (pFile == NULL) |
| 223 |
return set_error(STATUS_CLOSED); |
| 224 |
set_error(pFile->release()); |
| 225 |
if (pFile->refs <= 0) |
| 226 |
delete pFile; |
| 227 |
pFile = NULL; |
| 228 |
return last_error(); |
| 229 |
} |
| 230 |
|
| 231 |
status_t ChunkAccessor::close() |
| 232 |
{ |
| 233 |
return do_close(); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
} /* namespace lsp */ |