1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus-core - osal/files_unix.c *
3 * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *
4 * Copyright (C) 2009 Richard Goedeken *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
22 /* This file contains the definitions for the unix-specific file handling
26 #include <sys/types.h>
34 #include "api/m64p_types.h"
35 #include "api/callbacks.h"
38 /* OS X code for app bundle handling */
39 #include <CoreFoundation/CoreFoundation.h>
41 // dynamic data path detection onmac
42 bool macSetBundlePath(char* buffer)
44 // the following code will enable mupen to find its data when placed in an app bundle on mac OS X.
45 // returns true if path is set, returns false if path was not set
47 CFBundleRef main_bundle = CFBundleGetMainBundle(); assert(main_bundle);
48 CFURLRef main_bundle_URL = CFBundleCopyBundleURL(main_bundle); assert(main_bundle_URL);
49 CFStringRef cf_string_ref = CFURLCopyFileSystemPath( main_bundle_URL, kCFURLPOSIXPathStyle); assert(cf_string_ref);
50 CFStringGetCString(cf_string_ref, path, 1024, kCFStringEncodingASCII);
51 CFRelease(main_bundle_URL);
52 CFRelease(cf_string_ref);
54 if (strstr( path, ".app" ) != 0)
56 DebugMessage(M64MSG_VERBOSE, "checking whether we are using an app bundle: yes");
57 // executable is inside an app bundle, use app bundle-relative paths
58 sprintf(buffer, "%s/Contents/Resources/", path);
63 DebugMessage(M64MSG_VERBOSE, "checking whether we are using an app bundle: no");
69 /* definitions for system directories to search when looking for shared data files */
71 #define XSTR(S) STR(S) /* this wacky preprocessor thing is necessary to generate a quote-enclosed */
72 #define STR(S) #S /* copy of the SHAREDIR macro, which is defined by the makefile via gcc -DSHAREDIR="..." */
73 static const int datasearchdirs = 4;
74 static const char *datasearchpath[4] = { XSTR(SHAREDIR), "/usr/local/share/mupen64plus", "/usr/share/mupen64plus", "./" };
78 static const int datasearchdirs = 3;
79 static const char *datasearchpath[3] = { "/usr/local/share/mupen64plus", "/usr/share/mupen64plus", "./" };
84 static int get_xdg_dir(char *destpath, const char *envvar, const char *subdir)
87 const char *envpath = getenv(envvar);
89 /* error if this environment variable doesn't return a good string */
90 if (envpath == NULL || strlen(envpath) < 1)
93 /* error if path returned by the environemnt variable isn't a valid path to a directory */
94 if (stat(envpath, &fileinfo) != 0 || !S_ISDIR(fileinfo.st_mode))
97 /* append the given sub-directory to the path given by the environment variable */
98 strcpy(destpath, envpath);
99 if (destpath[strlen(destpath)-1] != '/')
100 strcat(destpath, "/");
101 strcat(destpath, subdir);
103 /* try to create the resulting directory tree, or return successfully if it already exists */
104 if (osal_mkdirp(destpath, 0700) != 0)
106 DebugMessage(M64MSG_ERROR, "Couldn't create directory: %s", destpath);
114 static int search_dir_file(char *destpath, const char *path, const char *filename)
116 struct stat fileinfo;
118 /* sanity check to start */
119 if (destpath == NULL || path == NULL || filename == NULL)
122 /* build the full filepath */
123 strcpy(destpath, path);
124 if (destpath[strlen(destpath)-1] != '/')
125 strcat(destpath, "/");
126 strcat(destpath, filename);
128 /* test for a valid file */
129 if (stat(destpath, &fileinfo) != 0)
131 if (!S_ISREG(fileinfo.st_mode))
134 /* success - file exists and is a regular file */
138 /* global functions */
140 int osal_mkdirp(const char *dirpath, int mode)
142 char *mypath, *currpath;
143 struct stat fileinfo;
145 // Terminate quickly if the path already exists
146 if (stat(dirpath, &fileinfo) == 0 && S_ISDIR(fileinfo.st_mode))
149 // Create partial paths
150 mypath = currpath = strdup(dirpath);
154 while ((currpath = strpbrk(currpath + 1, OSAL_DIR_SEPARATORS)) != NULL)
157 if (stat(mypath, &fileinfo) != 0)
159 if (mkdir(mypath, mode) != 0)
164 if (!S_ISDIR(fileinfo.st_mode))
167 *currpath = OSAL_DIR_SEPARATORS[0];
170 if (currpath != NULL)
174 if (stat(dirpath, &fileinfo) != 0 && mkdir(dirpath, mode) != 0)
180 const char * osal_get_shared_filepath(const char *filename, const char *firstsearch, const char *secondsearch)
182 static char retpath[PATH_MAX];
185 /* if caller gave us any directories to search, then look there first */
186 if (firstsearch != NULL && search_dir_file(retpath, firstsearch, filename) == 0)
188 if (secondsearch != NULL && search_dir_file(retpath, secondsearch, filename) == 0)
192 /* Special case : OS X bundles */
193 static char buf[1024];
194 if (macSetBundlePath(buf))
196 sprintf(retpath, "%s%s", buf, filename);
201 /* otherwise check our standard paths */
202 for (i = 0; i < datasearchdirs; i++)
204 if (search_dir_file(retpath, datasearchpath[i], filename) == 0)
208 /* we couldn't find the file */
212 const char * osal_get_user_configpath(void)
214 static char retpath[PATH_MAX];
217 /* first, try the XDG_CONFIG_HOME environment variable */
218 rval = get_xdg_dir(retpath, "XDG_CONFIG_HOME", "mupen64plus/");
222 /* then try the HOME environment variable */
223 rval = get_xdg_dir(retpath, "HOME", ".config/mupen64plus/");
227 /* otherwise we are in trouble */
229 DebugMessage(M64MSG_ERROR, "Failed to get configuration directory; $HOME is undefined or invalid.");
233 const char * osal_get_user_datapath(void)
235 static char retpath[PATH_MAX];
238 /* first, try the XDG_DATA_HOME environment variable */
239 rval = get_xdg_dir(retpath, "XDG_DATA_HOME", "mupen64plus/");
243 /* then try the HOME environment variable */
244 rval = get_xdg_dir(retpath, "HOME", ".local/share/mupen64plus/");
248 /* otherwise we are in trouble */
250 DebugMessage(M64MSG_ERROR, "Failed to get data directory; $HOME is undefined or invalid.");
254 const char * osal_get_user_cachepath(void)
256 static char retpath[PATH_MAX];
259 /* first, try the XDG_CACHE_HOME environment variable */
260 rval = get_xdg_dir(retpath, "XDG_CACHE_HOME", "mupen64plus/");
264 /* then try the HOME environment variable */
265 rval = get_xdg_dir(retpath, "HOME", ".cache/mupen64plus/");
269 /* otherwise we are in trouble */
271 DebugMessage(M64MSG_ERROR, "Failed to get cache directory; $HOME is undefined or invalid.");