Skip to content

Commit

Permalink
mapper: add a file mapper that freads file chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
Gottox committed Jun 8, 2024
1 parent a41dec9 commit 32fa11d
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/sqsh_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ enum SqshError {
SQSH_ERROR_INODE_PARENT_MISMATCH,
SQSH_ERROR_INODE_PARENT_UNSET,
SQSH_ERROR_NOT_A_SYMLINK,
SQSH_ERROR_READ_FAILED,
};

/**
Expand Down
9 changes: 9 additions & 0 deletions include/sqsh_mapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ extern const struct SqshMemoryMapperImpl *const sqsh_mapper_impl_mmap;
*/
extern const struct SqshMemoryMapperImpl *const sqsh_mapper_impl_static;

/***************************************
* mapper/static_file.c
*/

/**
* @brief a mapper that uses a static buffer.
*/
extern const struct SqshMemoryMapperImpl *const sqsh_mapper_impl_file;

#ifdef __cplusplus
}
#endif
Expand Down
130 changes: 130 additions & 0 deletions libsqsh/src/mapper/file_mapper.c
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(

Check warning on line 51 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L51

Added line #L51 was not covered by tests
struct SqshMapper *mapper, const void *input, size_t *size) {
(void)size;
int rv = 0;

Check warning on line 54 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L54

Added line #L54 was not covered by tests
FILE *file;
off64_t pos;

file = fopen(input, "r");

Check warning on line 58 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L58

Added line #L58 was not covered by tests
if (file == NULL) {
rv = -errno;
goto out;

Check warning on line 61 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L60-L61

Added lines #L60 - L61 were not covered by tests
}

pos = fseeko64(file, 0, SEEK_END);

Check warning on line 64 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L64

Added line #L64 was not covered by tests
if (pos < 0) {
rv = -errno;
goto out;

Check warning on line 67 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L66-L67

Added lines #L66 - L67 were not covered by tests
}
*size = (size_t)pos;

Check warning on line 69 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L69

Added line #L69 was not covered by tests

sqsh_mapper_set_user_data(mapper, file);

Check warning on line 71 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L71

Added line #L71 was not covered by tests

out:
return rv;

Check warning on line 74 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L73-L74

Added lines #L73 - L74 were not covered by tests
}
static int
sqsh_mapping_file_map(

Check warning on line 77 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L77

Added line #L77 was not covered by tests
const struct SqshMapper *mapper, sqsh_index_t offset, size_t size,
uint8_t **data) {
int rv = 0;
FILE *file = sqsh_mapper_user_data(mapper);

Check warning on line 81 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L80-L81

Added lines #L80 - L81 were not covered by tests

if (size != 0) {
*data = calloc(size, sizeof(uint8_t));

Check warning on line 84 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L84

Added line #L84 was not covered by tests
if (*data == NULL) {
rv = -SQSH_ERROR_MALLOC_FAILED;
goto out;

Check warning on line 87 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L86-L87

Added lines #L86 - L87 were not covered by tests
}
rv = fseeko64(file, (off64_t)offset, SEEK_SET);

Check warning on line 89 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L89

Added line #L89 was not covered by tests
if (rv < 0) {
rv = -errno;
goto out;

Check warning on line 92 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L91-L92

Added lines #L91 - L92 were not covered by tests
}

if (fread(*data, sizeof(uint8_t), size, file) != size) {
rv = -SQSH_ERROR_READ_FAILED;
goto out;

Check warning on line 97 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L96-L97

Added lines #L96 - L97 were not covered by tests
}
}

out:

Check warning on line 101 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L101

Added line #L101 was not covered by tests
if (rv < 0) {
free(*data);

Check warning on line 103 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L103

Added line #L103 was not covered by tests
}
return rv;

Check warning on line 105 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L105

Added line #L105 was not covered by tests
}

static int
sqsh_mapper_file_cleanup(struct SqshMapper *mapper) {
FILE *file = sqsh_mapper_user_data(mapper);
fclose(file);
return 0;

Check warning on line 112 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L109-L112

Added lines #L109 - L112 were not covered by tests
}

static int
sqsh_mapping_file_unmap(

Check warning on line 116 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L116

Added line #L116 was not covered by tests
const struct SqshMapper *mapper, uint8_t *data, size_t size) {
(void)mapper;
(void)size;
free(data);
return 0;

Check warning on line 121 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L120-L121

Added lines #L120 - L121 were not covered by tests
}

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;
1 change: 1 addition & 0 deletions libsqsh/src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ libsqsh_sources = files(
'file/inode_null.c',
'file/inode_symlink.c',
'mapper/curl_mapper.c',
'mapper/file_mapper.c',
'mapper/map_iterator.c',
'mapper/map_manager.c',
'mapper/map_reader.c',
Expand Down
2 changes: 2 additions & 0 deletions libsqsh/src/utils/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ sqsh_error_str(int error_code) {
return "Not a symlink";
case SQSH_ERROR_INODE_PARENT_UNSET:
return "Inode parent unset";
case SQSH_ERROR_READ_FAILED:
return "Read failed";

Check warning on line 162 in libsqsh/src/utils/error.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/utils/error.c#L161-L162

Added lines #L161 - L162 were not covered by tests
}
snprintf(err_str, sizeof(err_str), UNKNOWN_ERROR_FORMAT, error_code);
return err_str;
Expand Down
1 change: 0 additions & 1 deletion tools/src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ struct SqshArchive *
open_archive(const char *image_path, uint64_t offset, int *err) {
struct SqshConfig config = {
.source_mapper = NULL,
.mapper_block_size = 1024 * 256,
.archive_offset = offset,
};
if (sqsh_mapper_impl_curl != NULL) {
Expand Down

0 comments on commit 32fa11d

Please sign in to comment.