update for beta release
[framework/uifw/e17.git] / src / preload / e_precache.c
1 #include "config.h"
2 #include "e_precache.h"
3
4 static void *lib_eina = NULL;
5 static void *lib_ecore = NULL;
6 static void *lib_ecore_file = NULL;
7 static void *lib_ecore_x = NULL;
8 static void *lib_evas = NULL;
9 static void *lib_edje = NULL;
10 static void *lib_eet = NULL;
11
12 static int *e_precache_end = NULL;
13
14 /* internal calls */
15 static int log_fd = -1;
16 static int do_log = 0;
17
18 static void
19 log_open(void)
20 {
21    char buf[4096] = "DUMMY", *home;
22    
23    if (log_fd != -1) return;
24    if (!e_precache_end)
25      {
26 #ifdef HAVE_UNSETENV
27         unsetenv("LD_PRELOAD");
28 #else
29         if (getenv("LD_PRELOAD")) putenv("LD_PRELOAD");
30 #endif
31         e_precache_end = dlsym(NULL, "e_precache_end");
32      }
33    if (!e_precache_end) return;
34    if (*e_precache_end) return;
35    
36    home = getenv("HOME");
37    if (home)
38      snprintf(buf, sizeof(buf), "%s/.e-precache", home);
39    else
40      snprintf(buf, sizeof(buf), "/tmp/.e-precache");
41    log_fd = open(buf, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);
42    if (log_fd) do_log = 1;
43 }
44
45 static void
46 log_close(void)
47 {
48    if (log_fd >= 0)
49      {
50         close(log_fd);
51         log_fd = -1;
52      }
53    do_log = 0;
54 }
55
56 static void
57 log_write(const char *type, const char *file)
58 {
59    static Eina_Hash *s_hash = NULL;
60    static Eina_Hash *o_hash = NULL;
61    static Eina_Hash *d_hash = NULL;
62    char buf[2];
63
64    if ((e_precache_end) && (*e_precache_end))
65      {
66         log_close();
67         return;
68      }
69    if (type[0] == 's')
70      {
71         if (eina_hash_find(s_hash, file)) return;
72         if (!s_hash) s_hash = eina_hash_string_superfast_new(NULL);
73         eina_hash_add(s_hash, file, (void *)1);
74      }
75    else if (type[0] == 'o')
76      {
77         if (eina_hash_find(o_hash, file)) return;
78         if (!o_hash) o_hash = eina_hash_string_superfast_new(NULL);
79         eina_hash_add(o_hash, file, (void *)1);
80      }
81    else if (type[0] == 'd')
82      {
83         if (eina_hash_find(d_hash, file)) return;
84         if (!d_hash) d_hash = eina_hash_string_superfast_new(NULL);
85         eina_hash_add(d_hash, file, (void *)1);
86      }
87    buf[0] = type[0]; buf[1] = ' ';
88    write(log_fd, buf, 2);
89    write(log_fd, file, strlen(file));
90    write(log_fd, "\n", 1);
91 }
92
93 static void *
94 lib_func(const char *lib1, const char *lib2, const char *fname, const char *libname, void **lib)
95 {
96    void *func;
97    
98    if (!*lib) *lib = dlopen(lib1, RTLD_GLOBAL | RTLD_LAZY);
99    if (!*lib) *lib = dlopen(lib2, RTLD_GLOBAL | RTLD_LAZY);
100    func = dlsym(*lib, fname);
101    if (!func)
102      {
103         printf("ABORT: Can't find %s() in %s or %s (%s = %p)\n",
104                fname, lib1, lib2, libname, *lib);
105         abort();
106      }
107    log_open();
108    return func;
109 }
110
111 /* intercepts */
112 Eina_Module *
113 eina_module_find(const Eina_Array *array, const char *module) 
114 {
115    static Eina_Module *(*func) (const Eina_Array *array, const char *module) = NULL;
116
117    if (!func) 
118      func = lib_func("libeina.so", "libeina.so.1", 
119                      "eina_module_find", "lib_eina", &lib_eina);
120    if (do_log) log_write("o", module);
121    return (*func) (array, module);
122 }
123
124 void 
125 ecore_app_args_set(int argc, const char **argv)
126 {
127    static void (*func) (int argc, const char **argv) = NULL;
128
129    if (!func) 
130      func = lib_func("libecore.so", "libecore.so.1", 
131                      "ecore_app_args_set", "lib_ecore", &lib_ecore);
132    if (do_log) log_write("o", (const char *)argv);
133    (*func) (argc, argv);
134 }
135
136 long long
137 ecore_file_mod_time(const char *file)
138 {
139    static long long (*func) (const char *file) = NULL;
140
141    if (!func)
142      func = lib_func("libecore_file.so", "libecore_file.so.1", 
143                      "ecore_file_mod_time", "lib_ecore_file", &lib_ecore_file);
144    if (do_log) log_write("s", file);
145    return (*func) (file);
146 }
147
148 long long
149 ecore_file_size(const char *file)
150 {
151    static int (*func) (const char *file) = NULL;
152
153    if (!func)
154      func = lib_func("libecore_file.so", "libecore_file.so.1", 
155                      "ecore_file_size", "lib_ecore_file", &lib_ecore_file);
156    if (do_log) log_write("s", file);
157    return (*func) (file);
158 }
159
160 Eina_Bool
161 ecore_file_exists(const char *file)
162 {
163    static int (*func) (const char *file) = NULL;
164
165    if (!func)
166      func = lib_func("libecore_file.so", "libecore_file.so.1", 
167                      "ecore_file_exists", "lib_ecore_file", &lib_ecore_file);
168    if (do_log) log_write("s", file);
169    return (*func) (file);
170 }
171
172 Eina_Bool
173 ecore_file_is_dir(const char *file)
174 {
175    static int (*func) (const char *file) = NULL;
176
177    if (!func)
178      func = lib_func("libecore_file.so", "libecore_file.so.1", 
179                      "ecore_file_is_dir", "lib_ecore_file", &lib_ecore_file);
180    if (do_log) log_write("s", file);
181    return (*func) (file);
182 }
183
184 Eina_Bool
185 ecore_file_can_read(const char *file)
186 {
187    static int (*func) (const char *file) = NULL;
188
189    if (!func)
190      func = lib_func("libecore_file.so", "libecore_file.so.1", 
191                      "ecore_file_can_read", "lib_ecore_file", &lib_ecore_file);
192    if (do_log) log_write("s", file);
193    return (*func) (file);
194 }
195
196 Eina_Bool
197 ecore_file_can_write(const char *file)
198 {
199    static int (*func) (const char *file) = NULL;
200
201    if (!func)
202      func = lib_func("libecore_file.so", "libecore_file.so.1", 
203                      "ecore_file_can_write", "lib_ecore_file", &lib_ecore_file);
204    if (do_log) log_write("s", file);
205    return (*func) (file);
206 }
207
208 Eina_Bool
209 ecore_file_can_exec(const char *file)
210 {
211    static int (*func) (const char *file) = NULL;
212
213    if (!func)
214      func = lib_func("libecore_file.so", "libecore_file.so.1", 
215                      "ecore_file_can_exec", "lib_ecore_file", &lib_ecore_file);
216    if (do_log) log_write("s", file);
217    return (*func) (file);
218 }
219
220 Eina_List *
221 ecore_file_ls(const char *file)
222 {
223    static Eina_List * (*func) (const char *file) = NULL;
224
225    if (!func)
226      func = lib_func("libecore_file.so", "libecore_file.so.1", 
227                      "ecore_file_ls", "lib_ecore_file", &lib_ecore_file);
228    if (do_log) log_write("d", file);
229    return (*func) (file);
230 }
231
232 int 
233 ecore_x_init(const char *name) 
234 {
235    static int (*func) (const char *name) = NULL;
236
237    if (!func) 
238      func = lib_func("libecore_x.so", "libecore_x.so.1", 
239                      "ecore_x_init", "lib_ecore_x", &lib_ecore_x);
240    if (do_log) log_write("o", name);
241    return (*func) (name);
242 }
243
244 void
245 evas_object_image_file_set(Evas_Object *obj, const char *file, const char *key)
246 {
247    static void (*func) (Evas_Object *obj, const char *file, const char *key) = NULL;
248
249    if (!func)
250      func = lib_func("libevas.so", "libevas.so.1", 
251                      "evas_object_image_file_set", "lib_evas", &lib_evas);
252    if (do_log) log_write("o", file);
253    (*func) (obj, file, key);
254 }
255
256 /* edje */
257 Eina_Bool 
258 edje_object_file_set(Evas_Object *obj, const char *file, const char *group) 
259 {
260    static Eina_Bool (*func) (Evas_Object *obj, const char *file, const char *group) = NULL;
261
262    if (!func)
263      func = lib_func("libedje.so", "libedje.so.1", 
264                      "edje_object_file_set", "lib_edje", &lib_edje);
265    if (do_log) log_write("o", file);
266    return (*func) (obj, file, group);
267 }
268
269 Eet_File *
270 eet_open(const char *file, Eet_File_Mode mode)
271 {
272    static Eet_File * (*func) (const char *file, Eet_File_Mode mode) = NULL;
273
274    if (!func)
275      func = lib_func("libeet.so", "libeet.so.0", 
276                      "eet_open", "lib_eet", &lib_eet);
277    if (do_log) log_write("o", file);
278    return (*func) (file, mode);
279 }
280