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

Comparing Urlader/urlader.c (file contents):
Revision 1.4 by root, Fri Dec 30 05:27:32 2011 UTC vs.
Revision 1.8 by root, Fri Dec 30 23:34:08 2011 UTC

1#ifndef URLADER 1#include "urlib.c"
2# define URLADER "urlader"
3#endif
4#define URLADER_VERSION "1.0"
5 2
6#include <stdio.h> 3#include <stdio.h>
7#include <stdlib.h> 4#include <stdlib.h>
8#include <unistd.h> 5#include <unistd.h>
9#include <errno.h> 6#include <errno.h>
10#include <string.h> 7#include <string.h>
11#include <time.h> 8#include <time.h>
12#include <fcntl.h>
13 9
14#include "liblzf/lzf_d.c" 10#include "liblzf/lzf_d.c"
15 11
16#define TAIL_MAGIC "SCHMORPPACK0" 12#include "urlib.h"
17 13
18#ifdef _WIN32 14/* some simple? dynamic memory management for paths might save ltos of ram */
19
20 #include <windows.h>
21 //#include <winbase.h>
22 #include <shlobj.h>
23 #include <shlwapi.h>
24 #include <wininet.h>
25
26 static DWORD dword;
27
28 #define u_handle HANDLE
29 #define u_setenv(name,value) SetEnvironmentVariable (name, value)
30 #define u_mkdir(path) !CreateDirectory (path, NULL)
31 #define u_chdir(path) !SetCurrentDirectory (path)
32 #define u_rename(fr,to) !MoveFile (fr, to)
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)
35 #define u_close(handle) CloseHandle (handle)
36 #define u_append(path,add) PathAppend (path, add)
37 #define u_write(handle,data,len) (WriteFile (handle, data, len, &dword, 0) ? dword : -1)
38
39 #define u_fsync(handle) FlushFileBuffers (handle)
40 #define u_sync()
41
42#else
43
44 #include <sys/mman.h>
45 #include <sys/types.h>
46 #include <pwd.h>
47
48 #ifdef PATH_MAX
49 #define MAX_PATH (PATH_MAX < 4096 ? 4096 : PATH_MAX)
50 #else
51 #define MAX_PATH 4096
52 #endif
53
54 #define u_handle int
55 #define u_setenv(name,value) setenv (name, value, 1)
56 #define u_mkdir(path) mkdir (path, 0777)
57 #define u_chdir(path) chdir (path)
58 #define u_rename(fr,to) rename (fr, to)
59 #define u_open(path) open (path, O_RDONLY) + 1
60 #define u_creat(path,exec) open (path, O_WRONLY | O_CREAT | O_TRUNC, (exec) ? 0777 : 0666) + 1
61 #define u_close(handle) close (handle - 1)
62 #define u_append(path,add) strcat (strcat (path, "/"), add)
63 #define u_write(handle,data,len) write ((handle) - 1, data, len)
64
65 // on a mostly idle system, a sync at the end is certainly faster, hope for the best
66 #define u_fsync(handle)
67 #define u_sync() sync ()
68
69#endif
70
71#define u_16(ptr) (((ptr)[0] << 8) | (ptr)[1])
72#define u_32(ptr) (((ptr)[0] << 24) | ((ptr)[1] << 16) | ((ptr)[2] << 8) | (ptr)[3])
73
74static u_handle pack_handle; 15static u_handle pack_handle;
16static u_handle lock_handle;
75static char tmppath[MAX_PATH]; 17static char tmppath[MAX_PATH];
76static char currdir[MAX_PATH]; 18static char currdir[MAX_PATH];
77static char datadir[MAX_PATH]; // %AppData%/urlader 19static char datadir[MAX_PATH]; // %AppData%/urlader
78static char exe_dir[MAX_PATH]; // %AppData%/urlader/EXE_ID 20static char exe_dir[MAX_PATH]; // %AppData%/urlader/EXE_ID
79static char execdir[MAX_PATH]; // %AppData%/urlader/EXE_ID/EXE_VER 21static char execdir[MAX_PATH]; // %AppData%/urlader/EXE_ID/EXE_VER
80static char exe_id[MAX_PATH]; 22static char exe_id[MAX_PATH];
81static char exe_ver[MAX_PATH]; 23static char exe_ver[MAX_PATH];
82 24
83static int exe_argc; 25static int exe_argc;
84static const char *exe_argv[32]; 26static const char *exe_argv[MAX_ARGC];
27static char exe_args[MAX_ARGS]; /* actual arguments strings copied here */
28static unsigned int exe_argo;
85 29
86static void 30static void
87fatal (const char *msg) 31fatal (const char *msg)
88{ 32{
89#ifdef _WIN32 33#ifdef _WIN32
90 MessageBox (0, msg, URLADER, 0); 34 MessageBox (0, msg, URLADER, 0);
91#else 35#else
92 fprintf (stderr, "%s: %s\n", URLADER, msg); 36 write (2, URLADER ": ", sizeof (URLADER ": ") - 1);
37 write (2, msg, strlen (msg));
38 write (2, "\n", 1);
93#endif 39#endif
94 40
95 _exit (1); 41 _exit (1);
96} 42}
97 43
112 return; 58 return;
113 } 59 }
114} 60}
115 61
116static void 62static void
117systemv (const char *const argv[], int dowait) 63systemv (const char *const argv[])
118{ 64{
119#ifdef _WIN32 65#ifdef _WIN32
120 _spawnv (P_WAIT, argv [0], argv); 66 _spawnv (P_WAIT, argv [0], argv);
121#else 67#else
122 pid_t pid = dowait ? fork () : 0; 68 pid_t pid = fork ();
123 69
124 if (pid < 0) 70 if (pid < 0)
125 fatal ("fork failure"); 71 fatal ("fork failure");
126 72
127 if (!pid) 73 if (!pid)
143 const char *argv[] = { getenv ("COMSPEC"), "/c", "rd", "/s", "/q", buf, 0 }; 89 const char *argv[] = { getenv ("COMSPEC"), "/c", "rd", "/s", "/q", buf, 0 };
144 sprintf (buf, "\"%s\"", path); 90 sprintf (buf, "\"%s\"", path);
145 #else 91 #else
146 const char *argv[] = { "/bin/rm", "-rf", path, 0 }; 92 const char *argv[] = { "/bin/rm", "-rf", path, 0 };
147 #endif 93 #endif
148 systemv (argv, 1); 94 systemv (argv);
149} 95}
150
151enum
152{
153 T_NULL, // 5
154 T_META, // 1 : exe_id, exe_ver
155 T_ENV, // 2 : name, value
156 T_ARG, // 3 : arg
157 T_DIR, // 4+: path
158 T_FILE, // 4+: path, data
159 T_NUM
160};
161
162enum
163{
164 F_EXEC = 1,
165 F_LZF = 2,
166 F_NULL = 0
167};
168
169struct hdr
170{
171 unsigned char type;
172 unsigned char flags;
173 unsigned char namelen[2];
174 unsigned char datalen[4];
175};
176
177struct tail {
178 unsigned char max_filesize[4];
179 unsigned char size[4];
180 char magic[12];
181};
182 96
183static char *pack_base, *pack_end; 97static char *pack_base, *pack_end;
184static struct hdr *pack_cur; 98static struct u_pack_hdr *pack_cur;
185static char *scratch; 99static char *scratch;
186static unsigned int scratch_len; 100static unsigned int scratch_size;
187 101
188#define PACK_NAME ((char *)(pack_cur + 1)) 102#define PACK_NAME ((char *)(pack_cur + 1))
189#define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1) 103#define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1)
190#define PACK_VALID pack_cur->type 104#define PACK_VALID pack_cur->type
191 105
192static void 106static void
193pack_next (void) 107pack_next (void)
194{ 108{
195 unsigned int d = u_32 (pack_cur->datalen); 109 unsigned int d = u_32 (pack_cur->datalen);
196 110
197 pack_cur = (struct hdr *)(PACK_DATA + d); 111 pack_cur = (struct u_pack_hdr *)(PACK_DATA + d);
198} 112}
199 113
200static void 114static void
201pack_unmap (void) 115pack_unmap (void)
202{ 116{
203 if (!pack_base) 117 if (pack_base)
204 return; 118 {
205
206 free (scratch);
207
208#ifdef _WIN32
209 UnmapViewOfFile (pack_base);
210#else
211 munmap (pack_base, pack_end - pack_base); 119 u_munmap (pack_base, pack_end - pack_base);
212#endif 120 pack_base = 0;
121 }
122
123 if (scratch)
124 {
125 u_free (scratch, scratch_size);
126 scratch = 0;
127 }
213} 128}
214 129
215static int 130static int
216pack_map (void) 131pack_map (void)
217{ 132{
133 char *addr;
134 unsigned int size;
135
218#ifdef _WIN32 136#ifdef _WIN32
219 BY_HANDLE_FILE_INFORMATION fi; 137 BY_HANDLE_FILE_INFORMATION fi;
220 138
221 if (!GetFileInformationByHandle (pack_handle, &fi)) 139 if (!GetFileInformationByHandle (pack_handle, &fi))
222 return 0; 140 return 0;
223 141
224 if (fi.nFileSizeHigh) // too big 142 size = fi.nFileSizeLow;
143#else
144 size = lseek (pack_handle, 0, SEEK_END);
145#endif
146
147 addr = u_mmap (pack_handle, size);
148 if (!addr)
225 return 0; 149 return 0;
226 150
227 HANDLE handle = CreateFileMapping (pack_handle, 0, PAGE_READONLY, 0, fi.nFileSizeLow, NULL);
228
229 if (!handle)
230 return 0;
231
232 pack_unmap (); 151 pack_unmap ();
233 152
234 pack_base = MapViewOfFile (handle, FILE_MAP_READ, 0, 0, fi.nFileSizeLow);
235
236 CloseHandle (handle);
237
238 pack_end = pack_base + fi.nFileSizeLow;
239#else
240 off_t len = lseek (pack_handle - 1, 0, SEEK_END);
241
242 pack_unmap ();
243
244 pack_base = mmap (0, len, PROT_READ, MAP_SHARED, pack_handle - 1, 0);
245 if (pack_base == (void *)-1)
246 pack_base = 0; 153 pack_base = addr;
247
248 pack_end = pack_base + len; 154 pack_end = pack_base + size;
249#endif
250 155
251 if (!pack_base)
252 return 0;
253
254 struct tail *tail; 156 struct u_pack_tail *tail;
255 157
256 tail = (void *)(pack_end - sizeof (*tail)); 158 tail = (void *)(pack_end - sizeof (*tail));
257 159
258 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) 160 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1))
259 return 0; 161 return 0;
260 162
261 pack_cur = (struct hdr *)(pack_end - u_32 (tail->size)); 163 pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (tail->size));
262 164
263 free (scratch);
264 scratch = malloc (scratch_len = u_32 (tail->max_filesize)); 165 scratch = u_malloc (scratch_size = u_32 (tail->max_filesize));
166 if (!scratch)
167 return 0;
265 168
266 return 1; 169 return 1;
267} 170}
268 171
269static void 172static void
270exe_info (void) 173exe_info (void)
271{ 174{
272 if (!pack_map ()) 175 if (!pack_map ())
273 fatal ("unable to locate packfile in executable - executable corrupted?"); 176 fatal ("unable to locate packfile in executable - executable corrupted?");
274 177
275 if (pack_cur->type != T_META) 178 if (pack_cur->type != U_T_META)
276 fatal ("unable to locate executable metadata - executable corrupted?"); 179 fatal ("unable to locate executable metadata - executable corrupted?");
277 180
278 strcpy (exe_id, PACK_NAME); 181 strcpy (exe_id, PACK_NAME);
279} 182}
280 183
289 192
290 if (u_chdir (exe_dir)) 193 if (u_chdir (exe_dir))
291 fatal ("unable to change to application data directory"); 194 fatal ("unable to change to application data directory");
292 195
293 u_handle h = u_open ("override"); 196 u_handle h = u_open ("override");
294 if (h) 197 if (u_valid (h))
295 { 198 {
296 u_handle oh = pack_handle; 199 u_handle oh = pack_handle;
297 200
298 pack_handle = h; 201 pack_handle = h;
299 if (!pack_map ()) 202 if (!pack_map ())
303 } 206 }
304 207
305 u_close (oh); 208 u_close (oh);
306 } 209 }
307 210
308 if (pack_cur->type != T_META) 211 if (pack_cur->type != U_T_META)
309 fatal ("unable to locate override metadata"); 212 fatal ("unable to locate override metadata");
310 213
311 strcpy (exe_ver, PACK_DATA); 214 strcpy (exe_ver, PACK_DATA);
312 pack_next (); 215 pack_next ();
313 216
314 for (;;) 217 for (;;)
315 { 218 {
316 if (pack_cur->type == T_ENV) 219 if (pack_cur->type == U_T_ENV)
317 u_setenv (PACK_NAME, PACK_DATA); 220 u_setenv (PACK_NAME, PACK_DATA);
318 else if (pack_cur->type == T_ARG) 221 else if (pack_cur->type == U_T_ARG)
319 exe_argv [exe_argc++] = strdup (PACK_NAME); 222 {
223 int len = u_16 (pack_cur->namelen) + 1;
224 exe_argv [exe_argc++] = exe_args + exe_argo;
225 memcpy (exe_args + exe_argo, PACK_NAME, len);
226 exe_argo += len;
227 }
320 else 228 else
321 break; 229 break;
322 230
323 pack_next (); 231 pack_next ();
324 } 232 }
326done_env_arg: 234done_env_arg:
327 strcpy (execdir, exe_dir); 235 strcpy (execdir, exe_dir);
328 u_append (execdir, "i-"); 236 u_append (execdir, "i-");
329 strcat (execdir, exe_ver); 237 strcat (execdir, exe_ver);
330 238
239 strcat (strcpy (tmppath, execdir), ".lck");
240 lock_handle = u_lock (tmppath, 0, 1);
241 if (!lock_handle)
242 fatal ("unable to lock application instance");
243
331 if (access (execdir, F_OK)) 244 if (access (execdir, F_OK))
332 { 245 {
333 // does not exist yet, so unpack and move 246 // does not exist yet, so unpack and move
334 tmpdir (exe_dir); 247 tmpdir (exe_dir);
335 248
338 251
339 for (;;) 252 for (;;)
340 { 253 {
341 switch (pack_cur->type) 254 switch (pack_cur->type)
342 { 255 {
343 case T_DIR: 256 case U_T_DIR:
344 u_mkdir (PACK_NAME); 257 u_mkdir (PACK_NAME);
345 break; 258 break;
346 259
347 case T_FILE: 260 case U_T_FILE:
348 { 261 {
349 u_handle h = u_creat (PACK_NAME, pack_cur->flags & F_EXEC); 262 u_handle h = u_creat (PACK_NAME, pack_cur->flags & U_F_EXEC);
350 unsigned int dlen, len = u_32 (pack_cur->datalen); 263 unsigned int dlen, len = u_32 (pack_cur->datalen);
351 char *data = PACK_DATA; 264 char *data = PACK_DATA;
352 265
353 if (pack_cur->flags & F_LZF) 266 if (pack_cur->flags & U_F_LZF)
354 if (dlen = lzf_decompress (data, len, scratch, scratch_len)) 267 if (dlen = lzf_decompress (data, len, scratch, scratch_size))
355 { 268 {
356 data = scratch; 269 data = scratch;
357 len = dlen; 270 len = dlen;
358 } 271 }
359 else 272 else
360 fatal ("unable to uncompress file data - pack corrupted?"); 273 fatal ("unable to uncompress file data - pack corrupted?");
361 274
362 if (!h) 275 if (!u_valid (h))
363 fatal ("unable to unpack file from packfile - disk full?"); 276 fatal ("unable to unpack file from packfile - disk full?");
364 277
365 if (u_write (h, data, len) != len) 278 if (u_write (h, data, len) != len)
366 fatal ("unable to unpack file from packfile - disk full?"); 279 fatal ("unable to unpack file from packfile - disk full?");
367 280
368 u_fsync (h); 281 u_fsync (h);
369 u_close (h); 282 u_close (h);
370 } 283 }
371 break; 284 break;
372 285
373 case T_NULL: 286 case U_T_NULL:
374 goto done; 287 goto done;
375 } 288 }
376 289
377 pack_next (); 290 pack_next ();
378 } 291 }
398 u_setenv ("URLADER_EXECDIR", execdir); 311 u_setenv ("URLADER_EXECDIR", execdir);
399 u_setenv ("URLADER_EXE_ID" , exe_id); 312 u_setenv ("URLADER_EXE_ID" , exe_id);
400 u_setenv ("URLADER_EXE_DIR", exe_dir); 313 u_setenv ("URLADER_EXE_DIR", exe_dir);
401 u_setenv ("URLADER_EXE_VER", exe_ver); 314 u_setenv ("URLADER_EXE_VER", exe_ver);
402 315
316#if 0
403 // yes, this is overkill 317 // yes, this is overkill
404 u_setenv ("SHLIB_PATH" , execdir); // hpux 318 u_setenv ("SHLIB_PATH" , execdir); // hpux
405 u_setenv ("LIBPATH" , execdir); // aix 319 u_setenv ("LIBPATH" , execdir); // aix
406 u_setenv ("LD_LIBRARY_PATH" , execdir); // most elf systems 320 u_setenv ("LD_LIBRARY_PATH" , execdir); // most elf systems
407 u_setenv ("LD_LIBRARY_PATH_32", execdir); // solaris 321 u_setenv ("LD_LIBRARY_PATH_32", execdir); // solaris
408 u_setenv ("LD_LIBRARY_PATH_64", execdir); // solaris 322 u_setenv ("LD_LIBRARY_PATH_64", execdir); // solaris
409 u_setenv ("LD_LIBRARYN32_PATH", execdir); // irix 323 u_setenv ("LD_LIBRARYN32_PATH", execdir); // irix
410 u_setenv ("LD_LIBRARY64_PATH" , execdir); // irix 324 u_setenv ("LD_LIBRARY64_PATH" , execdir); // irix
411 u_setenv ("DYLD_LIBRARY_PATH" , execdir); // os sucks from apple 325 u_setenv ("DYLD_LIBRARY_PATH" , execdir); // os sucks from apple
326#endif
412} 327}
413 328
414static void 329static void
415execute (void) 330execute (void)
416{ 331{
417 exe_argv [exe_argc] = 0; 332 exe_argv [exe_argc] = 0;
418 systemv (exe_argv, 0); 333 systemv (exe_argv);
419} 334}
420 335
421#ifdef _WIN32 336#ifdef _WIN32
422 337
423int APIENTRY 338int APIENTRY
424WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) 339WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show)
425{ 340{
426 if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) 341 if (!GetModuleFileName (hI, tmppath, sizeof (tmppath)))
427 fatal ("unable to find executable pack"); 342 fatal ("unable to find executable pack");
428 343
429 if (!(pack_handle = u_open (tmppath))) 344 pack_handle = u_open (tmppath);
345 if (!u_valid (pack_handle))
430 fatal ("unable to open executable pack"); 346 fatal ("unable to open executable pack");
431 347
432 exe_info (); 348 exe_info ();
433 349
434 if (!GetCurrentDirectory (sizeof (currdir), currdir)) 350 if (!GetCurrentDirectory (sizeof (currdir), currdir))
451int 367int
452main (int argc, char *argv[]) 368main (int argc, char *argv[])
453{ 369{
454 char *home = getenv ("HOME"); 370 char *home = getenv ("HOME");
455 371
456 // we assume fd 0 and 2 are "normal"
457
458 if (!(pack_handle = u_open (argv [0]))) 372 pack_handle = u_open (argv [0]);
373 if (!u_valid (pack_handle))
459 fatal ("unable to open executable pack"); 374 fatal ("unable to open executable pack");
460 375
461 exe_info (); 376 exe_info ();
462 377
463 if (!home) 378 if (!home)
472 387
473 if (!getcwd (currdir, sizeof (currdir))) 388 if (!getcwd (currdir, sizeof (currdir)))
474 strcpy (currdir, "."); 389 strcpy (currdir, ".");
475 390
476 u_mkdir (home); 391 u_mkdir (home);
392 //strcat (strcat (strcpy (datadir, home), "/."), URLADER);
477 sprintf (datadir, "%s/.%s", home, URLADER); 393 sprintf (datadir, "%s/.%s", home, URLADER);
478 u_mkdir (datadir); 394 u_mkdir (datadir);
479 395
396#if 0
480 if (gethostname (tmppath, sizeof (tmppath))) 397 if (gethostname (tmppath, sizeof (tmppath)))
481 strcpy (tmppath, "default"); 398 strcpy (tmppath, "default");
482 399
483 u_append (datadir, tmppath); 400 u_append (datadir, tmppath);
401#endif
484 402
485 load (); 403 load ();
486 execute (); 404 execute ();
487 405
488 return 0; 406 return 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines