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.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"
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 "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_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#else
40
41 #include <sys/mman.h>
42 #include <sys/types.h>
43 #include <pwd.h>
44
45 #ifdef PATH_MAX
46 #define MAX_PATH (PATH_MAX < 4096 ? 4096 : PATH_MAX)
47 #else
48 #define MAX_PATH 4096
49 #endif
50
51 #define u_handle int
52 #define u_setenv(name,value) setenv (name, value, 1)
53 #define u_mkdir(path) mkdir (path, 0777)
54 #define u_chdir(path) chdir (path)
55 #define u_rename(fr,to) rename (fr, to)
56 #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_close(handle) close (handle - 1)
59 #define u_append(path,add) strcat (strcat (path, "/"), add)
60 #define u_write(handle,data,len) write ((handle) - 1, data, len)
61
62#endif
63
64#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])
66
67static u_handle pack_handle; 14static u_handle pack_handle;
15static u_handle lock_handle;
68static char tmppath[MAX_PATH]; 16static char tmppath[MAX_PATH];
69static char currdir[MAX_PATH];
70static char datadir[MAX_PATH]; // %AppData%/urlader
71static char exe_dir[MAX_PATH]; // %AppData%/urlader/EXE_ID
72static char execdir[MAX_PATH]; // %AppData%/urlader/EXE_ID/EXE_VER
73static char exe_id[MAX_PATH];
74static char exe_ver[MAX_PATH];
75 17
76static int exe_argc; 18static int exe_argc;
77static const char *exe_argv[32]; 19static const char *exe_argv[MAX_ARGC];
78 20static char exe_args[MAX_ARGS]; /* actual arguments strings copied here */
79static void 21static unsigned int exe_argo;
80fatal (const char *msg)
81{
82#ifdef _WIN32
83 MessageBox (0, msg, URLADER, 0);
84#else
85 fprintf (stderr, "%s: %s\n", URLADER, msg);
86#endif
87
88 _exit (1);
89}
90 22
91static void 23static void
92tmpdir (const char *dir) 24tmpdir (const char *dir)
93{ 25{
94 static int cnt; 26 static int cnt;
113 _spawnv (P_WAIT, argv [0], argv); 45 _spawnv (P_WAIT, argv [0], argv);
114#else 46#else
115 pid_t pid = fork (); 47 pid_t pid = fork ();
116 48
117 if (pid < 0) 49 if (pid < 0)
118 fatal ("fork failure"); 50 u_fatal ("fork failure");
119 51
120 if (!pid) 52 if (!pid)
121 { 53 {
122 execv (argv [0], (void *)argv); 54 execv (argv [0], (void *)argv);
123 _exit (124); 55 _exit (124);
139 const char *argv[] = { "/bin/rm", "-rf", path, 0 }; 71 const char *argv[] = { "/bin/rm", "-rf", path, 0 };
140 #endif 72 #endif
141 systemv (argv); 73 systemv (argv);
142} 74}
143 75
144static void
145execute (void)
146{
147 exe_argv [exe_argc] = 0;
148 systemv (exe_argv);
149}
150
151enum
152{
153 T_NULL, // 5
154 T_META, // 1 : exe_id, exe_ver
155 T_ARG, // 2 : arg
156 T_ENV, // 3 : name, value
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
183static char *pack_base, *pack_end; 76static char *pack_base, *pack_end;
184static struct hdr *pack_cur; 77static struct u_pack_hdr *pack_cur;
78static char *scratch;
79static unsigned int scratch_size;
185 80
186#define PACK_NAME ((char *)(pack_cur + 1)) 81#define PACK_NAME ((char *)(pack_cur + 1))
187#define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1) 82#define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1)
188#define PACK_VALID pack_cur->type 83#define PACK_VALID pack_cur->type
189 84
190static void 85static void
191pack_next (void) 86pack_next (void)
192{ 87{
193 unsigned int d = u_32 (pack_cur->datalen); 88 unsigned int d = u_32 (pack_cur->datalen);
194 89
195 pack_cur = (struct hdr *)(PACK_DATA + d); 90 pack_cur = (struct u_pack_hdr *)(PACK_DATA + d);
196} 91}
197 92
198static void 93static void
199pack_unmap (void) 94pack_unmap (void)
200{ 95{
201 if (!pack_base) 96 if (pack_base)
202 return; 97 {
203
204#ifdef _WIN32
205 UnmapViewOfFile (pack_base);
206#else
207 munmap (pack_base, pack_end - pack_base); 98 u_munmap (pack_base, pack_end - pack_base);
208#endif 99 pack_base = 0;
100 }
101
102 if (scratch)
103 {
104 u_free (scratch, scratch_size);
105 scratch = 0;
106 }
209} 107}
210 108
211static int 109static int
212pack_map (void) 110pack_map (void)
213{ 111{
112 char *addr;
113 unsigned int size;
114
214#ifdef _WIN32 115#ifdef _WIN32
215 BY_HANDLE_FILE_INFORMATION fi; 116 BY_HANDLE_FILE_INFORMATION fi;
216 117
217 if (!GetFileInformationByHandle (pack_handle, &fi)) 118 if (!GetFileInformationByHandle (pack_handle, &fi))
218 return 0; 119 return 0;
219 120
220 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)
221 return 0; 128 return 0;
222 129
223 HANDLE handle = CreateFileMapping (pack_handle, 0, PAGE_READONLY, 0, fi.nFileSizeLow, NULL);
224
225 if (!handle)
226 return 0;
227
228 pack_unmap (); 130 pack_unmap ();
229 131
230 pack_base = MapViewOfFile (handle, FILE_MAP_READ, 0, 0, fi.nFileSizeLow);
231
232 CloseHandle (handle);
233
234 pack_end = pack_base + fi.nFileSizeLow;
235#else
236 off_t len = lseek (pack_handle - 1, 0, SEEK_END);
237
238 pack_unmap ();
239
240 pack_base = mmap (0, len, PROT_READ, MAP_SHARED, pack_handle - 1, 0);
241 if (pack_base == (void *)-1)
242 pack_base = 0; 132 pack_base = addr;
243
244 pack_end = pack_base + len; 133 pack_end = pack_base + size;
245#endif
246 134
247 if (!pack_base)
248 return 0;
249
250 struct tail *tail; 135 struct u_pack_tail *tail;
251 136
252 tail = (void *)(pack_end - sizeof (*tail)); 137 tail = (void *)(pack_end - sizeof (*tail));
253 138
254 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) 139 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1))
255 return 0; 140 return 0;
256 141
257 pack_cur = (struct hdr *)(pack_end - u_32 (tail->size)); 142 pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (tail->size));
143
144 scratch = u_malloc (scratch_size = u_32 (tail->max_uncompressed));
145 if (!scratch)
146 return 0;
258 147
259 return 1; 148 return 1;
260} 149}
261 150
262static void 151static void
263exe_info (void) 152exe_info (void)
264{ 153{
265 if (!pack_map ()) 154 if (!pack_map ())
266 fatal ("unable to locate packfile in executable - executable corrupted?"); 155 u_fatal ("unable to locate packfile in executable - executable corrupted?");
267 156
268 if (pack_cur->type != T_META) 157 if (pack_cur->type != T_META)
269 fatal ("unable to locate executable metadata - executable corrupted?"); 158 u_fatal ("unable to locate executable metadata - executable corrupted?");
270 159
271 strcpy (exe_id, PACK_NAME); 160 strcpy (exe_id, PACK_NAME);
272} 161}
273 162
274static void 163static void
275load (void) 164load (void)
276{ 165{
277 u_mkdir (datadir);
278
279 strcpy (exe_dir, datadir);
280 u_append (exe_dir, exe_id);
281 u_mkdir (exe_dir);
282
283 if (u_chdir (exe_dir)) 166 if (u_chdir (exe_dir))
284 fatal ("unable to change to application data directory"); 167 u_fatal ("unable to change to application data directory");
285 168
286 u_handle h = u_open ("override"); 169 u_handle h = u_open ("override");
287 if (h) 170 if (u_valid (h))
288 { 171 {
289 u_handle oh = pack_handle; 172 u_handle oh = pack_handle;
290 173
291 pack_handle = h; 174 pack_handle = h;
292 if (!pack_map ()) 175 if (pack_map ())
176 u_setenv ("URLADER_OVERRIDE", "override");
177 else
293 { 178 {
294 pack_handle = oh; 179 pack_handle = oh;
295 oh = h; 180 oh = h;
296 } 181 }
297 182
298 u_close (oh); 183 u_close (oh);
299 } 184 }
300 185
301 if (pack_cur->type != T_META) 186 if (pack_cur->type != T_META)
302 fatal ("unable to locate override metadata"); 187 u_fatal ("unable to locate override metadata");
303 188
304 strcpy (exe_ver, PACK_DATA); 189 strcpy (exe_ver, PACK_DATA);
190 u_set_exe_info ();
305 pack_next (); 191 pack_next ();
306 192
193 for (;;)
194 {
195 if (pack_cur->type == T_ENV)
196 u_setenv (PACK_NAME, PACK_DATA);
307 while (pack_cur->type == T_ARG) 197 else if (pack_cur->type == T_ARG)
308 { 198 {
309 exe_argv [exe_argc++] = strdup (PACK_NAME); 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 }
204 else
205 break;
206
310 pack_next (); 207 pack_next ();
311 } 208 }
209
210done_env_arg:
312 211
313 while (pack_cur->type == T_ENV) 212 strcat (strcpy (tmppath, execdir), ".lck");
314 { 213 lock_handle = u_lock (tmppath, 0, 1);
315 u_setenv (PACK_NAME, PACK_DATA); 214 if (!lock_handle)
316 pack_next (); 215 u_fatal ("unable to lock application instance");
317 }
318
319 strcpy (execdir, exe_dir);
320 u_append (execdir, "i-");
321 strcat (execdir, exe_ver);
322 216
323 if (access (execdir, F_OK)) 217 if (access (execdir, F_OK))
324 { 218 {
325 // does not exist yet, so unpack and move 219 // does not exist yet, so unpack and move
326 tmpdir (exe_dir); 220 tmpdir (exe_dir);
327 221
328 if (u_chdir (tmppath)) 222 if (u_chdir (tmppath))
329 fatal ("unable to change to new instance directory"); 223 u_fatal ("unable to change to new instance directory");
330 224
331 for (;;) 225 for (;;)
332 { 226 {
333 switch (pack_cur->type) 227 switch (pack_cur->type)
334 { 228 {
337 break; 231 break;
338 232
339 case T_FILE: 233 case T_FILE:
340 { 234 {
341 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);
342 unsigned int len = u_32 (pack_cur->datalen); 236 unsigned int dlen, len = u_32 (pack_cur->datalen);
237 char *data = PACK_DATA;
343 238
239 if (pack_cur->flags & F_LZF)
240 if (dlen = lzf_decompress (data, len, scratch, scratch_size))
241 {
242 data = scratch;
243 len = dlen;
244 }
245 else
246 u_fatal ("unable to uncompress file data - pack corrupted?");
247
344 if (!h) 248 if (!u_valid (h))
345 fatal ("unable to unpack file from packfile - disk full?"); 249 u_fatal ("unable to unpack file from packfile - disk full?");
346 250
347 if (u_write (h, PACK_DATA, len) != len) 251 if (u_write (h, data, len) != len)
348 fatal ("unable to unpack file from packfile - disk full?"); 252 u_fatal ("unable to unpack file from packfile - disk full?");
349 253
254 u_fsync (h);
350 u_close (h); 255 u_close (h);
351 } 256 }
352 break; 257 break;
353 258
354 case T_NULL: 259 case T_NULL:
358 pack_next (); 263 pack_next ();
359 } 264 }
360 265
361done: 266done:
362 if (u_chdir (datadir)) 267 if (u_chdir (datadir))
363 fatal ("unable to change to data directory"); 268 u_fatal ("unable to change to data directory");
269
270 u_sync ();
364 271
365 if (u_rename (tmppath, execdir)) 272 if (u_rename (tmppath, execdir))
366 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
367
368 if (u_chdir (execdir))
369 fatal ("unable to change to application instance directory");
370 } 274 }
371 275
372 pack_unmap (); 276 pack_unmap ();
373 u_close (pack_handle); 277 u_close (pack_handle);
374 278
279 if (u_chdir (execdir))
280 u_fatal ("unable to change to application instance directory");
281
375 u_setenv ("URLADER_VERSION", URLADER_VERSION); 282 u_setenv ("URLADER_VERSION", URLADER_VERSION);
376 u_setenv ("URLADER_DATADIR", datadir);
377 u_setenv ("URLADER_EXECDIR", execdir);
378 u_setenv ("URLADER_EXE_ID" , exe_id);
379 u_setenv ("URLADER_EXE_DIR", exe_dir);
380 u_setenv ("URLADER_EXE_VER", exe_ver);
381 283
284#if 0
382 // yes, this is overkill 285 // yes, this is overkill
383 u_setenv ("SHLIB_PATH" , execdir); // hpux 286 u_setenv ("SHLIB_PATH" , execdir); // hpux
384 u_setenv ("LIBPATH" , execdir); // aix 287 u_setenv ("LIBPATH" , execdir); // aix
385 u_setenv ("LD_LIBRARY_PATH" , execdir); // most elf systems 288 u_setenv ("LD_LIBRARY_PATH" , execdir); // most elf systems
386 u_setenv ("LD_LIBRARY_PATH_32", execdir); // solaris 289 u_setenv ("LD_LIBRARY_PATH_32", execdir); // solaris
387 u_setenv ("LD_LIBRARY_PATH_64", execdir); // solaris 290 u_setenv ("LD_LIBRARY_PATH_64", execdir); // solaris
388 u_setenv ("LD_LIBRARYN32_PATH", execdir); // irix 291 u_setenv ("LD_LIBRARYN32_PATH", execdir); // irix
389 u_setenv ("LD_LIBRARY64_PATH" , execdir); // irix 292 u_setenv ("LD_LIBRARY64_PATH" , execdir); // irix
390 u_setenv ("DYLD_LIBRARY_PATH" , execdir); // os sucks from apple 293 u_setenv ("DYLD_LIBRARY_PATH" , execdir); // os sucks from apple
294#endif
295}
296
297static void
298execute (void)
299{
300 exe_argv [exe_argc] = 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 ();
391} 315}
392 316
393#ifdef _WIN32 317#ifdef _WIN32
394 318
395int APIENTRY 319int APIENTRY
396WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) 320WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show)
397{ 321{
398 if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) 322 if (!GetModuleFileName (hI, tmppath, sizeof (tmppath)))
399 fatal ("unable to find executable pack"); 323 u_fatal ("unable to find executable pack");
400 324
401 if (!(pack_handle = u_open (tmppath))) 325 pack_handle = u_open (tmppath);
326 if (!u_valid (pack_handle))
402 fatal ("unable to open executable pack"); 327 u_fatal ("unable to open executable pack");
403
404 exe_info ();
405 328
406 if (!GetCurrentDirectory (sizeof (currdir), currdir)) 329 if (!GetCurrentDirectory (sizeof (currdir), currdir))
407 strcpy (currdir, "."); 330 strcpy (currdir, ".");
408 331
409 if (SHGetFolderPath (0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, datadir) != S_OK) 332 doit ();
410 fatal ("unable to find application data directory");
411
412 u_mkdir (datadir);
413 u_append (datadir, URLADER);
414
415 load ();
416 execute ();
417 333
418 return 0; 334 return 0;
419} 335}
420 336
421#else 337#else
422 338
423int 339int
424main (int argc, char *argv[]) 340main (int argc, char *argv[])
425{ 341{
426 char *home = getenv ("HOME");
427
428 // we assume fd 0 and 2 are "normal"
429
430 if (!(pack_handle = u_open (argv [0]))) 342 pack_handle = u_open (argv [0]);
343 if (!u_valid (pack_handle))
431 fatal ("unable to open executable pack"); 344 u_fatal ("unable to open executable pack");
432
433 exe_info ();
434
435 if (!home)
436 {
437 struct passwd *pw;
438
439 if ((pw = getpwuid (getuid ())))
440 home = pw->pw_dir;
441 else
442 home = "/tmp";
443 }
444 345
445 if (!getcwd (currdir, sizeof (currdir))) 346 if (!getcwd (currdir, sizeof (currdir)))
446 strcpy (currdir, "."); 347 strcpy (currdir, ".");
447 348
448 u_mkdir (home); 349#if 0
449 sprintf (datadir, "%s/.%s", home, URLADER); 350 /* intersperse hostname, for whatever reason */
450 u_mkdir (datadir);
451 351
452 if (gethostname (tmppath, sizeof (tmppath))) 352 if (gethostname (tmppath, sizeof (tmppath)))
453 strcpy (tmppath, "default"); 353 strcpy (tmppath, "default");
454 354
455 u_append (datadir, tmppath); 355 u_append (datadir, tmppath);
356#endif
456 357
457 load (); 358 doit ();
458 execute ();
459 359
460 return 0; 360 return 0;
461} 361}
462 362
463#endif 363#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines