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 #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 #include <be/support/Locker.h>
31 #include "physfs_internal.h"
34 int __PHYSFS_platformInit(void)
36 return(1); /* always succeed. */
37 } /* __PHYSFS_platformInit */
40 int __PHYSFS_platformDeinit(void)
42 return(1); /* always succeed. */
43 } /* __PHYSFS_platformDeinit */
46 static char *getMountPoint(const char *devname)
52 while (mounts.GetNextVolume(&vol) == B_NO_ERROR)
55 fs_stat_dev(vol.Device(), &fsinfo);
56 if (strcmp(devname, fsinfo.device_name) == 0)
58 //char buf[B_FILE_NAME_LENGTH];
63 rc = vol.GetRootDirectory(&directory);
64 BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
65 rc = directory.GetEntry(&entry);
66 BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
67 rc = entry.GetPath(&path);
68 BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
69 const char *str = path.Path();
70 BAIL_IF_MACRO(str == NULL, ERR_OS_ERROR, NULL); /* ?! */
71 char *retval = (char *) allocator.Malloc(strlen(str) + 1);
72 BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
83 * This function is lifted from Simple Directmedia Layer (SDL):
84 * http://www.libsdl.org/
86 static void tryDir(const char *d, PHYSFS_StringCallback callback, void *data)
90 if (dir.InitCheck() != B_NO_ERROR)
95 while (dir.GetNextEntry(&entry) >= 0)
101 if (entry.GetPath(&path) != B_NO_ERROR)
106 if (entry.GetRef(&e) != B_NO_ERROR)
109 if (entry.IsDirectory())
111 if (strcmp(e.name, "floppy") != 0)
112 tryDir(name, callback, data);
121 if (strcmp(e.name, "raw") == 0) /* ignore partitions. */
123 int devfd = open(name, O_RDONLY);
126 if (ioctl(devfd, B_GET_GEOMETRY, &g, sizeof(g)) >= 0)
128 if (g.device_type == B_CD)
130 char *mntpnt = getMountPoint(name);
133 callback(data, mntpnt);
134 allocator.Free(mntpnt); /* !!! FIXME: lose this malloc! */
147 void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
149 tryDir("/dev/disk", cb, data);
150 } /* __PHYSFS_platformDetectAvailableCDs */
153 static team_id getTeamID(void)
156 thread_id tid = find_thread(NULL);
157 get_thread_info(tid, &info);
162 char *__PHYSFS_platformCalcBaseDir(const char *argv0)
164 /* in case there isn't a BApplication yet, we'll construct a roster. */
167 status_t rc = roster.GetRunningAppInfo(getTeamID(), &info);
168 BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
169 BEntry entry(&(info.ref), true);
171 rc = entry.GetPath(&path); /* (path) now has binary's path. */
173 rc = path.GetParent(&path); /* chop filename, keep directory. */
175 const char *str = path.Path();
177 char *retval = (char *) allocator.Malloc(strlen(str) + 1);
178 BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
181 } /* __PHYSFS_platformCalcBaseDir */
184 PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
186 return((PHYSFS_uint64) find_thread(NULL));
187 } /* __PHYSFS_platformGetThreadID */
190 char *__PHYSFS_platformRealPath(const char *path)
192 BPath normalized(path, NULL, true); /* force normalization of path. */
193 const char *resolved_path = normalized.Path();
194 BAIL_IF_MACRO(resolved_path == NULL, ERR_NO_SUCH_FILE, NULL);
195 char *retval = (char *) allocator.Malloc(strlen(resolved_path) + 1);
196 BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
197 strcpy(retval, resolved_path);
199 } /* __PHYSFS_platformRealPath */
202 char *__PHYSFS_platformCurrentDir(void)
204 return(__PHYSFS_platformRealPath(".")); /* let BPath sort it out. */
205 } /* __PHYSFS_platformCurrentDir */
208 void *__PHYSFS_platformCreateMutex(void)
210 return(new BLocker("PhysicsFS lock", true));
211 } /* __PHYSFS_platformCreateMutex */
214 void __PHYSFS_platformDestroyMutex(void *mutex)
216 delete ((BLocker *) mutex);
217 } /* __PHYSFS_platformDestroyMutex */
220 int __PHYSFS_platformGrabMutex(void *mutex)
222 return ((BLocker *) mutex)->Lock() ? 1 : 0;
223 } /* __PHYSFS_platformGrabMutex */
226 void __PHYSFS_platformReleaseMutex(void *mutex)
228 ((BLocker *) mutex)->Unlock();
229 } /* __PHYSFS_platformReleaseMutex */
232 int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
234 return(0); /* just use malloc() and friends. */
235 } /* __PHYSFS_platformSetDefaultAllocator */
237 #endif /* PHYSFS_PLATFORM_BEOS */
239 /* end of beos.cpp ... */