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.9 by root, Mon Jan 2 09:23:03 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 const char *exe_argv[MAX_ARGC];
27static char exe_args[MAX_ARGS]; /* actual arguments strings copied here */ 20static char exe_args[MAX_ARGS]; /* actual arguments strings copied here */
28static unsigned int exe_argo; 21static 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 22
44static void 23static void
45tmpdir (const char *dir) 24tmpdir (const char *dir)
46{ 25{
47 static int cnt; 26 static int cnt;
66 _spawnv (P_WAIT, argv [0], argv); 45 _spawnv (P_WAIT, argv [0], argv);
67#else 46#else
68 pid_t pid = fork (); 47 pid_t pid = fork ();
69 48
70 if (pid < 0) 49 if (pid < 0)
71 fatal ("fork failure"); 50 u_fatal ("fork failure");
72 51
73 if (!pid) 52 if (!pid)
74 { 53 {
75 execv (argv [0], (void *)argv); 54 execv (argv [0], (void *)argv);
76 _exit (124); 55 _exit (124);
171 150
172static void 151static void
173exe_info (void) 152exe_info (void)
174{ 153{
175 if (!pack_map ()) 154 if (!pack_map ())
176 fatal ("unable to locate packfile in executable - executable corrupted?"); 155 u_fatal ("unable to locate packfile in executable - executable corrupted?");
177 156
178 if (pack_cur->type != U_T_META) 157 if (pack_cur->type != T_META)
179 fatal ("unable to locate executable metadata - executable corrupted?"); 158 u_fatal ("unable to locate executable metadata - executable corrupted?");
180 159
181 strcpy (exe_id, PACK_NAME); 160 strcpy (exe_id, PACK_NAME);
182} 161}
183 162
184static void 163static void
185load (void) 164load (void)
186{ 165{
187 u_mkdir (datadir);
188
189 strcpy (exe_dir, datadir);
190 u_append (exe_dir, exe_id);
191 u_mkdir (exe_dir);
192
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;
206 } 179 }
207 180
208 u_close (oh); 181 u_close (oh);
209 } 182 }
210 183
211 if (pack_cur->type != U_T_META) 184 if (pack_cur->type != T_META)
212 fatal ("unable to locate override metadata"); 185 u_fatal ("unable to locate override metadata");
213 186
214 strcpy (exe_ver, PACK_DATA); 187 strcpy (exe_ver, PACK_DATA);
188 u_set_exe_info ();
215 pack_next (); 189 pack_next ();
216 190
217 for (;;) 191 for (;;)
218 { 192 {
219 if (pack_cur->type == U_T_ENV) 193 if (pack_cur->type == T_ENV)
220 u_setenv (PACK_NAME, PACK_DATA); 194 u_setenv (PACK_NAME, PACK_DATA);
221 else if (pack_cur->type == U_T_ARG) 195 else if (pack_cur->type == T_ARG)
222 { 196 {
223 int len = u_16 (pack_cur->namelen) + 1; 197 int len = u_16 (pack_cur->namelen) + 1;
224 exe_argv [exe_argc++] = exe_args + exe_argo; 198 exe_argv [exe_argc++] = exe_args + exe_argo;
225 memcpy (exe_args + exe_argo, PACK_NAME, len); 199 memcpy (exe_args + exe_argo, PACK_NAME, len);
226 exe_argo += len; 200 exe_argo += len;
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
331{ 297{
332 exe_argv [exe_argc] = 0; 298 exe_argv [exe_argc] = 0;
333 systemv (exe_argv); 299 systemv (exe_argv);
334} 300}
335 301
302static void
303doit (void)
304{
305 u_set_datadir ();
306 u_mkdir (datadir);
307
308 exe_info ();
309 u_set_exe_info ();
310
311 load ();
312 execute ();
313}
314
336#ifdef _WIN32 315#ifdef _WIN32
337 316
338int APIENTRY 317int APIENTRY
339WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) 318WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show)
340{ 319{
341 if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) 320 if (!GetModuleFileName (hI, tmppath, sizeof (tmppath)))
342 fatal ("unable to find executable pack"); 321 u_fatal ("unable to find executable pack");
343 322
344 pack_handle = u_open (tmppath); 323 pack_handle = u_open (tmppath);
345 if (!u_valid (pack_handle)) 324 if (!u_valid (pack_handle))
346 fatal ("unable to open executable pack"); 325 u_fatal ("unable to open executable pack");
347
348 exe_info ();
349 326
350 if (!GetCurrentDirectory (sizeof (currdir), currdir)) 327 if (!GetCurrentDirectory (sizeof (currdir), currdir))
351 strcpy (currdir, "."); 328 strcpy (currdir, ".");
352 329
353 if (SHGetFolderPath (0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, datadir) != S_OK)
354 fatal ("unable to find application data directory");
355
356 u_mkdir (datadir);
357 u_append (datadir, URLADER);
358
359 load ();
360 execute ();
361
362 return 0; 330 return 0;
363} 331}
364 332
365#else 333#else
366 334
367int 335int
368main (int argc, char *argv[]) 336main (int argc, char *argv[])
369{ 337{
370 char *home = getenv ("HOME");
371
372 pack_handle = u_open (argv [0]); 338 pack_handle = u_open (argv [0]);
373 if (!u_valid (pack_handle)) 339 if (!u_valid (pack_handle))
374 fatal ("unable to open executable pack"); 340 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 341
388 if (!getcwd (currdir, sizeof (currdir))) 342 if (!getcwd (currdir, sizeof (currdir)))
389 strcpy (currdir, "."); 343 strcpy (currdir, ".");
390
391 u_mkdir (home);
392 //strcat (strcat (strcpy (datadir, home), "/."), URLADER);
393 sprintf (datadir, "%s/.%s", home, URLADER);
394 u_mkdir (datadir);
395 344
396#if 0 345#if 0
397 if (gethostname (tmppath, sizeof (tmppath))) 346 if (gethostname (tmppath, sizeof (tmppath)))
398 strcpy (tmppath, "default"); 347 strcpy (tmppath, "default");
399 348
400 u_append (datadir, tmppath); 349 u_append (datadir, tmppath);
401#endif 350#endif
402 351
403 load (); 352 doit ();
404 execute ();
405 353
406 return 0; 354 return 0;
407} 355}
408 356
409#endif 357#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines