-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsb_indexer.c
More file actions
210 lines (180 loc) · 5.93 KB
/
Copy pathsb_indexer.c
File metadata and controls
210 lines (180 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* ShellBar v1.9.0 — A command-bar terminal emulator built on libghostty-vt
* Copyright (c) 2026 Xavier Araque <xavieraraque@gmail.com>
* MIT License
*/
#include "sb_indexer.h"
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
/* ------------------------------------------------------------------ */
/* Trie node */
/* ------------------------------------------------------------------ */
typedef struct _SbTrieNode {
GHashTable *children;
char *full_path;
} SbTrieNode;
static SbTrieNode *trie_node_new(void) {
SbTrieNode *node = g_malloc0(sizeof(SbTrieNode));
return node;
}
static void trie_node_free(SbTrieNode *node) {
if (!node) return;
if (node->children) {
GHashTableIter iter;
gpointer value;
g_hash_table_iter_init(&iter, node->children);
while (g_hash_table_iter_next(&iter, NULL, &value))
trie_node_free((SbTrieNode *)value);
g_hash_table_unref(node->children);
}
g_free(node->full_path);
g_free(node);
}
static void trie_insert(SbTrieNode *root, const char *name, const char *full_path) {
SbTrieNode *cur = root;
for (const char *p = name; *p; p++) {
if (!cur->children) {
cur->children = g_hash_table_new_full(g_direct_hash, g_direct_equal,
NULL, NULL);
}
SbTrieNode *child = g_hash_table_lookup(cur->children,
GINT_TO_POINTER((int)*p));
if (!child) {
child = trie_node_new();
g_hash_table_insert(cur->children, GINT_TO_POINTER((int)*p), child);
}
cur = child;
}
if (!cur->full_path)
cur->full_path = g_strdup(full_path);
}
static void trie_collect(SbTrieNode *node, GList **list) {
if (!node) return;
if (node->full_path)
*list = g_list_prepend(*list, g_strdup(node->full_path));
if (node->children) {
GHashTableIter iter;
gpointer value;
g_hash_table_iter_init(&iter, node->children);
while (g_hash_table_iter_next(&iter, NULL, &value))
trie_collect((SbTrieNode *)value, list);
}
}
static SbTrieNode *trie_find(SbTrieNode *root, const char *prefix) {
SbTrieNode *cur = root;
for (const char *p = prefix; *p; p++) {
if (!cur->children) return NULL;
SbTrieNode *child = g_hash_table_lookup(cur->children,
GINT_TO_POINTER((int)*p));
if (!child) return NULL;
cur = child;
}
return cur;
}
/* ------------------------------------------------------------------ */
/* Indexer struct */
/* ------------------------------------------------------------------ */
struct _SbIndexer {
SbTrieNode *trie;
GMutex mutex;
gboolean ready;
};
/* ------------------------------------------------------------------ */
/* PATH scanning (runs in a GTask thread) */
/* ------------------------------------------------------------------ */
typedef struct {
SbTrieNode *trie;
} ScanData;
static void scan_paths(GTask *task, gpointer source, gpointer task_data,
GCancellable *cancellable) {
(void)source;
ScanData *sd = task_data;
const char *path_env = g_getenv("PATH");
if (!path_env) {
g_task_return_boolean(task, TRUE);
return;
}
char *path_copy = g_strdup(path_env);
char *saveptr = NULL;
char *dir_path = strtok_r(path_copy, ":", &saveptr);
while (dir_path) {
if (g_cancellable_is_cancelled(cancellable)) {
g_free(path_copy);
g_task_return_boolean(task, FALSE);
return;
}
DIR *dir = opendir(dir_path);
if (dir) {
struct dirent *entry;
while ((entry = readdir(dir))) {
if (entry->d_name[0] == '.') continue;
char full[PATH_MAX];
snprintf(full, sizeof(full), "%s/%s", dir_path, entry->d_name);
struct stat st;
if (stat(full, &st) == 0 && S_ISREG(st.st_mode) &&
(st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
trie_insert(sd->trie, entry->d_name, full);
}
}
closedir(dir);
}
dir_path = strtok_r(NULL, ":", &saveptr);
}
g_free(path_copy);
g_task_return_boolean(task, TRUE);
}
/* ------------------------------------------------------------------ */
/* Public API */
/* ------------------------------------------------------------------ */
SbIndexer *sb_indexer_new(void) {
SbIndexer *self = g_malloc0(sizeof(SbIndexer));
self->trie = trie_node_new();
g_mutex_init(&self->mutex);
self->ready = FALSE;
return self;
}
void sb_indexer_free(SbIndexer *self) {
if (!self) return;
trie_node_free(self->trie);
g_mutex_clear(&self->mutex);
g_free(self);
}
void sb_indexer_start_async(SbIndexer *self, GCancellable *cancellable,
GAsyncReadyCallback callback, gpointer user_data) {
ScanData *sd = g_malloc0(sizeof(ScanData));
sd->trie = self->trie;
GTask *task = g_task_new(NULL, cancellable, callback, user_data);
g_task_set_source_tag(task, sb_indexer_start_async);
g_task_set_task_data(task, sd, g_free);
g_task_run_in_thread(task, scan_paths);
g_object_unref(task);
}
gboolean sb_indexer_start_finish(SbIndexer *self, GAsyncResult *result,
GError **error) {
(void)self;
g_return_val_if_fail(g_task_is_valid(result, NULL), FALSE);
gboolean ok = g_task_propagate_boolean(G_TASK(result), error);
if (ok) {
g_mutex_lock(&self->mutex);
self->ready = TRUE;
g_mutex_unlock(&self->mutex);
}
return ok;
}
GList *sb_indexer_get_matches(SbIndexer *self, const char *prefix) {
g_return_val_if_fail(self != NULL, NULL);
g_mutex_lock(&self->mutex);
if (!self->ready || !self->trie) {
g_mutex_unlock(&self->mutex);
return NULL;
}
GList *list = NULL;
SbTrieNode *node = trie_find(self->trie, prefix);
if (node)
trie_collect(node, &list);
g_mutex_unlock(&self->mutex);
return list;
}