Skip to content

Commit

Permalink
traversal: rewrite implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Gottox committed Jun 1, 2024
1 parent 2046669 commit 68e3eb5
Show file tree
Hide file tree
Showing 14 changed files with 531 additions and 457 deletions.
2 changes: 1 addition & 1 deletion examples/traverse_dir_ll.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ main(int argc, char *argv[]) {
}

struct SqshTreeTraversal *traversal =
sqsh_tree_traversal_new2(file, 0, &error_code);
sqsh_tree_traversal_new(file, &error_code);
if (error_code != 0) {
sqsh_perror(error_code, "sqsh_directory_iterator_new");
return 1;
Expand Down
2 changes: 1 addition & 1 deletion fuzzer/simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ LLVMFuzzerTestOneInput(char *data, size_t size) {
goto out;
}

traversal = sqsh_tree_traversal_new2(inode, 0, &rv);
traversal = sqsh_tree_traversal_new(inode, &rv);
if (rv < 0) {
goto out;
}
Expand Down
18 changes: 6 additions & 12 deletions include/sqsh_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,6 @@ enum SqshTreeTraversalState {
struct SqshTreeTraversal;

/**
* @deprecated Since 1.4.0. Use sqsh_tree_traversal_new2() instead.
* @brief Creates a new SqshTreeTraversal object at the root inode.
* @memberof SqshTreeTraversal
*
Expand All @@ -488,23 +487,18 @@ struct SqshTreeTraversal;
*
* @return a new file reader.
*/
__attribute__((deprecated("Since 1.4.0. Use sqsh_tree_traversal_new2() "
"instead."))) struct SqshTreeTraversal *
struct SqshTreeTraversal *
sqsh_tree_traversal_new(const struct SqshFile *file, int *err);

/**
* @brief Creates a new SqshTreeTraversal object at the root inode.
* @brief Sets the maximum depth of the traversal.
* @memberof SqshTreeTraversal
*
* @param[in] file the base inode to start from.
* @param[in] max_depth the maximum depth to traverse.
* @param[out] err Pointer to an int where the error code will be
* stored.
*
* @return a new file reader.
* @param[in] traversal The traversal to use
* @param[in] max_depth The maximum depth to traverse.
*/
struct SqshTreeTraversal *sqsh_tree_traversal_new2(
const struct SqshFile *file, size_t max_depth, int *err);
void sqsh_tree_traversal_set_max_depth(
struct SqshTreeTraversal *traversal, size_t max_depth);

/**
* @memberof SqshTreeTraversal
Expand Down
28 changes: 15 additions & 13 deletions libsqsh/include/sqsh_tree_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
#define SQSH_TREE_PRIVATE_H

#include "sqsh_directory_private.h"
#include "sqsh_tree.h"
#include <sqsh_tree.h>

#include <cextras/memory.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -99,6 +101,7 @@ sqsh__path_resolver_cleanup(struct SqshPathResolver *resolver);
struct SqshTreeTraversalStackElement {
struct SqshDirectoryIterator iterator;
struct SqshFile file;
struct SqshTreeTraversalStackElement *next;
};

/**
Expand All @@ -109,18 +112,19 @@ struct SqshTreeTraversal {
* @privatesection
*/

enum SqshTreeTraversalState state;
const struct SqshFile *base_file;
struct SqshTreeTraversalStackElement **stack;
struct CxPreallocPool stack_pool;
struct SqshTreeTraversalStackElement *stack;
size_t stack_size;
size_t stack_capacity;

size_t depth;
size_t max_depth;

const struct SqshFile *base_file;
struct SqshDirectoryIterator base_iterator;

const struct SqshFile *current_file;
struct SqshDirectoryIterator *current_iterator;
const char *current_name;
size_t current_name_size;
uint64_t current_inode_ref;
uint32_t current_dir_inode;
enum SqshFileType current_type;
enum SqshTreeTraversalState state;
};

/**
Expand All @@ -129,14 +133,12 @@ struct SqshTreeTraversal {
* @brief Initializes a SqshTreeTraversal struct.
*
* @param[out] traversal The file traversal to initialize.
* @param[in] max_depth The maximum depth to traverse
* @param[in] file the file to start traversal at
*
* @return 0 on success, less than 0 on error.
*/
SQSH_NO_EXPORT SQSH_NO_UNUSED int sqsh__tree_traversal_init(
struct SqshTreeTraversal *traversal, size_t max_depth,
const struct SqshFile *file);
struct SqshTreeTraversal *traversal, const struct SqshFile *file);

/**
* @internal
Expand Down
2 changes: 1 addition & 1 deletion libsqsh/src/easy/traversal.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ sqsh_easy_tree_traversal(
goto out;
}

rv = sqsh__tree_traversal_init(&iterator.traversal, 0, file);
rv = sqsh__tree_traversal_init(&iterator.traversal, file);
if (rv < 0) {
goto out;
}
Expand Down
Loading

0 comments on commit 68e3eb5

Please sign in to comment.