/
platform_beos.cpp
247 lines (200 loc) · 6.41 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
#ifdef PHYSFS_PLATFORM_HAIKU
#include <os/kernel/OS.h>
#include <os/app/Roster.h>
#include <os/storage/Volume.h>
#include <os/storage/VolumeRoster.h>
#include <os/storage/Directory.h>
#include <os/storage/Entry.h>
#include <os/storage/Path.h>
#include <os/kernel/fs_info.h>
#include <os/device/scsi.h>
#include <os/support/Locker.h>
#else
26
27
28
29
30
31
32
33
34
#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>
35
#include <be/support/Locker.h>
36
#endif
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
66
67
68
69
70
71
72
73
#include <errno.h>
#include <unistd.h>
#include "physfs_internal.h"
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);
74
BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
75
rc = directory.GetEntry(&entry);
76
BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
77
rc = entry.GetPath(&path);
78
BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
79
80
const char *str = path.Path();
BAIL_IF_MACRO(str == NULL, ERR_OS_ERROR, NULL); /* ?! */
81
char *retval = (char *) allocator.Malloc(strlen(str) + 1);
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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/
*/
96
static void tryDir(const char *d, PHYSFS_StringCallback callback, void *data)
97
98
{
BDirectory dir;
99
dir.SetTo(d);
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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)
122
tryDir(name, callback, data);
123
124
125
126
127
128
129
130
131
} /* if */
else
{
bool add_it = false;
device_geometry g;
if (strcmp(e.name, "raw") == 0) /* ignore partitions. */
{
132
const int devfd = open(name, O_RDONLY);
133
134
if (devfd >= 0)
{
135
136
137
const int rc = ioctl(devfd, B_GET_GEOMETRY, &g, sizeof(g));
close(devfd);
if (rc >= 0)
138
139
140
141
142
{
if (g.device_type == B_CD)
{
char *mntpnt = getMountPoint(name);
if (mntpnt != NULL)
143
144
{
callback(data, mntpnt);
145
allocator.Free(mntpnt); /* !!! FIXME: lose this malloc! */
146
} /* if */
147
148
149
150
151
152
153
154
155
} /* if */
} /* if */
} /* if */
} /* if */
} /* else */
} /* while */
} /* tryDir */
156
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
157
{
158
tryDir("/dev/disk", cb, data);
159
160
161
162
163
164
165
166
167
} /* __PHYSFS_platformDetectAvailableCDs */
static team_id getTeamID(void)
{
thread_info info;
thread_id tid = find_thread(NULL);
get_thread_info(tid, &info);
return(info.team);
168
} /* getTeamID */
169
170
171
172
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
173
174
175
176
177
178
179
180
181
182
image_info info;
int32 cookie = 0;
while (get_next_image_info(0, &cookie, &info) == B_OK)
{
if (info.type == B_APP_IMAGE)
break;
} /* while */
BEntry entry(info.name, true);
183
BPath path;
184
status_t rc = entry.GetPath(&path); /* (path) now has binary's path. */
185
186
187
188
189
assert(rc == B_OK);
rc = path.GetParent(&path); /* chop filename, keep directory. */
assert(rc == B_OK);
const char *str = path.Path();
assert(str != NULL);
190
char *retval = (char *) allocator.Malloc(strlen(str) + 1);
191
192
193
194
195
196
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
strcpy(retval, str);
return(retval);
} /* __PHYSFS_platformCalcBaseDir */
197
void *__PHYSFS_platformGetThreadID(void)
198
{
199
return((void *) find_thread(NULL));
200
201
202
203
204
} /* __PHYSFS_platformGetThreadID */
char *__PHYSFS_platformRealPath(const char *path)
{
205
BPath normalized(path, NULL, true); /* force normalization of path. */
206
const char *resolved_path = normalized.Path();
207
BAIL_IF_MACRO(resolved_path == NULL, ERR_NO_SUCH_FILE, NULL);
208
char *retval = (char *) allocator.Malloc(strlen(resolved_path) + 1);
209
210
211
212
213
214
215
216
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
strcpy(retval, resolved_path);
return(retval);
} /* __PHYSFS_platformRealPath */
void *__PHYSFS_platformCreateMutex(void)
{
217
return(new BLocker("PhysicsFS lock", true));
218
219
220
221
222
} /* __PHYSFS_platformCreateMutex */
void __PHYSFS_platformDestroyMutex(void *mutex)
{
223
delete ((BLocker *) mutex);
224
225
226
227
228
} /* __PHYSFS_platformDestroyMutex */
int __PHYSFS_platformGrabMutex(void *mutex)
{
229
return ((BLocker *) mutex)->Lock() ? 1 : 0;
230
231
232
233
234
} /* __PHYSFS_platformGrabMutex */
void __PHYSFS_platformReleaseMutex(void *mutex)
{
235
((BLocker *) mutex)->Unlock();
236
237
} /* __PHYSFS_platformReleaseMutex */
238
239
240
241
242
243
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
{
return(0); /* just use malloc() and friends. */
} /* __PHYSFS_platformSetDefaultAllocator */
244
#endif /* PHYSFS_PLATFORM_BEOS */
245
246
/* end of beos.cpp ... */