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

Comparing Urlader/urlader.c (file contents):
Revision 1.1 by root, Thu Dec 29 00:44:21 2011 UTC vs.
Revision 1.3 by root, Thu Dec 29 10:39:34 2011 UTC

9#include <errno.h> 9#include <errno.h>
10#include <string.h> 10#include <string.h>
11#include <time.h> 11#include <time.h>
12#include <fcntl.h> 12#include <fcntl.h>
13 13
14#include "lzf_d.c" 14#include "liblzf/lzf_d.c"
15 15
16#define TAIL_MAGIC "SCHMORPPACK0" 16#define TAIL_MAGIC "SCHMORPPACK0"
17 17
18#ifdef _WIN32 18#ifdef _WIN32
19 19
33 #define u_open(path) CreateFile (path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) 33 #define u_open(path) CreateFile (path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL)
34 #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) 34 #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL)
35 #define u_close(handle) CloseHandle (handle) 35 #define u_close(handle) CloseHandle (handle)
36 #define u_append(path,add) PathAppend (path, add) 36 #define u_append(path,add) PathAppend (path, add)
37 #define u_write(handle,data,len) (WriteFile (handle, data, len, &dword, 0) ? dword : -1) 37 #define u_write(handle,data,len) (WriteFile (handle, data, len, &dword, 0) ? dword : -1)
38 #define u_sync() // use transacted I/O?
38 39
39#else 40#else
40 41
41 #include <sys/mman.h> 42 #include <sys/mman.h>
42 #include <sys/types.h> 43 #include <sys/types.h>
56 #define u_open(path) open (path, O_RDONLY) + 1 57 #define u_open(path) open (path, O_RDONLY) + 1
57 #define u_creat(path,exec) open (path, O_WRONLY | O_CREAT | O_TRUNC, (exec) ? 0777 : 0666) + 1 58 #define u_creat(path,exec) open (path, O_WRONLY | O_CREAT | O_TRUNC, (exec) ? 0777 : 0666) + 1
58 #define u_close(handle) close (handle - 1) 59 #define u_close(handle) close (handle - 1)
59 #define u_append(path,add) strcat (strcat (path, "/"), add) 60 #define u_append(path,add) strcat (strcat (path, "/"), add)
60 #define u_write(handle,data,len) write ((handle) - 1, data, len) 61 #define u_write(handle,data,len) write ((handle) - 1, data, len)
62 #define u_sync() sync ()
61 63
62#endif 64#endif
63 65
64#define u_16(ptr) (((ptr)[0] << 8) | (ptr)[1]) 66#define u_16(ptr) (((ptr)[0] << 8) | (ptr)[1])
65#define u_32(ptr) (((ptr)[0] << 24) | ((ptr)[1] << 16) | ((ptr)[2] << 8) | (ptr)[3]) 67#define u_32(ptr) (((ptr)[0] << 24) | ((ptr)[1] << 16) | ((ptr)[2] << 8) | (ptr)[3])
105 return; 107 return;
106 } 108 }
107} 109}
108 110
109static void 111static void
110systemv (const char *const argv[]) 112systemv (const char *const argv[], int dowait)
111{ 113{
112#ifdef _WIN32 114#ifdef _WIN32
113 _spawnv (P_WAIT, argv [0], argv); 115 _spawnv (P_WAIT, argv [0], argv);
114#else 116#else
115 pid_t pid = fork (); 117 pid_t pid = dowait ? fork () : 0;
116 118
117 if (pid < 0) 119 if (pid < 0)
118 fatal ("fork failure"); 120 fatal ("fork failure");
119 121
120 if (!pid) 122 if (!pid)
136 const char *argv[] = { getenv ("COMSPEC"), "/c", "rd", "/s", "/q", buf, 0 }; 138 const char *argv[] = { getenv ("COMSPEC"), "/c", "rd", "/s", "/q", buf, 0 };
137 sprintf (buf, "\"%s\"", path); 139 sprintf (buf, "\"%s\"", path);
138 #else 140 #else
139 const char *argv[] = { "/bin/rm", "-rf", path, 0 }; 141 const char *argv[] = { "/bin/rm", "-rf", path, 0 };
140 #endif 142 #endif
141 systemv (argv); 143 systemv (argv, 1);
142}
143
144static void
145execute (void)
146{
147 exe_argv [exe_argc] = 0;
148 systemv (exe_argv);
149} 144}
150 145
151enum 146enum
152{ 147{
153 T_NULL, // 5 148 T_NULL, // 5
180 char magic[12]; 175 char magic[12];
181}; 176};
182 177
183static char *pack_base, *pack_end; 178static char *pack_base, *pack_end;
184static struct hdr *pack_cur; 179static struct hdr *pack_cur;
180static char *scratch;
181static unsigned int scratch_len;
185 182
186#define PACK_NAME ((char *)(pack_cur + 1)) 183#define PACK_NAME ((char *)(pack_cur + 1))
187#define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1) 184#define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1)
188#define PACK_VALID pack_cur->type 185#define PACK_VALID pack_cur->type
189 186
199pack_unmap (void) 196pack_unmap (void)
200{ 197{
201 if (!pack_base) 198 if (!pack_base)
202 return; 199 return;
203 200
201 free (scratch);
202
204#ifdef _WIN32 203#ifdef _WIN32
205 UnmapViewOfFile (pack_base); 204 UnmapViewOfFile (pack_base);
206#else 205#else
207 munmap (pack_base, pack_end - pack_base); 206 munmap (pack_base, pack_end - pack_base);
208#endif 207#endif
253 252
254 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) 253 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1))
255 return 0; 254 return 0;
256 255
257 pack_cur = (struct hdr *)(pack_end - u_32 (tail->size)); 256 pack_cur = (struct hdr *)(pack_end - u_32 (tail->size));
257
258 free (scratch);
259 scratch = malloc (scratch_len = u_32 (tail->max_filesize));
258 260
259 return 1; 261 return 1;
260} 262}
261 263
262static void 264static void
337 break; 339 break;
338 340
339 case T_FILE: 341 case T_FILE:
340 { 342 {
341 u_handle h = u_creat (PACK_NAME, pack_cur->flags & F_EXEC); 343 u_handle h = u_creat (PACK_NAME, pack_cur->flags & F_EXEC);
342 unsigned int len = u_32 (pack_cur->datalen); 344 unsigned int dlen, len = u_32 (pack_cur->datalen);
345 char *data = PACK_DATA;
346
347 if (pack_cur->flags & F_LZF)
348 if (dlen = lzf_decompress (data, len, scratch, scratch_len))
349 {
350 data = scratch;
351 len = dlen;
352 }
353 else
354 fatal ("unable to uncompress file data - pack corrupted?");
343 355
344 if (!h) 356 if (!h)
345 fatal ("unable to unpack file from packfile - disk full?"); 357 fatal ("unable to unpack file from packfile - disk full?");
346 358
347 if (u_write (h, PACK_DATA, len) != len) 359 if (u_write (h, data, len) != len)
348 fatal ("unable to unpack file from packfile - disk full?"); 360 fatal ("unable to unpack file from packfile - disk full?");
349 361
350 u_close (h); 362 u_close (h);
351 } 363 }
352 break; 364 break;
360 372
361done: 373done:
362 if (u_chdir (datadir)) 374 if (u_chdir (datadir))
363 fatal ("unable to change to data directory"); 375 fatal ("unable to change to data directory");
364 376
377 u_sync ();
378
365 if (u_rename (tmppath, execdir)) 379 if (u_rename (tmppath, execdir))
366 deltree (tmppath); // if move fails, delete new, assume other process created it independently 380 deltree (tmppath); // if move fails, delete new, assume other process created it independently
367
368 if (u_chdir (execdir))
369 fatal ("unable to change to application instance directory");
370 } 381 }
371 382
372 pack_unmap (); 383 pack_unmap ();
373 u_close (pack_handle); 384 u_close (pack_handle);
385
386 if (u_chdir (execdir))
387 fatal ("unable to change to application instance directory");
374 388
375 u_setenv ("URLADER_VERSION", URLADER_VERSION); 389 u_setenv ("URLADER_VERSION", URLADER_VERSION);
376 u_setenv ("URLADER_DATADIR", datadir); 390 u_setenv ("URLADER_DATADIR", datadir);
377 u_setenv ("URLADER_EXECDIR", execdir); 391 u_setenv ("URLADER_EXECDIR", execdir);
378 u_setenv ("URLADER_EXE_ID" , exe_id); 392 u_setenv ("URLADER_EXE_ID" , exe_id);
388 u_setenv ("LD_LIBRARYN32_PATH", execdir); // irix 402 u_setenv ("LD_LIBRARYN32_PATH", execdir); // irix
389 u_setenv ("LD_LIBRARY64_PATH" , execdir); // irix 403 u_setenv ("LD_LIBRARY64_PATH" , execdir); // irix
390 u_setenv ("DYLD_LIBRARY_PATH" , execdir); // os sucks from apple 404 u_setenv ("DYLD_LIBRARY_PATH" , execdir); // os sucks from apple
391} 405}
392 406
407static void
408execute (void)
409{
410 exe_argv [exe_argc] = 0;
411 systemv (exe_argv, 0);
412}
413
393#ifdef _WIN32 414#ifdef _WIN32
394 415
395int APIENTRY 416int APIENTRY
396WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) 417WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show)
397{ 418{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines