--- Urlader/urlader.c 2011/12/30 12:21:28 1.6 +++ Urlader/urlader.c 2012/01/05 06:00:13 1.11 @@ -1,10 +1,5 @@ -#ifndef URLADER -# define URLADER "urlader" -#endif -#define URLADER_VERSION "1.0" // a decimal number, not a version string - -#define MAX_ARGC 32 -#define MAX_ARGS 256 +#include "urlib.h" +#include "urlib.c" #include #include @@ -12,102 +7,13 @@ #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_invalid_handle 0 - #define u_valid(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_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_fsync(handle) FlushFileBuffers (handle) - #define u_sync() - - #define u_lockfile(path) CreateFile (path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL) - #define u_cloexec(handle) - // acquire shared in addition to excl, then unlock excl - #define u_lock_write_to_read(wait) u_lock (U_LOCK_READ, 1); u_lock (U_UNLOCK, 1); - -#else - - #define _GNU_SOURCE 1 - #define _BSD_SOURCE 1 - // the above increases our chances of getting MAP_ANONYMOUS - - #include - #include - #include - - #if defined (MAP_ANON) && !defined (MAP_ANONYMOUS) - #define MAP_ANONYMOUS MAP_ANON - #endif - - #ifdef PATH_MAX - #define MAX_PATH (PATH_MAX < 4096 ? 4096 : PATH_MAX) - #else - #define MAX_PATH 4096 - #endif - - #define u_handle int - #define u_invalid_handle -1 - #define u_valid(fd) ((fd) >= 0) - - #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) - #define u_creat(path,exec) open (path, O_WRONLY | O_CREAT | O_TRUNC, (exec) ? 0777 : 0666) - #define u_close(handle) close (handle) - #define u_append(path,add) strcat (strcat (path, "/"), add) - #define u_write(handle,data,len) write (handle, data, len) - - // on a mostly idle system, a sync at the end is certainly faster, hope for the best - #define u_fsync(handle) - #define u_sync() sync () - - #define u_lockfile(path) open (path, O_RDWR | O_CREAT, 0666) - #define u_cloexec(handle) fcntl (handle, F_SETFD, FD_CLOEXEC) - #define u_lock_write_to_read(wait) u_lock (U_LOCK_READ, 1); - -#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]) - /* 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 -static char exe_dir[MAX_PATH]; // %AppData%/urlader/EXE_ID -static char execdir[MAX_PATH]; // %AppData%/urlader/EXE_ID/EXE_VER -static char exe_id[MAX_PATH]; -static char exe_ver[MAX_PATH]; static int exe_argc; static const char *exe_argv[MAX_ARGC]; @@ -115,92 +21,6 @@ static unsigned int exe_argo; static void -fatal (const char *msg) -{ -#ifdef _WIN32 - MessageBox (0, msg, URLADER, 0); -#else - write (2, URLADER ": ", sizeof (URLADER ": ") - 1); - write (2, msg, strlen (msg)); - write (2, "\n", 1); -#endif - - _exit (1); -} - -static void * -u_malloc (unsigned int size) -{ - void *addr; - -#ifdef _WIN32 - HANDLE handle = CreateFileMapping (0, 0, PAGE_READWRITE, 0, size, NULL); - - if (!handle) - return 0; - - addr = MapViewOfFile (handle, FILE_MAP_WRITE, 0, 0, size); - - CloseHandle (handle); -#elif defined (MAP_ANONYMOUS) - addr = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); - - if (addr == (void *)-1) - addr = 0; -#else - addr = malloc (size); -#endif - - return addr; -} - -static void -u_free (void *addr, unsigned int size) -{ -#ifdef _WIN32 - UnmapViewOfFile (addr); -#elif defined (MAP_ANONYMOUS) - munmap (addr, size); -#else - free (addr); -#endif -} - -static void * -u_mmap (u_handle h, unsigned int size) -{ - void *addr; - -#ifdef _WIN32 - HANDLE handle = CreateFileMapping (h, 0, PAGE_READONLY, 0, size, NULL); - - if (!handle) - return 0; - - addr = MapViewOfFile (handle, FILE_MAP_READ, 0, 0, size); - - CloseHandle (handle); -#else - addr = mmap (0, size, PROT_READ, MAP_SHARED, h, 0); - - if (addr == (void *)-1) - addr = 0; -#endif - - return addr; -} - -static void -u_munmap (void *addr, unsigned int len) -{ -#ifdef _WIN32 - UnmapViewOfFile (addr); -#else - munmap (addr, len); -#endif -} - -static void tmpdir (const char *dir) { static int cnt; @@ -218,43 +38,6 @@ } } -enum -{ - U_UNLOCK, - U_LOCK_READ, - U_LOCK_WRITE -}; - -static int -u_lock (int mode, int dowait) -{ -#ifdef _WIN32 - if (mode == U_UNLOCK) - return !UnlockFile (lock_handle, 0, 0, 1, 0); - else - { - OVERLAPPED ov = { 0 }; - - return !LockFileEx (lock_handle, - 0 - | (mode == U_LOCK_WRITE ? LOCKFILE_EXCLUSIVE_LOCK : 0) - | (dowait ? 0 : LOCKFILE_FAIL_IMMEDIATELY), - 0, - 1, 0, - &ov); - } -#else - static short mode2lck [] = { F_UNLCK, F_RDLCK, F_WRLCK }; - struct flock lck; - lck.l_type = mode2lck [mode]; - lck.l_whence = SEEK_SET; - lck.l_start = 0; - lck.l_len = 1; - - return fcntl (lock_handle, dowait ? F_SETLKW : F_SETLK, &lck); -#endif -} - static void systemv (const char *const argv[]) { @@ -264,7 +47,7 @@ pid_t pid = fork (); if (pid < 0) - fatal ("fork failure"); + u_fatal ("fork failure"); if (!pid) { @@ -290,40 +73,8 @@ systemv (argv); } -enum -{ - T_NULL, // 5 - T_META, // 1 : exe_id, exe_ver - T_ENV, // 2 : name, value - T_ARG, // 3 : arg - 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_size; @@ -336,7 +87,7 @@ { 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 @@ -381,16 +132,16 @@ 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)); - scratch = u_malloc (scratch_size = u_32 (tail->max_filesize)); + scratch = u_malloc (scratch_size = u_32 (tail->max_uncompressed)); if (!scratch) return 0; @@ -401,10 +152,10 @@ exe_info (void) { if (!pack_map ()) - fatal ("unable to locate packfile in executable - executable corrupted?"); + u_fatal ("unable to locate packfile in executable - executable corrupted?"); if (pack_cur->type != T_META) - fatal ("unable to locate executable metadata - executable corrupted?"); + u_fatal ("unable to locate executable metadata - executable corrupted?"); strcpy (exe_id, PACK_NAME); } @@ -412,14 +163,8 @@ static void load (void) { - u_mkdir (datadir); - - strcpy (exe_dir, datadir); - u_append (exe_dir, exe_id); - u_mkdir (exe_dir); - if (u_chdir (exe_dir)) - fatal ("unable to change to application data directory"); + u_fatal ("unable to change to application data directory"); u_handle h = u_open ("override"); if (u_valid (h)) @@ -427,7 +172,9 @@ u_handle oh = pack_handle; pack_handle = h; - if (!pack_map ()) + if (pack_map ()) + u_setenv ("URLADER_OVERRIDE", "override"); + else { pack_handle = oh; oh = h; @@ -437,9 +184,10 @@ } if (pack_cur->type != T_META) - fatal ("unable to locate override metadata"); + u_fatal ("unable to locate override metadata"); strcpy (exe_ver, PACK_DATA); + u_set_exe_info (); pack_next (); for (;;) @@ -460,21 +208,11 @@ } done_env_arg: - strcpy (execdir, exe_dir); - u_append (execdir, "i-"); - strcat (execdir, exe_ver); strcat (strcpy (tmppath, execdir), ".lck"); - lock_handle = u_lockfile (tmppath); + lock_handle = u_lock (tmppath, 0, 1); if (!lock_handle) - fatal ("unable to create lock file"); - - u_cloexec (lock_handle); - - // all users that uee an instance acquire a shared lock - // acquiring an exclusive lock is enough to delete the dir - // deleeting the lockfile is another topic altogether - u_lock (U_LOCK_READ, 1); + u_fatal ("unable to lock application instance"); if (access (execdir, F_OK)) { @@ -482,7 +220,7 @@ tmpdir (exe_dir); if (u_chdir (tmppath)) - fatal ("unable to change to new instance directory"); + u_fatal ("unable to change to new instance directory"); for (;;) { @@ -505,13 +243,13 @@ len = dlen; } else - fatal ("unable to uncompress file data - pack corrupted?"); + u_fatal ("unable to uncompress file data - pack corrupted?"); if (!u_valid (h)) - fatal ("unable to unpack file from packfile - disk full?"); + u_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_fatal ("unable to unpack file from packfile - disk full?"); u_fsync (h); u_close (h); @@ -527,7 +265,7 @@ done: if (u_chdir (datadir)) - fatal ("unable to change to data directory"); + u_fatal ("unable to change to data directory"); u_sync (); @@ -539,14 +277,9 @@ u_close (pack_handle); if (u_chdir (execdir)) - fatal ("unable to change to application instance directory"); + u_fatal ("unable to change to application instance directory"); u_setenv ("URLADER_VERSION", URLADER_VERSION); - u_setenv ("URLADER_DATADIR", datadir); - u_setenv ("URLADER_EXECDIR", execdir); - u_setenv ("URLADER_EXE_ID" , exe_id); - u_setenv ("URLADER_EXE_DIR", exe_dir); - u_setenv ("URLADER_EXE_VER", exe_ver); #if 0 // yes, this is overkill @@ -568,31 +301,35 @@ systemv (exe_argv); } +static void +doit (void) +{ + u_set_datadir (); + u_mkdir (datadir); + + exe_info (); + u_set_exe_info (); + + load (); + execute (); +} + #ifdef _WIN32 int APIENTRY WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) { if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) - fatal ("unable to find executable pack"); + u_fatal ("unable to find executable pack"); pack_handle = u_open (tmppath); if (!u_valid (pack_handle)) - fatal ("unable to open executable pack"); - - exe_info (); + u_fatal ("unable to open executable pack"); if (!GetCurrentDirectory (sizeof (currdir), currdir)) strcpy (currdir, "."); - if (SHGetFolderPath (0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, datadir) != S_OK) - fatal ("unable to find application data directory"); - - u_mkdir (datadir); - u_append (datadir, URLADER); - - load (); - execute (); + doit (); return 0; } @@ -602,41 +339,23 @@ int main (int argc, char *argv[]) { - char *home = getenv ("HOME"); - pack_handle = u_open (argv [0]); if (!u_valid (pack_handle)) - fatal ("unable to open executable pack"); - - exe_info (); - - if (!home) - { - struct passwd *pw; - - if ((pw = getpwuid (getuid ()))) - home = pw->pw_dir; - else - home = "/tmp"; - } + u_fatal ("unable to open executable pack"); if (!getcwd (currdir, sizeof (currdir))) strcpy (currdir, "."); - u_mkdir (home); - //strcat (strcat (strcpy (datadir, home), "/."), URLADER); - sprintf (datadir, "%s/.%s", home, URLADER); - u_mkdir (datadir); - #if 0 + /* intersperse hostname, for whatever reason */ + if (gethostname (tmppath, sizeof (tmppath))) strcpy (tmppath, "default"); u_append (datadir, tmppath); #endif - load (); - execute (); + doit (); return 0; }