-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_fs.c
96 lines (86 loc) · 2.55 KB
/
search_fs.c
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
#include <string.h>
#include "query.h"
#include "types.h"
#include "params.h"
#include "search_fs.h"
#include "log.h"
// multiple searches are stored for lookup by their search strings
// the actual search result gets stored in search_fs_to_listable
// The search_str argument is the search string without any leading
// "FILE SEARCH", it may, however, be preceded by the :S prefix
// strips off everything up to and including the :S part
static char *fix_key (const char *dirty_key)
{
char *res = g_strdup(strstr(dirty_key, SEARCH_PREFIX));
char *slashp = strstr(res, "/");
if (slashp)
*slashp = '\0';
if (res)
return res + strlen(SEARCH_PREFIX);
else
return res;
}
SearchList *new_search_list ()
{
return g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify) search_result_destroy);
}
SearchResult *new_search_result ()
{
return g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
}
void search_result_destroy (SearchResult *sr)
{
g_hash_table_destroy((GHashTable*)sr);
}
static char *get_list_name (File *f, SearchResult *past_names)
{
char *res = g_strdup(f->name);
while (g_hash_table_lookup(past_names, res))
{
char *tmp = g_strdup_printf("_%s", res);
g_free(res);
res = tmp;
}
return res;
}
void search_fs_insert (const char *search_str)
{
char *key = fix_key(search_str);
char *qstring = g_strdup_printf("FILE SEARCH %s", key);
result_t *qres = tagdb_query(DB, qstring);
SearchResult *sr = new_search_result();
GHashTable *dict = tagdb_value_extract_dict(qres);
if (dict)
{
HL(dict, it, k, v)
{
file_id_t id = tagdb_value_extract_int(k);
File *f = retrieve_file(DB, id);
char *name = get_list_name(f, sr);
g_hash_table_insert(sr, name, f);
} HL_END;
}
g_hash_table_insert(SEARCHES, key, sr);
g_free(qstring);
result_destroy(qres);
}
void search_fs_readdir (const char *search_str, fuse_fill_dir_t filler, void *fill_buffer)
{
char *key = fix_key(search_str);
SearchResult *sr = g_hash_table_lookup(SEARCHES, key);
HL (sr, it, k, v)
{
filler(fill_buffer, k, NULL, 0);
} HL_END;
}
/* saves us the trouble of writing a special stat method ... */
File *search_fs_get_file (const char *path)
{
char *key = fix_key(path);
char *fname = g_path_get_basename(path);
SearchResult *sr = g_hash_table_lookup(SEARCHES, key);
File *res = g_hash_table_lookup(sr, fname);
g_free(fname);
//g_free(key);
return res;
}