2002-05-24 11:46:43 +02:00
|
|
|
/*
|
|
|
|
* BeOS platform-dependent support routines for PhysicsFS.
|
|
|
|
*
|
2007-03-11 11:10:28 +01:00
|
|
|
* Please see the file LICENSE.txt in the source's root directory.
|
2002-05-24 11:46:43 +02:00
|
|
|
*
|
|
|
|
* This file written by Ryan C. Gordon.
|
|
|
|
*/
|
|
|
|
|
2007-03-11 23:50:53 +01:00
|
|
|
#define __PHYSICSFS_INTERNAL__
|
|
|
|
#include "physfs_platforms.h"
|
|
|
|
|
|
|
|
#ifdef PHYSFS_PLATFORM_BEOS
|
2002-05-24 11:46:43 +02:00
|
|
|
|
2008-11-05 20:42:48 +01:00
|
|
|
#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
|
2002-05-24 11:46:43 +02:00
|
|
|
#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>
|
2007-03-24 06:42:22 +01:00
|
|
|
#include <be/support/Locker.h>
|
2008-11-05 20:42:48 +01:00
|
|
|
#endif
|
2002-05-24 11:46:43 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#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);
|
2002-07-25 23:52:07 +02:00
|
|
|
BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
|
2002-05-24 11:46:43 +02:00
|
|
|
rc = directory.GetEntry(&entry);
|
2002-07-25 23:52:07 +02:00
|
|
|
BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
|
2002-05-24 11:46:43 +02:00
|
|
|
rc = entry.GetPath(&path);
|
2002-07-25 23:52:07 +02:00
|
|
|
BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
|
2002-05-24 11:46:43 +02:00
|
|
|
const char *str = path.Path();
|
|
|
|
BAIL_IF_MACRO(str == NULL, ERR_OS_ERROR, NULL); /* ?! */
|
2005-07-25 03:38:07 +02:00
|
|
|
char *retval = (char *) allocator.Malloc(strlen(str) + 1);
|
2002-05-24 11:46:43 +02:00
|
|
|
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/
|
|
|
|
*/
|
2004-09-29 08:09:29 +02:00
|
|
|
static void tryDir(const char *d, PHYSFS_StringCallback callback, void *data)
|
2002-05-24 11:46:43 +02:00
|
|
|
{
|
|
|
|
BDirectory dir;
|
2004-09-29 08:09:29 +02:00
|
|
|
dir.SetTo(d);
|
2002-05-24 11:46:43 +02:00
|
|
|
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)
|
2004-09-29 08:09:29 +02:00
|
|
|
tryDir(name, callback, data);
|
2002-05-24 11:46:43 +02:00
|
|
|
} /* 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)
|
2004-09-29 08:09:29 +02:00
|
|
|
{
|
|
|
|
callback(data, mntpnt);
|
2005-07-25 03:38:07 +02:00
|
|
|
allocator.Free(mntpnt); /* !!! FIXME: lose this malloc! */
|
2004-09-29 08:09:29 +02:00
|
|
|
} /* if */
|
2002-05-24 11:46:43 +02:00
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
close(devfd);
|
|
|
|
} /* else */
|
|
|
|
} /* while */
|
|
|
|
} /* tryDir */
|
|
|
|
|
|
|
|
|
2004-09-29 08:09:29 +02:00
|
|
|
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
|
2002-05-24 11:46:43 +02:00
|
|
|
{
|
2004-09-29 08:09:29 +02:00
|
|
|
tryDir("/dev/disk", cb, data);
|
2002-05-24 11:46:43 +02:00
|
|
|
} /* __PHYSFS_platformDetectAvailableCDs */
|
|
|
|
|
|
|
|
|
|
|
|
static team_id getTeamID(void)
|
|
|
|
{
|
|
|
|
thread_info info;
|
|
|
|
thread_id tid = find_thread(NULL);
|
|
|
|
get_thread_info(tid, &info);
|
|
|
|
return(info.team);
|
2007-03-21 06:03:17 +01:00
|
|
|
} /* getTeamID */
|
2002-05-24 11:46:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
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);
|
2002-07-25 23:52:07 +02:00
|
|
|
BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL);
|
2002-05-24 11:46:43 +02:00
|
|
|
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);
|
2005-07-25 03:38:07 +02:00
|
|
|
char *retval = (char *) allocator.Malloc(strlen(str) + 1);
|
2002-05-24 11:46:43 +02:00
|
|
|
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 */
|
|
|
|
|
|
|
|
|
|
|
|
char *__PHYSFS_platformRealPath(const char *path)
|
|
|
|
{
|
2006-11-05 20:06:23 +01:00
|
|
|
BPath normalized(path, NULL, true); /* force normalization of path. */
|
2002-05-24 11:46:43 +02:00
|
|
|
const char *resolved_path = normalized.Path();
|
2002-07-23 09:46:36 +02:00
|
|
|
BAIL_IF_MACRO(resolved_path == NULL, ERR_NO_SUCH_FILE, NULL);
|
2005-07-25 03:38:07 +02:00
|
|
|
char *retval = (char *) allocator.Malloc(strlen(resolved_path) + 1);
|
2002-05-24 11:46:43 +02:00
|
|
|
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
strcpy(retval, resolved_path);
|
|
|
|
return(retval);
|
|
|
|
} /* __PHYSFS_platformRealPath */
|
|
|
|
|
|
|
|
|
2007-03-21 06:03:17 +01:00
|
|
|
char *__PHYSFS_platformCurrentDir(void)
|
|
|
|
{
|
|
|
|
return(__PHYSFS_platformRealPath(".")); /* let BPath sort it out. */
|
|
|
|
} /* __PHYSFS_platformCurrentDir */
|
|
|
|
|
|
|
|
|
2002-05-24 11:46:43 +02:00
|
|
|
void *__PHYSFS_platformCreateMutex(void)
|
|
|
|
{
|
2007-03-24 06:42:22 +01:00
|
|
|
return(new BLocker("PhysicsFS lock", true));
|
2002-05-24 11:46:43 +02:00
|
|
|
} /* __PHYSFS_platformCreateMutex */
|
|
|
|
|
|
|
|
|
|
|
|
void __PHYSFS_platformDestroyMutex(void *mutex)
|
|
|
|
{
|
2007-03-24 06:42:22 +01:00
|
|
|
delete ((BLocker *) mutex);
|
2002-05-24 11:46:43 +02:00
|
|
|
} /* __PHYSFS_platformDestroyMutex */
|
|
|
|
|
|
|
|
|
|
|
|
int __PHYSFS_platformGrabMutex(void *mutex)
|
|
|
|
{
|
2007-03-24 06:42:22 +01:00
|
|
|
return ((BLocker *) mutex)->Lock() ? 1 : 0;
|
2002-05-24 11:46:43 +02:00
|
|
|
} /* __PHYSFS_platformGrabMutex */
|
|
|
|
|
|
|
|
|
|
|
|
void __PHYSFS_platformReleaseMutex(void *mutex)
|
|
|
|
{
|
2007-03-24 06:42:22 +01:00
|
|
|
((BLocker *) mutex)->Unlock();
|
2002-05-24 11:46:43 +02:00
|
|
|
} /* __PHYSFS_platformReleaseMutex */
|
|
|
|
|
2007-03-20 19:33:56 +01:00
|
|
|
|
|
|
|
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
|
|
|
|
{
|
|
|
|
return(0); /* just use malloc() and friends. */
|
|
|
|
} /* __PHYSFS_platformSetDefaultAllocator */
|
|
|
|
|
2007-03-11 23:50:53 +01:00
|
|
|
#endif /* PHYSFS_PLATFORM_BEOS */
|
2002-05-24 11:46:43 +02:00
|
|
|
|
|
|
|
/* end of beos.cpp ... */
|
|
|
|
|