-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mapper: add a file mapper that freads file chunks
- Loading branch information
Showing
6 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/****************************************************************************** | ||
* * | ||
* Copyright (c) 2024, Enno Boland <[email protected]> * | ||
* * | ||
* Redistribution and use in source and binary forms, with or without * | ||
* modification, are permitted provided that the following conditions are * | ||
* met: * | ||
* * | ||
* * Redistributions of source code must retain the above copyright notice, * | ||
* this list of conditions and the following disclaimer. * | ||
* * Redistributions in binary form must reproduce the above copyright * | ||
* notice, this list of conditions and the following disclaimer in the * | ||
* documentation and/or other materials provided with the distribution. * | ||
* * | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * | ||
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * | ||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * | ||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * | ||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * | ||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * | ||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * | ||
* * | ||
******************************************************************************/ | ||
|
||
/** | ||
* @author Enno Boland ([email protected]) | ||
* @file file_mapper.c | ||
*/ | ||
|
||
#define _LARGEFILE64_SOURCE | ||
#define _DEFAULT_SOURCE | ||
|
||
#include <sqsh_error.h> | ||
#include <sqsh_mapper.h> | ||
#include <stdlib.h> | ||
|
||
#include <errno.h> | ||
#include <stdio.h> | ||
#include <sys/types.h> | ||
|
||
#if defined(__APPLE__) || defined(__OpenBSD__) | ||
# define off64_t off_t | ||
# define fseeko64 fseeko | ||
#endif | ||
|
||
static int | ||
sqsh_mapper_file_init( | ||
struct SqshMapper *mapper, const void *input, size_t *size) { | ||
(void)size; | ||
int rv = 0; | ||
FILE *file; | ||
off64_t pos; | ||
|
||
file = fopen(input, "r"); | ||
if (file == NULL) { | ||
rv = -errno; | ||
goto out; | ||
} | ||
|
||
pos = fseeko64(file, 0, SEEK_END); | ||
if (pos < 0) { | ||
rv = -errno; | ||
goto out; | ||
} | ||
*size = (size_t)pos; | ||
|
||
sqsh_mapper_set_user_data(mapper, file); | ||
|
||
out: | ||
return rv; | ||
} | ||
static int | ||
sqsh_mapping_file_map( | ||
const struct SqshMapper *mapper, sqsh_index_t offset, size_t size, | ||
uint8_t **data) { | ||
int rv = 0; | ||
FILE *file = sqsh_mapper_user_data(mapper); | ||
|
||
if (size != 0) { | ||
*data = calloc(size, sizeof(uint8_t)); | ||
if (*data == NULL) { | ||
rv = -SQSH_ERROR_MALLOC_FAILED; | ||
goto out; | ||
} | ||
rv = fseeko64(file, (off64_t)offset, SEEK_SET); | ||
if (rv < 0) { | ||
rv = -errno; | ||
goto out; | ||
} | ||
|
||
if (fread(*data, sizeof(uint8_t), size, file) != size) { | ||
rv = -SQSH_ERROR_READ_FAILED; | ||
goto out; | ||
} | ||
} | ||
|
||
out: | ||
if (rv < 0) { | ||
free(*data); | ||
} | ||
return rv; | ||
} | ||
|
||
static int | ||
sqsh_mapper_file_cleanup(struct SqshMapper *mapper) { | ||
FILE *file = sqsh_mapper_user_data(mapper); | ||
fclose(file); | ||
return 0; | ||
} | ||
|
||
static int | ||
sqsh_mapping_file_unmap( | ||
const struct SqshMapper *mapper, uint8_t *data, size_t size) { | ||
(void)mapper; | ||
(void)size; | ||
free(data); | ||
return 0; | ||
} | ||
|
||
static const struct SqshMemoryMapperImpl impl = { | ||
/* 1 MiB */ | ||
.block_size_hint = 1024 * 1024, .init = sqsh_mapper_file_init, | ||
.map = sqsh_mapping_file_map, .unmap = sqsh_mapping_file_unmap, | ||
.cleanup = sqsh_mapper_file_cleanup, | ||
}; | ||
const struct SqshMemoryMapperImpl *const sqsh_mapper_impl_file = &impl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters