Sanity check mod_cache_save
This commit is contained in:
parent
a31ddaff9d
commit
e3f85eb559
|
@ -31,35 +31,90 @@ void mod_cache_shutdown(void) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void mod_cache_md5(const char* inPath, u8* outDataPath) {
|
||||||
|
char cpath[SYS_MAX_PATH] = { 0 };
|
||||||
|
u8 buffer[MD5_BUFFER_SIZE] = { 0 };
|
||||||
|
|
||||||
|
MD5_CTX ctx = { 0 };
|
||||||
|
MD5_Init(&ctx);
|
||||||
|
|
||||||
|
snprintf(cpath, SYS_MAX_PATH-1, "%s", inPath);
|
||||||
|
normalize_path(cpath);
|
||||||
|
|
||||||
|
// open file pointer
|
||||||
|
FILE* fp = fopen(cpath, "rb");
|
||||||
|
if (fp == NULL) {
|
||||||
|
LOG_ERROR("Failed to open filepointer for mod hashing: '%s'.", cpath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// read bytes and md5 them
|
||||||
|
size_t readBytes = 0;
|
||||||
|
do {
|
||||||
|
readBytes = fread(buffer, sizeof(u8), MD5_BUFFER_SIZE, fp);
|
||||||
|
MD5_Update(&ctx, buffer, readBytes);
|
||||||
|
} while (readBytes >= MD5_BUFFER_SIZE);
|
||||||
|
|
||||||
|
// close file pointer
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
// finish computing
|
||||||
|
MD5_Final(outDataPath, &ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool mod_cache_is_valid(struct ModCacheEntry* node) {
|
||||||
|
u8 dataHash[16];
|
||||||
|
mod_cache_md5(node->path, dataHash);
|
||||||
|
return !memcmp(node->dataHash, dataHash, 16);
|
||||||
|
}
|
||||||
|
|
||||||
struct ModCacheEntry* mod_cache_get_from_hash(u8* dataHash) {
|
struct ModCacheEntry* mod_cache_get_from_hash(u8* dataHash) {
|
||||||
struct ModCacheEntry* node = sModCacheHead;
|
struct ModCacheEntry* node = sModCacheHead;
|
||||||
char str[128] = { 0 };
|
struct ModCacheEntry* prev = NULL;
|
||||||
MD5_ToString(dataHash, str);
|
|
||||||
while (node != NULL) {
|
while (node != NULL) {
|
||||||
MD5_ToString(node->dataHash, str);
|
struct ModCacheEntry* next = node->next;
|
||||||
if (!memcmp(node->dataHash, dataHash, 16)) {
|
if (!memcmp(node->dataHash, dataHash, 16)) {
|
||||||
|
if (mod_cache_is_valid(node)) {
|
||||||
return node;
|
return node;
|
||||||
|
} else {
|
||||||
|
mod_cache_remove_node(node, prev);
|
||||||
}
|
}
|
||||||
node = node->next;
|
}
|
||||||
|
prev = node;
|
||||||
|
node = next;
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ModCacheEntry* mod_cache_get_from_path(const char* path) {
|
struct ModCacheEntry* mod_cache_get_from_path(const char* path) {
|
||||||
struct ModCacheEntry* node = sModCacheHead;
|
struct ModCacheEntry* node = sModCacheHead;
|
||||||
|
struct ModCacheEntry* prev = NULL;
|
||||||
while (node != NULL) {
|
while (node != NULL) {
|
||||||
|
struct ModCacheEntry* next = node->next;
|
||||||
if (!strcmp(node->path, path)) {
|
if (!strcmp(node->path, path)) {
|
||||||
|
if (mod_cache_is_valid(node)) {
|
||||||
return node;
|
return node;
|
||||||
|
} else {
|
||||||
|
mod_cache_remove_node(node, prev);
|
||||||
}
|
}
|
||||||
node = node->next;
|
}
|
||||||
|
prev = node;
|
||||||
|
node = next;
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void mod_cache_add_internal(u8* dataHash, u64 lastLoaded, const char* path) {
|
void mod_cache_add_internal(u8* dataHash, u64 lastLoaded, const char* path) {
|
||||||
|
// sanity check
|
||||||
if (mod_cache_get_from_hash(dataHash)) {
|
if (mod_cache_get_from_hash(dataHash)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (path == NULL || strlen(path) == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!fs_sys_file_exists(path)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
struct ModCacheEntry* node = calloc(1, sizeof(struct ModCacheEntry));
|
struct ModCacheEntry* node = calloc(1, sizeof(struct ModCacheEntry));
|
||||||
memcpy(node->dataHash, dataHash, 16);
|
memcpy(node->dataHash, dataHash, 16);
|
||||||
|
@ -95,38 +150,15 @@ void mod_cache_add_internal(u8* dataHash, u64 lastLoaded, const char* path) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void mod_cache_md5(const char* inPath, u8* outDataPath) {
|
void mod_cache_add(struct Mod* mod, struct ModFile* file) {
|
||||||
char cpath[SYS_MAX_PATH] = { 0 };
|
// sanity check
|
||||||
u8 buffer[MD5_BUFFER_SIZE] = { 0 };
|
if (mod == NULL || file == NULL) {
|
||||||
|
return;
|
||||||
MD5_CTX ctx = { 0 };
|
}
|
||||||
MD5_Init(&ctx);
|
if (mod->basePath == NULL || file->relativePath == NULL) {
|
||||||
|
|
||||||
snprintf(cpath, SYS_MAX_PATH-1, "%s", inPath);
|
|
||||||
normalize_path(cpath);
|
|
||||||
|
|
||||||
// open file pointer
|
|
||||||
FILE* fp = fopen(cpath, "rb");
|
|
||||||
if (fp == NULL) {
|
|
||||||
LOG_ERROR("Failed to open filepointer for mod hashing: '%s'.", cpath);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// read bytes and md5 them
|
|
||||||
size_t readBytes = 0;
|
|
||||||
do {
|
|
||||||
readBytes = fread(buffer, sizeof(u8), MD5_BUFFER_SIZE, fp);
|
|
||||||
MD5_Update(&ctx, buffer, readBytes);
|
|
||||||
} while (readBytes >= MD5_BUFFER_SIZE);
|
|
||||||
|
|
||||||
// close file pointer
|
|
||||||
fclose(fp);
|
|
||||||
|
|
||||||
// finish computing
|
|
||||||
MD5_Final(outDataPath, &ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mod_cache_add(struct Mod* mod, struct ModFile* file) {
|
|
||||||
// if we already have a cached path, don't do anything
|
// if we already have a cached path, don't do anything
|
||||||
if (file->cachedPath != NULL) {
|
if (file->cachedPath != NULL) {
|
||||||
return;
|
return;
|
||||||
|
@ -193,6 +225,12 @@ void mod_cache_load(void) {
|
||||||
void mod_cache_save(void) {
|
void mod_cache_save(void) {
|
||||||
LOG_INFO("Saving mod cache");
|
LOG_INFO("Saving mod cache");
|
||||||
const char* filename = fs_get_write_path(MOD_CACHE_FILENAME);
|
const char* filename = fs_get_write_path(MOD_CACHE_FILENAME);
|
||||||
|
|
||||||
|
if (filename == NULL) {
|
||||||
|
LOG_ERROR("Failed to get filename for mod cache");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
FILE* fp = fopen(filename, "wb");
|
FILE* fp = fopen(filename, "wb");
|
||||||
if (fp == NULL) {
|
if (fp == NULL) {
|
||||||
LOG_ERROR("Failed to open mod cache save fp: %s", filename);
|
LOG_ERROR("Failed to open mod cache save fp: %s", filename);
|
||||||
|
@ -204,9 +242,12 @@ void mod_cache_save(void) {
|
||||||
|
|
||||||
struct ModCacheEntry* node = sModCacheHead;
|
struct ModCacheEntry* node = sModCacheHead;
|
||||||
while (node != NULL) {
|
while (node != NULL) {
|
||||||
|
if (node->path == NULL) { continue; }
|
||||||
|
u16 pathLen = strlen(node->path);
|
||||||
|
if (pathLen == 0) { continue; }
|
||||||
|
|
||||||
fwrite(node->dataHash, sizeof(u8), 16, fp);
|
fwrite(node->dataHash, sizeof(u8), 16, fp);
|
||||||
fwrite(&node->lastLoaded, sizeof(u64), 1, fp);
|
fwrite(&node->lastLoaded, sizeof(u64), 1, fp);
|
||||||
u16 pathLen = strlen(node->path);
|
|
||||||
fwrite(&pathLen, sizeof(u16), 1, fp);
|
fwrite(&pathLen, sizeof(u16), 1, fp);
|
||||||
fwrite(node->path, sizeof(u8), pathLen + 1, fp);
|
fwrite(node->path, sizeof(u8), pathLen + 1, fp);
|
||||||
node = node->next;
|
node = node->next;
|
||||||
|
|
Loading…
Reference in New Issue