1
+ #define HASH_FUNCTION HASH_FNV
1
2
2
3
#include <stdio.h>
3
4
#include <sys/types.h>
4
5
#include <sys/stat.h>
5
6
#include <unistd.h>
6
7
#include <dlfcn.h>
7
8
#include <dirent.h>
9
+ #include "uthash.h"
8
10
#include "android/log.h"
9
11
10
12
//#define LOG(...) __android_log_print(ANDROID_LOG_INFO, "redirect", __VA_ARGS__)
11
- #define LOG (...)
13
+ #define LOG (...)
12
14
13
15
typedef struct filemap_entry_s {
14
16
const char * source ;
15
17
const char dest [PATH_MAX ];
16
18
int is_dir ;
17
- struct filemap_entry_s * next ;
19
+ UT_hash_handle hh ;
18
20
} filemap_entry_t ;
19
21
20
22
static filemap_entry_t * entries = NULL ;
@@ -24,47 +26,42 @@ static char libdir[PATH_MAX];
24
26
25
27
static void filemap_entry_add (char * source , char * dest ) {
26
28
filemap_entry_t * entry = (filemap_entry_t * )malloc (sizeof (filemap_entry_t ));
27
- entry -> source = source ;
28
29
snprintf ((char * )entry -> dest , PATH_MAX , "%s/%s" , libdir , dest );
29
- entry -> next = entries ;
30
+ entry -> source = source ;
30
31
entry -> is_dir = 0 ;
31
- entries = entry ;
32
+ HASH_ADD_KEYPTR ( hh , entries , entry -> source , strlen ( entry -> source ), entry ) ;
32
33
}
33
34
34
35
static void filemap_entry_add_dir (char * source ) {
35
36
filemap_entry_t * entry = (filemap_entry_t * )malloc (sizeof (filemap_entry_t ));
36
- entry -> source = source ;
37
37
snprintf ((char * )entry -> dest , PATH_MAX , "%s" , libdir );
38
- entry -> next = entries ;
38
+ entry -> source = source ;
39
39
entry -> is_dir = 1 ;
40
- entries = entry ;
40
+ HASH_ADD_KEYPTR ( hh , entries , entry -> source , strlen ( entry -> source ), entry ) ;
41
41
}
42
42
43
- static const char * filemap_entry_find (const char * source ) {
44
- filemap_entry_t * entry = entries ;
45
- while ( entry != NULL ) {
46
- if ( strcmp (entry -> source , source ) == 0 )
47
- return entry -> dest ;
48
- entry = entry -> next ;
49
- }
50
- return NULL ;
43
+ static filemap_entry_t * filemap_entry_find (const char * source ) {
44
+ filemap_entry_t * entry = NULL ;
45
+ HASH_FIND_STR (entries , source , entry );
46
+ return entry ;
51
47
}
52
48
53
- static filemap_entry_t * filemap_entry_find_dir (const char * source ) {
54
- filemap_entry_t * entry = entries ;
55
- while ( entry != NULL ) {
56
- if ( strcmp (entry -> source , source ) == 0 && entry -> is_dir )
49
+ static filemap_entry_t * find_dir (const char * source ) {
50
+ filemap_entry_t * entry = NULL ;
51
+ if (strncmp (source , basedir , basedirlen ) == 0 ) {
52
+ entry = filemap_entry_find (source + basedirlen + 1 );
53
+ if (entry && entry -> is_dir )
57
54
return entry ;
58
- entry = entry -> next ;
59
55
}
56
+ entry = filemap_entry_find (source );
57
+ if (entry && entry -> is_dir )
58
+ return entry ;
60
59
return NULL ;
61
60
}
62
61
63
- static filemap_entry_t * find_dir (const char * source ) {
64
- if (strncmp (source , basedir , basedirlen ) == 0 ) {
65
- return filemap_entry_find_dir (source + basedirlen + 1 );
66
- }
67
- return filemap_entry_find_dir (source );
62
+ static const char * filemap_entry_find_str (const char * source ) {
63
+ filemap_entry_t * entry = filemap_entry_find (source );
64
+ return entry ? entry -> dest : NULL ;
68
65
}
69
66
70
67
static int file_exists (const char * fn ) {
@@ -79,20 +76,20 @@ static const char *mangle(const char *fn) {
79
76
80
77
if (strncmp (fn , basedir , basedirlen ) == 0 ) {
81
78
LOG (" --> search in the filemap(basedir): %s" , fn + basedirlen + 1 );
82
- const char * fm = filemap_entry_find (fn + basedirlen + 1 );
79
+ const char * fm = filemap_entry_find_str (fn + basedirlen + 1 );
83
80
LOG (" --> filemap returned %s" , fm );
84
81
return fm != NULL ? fm : fn ;
85
82
}
86
83
87
84
if ( fn [0 ] == '.' && fn [1 ] == '/' ) {
88
85
LOG (" --> search in the filemap(.): %s" , fn + 2 );
89
- const char * fm3 = filemap_entry_find (fn + 2 );
86
+ const char * fm3 = filemap_entry_find_str (fn + 2 );
90
87
LOG (" --> filemap returned %s" , fm3 );
91
88
return fm3 != NULL ? fm3 : fn ;
92
89
}
93
90
94
91
LOG (" --> search in the filemap(no basedir): %s" , fn );
95
- const char * fm2 = filemap_entry_find (fn );
92
+ const char * fm2 = filemap_entry_find_str (fn );
96
93
LOG (" --> filemap returned %s" , fm2 );
97
94
return fm2 != NULL ? fm2 : fn ;
98
95
}
@@ -228,7 +225,7 @@ struct dirent *__android_readdir(DIR *dirp) {
228
225
void * * mem = (void * * )dirp ;
229
226
char * source ;
230
227
static struct dirent d ;
231
- filemap_entry_t * entry ;
228
+ filemap_entry_t * entry , * tmp ;
232
229
filemap_entry_t * basedir ;
233
230
234
231
// native access
@@ -260,11 +257,11 @@ struct dirent *__android_readdir(DIR *dirp) {
260
257
d .d_type = entry -> is_dir ? DT_DIR : DT_REG ;
261
258
strncpy (d .d_name , entry -> source , 256 );
262
259
263
- mem [2 ] = entry -> next ;
260
+ mem [2 ] = entry -> hh . next ;
264
261
return & d ;
265
262
266
263
nextentry :;
267
- entry = entry -> next ;
264
+ entry = entry -> hh . next ;
268
265
}
269
266
270
267
LOG (" --> wrapped access ( end )" );
0 commit comments