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

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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines