Skip to content

Commit

Permalink
fix(install): Ensure installed files has read permission
Browse files Browse the repository at this point in the history
Signed-off-by: Aiello Chan <[email protected]>
  • Loading branch information
AielloChan committed Oct 10, 2024
1 parent 05f68d7 commit a3f3a35
Showing 1 changed file with 80 additions and 26 deletions.
106 changes: 80 additions & 26 deletions src/install/extract_tarball.zig
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,26 @@ pub fn buildURLWithPrinter(
}
}

const EnsureReadPermissions = struct {
fn apply(dir: std.fs.Dir, path: []const u8) !void {
const file = try dir.openFile(path, .{ .mode = .read_write });
defer file.close();
const stat = try file.stat();
const new_mode = stat.mode | 0o444;
try file.chmod(new_mode);
}

fn walkApply(dir: std.fs.Dir) !void {
var walker = try dir.walk(bun.default_allocator);
defer walker.deinit();

while (try walker.next()) |entry| {
if (entry.kind != .file) continue;
try apply(dir, entry.path);
}
}
};

threadlocal var final_path_buf: bun.PathBuffer = undefined;
threadlocal var folder_name_buf: bun.PathBuffer = undefined;
threadlocal var json_path_buf: bun.PathBuffer = undefined;
Expand Down Expand Up @@ -278,18 +298,35 @@ fn extract(this: *const ExtractTarball, tgz_bytes: []const u8) !Install.ExtractD
var dirname_reader = DirnameReader{ .outdirname = &resolved };

switch (PackageManager.verbose_install) {
inline else => |log| _ = try Archiver.extractToDir(
zlib_pool.data.list.items,
extract_destination,
null,
*DirnameReader,
&dirname_reader,
.{
// for GitHub tarballs, the root dir is always <user>-<repo>-<commit_id>
.depth_to_skip = 1,
.log = log,
},
),
inline else => |log| {
const extracted_count = try Archiver.extractToDir(
zlib_pool.data.list.items,
extract_destination,
null,
*DirnameReader,
&dirname_reader,
.{
// for GitHub tarballs, the root dir is always <user>-<repo>-<commit_id>
.depth_to_skip = 1,
.log = log,
},
);

// Apply read permissions to all extracted files
EnsureReadPermissions.walkApply(extract_destination) catch |err| {
this.package_manager.log.addErrorFmt(
null,
logger.Loc.Empty,
this.package_manager.allocator,
"{s} Failed to set read permissions for some files",
.{@errorName(err)},
) catch unreachable;
return error.InstallFailed;
};
if (PackageManager.verbose_install) {
Output.prettyErrorln("[{s}] Extracted {} files and ensured read permissions", .{ name, extracted_count });
}
},
}

// This tag is used to know which version of the package was
Expand All @@ -304,20 +341,37 @@ fn extract(this: *const ExtractTarball, tgz_bytes: []const u8) !Install.ExtractD
}
},
else => switch (PackageManager.verbose_install) {
inline else => |log| _ = try Archiver.extractToDir(
zlib_pool.data.list.items,
extract_destination,
null,
void,
{},
.{
.log = log,
// packages usually have root directory `package/`, and scoped packages usually have root `<scopename>/`
// https://github.com/npm/cli/blob/93883bb6459208a916584cad8c6c72a315cf32af/node_modules/pacote/lib/fetcher.js#L442
.depth_to_skip = 1,
.npm = true,
},
),
inline else => |log| {
const extracted_count = try Archiver.extractToDir(
zlib_pool.data.list.items,
extract_destination,
null,
void,
{},
.{
.log = log,
// packages usually have root directory `package/`, and scoped packages usually have root `<scopename>/`
// https://github.com/npm/cli/blob/93883bb6459208a916584cad8c6c72a315cf32af/node_modules/pacote/lib/fetcher.js#L442
.depth_to_skip = 1,
.npm = true,
},
);

// Apply read permissions to all extracted files
EnsureReadPermissions.walkApply(extract_destination) catch |err| {
this.package_manager.log.addErrorFmt(
null,
logger.Loc.Empty,
this.package_manager.allocator,
"{s} Failed to set read permissions for some files",
.{@errorName(err)},
) catch unreachable;
return error.InstallFailed;
};
if (PackageManager.verbose_install) {
Output.prettyErrorln("[{s}] Extracted {} files and ensured read permissions", .{ name, extracted_count });
}
},
},
}

Expand Down

0 comments on commit a3f3a35

Please sign in to comment.