Close modfile at the correct time when downloading

This commit is contained in:
MysterD 2022-06-04 17:58:36 -07:00
parent 26a465fd80
commit 5424b1bb69
5 changed files with 9 additions and 11 deletions

View file

@ -738,9 +738,8 @@
--- @class ModFile
--- @field public cachedPath string
--- @field public complete boolean
--- @field public curOffset integer
--- @field public relativePath string
--- @field public wroteBytes integer
--- @class ModeTransitionInfo
--- @field public frame integer

View file

@ -1053,9 +1053,8 @@
| Field | Type | Access |
| ----- | ---- | ------ |
| cachedPath | `string` | read-only |
| complete | `boolean` | read-only |
| curOffset | `integer` | read-only |
| relativePath | `string` | read-only |
| wroteBytes | `integer` | read-only |
[:arrow_up_small:](#)

View file

@ -854,15 +854,14 @@ static struct LuaObjectField sModFields[LUA_MOD_FIELD_COUNT] = {
// { "size", LVT_???, offsetof(struct Mod, size), true, LOT_??? }, <--- UNIMPLEMENTED
};
#define LUA_MOD_FILE_FIELD_COUNT 4
#define LUA_MOD_FILE_FIELD_COUNT 3
static struct LuaObjectField sModFileFields[LUA_MOD_FILE_FIELD_COUNT] = {
{ "cachedPath", LVT_STRING_P, offsetof(struct ModFile, cachedPath), true, LOT_NONE },
{ "complete", LVT_BOOL, offsetof(struct ModFile, complete), true, LOT_NONE },
{ "curOffset", LVT_U64, offsetof(struct ModFile, curOffset), true, LOT_NONE },
// { "dataHash", LOT_???, offsetof(struct ModFile, dataHash), true, LOT_??? }, <--- UNIMPLEMENTED
// { "fp", LVT_???, offsetof(struct ModFile, fp), true, LOT_??? }, <--- UNIMPLEMENTED
{ "relativePath", LVT_STRING, offsetof(struct ModFile, relativePath), true, LOT_NONE },
// { "size", LVT_???, offsetof(struct ModFile, size), true, LOT_??? }, <--- UNIMPLEMENTED
{ "wroteBytes", LVT_U64, offsetof(struct ModFile, wroteBytes), true, LOT_NONE },
};
#define LUA_MODE_TRANSITION_INFO_FIELD_COUNT 6

View file

@ -12,8 +12,7 @@ struct ModFile {
size_t size;
FILE* fp;
u64 curOffset;
bool complete;
u64 wroteBytes;
u8 dataHash[16];
char* cachedPath;
};

View file

@ -322,6 +322,7 @@ static void open_mod_file(struct Mod* mod, struct ModFile* file) {
mod_file_create_directories(mod, file);
file->wroteBytes = 0;
file->fp = fopen(fullPath, "wb");
if (file->fp == NULL) {
LOG_ERROR("unable to open for write: '%s' - '%s'", fullPath, strerror(errno));
@ -399,7 +400,7 @@ after_group:;
u64 fileWriteLength = MIN((modFile->size - fileWriteOffset), (chunkLength - chunkPour));
// read from file, filling chunk
if (!modFile->cachedPath) {
if (!modFile->cachedPath && (modFile->wroteBytes < modFile->size)) {
open_mod_file(mod, modFile);
if (modFile->fp == NULL) {
LOG_ERROR("Failed to open file for download write: %s", modFile->cachedPath);
@ -407,8 +408,9 @@ after_group:;
}
fseek(modFile->fp, fileWriteOffset, SEEK_SET);
fwrite(&chunk[chunkPour], sizeof(u8), fileWriteLength, modFile->fp);
modFile->wroteBytes += fileWriteLength;
if (modFile->fp != NULL) {
if (modFile->wroteBytes >= modFile->size) {
fflush(modFile->fp);
fclose(modFile->fp);
modFile->fp = NULL;