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
It needs to be tested if this is faster or slower than the mmap.
  • Loading branch information
Gottox committed Jun 8, 2024
1 parent a41dec9 commit 22cf2f1
Show file tree
Hide file tree
Showing 6 changed files with 142 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
129 changes: 129 additions & 0 deletions libsqsh/src/mapper/file_mapper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/******************************************************************************
* *
* Copyright (c) 2023, 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 mmap_mapper.c
*/

#define _LARGEFILE64_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 50 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L50

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

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

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L53

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

file = fopen(input, "r");

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

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L57

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

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

View check run for this annotation

Codecov / codecov/patch

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

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

pos = fseeko64(file, 0, SEEK_END);

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

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L63

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

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L68

Added line #L68 was not covered by tests

sqsh_mapper_set_user_data(mapper, file);

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

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L70

Added line #L70 was not covered by tests

out:
return rv;

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L76

Added line #L76 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 80 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L79 - L80 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L83

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

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L88

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L90 - L91 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 96 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

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

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

out:

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

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L100

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

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

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L102

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

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

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L104

Added line #L104 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 111 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L108-L111

Added lines #L108 - L111 were not covered by tests
}

static int
sqsh_mapping_file_unmap(

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

View check run for this annotation

Codecov / codecov/patch

libsqsh/src/mapper/file_mapper.c#L115

Added line #L115 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 120 in libsqsh/src/mapper/file_mapper.c

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L119 - L120 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 22cf2f1

Please sign in to comment.