ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Urlader/urlader.c
(Generate patch)

Comparing Urlader/urlader.c (file contents):
Revision 1.5 by root, Fri Dec 30 09:34:20 2011 UTC vs.
Revision 1.8 by root, Fri Dec 30 23:34:08 2011 UTC

1#ifndef URLADER 1#include "urlib.c"
2# define URLADER "urlader"
3#endif
4#define URLADER_VERSION "1.0" // a decimal number, not a version string
5 2
6#include <stdio.h> 3#include <stdio.h>
7#include <stdlib.h> 4#include <stdlib.h>
8#include <unistd.h> 5#include <unistd.h>
9#include <errno.h> 6#include <errno.h>
10#include <string.h> 7#include <string.h>
11#include <time.h> 8#include <time.h>
12#include <fcntl.h>
13 9
14#include "liblzf/lzf_d.c" 10#include "liblzf/lzf_d.c"
15 11
16#define TAIL_MAGIC "SCHMORPPACK0" 12#include "urlib.h"
17 13
18#ifdef _WIN32 14/* some simple? dynamic memory management for paths might save ltos of ram */
19
20 #include <windows.h>
21 //#include <winbase.h>
22 #include <shlobj.h>
23 #include <shlwapi.h>
24 #include <wininet.h>
25
26 static DWORD dword;
27
28 #define u_handle HANDLE
29 #define u_valid(handle) (!!handle)
30
31 #define u_setenv(name,value) SetEnvironmentVariable (name, value)
32 #define u_mkdir(path) !CreateDirectory (path, NULL)
33 #define u_chdir(path) !SetCurrentDirectory (path)
34 #define u_rename(fr,to) !MoveFile (fr, to)
35 #define u_open(path) CreateFile (path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL)
36 #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL)
37 #define u_close(handle) CloseHandle (handle)
38 #define u_append(path,add) PathAppend (path, add)
39 #define u_write(handle,data,len) (WriteFile (handle, data, len, &dword, 0) ? dword : -1)
40
41 #define u_fsync(handle) FlushFileBuffers (handle)
42 #define u_sync()
43
44#else
45
46 #include <sys/mman.h>
47 #include <sys/types.h>
48 #include <pwd.h>
49
50 #ifdef PATH_MAX
51 #define MAX_PATH (PATH_MAX < 4096 ? 4096 : PATH_MAX)
52 #else
53 #define MAX_PATH 4096
54 #endif
55
56 #define u_handle int
57 #define u_valid(fd) ((fd) >= 0)
58
59 #define u_setenv(name,value) setenv (name, value, 1)
60 #define u_mkdir(path) mkdir (path, 0777)
61 #define u_chdir(path) chdir (path)
62 #define u_rename(fr,to) rename (fr, to)
63 #define u_open(path) open (path, O_RDONLY)
64 #define u_creat(path,exec) open (path, O_WRONLY | O_CREAT | O_TRUNC, (exec) ? 0777 : 0666)
65 #define u_close(handle) close (handle)
66 #define u_append(path,add) strcat (strcat (path, "/"), add)
67 #define u_write(handle,data,len) write (handle, data, len)
68
69 // on a mostly idle system, a sync at the end is certainly faster, hope for the best
70 #define u_fsync(handle)
71 #define u_sync() sync ()
72
73#endif
74
75#define u_16(ptr) (((ptr)[0] << 8) | (ptr)[1])
76#define u_32(ptr) (((ptr)[0] << 24) | ((ptr)[1] << 16) | ((ptr)[2] << 8) | (ptr)[3])
77
78static u_handle pack_handle; 15static u_handle pack_handle;
16static u_handle lock_handle;
79static char tmppath[MAX_PATH]; 17static char tmppath[MAX_PATH];
80static char currdir[MAX_PATH]; 18static char currdir[MAX_PATH];
81static char datadir[MAX_PATH]; // %AppData%/urlader 19static char datadir[MAX_PATH]; // %AppData%/urlader
82static char exe_dir[MAX_PATH]; // %AppData%/urlader/EXE_ID 20static char exe_dir[MAX_PATH]; // %AppData%/urlader/EXE_ID
83static char execdir[MAX_PATH]; // %AppData%/urlader/EXE_ID/EXE_VER 21static char execdir[MAX_PATH]; // %AppData%/urlader/EXE_ID/EXE_VER
84static char exe_id[MAX_PATH]; 22static char exe_id[MAX_PATH];
85static char exe_ver[MAX_PATH]; 23static char exe_ver[MAX_PATH];
86 24
87static int exe_argc; 25static int exe_argc;
88static const char *exe_argv[32]; 26static const char *exe_argv[MAX_ARGC];
27static char exe_args[MAX_ARGS]; /* actual arguments strings copied here */
28static unsigned int exe_argo;
89 29
90static void 30static void
91fatal (const char *msg) 31fatal (const char *msg)
92{ 32{
93#ifdef _WIN32 33#ifdef _WIN32
94 MessageBox (0, msg, URLADER, 0); 34 MessageBox (0, msg, URLADER, 0);
95#else 35#else
96 fprintf (stderr, "%s: %s\n", URLADER, msg); 36 write (2, URLADER ": ", sizeof (URLADER ": ") - 1);
37 write (2, msg, strlen (msg));
38 write (2, "\n", 1);
97#endif 39#endif
98 40
99 _exit (1); 41 _exit (1);
100} 42}
101 43
116 return; 58 return;
117 } 59 }
118} 60}
119 61
120static void 62static void
121systemv (const char *const argv[], int dowait) 63systemv (const char *const argv[])
122{ 64{
123#ifdef _WIN32 65#ifdef _WIN32
124 _spawnv (P_WAIT, argv [0], argv); 66 _spawnv (P_WAIT, argv [0], argv);
125#else 67#else
126 pid_t pid = dowait ? fork () : 0; 68 pid_t pid = fork ();
127 69
128 if (pid < 0) 70 if (pid < 0)
129 fatal ("fork failure"); 71 fatal ("fork failure");
130 72
131 if (!pid) 73 if (!pid)
147 const char *argv[] = { getenv ("COMSPEC"), "/c", "rd", "/s", "/q", buf, 0 }; 89 const char *argv[] = { getenv ("COMSPEC"), "/c", "rd", "/s", "/q", buf, 0 };
148 sprintf (buf, "\"%s\"", path); 90 sprintf (buf, "\"%s\"", path);
149 #else 91 #else
150 const char *argv[] = { "/bin/rm", "-rf", path, 0 }; 92 const char *argv[] = { "/bin/rm", "-rf", path, 0 };
151 #endif 93 #endif
152 systemv (argv, 1); 94 systemv (argv);
153} 95}
154
155enum
156{
157 T_NULL, // 5
158 T_META, // 1 : exe_id, exe_ver
159 T_ENV, // 2 : name, value
160 T_ARG, // 3 : arg
161 T_DIR, // 4+: path
162 T_FILE, // 4+: path, data
163 T_NUM
164};
165
166enum
167{
168 F_EXEC = 1,
169 F_LZF = 2,
170 F_NULL = 0
171};
172
173struct hdr
174{
175 unsigned char type;
176 unsigned char flags;
177 unsigned char namelen[2];
178 unsigned char datalen[4];
179};
180
181struct tail {
182 unsigned char max_filesize[4];
183 unsigned char size[4];
184 char magic[12];
185};
186 96
187static char *pack_base, *pack_end; 97static char *pack_base, *pack_end;
188static struct hdr *pack_cur; 98static struct u_pack_hdr *pack_cur;
189static char *scratch; 99static char *scratch;
190static unsigned int scratch_len; 100static unsigned int scratch_size;
191 101
192#define PACK_NAME ((char *)(pack_cur + 1)) 102#define PACK_NAME ((char *)(pack_cur + 1))
193#define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1) 103#define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1)
194#define PACK_VALID pack_cur->type 104#define PACK_VALID pack_cur->type
195 105
196static void 106static void
197pack_next (void) 107pack_next (void)
198{ 108{
199 unsigned int d = u_32 (pack_cur->datalen); 109 unsigned int d = u_32 (pack_cur->datalen);
200 110
201 pack_cur = (struct hdr *)(PACK_DATA + d); 111 pack_cur = (struct u_pack_hdr *)(PACK_DATA + d);
202} 112}
203 113
204static void 114static void
205pack_unmap (void) 115pack_unmap (void)
206{ 116{
207 if (!pack_base) 117 if (pack_base)
208 return; 118 {
209
210 free (scratch);
211
212#ifdef _WIN32
213 UnmapViewOfFile (pack_base);
214#else
215 munmap (pack_base, pack_end - pack_base); 119 u_munmap (pack_base, pack_end - pack_base);
216#endif 120 pack_base = 0;
121 }
122
123 if (scratch)
124 {
125 u_free (scratch, scratch_size);
126 scratch = 0;
127 }
217} 128}
218 129
219static int 130static int
220pack_map (void) 131pack_map (void)
221{ 132{
133 char *addr;
134 unsigned int size;
135
222#ifdef _WIN32 136#ifdef _WIN32
223 BY_HANDLE_FILE_INFORMATION fi; 137 BY_HANDLE_FILE_INFORMATION fi;
224 138
225 if (!GetFileInformationByHandle (pack_handle, &fi)) 139 if (!GetFileInformationByHandle (pack_handle, &fi))
226 return 0; 140 return 0;
227 141
228 if (fi.nFileSizeHigh) // too big 142 size = fi.nFileSizeLow;
143#else
144 size = lseek (pack_handle, 0, SEEK_END);
145#endif
146
147 addr = u_mmap (pack_handle, size);
148 if (!addr)
229 return 0; 149 return 0;
230 150
231 HANDLE handle = CreateFileMapping (pack_handle, 0, PAGE_READONLY, 0, fi.nFileSizeLow, NULL);
232
233 if (!handle)
234 return 0;
235
236 pack_unmap (); 151 pack_unmap ();
237 152
238 pack_base = MapViewOfFile (handle, FILE_MAP_READ, 0, 0, fi.nFileSizeLow);
239
240 CloseHandle (handle);
241
242 pack_end = pack_base + fi.nFileSizeLow;
243#else
244 off_t len = lseek (pack_handle - 1, 0, SEEK_END);
245
246 pack_unmap ();
247
248 pack_base = mmap (0, len, PROT_READ, MAP_SHARED, pack_handle - 1, 0);
249 if (pack_base == (void *)-1)
250 pack_base = 0; 153 pack_base = addr;
251
252 pack_end = pack_base + len; 154 pack_end = pack_base + size;
253#endif
254 155
255 if (!pack_base)
256 return 0;
257
258 struct tail *tail; 156 struct u_pack_tail *tail;
259 157
260 tail = (void *)(pack_end - sizeof (*tail)); 158 tail = (void *)(pack_end - sizeof (*tail));
261 159
262 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) 160 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1))
263 return 0; 161 return 0;
264 162
265 pack_cur = (struct hdr *)(pack_end - u_32 (tail->size)); 163 pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (tail->size));
266 164
267 free (scratch);
268 scratch = malloc (scratch_len = u_32 (tail->max_filesize)); 165 scratch = u_malloc (scratch_size = u_32 (tail->max_filesize));
166 if (!scratch)
167 return 0;
269 168
270 return 1; 169 return 1;
271} 170}
272 171
273static void 172static void
274exe_info (void) 173exe_info (void)
275{ 174{
276 if (!pack_map ()) 175 if (!pack_map ())
277 fatal ("unable to locate packfile in executable - executable corrupted?"); 176 fatal ("unable to locate packfile in executable - executable corrupted?");
278 177
279 if (pack_cur->type != T_META) 178 if (pack_cur->type != U_T_META)
280 fatal ("unable to locate executable metadata - executable corrupted?"); 179 fatal ("unable to locate executable metadata - executable corrupted?");
281 180
282 strcpy (exe_id, PACK_NAME); 181 strcpy (exe_id, PACK_NAME);
283} 182}
284 183
307 } 206 }
308 207
309 u_close (oh); 208 u_close (oh);
310 } 209 }
311 210
312 if (pack_cur->type != T_META) 211 if (pack_cur->type != U_T_META)
313 fatal ("unable to locate override metadata"); 212 fatal ("unable to locate override metadata");
314 213
315 strcpy (exe_ver, PACK_DATA); 214 strcpy (exe_ver, PACK_DATA);
316 pack_next (); 215 pack_next ();
317 216
318 for (;;) 217 for (;;)
319 { 218 {
320 if (pack_cur->type == T_ENV) 219 if (pack_cur->type == U_T_ENV)
321 u_setenv (PACK_NAME, PACK_DATA); 220 u_setenv (PACK_NAME, PACK_DATA);
322 else if (pack_cur->type == T_ARG) 221 else if (pack_cur->type == U_T_ARG)
323 exe_argv [exe_argc++] = strdup (PACK_NAME); 222 {
223 int len = u_16 (pack_cur->namelen) + 1;
224 exe_argv [exe_argc++] = exe_args + exe_argo;
225 memcpy (exe_args + exe_argo, PACK_NAME, len);
226 exe_argo += len;
227 }
324 else 228 else
325 break; 229 break;
326 230
327 pack_next (); 231 pack_next ();
328 } 232 }
330done_env_arg: 234done_env_arg:
331 strcpy (execdir, exe_dir); 235 strcpy (execdir, exe_dir);
332 u_append (execdir, "i-"); 236 u_append (execdir, "i-");
333 strcat (execdir, exe_ver); 237 strcat (execdir, exe_ver);
334 238
239 strcat (strcpy (tmppath, execdir), ".lck");
240 lock_handle = u_lock (tmppath, 0, 1);
241 if (!lock_handle)
242 fatal ("unable to lock application instance");
243
335 if (access (execdir, F_OK)) 244 if (access (execdir, F_OK))
336 { 245 {
337 // does not exist yet, so unpack and move 246 // does not exist yet, so unpack and move
338 tmpdir (exe_dir); 247 tmpdir (exe_dir);
339 248
342 251
343 for (;;) 252 for (;;)
344 { 253 {
345 switch (pack_cur->type) 254 switch (pack_cur->type)
346 { 255 {
347 case T_DIR: 256 case U_T_DIR:
348 u_mkdir (PACK_NAME); 257 u_mkdir (PACK_NAME);
349 break; 258 break;
350 259
351 case T_FILE: 260 case U_T_FILE:
352 { 261 {
353 u_handle h = u_creat (PACK_NAME, pack_cur->flags & F_EXEC); 262 u_handle h = u_creat (PACK_NAME, pack_cur->flags & U_F_EXEC);
354 unsigned int dlen, len = u_32 (pack_cur->datalen); 263 unsigned int dlen, len = u_32 (pack_cur->datalen);
355 char *data = PACK_DATA; 264 char *data = PACK_DATA;
356 265
357 if (pack_cur->flags & F_LZF) 266 if (pack_cur->flags & U_F_LZF)
358 if (dlen = lzf_decompress (data, len, scratch, scratch_len)) 267 if (dlen = lzf_decompress (data, len, scratch, scratch_size))
359 { 268 {
360 data = scratch; 269 data = scratch;
361 len = dlen; 270 len = dlen;
362 } 271 }
363 else 272 else
372 u_fsync (h); 281 u_fsync (h);
373 u_close (h); 282 u_close (h);
374 } 283 }
375 break; 284 break;
376 285
377 case T_NULL: 286 case U_T_NULL:
378 goto done; 287 goto done;
379 } 288 }
380 289
381 pack_next (); 290 pack_next ();
382 } 291 }
419 328
420static void 329static void
421execute (void) 330execute (void)
422{ 331{
423 exe_argv [exe_argc] = 0; 332 exe_argv [exe_argc] = 0;
424 systemv (exe_argv, 0); 333 systemv (exe_argv);
425} 334}
426 335
427#ifdef _WIN32 336#ifdef _WIN32
428 337
429int APIENTRY 338int APIENTRY
478 387
479 if (!getcwd (currdir, sizeof (currdir))) 388 if (!getcwd (currdir, sizeof (currdir)))
480 strcpy (currdir, "."); 389 strcpy (currdir, ".");
481 390
482 u_mkdir (home); 391 u_mkdir (home);
392 //strcat (strcat (strcpy (datadir, home), "/."), URLADER);
483 sprintf (datadir, "%s/.%s", home, URLADER); 393 sprintf (datadir, "%s/.%s", home, URLADER);
484 u_mkdir (datadir); 394 u_mkdir (datadir);
485 395
486#if 0 396#if 0
487 if (gethostname (tmppath, sizeof (tmppath))) 397 if (gethostname (tmppath, sizeof (tmppath)))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines