Skip to content

Commit

Permalink
fix: ensure same mtime for js and code cache to prevent loading old c…
Browse files Browse the repository at this point in the history
…ode caches (#261)
  • Loading branch information
edusperoni authored Sep 6, 2024
1 parent 9d9f4ae commit 055b042
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions NativeScript/runtime/ModuleInternal.mm
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "ModuleInternal.h"
#include <Foundation/Foundation.h>
#include <sys/stat.h>
#include <time.h>
#include <utime.h>
#include <string>
#include "Caches.h"
#include "Helpers.h"
Expand Down Expand Up @@ -453,9 +455,9 @@ throw NativeScriptException("Unable to locate main entry in " +
auto cacheLastModifiedTime = result.st_mtime;
if (stat(path.c_str(), &result) == 0) {
auto jsLastModifiedTime = result.st_mtime;
if (jsLastModifiedTime > 0 && cacheLastModifiedTime > 0 &&
jsLastModifiedTime > cacheLastModifiedTime) {
// The javascript file is more recent than the cache file => ignore the cache
if (jsLastModifiedTime != cacheLastModifiedTime) {
// files have different dates, ignore the cache file (this is enforced by the
// SaveScriptCache function)
return nullptr;
}
}
Expand Down Expand Up @@ -485,6 +487,17 @@ throw NativeScriptException("Unable to locate main entry in " +
std::string cachePath = GetCacheFileName(path + ".cache");
tns::WriteBinary(cachePath, cachedData->data, length);
delete cachedData;

// make sure cache and js file have the same modification date
struct stat result;
struct utimbuf new_times;
new_times.actime = time(nullptr);
new_times.modtime = time(nullptr);
if (stat(path.c_str(), &result) == 0) {
auto jsLastModifiedTime = result.st_mtime;
new_times.modtime = jsLastModifiedTime;
}
utime(cachePath.c_str(), &new_times);
}

std::string ModuleInternal::GetCacheFileName(const std::string& path) {
Expand Down

0 comments on commit 055b042

Please sign in to comment.