summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2022-06-01 00:10:22 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2022-06-02 17:26:53 +0200
commita36cf2824c74a3ba7333801268d0ba4194684423 (patch)
treef1fd950ea37bf6c91af7a154f90f5e6eb8b70e01 /include
parentfa7110a9c4bd0ece4be33998b6f9e7d4f528acc7 (diff)
Cleanup: libsqfs: move directory iteration out of the directory reader
Add a simple directory state object to the meta data reader and use that to iterate directory entries. The code for reading the directory listing is movde to readdir.c Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'include')
-rw-r--r--include/sqfs/meta_reader.h83
-rw-r--r--include/sqfs/predef.h1
2 files changed, 84 insertions, 0 deletions
diff --git a/include/sqfs/meta_reader.h b/include/sqfs/meta_reader.h
index e0dc186..149672b 100644
--- a/include/sqfs/meta_reader.h
+++ b/include/sqfs/meta_reader.h
@@ -21,6 +21,7 @@
#define SQFS_META_READER_H
#include "sqfs/predef.h"
+#include "sqfs/super.h"
/**
* @file meta_reader.h
@@ -47,6 +48,37 @@
* from disk and reading transparently across block boarders if required.
*/
+/**
+ * @struct sqfs_readdir_state_t
+ *
+ * @brief Encapsulates state for simple directory reading
+ */
+struct sqfs_readdir_state_t {
+ struct {
+ sqfs_u64 block;
+ size_t offset;
+ size_t size;
+ } init, current;
+
+ size_t entries;
+
+ sqfs_u32 inum_base;
+ sqfs_u64 inode_block;
+};
+
+/**
+ * @brief Rewind a directory state object back to its starting location
+ *
+ * @memberof sqfs_readdir_state_t
+ *
+ * @param it A pointer to the directory state.
+ */
+static SQFS_INLINE void sqfs_readdir_state_reset(sqfs_readdir_state_t *s)
+{
+ s->current = s->init;
+ s->entries = 0;
+}
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -153,6 +185,57 @@ SQFS_API int sqfs_meta_reader_read_dir_ent(sqfs_meta_reader_t *m,
sqfs_dir_entry_t **ent);
/**
+ * @brief Initialize a state object for reading a directory
+ *
+ * @memberof sqfs_readdir_state_t
+ *
+ * This function initializes a simple state object to point to the
+ * location of a directory header and store the total, uncompressed
+ * size of the directory.
+ *
+ * The state object can be passed to @ref sqfs_meta_reader_readdir
+ * to read entries one-by-one.
+ *
+ * @param s A pointer to the state object to initialize.
+ * @param super A pointer to the super block, telling us where
+ * the directory table starts.
+ * @param inode A pointer to a directory inode from which to get the
+ * directory location.
+ *
+ * @return Zero on success, an @ref SQFS_ERROR value on
+ * failure (e.g. the inode is not a directory inode).
+ */
+SQFS_API
+int sqfs_readdir_state_init(sqfs_readdir_state_t *s, const sqfs_super_t *super,
+ const sqfs_inode_generic_t *inode);
+
+/**
+ * @brief Simple directory reading interface
+ *
+ * @memberof sqfs_meta_reader_t
+ *
+ * This function successively reads directory entries, transparently
+ * parsing and skipping across headers. The state is encapsulated in
+ * an external object that is passed in and the function seeks to the
+ * location, so one can swap between multiple states and read several
+ * directories interchangeably.
+ *
+ * @param m A pointer to a meta data reader.
+ * @param s A pointer to a directory state that is used and updated.
+ * @param ent Returns a pointer to a directory entry. Can be
+ * released with a single @ref sqfs_free call.
+ * @param inum If not NULL, returns the decoded inode number.
+ * @param iref If not NULL, returns a reference to the inode.
+ *
+ * @return Zero on success, a negative @ref SQFS_ERROR number on failure,
+ * a positive number if the end of the directory is reached.
+ */
+SQFS_API int sqfs_meta_reader_readdir(sqfs_meta_reader_t *m,
+ sqfs_readdir_state_t *s,
+ sqfs_dir_entry_t **ent,
+ sqfs_u32 *inum, sqfs_u64 *iref);
+
+/**
* @brief Read and decode an inode from a meta data reader.
*
* @memberof sqfs_meta_reader_t
diff --git a/include/sqfs/predef.h b/include/sqfs/predef.h
index a976cd4..3c13ef5 100644
--- a/include/sqfs/predef.h
+++ b/include/sqfs/predef.h
@@ -94,6 +94,7 @@ typedef struct sqfs_block_writer_t sqfs_block_writer_t;
typedef struct sqfs_block_writer_stats_t sqfs_block_writer_stats_t;
typedef struct sqfs_block_processor_stats_t sqfs_block_processor_stats_t;
typedef struct sqfs_block_processor_desc_t sqfs_block_processor_desc_t;
+typedef struct sqfs_readdir_state_t sqfs_readdir_state_t;
typedef struct sqfs_fragment_t sqfs_fragment_t;
typedef struct sqfs_dir_header_t sqfs_dir_header_t;