Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reader: reimplementation the reader without _skip #39

Merged
merged 9 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 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
1 change: 1 addition & 0 deletions include/sqsh_mapper_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "sqsh_mapper.h"

#include "sqsh_primitive_private.h"
#include "sqsh_reader_private.h"
#include "sqsh_thread_private.h"
#include <sys/wait.h>

Expand Down
117 changes: 13 additions & 104 deletions include/sqsh_primitive_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@ SQSH_NO_EXPORT SQSH_NO_UNUSED int sqsh__buffer_append(
SQSH_NO_EXPORT SQSH_NO_UNUSED int
sqsh__buffer_move(struct SqshBuffer *buffer, struct SqshBuffer *source);

/**
* @internal
* @memberof SqshBuffer
* @brief resets the buffer size to 0.
*
* This does not free the memory allocated by the buffer so that
* the buffer can be reused.
*
* @param[in,out] buffer The SqshBuffer to drain.
*/

void sqsh__buffer_drain(struct SqshBuffer *buffer);

/**
* @internal
* @memberof SqshBuffer
Expand Down Expand Up @@ -509,110 +522,6 @@ sqsh__lru_touch(struct SqshLru *lru, sqsh_index_t id);
*/
SQSH_NO_EXPORT int sqsh__lru_cleanup(struct SqshLru *lru);

/***************************************
* primitive/reader.c
*/

/**
* @internal
* @brief A buffer that is used to read data from a SqshReader.
*/
struct SqshIteratorImpl {
/**
* @privatesection
*/
int (*next)(void *iterator, size_t desired_size);
int (*skip)(void *iterator, size_t amount, size_t desired_size);
size_t (*block_size)(const void *iterator);
const uint8_t *(*data)(const void *iterator);
size_t (*size)(const void *iterator);
};

/**
* @internal
* @brief A buffer that is used to read data from a SqshReader.
*/
struct SqshReader {
/**
* @privatesection
*/
const struct SqshIteratorImpl *impl;
void *iterator;

sqsh_index_t iterator_offset;
sqsh_index_t buffer_offset;
sqsh_index_t data_offset;
size_t size;
size_t data_size;
struct SqshBuffer buffer;
const uint8_t *data;
};

/**
* @internal
* @memberof SqshReader
* @brief Initializes a reader.
*
* @param[out] reader Pointer to the metablock reader to be initialized.
* @param[in] 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__reader_init(
struct SqshReader *reader, const struct SqshIteratorImpl *impl,
void *iterator);

/**
* @internal
* @memberof SqshReader
* @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__reader_advance(
struct SqshReader *reader, sqsh_index_t offset, size_t size);

/**
* @internal
* @memberof SqshReader
* @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__reader_data(const struct SqshReader *reader);

/**
* @internal
* @memberof SqshReader
* @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__reader_size(const struct SqshReader *reader);

/**
* @internal
* @memberof SqshReader
* @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__reader_cleanup(struct SqshReader *reader);

#ifdef __cplusplus
}
#endif
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 <[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 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/reader.c
*/

struct SqshReaderIteratorImpl {
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 SqshReader {
/**
* @privatesection
*/

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

/**
* @brief interface to the iterator.
*/
const struct SqshReaderIteratorImpl *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 SqshReader
* @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__reader_init(
struct SqshReader *reader,
const struct SqshReaderIteratorImpl *iterator_impl, void *iterator);

/**
* @internal
* @memberof SqshReader
* @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__reader_advance(
struct SqshReader *reader, sqsh_index_t offset, size_t size);

/**
* @internal
* @memberof SqshReader
* @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__reader_data(const struct SqshReader *reader);

/**
* @internal
* @memberof SqshReader
* @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__reader_size(const struct SqshReader *reader);

/**
* @internal
* @memberof SqshReader
* @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__reader_cleanup(struct SqshReader *reader);

#ifdef __cplusplus
}
#endif
#endif /* SQSH_READER_PRIVATE_H */
14 changes: 2 additions & 12 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 SqshReaderIteratorImpl 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 Down
Loading
Loading