diff --git a/src/AssetCache.cpp b/src/AssetCache.cpp index ec54440..3297838 100644 --- a/src/AssetCache.cpp +++ b/src/AssetCache.cpp @@ -49,13 +49,25 @@ int AssetCache::fromFile(const char *cache_file) { #else while ((ret = fscanf(file, "%s %u %u\n", fpath, &fsum, &fsize)) != EOF) { #endif - Asset *asset = new Asset(); - asset->filename.assign(fpath); - asset->flags &= ~Asset::IS_NULL; - asset->data_checksum = fsum; - asset->data_length = fsize; - assets->set(fpath, asset); - LOG(LOG_DEBUG) << "opened " << fpath << " " << fsum << " " << fsize; + char fullpath[1024]; + snprintf(fullpath, 1024, "%s/%s", directory.c_str(), fpath); + // only add the asset if it exists. +#ifdef _WIN32 + DWORD file_attrib = GetFileAttributes(fullpath); + if (file_attrib != INVALID_FILE_ATTRIBUTES) { +#else + // NOTE: TOCTOU/race vulnerability exists here, but I don't think it matters. + struct stat fstat; + if (stat(fullpath, &fstat) == 0) { +#endif + Asset *asset = new Asset(); + asset->filename.assign(fpath); + asset->flags &= ~Asset::IS_NULL; + asset->data_checksum = fsum; + asset->data_length = fsize; + assets->set(fpath, asset); + LOG(LOG_DEBUG) << "opened " << fpath << " " << fsum << " " << fsize; + } } return 0; } @@ -71,10 +83,10 @@ int AssetCache::fromDir(const char *dir) { #else sprintf_s(cachepath, 1024, "%s/.CACHE", dir); #endif + directory.assign(dir); if (fromFile(cachepath) != 0) { LOG(LOG_INFO) << FUNC_NAME << ": " << cachepath << " does not exist"; } - directory.assign(dir); traverseDir_r(""); // TODO: move this to a "genChecksum()" sort of function /*HashEntry *entry;