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