ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Urlader/urlader.c
(Generate patch)

Comparing Urlader/urlader.c (file contents):
Revision 1.8 by root, Fri Dec 30 23:34:08 2011 UTC vs.
Revision 1.11 by root, Thu Jan 5 06:00:13 2012 UTC

1#include "urlib.h"
1#include "urlib.c" 2#include "urlib.c"
2 3
3#include <stdio.h> 4#include <stdio.h>
4#include <stdlib.h> 5#include <stdlib.h>
5#include <unistd.h> 6#include <unistd.h>
7#include <string.h> 8#include <string.h>
8#include <time.h> 9#include <time.h>
9 10
10#include "liblzf/lzf_d.c" 11#include "liblzf/lzf_d.c"
11 12
12#include "urlib.h"
13
14/* some simple? dynamic memory management for paths might save ltos of ram */ 13/* some simple? dynamic memory management for paths might save ltos of ram */
15static u_handle pack_handle; 14static u_handle pack_handle;
16static u_handle lock_handle; 15static u_handle lock_handle;
17static char tmppath[MAX_PATH]; 16static char tmppath[MAX_PATH];
18static char currdir[MAX_PATH];
19static char datadir[MAX_PATH]; // %AppData%/urlader
20static char exe_dir[MAX_PATH]; // %AppData%/urlader/EXE_ID
21static char execdir[MAX_PATH]; // %AppData%/urlader/EXE_ID/EXE_VER
22static char exe_id[MAX_PATH];
23static char exe_ver[MAX_PATH];
24 17
25static int exe_argc; 18static int exe_argc;
26static const char *exe_argv[MAX_ARGC]; 19static const char *exe_argv[MAX_ARGC];
27static char exe_args[MAX_ARGS]; /* actual arguments strings copied here */ 20static char exe_args[MAX_ARGS]; /* actual arguments strings copied here */
28static unsigned int exe_argo; 21static unsigned int exe_argo;
29
30static void
31fatal (const char *msg)
32{
33#ifdef _WIN32
34 MessageBox (0, msg, URLADER, 0);
35#else
36 write (2, URLADER ": ", sizeof (URLADER ": ") - 1);
37 write (2, msg, strlen (msg));
38 write (2, "\n", 1);
39#endif
40
41 _exit (1);
42}
43 22
44static void 23static void
45tmpdir (const char *dir) 24tmpdir (const char *dir)
46{ 25{
47 static int cnt; 26 static int cnt;
66 _spawnv (P_WAIT, argv [0], argv); 45 _spawnv (P_WAIT, argv [0], argv);
67#else 46#else
68 pid_t pid = fork (); 47 pid_t pid = fork ();
69 48
70 if (pid < 0) 49 if (pid < 0)
71 fatal ("fork failure"); 50 u_fatal ("fork failure");
72 51
73 if (!pid) 52 if (!pid)
74 { 53 {
75 execv (argv [0], (void *)argv); 54 execv (argv [0], (void *)argv);
76 _exit (124); 55 _exit (124);
160 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) 139 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1))
161 return 0; 140 return 0;
162 141
163 pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (tail->size)); 142 pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (tail->size));
164 143
165 scratch = u_malloc (scratch_size = u_32 (tail->max_filesize)); 144 scratch = u_malloc (scratch_size = u_32 (tail->max_uncompressed));
166 if (!scratch) 145 if (!scratch)
167 return 0; 146 return 0;
168 147
169 return 1; 148 return 1;
170} 149}
171 150
172static void 151static void
173exe_info (void) 152exe_info (void)
174{ 153{
175 if (!pack_map ()) 154 if (!pack_map ())
176 fatal ("unable to locate packfile in executable - executable corrupted?"); 155 u_fatal ("unable to locate packfile in executable - executable corrupted?");
177 156
178 if (pack_cur->type != U_T_META) 157 if (pack_cur->type != T_META)
179 fatal ("unable to locate executable metadata - executable corrupted?"); 158 u_fatal ("unable to locate executable metadata - executable corrupted?");
180 159
181 strcpy (exe_id, PACK_NAME); 160 strcpy (exe_id, PACK_NAME);
182} 161}
183 162
184static void 163static void
185load (void) 164load (void)
186{ 165{
187 u_mkdir (datadir);
188
189 strcpy (exe_dir, datadir);
190 u_append (exe_dir, exe_id);
191 u_mkdir (exe_dir);
192
193 if (u_chdir (exe_dir)) 166 if (u_chdir (exe_dir))
194 fatal ("unable to change to application data directory"); 167 u_fatal ("unable to change to application data directory");
195 168
196 u_handle h = u_open ("override"); 169 u_handle h = u_open ("override");
197 if (u_valid (h)) 170 if (u_valid (h))
198 { 171 {
199 u_handle oh = pack_handle; 172 u_handle oh = pack_handle;
200 173
201 pack_handle = h; 174 pack_handle = h;
202 if (!pack_map ()) 175 if (pack_map ())
176 u_setenv ("URLADER_OVERRIDE", "override");
177 else
203 { 178 {
204 pack_handle = oh; 179 pack_handle = oh;
205 oh = h; 180 oh = h;
206 } 181 }
207 182
208 u_close (oh); 183 u_close (oh);
209 } 184 }
210 185
211 if (pack_cur->type != U_T_META) 186 if (pack_cur->type != T_META)
212 fatal ("unable to locate override metadata"); 187 u_fatal ("unable to locate override metadata");
213 188
214 strcpy (exe_ver, PACK_DATA); 189 strcpy (exe_ver, PACK_DATA);
190 u_set_exe_info ();
215 pack_next (); 191 pack_next ();
216 192
217 for (;;) 193 for (;;)
218 { 194 {
219 if (pack_cur->type == U_T_ENV) 195 if (pack_cur->type == T_ENV)
220 u_setenv (PACK_NAME, PACK_DATA); 196 u_setenv (PACK_NAME, PACK_DATA);
221 else if (pack_cur->type == U_T_ARG) 197 else if (pack_cur->type == T_ARG)
222 { 198 {
223 int len = u_16 (pack_cur->namelen) + 1; 199 int len = u_16 (pack_cur->namelen) + 1;
224 exe_argv [exe_argc++] = exe_args + exe_argo; 200 exe_argv [exe_argc++] = exe_args + exe_argo;
225 memcpy (exe_args + exe_argo, PACK_NAME, len); 201 memcpy (exe_args + exe_argo, PACK_NAME, len);
226 exe_argo += len; 202 exe_argo += len;
230 206
231 pack_next (); 207 pack_next ();
232 } 208 }
233 209
234done_env_arg: 210done_env_arg:
235 strcpy (execdir, exe_dir);
236 u_append (execdir, "i-");
237 strcat (execdir, exe_ver);
238 211
239 strcat (strcpy (tmppath, execdir), ".lck"); 212 strcat (strcpy (tmppath, execdir), ".lck");
240 lock_handle = u_lock (tmppath, 0, 1); 213 lock_handle = u_lock (tmppath, 0, 1);
241 if (!lock_handle) 214 if (!lock_handle)
242 fatal ("unable to lock application instance"); 215 u_fatal ("unable to lock application instance");
243 216
244 if (access (execdir, F_OK)) 217 if (access (execdir, F_OK))
245 { 218 {
246 // does not exist yet, so unpack and move 219 // does not exist yet, so unpack and move
247 tmpdir (exe_dir); 220 tmpdir (exe_dir);
248 221
249 if (u_chdir (tmppath)) 222 if (u_chdir (tmppath))
250 fatal ("unable to change to new instance directory"); 223 u_fatal ("unable to change to new instance directory");
251 224
252 for (;;) 225 for (;;)
253 { 226 {
254 switch (pack_cur->type) 227 switch (pack_cur->type)
255 { 228 {
256 case U_T_DIR: 229 case T_DIR:
257 u_mkdir (PACK_NAME); 230 u_mkdir (PACK_NAME);
258 break; 231 break;
259 232
260 case U_T_FILE: 233 case T_FILE:
261 { 234 {
262 u_handle h = u_creat (PACK_NAME, pack_cur->flags & U_F_EXEC); 235 u_handle h = u_creat (PACK_NAME, pack_cur->flags & F_EXEC);
263 unsigned int dlen, len = u_32 (pack_cur->datalen); 236 unsigned int dlen, len = u_32 (pack_cur->datalen);
264 char *data = PACK_DATA; 237 char *data = PACK_DATA;
265 238
266 if (pack_cur->flags & U_F_LZF) 239 if (pack_cur->flags & F_LZF)
267 if (dlen = lzf_decompress (data, len, scratch, scratch_size)) 240 if (dlen = lzf_decompress (data, len, scratch, scratch_size))
268 { 241 {
269 data = scratch; 242 data = scratch;
270 len = dlen; 243 len = dlen;
271 } 244 }
272 else 245 else
273 fatal ("unable to uncompress file data - pack corrupted?"); 246 u_fatal ("unable to uncompress file data - pack corrupted?");
274 247
275 if (!u_valid (h)) 248 if (!u_valid (h))
276 fatal ("unable to unpack file from packfile - disk full?"); 249 u_fatal ("unable to unpack file from packfile - disk full?");
277 250
278 if (u_write (h, data, len) != len) 251 if (u_write (h, data, len) != len)
279 fatal ("unable to unpack file from packfile - disk full?"); 252 u_fatal ("unable to unpack file from packfile - disk full?");
280 253
281 u_fsync (h); 254 u_fsync (h);
282 u_close (h); 255 u_close (h);
283 } 256 }
284 break; 257 break;
285 258
286 case U_T_NULL: 259 case T_NULL:
287 goto done; 260 goto done;
288 } 261 }
289 262
290 pack_next (); 263 pack_next ();
291 } 264 }
292 265
293done: 266done:
294 if (u_chdir (datadir)) 267 if (u_chdir (datadir))
295 fatal ("unable to change to data directory"); 268 u_fatal ("unable to change to data directory");
296 269
297 u_sync (); 270 u_sync ();
298 271
299 if (u_rename (tmppath, execdir)) 272 if (u_rename (tmppath, execdir))
300 deltree (tmppath); // if move fails, delete new, assume other process created it independently 273 deltree (tmppath); // if move fails, delete new, assume other process created it independently
302 275
303 pack_unmap (); 276 pack_unmap ();
304 u_close (pack_handle); 277 u_close (pack_handle);
305 278
306 if (u_chdir (execdir)) 279 if (u_chdir (execdir))
307 fatal ("unable to change to application instance directory"); 280 u_fatal ("unable to change to application instance directory");
308 281
309 u_setenv ("URLADER_VERSION", URLADER_VERSION); 282 u_setenv ("URLADER_VERSION", URLADER_VERSION);
310 u_setenv ("URLADER_DATADIR", datadir);
311 u_setenv ("URLADER_EXECDIR", execdir);
312 u_setenv ("URLADER_EXE_ID" , exe_id);
313 u_setenv ("URLADER_EXE_DIR", exe_dir);
314 u_setenv ("URLADER_EXE_VER", exe_ver);
315 283
316#if 0 284#if 0
317 // yes, this is overkill 285 // yes, this is overkill
318 u_setenv ("SHLIB_PATH" , execdir); // hpux 286 u_setenv ("SHLIB_PATH" , execdir); // hpux
319 u_setenv ("LIBPATH" , execdir); // aix 287 u_setenv ("LIBPATH" , execdir); // aix
331{ 299{
332 exe_argv [exe_argc] = 0; 300 exe_argv [exe_argc] = 0;
333 systemv (exe_argv); 301 systemv (exe_argv);
334} 302}
335 303
304static void
305doit (void)
306{
307 u_set_datadir ();
308 u_mkdir (datadir);
309
310 exe_info ();
311 u_set_exe_info ();
312
313 load ();
314 execute ();
315}
316
336#ifdef _WIN32 317#ifdef _WIN32
337 318
338int APIENTRY 319int APIENTRY
339WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) 320WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show)
340{ 321{
341 if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) 322 if (!GetModuleFileName (hI, tmppath, sizeof (tmppath)))
342 fatal ("unable to find executable pack"); 323 u_fatal ("unable to find executable pack");
343 324
344 pack_handle = u_open (tmppath); 325 pack_handle = u_open (tmppath);
345 if (!u_valid (pack_handle)) 326 if (!u_valid (pack_handle))
346 fatal ("unable to open executable pack"); 327 u_fatal ("unable to open executable pack");
347
348 exe_info ();
349 328
350 if (!GetCurrentDirectory (sizeof (currdir), currdir)) 329 if (!GetCurrentDirectory (sizeof (currdir), currdir))
351 strcpy (currdir, "."); 330 strcpy (currdir, ".");
352 331
353 if (SHGetFolderPath (0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, datadir) != S_OK) 332 doit ();
354 fatal ("unable to find application data directory");
355
356 u_mkdir (datadir);
357 u_append (datadir, URLADER);
358
359 load ();
360 execute ();
361 333
362 return 0; 334 return 0;
363} 335}
364 336
365#else 337#else
366 338
367int 339int
368main (int argc, char *argv[]) 340main (int argc, char *argv[])
369{ 341{
370 char *home = getenv ("HOME");
371
372 pack_handle = u_open (argv [0]); 342 pack_handle = u_open (argv [0]);
373 if (!u_valid (pack_handle)) 343 if (!u_valid (pack_handle))
374 fatal ("unable to open executable pack"); 344 u_fatal ("unable to open executable pack");
375
376 exe_info ();
377
378 if (!home)
379 {
380 struct passwd *pw;
381
382 if ((pw = getpwuid (getuid ())))
383 home = pw->pw_dir;
384 else
385 home = "/tmp";
386 }
387 345
388 if (!getcwd (currdir, sizeof (currdir))) 346 if (!getcwd (currdir, sizeof (currdir)))
389 strcpy (currdir, "."); 347 strcpy (currdir, ".");
390 348
391 u_mkdir (home);
392 //strcat (strcat (strcpy (datadir, home), "/."), URLADER);
393 sprintf (datadir, "%s/.%s", home, URLADER);
394 u_mkdir (datadir);
395
396#if 0 349#if 0
350 /* intersperse hostname, for whatever reason */
351
397 if (gethostname (tmppath, sizeof (tmppath))) 352 if (gethostname (tmppath, sizeof (tmppath)))
398 strcpy (tmppath, "default"); 353 strcpy (tmppath, "default");
399 354
400 u_append (datadir, tmppath); 355 u_append (datadir, tmppath);
401#endif 356#endif
402 357
403 load (); 358 doit ();
404 execute ();
405 359
406 return 0; 360 return 0;
407} 361}
408 362
409#endif 363#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines