Skip to content

Commit 055b042

Browse files
authored
fix: ensure same mtime for js and code cache to prevent loading old code caches (#261)
1 parent 9d9f4ae commit 055b042

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

NativeScript/runtime/ModuleInternal.mm

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "ModuleInternal.h"
22
#include <Foundation/Foundation.h>
33
#include <sys/stat.h>
4+
#include <time.h>
5+
#include <utime.h>
46
#include <string>
57
#include "Caches.h"
68
#include "Helpers.h"
@@ -453,9 +455,9 @@ throw NativeScriptException("Unable to locate main entry in " +
453455
auto cacheLastModifiedTime = result.st_mtime;
454456
if (stat(path.c_str(), &result) == 0) {
455457
auto jsLastModifiedTime = result.st_mtime;
456-
if (jsLastModifiedTime > 0 && cacheLastModifiedTime > 0 &&
457-
jsLastModifiedTime > cacheLastModifiedTime) {
458-
// The javascript file is more recent than the cache file => ignore the cache
458+
if (jsLastModifiedTime != cacheLastModifiedTime) {
459+
// files have different dates, ignore the cache file (this is enforced by the
460+
// SaveScriptCache function)
459461
return nullptr;
460462
}
461463
}
@@ -485,6 +487,17 @@ throw NativeScriptException("Unable to locate main entry in " +
485487
std::string cachePath = GetCacheFileName(path + ".cache");
486488
tns::WriteBinary(cachePath, cachedData->data, length);
487489
delete cachedData;
490+
491+
// make sure cache and js file have the same modification date
492+
struct stat result;
493+
struct utimbuf new_times;
494+
new_times.actime = time(nullptr);
495+
new_times.modtime = time(nullptr);
496+
if (stat(path.c_str(), &result) == 0) {
497+
auto jsLastModifiedTime = result.st_mtime;
498+
new_times.modtime = jsLastModifiedTime;
499+
}
500+
utime(cachePath.c_str(), &new_times);
488501
}
489502

490503
std::string ModuleInternal::GetCacheFileName(const std::string& path) {

0 commit comments

Comments
 (0)