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.12 by root, Fri Jan 6 07:56:36 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 u_dynbuf exe_argv;
27static char exe_args[MAX_ARGS]; /* actual arguments strings copied here */ 20static u_dynbuf exe_args; /* actual arguments strings copied here */
28static 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 21
44static void 22static void
45tmpdir (const char *dir) 23tmpdir (const char *dir)
46{ 24{
47 static int cnt; 25 static int cnt;
66 _spawnv (P_WAIT, argv [0], argv); 44 _spawnv (P_WAIT, argv [0], argv);
67#else 45#else
68 pid_t pid = fork (); 46 pid_t pid = fork ();
69 47
70 if (pid < 0) 48 if (pid < 0)
71 fatal ("fork failure"); 49 u_fatal ("fork failure");
72 50
73 if (!pid) 51 if (!pid)
74 { 52 {
75 execv (argv [0], (void *)argv); 53 execv (argv [0], (void *)argv);
76 _exit (124); 54 _exit (124);
93 #endif 71 #endif
94 systemv (argv); 72 systemv (argv);
95} 73}
96 74
97static char *pack_base, *pack_end; 75static char *pack_base, *pack_end;
76static struct u_pack_tail *pack_tail;
98static struct u_pack_hdr *pack_cur; 77static struct u_pack_hdr *pack_cur;
99static char *scratch; 78static char *scratch;
100static unsigned int scratch_size; 79static unsigned int scratch_size;
101 80
102#define PACK_NAME ((char *)(pack_cur + 1)) 81#define PACK_NAME ((char *)(pack_cur + 1))
146 125
147 addr = u_mmap (pack_handle, size); 126 addr = u_mmap (pack_handle, size);
148 if (!addr) 127 if (!addr)
149 return 0; 128 return 0;
150 129
151 pack_unmap (); 130 /*pack_unmap ();*/
152 131
153 pack_base = addr; 132 pack_base = addr;
154 pack_end = pack_base + size; 133 pack_end = pack_base + size;
155 134
156 struct u_pack_tail *tail;
157
158 tail = (void *)(pack_end - sizeof (*tail)); 135 pack_tail = (void *)(pack_end - sizeof (*pack_tail));
159 136
160 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) 137 if (memcmp (pack_tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1))
161 return 0; 138 return 0;
162 139
163 pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (tail->size)); 140 pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (pack_tail->size));
164 141
142 if (pack_cur->type != T_META)
143 return 0;
144
165 scratch = u_malloc (scratch_size = u_32 (tail->max_filesize)); 145 scratch = u_malloc (scratch_size = u_32 (pack_tail->max_uncompressed));
166 if (!scratch) 146 if (!scratch)
167 return 0; 147 return 0;
168 148
169 return 1; 149 return 1;
170} 150}
171 151
172static void 152static void
173exe_info (void) 153add_arg (char *arg, unsigned int len)
174{ 154{
175 if (!pack_map ()) 155 char *addr = u_dynbuf_append (&exe_args, arg, len);
176 fatal ("unable to locate packfile in executable - executable corrupted?"); 156 u_dynbuf_append (&exe_argv, &addr, sizeof (addr));
177
178 if (pack_cur->type != U_T_META)
179 fatal ("unable to locate executable metadata - executable corrupted?");
180
181 strcpy (exe_id, PACK_NAME);
182} 157}
183 158
184static void 159static void
185load (void) 160load (void)
186{ 161{
187 u_mkdir (datadir); 162 strcpy (exe_id , PACK_NAME);
188 163 strcpy (exe_ver, PACK_DATA);
189 strcpy (exe_dir, datadir); 164 u_set_exe_info ();
190 u_append (exe_dir, exe_id);
191 u_mkdir (exe_dir);
192 165
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
174 pack_unmap ();
201 pack_handle = h; 175 pack_handle = h;
176
202 if (!pack_map ()) 177 if (pack_map ()
178 && strcmp (exe_id , PACK_NAME) == 0
179 && strcmp (exe_ver, PACK_DATA) <= 0)
180 u_setenv ("URLADER_OVERRIDE", "override");
181 else
203 { 182 {
183 pack_unmap ();
204 pack_handle = oh; 184 pack_handle = oh;
205 oh = h; 185 oh = h;
186 pack_map ();
206 } 187 }
207 188
208 u_close (oh); 189 u_close (oh);
209 } 190 }
210 191
211 if (pack_cur->type != U_T_META)
212 fatal ("unable to locate override metadata");
213
214 strcpy (exe_ver, PACK_DATA); 192 strcpy (exe_ver, PACK_DATA);
193 u_set_exe_info ();
215 pack_next (); 194 pack_next ();
216 195
217 for (;;) 196 for (;;)
218 { 197 {
219 if (pack_cur->type == U_T_ENV) 198 if (pack_cur->type == T_ENV)
220 u_setenv (PACK_NAME, PACK_DATA); 199 u_setenv (PACK_NAME, PACK_DATA);
221 else if (pack_cur->type == U_T_ARG) 200 else if (pack_cur->type == T_ARG)
222 { 201 add_arg (PACK_NAME, u_16 (pack_cur->namelen) + 1);
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 }
228 else 202 else
229 break; 203 break;
230 204
231 pack_next (); 205 pack_next ();
232 } 206 }
233 207
234done_env_arg: 208done_env_arg:
235 strcpy (execdir, exe_dir);
236 u_append (execdir, "i-");
237 strcat (execdir, exe_ver);
238 209
239 strcat (strcpy (tmppath, execdir), ".lck"); 210 strcat (strcpy (tmppath, execdir), ".lck");
240 lock_handle = u_lock (tmppath, 0, 1); 211 lock_handle = u_lock (tmppath, 0, 1);
241 if (!lock_handle) 212 if (!lock_handle)
242 fatal ("unable to lock application instance"); 213 u_fatal ("unable to lock application instance");
243 214
244 if (access (execdir, F_OK)) 215 if (access (execdir, F_OK))
245 { 216 {
246 // does not exist yet, so unpack and move 217 // does not exist yet, so unpack and move
247 tmpdir (exe_dir); 218 tmpdir (exe_dir);
248 219
249 if (u_chdir (tmppath)) 220 if (u_chdir (tmppath))
250 fatal ("unable to change to new instance directory"); 221 u_fatal ("unable to change to new instance directory");
251 222
252 for (;;) 223 for (;;)
253 { 224 {
254 switch (pack_cur->type) 225 switch (pack_cur->type)
255 { 226 {
256 case U_T_DIR: 227 case T_DIR:
257 u_mkdir (PACK_NAME); 228 u_mkdir (PACK_NAME);
258 break; 229 break;
259 230
260 case U_T_FILE: 231 case T_FILE:
261 { 232 {
262 u_handle h = u_creat (PACK_NAME, pack_cur->flags & U_F_EXEC); 233 u_handle h = u_creat (PACK_NAME, pack_cur->flags & F_EXEC);
263 unsigned int dlen, len = u_32 (pack_cur->datalen); 234 unsigned int dlen, len = u_32 (pack_cur->datalen);
264 char *data = PACK_DATA; 235 char *data = PACK_DATA;
265 236
266 if (pack_cur->flags & U_F_LZF) 237 if (pack_cur->flags & F_LZF)
267 if (dlen = lzf_decompress (data, len, scratch, scratch_size)) 238 if (dlen = lzf_decompress (data, len, scratch, scratch_size))
268 { 239 {
269 data = scratch; 240 data = scratch;
270 len = dlen; 241 len = dlen;
271 } 242 }
272 else 243 else
273 fatal ("unable to uncompress file data - pack corrupted?"); 244 u_fatal ("unable to uncompress file data - pack corrupted?");
274 245
275 if (!u_valid (h)) 246 if (!u_valid (h))
276 fatal ("unable to unpack file from packfile - disk full?"); 247 u_fatal ("unable to unpack file from packfile - disk full?");
277 248
278 if (u_write (h, data, len) != len) 249 if (u_write (h, data, len) != len)
279 fatal ("unable to unpack file from packfile - disk full?"); 250 u_fatal ("unable to unpack file from packfile - disk full?");
280 251
281 u_fsync (h); 252 u_fsync (h);
282 u_close (h); 253 u_close (h);
283 } 254 }
284 break; 255 break;
285 256
286 case U_T_NULL: 257 case T_NULL:
287 goto done; 258 goto done;
288 } 259 }
289 260
290 pack_next (); 261 pack_next ();
291 } 262 }
292 263
293done: 264done:
294 if (u_chdir (datadir)) 265 if (u_chdir (datadir))
295 fatal ("unable to change to data directory"); 266 u_fatal ("unable to change to data directory");
296 267
297 u_sync (); 268 u_sync ();
298 269
299 if (u_rename (tmppath, execdir)) 270 if (u_rename (tmppath, execdir))
300 deltree (tmppath); // if move fails, delete new, assume other process created it independently 271 deltree (tmppath); // if move fails, delete new, assume other process created it independently
302 273
303 pack_unmap (); 274 pack_unmap ();
304 u_close (pack_handle); 275 u_close (pack_handle);
305 276
306 if (u_chdir (execdir)) 277 if (u_chdir (execdir))
307 fatal ("unable to change to application instance directory"); 278 u_fatal ("unable to change to application instance directory");
308 279
309 u_setenv ("URLADER_VERSION", URLADER_VERSION); 280 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 281
316#if 0 282#if 0
317 // yes, this is overkill 283 // yes, this is overkill
318 u_setenv ("SHLIB_PATH" , execdir); // hpux 284 u_setenv ("SHLIB_PATH" , execdir); // hpux
319 u_setenv ("LIBPATH" , execdir); // aix 285 u_setenv ("LIBPATH" , execdir); // aix
327} 293}
328 294
329static void 295static void
330execute (void) 296execute (void)
331{ 297{
332 exe_argv [exe_argc] = 0; 298 char *null = 0;
333 systemv (exe_argv); 299 u_dynbuf_append (&exe_argv, &null, sizeof (null));
300 systemv ((const char *const *)exe_argv.addr);
301}
302
303// this argc/argv is without argv [0]
304static void
305doit (int argc, char *argv[])
306{
307 int i;
308
309 u_setenv ("URLADER_CURRDIR", currdir);
310
311 u_set_datadir ();
312 u_mkdir (datadir);
313
314 if (!pack_map ())
315 u_fatal ("unable to map pack file - executable corrupted?");
316
317 load ();
318
319 while (argc--)
320 {
321 add_arg (*argv, strlen (*argv) + 1);
322 ++argv;
323 }
324
325 execute ();
334} 326}
335 327
336#ifdef _WIN32 328#ifdef _WIN32
337 329
338int APIENTRY 330int APIENTRY
339WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) 331WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show)
340{ 332{
341 if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) 333 if (!GetModuleFileName (hI, tmppath, sizeof (tmppath)))
342 fatal ("unable to find executable pack"); 334 u_fatal ("unable to find executable pack");
335
336 u_setenv ("URLADER_EXEPATH", tmppath);
343 337
344 pack_handle = u_open (tmppath); 338 pack_handle = u_open (tmppath);
345 if (!u_valid (pack_handle)) 339 if (!u_valid (pack_handle))
346 fatal ("unable to open executable pack"); 340 u_fatal ("unable to open executable pack");
347
348 exe_info ();
349 341
350 if (!GetCurrentDirectory (sizeof (currdir), currdir)) 342 if (!GetCurrentDirectory (sizeof (currdir), currdir))
351 strcpy (currdir, "."); 343 strcpy (currdir, ".");
352 344
353 if (SHGetFolderPath (0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, datadir) != S_OK) 345 doit (1, &argv);
354 fatal ("unable to find application data directory");
355
356 u_mkdir (datadir);
357 u_append (datadir, URLADER);
358
359 load ();
360 execute ();
361 346
362 return 0; 347 return 0;
363} 348}
364 349
365#else 350#else
366 351
367int 352int
368main (int argc, char *argv[]) 353main (int argc, char *argv[])
369{ 354{
370 char *home = getenv ("HOME"); 355 u_setenv ("URLADER_EXEPATH", argv [0]);
371 356
372 pack_handle = u_open (argv [0]); 357 pack_handle = u_open (argv [0]);
373 if (!u_valid (pack_handle)) 358 if (!u_valid (pack_handle))
374 fatal ("unable to open executable pack"); 359 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 360
388 if (!getcwd (currdir, sizeof (currdir))) 361 if (!getcwd (currdir, sizeof (currdir)))
389 strcpy (currdir, "."); 362 strcpy (currdir, ".");
390 363
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 364#if 0
365 /* intersperse hostname, for whatever reason */
366
397 if (gethostname (tmppath, sizeof (tmppath))) 367 if (gethostname (tmppath, sizeof (tmppath)))
398 strcpy (tmppath, "default"); 368 strcpy (tmppath, "default");
399 369
400 u_append (datadir, tmppath); 370 u_append (datadir, tmppath);
401#endif 371#endif
402 372
403 load (); 373 doit (argc - 1, argv + 1);
404 execute ();
405 374
406 return 0; 375 return 0;
407} 376}
408 377
409#endif 378#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines