author | Ryan C. Gordon <icculus@icculus.org> |
Fri, 22 Nov 2002 06:24:10 +0000 | |
changeset 504 | 3420d82f9b01 |
parent 393 | 468f75bf4d7d |
child 657 | dad3b5c307a9 |
permissions | -rw-r--r-- |
235 | 1 |
/* |
2 |
* BeOS platform-dependent support routines for PhysicsFS. |
|
3 |
* |
|
4 |
* Please see the file LICENSE in the source's root directory. |
|
5 |
* |
|
6 |
* This file written by Ryan C. Gordon. |
|
7 |
*/ |
|
8 |
||
9 |
#if HAVE_CONFIG_H |
|
10 |
# include <config.h> |
|
11 |
#endif |
|
12 |
||
13 |
#ifdef __BEOS__ |
|
14 |
||
15 |
#include <be/kernel/OS.h> |
|
16 |
#include <be/app/Roster.h> |
|
17 |
#include <be/storage/Volume.h> |
|
18 |
#include <be/storage/VolumeRoster.h> |
|
19 |
#include <be/storage/Directory.h> |
|
20 |
#include <be/storage/Entry.h> |
|
21 |
#include <be/storage/Path.h> |
|
22 |
#include <be/kernel/fs_info.h> |
|
23 |
#include <be/device/scsi.h> |
|
24 |
||
25 |
#include <stdio.h> |
|
26 |
#include <stdlib.h> |
|
27 |
#include <string.h> |
|
28 |
#include <errno.h> |
|
29 |
#include <unistd.h> |
|
30 |
||
31 |
#define __PHYSICSFS_INTERNAL__ |
|
32 |
#include "physfs_internal.h" |
|
33 |
||
34 |
||
35 |
const char *__PHYSFS_platformDirSeparator = "/"; |
|
36 |
||
37 |
||
38 |
int __PHYSFS_platformInit(void) |
|
39 |
{ |
|
40 |
return(1); /* always succeed. */ |
|
41 |
} /* __PHYSFS_platformInit */ |
|
42 |
||
43 |
||
44 |
int __PHYSFS_platformDeinit(void) |
|
45 |
{ |
|
46 |
return(1); /* always succeed. */ |
|
47 |
} /* __PHYSFS_platformDeinit */ |
|
48 |
||
49 |
||
50 |
||
51 |
/* caller needs to malloc() mntpnt, and expect us to free() it. */ |
|
52 |
static void addDisc(char *mntpnt, char ***discs, int *disccount) |
|
53 |
{ |
|
54 |
char **tmp = (char **) realloc(*discs, sizeof (char *) * (*disccount + 1)); |
|
55 |
if (tmp) |
|
56 |
{ |
|
57 |
tmp[*disccount - 1] = mntpnt; |
|
58 |
*discs = tmp; |
|
59 |
(*disccount)++; |
|
60 |
} /* if */ |
|
61 |
} /* addDisc */ |
|
62 |
||
63 |
||
64 |
static char *getMountPoint(const char *devname) |
|
65 |
{ |
|
66 |
BVolumeRoster mounts; |
|
67 |
BVolume vol; |
|
68 |
||
69 |
mounts.Rewind(); |
|
70 |
while (mounts.GetNextVolume(&vol) == B_NO_ERROR) |
|
71 |
{ |
|
72 |
fs_info fsinfo; |
|
73 |
fs_stat_dev(vol.Device(), &fsinfo); |
|
74 |
if (strcmp(devname, fsinfo.device_name) == 0) |
|
75 |
{ |
|
76 |
//char buf[B_FILE_NAME_LENGTH]; |
|
77 |
BDirectory directory; |
|
78 |
BEntry entry; |
|
79 |
BPath path; |
|
80 |
status_t rc; |
|
81 |
rc = vol.GetRootDirectory(&directory); |
|
393 | 82 |
BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL); |
235 | 83 |
rc = directory.GetEntry(&entry); |
393 | 84 |
BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL); |
235 | 85 |
rc = entry.GetPath(&path); |
393 | 86 |
BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL); |
235 | 87 |
const char *str = path.Path(); |
88 |
BAIL_IF_MACRO(str == NULL, ERR_OS_ERROR, NULL); /* ?! */ |
|
89 |
char *retval = (char *) malloc(strlen(str) + 1); |
|
90 |
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); |
|
91 |
strcpy(retval, str); |
|
92 |
return(retval); |
|
93 |
} /* if */ |
|
94 |
} /* while */ |
|
95 |
||
96 |
return(NULL); |
|
97 |
} /* getMountPoint */ |
|
98 |
||
99 |
||
100 |
/* |
|
101 |
* This function is lifted from Simple Directmedia Layer (SDL): |
|
102 |
* http://www.libsdl.org/ |
|
103 |
*/ |
|
104 |
static void tryDir(const char *dirname, char ***discs, int *disccount) |
|
105 |
{ |
|
106 |
BDirectory dir; |
|
107 |
dir.SetTo(dirname); |
|
108 |
if (dir.InitCheck() != B_NO_ERROR) |
|
109 |
return; |
|
110 |
||
111 |
dir.Rewind(); |
|
112 |
BEntry entry; |
|
113 |
while (dir.GetNextEntry(&entry) >= 0) |
|
114 |
{ |
|
115 |
BPath path; |
|
116 |
const char *name; |
|
117 |
entry_ref e; |
|
118 |
||
119 |
if (entry.GetPath(&path) != B_NO_ERROR) |
|
120 |
continue; |
|
121 |
||
122 |
name = path.Path(); |
|
123 |
||
124 |
if (entry.GetRef(&e) != B_NO_ERROR) |
|
125 |
continue; |
|
126 |
||
127 |
if (entry.IsDirectory()) |
|
128 |
{ |
|
129 |
if (strcmp(e.name, "floppy") != 0) |
|
130 |
tryDir(name, discs, disccount); |
|
131 |
} /* if */ |
|
132 |
||
133 |
else |
|
134 |
{ |
|
135 |
bool add_it = false; |
|
136 |
int devfd; |
|
137 |
device_geometry g; |
|
138 |
||
139 |
if (strcmp(e.name, "raw") == 0) /* ignore partitions. */ |
|
140 |
{ |
|
141 |
int devfd = open(name, O_RDONLY); |
|
142 |
if (devfd >= 0) |
|
143 |
{ |
|
144 |
if (ioctl(devfd, B_GET_GEOMETRY, &g, sizeof(g)) >= 0) |
|
145 |
{ |
|
146 |
if (g.device_type == B_CD) |
|
147 |
{ |
|
148 |
char *mntpnt = getMountPoint(name); |
|
149 |
if (mntpnt != NULL) |
|
150 |
addDisc(mntpnt, discs, disccount); |
|
151 |
} /* if */ |
|
152 |
} /* if */ |
|
153 |
} /* if */ |
|
154 |
} /* if */ |
|
155 |
||
156 |
close(devfd); |
|
157 |
} /* else */ |
|
158 |
} /* while */ |
|
159 |
} /* tryDir */ |
|
160 |
||
161 |
||
162 |
char **__PHYSFS_platformDetectAvailableCDs(void) |
|
163 |
{ |
|
164 |
char **retval = (char **) malloc(sizeof (char *)); |
|
165 |
int cd_count = 1; /* We count the NULL entry. */ |
|
166 |
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); |
|
167 |
tryDir("/dev/disk", &retval, &cd_count); |
|
168 |
retval[cd_count - 1] = NULL; |
|
169 |
return(retval); |
|
170 |
} /* __PHYSFS_platformDetectAvailableCDs */ |
|
171 |
||
172 |
||
173 |
static team_id getTeamID(void) |
|
174 |
{ |
|
175 |
thread_info info; |
|
176 |
thread_id tid = find_thread(NULL); |
|
177 |
get_thread_info(tid, &info); |
|
178 |
return(info.team); |
|
179 |
} /* getMyTeamID */ |
|
180 |
||
181 |
||
182 |
char *__PHYSFS_platformCalcBaseDir(const char *argv0) |
|
183 |
{ |
|
184 |
/* in case there isn't a BApplication yet, we'll construct a roster. */ |
|
185 |
BRoster roster; |
|
186 |
app_info info; |
|
187 |
status_t rc = roster.GetRunningAppInfo(getTeamID(), &info); |
|
393 | 188 |
BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL); |
235 | 189 |
BEntry entry(&(info.ref), true); |
190 |
BPath path; |
|
191 |
rc = entry.GetPath(&path); /* (path) now has binary's path. */ |
|
192 |
assert(rc == B_OK); |
|
193 |
rc = path.GetParent(&path); /* chop filename, keep directory. */ |
|
194 |
assert(rc == B_OK); |
|
195 |
const char *str = path.Path(); |
|
196 |
assert(str != NULL); |
|
197 |
char *retval = (char *) malloc(strlen(str) + 1); |
|
198 |
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); |
|
199 |
strcpy(retval, str); |
|
200 |
return(retval); |
|
201 |
} /* __PHYSFS_platformCalcBaseDir */ |
|
202 |
||
203 |
||
204 |
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void) |
|
205 |
{ |
|
206 |
return((PHYSFS_uint64) find_thread(NULL)); |
|
207 |
} /* __PHYSFS_platformGetThreadID */ |
|
208 |
||
209 |
||
210 |
/* Much like my college days, try to sleep for 10 milliseconds at a time... */ |
|
211 |
void __PHYSFS_platformTimeslice(void) |
|
212 |
{ |
|
213 |
snooze(10000); /* put thread to sleep for 10 milliseconds. */ |
|
214 |
} /* __PHYSFS_platformTimeslice */ |
|
215 |
||
216 |
||
217 |
char *__PHYSFS_platformRealPath(const char *path) |
|
218 |
{ |
|
219 |
char *str = (char *) alloca(strlen(path) + 1); |
|
220 |
BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL); |
|
221 |
strcpy(str, path); |
|
222 |
char *leaf = strrchr(str, '/'); |
|
223 |
if (leaf != NULL) |
|
224 |
*(leaf++) = '\0'; |
|
225 |
||
226 |
BPath normalized(str, leaf, true); /* force normalization of path. */ |
|
227 |
const char *resolved_path = normalized.Path(); |
|
362
ac8c846a0a66
Changed enumeration code to use __PHYSFS_addToLinkedStringList().
Ryan C. Gordon <icculus@icculus.org>
parents:
327
diff
changeset
|
228 |
BAIL_IF_MACRO(resolved_path == NULL, ERR_NO_SUCH_FILE, NULL); |
235 | 229 |
char *retval = (char *) malloc(strlen(resolved_path) + 1); |
230 |
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); |
|
231 |
strcpy(retval, resolved_path); |
|
232 |
return(retval); |
|
233 |
} /* __PHYSFS_platformRealPath */ |
|
234 |
||
235 |
||
236 |
void *__PHYSFS_platformCreateMutex(void) |
|
237 |
{ |
|
238 |
sem_id *retval = (sem_id *) malloc(sizeof (sem_id)); |
|
239 |
sem_id rc; |
|
240 |
||
241 |
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); |
|
242 |
rc = create_sem(1, "PhysicsFS semaphore"); |
|
243 |
if (rc < B_OK) |
|
244 |
{ |
|
245 |
free(retval); |
|
393 | 246 |
BAIL_MACRO(strerror(rc), NULL); |
235 | 247 |
} // if |
248 |
||
249 |
*retval = rc; |
|
250 |
return(retval); |
|
251 |
} /* __PHYSFS_platformCreateMutex */ |
|
252 |
||
253 |
||
254 |
void __PHYSFS_platformDestroyMutex(void *mutex) |
|
255 |
{ |
|
256 |
delete_sem( *((sem_id *) mutex) ); |
|
257 |
free(mutex); |
|
258 |
} /* __PHYSFS_platformDestroyMutex */ |
|
259 |
||
260 |
||
261 |
int __PHYSFS_platformGrabMutex(void *mutex) |
|
262 |
{ |
|
263 |
status_t rc = acquire_sem(*((sem_id *) mutex)); |
|
393 | 264 |
BAIL_IF_MACRO(rc < B_OK, strerror(rc), 0); |
235 | 265 |
return(1); |
266 |
} /* __PHYSFS_platformGrabMutex */ |
|
267 |
||
268 |
||
269 |
void __PHYSFS_platformReleaseMutex(void *mutex) |
|
270 |
{ |
|
271 |
release_sem(*((sem_id *) mutex)); |
|
272 |
} /* __PHYSFS_platformReleaseMutex */ |
|
273 |
||
274 |
#endif |
|
275 |
||
276 |
/* end of beos.cpp ... */ |
|
277 |