Skip to content

Add support for mount priority #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions src/physfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ typedef struct __PHYSFS_DIRHANDLE__
size_t rootlen; /* subdirectory of archiver to use as root of archive (NULL for actual root) */
const PHYSFS_Archiver *funcs; /* Ptr to archiver info for this handle. */
struct __PHYSFS_DIRHANDLE__ *next; /* linked list stuff. */
int priority;
} DirHandle;


Expand Down Expand Up @@ -1749,7 +1750,7 @@ int PHYSFS_setRoot(const char *archive, const char *subdir)


static int doMount(PHYSFS_Io *io, const char *fname,
const char *mountPoint, int appendToPath)
const char *mountPoint, int priority)
{
DirHandle *dh;
DirHandle *prev = NULL;
Expand All @@ -1767,23 +1768,24 @@ static int doMount(PHYSFS_Io *io, const char *fname,
/* already in search path? */
if ((i->dirName != NULL) && (strcmp(fname, i->dirName) == 0))
BAIL_MUTEX_ERRPASS(stateLock, 1);
prev = i;
if ((priority < 0) || ((priority > 0) && (i->priority <= priority)))
prev = i;
} /* for */

dh = createDirHandle(io, fname, mountPoint, 0);
BAIL_IF_MUTEX_ERRPASS(!dh, stateLock, 0);

if (appendToPath)
dh->priority = priority;

if (prev == NULL)
{
if (prev == NULL)
searchPath = dh;
else
prev->next = dh;
dh->next = searchPath;
searchPath = dh;
} /* if */
else
{
dh->next = searchPath;
searchPath = dh;
dh->next = prev->next;
prev->next = dh;
} /* else */

__PHYSFS_platformReleaseMutex(stateLock);
Expand All @@ -1792,18 +1794,18 @@ static int doMount(PHYSFS_Io *io, const char *fname,


int PHYSFS_mountIo(PHYSFS_Io *io, const char *fname,
const char *mountPoint, int appendToPath)
const char *mountPoint, int priority)
{
BAIL_IF(!io, PHYSFS_ERR_INVALID_ARGUMENT, 0);
BAIL_IF(!fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
BAIL_IF(io->version != 0, PHYSFS_ERR_UNSUPPORTED, 0);
return doMount(io, fname, mountPoint, appendToPath);
return doMount(io, fname, mountPoint, priority);
} /* PHYSFS_mountIo */


int PHYSFS_mountMemory(const void *buf, PHYSFS_uint64 len, void (*del)(void *),
const char *fname, const char *mountPoint,
int appendToPath)
int priority)
{
int retval = 0;
PHYSFS_Io *io = NULL;
Expand All @@ -1813,7 +1815,7 @@ int PHYSFS_mountMemory(const void *buf, PHYSFS_uint64 len, void (*del)(void *),

io = __PHYSFS_createMemoryIo(buf, len, del);
BAIL_IF_ERRPASS(!io, 0);
retval = doMount(io, fname, mountPoint, appendToPath);
retval = doMount(io, fname, mountPoint, priority);
if (!retval)
{
/* docs say not to call (del) in case of failure, so cheat. */
Expand All @@ -1827,7 +1829,7 @@ int PHYSFS_mountMemory(const void *buf, PHYSFS_uint64 len, void (*del)(void *),


int PHYSFS_mountHandle(PHYSFS_File *file, const char *fname,
const char *mountPoint, int appendToPath)
const char *mountPoint, int priority)
{
int retval = 0;
PHYSFS_Io *io = NULL;
Expand All @@ -1837,7 +1839,7 @@ int PHYSFS_mountHandle(PHYSFS_File *file, const char *fname,

io = __PHYSFS_createHandleIo(file);
BAIL_IF_ERRPASS(!io, 0);
retval = doMount(io, fname, mountPoint, appendToPath);
retval = doMount(io, fname, mountPoint, priority);
if (!retval)
{
/* docs say not to destruct in case of failure, so cheat. */
Expand All @@ -1849,16 +1851,16 @@ int PHYSFS_mountHandle(PHYSFS_File *file, const char *fname,
} /* PHYSFS_mountHandle */


int PHYSFS_mount(const char *newDir, const char *mountPoint, int appendToPath)
int PHYSFS_mount(const char *newDir, const char *mountPoint, int priority)
{
BAIL_IF(!newDir, PHYSFS_ERR_INVALID_ARGUMENT, 0);
return doMount(NULL, newDir, mountPoint, appendToPath);
return doMount(NULL, newDir, mountPoint, priority);
} /* PHYSFS_mount */


int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
{
return PHYSFS_mount(newDir, NULL, appendToPath);
return PHYSFS_mount(newDir, NULL, appendToPath ? -1 : 0);
} /* PHYSFS_addToSearchPath */


Expand Down
25 changes: 13 additions & 12 deletions src/physfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2184,7 +2184,7 @@ PHYSFS_DECL int PHYSFS_setAllocator(const PHYSFS_Allocator *allocator);


/**
* \fn int PHYSFS_mount(const char *newDir, const char *mountPoint, int appendToPath)
* \fn int PHYSFS_mount(const char *newDir, const char *mountPoint, int priority)
* \brief Add an archive or directory to the search path.
*
* If this is a duplicate, the entry is not added again, even though the
Expand Down Expand Up @@ -2218,7 +2218,8 @@ PHYSFS_DECL int PHYSFS_setAllocator(const PHYSFS_Allocator *allocator);
* \param mountPoint Location in the interpolated tree that this archive
* will be "mounted", in platform-independent notation.
* NULL or "" is equivalent to "/".
* \param appendToPath nonzero to append to search path, zero to prepend.
* \param priority Archive priority. Positive inserts archive AFTER others
* with the same priority; negative appends, zero prepends.
* \return nonzero if added to path, zero on failure (bogus archive, dir
* missing, etc). Use PHYSFS_getLastErrorCode() to obtain
* the specific error.
Expand All @@ -2230,7 +2231,7 @@ PHYSFS_DECL int PHYSFS_setAllocator(const PHYSFS_Allocator *allocator);
*/
PHYSFS_DECL int PHYSFS_mount(const char *newDir,
const char *mountPoint,
int appendToPath);
int priority);

/**
* \fn int PHYSFS_getMountPoint(const char *dir)
Expand Down Expand Up @@ -3226,7 +3227,7 @@ typedef struct PHYSFS_Io


/**
* \fn int PHYSFS_mountIo(PHYSFS_Io *io, const char *newDir, const char *mountPoint, int appendToPath)
* \fn int PHYSFS_mountIo(PHYSFS_Io *io, const char *newDir, const char *mountPoint, int priority)
* \brief Add an archive, built on a PHYSFS_Io, to the search path.
*
* \warning Unless you have some special, low-level need, you should be using
Expand Down Expand Up @@ -3256,7 +3257,7 @@ typedef struct PHYSFS_Io
* \param mountPoint Location in the interpolated tree that this archive
* will be "mounted", in platform-independent notation.
* NULL or "" is equivalent to "/".
* \param appendToPath nonzero to append to search path, zero to prepend.
* \param priority Archive priority; see PHYSFS_mount.
* \return nonzero if added to path, zero on failure (bogus archive, stream
* i/o issue, etc). Use PHYSFS_getLastErrorCode() to obtain
* the specific error.
Expand All @@ -3266,11 +3267,11 @@ typedef struct PHYSFS_Io
* \sa PHYSFS_getMountPoint
*/
PHYSFS_DECL int PHYSFS_mountIo(PHYSFS_Io *io, const char *newDir,
const char *mountPoint, int appendToPath);
const char *mountPoint, int priority);


/**
* \fn int PHYSFS_mountMemory(const void *buf, PHYSFS_uint64 len, void (*del)(void *), const char *newDir, const char *mountPoint, int appendToPath)
* \fn int PHYSFS_mountMemory(const void *buf, PHYSFS_uint64 len, void (*del)(void *), const char *newDir, const char *mountPoint, int priority)
* \brief Add an archive, contained in a memory buffer, to the search path.
*
* \warning Unless you have some special, low-level need, you should be using
Expand Down Expand Up @@ -3304,7 +3305,7 @@ PHYSFS_DECL int PHYSFS_mountIo(PHYSFS_Io *io, const char *newDir,
* \param mountPoint Location in the interpolated tree that this archive
* will be "mounted", in platform-independent notation.
* NULL or "" is equivalent to "/".
* \param appendToPath nonzero to append to search path, zero to prepend.
* \param priority Archive priority; see PHYSFS_mount.
* \return nonzero if added to path, zero on failure (bogus archive, etc).
* Use PHYSFS_getLastErrorCode() to obtain the specific error.
*
Expand All @@ -3314,11 +3315,11 @@ PHYSFS_DECL int PHYSFS_mountIo(PHYSFS_Io *io, const char *newDir,
*/
PHYSFS_DECL int PHYSFS_mountMemory(const void *buf, PHYSFS_uint64 len,
void (*del)(void *), const char *newDir,
const char *mountPoint, int appendToPath);
const char *mountPoint, int priority);


/**
* \fn int PHYSFS_mountHandle(PHYSFS_File *file, const char *newDir, const char *mountPoint, int appendToPath)
* \fn int PHYSFS_mountHandle(PHYSFS_File *file, const char *newDir, const char *mountPoint, int priority)
* \brief Add an archive, contained in a PHYSFS_File handle, to the search path.
*
* \warning Unless you have some special, low-level need, you should be using
Expand Down Expand Up @@ -3362,7 +3363,7 @@ PHYSFS_DECL int PHYSFS_mountMemory(const void *buf, PHYSFS_uint64 len,
* \param mountPoint Location in the interpolated tree that this archive
* will be "mounted", in platform-independent notation.
* NULL or "" is equivalent to "/".
* \param appendToPath nonzero to append to search path, zero to prepend.
* \param priority Archive priority; see PHYSFS_mount.
* \return nonzero if added to path, zero on failure (bogus archive, etc).
* Use PHYSFS_getLastErrorCode() to obtain the specific error.
*
Expand All @@ -3371,7 +3372,7 @@ PHYSFS_DECL int PHYSFS_mountMemory(const void *buf, PHYSFS_uint64 len,
* \sa PHYSFS_getMountPoint
*/
PHYSFS_DECL int PHYSFS_mountHandle(PHYSFS_File *file, const char *newDir,
const char *mountPoint, int appendToPath);
const char *mountPoint, int priority);


/**
Expand Down