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.13 by root, Tue Jan 17 18:38:37 2012 UTC

1/*
2 * Copyright (c) 2012 Marc Alexander Lehmann <schmorp@schmorp.de>
3 *
4 * Redistribution and use in source and binary forms, with or without modifica-
5 * tion, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
16 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
18 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
22 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23 * OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * the GNU General Public License ("GPL") version 2 or any later version,
27 * in which case the provisions of the GPL are applicable instead of
28 * the above. If you wish to allow the use of your version of this file
29 * only under the terms of the GPL and not to allow others to use your
30 * version of this file under the BSD license, indicate your decision
31 * by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL. If you do not delete the
33 * provisions above, a recipient may use your version of this file under
34 * either the BSD or the GPL.
35 */
36
37#include "urlib.h"
1#include "urlib.c" 38#include "urlib.c"
2 39
3#include <stdio.h> 40#include <stdio.h>
4#include <stdlib.h> 41#include <stdlib.h>
5#include <unistd.h> 42#include <unistd.h>
7#include <string.h> 44#include <string.h>
8#include <time.h> 45#include <time.h>
9 46
10#include "liblzf/lzf_d.c" 47#include "liblzf/lzf_d.c"
11 48
12#include "urlib.h"
13
14/* some simple? dynamic memory management for paths might save ltos of ram */ 49/* some simple? dynamic memory management for paths might save ltos of ram */
15static u_handle pack_handle; 50static u_handle pack_handle;
16static u_handle lock_handle; 51static u_handle lock_handle;
17static char tmppath[MAX_PATH]; 52static 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 53
25static int exe_argc; 54static int exe_argc;
26static const char *exe_argv[MAX_ARGC]; 55static u_dynbuf exe_argv;
27static char exe_args[MAX_ARGS]; /* actual arguments strings copied here */ 56static 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 57
44static void 58static void
45tmpdir (const char *dir) 59tmpdir (const char *dir)
46{ 60{
47 static int cnt; 61 static int cnt;
66 _spawnv (P_WAIT, argv [0], argv); 80 _spawnv (P_WAIT, argv [0], argv);
67#else 81#else
68 pid_t pid = fork (); 82 pid_t pid = fork ();
69 83
70 if (pid < 0) 84 if (pid < 0)
71 fatal ("fork failure"); 85 u_fatal ("fork failure");
72 86
73 if (!pid) 87 if (!pid)
74 { 88 {
75 execv (argv [0], (void *)argv); 89 execv (argv [0], (void *)argv);
76 _exit (124); 90 _exit (124);
93 #endif 107 #endif
94 systemv (argv); 108 systemv (argv);
95} 109}
96 110
97static char *pack_base, *pack_end; 111static char *pack_base, *pack_end;
112static struct u_pack_tail *pack_tail;
98static struct u_pack_hdr *pack_cur; 113static struct u_pack_hdr *pack_cur;
99static char *scratch; 114static char *scratch;
100static unsigned int scratch_size; 115static unsigned int scratch_size;
101 116
102#define PACK_NAME ((char *)(pack_cur + 1)) 117#define PACK_NAME ((char *)(pack_cur + 1))
146 161
147 addr = u_mmap (pack_handle, size); 162 addr = u_mmap (pack_handle, size);
148 if (!addr) 163 if (!addr)
149 return 0; 164 return 0;
150 165
151 pack_unmap (); 166 /*pack_unmap ();*/
152 167
153 pack_base = addr; 168 pack_base = addr;
154 pack_end = pack_base + size; 169 pack_end = pack_base + size;
155 170
156 struct u_pack_tail *tail;
157
158 tail = (void *)(pack_end - sizeof (*tail)); 171 pack_tail = (void *)(pack_end - sizeof (*pack_tail));
159 172
160 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) 173 if (memcmp (pack_tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1))
161 return 0; 174 return 0;
162 175
163 pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (tail->size)); 176 pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (pack_tail->size));
164 177
178 if (pack_cur->type != T_META)
179 return 0;
180
165 scratch = u_malloc (scratch_size = u_32 (tail->max_filesize)); 181 scratch = u_malloc (scratch_size = u_32 (pack_tail->max_uncompressed));
166 if (!scratch) 182 if (!scratch)
167 return 0; 183 return 0;
168 184
169 return 1; 185 return 1;
170} 186}
171 187
172static void 188static void
173exe_info (void) 189add_arg (char *arg, unsigned int len)
174{ 190{
175 if (!pack_map ()) 191 char *addr = u_dynbuf_append (&exe_args, arg, len);
176 fatal ("unable to locate packfile in executable - executable corrupted?"); 192 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} 193}
183 194
184static void 195static void
185load (void) 196load (void)
186{ 197{
187 u_mkdir (datadir); 198 strcpy (exe_id , PACK_NAME);
188 199 strcpy (exe_ver, PACK_DATA);
189 strcpy (exe_dir, datadir); 200 u_set_exe_info ();
190 u_append (exe_dir, exe_id);
191 u_mkdir (exe_dir);
192 201
193 if (u_chdir (exe_dir)) 202 if (u_chdir (exe_dir))
194 fatal ("unable to change to application data directory"); 203 u_fatal ("unable to change to application data directory");
195 204
196 u_handle h = u_open ("override"); 205 u_handle h = u_open ("override");
197 if (u_valid (h)) 206 if (u_valid (h))
198 { 207 {
199 u_handle oh = pack_handle; 208 u_handle oh = pack_handle;
200 209
210 pack_unmap ();
201 pack_handle = h; 211 pack_handle = h;
212
202 if (!pack_map ()) 213 if (pack_map ()
214 && strcmp (exe_id , PACK_NAME) == 0
215 && strcmp (exe_ver, PACK_DATA) <= 0)
216 u_setenv ("URLADER_OVERRIDE", "override");
217 else
203 { 218 {
219 pack_unmap ();
204 pack_handle = oh; 220 pack_handle = oh;
205 oh = h; 221 oh = h;
222 pack_map ();
206 } 223 }
207 224
208 u_close (oh); 225 u_close (oh);
209 } 226 }
210 227
211 if (pack_cur->type != U_T_META)
212 fatal ("unable to locate override metadata");
213
214 strcpy (exe_ver, PACK_DATA); 228 strcpy (exe_ver, PACK_DATA);
229 u_set_exe_info ();
215 pack_next (); 230 pack_next ();
216 231
217 for (;;) 232 for (;;)
218 { 233 {
219 if (pack_cur->type == U_T_ENV) 234 if (pack_cur->type == T_ENV)
220 u_setenv (PACK_NAME, PACK_DATA); 235 u_setenv (PACK_NAME, PACK_DATA);
221 else if (pack_cur->type == U_T_ARG) 236 else if (pack_cur->type == T_ARG)
222 { 237 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 238 else
229 break; 239 break;
230 240
231 pack_next (); 241 pack_next ();
232 } 242 }
233 243
234done_env_arg: 244done_env_arg:
235 strcpy (execdir, exe_dir);
236 u_append (execdir, "i-");
237 strcat (execdir, exe_ver);
238 245
239 strcat (strcpy (tmppath, execdir), ".lck"); 246 strcat (strcpy (tmppath, execdir), ".lck");
240 lock_handle = u_lock (tmppath, 0, 1); 247 lock_handle = u_lock (tmppath, 0, 1);
241 if (!lock_handle) 248 if (!lock_handle)
242 fatal ("unable to lock application instance"); 249 u_fatal ("unable to lock application instance");
243 250
244 if (access (execdir, F_OK)) 251 if (access (execdir, F_OK))
245 { 252 {
246 // does not exist yet, so unpack and move 253 // does not exist yet, so unpack and move
247 tmpdir (exe_dir); 254 tmpdir (exe_dir);
248 255
249 if (u_chdir (tmppath)) 256 if (u_chdir (tmppath))
250 fatal ("unable to change to new instance directory"); 257 u_fatal ("unable to change to new instance directory");
251 258
252 for (;;) 259 for (;;)
253 { 260 {
254 switch (pack_cur->type) 261 switch (pack_cur->type)
255 { 262 {
256 case U_T_DIR: 263 case T_DIR:
257 u_mkdir (PACK_NAME); 264 u_mkdir (PACK_NAME);
258 break; 265 break;
259 266
260 case U_T_FILE: 267 case T_FILE:
261 { 268 {
262 u_handle h = u_creat (PACK_NAME, pack_cur->flags & U_F_EXEC); 269 u_handle h = u_creat (PACK_NAME, pack_cur->flags & F_EXEC);
263 unsigned int dlen, len = u_32 (pack_cur->datalen); 270 unsigned int dlen, len = u_32 (pack_cur->datalen);
264 char *data = PACK_DATA; 271 char *data = PACK_DATA;
265 272
266 if (pack_cur->flags & U_F_LZF) 273 if (pack_cur->flags & F_LZF)
267 if (dlen = lzf_decompress (data, len, scratch, scratch_size)) 274 if (dlen = lzf_decompress (data, len, scratch, scratch_size))
268 { 275 {
269 data = scratch; 276 data = scratch;
270 len = dlen; 277 len = dlen;
271 } 278 }
272 else 279 else
273 fatal ("unable to uncompress file data - pack corrupted?"); 280 u_fatal ("unable to uncompress file data - pack corrupted?");
274 281
275 if (!u_valid (h)) 282 if (!u_valid (h))
276 fatal ("unable to unpack file from packfile - disk full?"); 283 u_fatal ("unable to unpack file from packfile - disk full?");
277 284
278 if (u_write (h, data, len) != len) 285 if (u_write (h, data, len) != len)
279 fatal ("unable to unpack file from packfile - disk full?"); 286 u_fatal ("unable to unpack file from packfile - disk full?");
280 287
281 u_fsync (h); 288 u_fsync (h);
282 u_close (h); 289 u_close (h);
283 } 290 }
284 break; 291 break;
285 292
286 case U_T_NULL: 293 case T_NULL:
287 goto done; 294 goto done;
288 } 295 }
289 296
290 pack_next (); 297 pack_next ();
291 } 298 }
292 299
293done: 300done:
294 if (u_chdir (datadir)) 301 if (u_chdir (datadir))
295 fatal ("unable to change to data directory"); 302 u_fatal ("unable to change to data directory");
296 303
297 u_sync (); 304 u_sync ();
298 305
299 if (u_rename (tmppath, execdir)) 306 if (u_rename (tmppath, execdir))
300 deltree (tmppath); // if move fails, delete new, assume other process created it independently 307 deltree (tmppath); // if move fails, delete new, assume other process created it independently
302 309
303 pack_unmap (); 310 pack_unmap ();
304 u_close (pack_handle); 311 u_close (pack_handle);
305 312
306 if (u_chdir (execdir)) 313 if (u_chdir (execdir))
307 fatal ("unable to change to application instance directory"); 314 u_fatal ("unable to change to application instance directory");
308 315
309 u_setenv ("URLADER_VERSION", URLADER_VERSION); 316 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 317
316#if 0 318#if 0
317 // yes, this is overkill 319 // yes, this is overkill
318 u_setenv ("SHLIB_PATH" , execdir); // hpux 320 u_setenv ("SHLIB_PATH" , execdir); // hpux
319 u_setenv ("LIBPATH" , execdir); // aix 321 u_setenv ("LIBPATH" , execdir); // aix
327} 329}
328 330
329static void 331static void
330execute (void) 332execute (void)
331{ 333{
332 exe_argv [exe_argc] = 0; 334 char *null = 0;
333 systemv (exe_argv); 335 u_dynbuf_append (&exe_argv, &null, sizeof (null));
336 systemv ((const char *const *)exe_argv.addr);
337}
338
339// this argc/argv is without argv [0]
340static void
341doit (int argc, char *argv[])
342{
343 int i;
344
345 u_setenv ("URLADER_CURRDIR", currdir);
346
347 u_set_datadir ();
348 u_mkdir (datadir);
349
350 if (!pack_map ())
351 u_fatal ("unable to map pack file - executable corrupted?");
352
353 load ();
354
355 while (argc--)
356 {
357 add_arg (*argv, strlen (*argv) + 1);
358 ++argv;
359 }
360
361 execute ();
334} 362}
335 363
336#ifdef _WIN32 364#ifdef _WIN32
337 365
338int APIENTRY 366int APIENTRY
339WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) 367WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show)
340{ 368{
341 if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) 369 if (!GetModuleFileName (hI, tmppath, sizeof (tmppath)))
342 fatal ("unable to find executable pack"); 370 u_fatal ("unable to find executable pack");
371
372 u_setenv ("URLADER_EXEPATH", tmppath);
343 373
344 pack_handle = u_open (tmppath); 374 pack_handle = u_open (tmppath);
345 if (!u_valid (pack_handle)) 375 if (!u_valid (pack_handle))
346 fatal ("unable to open executable pack"); 376 u_fatal ("unable to open executable pack");
347
348 exe_info ();
349 377
350 if (!GetCurrentDirectory (sizeof (currdir), currdir)) 378 if (!GetCurrentDirectory (sizeof (currdir), currdir))
351 strcpy (currdir, "."); 379 strcpy (currdir, ".");
352 380
353 if (SHGetFolderPath (0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, datadir) != S_OK) 381 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 382
362 return 0; 383 return 0;
363} 384}
364 385
365#else 386#else
366 387
367int 388int
368main (int argc, char *argv[]) 389main (int argc, char *argv[])
369{ 390{
370 char *home = getenv ("HOME"); 391 u_setenv ("URLADER_EXEPATH", argv [0]);
371 392
372 pack_handle = u_open (argv [0]); 393 pack_handle = u_open (argv [0]);
373 if (!u_valid (pack_handle)) 394 if (!u_valid (pack_handle))
374 fatal ("unable to open executable pack"); 395 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 396
388 if (!getcwd (currdir, sizeof (currdir))) 397 if (!getcwd (currdir, sizeof (currdir)))
389 strcpy (currdir, "."); 398 strcpy (currdir, ".");
390 399
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 400#if 0
401 /* intersperse hostname, for whatever reason */
402
397 if (gethostname (tmppath, sizeof (tmppath))) 403 if (gethostname (tmppath, sizeof (tmppath)))
398 strcpy (tmppath, "default"); 404 strcpy (tmppath, "default");
399 405
400 u_append (datadir, tmppath); 406 u_append (datadir, tmppath);
401#endif 407#endif
402 408
403 load (); 409 doit (argc - 1, argv + 1);
404 execute ();
405 410
406 return 0; 411 return 0;
407} 412}
408 413
409#endif 414#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines