Skip to content

Commit b12992c

Browse files
kubkonandrewrk
authored andcommitted
macho: do not open file handle when building static archive
Firstly, opening a file handle is not really needed since we won't even use it, and secondly, this can cause AccessDenied errors on Windows when trying to move a directory from zig-cache/tmp/ to zig-cache/o/ since, without POSIX semantics, it is illegal to move directories with open handles to any of its resources.
1 parent f645972 commit b12992c

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/link.zig

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,12 +772,15 @@ pub const File = struct {
772772
error.FileNotFound => {},
773773
else => |e| return e,
774774
}
775-
try std.fs.rename(
775+
std.fs.rename(
776776
cache_directory.handle,
777777
tmp_dir_sub_path,
778778
cache_directory.handle,
779779
o_sub_path,
780-
);
780+
) catch |err| switch (err) {
781+
error.AccessDenied => unreachable, // We are most likely trying to move a dir with open handles to its resources
782+
else => |e| return e,
783+
};
781784
break;
782785
} else {
783786
std.fs.rename(

src/link/MachO.zig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -276,22 +276,14 @@ pub fn openPath(allocator: Allocator, options: link.Options) !*MachO {
276276
if (use_stage1 or options.emit == null) {
277277
return createEmpty(allocator, options);
278278
}
279-
const emit = options.emit.?;
280-
const file = try emit.directory.handle.createFile(emit.sub_path, .{
281-
.truncate = false,
282-
.read = true,
283-
.mode = link.determineMode(options),
284-
});
285-
errdefer file.close();
286279

280+
const emit = options.emit.?;
287281
const self = try createEmpty(allocator, options);
288282
errdefer {
289283
self.base.file = null;
290284
self.base.destroy();
291285
}
292286

293-
self.base.file = file;
294-
295287
if (build_options.have_llvm and options.use_llvm and options.module != null) {
296288
// TODO this intermediary_basename isn't enough; in the case of `zig build-exe`,
297289
// we also want to put the intermediary object file in the cache while the
@@ -307,6 +299,14 @@ pub fn openPath(allocator: Allocator, options: link.Options) !*MachO {
307299
return self;
308300
}
309301

302+
const file = try emit.directory.handle.createFile(emit.sub_path, .{
303+
.truncate = false,
304+
.read = true,
305+
.mode = link.determineMode(options),
306+
});
307+
errdefer file.close();
308+
self.base.file = file;
309+
310310
if (!options.strip and options.module != null) blk: {
311311
// TODO once I add support for converting (and relocating) DWARF info from relocatable
312312
// object files, this check becomes unnecessary.

0 commit comments

Comments
 (0)