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