--- Urlader/urlader.c 2011/12/29 10:39:34 1.3 +++ Urlader/urlader.c 2011/12/30 23:34:08 1.8 @@ -1,7 +1,4 @@ -#ifndef URLADER -# define URLADER "urlader" -#endif -#define URLADER_VERSION "1.0" +#include "urlib.c" #include #include @@ -9,64 +6,14 @@ #include #include #include -#include #include "liblzf/lzf_d.c" -#define TAIL_MAGIC "SCHMORPPACK0" - -#ifdef _WIN32 - - #include - //#include - #include - #include - #include - - static DWORD dword; - - #define u_handle HANDLE - #define u_setenv(name,value) SetEnvironmentVariable (name, value) - #define u_mkdir(path) !CreateDirectory (path, NULL) - #define u_chdir(path) !SetCurrentDirectory (path) - #define u_rename(fr,to) !MoveFile (fr, to) - #define u_open(path) CreateFile (path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) - #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) - #define u_close(handle) CloseHandle (handle) - #define u_append(path,add) PathAppend (path, add) - #define u_write(handle,data,len) (WriteFile (handle, data, len, &dword, 0) ? dword : -1) - #define u_sync() // use transacted I/O? - -#else - - #include - #include - #include - - #ifdef PATH_MAX - #define MAX_PATH (PATH_MAX < 4096 ? 4096 : PATH_MAX) - #else - #define MAX_PATH 4096 - #endif - - #define u_handle int - #define u_setenv(name,value) setenv (name, value, 1) - #define u_mkdir(path) mkdir (path, 0777) - #define u_chdir(path) chdir (path) - #define u_rename(fr,to) rename (fr, to) - #define u_open(path) open (path, O_RDONLY) + 1 - #define u_creat(path,exec) open (path, O_WRONLY | O_CREAT | O_TRUNC, (exec) ? 0777 : 0666) + 1 - #define u_close(handle) close (handle - 1) - #define u_append(path,add) strcat (strcat (path, "/"), add) - #define u_write(handle,data,len) write ((handle) - 1, data, len) - #define u_sync() sync () - -#endif - -#define u_16(ptr) (((ptr)[0] << 8) | (ptr)[1]) -#define u_32(ptr) (((ptr)[0] << 24) | ((ptr)[1] << 16) | ((ptr)[2] << 8) | (ptr)[3]) +#include "urlib.h" +/* some simple? dynamic memory management for paths might save ltos of ram */ static u_handle pack_handle; +static u_handle lock_handle; static char tmppath[MAX_PATH]; static char currdir[MAX_PATH]; static char datadir[MAX_PATH]; // %AppData%/urlader @@ -76,7 +23,9 @@ static char exe_ver[MAX_PATH]; static int exe_argc; -static const char *exe_argv[32]; +static const char *exe_argv[MAX_ARGC]; +static char exe_args[MAX_ARGS]; /* actual arguments strings copied here */ +static unsigned int exe_argo; static void fatal (const char *msg) @@ -84,7 +33,9 @@ #ifdef _WIN32 MessageBox (0, msg, URLADER, 0); #else - fprintf (stderr, "%s: %s\n", URLADER, msg); + write (2, URLADER ": ", sizeof (URLADER ": ") - 1); + write (2, msg, strlen (msg)); + write (2, "\n", 1); #endif _exit (1); @@ -109,12 +60,12 @@ } static void -systemv (const char *const argv[], int dowait) +systemv (const char *const argv[]) { #ifdef _WIN32 _spawnv (P_WAIT, argv [0], argv); #else - pid_t pid = dowait ? fork () : 0; + pid_t pid = fork (); if (pid < 0) fatal ("fork failure"); @@ -140,45 +91,13 @@ #else const char *argv[] = { "/bin/rm", "-rf", path, 0 }; #endif - systemv (argv, 1); + systemv (argv); } -enum -{ - T_NULL, // 5 - T_META, // 1 : exe_id, exe_ver - T_ARG, // 2 : arg - T_ENV, // 3 : name, value - T_DIR, // 4+: path - T_FILE, // 4+: path, data - T_NUM -}; - -enum -{ - F_EXEC = 1, - F_LZF = 2, - F_NULL = 0 -}; - -struct hdr -{ - unsigned char type; - unsigned char flags; - unsigned char namelen[2]; - unsigned char datalen[4]; -}; - -struct tail { - unsigned char max_filesize[4]; - unsigned char size[4]; - char magic[12]; -}; - static char *pack_base, *pack_end; -static struct hdr *pack_cur; +static struct u_pack_hdr *pack_cur; static char *scratch; -static unsigned int scratch_len; +static unsigned int scratch_size; #define PACK_NAME ((char *)(pack_cur + 1)) #define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1) @@ -189,74 +108,63 @@ { unsigned int d = u_32 (pack_cur->datalen); - pack_cur = (struct hdr *)(PACK_DATA + d); + pack_cur = (struct u_pack_hdr *)(PACK_DATA + d); } static void pack_unmap (void) { - if (!pack_base) - return; - - free (scratch); + if (pack_base) + { + u_munmap (pack_base, pack_end - pack_base); + pack_base = 0; + } -#ifdef _WIN32 - UnmapViewOfFile (pack_base); -#else - munmap (pack_base, pack_end - pack_base); -#endif + if (scratch) + { + u_free (scratch, scratch_size); + scratch = 0; + } } static int pack_map (void) { + char *addr; + unsigned int size; + #ifdef _WIN32 BY_HANDLE_FILE_INFORMATION fi; if (!GetFileInformationByHandle (pack_handle, &fi)) return 0; - if (fi.nFileSizeHigh) // too big - return 0; - - HANDLE handle = CreateFileMapping (pack_handle, 0, PAGE_READONLY, 0, fi.nFileSizeLow, NULL); + size = fi.nFileSizeLow; +#else + size = lseek (pack_handle, 0, SEEK_END); +#endif - if (!handle) + addr = u_mmap (pack_handle, size); + if (!addr) return 0; pack_unmap (); - pack_base = MapViewOfFile (handle, FILE_MAP_READ, 0, 0, fi.nFileSizeLow); - - CloseHandle (handle); - - pack_end = pack_base + fi.nFileSizeLow; -#else - off_t len = lseek (pack_handle - 1, 0, SEEK_END); - - pack_unmap (); - - pack_base = mmap (0, len, PROT_READ, MAP_SHARED, pack_handle - 1, 0); - if (pack_base == (void *)-1) - pack_base = 0; - - pack_end = pack_base + len; -#endif - - if (!pack_base) - return 0; + pack_base = addr; + pack_end = pack_base + size; - struct tail *tail; + struct u_pack_tail *tail; tail = (void *)(pack_end - sizeof (*tail)); if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) return 0; - pack_cur = (struct hdr *)(pack_end - u_32 (tail->size)); + pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (tail->size)); - free (scratch); - scratch = malloc (scratch_len = u_32 (tail->max_filesize)); + scratch = u_malloc (scratch_size = u_32 (tail->max_filesize)); + if (!scratch) + return 0; return 1; } @@ -267,7 +175,7 @@ if (!pack_map ()) fatal ("unable to locate packfile in executable - executable corrupted?"); - if (pack_cur->type != T_META) + if (pack_cur->type != U_T_META) fatal ("unable to locate executable metadata - executable corrupted?"); strcpy (exe_id, PACK_NAME); @@ -286,7 +194,7 @@ fatal ("unable to change to application data directory"); u_handle h = u_open ("override"); - if (h) + if (u_valid (h)) { u_handle oh = pack_handle; @@ -300,28 +208,39 @@ u_close (oh); } - if (pack_cur->type != T_META) + if (pack_cur->type != U_T_META) fatal ("unable to locate override metadata"); strcpy (exe_ver, PACK_DATA); pack_next (); - while (pack_cur->type == T_ARG) + for (;;) { - exe_argv [exe_argc++] = strdup (PACK_NAME); - pack_next (); - } + if (pack_cur->type == U_T_ENV) + u_setenv (PACK_NAME, PACK_DATA); + else if (pack_cur->type == U_T_ARG) + { + int len = u_16 (pack_cur->namelen) + 1; + exe_argv [exe_argc++] = exe_args + exe_argo; + memcpy (exe_args + exe_argo, PACK_NAME, len); + exe_argo += len; + } + else + break; - while (pack_cur->type == T_ENV) - { - u_setenv (PACK_NAME, PACK_DATA); pack_next (); } - + +done_env_arg: strcpy (execdir, exe_dir); u_append (execdir, "i-"); strcat (execdir, exe_ver); + strcat (strcpy (tmppath, execdir), ".lck"); + lock_handle = u_lock (tmppath, 0, 1); + if (!lock_handle) + fatal ("unable to lock application instance"); + if (access (execdir, F_OK)) { // does not exist yet, so unpack and move @@ -334,18 +253,18 @@ { switch (pack_cur->type) { - case T_DIR: + case U_T_DIR: u_mkdir (PACK_NAME); break; - case T_FILE: + case U_T_FILE: { - u_handle h = u_creat (PACK_NAME, pack_cur->flags & F_EXEC); + u_handle h = u_creat (PACK_NAME, pack_cur->flags & U_F_EXEC); unsigned int dlen, len = u_32 (pack_cur->datalen); char *data = PACK_DATA; - if (pack_cur->flags & F_LZF) - if (dlen = lzf_decompress (data, len, scratch, scratch_len)) + if (pack_cur->flags & U_F_LZF) + if (dlen = lzf_decompress (data, len, scratch, scratch_size)) { data = scratch; len = dlen; @@ -353,17 +272,18 @@ else fatal ("unable to uncompress file data - pack corrupted?"); - if (!h) + if (!u_valid (h)) fatal ("unable to unpack file from packfile - disk full?"); if (u_write (h, data, len) != len) fatal ("unable to unpack file from packfile - disk full?"); + u_fsync (h); u_close (h); } break; - case T_NULL: + case U_T_NULL: goto done; } @@ -393,6 +313,7 @@ u_setenv ("URLADER_EXE_DIR", exe_dir); u_setenv ("URLADER_EXE_VER", exe_ver); +#if 0 // yes, this is overkill u_setenv ("SHLIB_PATH" , execdir); // hpux u_setenv ("LIBPATH" , execdir); // aix @@ -402,13 +323,14 @@ u_setenv ("LD_LIBRARYN32_PATH", execdir); // irix u_setenv ("LD_LIBRARY64_PATH" , execdir); // irix u_setenv ("DYLD_LIBRARY_PATH" , execdir); // os sucks from apple +#endif } static void execute (void) { exe_argv [exe_argc] = 0; - systemv (exe_argv, 0); + systemv (exe_argv); } #ifdef _WIN32 @@ -419,7 +341,8 @@ if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) fatal ("unable to find executable pack"); - if (!(pack_handle = u_open (tmppath))) + pack_handle = u_open (tmppath); + if (!u_valid (pack_handle)) fatal ("unable to open executable pack"); exe_info (); @@ -446,9 +369,8 @@ { char *home = getenv ("HOME"); - // we assume fd 0 and 2 are "normal" - - if (!(pack_handle = u_open (argv [0]))) + pack_handle = u_open (argv [0]); + if (!u_valid (pack_handle)) fatal ("unable to open executable pack"); exe_info (); @@ -467,13 +389,16 @@ strcpy (currdir, "."); u_mkdir (home); + //strcat (strcat (strcpy (datadir, home), "/."), URLADER); sprintf (datadir, "%s/.%s", home, URLADER); u_mkdir (datadir); +#if 0 if (gethostname (tmppath, sizeof (tmppath))) strcpy (tmppath, "default"); u_append (datadir, tmppath); +#endif load (); execute ();