Reworked BeOS CD detection code.

Cleaned up, removed a malloc(), removed unnecessary BAIL macros.
This commit is contained in:
Ryan C. Gordon 2012-03-14 21:24:32 -04:00
parent abd2624c59
commit 466f538926
1 changed files with 33 additions and 43 deletions

View File

@ -53,7 +53,7 @@ int __PHYSFS_platformDeinit(void)
} /* __PHYSFS_platformDeinit */ } /* __PHYSFS_platformDeinit */
static char *getMountPoint(const char *devname) static char *getMountPoint(const char *devname, char *buf, size_t bufsize)
{ {
BVolumeRoster mounts; BVolumeRoster mounts;
BVolume vol; BVolume vol;
@ -65,33 +65,30 @@ static char *getMountPoint(const char *devname)
fs_stat_dev(vol.Device(), &fsinfo); fs_stat_dev(vol.Device(), &fsinfo);
if (strcmp(devname, fsinfo.device_name) == 0) if (strcmp(devname, fsinfo.device_name) == 0)
{ {
//char buf[B_FILE_NAME_LENGTH];
BDirectory directory; BDirectory directory;
BEntry entry; BEntry entry;
BPath path; BPath path;
status_t rc; const char *str;
rc = vol.GetRootDirectory(&directory);
BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL); if ( (vol.GetRootDirectory(&directory) < B_OK) ||
rc = directory.GetEntry(&entry); (directory.GetEntry(&entry) < B_OK) ||
BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL); (entry.GetPath(&path) < B_OK) ||
rc = entry.GetPath(&path); ( (str = path.Path()) == NULL) )
BAIL_IF_MACRO(rc < B_OK, strerror(rc), NULL); return NULL;
const char *str = path.Path();
BAIL_IF_MACRO(str == NULL, ERR_OS_ERROR, NULL); /* ?! */ strncpy(buf, str, bufsize-1);
char *retval = (char *) allocator.Malloc(strlen(str) + 1); buf[bufsize-1] = '\0';
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); return buf;
strcpy(retval, str);
return(retval);
} /* if */ } /* if */
} /* while */ } /* while */
return(NULL); return NULL;
} /* getMountPoint */ } /* getMountPoint */
/* /*
* This function is lifted from Simple Directmedia Layer (SDL): * This function is lifted from Simple Directmedia Layer (SDL):
* http://www.libsdl.org/ * http://www.libsdl.org/ ... this is zlib-licensed code, too.
*/ */
static void tryDir(const char *d, PHYSFS_StringCallback callback, void *data) static void tryDir(const char *d, PHYSFS_StringCallback callback, void *data)
{ {
@ -120,35 +117,28 @@ static void tryDir(const char *d, PHYSFS_StringCallback callback, void *data)
{ {
if (strcmp(e.name, "floppy") != 0) if (strcmp(e.name, "floppy") != 0)
tryDir(name, callback, data); tryDir(name, callback, data);
continue;
} /* if */ } /* if */
else if (strcmp(e.name, "raw") != 0) /* ignore partitions. */
{ continue;
bool add_it = false;
device_geometry g;
if (strcmp(e.name, "raw") == 0) /* ignore partitions. */
{
const int devfd = open(name, O_RDONLY); const int devfd = open(name, O_RDONLY);
if (devfd >= 0) if (devfd < 0)
{ continue;
const int rc = ioctl(devfd, B_GET_GEOMETRY, &g, sizeof(g));
device_geometry g;
const int rc = ioctl(devfd, B_GET_GEOMETRY, &g, sizeof (g));
close(devfd); close(devfd);
if (rc >= 0) if (rc < 0)
{ continue;
if (g.device_type == B_CD)
{ if (g.device_type != B_CD)
char *mntpnt = getMountPoint(name); continue;
if (mntpnt != NULL)
{ char mntpnt[B_FILE_NAME_LENGTH];
if (getMountPoint(name, mntpnt, sizeof (mntpnt)))
callback(data, mntpnt); callback(data, mntpnt);
allocator.Free(mntpnt); /* !!! FIXME: lose this malloc! */
} /* if */
} /* if */
} /* if */
} /* if */
} /* if */
} /* else */
} /* while */ } /* while */
} /* tryDir */ } /* tryDir */