44#include " node_file.h"
55#include " node_internals.h"
66#include " node_version.h"
7+ #include " path.h"
78#include " zlib.h"
89
910namespace node {
@@ -27,7 +28,7 @@ uint32_t GetHash(const char* data, size_t size) {
2728}
2829
2930uint32_t GetCacheVersionTag () {
30- std::string node_version (NODE_VERSION);
31+ std::string_view node_version (NODE_VERSION);
3132 uint32_t v8_tag = v8::ScriptCompiler::CachedDataVersionTag ();
3233 uLong crc = crc32 (0L , Z_NULL, 0 );
3334 crc = crc32 (crc, reinterpret_cast <const Bytef*>(&v8_tag), sizeof (uint32_t ));
@@ -119,7 +120,7 @@ void CompileCacheHandler::ReadCacheFile(CompileCacheEntry* entry) {
119120 return ;
120121 }
121122
122- // Read the cache, grow the buffer exponentially whenever it ills up.
123+ // Read the cache, grow the buffer exponentially whenever it fills up.
123124 size_t offset = headers_buf.len ;
124125 size_t capacity = 4096 ; // Initial buffer capacity
125126 size_t total_read = 0 ;
@@ -340,10 +341,33 @@ CompileCacheHandler::CompileCacheHandler(Environment* env)
340341// - <cache_file_1>: a hash of filename + module type
341342// - <cache_file_2>
342343// - <cache_file_3>
343- bool CompileCacheHandler::InitializeDirectory (const std::string& dir) {
344+ bool CompileCacheHandler::InitializeDirectory (Environment* env,
345+ const std::string& dir) {
344346 compiler_cache_key_ = GetCacheVersionTag ();
345- std::string cache_dir =
346- dir + kPathSeparator + Uint32ToHex (compiler_cache_key_);
347+ std::string compiler_cache_key_string = Uint32ToHex (compiler_cache_key_);
348+ std::vector<std::string_view> paths = {dir, compiler_cache_key_string};
349+ std::string cache_dir = PathResolve (env, paths);
350+
351+ Debug (" [compile cache] resolved path %s + %s -> %s\n " ,
352+ dir,
353+ compiler_cache_key_string,
354+ cache_dir);
355+
356+ if (UNLIKELY (!env->permission ()->is_granted (
357+ permission::PermissionScope::kFileSystemWrite , cache_dir))) {
358+ Debug (" [compile cache] skipping cache because write permission for %s "
359+ " is not granted\n " ,
360+ cache_dir);
361+ return false ;
362+ }
363+
364+ if (UNLIKELY (!env->permission ()->is_granted (
365+ permission::PermissionScope::kFileSystemRead , cache_dir))) {
366+ Debug (" [compile cache] skipping cache because read permission for %s "
367+ " is not granted\n " ,
368+ cache_dir);
369+ return false ;
370+ }
347371
348372 fs::FSReqWrapSync req_wrap;
349373 int err = fs::MKDirpSync (nullptr , &(req_wrap.req ), cache_dir, 0777 , nullptr );
@@ -356,22 +380,7 @@ bool CompileCacheHandler::InitializeDirectory(const std::string& dir) {
356380 return false ;
357381 }
358382
359- uv_fs_t req;
360- auto clean = OnScopeLeave ([&req]() { uv_fs_req_cleanup (&req); });
361- err = uv_fs_realpath (nullptr , &req, cache_dir.data (), nullptr );
362- if (is_debug_) {
363- Debug (" [compile cache] resolving real path %s...%s\n " ,
364- cache_dir,
365- err < 0 ? uv_strerror (err) : " success" );
366- }
367- if (err != 0 && err != UV_ENOENT) {
368- return false ;
369- }
370-
371- compile_cache_dir_ = std::string (static_cast <char *>(req.ptr ));
372- Debug (" [compile cache] resolved real path %s -> %s\n " ,
373- cache_dir,
374- compile_cache_dir_);
383+ compile_cache_dir_ = cache_dir;
375384 return true ;
376385}
377386
0 commit comments