Core commit. Compile and run on the OpenPandora
[mupen64plus-pandora.git] / source / mupen64plus-core / src / osal / files_unix.c
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  *   Mupen64plus-core - osal/files_unix.c                                  *
3  *   Mupen64Plus homepage: http://code.google.com/p/mupen64plus/           *
4  *   Copyright (C) 2009 Richard Goedeken                                   *
5  *                                                                         *
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.                                   *
10  *                                                                         *
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.                          *
15  *                                                                         *
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  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
21                        
22 /* This file contains the definitions for the unix-specific file handling
23  * functions
24  */
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32
33 #include "files.h"
34 #include "api/m64p_types.h"
35 #include "api/callbacks.h"
36
37 #ifdef __APPLE__
38 /* OS X code for app bundle handling */
39 #include <CoreFoundation/CoreFoundation.h>
40
41 // dynamic data path detection onmac
42 bool macSetBundlePath(char* buffer)
43 {
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
46     char path[1024];
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);
53     
54     if (strstr( path, ".app" ) != 0)
55     {
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);
59         return true;
60     }
61     else
62     {
63         DebugMessage(M64MSG_VERBOSE, "checking whether we are using an app bundle: no");
64         return false;
65     }
66 }
67 #endif
68
69 /* definitions for system directories to search when looking for shared data files */
70 #if defined(SHAREDIR)
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", "./" };
75   #undef STR
76   #undef XSTR
77 #else
78   static const int   datasearchdirs = 3;
79   static const char *datasearchpath[3] = { "/usr/local/share/mupen64plus",  "/usr/share/mupen64plus", "./" };
80 #endif
81
82 /* local functions */
83
84 static int get_xdg_dir(char *destpath, const char *envvar, const char *subdir)
85 {
86     struct stat fileinfo;
87     const char *envpath = getenv(envvar);
88
89     /* error if this environment variable doesn't return a good string */
90     if (envpath == NULL || strlen(envpath) < 1)
91         return 1;
92
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))
95         return 2;
96
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);
102
103     /* try to create the resulting directory tree, or return successfully if it already exists */
104     if (osal_mkdirp(destpath, 0700) != 0)
105     {
106         DebugMessage(M64MSG_ERROR, "Couldn't create directory: %s", destpath);
107         return 3;
108     }
109
110     /* Success */
111     return 0;
112 }
113
114 static int search_dir_file(char *destpath, const char *path, const char *filename)
115 {
116     struct stat fileinfo;
117
118     /* sanity check to start */
119     if (destpath == NULL || path == NULL || filename == NULL)
120         return 1;
121
122     /* build the full filepath */
123     strcpy(destpath, path);
124     if (destpath[strlen(destpath)-1] != '/')
125         strcat(destpath, "/");
126     strcat(destpath, filename);
127
128     /* test for a valid file */
129     if (stat(destpath, &fileinfo) != 0)
130         return 2;
131     if (!S_ISREG(fileinfo.st_mode))
132         return 3;
133
134     /* success - file exists and is a regular file */
135     return 0;
136 }
137
138 /* global functions */
139
140 int osal_mkdirp(const char *dirpath, int mode)
141 {
142     char *mypath, *currpath;
143     struct stat fileinfo;
144
145     // Terminate quickly if the path already exists
146     if (stat(dirpath, &fileinfo) == 0 && S_ISDIR(fileinfo.st_mode))
147         return 0;
148
149     // Create partial paths
150     mypath = currpath = strdup(dirpath);
151     if (mypath == NULL)
152         return 1;
153
154     while ((currpath = strpbrk(currpath + 1, OSAL_DIR_SEPARATORS)) != NULL)
155     {
156         *currpath = '\0';
157         if (stat(mypath, &fileinfo) != 0)
158         {
159             if (mkdir(mypath, mode) != 0)
160                 break;
161         }
162         else
163         {
164             if (!S_ISDIR(fileinfo.st_mode))
165                 break;
166         }
167         *currpath = OSAL_DIR_SEPARATORS[0];
168     }
169     free(mypath);
170     if (currpath != NULL)
171         return 1;
172
173     // Create full path
174     if (stat(dirpath, &fileinfo) != 0 && mkdir(dirpath, mode) != 0)
175         return 1;
176
177     return 0;
178 }
179
180 const char * osal_get_shared_filepath(const char *filename, const char *firstsearch, const char *secondsearch)
181 {
182     static char retpath[PATH_MAX];
183     int i;
184
185     /* if caller gave us any directories to search, then look there first */
186     if (firstsearch != NULL && search_dir_file(retpath, firstsearch, filename) == 0)
187         return retpath;
188     if (secondsearch != NULL && search_dir_file(retpath, secondsearch, filename) == 0)
189         return retpath;
190
191 #ifdef __APPLE__
192     /* Special case : OS X bundles */
193     static char buf[1024];
194     if (macSetBundlePath(buf))
195     {
196         sprintf(retpath, "%s%s", buf, filename);
197         return retpath;
198     }
199 #endif
200
201     /* otherwise check our standard paths */
202     for (i = 0; i < datasearchdirs; i++)
203     {
204         if (search_dir_file(retpath, datasearchpath[i], filename) == 0)
205             return retpath;
206     }
207
208     /* we couldn't find the file */
209     return NULL;
210 }
211
212 const char * osal_get_user_configpath(void)
213 {
214     static char retpath[PATH_MAX];
215     int rval;
216     
217     /* first, try the XDG_CONFIG_HOME environment variable */
218     rval = get_xdg_dir(retpath, "XDG_CONFIG_HOME", "mupen64plus/");
219     if (rval == 0)
220         return retpath;
221
222     /* then try the HOME environment variable */
223     rval = get_xdg_dir(retpath, "HOME", ".config/mupen64plus/");
224     if (rval == 0)
225         return retpath;
226
227     /* otherwise we are in trouble */
228     if (rval < 3)
229         DebugMessage(M64MSG_ERROR, "Failed to get configuration directory; $HOME is undefined or invalid.");
230     return NULL;
231 }
232
233 const char * osal_get_user_datapath(void)
234 {
235     static char retpath[PATH_MAX];
236     int rval;
237     
238     /* first, try the XDG_DATA_HOME environment variable */
239     rval = get_xdg_dir(retpath, "XDG_DATA_HOME", "mupen64plus/");
240     if (rval == 0)
241         return retpath;
242
243     /* then try the HOME environment variable */
244     rval = get_xdg_dir(retpath, "HOME", ".local/share/mupen64plus/");
245     if (rval == 0)
246         return retpath;
247
248     /* otherwise we are in trouble */
249     if (rval < 3)
250         DebugMessage(M64MSG_ERROR, "Failed to get data directory; $HOME is undefined or invalid.");
251     return NULL;
252 }
253
254 const char * osal_get_user_cachepath(void)
255 {
256     static char retpath[PATH_MAX];
257     int rval;
258     
259     /* first, try the XDG_CACHE_HOME environment variable */
260     rval = get_xdg_dir(retpath, "XDG_CACHE_HOME", "mupen64plus/");
261     if (rval == 0)
262         return retpath;
263
264     /* then try the HOME environment variable */
265     rval = get_xdg_dir(retpath, "HOME", ".cache/mupen64plus/");
266     if (rval == 0)
267         return retpath;
268
269     /* otherwise we are in trouble */
270     if (rval < 3)
271         DebugMessage(M64MSG_ERROR, "Failed to get cache directory; $HOME is undefined or invalid.");
272     return NULL;
273 }
274
275