Update fs.

This commit is contained in:
David Reid
2026-05-02 15:43:29 +10:00
parent 09615e6bc1
commit 326d7ec9a5
2 changed files with 58 additions and 35 deletions
+36 -13
View File
@@ -2102,18 +2102,22 @@ static fs_opened_archive* fs_find_opened_archive(fs* pFS, const char* pArchivePa
} }
FS_ASSERT(pArchivePath != NULL); FS_ASSERT(pArchivePath != NULL);
FS_ASSERT(archivePathLen > 0); FS_ASSERT(archivePathLen != 0);
if (archivePathLen == FS_NULL_TERMINATED) {
archivePathLen = strlen(pArchivePath);
}
cursor = 0; cursor = 0;
while (cursor < pFS->openedArchivesSize) { while (cursor < pFS->openedArchivesSize) {
fs_opened_archive* pOpenedArchive = (fs_opened_archive*)FS_OFFSET_PTR(pFS->pOpenedArchives, cursor); fs_opened_archive* pOpenedArchive = (fs_opened_archive*)FS_OFFSET_PTR(pFS->pOpenedArchives, cursor);
if (fs_strncmp(pOpenedArchive->pPath, pArchivePath, archivePathLen) == 0) { if (strlen(pOpenedArchive->pPath) == archivePathLen && fs_strncmp(pOpenedArchive->pPath, pArchivePath, archivePathLen) == 0) {
return pOpenedArchive; return pOpenedArchive;
} }
/* Getting here means this archive is not the one we're looking for. */ /* Getting here means this archive is not the one we're looking for. */
cursor += FS_ALIGN(sizeof(fs*) + sizeof(size_t) + strlen(pOpenedArchive->pPath) + 1, FS_SIZEOF_PTR); cursor += FS_ALIGN(sizeof(pOpenedArchive->pArchive) + strlen(pOpenedArchive->pPath) + 1, FS_SIZEOF_PTR);
} }
/* If we get here it means we couldn't find the archive by it's name. */ /* If we get here it means we couldn't find the archive by it's name. */
@@ -2140,7 +2144,7 @@ static fs_opened_archive* fs_find_opened_archive_by_fs(fs* pFS, fs* pArchive)
} }
/* Getting here means this archive is not the one we're looking for. */ /* Getting here means this archive is not the one we're looking for. */
cursor += FS_ALIGN(sizeof(fs*) + sizeof(size_t) + strlen(pOpenedArchive->pPath) + 1, FS_SIZEOF_PTR); cursor += FS_ALIGN(sizeof(pOpenedArchive->pArchive) + strlen(pOpenedArchive->pPath) + 1, FS_SIZEOF_PTR);
} }
/* If we get here it means we couldn't find the archive. */ /* If we get here it means we couldn't find the archive. */
@@ -2161,7 +2165,7 @@ static fs_result fs_add_opened_archive(fs* pFS, fs* pArchive, const char* pArchi
archivePathLen = strlen(pArchivePath); archivePathLen = strlen(pArchivePath);
} }
openedArchiveSize = FS_ALIGN(sizeof(fs*) + sizeof(size_t) + archivePathLen + 1, FS_SIZEOF_PTR); openedArchiveSize = FS_ALIGN(sizeof(pOpenedArchive->pArchive) + archivePathLen + 1, FS_SIZEOF_PTR);
if (pFS->openedArchivesSize + openedArchiveSize > pFS->openedArchivesCap) { if (pFS->openedArchivesSize + openedArchiveSize > pFS->openedArchivesCap) {
size_t newOpenedArchivesCap; size_t newOpenedArchivesCap;
@@ -2198,7 +2202,7 @@ static fs_result fs_remove_opened_archive(fs* pFS, fs_opened_archive* pOpenedArc
/* This is a simple matter of doing a memmove() to move memory down. pOpenedArchive should be an offset of pFS->pOpenedArchives. */ /* This is a simple matter of doing a memmove() to move memory down. pOpenedArchive should be an offset of pFS->pOpenedArchives. */
size_t openedArchiveSize; size_t openedArchiveSize;
openedArchiveSize = FS_ALIGN(sizeof(fs_opened_archive*) + sizeof(size_t) + strlen(pOpenedArchive->pPath) + 1, FS_SIZEOF_PTR); openedArchiveSize = FS_ALIGN(sizeof(pOpenedArchive->pArchive) + strlen(pOpenedArchive->pPath) + 1, FS_SIZEOF_PTR);
FS_ASSERT(((fs_uintptr)pOpenedArchive + openedArchiveSize) > ((fs_uintptr)pFS->pOpenedArchives)); FS_ASSERT(((fs_uintptr)pOpenedArchive + openedArchiveSize) > ((fs_uintptr)pFS->pOpenedArchives));
FS_ASSERT(((fs_uintptr)pOpenedArchive + openedArchiveSize) <= ((fs_uintptr)pFS->pOpenedArchives + pFS->openedArchivesSize)); FS_ASSERT(((fs_uintptr)pOpenedArchive + openedArchiveSize) <= ((fs_uintptr)pFS->pOpenedArchives + pFS->openedArchivesSize));
@@ -3462,6 +3466,7 @@ static fs_result fs_file_alloc_and_open_or_info(fs* pFS, const char* pFilePath,
int dirPathLen; int dirPathLen;
dirPathLen = fs_path_directory(pDirPathStack, sizeof(pDirPathStack), pFilePath, FS_NULL_TERMINATED); dirPathLen = fs_path_directory(pDirPathStack, sizeof(pDirPathStack), pFilePath, FS_NULL_TERMINATED);
if (dirPathLen > 0) {
if (dirPathLen >= (int)sizeof(pDirPathStack)) { if (dirPathLen >= (int)sizeof(pDirPathStack)) {
pDirPathHeap = (char*)fs_malloc(dirPathLen + 1, fs_get_allocation_callbacks(pFS)); pDirPathHeap = (char*)fs_malloc(dirPathLen + 1, fs_get_allocation_callbacks(pFS));
if (pDirPathHeap == NULL) { if (pDirPathHeap == NULL) {
@@ -3483,12 +3488,16 @@ static fs_result fs_file_alloc_and_open_or_info(fs* pFS, const char* pFilePath,
pDirPath = pDirPathStack; pDirPath = pDirPathStack;
} }
/* Don't try creating a directory if there is */
result = fs_mkdir(pFS, pDirPath, FS_IGNORE_MOUNTS); result = fs_mkdir(pFS, pDirPath, FS_IGNORE_MOUNTS);
if (result != FS_SUCCESS && result != FS_ALREADY_EXISTS) { if (result != FS_SUCCESS && result != FS_ALREADY_EXISTS) {
fs_stream_delete_duplicate((*ppFile)->pStreamForBackend, fs_get_allocation_callbacks(pFS)); fs_stream_delete_duplicate((*ppFile)->pStreamForBackend, fs_get_allocation_callbacks(pFS));
fs_file_free(ppFile); fs_file_free(ppFile);
return result; return result;
} }
} else {
/* Getting here means the file was specified without a leading path and there is nothing to create. */
}
} }
result = fs_backend_file_open(pBackend, pFS, (*ppFile)->pStreamForBackend, pFilePath, openMode, *ppFile); result = fs_backend_file_open(pBackend, pFS, (*ppFile)->pStreamForBackend, pFilePath, openMode, *ppFile);
@@ -3991,7 +4000,7 @@ static void fs_iterator_internal_resolve_public_members(fs_iterator_internal* pI
pIterator->base.info = pIterator->ppItems[pIterator->itemIndex]->info; pIterator->base.info = pIterator->ppItems[pIterator->itemIndex]->info;
} }
static fs_iterator_item* fs_iterator_internal_find(fs_iterator_internal* pIterator, const char* pName) static fs_iterator_item* fs_iterator_internal_find(fs_iterator_internal* pIterator, const char* pName, size_t nameLen)
{ {
/* /*
We cannot use ppItems here because this function will be called before that has been set up. Instead we need We cannot use ppItems here because this function will be called before that has been set up. Instead we need
@@ -4002,7 +4011,7 @@ static fs_iterator_item* fs_iterator_internal_find(fs_iterator_internal* pIterat
for (iItem = 0; iItem < pIterator->itemCount; iItem += 1) { for (iItem = 0; iItem < pIterator->itemCount; iItem += 1) {
fs_iterator_item* pItem = (fs_iterator_item*)FS_OFFSET_PTR(pIterator, sizeof(fs_iterator_internal) + cursor); fs_iterator_item* pItem = (fs_iterator_item*)FS_OFFSET_PTR(pIterator, sizeof(fs_iterator_internal) + cursor);
if (fs_strncmp(fs_iterator_item_name(pItem), pName, pItem->nameLen) == 0) { if (pItem->nameLen == nameLen && fs_strncmp(fs_iterator_item_name(pItem), pName, pItem->nameLen) == 0) {
return pItem; return pItem;
} }
@@ -4031,7 +4040,7 @@ static fs_iterator_internal* fs_iterator_internal_append(fs_iterator_internal* p
/* Check if the item already exists. If so, skip it. */ /* Check if the item already exists. If so, skip it. */
if (pIterator != NULL) { if (pIterator != NULL) {
pNewItem = fs_iterator_internal_find(pIterator, pOther->pName); pNewItem = fs_iterator_internal_find(pIterator, pOther->pName, pOther->nameLen);
if (pNewItem != NULL) { if (pNewItem != NULL) {
return pIterator; /* Already exists. Skip it. */ return pIterator; /* Already exists. Skip it. */
} }
@@ -4672,7 +4681,7 @@ static void fs_gc_nolock(fs* pFS, int policy, fs* pSpecificArchive)
FS_ASSERT(pOpenedArchive != NULL); FS_ASSERT(pOpenedArchive != NULL);
fs_gc_archives(pOpenedArchive->pArchive, policy); fs_gc_archives(pOpenedArchive->pArchive, policy);
cursor += FS_ALIGN(sizeof(fs*) + sizeof(size_t) + strlen(pOpenedArchive->pPath) + 1, FS_SIZEOF_PTR); cursor += FS_ALIGN(sizeof(pOpenedArchive->pArchive) + strlen(pOpenedArchive->pPath) + 1, FS_SIZEOF_PTR);
} }
} }
} }
@@ -4696,7 +4705,7 @@ static void fs_gc_nolock(fs* pFS, int policy, fs* pSpecificArchive)
} }
} }
cursor += FS_ALIGN(sizeof(fs*) + sizeof(size_t) + strlen(pOpenedArchive->pPath) + 1, FS_SIZEOF_PTR); cursor += FS_ALIGN(sizeof(pOpenedArchive->pArchive) + strlen(pOpenedArchive->pPath) + 1, FS_SIZEOF_PTR);
} }
/* Determine how many archives to collect. */ /* Determine how many archives to collect. */
@@ -4728,7 +4737,7 @@ static void fs_gc_nolock(fs* pFS, int policy, fs* pSpecificArchive)
collectionCount -= 1; collectionCount -= 1;
/* Note that we're not advancing the cursor here because we just removed this entry. */ /* Note that we're not advancing the cursor here because we just removed this entry. */
} else { } else {
cursor += FS_ALIGN(sizeof(fs*) + sizeof(size_t) + strlen(pOpenedArchive->pPath) + 1, FS_SIZEOF_PTR); cursor += FS_ALIGN(sizeof(pOpenedArchive->pArchive) + strlen(pOpenedArchive->pPath) + 1, FS_SIZEOF_PTR);
} }
} }
} }
@@ -8277,6 +8286,8 @@ FS_API fs_bool32 fs_path_is_last(const fs_path_iterator* pIterator)
FS_API int fs_path_iterators_compare(const fs_path_iterator* pIteratorA, const fs_path_iterator* pIteratorB) FS_API int fs_path_iterators_compare(const fs_path_iterator* pIteratorA, const fs_path_iterator* pIteratorB)
{ {
int cmp;
FS_ASSERT(pIteratorA != NULL); FS_ASSERT(pIteratorA != NULL);
FS_ASSERT(pIteratorB != NULL); FS_ASSERT(pIteratorB != NULL);
@@ -8284,7 +8295,19 @@ FS_API int fs_path_iterators_compare(const fs_path_iterator* pIteratorA, const f
return 0; return 0;
} }
return fs_strncmp(pIteratorA->pFullPath + pIteratorA->segmentOffset, pIteratorB->pFullPath + pIteratorB->segmentOffset, FS_MIN(pIteratorA->segmentLength, pIteratorB->segmentLength)); cmp = fs_strncmp(pIteratorA->pFullPath + pIteratorA->segmentOffset, pIteratorB->pFullPath + pIteratorB->segmentOffset, FS_MIN(pIteratorA->segmentLength, pIteratorB->segmentLength));
if (cmp != 0) {
return cmp;
}
if (pIteratorA->segmentLength < pIteratorB->segmentLength) {
return -1;
}
if (pIteratorA->segmentLength > pIteratorB->segmentLength) {
return 1;
}
return 0;
} }
FS_API int fs_path_compare(const char* pPathA, size_t pathALen, const char* pPathB, size_t pathBLen) FS_API int fs_path_compare(const char* pPathA, size_t pathALen, const char* pPathB, size_t pathBLen)
+2 -2
View File
@@ -933,13 +933,13 @@ see some random tags and stuff in this file. These are just used for doing a dum
extern "C" { extern "C" {
#endif #endif
/* BEG fs_platform_detection.c */ /* BEG fs_platform_detection.h */
#if defined(_WIN32) #if defined(_WIN32)
#define FS_WIN32 #define FS_WIN32
#else #else
#define FS_POSIX #define FS_POSIX
#endif #endif
/* END fs_platform_detection.c */ /* END fs_platform_detection.h */
/* BEG fs_compiler_compat.h */ /* BEG fs_compiler_compat.h */
#include <stddef.h> /* For size_t. */ #include <stddef.h> /* For size_t. */