Skip to content

Commit

Permalink
reader: reimplementation the reader without _skip
Browse files Browse the repository at this point in the history
Currently, there is an issue with a subtle incompatibility between
different iterators. The old SqshReader expected, that all blocks
(except the last one) are of the same size. This is not true for
SqshFileIterator when iterating over sparse files. The new
SqshReader2 does not have this limitation.
  • Loading branch information
Gottox committed Aug 16, 2023
1 parent efa09aa commit 57b8b77
Show file tree
Hide file tree
Showing 7 changed files with 702 additions and 18 deletions.
3 changes: 2 additions & 1 deletion include/sqsh_file_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "sqsh_extract_private.h"
#include "sqsh_mapper_private.h"
#include "sqsh_metablock_private.h"
#include "sqsh_reader_private.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -173,7 +174,7 @@ struct SqshFileReader {
* @privatesection
*/
struct SqshFileIterator iterator;
struct SqshReader reader;
struct SqshReader2 reader;
};

/**
Expand Down
193 changes: 193 additions & 0 deletions include/sqsh_reader_private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/******************************************************************************
* *
* Copyright (c) 2023, Enno Boland <g@s01.de> *
* *
* 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 (mail@eboland.de)
* @file sqsh_reader_private.h
*/

#ifndef SQSH_READER_PRIVATE_H
#define SQSH_READER_PRIVATE_H

#include "sqsh_common.h"
#include "sqsh_primitive_private.h"
#include <sys/wait.h>

#ifdef __cplusplus
extern "C" {
#endif

/***************************************
* reader/reader2.c
*/

struct SqshReader2IteratorImpl {
int (*next)(void *iterator, size_t desired_size);
const uint8_t *(*data)(const void *iterator);
size_t (*size)(const void *iterator);
};

/**
* @brief An iterator over extended attributes.
*/
struct SqshReader2 {
/**
* @privatesection
*/

/**
* @brief The for this reader.
*/
void *iterator;

/**
* @brief interface to the iterator.
*/
const struct SqshReader2IteratorImpl *iterator_impl;

/**
* @brief The offset of the iterator.
*
* A value of "0" indicates, that the reader currently directly maps data
* from the iterator. That means, that `data` points into memory managed by
* the iterator.
*
* A non-zero value indicates, that the reader has copied data from the
* iterator into the buffer. That means, that `data` points into memory
* managed by the buffer. The actual value of iterator_offset is the offset
* between the buffer and the iterator.
*
* example:
*
* ```
* 0123456789
* buffer: ##########
* iterator: ####
* ```
*
* in this case, the iterator_offset is 6.
*/
sqsh_index_t iterator_offset;

/**
* @brief The offset of mapped data.
*
* This value is set to zero if the reader uses buffered data.
*
* Otherwise is indicates the offset of the data in the iterator.
*/
sqsh_index_t offset;

/**
* @brief The buffer to store data in if they cannot be directly mapped.
*/
struct SqshBuffer buffer;

/**
* @brief The data that is presented to the user.
*
* This pointer has the offset already applied if in mapped mode.
*
* Otherwise it points to the beginning of the buffer.
*/
const uint8_t *data;
/**
* @brief The size of the data that is presented to the user.
*/
size_t size;
};

/**
* @internal
* @memberof SqshReader2
* @brief Initializes a reader.
*
* @param[out] reader Pointer to the metablock reader to be initialized.
* @param[in] iterator_impl Implementation of the iterator.
* @param[in] iterator Iterator to use for the reader.
*
* @return 0 on success, less than zero on error.
*/
SQSH_NO_EXPORT SQSH_NO_UNUSED int sqsh__reader2_init(
struct SqshReader2 *reader,
const struct SqshReader2IteratorImpl *iterator_impl, void *iterator);

/**
* @internal
* @memberof SqshReader2
* @brief Advances the reader by the given offset and size.
*
* @param[in,out] reader Pointer to the metablock reader to be advanced.
* @param[in] offset Offset to advance the reader by.
* @param[in] size Size of the block to advance the reader by.
*
* @return 0 on success, less than zero on error.
*/
SQSH_NO_EXPORT SQSH_NO_UNUSED int sqsh__reader2_advance(
struct SqshReader2 *reader, sqsh_index_t offset, size_t size);

/**
* @internal
* @memberof SqshReader2
* @brief Returns a pointer to the data at the current position of the metablock
* reader.
*
* @param[in] reader Pointer to the metablock reader.
*
* @return Pointer to the data at the current position of the metablock reader.
*/
SQSH_NO_EXPORT const uint8_t *
sqsh__reader2_data(const struct SqshReader2 *reader);

/**
* @internal
* @memberof SqshReader2
* @brief Returns the size of the data at the current position of the metablock
* reader.
*
* @param[in] reader Pointer to the metablock reader.
*
* @return Size of the data at the current position of the metablock reader.
*/
SQSH_NO_EXPORT size_t sqsh__reader2_size(const struct SqshReader2 *reader);

/**
* @internal
* @memberof SqshReader2
* @brief Cleans up and frees the resources used by the metablock reader.
*
* @param[in,out] reader Pointer to the metablock reader to be cleaned up.
*
* @return 0 on success, less than zero on error.
*/
SQSH_NO_EXPORT int sqsh__reader2_cleanup(struct SqshReader2 *reader);

#ifdef __cplusplus
}
#endif
#endif /* SQSH_READER_PRIVATE_H */
24 changes: 7 additions & 17 deletions lib/file/file_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,14 @@

#include "../../include/sqsh_archive_private.h"
#include "../../include/sqsh_error.h"
#include "../../include/sqsh_primitive_private.h"
#include "../../include/sqsh_reader_private.h"

#include "../utils/utils.h"

static int
file_iterator_next(void *iterator, size_t desired_size) {
return sqsh_file_iterator_next(iterator, desired_size);
}
static int
file_iterator_skip(void *iterator, size_t amount, size_t desired_size) {
return sqsh_file_iterator_skip(iterator, amount, desired_size);
}
static size_t
file_iterator_block_size(const void *iterator) {
return sqsh_file_iterator_block_size(iterator);
}
static const uint8_t *
file_iterator_data(const void *iterator) {
return sqsh_file_iterator_data(iterator);
Expand All @@ -60,10 +52,8 @@ file_iterator_size(const void *iterator) {
return sqsh_file_iterator_size(iterator);
}

static const struct SqshIteratorImpl file_reader_impl = {
static const struct SqshReader2IteratorImpl file_reader_impl = {
.next = file_iterator_next,
.skip = file_iterator_skip,
.block_size = file_iterator_block_size,
.data = file_iterator_data,
.size = file_iterator_size,
};
Expand All @@ -76,7 +66,7 @@ sqsh__file_reader_init(
if (rv < 0) {
goto out;
}
rv = sqsh__reader_init(
rv = sqsh__reader2_init(
&reader->reader, &file_reader_impl, &reader->iterator);
out:
return rv;
Expand Down Expand Up @@ -105,22 +95,22 @@ sqsh_file_reader_new(const struct SqshFile *file, int *err) {
int
sqsh_file_reader_advance(
struct SqshFileReader *reader, sqsh_index_t offset, size_t size) {
return sqsh__reader_advance(&reader->reader, offset, size);
return sqsh__reader2_advance(&reader->reader, offset, size);
}

const uint8_t *
sqsh_file_reader_data(const struct SqshFileReader *reader) {
return sqsh__reader_data(&reader->reader);
return sqsh__reader2_data(&reader->reader);
}

size_t
sqsh_file_reader_size(const struct SqshFileReader *reader) {
return sqsh__reader_size(&reader->reader);
return sqsh__reader2_size(&reader->reader);
}

int
sqsh__file_reader_cleanup(struct SqshFileReader *reader) {
sqsh__reader_cleanup(&reader->reader);
sqsh__reader2_cleanup(&reader->reader);
sqsh__file_iterator_cleanup(&reader->iterator);

return 0;
Expand Down
1 change: 1 addition & 0 deletions lib/meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
libsqsh_sources = files(
'reader/reader2.c',
'archive/archive.c',
'archive/compression_options.c',
'archive/inode_map.c',
Expand Down
Loading

0 comments on commit 57b8b77

Please sign in to comment.