physfs icon indicating copy to clipboard operation
physfs copied to clipboard

Significant complications when writing an archiver with write support

Open Torphedo opened this issue 2 years ago • 1 comments

I'm trying to write an archiver with streaming write support (a simple format covered by the unpacked template). I implemented an openWrite() function I wanted to test, but PHYSFS_openWrite() never reaches it. It fails in verifyPath() because it uses the directory archiver's stat function, determines it doesn't exist, and returns an error.

It calls DIR_stat() here, and here's an example of my source code:


const char packname[] = "Armor_012_Upper.pack";

int main(int argc, char** argv) {
    PHYSFS_init(argv[0]);
    PHYSFS_mount(PHYSFS_getBaseDir(), NULL, true);
    PHYSFS_setWriteDir(PHYSFS_getBaseDir());

    PHYSFS_registerArchiver(&archiver_sarc_default);

    printf("Mounting %s...\n\n", packname);
    PHYSFS_mount(packname, NULL, true);

    PHYSFS_file* test_write = PHYSFS_openWrite("/ActorLink/Armor_012_Upper.bxml");
    printf("%p\n", test_write);
}

I'm able to read the file into a buffer with PHYSFS_openRead() and PHYSFS_readBytes() and write it to disk (I omitted the code for this to keep the code snippet short). So the file definitely exists, and the archiver is registered correctly.

TLDR: Writing has very different behaviour from reading which seems to prevent me from even reaching the writing code in my archiver. I could try to re-implement PHYSFS_openWrite() myself, but I would prefer not to if possible.

Torphedo avatar May 07 '23 17:05 Torphedo

Update: I fixed this by making the function search the search path for an appropriate handle instead of using the write directory in a fork. I'm still running into a ton of roadblocks trying to get write support working for a simple format, though.

I need to copy files into their own buffers for streaming writes, then rebuild it into an archive when writing is done for it to be even a little bit efficient. I could make it atomic, but I really don't want to rebuild the archive file and write it to disk on every single write. If there's some better way to implement this that doesn't require changing a lot of the PhysicsFS code, that would be great.

Torphedo avatar May 19 '23 17:05 Torphedo