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> |
… | |
… | |
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 | |
… | |
… | |
150 | UnmapViewOfFile (addr); |
176 | UnmapViewOfFile (addr); |
151 | #else |
177 | #else |
152 | munmap (addr, len); |
178 | munmap (addr, len); |
153 | #endif |
179 | #endif |
154 | } |
180 | } |
|
|
181 | |
|
|
182 | ///////////////////////////////////////////////////////////////////////////// |
|
|
183 | |
|
|
184 | static void |
|
|
185 | u_set_datadir (void) |
|
|
186 | { |
|
|
187 | #ifdef _WIN32 |
|
|
188 | if (SHGetFolderPath (0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, datadir) != S_OK) |
|
|
189 | u_fatal ("unable to find application data directory"); |
|
|
190 | |
|
|
191 | u_mkdir (datadir); |
|
|
192 | u_append (datadir, URLADER); |
|
|
193 | |
|
|
194 | #else |
|
|
195 | char *home = getenv ("HOME"); |
|
|
196 | |
|
|
197 | if (!home) |
|
|
198 | { |
|
|
199 | struct passwd *pw; |
|
|
200 | |
|
|
201 | if ((pw = getpwuid (getuid ()))) |
|
|
202 | home = pw->pw_dir; |
|
|
203 | else |
|
|
204 | home = "/tmp"; |
|
|
205 | } |
|
|
206 | |
|
|
207 | u_mkdir (home); |
|
|
208 | //strcat (strcat (strcpy (datadir, home), "/."), URLADER); |
|
|
209 | sprintf (datadir, "%s/.%s", home, URLADER); |
|
|
210 | #endif |
|
|
211 | |
|
|
212 | u_setenv ("URLADER_DATADIR", datadir); |
|
|
213 | } |
|
|
214 | |
|
|
215 | static void |
|
|
216 | u_set_exe_info (void) |
|
|
217 | { |
|
|
218 | strcpy (exe_dir, datadir); |
|
|
219 | u_append (exe_dir, exe_id); |
|
|
220 | u_mkdir (exe_dir); |
|
|
221 | |
|
|
222 | strcpy (execdir, exe_dir); |
|
|
223 | u_append (execdir, "i-"); |
|
|
224 | strcat (execdir, exe_ver); |
|
|
225 | |
|
|
226 | u_setenv ("URLADER_EXECDIR", execdir); |
|
|
227 | u_setenv ("URLADER_EXE_ID" , exe_id); |
|
|
228 | u_setenv ("URLADER_EXE_DIR", exe_dir); |
|
|
229 | u_setenv ("URLADER_EXE_VER", exe_ver); |
|
|
230 | } |
|
|
231 | |
|
|
232 | ///////////////////////////////////////////////////////////////////////////// |
|
|
233 | |
155 | static u_handle |
234 | static u_handle |
156 | u_lock (const char *path, int excl, int dowait) |
235 | u_lock (const char *path, int excl, int dowait) |
157 | { |
236 | { |
158 | u_handle h; |
237 | u_handle h; |
159 | |
238 | |
… | |
… | |
192 | |
271 | |
193 | // we have the lock, now verify that the lockfile still exists |
272 | // we have the lock, now verify that the lockfile still exists |
194 | |
273 | |
195 | #ifdef _WIN32 |
274 | #ifdef _WIN32 |
196 | // apparently, we have to open the file to get its info :( |
275 | // apparently, we have to open the file to get its info :( |
|
|
276 | { |
197 | BY_HANDLE_FILE_INFORMATION s1, s2; |
277 | BY_HANDLE_FILE_INFORMATION s1, s2; |
198 | u_handle h2 = u_lockfile (path); |
278 | u_handle h2 = u_lockfile (path); |
199 | if (!u_valid (h)) |
279 | if (!u_valid (h)) |
200 | break; |
280 | break; |
201 | |
281 | |
202 | success = GetFileInformationByHandle (h, &s1) |
282 | success = GetFileInformationByHandle (h, &s1) |
203 | && GetFileInformationByHandle (h2, &s2); |
283 | && GetFileInformationByHandle (h2, &s2); |
204 | |
284 | |
205 | u_close (h2); |
285 | u_close (h2); |
206 | |
286 | |
207 | if (!success) |
287 | if (!success) |
208 | break; |
288 | break; |
209 | |
289 | |
210 | success = s1.dwVolumeSerialNumber == s2.dwVolumeSerialNumber |
290 | success = s1.dwVolumeSerialNumber == s2.dwVolumeSerialNumber |
211 | && s1.nFileIndexHigh == s2.nFileIndexHigh |
291 | && s1.nFileIndexHigh == s2.nFileIndexHigh |
212 | && s1.nFileIndexLow == s2.nFileIndexLow; |
292 | && s1.nFileIndexLow == s2.nFileIndexLow; |
|
|
293 | } |
213 | #else |
294 | #else |
214 | struct stat s1, s2; |
295 | struct stat s1, s2; |
215 | |
296 | |
216 | if (fstat (h, &s1) || stat (path, &s2)) |
297 | if (fstat (h, &s1) || stat (path, &s2)) |
217 | break; |
298 | break; |