-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlib.c
405 lines (321 loc) · 10.6 KB
/
urlib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* Copyright (c) 2012 Marc Alexander Lehmann <[email protected]>
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License ("GPL") version 2 or any later version,
* in which case the provisions of the GPL are applicable instead of
* the above. If you wish to allow the use of your version of this file
* only under the terms of the GPL and not to allow others to use your
* version of this file under the BSD license, indicate your decision
* by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete the
* provisions above, a recipient may use your version of this file under
* either the BSD or the GPL.
*/
#include "urlib.h"
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef _WIN32
#include <windows.h>
//#include <winbase.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <wininet.h>
static DWORD dword;
#ifndef SHGFP_TYPE_CURRENT
#define SHGFP_TYPE_CURRENT 0
#endif
#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 | FILE_SHARE_DELETE, 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 | FILE_SHARE_DELETE, 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 <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#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])
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 void
u_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;
if (!size)
return 0;
#ifdef _WIN32
{
HANDLE handle = CreateFileMapping (0, 0, PAGE_READWRITE, 0, size, NULL);
addr = 0;
if (handle)
{
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
if (!addr)
u_fatal ("memory allocation failure, aborting.");
return addr;
}
static void
u_free (void *addr, unsigned int size)
{
if (!addr)
return;
#ifdef _WIN32
UnmapViewOfFile (addr);
#elif defined (MAP_ANONYMOUS)
munmap (addr, size);
#else
free (addr);
#endif
}
static void *
u_realloc (void *addr, unsigned int old_size, unsigned int new_size)
{
void *addr2 = u_malloc (new_size);
memcpy (addr2, addr, (new_size < old_size ? new_size : old_size));
u_free (addr, old_size);
return addr2;
}
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
}
/////////////////////////////////////////////////////////////////////////////
typedef struct
{
char *addr;
unsigned int used;
unsigned int size;
} u_dynbuf;
static void *
u_dynbuf_append (u_dynbuf *dynbuf, void *data, unsigned int len)
{
char *dest;
if ((dynbuf->used += len) > dynbuf->size)
{
unsigned int new_size = dynbuf->size ? dynbuf->size * 2 : 4096;
dynbuf->addr = u_realloc (dynbuf->addr, dynbuf->size, new_size);
dynbuf->size = new_size;
}
dest = dynbuf->addr + dynbuf->used - len;
if (data)
memcpy (dest, data, len);
return dest;
}
/////////////////////////////////////////////////////////////////////////////
static void
u_set_datadir (void)
{
#ifdef _WIN32
if (SHGetFolderPath (0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, datadir) != S_OK)
u_fatal ("unable to find application data directory");
u_mkdir (datadir);
u_append (datadir, URLADER);
#else
char *home = getenv ("HOME");
if (!home)
{
struct passwd *pw;
if ((pw = getpwuid (getuid ())))
home = pw->pw_dir;
else
home = "/tmp";
}
u_mkdir (home);
//strcat (strcat (strcpy (datadir, home), "/."), URLADER);
sprintf (datadir, "%s/.%s", home, URLADER);
#endif
u_setenv ("URLADER_DATADIR", datadir);
}
static void
u_set_exe_info (void)
{
strcpy (exe_dir, datadir);
u_append (exe_dir, exe_id);
u_mkdir (exe_dir);
strcpy (execdir, exe_dir);
u_append (execdir, "i-");
strcat (execdir, exe_ver);
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);
}
/////////////////////////////////////////////////////////////////////////////
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;
}