| 1 |
/********************************************************************* |
| 2 |
Blosc - Blocked Shuffling and Compression Library |
| 3 |
|
| 4 |
Copyright (c) 2021 Blosc Development Team <[email protected]> |
| 5 |
https://blosc.org |
| 6 |
License: BSD 3-Clause (see LICENSE.txt) |
| 7 |
|
| 8 |
See LICENSE.txt for details about copyright and rights to use. |
| 9 |
**********************************************************************/ |
| 10 |
|
| 11 |
#include "frame.h" |
| 12 |
#include "blosc2.h" |
| 13 |
|
| 14 |
#include <stdio.h> |
| 15 |
#include <stdint.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
|
| 20 |
/* If C11 is supported, use it's built-in aligned allocation. */ |
| 21 |
#if __STDC_VERSION__ >= 201112L |
| 22 |
#include <stdalign.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
|
| 26 |
/* Open sparse frame index chunk */ |
| 27 |
void* sframe_open_index(const char* urlpath, const char* mode, const blosc2_io *io) { |
| 28 |
void* fp = NULL; |
| 29 |
char* index_path = malloc(strlen(urlpath) + strlen("/chunks.b2frame") + 1); |
| 30 |
if (index_path) { |
| 31 |
sprintf(index_path, "%s/chunks.b2frame", urlpath); |
| 32 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(io->id); |
| 33 |
if (io_cb == NULL) { |
| 34 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 35 |
return NULL; |
| 36 |
} |
| 37 |
fp = io_cb->open(index_path, mode, io->params); |
| 38 |
if (fp == NULL) |
| 39 |
BLOSC_TRACE_ERROR("Error creating index path in: %s", index_path); |
| 40 |
free(index_path); |
| 41 |
} |
| 42 |
return fp; |
| 43 |
} |
| 44 |
|
| 45 |
/* Open directory/nchunk.chunk with 8 zeros of padding */ |
| 46 |
void* sframe_open_chunk(const char* urlpath, int64_t nchunk, const char* mode, const blosc2_io *io) { |
| 47 |
void* fp = NULL; |
| 48 |
char* chunk_path = malloc(strlen(urlpath) + 1 + 8 + strlen(".chunk") + 1); |
| 49 |
if (chunk_path) { |
| 50 |
sprintf(chunk_path, "%s/%08X.chunk", urlpath, (unsigned int)nchunk); |
| 51 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(io->id); |
| 52 |
if (io_cb == NULL) { |
| 53 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 54 |
return NULL; |
| 55 |
} |
| 56 |
fp = io_cb->open(chunk_path, mode, io->params); |
| 57 |
if (fp == NULL) |
| 58 |
BLOSC_TRACE_ERROR("Error opening chunk path in: %s", chunk_path); |
| 59 |
free(chunk_path); |
| 60 |
} |
| 61 |
return fp; |
| 62 |
} |
| 63 |
|
| 64 |
/* Append an existing chunk into a sparse frame. */ |
| 65 |
void* sframe_create_chunk(blosc2_frame_s* frame, uint8_t* chunk, int64_t nchunk, int64_t cbytes) { |
| 66 |
void* fpc = sframe_open_chunk(frame->urlpath, nchunk, "wb", frame->schunk->storage->io); |
| 67 |
if (fpc == NULL) { |
| 68 |
BLOSC_TRACE_ERROR("Cannot open the chunkfile."); |
| 69 |
return NULL; |
| 70 |
} |
| 71 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(frame->schunk->storage->io->id); |
| 72 |
if (io_cb == NULL) { |
| 73 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 74 |
return NULL; |
| 75 |
} |
| 76 |
int64_t io_pos = 0; |
| 77 |
int64_t wbytes = io_cb->write(chunk, 1, cbytes, io_pos, fpc); |
| 78 |
io_cb->close(fpc); |
| 79 |
if (wbytes != cbytes) { |
| 80 |
BLOSC_TRACE_ERROR("Cannot write the full chunk."); |
| 81 |
return NULL; |
| 82 |
} |
| 83 |
|
| 84 |
return frame; |
| 85 |
} |
| 86 |
|
| 87 |
/* Append an existing chunk into a sparse frame. */ |
| 88 |
int sframe_delete_chunk(const char *urlpath, int64_t nchunk) { |
| 89 |
char* chunk_path = malloc(strlen(urlpath) + 1 + 8 + strlen(".chunk") + 1); |
| 90 |
if (chunk_path) { |
| 91 |
sprintf(chunk_path, "%s/%08X.chunk", urlpath, (unsigned int)nchunk); |
| 92 |
int rc = remove(chunk_path); |
| 93 |
free(chunk_path); |
| 94 |
return rc; |
| 95 |
} |
| 96 |
return BLOSC2_ERROR_FILE_REMOVE; |
| 97 |
} |
| 98 |
|
| 99 |
/* Get chunk from sparse frame. */ |
| 100 |
int32_t sframe_get_chunk(blosc2_frame_s* frame, int64_t nchunk, uint8_t** chunk, bool* needs_free){ |
| 101 |
void *fpc = sframe_open_chunk(frame->urlpath, nchunk, "rb", frame->schunk->storage->io); |
| 102 |
if(fpc == NULL){ |
| 103 |
BLOSC_TRACE_ERROR("Cannot open the chunkfile."); |
| 104 |
return BLOSC2_ERROR_FILE_OPEN; |
| 105 |
} |
| 106 |
|
| 107 |
blosc2_io_cb *io_cb = blosc2_get_io_cb(frame->schunk->storage->io->id); |
| 108 |
if (io_cb == NULL) { |
| 109 |
BLOSC_TRACE_ERROR("Error getting the input/output API"); |
| 110 |
return BLOSC2_ERROR_PLUGIN_IO; |
| 111 |
} |
| 112 |
|
| 113 |
int64_t chunk_cbytes = io_cb->size(fpc); |
| 114 |
|
| 115 |
if (io_cb->is_allocation_necessary) { |
| 116 |
*chunk = malloc((size_t)chunk_cbytes); |
| 117 |
*needs_free = true; |
| 118 |
} |
| 119 |
else { |
| 120 |
*needs_free = false; |
| 121 |
} |
| 122 |
|
| 123 |
int64_t io_pos = 0; |
| 124 |
int64_t rbytes = io_cb->read((void**)chunk, 1, chunk_cbytes, io_pos, fpc); |
| 125 |
io_cb->close(fpc); |
| 126 |
if (rbytes != chunk_cbytes) { |
| 127 |
BLOSC_TRACE_ERROR("Cannot read the chunk out of the chunkfile."); |
| 128 |
return BLOSC2_ERROR_FILE_READ; |
| 129 |
} |
| 130 |
|
| 131 |
return (int32_t)chunk_cbytes; |
| 132 |
} |