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