author | Ryan C. Gordon <icculus@icculus.org> |
Mon, 25 Jul 2005 01:38:07 +0000 | |
changeset 745 | df04959950eb |
parent 657 | dad3b5c307a9 |
child 788 | 3df4f1b61339 |
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 |
static char *getMountPoint(const char *devname) |
|
51 |
{ |
|
52 |
BVolumeRoster mounts; |
|
53 |
BVolume vol; |
|
54 |
||
55 |
mounts.Rewind(); |
|
56 |
while (mounts.GetNextVolume(&vol) == B_NO_ERROR) |
|
57 |
{ |
|
58 |
fs_info fsinfo; |
|
59 |
fs_stat_dev(vol.Device(), &fsinfo); |
|
60 |
if (strcmp(devname, fsinfo.device_name) == 0) |
|
61 |
{ |
|
62 |
//char buf[B_FILE_NAME_LENGTH]; |
|
63 |
BDirectory directory; |
|
64 |
BEntry entry; |
|
65 |
BPath path; |
|
66 |
status_t rc; |
|
67 |
rc = vol.GetRootDirectory(&directory); |
|
393 | 68 |
BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL); |
235 | 69 |
rc = directory.GetEntry(&entry); |
393 | 70 |
BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL); |
235 | 71 |
rc = entry.GetPath(&path); |
393 | 72 |
BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL); |
235 | 73 |
const char *str = path.Path(); |
74 |
BAIL_IF_MACRO(str == NULL, ERR_OS_ERROR, NULL); /* ?! */ |
|
745
df04959950eb
Patched to compile again on BeOS.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
75 |
char *retval = (char *) allocator.Malloc(strlen(str) + 1); |
235 | 76 |
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); |
77 |
strcpy(retval, str); |
|
78 |
return(retval); |
|
79 |
} /* if */ |
|
80 |
} /* while */ |
|
81 |
||
82 |
return(NULL); |
|
83 |
} /* getMountPoint */ |
|
84 |
||
85 |
||
86 |
/* |
|
87 |
* This function is lifted from Simple Directmedia Layer (SDL): |
|
88 |
* http://www.libsdl.org/ |
|
89 |
*/ |
|
657
dad3b5c307a9
Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents:
504
diff
changeset
|
90 |
static void tryDir(const char *d, PHYSFS_StringCallback callback, void *data) |
235 | 91 |
{ |
92 |
BDirectory dir; |
|
657
dad3b5c307a9
Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents:
504
diff
changeset
|
93 |
dir.SetTo(d); |
235 | 94 |
if (dir.InitCheck() != B_NO_ERROR) |
95 |
return; |
|
96 |
||
97 |
dir.Rewind(); |
|
98 |
BEntry entry; |
|
99 |
while (dir.GetNextEntry(&entry) >= 0) |
|
100 |
{ |
|
101 |
BPath path; |
|
102 |
const char *name; |
|
103 |
entry_ref e; |
|
104 |
||
105 |
if (entry.GetPath(&path) != B_NO_ERROR) |
|
106 |
continue; |
|
107 |
||
108 |
name = path.Path(); |
|
109 |
||
110 |
if (entry.GetRef(&e) != B_NO_ERROR) |
|
111 |
continue; |
|
112 |
||
113 |
if (entry.IsDirectory()) |
|
114 |
{ |
|
115 |
if (strcmp(e.name, "floppy") != 0) |
|
657
dad3b5c307a9
Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents:
504
diff
changeset
|
116 |
tryDir(name, callback, data); |
235 | 117 |
} /* if */ |
118 |
||
119 |
else |
|
120 |
{ |
|
121 |
bool add_it = false; |
|
122 |
int devfd; |
|
123 |
device_geometry g; |
|
124 |
||
125 |
if (strcmp(e.name, "raw") == 0) /* ignore partitions. */ |
|
126 |
{ |
|
127 |
int devfd = open(name, O_RDONLY); |
|
128 |
if (devfd >= 0) |
|
129 |
{ |
|
130 |
if (ioctl(devfd, B_GET_GEOMETRY, &g, sizeof(g)) >= 0) |
|
131 |
{ |
|
132 |
if (g.device_type == B_CD) |
|
133 |
{ |
|
134 |
char *mntpnt = getMountPoint(name); |
|
135 |
if (mntpnt != NULL) |
|
657
dad3b5c307a9
Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents:
504
diff
changeset
|
136 |
{ |
dad3b5c307a9
Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents:
504
diff
changeset
|
137 |
callback(data, mntpnt); |
745
df04959950eb
Patched to compile again on BeOS.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
138 |
allocator.Free(mntpnt); /* !!! FIXME: lose this malloc! */ |
657
dad3b5c307a9
Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents:
504
diff
changeset
|
139 |
} /* if */ |
235 | 140 |
} /* if */ |
141 |
} /* if */ |
|
142 |
} /* if */ |
|
143 |
} /* if */ |
|
144 |
||
145 |
close(devfd); |
|
146 |
} /* else */ |
|
147 |
} /* while */ |
|
148 |
} /* tryDir */ |
|
149 |
||
150 |
||
657
dad3b5c307a9
Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents:
504
diff
changeset
|
151 |
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data) |
235 | 152 |
{ |
657
dad3b5c307a9
Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents:
504
diff
changeset
|
153 |
tryDir("/dev/disk", cb, data); |
235 | 154 |
} /* __PHYSFS_platformDetectAvailableCDs */ |
155 |
||
156 |
||
157 |
static team_id getTeamID(void) |
|
158 |
{ |
|
159 |
thread_info info; |
|
160 |
thread_id tid = find_thread(NULL); |
|
161 |
get_thread_info(tid, &info); |
|
162 |
return(info.team); |
|
163 |
} /* getMyTeamID */ |
|
164 |
||
165 |
||
166 |
char *__PHYSFS_platformCalcBaseDir(const char *argv0) |
|
167 |
{ |
|
168 |
/* in case there isn't a BApplication yet, we'll construct a roster. */ |
|
169 |
BRoster roster; |
|
170 |
app_info info; |
|
171 |
status_t rc = roster.GetRunningAppInfo(getTeamID(), &info); |
|
393 | 172 |
BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL); |
235 | 173 |
BEntry entry(&(info.ref), true); |
174 |
BPath path; |
|
175 |
rc = entry.GetPath(&path); /* (path) now has binary's path. */ |
|
176 |
assert(rc == B_OK); |
|
177 |
rc = path.GetParent(&path); /* chop filename, keep directory. */ |
|
178 |
assert(rc == B_OK); |
|
179 |
const char *str = path.Path(); |
|
180 |
assert(str != NULL); |
|
745
df04959950eb
Patched to compile again on BeOS.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
181 |
char *retval = (char *) allocator.Malloc(strlen(str) + 1); |
235 | 182 |
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); |
183 |
strcpy(retval, str); |
|
184 |
return(retval); |
|
185 |
} /* __PHYSFS_platformCalcBaseDir */ |
|
186 |
||
187 |
||
188 |
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void) |
|
189 |
{ |
|
190 |
return((PHYSFS_uint64) find_thread(NULL)); |
|
191 |
} /* __PHYSFS_platformGetThreadID */ |
|
192 |
||
193 |
||
194 |
/* Much like my college days, try to sleep for 10 milliseconds at a time... */ |
|
195 |
void __PHYSFS_platformTimeslice(void) |
|
196 |
{ |
|
197 |
snooze(10000); /* put thread to sleep for 10 milliseconds. */ |
|
198 |
} /* __PHYSFS_platformTimeslice */ |
|
199 |
||
200 |
||
201 |
char *__PHYSFS_platformRealPath(const char *path) |
|
202 |
{ |
|
203 |
char *str = (char *) alloca(strlen(path) + 1); |
|
204 |
BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL); |
|
205 |
strcpy(str, path); |
|
206 |
char *leaf = strrchr(str, '/'); |
|
207 |
if (leaf != NULL) |
|
208 |
*(leaf++) = '\0'; |
|
209 |
||
210 |
BPath normalized(str, leaf, true); /* force normalization of path. */ |
|
211 |
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
|
212 |
BAIL_IF_MACRO(resolved_path == NULL, ERR_NO_SUCH_FILE, NULL); |
745
df04959950eb
Patched to compile again on BeOS.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
213 |
char *retval = (char *) allocator.Malloc(strlen(resolved_path) + 1); |
235 | 214 |
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); |
215 |
strcpy(retval, resolved_path); |
|
216 |
return(retval); |
|
217 |
} /* __PHYSFS_platformRealPath */ |
|
218 |
||
219 |
||
220 |
void *__PHYSFS_platformCreateMutex(void) |
|
221 |
{ |
|
745
df04959950eb
Patched to compile again on BeOS.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
222 |
sem_id *retval = (sem_id *) allocator.Malloc(sizeof (sem_id)); |
235 | 223 |
sem_id rc; |
224 |
||
225 |
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); |
|
226 |
rc = create_sem(1, "PhysicsFS semaphore"); |
|
227 |
if (rc < B_OK) |
|
228 |
{ |
|
745
df04959950eb
Patched to compile again on BeOS.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
229 |
allocator.Free(retval); |
393 | 230 |
BAIL_MACRO(strerror(rc), NULL); |
235 | 231 |
} // if |
232 |
||
233 |
*retval = rc; |
|
234 |
return(retval); |
|
235 |
} /* __PHYSFS_platformCreateMutex */ |
|
236 |
||
237 |
||
238 |
void __PHYSFS_platformDestroyMutex(void *mutex) |
|
239 |
{ |
|
240 |
delete_sem( *((sem_id *) mutex) ); |
|
745
df04959950eb
Patched to compile again on BeOS.
Ryan C. Gordon <icculus@icculus.org>
parents:
657
diff
changeset
|
241 |
allocator.Free(mutex); |
235 | 242 |
} /* __PHYSFS_platformDestroyMutex */ |
243 |
||
244 |
||
245 |
int __PHYSFS_platformGrabMutex(void *mutex) |
|
246 |
{ |
|
247 |
status_t rc = acquire_sem(*((sem_id *) mutex)); |
|
393 | 248 |
BAIL_IF_MACRO(rc < B_OK, strerror(rc), 0); |
235 | 249 |
return(1); |
250 |
} /* __PHYSFS_platformGrabMutex */ |
|
251 |
||
252 |
||
253 |
void __PHYSFS_platformReleaseMutex(void *mutex) |
|
254 |
{ |
|
255 |
release_sem(*((sem_id *) mutex)); |
|
256 |
} /* __PHYSFS_platformReleaseMutex */ |
|
257 |
||
258 |
#endif |
|
259 |
||
260 |
/* end of beos.cpp ... */ |
|
261 |