--- Urlader/urlader.c 2011/12/30 14:51:45 1.7 +++ Urlader/urlader.c 2011/12/30 23:34:08 1.8 @@ -1,10 +1,4 @@ -#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.c" #include #include @@ -12,90 +6,10 @@ #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) - -#else - - #define _GNU_SOURCE 1 - #define _BSD_SOURCE 1 - // the above increases our chances of getting MAP_ANONYMOUS - - #include - #include - #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) - -#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; @@ -127,78 +41,6 @@ _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) { @@ -217,86 +59,6 @@ } } -static u_handle -u_lock (const char *path, int excl, int dowait) -{ - u_handle h; - - h = u_lockfile (path); - if (!u_valid (h)) - return h; - - u_cloexec (h); - - for (;;) - { - int success; - - // acquire the lock -#ifdef _WIN32 - OVERLAPPED ov = { 0 }; - - success = LockFileEx (h, - (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0) - | (dowait ? 0 : LOCKFILE_FAIL_IMMEDIATELY), - 0, - 1, 0, - &ov); -#else - struct flock lck = { 0 }; - - lck.l_type = excl ? F_WRLCK : F_RDLCK; - lck.l_whence = SEEK_SET; - lck.l_len = 1; - - success = !fcntl (h, dowait ? F_SETLKW : F_SETLK, &lck); -#endif - - if (!success) - break; - - // we have the lock, now verify that the lockfile still exists - -#ifdef _WIN32 - // apparently, we have to open the file to get its info :( - BY_HANDLE_FILE_INFORMATION s1, s2; - u_handle h2 = u_lockfile (path); - if (!u_valid (h)) - break; - - success = GetFileInformationByHandle (h, &s1) - && GetFileInformationByHandle (h2, &s2); - - u_close (h2); - - if (!success) - break; - - success = s1.dwVolumeSerialNumber == s2.dwVolumeSerialNumber - && s1.nFileIndexHigh == s2.nFileIndexHigh - && s1.nFileIndexLow == s2.nFileIndexLow; -#else - struct stat s1, s2; - - if (fstat (h, &s1) || stat (path, &s2)) - break; - - success = s1.st_dev == s2.st_dev - && s1.st_ino == s2.st_ino; -#endif - - if (success) - return h; // lock successfully acquired - - // files differ, close and retry - should be very rare - u_close (h); - } - - // failure - u_close (h); - return u_invalid_handle; -} - static void systemv (const char *const argv[]) { @@ -332,40 +94,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; @@ -378,7 +108,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 @@ -423,14 +153,14 @@ 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)); if (!scratch) @@ -445,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); @@ -478,7 +208,7 @@ 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); @@ -486,9 +216,9 @@ for (;;) { - if (pack_cur->type == T_ENV) + if (pack_cur->type == U_T_ENV) u_setenv (PACK_NAME, PACK_DATA); - else if (pack_cur->type == T_ARG) + else if (pack_cur->type == U_T_ARG) { int len = u_16 (pack_cur->namelen) + 1; exe_argv [exe_argc++] = exe_args + exe_argo; @@ -523,17 +253,17 @@ { 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 (pack_cur->flags & U_F_LZF) if (dlen = lzf_decompress (data, len, scratch, scratch_size)) { data = scratch; @@ -553,7 +283,7 @@ } break; - case T_NULL: + case U_T_NULL: goto done; }