--- Urlader/urlader.c 2011/12/29 10:20:56 1.2 +++ Urlader/urlader.c 2011/12/30 09:34:20 1.5 @@ -1,7 +1,7 @@ #ifndef URLADER # define URLADER "urlader" #endif -#define URLADER_VERSION "1.0" +#define URLADER_VERSION "1.0" // a decimal number, not a version string #include #include @@ -26,6 +26,8 @@ static DWORD dword; #define u_handle HANDLE + #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) @@ -36,6 +38,9 @@ #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() + #else #include @@ -49,15 +54,21 @@ #endif #define u_handle int + #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) + 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_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) - 1, data, len) + #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 () #endif @@ -145,8 +156,8 @@ { T_NULL, // 5 T_META, // 1 : exe_id, exe_ver - T_ARG, // 2 : arg - T_ENV, // 3 : name, value + T_ENV, // 2 : name, value + T_ARG, // 3 : arg T_DIR, // 4+: path T_FILE, // 4+: path, data T_NUM @@ -284,7 +295,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; @@ -304,18 +315,19 @@ 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 == T_ENV) + u_setenv (PACK_NAME, PACK_DATA); + else if (pack_cur->type == T_ARG) + exe_argv [exe_argc++] = strdup (PACK_NAME); + 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); @@ -351,12 +363,13 @@ 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; @@ -372,6 +385,8 @@ if (u_chdir (datadir)) fatal ("unable to change to data directory"); + u_sync (); + if (u_rename (tmppath, execdir)) deltree (tmppath); // if move fails, delete new, assume other process created it independently } @@ -389,6 +404,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 @@ -398,6 +414,7 @@ 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 @@ -415,7 +432,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 (); @@ -442,9 +460,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 (); @@ -466,10 +483,12 @@ 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 ();