Debian Codegraph @

modules/lsp-runtime-lib/src/main/fmt/lspc/File.cpp

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 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/common/endian.h>
23 #include <lsp-plug.in/stdlib/string.h>
24 #include <lsp-plug.in/fmt/lspc/lspc.h>
25 #include <lsp-plug.in/fmt/lspc/File.h>
26 #include <lsp-plug.in/lltl/darray.h>
27
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31
32 #ifdef PLATFORM_WINDOWS
33 #include <windows.h>
34 #endif /* PLATFORM_WINDOWS */
35
36 namespace lsp
37 {
38 namespace lspc
39 {
40 File::File()
41 {
42 pFile = NULL;
43 bWrite = false;
44 nHdrSize = 0;
45 }
46
47 File::~File()
48 {
49 close();
50 }
51
52 Resource *File::create_resource(fhandle_t fd)
53 {
54 Resource *res = new Resource;
55 if (res == NULL)
56 return NULL;
57
58 res->fd = fd;
59 res->refs = 1;
60 res->bufsize = 0x10000;
61 res->chunk_id = 0;
62 res->length = 0;
63
64 return res;
65 }
66
67 status_t File::open(const char *path)
68 {
69 if (path == NULL)
70 return STATUS_BAD_ARGUMENTS;
71
72 LSPString fpath;
73 if (!fpath.set_utf8(path))
74 return STATUS_NO_MEM;
75 return open(&fpath);
76 }
77
78 status_t File::open(const io::Path *path)
79 {
80 if (path == NULL)
81 return STATUS_BAD_ARGUMENTS;
82
83 LSPString fpath;
84 status_t res = path->get(&fpath);
85 if (res == STATUS_OK)
86 res = open(&fpath);
87 return res;
88 }
89
90 status_t File::open(const LSPString *path)
91 {
92 if (path == NULL)
93 return STATUS_BAD_ARGUMENTS;
94 if (pFile != NULL)
95 return STATUS_BAD_STATE;
96
97 root_header_t hdr;
98
99 #if defined(PLATFORM_WINDOWS)
100 fhandle_t fd = CreateFileW(
101 path->get_utf16(), GENERIC_READ,
102 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
103 NULL, OPEN_EXISTING,
104 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
105 NULL
106 );
107 if (fd == INVALID_HANDLE_VALUE)
108 return STATUS_IO_ERROR;
109
110 Resource *res = create_resource(fd);
111 if (res == NULL)
112 {
113 CloseHandle(fd);
114 return STATUS_NO_MEM;
115 }
116 #else
117 fhandle_t fd = ::open(path->get_utf8(), O_RDONLY);
118 if (fd < 0)
119 return STATUS_IO_ERROR;
120
121 Resource *res = create_resource(fd);
122 if (res == NULL)
123 {
124 ::close(fd);
125 return STATUS_NO_MEM;
126 }
127 #endif /* PLATFORM_WINDOWS */
128
129 ssize_t bytes = res->read(0, &hdr, sizeof(root_header_t));
130
131 if ((bytes < ssize_t(sizeof(root_header_t))) ||
132 (BE_TO_CPU(hdr.size) < sizeof(root_header_t)) ||
133 (BE_TO_CPU(hdr.magic) != LSPC_ROOT_MAGIC) ||
134 (BE_TO_CPU(hdr.version) != 1))
135 {
136 res->release();
137 delete res;
138 return STATUS_BAD_FORMAT;
139 }
140
141 nHdrSize = BE_TO_CPU(hdr.size);
142 pFile = res;
143 bWrite = false;
144
145 return STATUS_OK;
146 }
147
148 status_t File::create(const char *path)
149 {
150 if (path == NULL)
151 return STATUS_BAD_ARGUMENTS;
152
153 LSPString fpath;
154 if (!fpath.set_utf8(path))
155 return STATUS_NO_MEM;
156 return create(&fpath);
157 }
158
159 status_t File::create(const io::Path *path)
160 {
161 if (path == NULL)
162 return STATUS_BAD_ARGUMENTS;
163
164 LSPString fpath;
165 status_t res = path->get(&fpath);
166 if (res == STATUS_OK)
167 res = create(&fpath);
168 return res;
169 }
170
171 status_t File::create(const LSPString *path)
172 {
173 if (path == NULL)
174 return STATUS_BAD_ARGUMENTS;
175 if (pFile != NULL)
176 return STATUS_BAD_STATE;
177
178 #if defined(PLATFORM_WINDOWS)
179 fhandle_t fd = CreateFileW(
180 path->get_utf16(), GENERIC_READ | GENERIC_WRITE,
181 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
182 NULL, CREATE_ALWAYS,
183 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
184 NULL
185 );
186 if (fd == INVALID_HANDLE_VALUE)
187 return STATUS_IO_ERROR;
188
189 Resource *res = create_resource(fd);
190 if (res == NULL)
191 {
192 CloseHandle(fd);
193 return STATUS_NO_MEM;
194 }
195 #else
196 fhandle_t fd = ::open(path->get_utf8(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
197 if (fd < 0)
198 return STATUS_IO_ERROR;
199
200 Resource *res = create_resource(fd);
201 if (res == NULL)
202 {
203 ::close(fd);
204 return STATUS_NO_MEM;
205 }
206 #endif /* PLATFORM_WINDOWS */
207
208 root_header_t hdr;
209 ::memset(&hdr, 0, sizeof(root_header_t));
210 hdr.magic = LSPC_ROOT_MAGIC;
211 hdr.version = 1;
212 hdr.size = sizeof(hdr);
213
214 hdr.magic = CPU_TO_BE(hdr.magic);
215 hdr.version = CPU_TO_BE(hdr.version);
216 hdr.size = CPU_TO_BE(hdr.size);
217
218 status_t io_res = res->write(&hdr, sizeof(root_header_t));
219 if (io_res != STATUS_OK)
220 {
221 res->release();
222 delete res;
223 return io_res;
224 }
225
226 res->length = sizeof(hdr);
227 pFile = res;
228 bWrite = true;
229
230 return STATUS_OK;
231 }
232
233 status_t File::close()
234 {
235 if (pFile == NULL)
236 return STATUS_BAD_STATE;
237 status_t res = pFile->release();
238 if (pFile->refs <= 0)
239 delete pFile;
240 pFile = NULL;
241 return res;
242 }
243
244 ChunkWriter *File::write_chunk(uint32_t magic)
245 {
246 if ((pFile == NULL) || (!bWrite))
247 return NULL;
248
249 ChunkWriter *wr = new ChunkWriter(pFile, magic);
250 return wr;
251 }
252
253 ChunkReader *File::read_chunk(uint32_t uid)
254 {
255 if ((pFile == NULL) || (bWrite))
256 return NULL;
257
258 // Find the initial position of the chunk in file
259 chunk_header_t hdr;
260 wsize_t pos = nHdrSize;
261 while (true)
262 {
263 ssize_t res = pFile->read(pos, &hdr, sizeof(chunk_header_t));
264 if (res != sizeof(chunk_header_t))
265 return NULL;
266 pos += sizeof(chunk_header_t);
267
268 hdr.magic = BE_TO_CPU(hdr.magic);
269 hdr.uid = BE_TO_CPU(hdr.uid);
270 hdr.flags = BE_TO_CPU(hdr.flags);
271 hdr.size = BE_TO_CPU(hdr.size);
272
273 // lsp_trace("chunk header uid=%x, magic=%x, flags=%x, search_uid=%x, size=%llx",
274 // int(hdr.uid), int(hdr.magic), int(hdr.flags), int(uid), (long long)(hdr.size));
275 if (hdr.uid == uid)
276 break;
277 pos += hdr.size;
278 }
279
280 // Create reader
281 ChunkReader *rd = new ChunkReader(pFile, hdr.magic, uid);
282 if (rd == NULL)
283 return NULL;
284 rd->nFileOff = pos;
285 rd->nUnread = hdr.size;
286 return rd;
287 }
288
289 ChunkReader *File::read_chunk(uint32_t uid, uint32_t magic)
290 {
291 if ((pFile == NULL) || (bWrite))
292 return NULL;
293
294 // Find the initial position of the chunk in file
295 chunk_header_t hdr;
296 wsize_t pos = nHdrSize;
297 while (true)
298 {
299 ssize_t res = pFile->read(pos, &hdr, sizeof(chunk_header_t));
300 if (res != sizeof(chunk_header_t))
301 return NULL;
302 pos += sizeof(chunk_header_t);
303
304 hdr.magic = BE_TO_CPU(hdr.magic);
305 hdr.uid = BE_TO_CPU(hdr.uid);
306 hdr.flags = BE_TO_CPU(hdr.flags);
307 hdr.size = BE_TO_CPU(hdr.size);
308
309 if ((hdr.uid == uid) && (hdr.magic == magic))
310 break;
311 pos += hdr.size;
312 }
313
314 // Create reader
315 ChunkReader *rd = new ChunkReader(pFile, hdr.magic, uid);
316 if (rd == NULL)
317 return NULL;
318 rd->nFileOff = pos;
319 rd->nUnread = hdr.size;
320 return rd;
321 }
322
323 ChunkReader *File::find_chunk(uint32_t magic, uint32_t *id, uint32_t start_id)
324 {
325 if ((pFile == NULL) || (bWrite))
326 return NULL;
327
328 // Find the initial position of the chunk in file
329 chunk_header_t hdr;
330 wsize_t pos = nHdrSize;
331 while (true)
332 {
333 ssize_t res = pFile->read(pos, &hdr, sizeof(chunk_header_t));
334 if (res != sizeof(chunk_header_t))
335 return NULL;
336 pos += sizeof(chunk_header_t);
337
338 hdr.magic = BE_TO_CPU(hdr.magic);
339 hdr.uid = BE_TO_CPU(hdr.uid);
340 hdr.flags = BE_TO_CPU(hdr.flags);
341 hdr.size = BE_TO_CPU(hdr.size);
342
343 if ((hdr.uid >= start_id) && (hdr.magic == magic))
344 break;
345 pos += hdr.size;
346 }
347
348 // Create reader
349 ChunkReader *rd = new ChunkReader(pFile, hdr.magic, hdr.uid);
350 if (rd == NULL)
351 return NULL;
352 if (id != NULL)
353 *id = rd->unique_id();
354 rd->nFileOff = pos;
355 rd->nUnread = hdr.size;
356 return rd;
357 }
358
359 ssize_t File::enumerate_chunks(uint32_t magic, chunk_id_t **list)
360 {
361 lltl::darray<chunk_id_t> chunk_ids;
362 if ((pFile == NULL) || (bWrite))
363 return -STATUS_BAD_STATE;
364
365 // Find the initial position of the chunk in file
366 status_t res;
367 chunk_header_t hdr;
368 wsize_t pos = nHdrSize;
369 while (true)
370 {
371 const wssize_t num_read = pFile->read(pos, &hdr, sizeof(chunk_header_t));
372 if (num_read != sizeof(chunk_header_t))
373 {
374 res = status_t(num_read);
375 break;
376 }
377
378 hdr.magic = BE_TO_CPU(hdr.magic);
379 hdr.uid = BE_TO_CPU(hdr.uid);
380 hdr.flags = BE_TO_CPU(hdr.flags);
381 hdr.size = BE_TO_CPU(hdr.size);
382
383 // Found matching chunk?
384 if (hdr.magic == magic)
385 {
386 // Check that chunk is not already in the list
387 bool found = false;
388 for (size_t i=0, n=chunk_ids.size(); i<n; ++i)
389 {
390 chunk_id_t *id = chunk_ids.uget(i);
391 if (*id == hdr.uid)
392 {
393 found = true;
394 break;
395 }
396 }
397
398 // Do we need to add chunk to the list?
399 if (!found)
400 {
401 if (!chunk_ids.add(hdr.uid))
402 return -STATUS_NO_MEM;
403 }
404 }
405
406 // Update position
407 pos += hdr.size + sizeof(chunk_header_t);
408 }
409
410 // Did we reach the end of file?
411 if ((res != 0) && (res != -STATUS_EOF))
412 return -STATUS_CORRUPTED;
413
414 // Return success result
415 ssize_t count = chunk_ids.size();
416 if (list != NULL)
417 *list = chunk_ids.release();
418 return count;
419 }
420
421 ssize_t File::enumerate_chunks(chunk_info_t **list)
422 {
423 lltl::darray<chunk_info_t> chunk_infos;
424 if ((pFile == NULL) || (bWrite))
425 return -STATUS_BAD_STATE;
426
427 // Find the initial position of the chunk in file
428 status_t res;
429 chunk_header_t hdr;
430 wsize_t pos = nHdrSize;
431 while (true)
432 {
433 const wssize_t num_read = pFile->read(pos, &hdr, sizeof(chunk_header_t));
434 if (num_read != sizeof(chunk_header_t))
435 {
436 res = status_t(num_read);
437 break;
438 }
439
440 hdr.magic = BE_TO_CPU(hdr.magic);
441 hdr.uid = BE_TO_CPU(hdr.uid);
442 hdr.flags = BE_TO_CPU(hdr.flags);
443 hdr.size = BE_TO_CPU(hdr.size);
444
445 // Check that chunk is not already in the list
446 chunk_info_t *chunk = NULL;
447 for (size_t i=0, n=chunk_infos.size(); i<n; ++i)
448 {
449 chunk_info_t *info = chunk_infos.uget(i);
450 if (info->chunk_id == hdr.uid)
451 {
452 chunk = info;
453 break;
454 }
455 }
456
457 // Do we need to add chunk to the list?
458 if (chunk == NULL)
459 {
460 if ((chunk = chunk_infos.add()) == NULL)
461 return -STATUS_NO_MEM;
462
463 chunk->magic = hdr.magic;
464 chunk->chunk_id = hdr.uid;
465 chunk->position = pos;
466 chunk->size = hdr.size;
467 }
468 else
469 chunk->size += hdr.size;
470
471 // Update position
472 pos += hdr.size + sizeof(chunk_header_t);
473 }
474
475 // Did we reach the end of file?
476 if ((res != 0) && (res != -STATUS_EOF))
477 return -STATUS_CORRUPTED;
478
479 // Return success result
480 ssize_t count = chunk_infos.size();
481 if (list != NULL)
482 *list = chunk_infos.release();
483 return count;
484 }
485
486 } /* namespace lspc */
487 } /* namespace lsp */