1 | #include "urlib.h" |
1 | #include "urlib.h" |
2 | |
2 | |
3 | #include <fcntl.h> |
3 | #include <fcntl.h> |
|
|
4 | #include <stdio.h> |
|
|
5 | #include <string.h> |
|
|
6 | #include <stdlib.h> |
4 | |
7 | |
5 | #ifdef _WIN32 |
8 | #ifdef _WIN32 |
6 | |
9 | |
7 | #include <windows.h> |
10 | #include <windows.h> |
8 | //#include <winbase.h> |
11 | //#include <winbase.h> |
… | |
… | |
18 | |
21 | |
19 | #define u_setenv(name,value) SetEnvironmentVariable (name, value) |
22 | #define u_setenv(name,value) SetEnvironmentVariable (name, value) |
20 | #define u_mkdir(path) !CreateDirectory (path, NULL) |
23 | #define u_mkdir(path) !CreateDirectory (path, NULL) |
21 | #define u_chdir(path) !SetCurrentDirectory (path) |
24 | #define u_chdir(path) !SetCurrentDirectory (path) |
22 | #define u_rename(fr,to) !MoveFile (fr, to) |
25 | #define u_rename(fr,to) !MoveFile (fr, to) |
23 | #define u_open(path) CreateFile (path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) |
26 | #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) |
24 | #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) |
27 | #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) |
25 | #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) |
28 | #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) |
26 | #define u_close(handle) CloseHandle (handle) |
29 | #define u_close(handle) CloseHandle (handle) |
27 | #define u_append(path,add) PathAppend (path, add) |
30 | #define u_append(path,add) PathAppend (path, add) |
28 | #define u_write(handle,data,len) (WriteFile (handle, data, len, &dword, 0) ? dword : -1) |
31 | #define u_write(handle,data,len) (WriteFile (handle, data, len, &dword, 0) ? dword : -1) |
29 | |
32 | |
30 | #define u_fsync(handle) FlushFileBuffers (handle) |
33 | #define u_fsync(handle) FlushFileBuffers (handle) |
31 | #define u_sync() |
34 | #define u_sync() |
32 | |
35 | |
33 | #define u_lockfile(path) CreateFile (path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL) |
36 | #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) |
34 | #define u_cloexec(handle) |
37 | #define u_cloexec(handle) |
35 | |
38 | |
36 | #else |
39 | #else |
37 | |
40 | |
38 | #define _GNU_SOURCE 1 |
41 | #define _GNU_SOURCE 1 |
… | |
… | |
79 | #endif |
82 | #endif |
80 | |
83 | |
81 | #define u_16(ptr) (((ptr)[0] << 8) | (ptr)[1]) |
84 | #define u_16(ptr) (((ptr)[0] << 8) | (ptr)[1]) |
82 | #define u_32(ptr) (((ptr)[0] << 24) | ((ptr)[1] << 16) | ((ptr)[2] << 8) | (ptr)[3]) |
85 | #define u_32(ptr) (((ptr)[0] << 24) | ((ptr)[1] << 16) | ((ptr)[2] << 8) | (ptr)[3]) |
83 | |
86 | |
|
|
87 | static char currdir[MAX_PATH]; |
|
|
88 | static char datadir[MAX_PATH]; // %AppData%/urlader |
|
|
89 | static char exe_dir[MAX_PATH]; // %AppData%/urlader/EXE_ID |
|
|
90 | static char execdir[MAX_PATH]; // %AppData%/urlader/EXE_ID/EXE_VER |
|
|
91 | static char exe_id[MAX_PATH]; |
|
|
92 | static char exe_ver[MAX_PATH]; |
|
|
93 | |
|
|
94 | ///////////////////////////////////////////////////////////////////////////// |
|
|
95 | |
|
|
96 | static void |
|
|
97 | u_fatal (const char *msg) |
|
|
98 | { |
|
|
99 | #ifdef _WIN32 |
|
|
100 | MessageBox (0, msg, URLADER, 0); |
|
|
101 | #else |
|
|
102 | write (2, URLADER ": ", sizeof (URLADER ": ") - 1); |
|
|
103 | write (2, msg, strlen (msg)); |
|
|
104 | write (2, "\n", 1); |
|
|
105 | #endif |
|
|
106 | |
|
|
107 | _exit (1); |
|
|
108 | } |
|
|
109 | |
84 | static void * |
110 | static void * |
85 | u_malloc (unsigned int size) |
111 | u_malloc (unsigned int size) |
86 | { |
112 | { |
87 | void *addr; |
113 | void *addr; |
88 | |
114 | |
89 | #ifdef _WIN32 |
115 | if (!size) |
90 | HANDLE handle = CreateFileMapping (0, 0, PAGE_READWRITE, 0, size, NULL); |
|
|
91 | |
|
|
92 | if (!handle) |
|
|
93 | return 0; |
116 | return 0; |
94 | |
117 | |
|
|
118 | #ifdef _WIN32 |
|
|
119 | { |
|
|
120 | HANDLE handle = CreateFileMapping (0, 0, PAGE_READWRITE, 0, size, NULL); |
|
|
121 | |
|
|
122 | addr = 0; |
|
|
123 | if (handle) |
|
|
124 | { |
95 | addr = MapViewOfFile (handle, FILE_MAP_WRITE, 0, 0, size); |
125 | addr = MapViewOfFile (handle, FILE_MAP_WRITE, 0, 0, size); |
96 | |
|
|
97 | CloseHandle (handle); |
126 | CloseHandle (handle); |
|
|
127 | } |
|
|
128 | } |
98 | #elif defined (MAP_ANONYMOUS) |
129 | #elif defined (MAP_ANONYMOUS) |
99 | addr = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); |
130 | addr = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); |
100 | |
131 | |
101 | if (addr == (void *)-1) |
132 | if (addr == (void *)-1) |
102 | addr = 0; |
133 | addr = 0; |
103 | #else |
134 | #else |
104 | addr = malloc (size); |
135 | addr = malloc (size); |
105 | #endif |
136 | #endif |
106 | |
137 | |
|
|
138 | if (!addr) |
|
|
139 | u_fatal ("memory allocation failure, aborting."); |
|
|
140 | |
107 | return addr; |
141 | return addr; |
108 | } |
142 | } |
109 | |
143 | |
110 | static void |
144 | static void |
111 | u_free (void *addr, unsigned int size) |
145 | u_free (void *addr, unsigned int size) |
112 | { |
146 | { |
|
|
147 | if (!addr) |
|
|
148 | return; |
|
|
149 | |
113 | #ifdef _WIN32 |
150 | #ifdef _WIN32 |
114 | UnmapViewOfFile (addr); |
151 | UnmapViewOfFile (addr); |
115 | #elif defined (MAP_ANONYMOUS) |
152 | #elif defined (MAP_ANONYMOUS) |
116 | munmap (addr, size); |
153 | munmap (addr, size); |
117 | #else |
154 | #else |
118 | free (addr); |
155 | free (addr); |
119 | #endif |
156 | #endif |
120 | } |
157 | } |
121 | |
158 | |
122 | static void * |
159 | static void * |
|
|
160 | u_realloc (void *addr, unsigned int old_size, unsigned int new_size) |
|
|
161 | { |
|
|
162 | void *addr2 = u_malloc (new_size); |
|
|
163 | memcpy (addr2, addr, (new_size < old_size ? new_size : old_size)); |
|
|
164 | u_free (addr, old_size); |
|
|
165 | |
|
|
166 | return addr2; |
|
|
167 | } |
|
|
168 | |
|
|
169 | static void * |
123 | u_mmap (u_handle h, unsigned int size) |
170 | u_mmap (u_handle h, unsigned int size) |
124 | { |
171 | { |
125 | void *addr; |
172 | void *addr; |
126 | |
173 | |
127 | #ifdef _WIN32 |
174 | #ifdef _WIN32 |
… | |
… | |
150 | UnmapViewOfFile (addr); |
197 | UnmapViewOfFile (addr); |
151 | #else |
198 | #else |
152 | munmap (addr, len); |
199 | munmap (addr, len); |
153 | #endif |
200 | #endif |
154 | } |
201 | } |
|
|
202 | |
|
|
203 | ///////////////////////////////////////////////////////////////////////////// |
|
|
204 | |
|
|
205 | typedef struct |
|
|
206 | { |
|
|
207 | char *addr; |
|
|
208 | unsigned int used; |
|
|
209 | unsigned int size; |
|
|
210 | } u_dynbuf; |
|
|
211 | |
|
|
212 | static void * |
|
|
213 | u_dynbuf_append (u_dynbuf *dynbuf, void *data, unsigned int len) |
|
|
214 | { |
|
|
215 | char *dest; |
|
|
216 | |
|
|
217 | if ((dynbuf->used += len) > dynbuf->size) |
|
|
218 | { |
|
|
219 | unsigned int new_size = dynbuf->size ? dynbuf->size * 2 : 4096; |
|
|
220 | dynbuf->addr = u_realloc (dynbuf->addr, dynbuf->size, new_size); |
|
|
221 | dynbuf->size = new_size; |
|
|
222 | } |
|
|
223 | |
|
|
224 | dest = dynbuf->addr + dynbuf->used - len; |
|
|
225 | |
|
|
226 | if (data) |
|
|
227 | memcpy (dest, data, len); |
|
|
228 | |
|
|
229 | return dest; |
|
|
230 | } |
|
|
231 | |
|
|
232 | ///////////////////////////////////////////////////////////////////////////// |
|
|
233 | |
|
|
234 | static void |
|
|
235 | u_set_datadir (void) |
|
|
236 | { |
|
|
237 | #ifdef _WIN32 |
|
|
238 | if (SHGetFolderPath (0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, datadir) != S_OK) |
|
|
239 | u_fatal ("unable to find application data directory"); |
|
|
240 | |
|
|
241 | u_mkdir (datadir); |
|
|
242 | u_append (datadir, URLADER); |
|
|
243 | |
|
|
244 | #else |
|
|
245 | char *home = getenv ("HOME"); |
|
|
246 | |
|
|
247 | if (!home) |
|
|
248 | { |
|
|
249 | struct passwd *pw; |
|
|
250 | |
|
|
251 | if ((pw = getpwuid (getuid ()))) |
|
|
252 | home = pw->pw_dir; |
|
|
253 | else |
|
|
254 | home = "/tmp"; |
|
|
255 | } |
|
|
256 | |
|
|
257 | u_mkdir (home); |
|
|
258 | //strcat (strcat (strcpy (datadir, home), "/."), URLADER); |
|
|
259 | sprintf (datadir, "%s/.%s", home, URLADER); |
|
|
260 | #endif |
|
|
261 | |
|
|
262 | u_setenv ("URLADER_DATADIR", datadir); |
|
|
263 | } |
|
|
264 | |
|
|
265 | static void |
|
|
266 | u_set_exe_info (void) |
|
|
267 | { |
|
|
268 | strcpy (exe_dir, datadir); |
|
|
269 | u_append (exe_dir, exe_id); |
|
|
270 | u_mkdir (exe_dir); |
|
|
271 | |
|
|
272 | strcpy (execdir, exe_dir); |
|
|
273 | u_append (execdir, "i-"); |
|
|
274 | strcat (execdir, exe_ver); |
|
|
275 | |
|
|
276 | u_setenv ("URLADER_EXECDIR", execdir); |
|
|
277 | u_setenv ("URLADER_EXE_ID" , exe_id); |
|
|
278 | u_setenv ("URLADER_EXE_DIR", exe_dir); |
|
|
279 | u_setenv ("URLADER_EXE_VER", exe_ver); |
|
|
280 | } |
|
|
281 | |
|
|
282 | ///////////////////////////////////////////////////////////////////////////// |
|
|
283 | |
155 | static u_handle |
284 | static u_handle |
156 | u_lock (const char *path, int excl, int dowait) |
285 | u_lock (const char *path, int excl, int dowait) |
157 | { |
286 | { |
158 | u_handle h; |
287 | u_handle h; |
159 | |
288 | |
… | |
… | |
192 | |
321 | |
193 | // we have the lock, now verify that the lockfile still exists |
322 | // we have the lock, now verify that the lockfile still exists |
194 | |
323 | |
195 | #ifdef _WIN32 |
324 | #ifdef _WIN32 |
196 | // apparently, we have to open the file to get its info :( |
325 | // apparently, we have to open the file to get its info :( |
|
|
326 | { |
197 | BY_HANDLE_FILE_INFORMATION s1, s2; |
327 | BY_HANDLE_FILE_INFORMATION s1, s2; |
198 | u_handle h2 = u_lockfile (path); |
328 | u_handle h2 = u_lockfile (path); |
199 | if (!u_valid (h)) |
329 | if (!u_valid (h)) |
200 | break; |
330 | break; |
201 | |
331 | |
202 | success = GetFileInformationByHandle (h, &s1) |
332 | success = GetFileInformationByHandle (h, &s1) |
203 | && GetFileInformationByHandle (h2, &s2); |
333 | && GetFileInformationByHandle (h2, &s2); |
204 | |
334 | |
205 | u_close (h2); |
335 | u_close (h2); |
206 | |
336 | |
207 | if (!success) |
337 | if (!success) |
208 | break; |
338 | break; |
209 | |
339 | |
210 | success = s1.dwVolumeSerialNumber == s2.dwVolumeSerialNumber |
340 | success = s1.dwVolumeSerialNumber == s2.dwVolumeSerialNumber |
211 | && s1.nFileIndexHigh == s2.nFileIndexHigh |
341 | && s1.nFileIndexHigh == s2.nFileIndexHigh |
212 | && s1.nFileIndexLow == s2.nFileIndexLow; |
342 | && s1.nFileIndexLow == s2.nFileIndexLow; |
|
|
343 | } |
213 | #else |
344 | #else |
214 | struct stat s1, s2; |
345 | struct stat s1, s2; |
215 | |
346 | |
216 | if (fstat (h, &s1) || stat (path, &s2)) |
347 | if (fstat (h, &s1) || stat (path, &s2)) |
217 | break; |
348 | break; |