|
| 1 | +/* |
| 2 | + * pfstasks_tree.c — pure tree operations for the fstask subsystem. |
| 3 | + * |
| 4 | + * All functions here are psql-free and operate only on in-memory psync_tree |
| 5 | + * structures. Extracted so that unit tests can link this file alone without |
| 6 | + * pulling in pfstasks.c's heavy dependencies (psql, pcrypto, pfs*, …). |
| 7 | + */ |
| 8 | + |
| 9 | +#include <stddef.h> |
| 10 | +#include <string.h> |
| 11 | + |
| 12 | +#include "pdbg.h" |
| 13 | +#include "pfstasks_tree.h" |
| 14 | + |
| 15 | +/* ------------------------------------------------------------------ */ |
| 16 | +/* Static helpers */ |
| 17 | +/* ------------------------------------------------------------------ */ |
| 18 | + |
| 19 | +/* |
| 20 | + * BST search by name string at `nameoff`. |
| 21 | + * If `taskid` != 0 and a name-equal node has a different taskid, |
| 22 | + * walks prev/next siblings to find the matching one. |
| 23 | + */ |
| 24 | +static psync_tree *pfs_task_search_tree(psync_tree *tree, size_t nameoff, |
| 25 | + const char *name, uint64_t taskid, |
| 26 | + size_t taskidoff) { |
| 27 | + int c; |
| 28 | + while (tree) { |
| 29 | + c = strcmp(name, ((char *)tree) + nameoff); |
| 30 | + if (c < 0) tree = tree->left; |
| 31 | + else if (c > 0) tree = tree->right; |
| 32 | + else break; |
| 33 | + } |
| 34 | + if (!tree || !taskid || |
| 35 | + *((uint64_t *)(((char *)tree) + taskidoff)) == taskid) |
| 36 | + return tree; |
| 37 | + /* Walk siblings with the same name to find the matching taskid */ |
| 38 | + psync_tree *tn = ptree_get_prev(tree); |
| 39 | + while (tn) { |
| 40 | + if (strcmp(name, ((char *)tn) + nameoff)) break; |
| 41 | + if (*((uint64_t *)(((char *)tn) + taskidoff)) == taskid) return tn; |
| 42 | + tn = ptree_get_prev(tn); |
| 43 | + } |
| 44 | + tn = ptree_get_next(tree); |
| 45 | + while (tn) { |
| 46 | + if (strcmp(name, ((char *)tn) + nameoff)) break; |
| 47 | + if (*((uint64_t *)(((char *)tn) + taskidoff)) == taskid) return tn; |
| 48 | + tn = ptree_get_next(tn); |
| 49 | + } |
| 50 | + return NULL; |
| 51 | +} |
| 52 | + |
| 53 | +/* |
| 54 | + * Linear in-order walk to find the first node whose uint64_t field at |
| 55 | + * `taskidoff` equals `taskid`. |
| 56 | + */ |
| 57 | +static psync_tree *pfs_task_walk_tree(psync_tree *tree, uint64_t taskid, |
| 58 | + size_t taskidoff) { |
| 59 | + tree = ptree_get_first(tree); |
| 60 | + while (tree) { |
| 61 | + if (*((uint64_t *)(((char *)tree) + taskidoff)) == taskid) return tree; |
| 62 | + tree = ptree_get_next(tree); |
| 63 | + } |
| 64 | + return NULL; |
| 65 | +} |
| 66 | + |
| 67 | +/* ------------------------------------------------------------------ */ |
| 68 | +/* Exported: tree insertion */ |
| 69 | +/* ------------------------------------------------------------------ */ |
| 70 | + |
| 71 | +void pfs_task_insert_into_tree(psync_tree **tree, size_t nameoff, |
| 72 | + psync_tree *element) { |
| 73 | + const char *name; |
| 74 | + psync_tree *node; |
| 75 | + int c; |
| 76 | + |
| 77 | + if (!*tree) { |
| 78 | + ptree_add_after(tree, NULL, element); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + name = ((char *)element) + nameoff; |
| 83 | + node = *tree; |
| 84 | + |
| 85 | + while (1) { |
| 86 | + c = strcmp(name, ((char *)node) + nameoff); |
| 87 | + if (c < 0) { |
| 88 | + if (node->left) |
| 89 | + node = node->left; |
| 90 | + else { |
| 91 | + ptree_add_before(tree, node, element); |
| 92 | + return; |
| 93 | + } |
| 94 | + } else { |
| 95 | + if (c == 0) |
| 96 | + pdbg_logf(D_WARNING, "duplicate entry %s, should not happen", |
| 97 | + name); |
| 98 | + if (node->right) |
| 99 | + node = node->right; |
| 100 | + else { |
| 101 | + ptree_add_after(tree, node, element); |
| 102 | + return; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/* ------------------------------------------------------------------ */ |
| 109 | +/* Exported: find by name (+ optional taskid discriminator) */ |
| 110 | +/* ------------------------------------------------------------------ */ |
| 111 | + |
| 112 | +psync_fstask_mkdir_t *pfs_task_find_mkdir(psync_fstask_folder_t *folder, |
| 113 | + const char *name, uint64_t taskid) { |
| 114 | + return ptree_element( |
| 115 | + pfs_task_search_tree(folder->mkdirs, |
| 116 | + offsetof(psync_fstask_mkdir_t, name), name, |
| 117 | + taskid, offsetof(psync_fstask_mkdir_t, taskid)), |
| 118 | + psync_fstask_mkdir_t, tree); |
| 119 | +} |
| 120 | + |
| 121 | +psync_fstask_rmdir_t *pfs_task_find_rmdir(psync_fstask_folder_t *folder, |
| 122 | + const char *name, uint64_t taskid) { |
| 123 | + return ptree_element( |
| 124 | + pfs_task_search_tree(folder->rmdirs, |
| 125 | + offsetof(psync_fstask_rmdir_t, name), name, |
| 126 | + taskid, offsetof(psync_fstask_rmdir_t, taskid)), |
| 127 | + psync_fstask_rmdir_t, tree); |
| 128 | +} |
| 129 | + |
| 130 | +psync_fstask_creat_t *pfs_task_find_creat(psync_fstask_folder_t *folder, |
| 131 | + const char *name, uint64_t taskid) { |
| 132 | + return ptree_element( |
| 133 | + pfs_task_search_tree(folder->creats, |
| 134 | + offsetof(psync_fstask_creat_t, name), name, |
| 135 | + taskid, offsetof(psync_fstask_creat_t, taskid)), |
| 136 | + psync_fstask_creat_t, tree); |
| 137 | +} |
| 138 | + |
| 139 | +psync_fstask_unlink_t *pfs_task_find_unlink(psync_fstask_folder_t *folder, |
| 140 | + const char *name, uint64_t taskid) { |
| 141 | + return ptree_element( |
| 142 | + pfs_task_search_tree(folder->unlinks, |
| 143 | + offsetof(psync_fstask_unlink_t, name), name, |
| 144 | + taskid, offsetof(psync_fstask_unlink_t, taskid)), |
| 145 | + psync_fstask_unlink_t, tree); |
| 146 | +} |
| 147 | + |
| 148 | +/* ------------------------------------------------------------------ */ |
| 149 | +/* Exported: find by numeric ID */ |
| 150 | +/* ------------------------------------------------------------------ */ |
| 151 | + |
| 152 | +psync_fstask_mkdir_t *pfs_task_find_mkdir_by_folderid( |
| 153 | + psync_fstask_folder_t *folder, psync_fsfolderid_t folderid) { |
| 154 | + return ptree_element( |
| 155 | + pfs_task_walk_tree(folder->mkdirs, folderid, |
| 156 | + offsetof(psync_fstask_mkdir_t, folderid)), |
| 157 | + psync_fstask_mkdir_t, tree); |
| 158 | +} |
| 159 | + |
| 160 | +psync_fstask_creat_t *pfs_task_find_creat_by_fileid( |
| 161 | + psync_fstask_folder_t *folder, psync_fsfileid_t fileid) { |
| 162 | + return ptree_element( |
| 163 | + pfs_task_walk_tree(folder->creats, fileid, |
| 164 | + offsetof(psync_fstask_creat_t, fileid)), |
| 165 | + psync_fstask_creat_t, tree); |
| 166 | +} |
0 commit comments