diff options
author | 2021-08-23 02:30:30 -0700 | |
---|---|---|
committer | 2021-08-23 02:30:30 -0700 | |
commit | afb6684782ffb35f6c750a57426c79ac762acc7b (patch) | |
tree | 0789404c9356eba255600797e7cbd5f906521a48 /src/cache.zig | |
parent | 8c6700792666b1d7a128095cd5cff392df68375c (diff) | |
download | bun-afb6684782ffb35f6c750a57426c79ac762acc7b.tar.gz bun-afb6684782ffb35f6c750a57426c79ac762acc7b.tar.zst bun-afb6684782ffb35f6c750a57426c79ac762acc7b.zip |
Fix resolve bug with mixed-case node_modules
Former-commit-id: 5bdae0ee491e28b49ceed6136f04e0c1feddc808
Diffstat (limited to 'src/cache.zig')
-rw-r--r-- | src/cache.zig | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/cache.zig b/src/cache.zig index 2026b0997..533e35a38 100644 --- a/src/cache.zig +++ b/src/cache.zig @@ -63,6 +63,96 @@ pub fn NewCache(comptime cache_files: bool) type { c.entries.deinit(); } + pub fn readFileShared( + c: *Fs, + _fs: *fs.FileSystem, + path: string, + dirname_fd: StoredFileDescriptorType, + _file_handle: ?StoredFileDescriptorType, + shared: *MutableString, + ) !Entry { + var rfs = _fs.fs; + + if (cache_files) { + { + c.mutex.lock(); + defer c.mutex.unlock(); + if (c.entries.get(path)) |entry| { + return entry; + } + } + } + + var file_handle: std.fs.File = if (_file_handle) |__file| std.fs.File{ .handle = __file } else undefined; + + if (_file_handle == null) { + if (FeatureFlags.store_file_descriptors and dirname_fd > 0) { + file_handle = try std.fs.Dir.openFile(std.fs.Dir{ .fd = dirname_fd }, std.fs.path.basename(path), .{ .read = true }); + } else { + file_handle = try std.fs.openFileAbsolute(path, .{ .read = true }); + } + } + + defer { + if (rfs.needToCloseFiles() and _file_handle == null) { + file_handle.close(); + } + } + + // If the file's modification key hasn't changed since it was cached, assume + // the contents of the file are also the same and skip reading the file. + var mod_key: ?fs.FileSystem.Implementation.ModKey = rfs.modKeyWithFile(path, file_handle) catch |err| handler: { + switch (err) { + error.FileNotFound, error.AccessDenied => { + return err; + }, + else => { + if (isDebug) { + Output.printError("modkey error: {s}", .{@errorName(err)}); + } + break :handler null; + }, + } + }; + + var file: fs.File = undefined; + if (mod_key) |modk| { + file = rfs.readFileWithHandle(path, modk.size, file_handle, true, shared) catch |err| { + if (isDebug) { + Output.printError("{s}: readFile error -- {s}", .{ path, @errorName(err) }); + } + return err; + }; + } else { + file = rfs.readFileWithHandle(path, null, file_handle, true, shared) catch |err| { + if (isDebug) { + Output.printError("{s}: readFile error -- {s}", .{ path, @errorName(err) }); + } + return err; + }; + } + + const entry = Entry{ + .contents = file.contents, + .mod_key = mod_key, + .fd = if (FeatureFlags.store_file_descriptors) file_handle.handle else 0, + }; + + if (cache_files) { + c.mutex.lock(); + defer c.mutex.unlock(); + var res = c.entries.getOrPut(path) catch unreachable; + + if (res.found_existing) { + res.value_ptr.*.deinit(c.entries.allocator); + } + res.value_ptr.* = entry; + return res.value_ptr.*; + } else { + return entry; + } + } + pub fn readFile( c: *Fs, _fs: *fs.FileSystem, |