2 * BeOS platform-dependent support routines for PhysicsFS.
4 * Please see the file LICENSE.txt in the source's root directory.
6 * This file written by Ryan C. Gordon.
9 #define __PHYSICSFS_INTERNAL__
10 #include "physfs_platforms.h"
12 #ifdef PHYSFS_PLATFORM_BEOS
14 #ifdef PHYSFS_PLATFORM_HAIKU
15 #include <os/kernel/OS.h>
16 #include <os/app/Roster.h>
17 #include <os/storage/Volume.h>
18 #include <os/storage/VolumeRoster.h>
19 #include <os/storage/Directory.h>
20 #include <os/storage/Entry.h>
21 #include <os/storage/Path.h>
22 #include <os/kernel/fs_info.h>
23 #include <os/device/scsi.h>
24 #include <os/support/Locker.h>
26 #include <be/kernel/OS.h>
27 #include <be/app/Roster.h>
28 #include <be/storage/Volume.h>
29 #include <be/storage/VolumeRoster.h>
30 #include <be/storage/Directory.h>
31 #include <be/storage/Entry.h>
32 #include <be/storage/Path.h>
33 #include <be/kernel/fs_info.h>
34 #include <be/device/scsi.h>
35 #include <be/support/Locker.h>
44 #include "physfs_internal.h"
47 int __PHYSFS_platformInit(void)
49 return(1); /* always succeed. */
50 } /* __PHYSFS_platformInit */
53 int __PHYSFS_platformDeinit(void)
55 return(1); /* always succeed. */
56 } /* __PHYSFS_platformDeinit */
59 static char *getMountPoint(const char *devname)
65 while (mounts.GetNextVolume(&vol) == B_NO_ERROR)
68 fs_stat_dev(vol.Device(), &fsinfo);
69 if (strcmp(devname, fsinfo.device_name) == 0)
71 //char buf[B_FILE_NAME_LENGTH];
76 rc = vol.GetRootDirectory(&directory);
77 BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
78 rc = directory.GetEntry(&entry);
79 BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
80 rc = entry.GetPath(&path);
81 BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
82 const char *str = path.Path();
83 BAIL_IF_MACRO(str == NULL, ERR_OS_ERROR, NULL); /* ?! */
84 char *retval = (char *) allocator.Malloc(strlen(str) + 1);
85 BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
96 * This function is lifted from Simple Directmedia Layer (SDL):
97 * http://www.libsdl.org/
99 static void tryDir(const char *d, PHYSFS_StringCallback callback, void *data)
103 if (dir.InitCheck() != B_NO_ERROR)
108 while (dir.GetNextEntry(&entry) >= 0)
114 if (entry.GetPath(&path) != B_NO_ERROR)
119 if (entry.GetRef(&e) != B_NO_ERROR)
122 if (entry.IsDirectory())
124 if (strcmp(e.name, "floppy") != 0)
125 tryDir(name, callback, data);
134 if (strcmp(e.name, "raw") == 0) /* ignore partitions. */
136 int devfd = open(name, O_RDONLY);
139 if (ioctl(devfd, B_GET_GEOMETRY, &g, sizeof(g)) >= 0)
141 if (g.device_type == B_CD)
143 char *mntpnt = getMountPoint(name);
146 callback(data, mntpnt);
147 allocator.Free(mntpnt); /* !!! FIXME: lose this malloc! */
160 void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
162 tryDir("/dev/disk", cb, data);
163 } /* __PHYSFS_platformDetectAvailableCDs */
166 static team_id getTeamID(void)
169 thread_id tid = find_thread(NULL);
170 get_thread_info(tid, &info);
175 char *__PHYSFS_platformCalcBaseDir(const char *argv0)
180 while (get_next_image_info(0, &cookie, &info) == B_OK) {
181 if (info.type == B_APP_IMAGE)
185 BEntry entry(info.name, true);
187 status_t rc = entry.GetPath(&path); /* (path) now has binary's path. */
189 rc = path.GetParent(&path); /* chop filename, keep directory. */
191 const char *str = path.Path();
193 char *retval = (char *) allocator.Malloc(strlen(str) + 1);
194 BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
197 } /* __PHYSFS_platformCalcBaseDir */
200 PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
202 return((PHYSFS_uint64) find_thread(NULL));
203 } /* __PHYSFS_platformGetThreadID */
206 char *__PHYSFS_platformRealPath(const char *path)
208 BPath normalized(path, NULL, true); /* force normalization of path. */
209 const char *resolved_path = normalized.Path();
210 BAIL_IF_MACRO(resolved_path == NULL, ERR_NO_SUCH_FILE, NULL);
211 char *retval = (char *) allocator.Malloc(strlen(resolved_path) + 1);
212 BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
213 strcpy(retval, resolved_path);
215 } /* __PHYSFS_platformRealPath */
218 char *__PHYSFS_platformCurrentDir(void)
220 return(__PHYSFS_platformRealPath(".")); /* let BPath sort it out. */
221 } /* __PHYSFS_platformCurrentDir */
224 void *__PHYSFS_platformCreateMutex(void)
226 return(new BLocker("PhysicsFS lock", true));
227 } /* __PHYSFS_platformCreateMutex */
230 void __PHYSFS_platformDestroyMutex(void *mutex)
232 delete ((BLocker *) mutex);
233 } /* __PHYSFS_platformDestroyMutex */
236 int __PHYSFS_platformGrabMutex(void *mutex)
238 return ((BLocker *) mutex)->Lock() ? 1 : 0;
239 } /* __PHYSFS_platformGrabMutex */
242 void __PHYSFS_platformReleaseMutex(void *mutex)
244 ((BLocker *) mutex)->Unlock();
245 } /* __PHYSFS_platformReleaseMutex */
248 int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
250 return(0); /* just use malloc() and friends. */
251 } /* __PHYSFS_platformSetDefaultAllocator */
253 #endif /* PHYSFS_PLATFORM_BEOS */
255 /* end of beos.cpp ... */