Debian Codegraph @

src/paths.cpp

raw
1 /**
2 * Compute and store the search paths
3
4 * Copyright (C) 2007, 2008, 2009, 2014, 2015 Sylvain Beucler
5
6 * This file is part of GNU FreeDink
7
8 * GNU FreeDink is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 3 of the
11 * License, or (at your option) any later version.
12
13 * GNU FreeDink is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 #include <stdlib.h>
27 #include <string.h> /* strdup */
28 #include <unistd.h> /* getcwd */
29 #include <errno.h>
30
31 #include "io_util.h"
32 #include "paths.h"
33 #include "log.h"
34
35 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__
36 #define WIN32_LEAN_AND_MEAN
37 #define _WIN32_IE 0x0401
38 #include <windows.h>
39 #include <shlobj.h>
40 #endif
41
42 /* mkdir */
43 #include <sys/stat.h>
44 #include <sys/types.h>
45 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__
46 #include <direct.h>
47 #define mkdir(name,mode) mkdir(name)
48 #endif
49
50 #include "str_util.h" /* asprintf_append */
51
52 #ifdef __ANDROID__
53 #include <unistd.h> /* chdir */
54 #include <errno.h>
55 #include "SDL.h"
56 #endif
57
58 /* Pleases gnulib's error module */
59 char* program_name = PACKAGE;
60
61 static char* pkgdatadir = NULL;
62 static char* fallbackdir = NULL;
63 static char* dmoddir = NULL;
64 static char* dmodname = NULL;
65 static char* userappdir = NULL;
66
67 /**
68 * getcwd without size restriction
69 * aka get_current_dir_name(3)
70 */
71 char* paths_getcwd()
72 {
73 char* cwd = NULL;
74 int cwd_size = PATH_MAX;
75 do {
76 cwd = (char*)realloc(cwd, cwd_size);
77 getcwd(cwd, cwd_size);
78 cwd_size *= 2;
79 } while (errno == ERANGE);
80 return cwd;
81 }
82
83 static char * br_strcat (const char *str1, const char *str2)
84 {
85 char *result;
86 size_t len1, len2;
87
88 if (str1 == NULL)
89 str1 = "";
90 if (str2 == NULL)
91 str2 = "";
92
93 len1 = strlen (str1);
94 len2 = strlen (str2);
95
96 result = (char *) malloc (len1 + len2 + 1);
97 memcpy (result, str1, len1);
98 memcpy (result + len1, str2, len2);
99 result[len1 + len2] = '\0';
100
101 return result;
102 }
103
104 static char* br_build_path (const char *dir, const char *file)
105 {
106 char *dir2, *result;
107 size_t len;
108 int must_free = 0;
109
110 len = strlen (dir);
111 if (len > 0 && dir[len - 1] != '/') {
112 dir2 = br_strcat (dir, "/");
113 must_free = 1;
114 } else
115 dir2 = (char *) dir;
116
117 result = br_strcat (dir2, file);
118 if (must_free)
119 free (dir2);
120 return result;
121 }
122
123
124 /**
125 * Allow using paths in test suite until we implement in-memory mocked
126 * filesystem, using std::stream rather than hard-to-mock FILE*
127 */
128 #ifdef _WIN32
129 #include <shellapi.h>
130 #else
131 #include <sys/types.h>
132 #include <sys/wait.h>
133 #include <unistd.h>
134 #endif
135 void ts_paths_init() {
136 char* cwd = paths_getcwd();
137 pkgdatadir = br_build_path(cwd, "tmp_ts/pkgdatadir");
138 fallbackdir = br_build_path(cwd, "tmp_ts/fallbackdir");
139 dmoddir = br_build_path(cwd, "tmp_ts/dmoddir");
140 dmodname = br_build_path(cwd, "dmoddir");
141 userappdir = br_build_path(cwd, "tmp_ts/useradddir");
142 #ifdef _WIN32
143 SHFILEOPSTRUCT fileop;
144 fileop.hwnd = NULL;
145 fileop.wFunc = FO_DELETE;
146 fileop.pFrom = br_build_path(cwd, "tmp_ts\0");
147 fileop.pTo = NULL;
148 fileop.fFlags = FOF_NOCONFIRMATION|FOF_SILENT; // do not prompt the user
149 fileop.lpszProgressTitle = NULL;
150 fileop.hNameMappings = NULL;
151 SHFileOperation(&fileop);
152 #else
153 {
154 pid_t pid = 0;
155 if ((pid = fork()) < 0) {
156 perror("fork");
157 } else if (pid == 0) {
158 if (execlp("rm", "rm", "-rf", "tmp_ts/", NULL) < 0)
159 perror("execlp");
160 exit(EXIT_FAILURE);
161 } else {
162 int status = 0;
163 waitpid(pid, &status, 0);
164 }
165 }
166 #endif
167 free(cwd);
168 if (mkdir("tmp_ts", 0777) < 0) perror("mkdir");
169 if (mkdir(pkgdatadir, 0777) < 0) perror("mkdir");
170 if (mkdir(fallbackdir, 0777) < 0) perror("mkdir");
171 if (mkdir(dmoddir, 0777) < 0) perror("mkdir");
172 if (mkdir(dmoddir, 0777) < 0) perror("mkdir");
173 if (mkdir(userappdir, 0777) < 0) perror("mkdir");
174 // map.dat is always opened in "r" or "r+b", pre-create it:
175 FILE* in = paths_dmodfile_fopen("map.dat", "w");
176 fclose(in);
177 }
178
179 bool paths_init(char *argv0, char *refdir_opt, char *dmoddir_opt)
180 {
181 char *refdir = NULL;
182
183 /** pkgdatadir **/
184 {
185 #if defined __ANDROID__ || defined _WIN32 || defined __WIN32__ || defined __CYGWIN__
186 /* "./freedink" */
187 pkgdatadir = strdup("./" PACKAGE);
188 #else
189 /* (e.g. "/usr/share/freedink") */
190 pkgdatadir = br_build_path(BUILD_DATA_DIR, PACKAGE);
191 #endif
192 }
193
194 /** refdir (e.g. "/usr/share/dink") **/
195 {
196 /** => refdir **/
197 char* match = NULL;
198 int nb_dirs = 7;
199 char* lookup[nb_dirs];
200 int i = 0;
201 if (refdir_opt == NULL)
202 lookup[0] = NULL;
203 else
204 lookup[0] = refdir_opt;
205
206 /* Use absolute filename, otherwise SDL_rwops fails on Android (java.io.FileNotFoundException) */
207 lookup[1] = paths_getcwd();
208
209 /* FHS mentions optional 'share/games' which some Debian packagers
210 seem to be found of */
211 /* PACKAGERS: don't alter these paths. FreeDink must run in a
212 _consistent_ way across platforms. If you need an alternate
213 path, consider using ./configure --prefix=..., or contact
214 [email protected] to discuss it. */
215 lookup[2] = br_build_path(BUILD_DATA_DIR, "dink");
216 lookup[3] = "/usr/local/share/games/dink";
217 lookup[4] = "/usr/local/share/dink";
218 lookup[5] = "/usr/share/games/dink";
219 lookup[6] = "/usr/share/dink";
220
221 for (; i < nb_dirs; i++)
222 {
223 char *dir_graphics_ci = NULL, *dir_tiles_ci = NULL;
224 if (lookup[i] == NULL)
225 continue;
226 dir_graphics_ci = br_build_path(lookup[i], "dink/graphics");
227 dir_tiles_ci = br_build_path(lookup[i], "dink/tiles");
228 ciconvert(dir_graphics_ci);
229 ciconvert(dir_tiles_ci);
230 if (is_directory(dir_graphics_ci) && is_directory(dir_tiles_ci))
231 {
232 match = lookup[i];
233 }
234
235 if (match == NULL && i == 0)
236 {
237 log_error("Invalid --refdir option: %s and/or %s are not accessible.",
238 dir_graphics_ci, dir_tiles_ci);
239 return false;
240 }
241
242 free(dir_graphics_ci);
243 free(dir_tiles_ci);
244 if (match != NULL)
245 break;
246 }
247 refdir = match;
248 if (refdir != NULL)
249 {
250 refdir = strdup(refdir);
251 }
252 else
253 {
254 log_error("Error: cannot find reference directory (--refdir). I looked in:\n"
255 // lookup[0] already treated above
256 "- %s [current dir]\n"
257 "- %s [build prefix]\n"
258 "- %s [hard-coded prefix]\n"
259 "- %s [hard-coded prefix]\n"
260 "- %s [hard-coded prefix]\n"
261 "- %s [hard-coded prefix]\n"
262 "The reference directory contains among others the "
263 "'dink/graphics/' and 'dink/tiles/' directories (as well as D-Mods)."
264 , lookup[1], lookup[2], lookup[3], lookup[4], lookup[5], lookup[6]);
265 return false;
266 }
267
268 free(lookup[1]); // paths_getcwd
269 free(lookup[2]); // br_build_path
270 }
271
272 /** fallbackdir (e.g. "/usr/share/dink/dink") **/
273 /* (directory used when a file cannot be found in a D-Mod) */
274 {
275 fallbackdir = br_strcat(refdir, "/dink");
276 }
277
278 /** dmoddir (e.g. "/usr/share/dink/island") **/
279 {
280 if (dmoddir_opt != NULL && is_directory(dmoddir_opt))
281 {
282 /* Use path given on the command line, either a full path or a
283 path relative to the current directory. */
284 /* Note: don't search for "dink" in the default dir if no
285 '-game' option was given */
286 dmoddir = strdup(dmoddir_opt);
287 }
288 else
289 {
290 /* Use path given on the command line, relative to $refdir */
291 char *subdir = dmoddir_opt;
292 if (subdir == NULL || strlen(subdir) == 0) /* no opt, or -g '' */
293 subdir = "dink";
294 dmoddir = (char*)malloc(strlen(refdir) + 1 + strlen(subdir) + 1);
295 strcpy(dmoddir, refdir);
296 strcat(dmoddir, "/");
297 strcat(dmoddir, subdir);
298 if (!is_directory(dmoddir))
299 {
300 char *msg = NULL;
301
302 asprintf_append(&msg, "Error: D-Mod directory '%s' doesn't exist. I looked in:\n", subdir);
303 if (dmoddir_opt != NULL)
304 asprintf_append(&msg, "- ./%s\n", dmoddir_opt);
305 asprintf_append(&msg, "- %s (refdir is '%s')", dmoddir, refdir);
306
307 log_error("%s", msg);
308 free(msg);
309 return false;
310 }
311 }
312 /* Strip slashes */
313 while (strlen(dmoddir) > 0 && dmoddir[strlen(dmoddir) - 1] == '/')
314 dmoddir[strlen(dmoddir) - 1] = '\0';
315 }
316
317 /** dmodname (e.g. "island") **/
318 /* Used to save games in ~/.dink/<dmod>/... */
319 {
320 /* Don't accept '.' or '' or '/..' (nameless) but accept 'island' (relative path) */
321 int len = strlen(dmoddir);
322 char* start = dmoddir + len;
323 for (; start >= dmoddir; start--) /* basename(1) */
324 if (*start == '\\' || *start == '/')
325 break;
326 start++; // we're either -1 or at the last slash, start is right after
327 if (strcmp(start, ".") == 0 || strcmp(start, "..") == 0
328 || strcmp(start, "") == 0)
329 {
330 log_error("Error: not loading nameless D-Mod '%s'", start);
331 return false;
332 }
333 int dmodname_len = start - dmoddir;
334 dmodname = (char*)malloc(dmodname_len+1);
335 strncpy(dmodname, start, dmodname_len);
336 dmodname[dmodname_len] = '\0';
337 }
338
339 /** userappdir (e.g. "~/.dink") **/
340 {
341 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__
342 userappdir = (char*)malloc(MAX_PATH);
343 /* C:\Documents and Settings\name\Application Data */
344 SHGetSpecialFolderPath(NULL, userappdir, CSIDL_APPDATA, 1);
345 #else
346 char* envhome = getenv("HOME");
347 if (envhome != NULL)
348 userappdir = strdup(getenv("HOME"));
349 #endif
350 if (userappdir != NULL)
351 {
352 userappdir = (char*)realloc(userappdir, strlen(userappdir) + 1 + 1 + strlen(PACKAGE) + 1);
353 strcat(userappdir, "/");
354 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__
355 #else
356 strcat(userappdir, ".");
357 #endif
358 strcat(userappdir, "dink");
359 }
360 else
361 {
362 // No detected home directory - saving in the reference
363 // directory
364 userappdir = strdup(refdir);
365 }
366 }
367
368 log_info("datadir = %s", BUILD_DATA_DIR);
369 log_info("pkgdatadir = %s", pkgdatadir);
370 log_info("refdir = %s", refdir);
371 log_info("fallbackdir = %s", fallbackdir);
372 log_info("dmoddir = %s", dmoddir);
373 log_info("dmodname = %s", dmodname);
374 log_info("userappdir = %s", userappdir);
375
376 free(refdir);
377 return true;
378 }
379
380
381 const char *paths_getpkgdatadir(void)
382 {
383 return pkgdatadir;
384 }
385 const char *paths_getdmoddir(void)
386 {
387 return dmoddir;
388 }
389 const char *paths_getdmodname(void)
390 {
391 return dmodname;
392 }
393
394 const char *paths_getfallbackdir(void)
395 {
396 return fallbackdir;
397 }
398
399 char* paths_dmodfile(const char *file)
400 {
401 char *fullpath = br_build_path(dmoddir, file);
402 ciconvert(fullpath);
403 return fullpath;
404 }
405
406 FILE* paths_dmodfile_fopen(const char *file, const char *mode)
407 {
408 char *fullpath = paths_dmodfile(file);
409 FILE *result = fopen(fullpath, mode);
410 free(fullpath);
411 return result;
412 }
413
414 char* paths_fallbackfile(const char *file)
415 {
416 char *fullpath = br_build_path(fallbackdir, file);
417 ciconvert(fullpath);
418 return fullpath;
419 }
420
421 FILE* paths_fallbackfile_fopen(const char *file, const char *mode)
422 {
423 char *fullpath = paths_fallbackfile(file);
424 FILE *result = fopen(fullpath, mode);
425 free(fullpath);
426 return result;
427 }
428
429 char* paths_pkgdatafile(const char *file)
430 {
431 char *fullpath = br_build_path(pkgdatadir, file);
432 ciconvert(fullpath);
433 return fullpath;
434 }
435
436 FILE* paths_pkgdatafile_fopen(const char *file, const char *mode)
437 {
438 char *fullpath = paths_pkgdatafile(file);
439 FILE *result = fopen(fullpath, mode);
440 free(fullpath);
441 return result;
442 }
443
444 FILE *paths_savegame_fopen(int num, const char *mode)
445 {
446 char *fullpath_in_dmoddir = NULL;
447 char *fullpath_in_userappdir = NULL;
448 FILE *fp = NULL;
449
450 /* 20 decimal digits max for 64bit integer - should be enough :) */
451 char file[4 + 20 + 4 + 1];
452 sprintf(file, "save%d.dat", num);
453
454
455 /** fullpath_in_userappdir **/
456 char *savedir = strdup(userappdir);
457 savedir = (char*)realloc(savedir, strlen(userappdir) + 1 + strlen(dmodname) + 1);
458 strcat(savedir, "/");
459 strcat(savedir, dmodname);
460 /* Create directories if needed */
461 if (strchr(mode, 'w') != NULL || strchr(mode, 'a') != NULL)
462 /* Note: 0777 & umask => 0755 in common case */
463 if ((!is_directory(userappdir) && (mkdir(userappdir, 0777) < 0))
464 || (!is_directory(savedir) && (mkdir(savedir, 0777) < 0)))
465 {
466 free(savedir);
467 return NULL;
468 }
469 fullpath_in_userappdir = br_build_path(savedir, file);
470 ciconvert(fullpath_in_userappdir);
471 free(savedir);
472
473
474 /** fullpath_in_dmoddir **/
475 fullpath_in_dmoddir = paths_dmodfile(file);
476 ciconvert(fullpath_in_dmoddir);
477
478
479 /* Try ~/.dink (if present) when reading - but don't try that first
480 when writing - except in Emscripten where we need a stable,
481 exclusive mount point. */
482 #ifndef __EMSCRIPTEN__
483 if (strchr(mode, 'r') != NULL)
484 #endif
485 fp = fopen(fullpath_in_userappdir, mode);
486
487 /* Try in the D-Mod dir */
488 if (fp == NULL)
489 fp = fopen(fullpath_in_dmoddir, mode);
490
491 /* Then try in ~/.dink */
492 if (fp == NULL)
493 fp = fopen(fullpath_in_userappdir, mode);
494
495 free(fullpath_in_dmoddir);
496 free(fullpath_in_userappdir);
497
498 return fp;
499 }
500
501 void paths_quit(void)
502 {
503 free(pkgdatadir);
504 free(fallbackdir);
505 free(dmoddir);
506 free(dmodname);
507 free(userappdir);
508
509 pkgdatadir = NULL;
510 fallbackdir = NULL;
511 dmoddir = NULL;
512 dmodname = NULL;
513 userappdir = NULL;
514 }