-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathdircache.c
204 lines (182 loc) · 6.18 KB
/
dircache.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
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
/*
Copyright (C) 2009 Hans Beckerus (hans.beckerus@gmail.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
This program take use of the freeware "Unrar C++ Library" (libunrar)
by Alexander Roshal and some extensions to it.
Unrar source may be used in any software to handle RAR archives
without limitations free of charge, but cannot be used to re-create
the RAR compression algorithm, which is proprietary. Distribution
of modified Unrar source in separate form or as a part of other
software is permitted, provided that it is clearly stated in
the documentation and source comments that the code may not be used
to develop a RAR (WinRAR) compatible archiver.
*/
#include "platform.h"
#include <memory.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "hashtable.h"
#include "dirlist.h"
#include "dircache.h"
#include "dirname.h"
#include "optdb.h"
#include "common.h"
#define DIRCACHE_SZ 1024
/* Hash table handle */
static void *ht = NULL;
pthread_rwlock_t dir_access_lock;
static struct dircache_cb user_cb;
/*!
*****************************************************************************
*
****************************************************************************/
static void *__alloc()
{
struct dircache_entry *e;
e = malloc(sizeof(struct dircache_entry));
if (e)
dir_list_open(&e->dir_entry_list);
return e;
}
/*!
*****************************************************************************
*
****************************************************************************/
static void __free(const char *key, void *data)
{
struct dircache_entry *e = data;
if (user_cb.free)
user_cb.free(key, e ? &e->dir_entry_list : NULL);
if (e)
dir_list_free(&e->dir_entry_list);
free(e);
}
/*!
*****************************************************************************
*
****************************************************************************/
void dircache_init(struct dircache_cb *cb)
{
struct hash_table_ops ops = {
.alloc = __alloc,
.free = __free,
};
ht = hashtable_init(DIRCACHE_SZ, &ops);
pthread_rwlock_init(&dir_access_lock, NULL);
if (cb)
user_cb = *cb;
}
/*!
*****************************************************************************
*
****************************************************************************/
void dircache_destroy()
{
pthread_rwlock_destroy(&dir_access_lock);
hashtable_destroy(ht);
ht = NULL;
}
/*!
*****************************************************************************
*
****************************************************************************/
void dircache_invalidate(const char *path)
{
uint32_t hash;
if (path) {
char *safe_path = strdup(path);
char *tmp = safe_path;
safe_path = __gnu_dirname(safe_path);
hash = get_hash(safe_path, 0);
free(tmp);
hashtable_entry_delete_subkeys(ht, path, hash);
} else {
hashtable_entry_delete(ht, NULL);
}
}
/*!
*****************************************************************************
*
****************************************************************************/
struct dircache_entry *dircache_alloc(const char *path)
{
struct hash_table_entry *hte;
struct dircache_entry *e;
char *root;
struct stat st;
uint32_t hash;
char *safe_path = strdup(path);
char *tmp = safe_path;
safe_path = __gnu_dirname(safe_path);
hash = get_hash(safe_path, 0);
free(tmp);
hte = hashtable_entry_alloc_hash(ht, path, hash);
if (hte) {
e = hte->user_data;
ABS_ROOT(root, path);
if (!stat(root, &st)) {
#ifdef HAVE_STRUCT_STAT_ST_MTIM
e->mtim = st.st_mtim;
#else
e->mtim.tv_sec = st.st_mtime;
#endif
e->ts_valid = 1;
} else {
e->ts_valid = 0;
}
return e;
}
return NULL;
}
/*!
*****************************************************************************
*
****************************************************************************/
struct dircache_entry *dircache_get(const char *path)
{
struct hash_table_entry *hte;
struct dircache_entry *e;
char *root;
struct stat st;
uint32_t hash;
int ret;
char *safe_path = strdup(path);
char *tmp = safe_path;
safe_path = __gnu_dirname(safe_path);
hash = get_hash(safe_path, 0);
free(tmp);
hte = hashtable_entry_get_hash(ht, path, hash);
if (hte) {
e = hte->user_data;
if (e->ts_valid) {
ABS_ROOT(root, path);
ret = stat(root, &st);
#ifdef HAVE_STRUCT_STAT_ST_MTIM
if (ret || (st.st_mtim.tv_nsec != e->mtim.tv_nsec) ||
(st.st_mtim.tv_sec != e->mtim.tv_sec)) {
#else
if (ret || st.st_mtime != e->mtim.tv_sec) {
#endif
if (user_cb.stale)
user_cb.stale(path);
return NULL;
}
}
return e;
}
return NULL;
}