Skip to content

Commit ed6134a

Browse files
committed
tmpFilePath: re-use the same function
And move under `Compilation.Directory`, since it belongs there most.
1 parent 2922320 commit ed6134a

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

src/Compilation.zig

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,16 @@ pub const Directory = struct {
844844
if (self.path) |p| gpa.free(p);
845845
self.* = undefined;
846846
}
847+
848+
pub fn tmpFilePath(self: *const Directory, ally: Allocator, suffix: []const u8) error{OutOfMemory}![]const u8 {
849+
const s = std.fs.path.sep_str;
850+
const rand_int = std.crypto.random.int(u64);
851+
if (self.path) |p| {
852+
return std.fmt.allocPrint(ally, "{s}" ++ s ++ "tmp" ++ s ++ "{x}-{s}", .{ p, rand_int, suffix });
853+
} else {
854+
return std.fmt.allocPrint(ally, "tmp" ++ s ++ "{x}-{s}", .{ rand_int, suffix });
855+
}
856+
}
847857
};
848858

849859
pub const EmitLoc = struct {
@@ -4061,7 +4071,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: *std.P
40614071

40624072
// We can't know the digest until we do the C compiler invocation,
40634073
// so we need a temporary filename.
4064-
const out_obj_path = try comp.tmpFilePath(arena, o_basename);
4074+
const out_obj_path = try comp.local_cache_directory.tmpFilePath(arena, o_basename);
40654075
var zig_cache_tmp_dir = try comp.local_cache_directory.handle.makeOpenPath("tmp", .{});
40664076
defer zig_cache_tmp_dir.close();
40674077

@@ -4209,16 +4219,6 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: *std.P
42094219
};
42104220
}
42114221

4212-
pub fn tmpFilePath(comp: *Compilation, ally: Allocator, suffix: []const u8) error{OutOfMemory}![]const u8 {
4213-
const s = std.fs.path.sep_str;
4214-
const rand_int = std.crypto.random.int(u64);
4215-
if (comp.local_cache_directory.path) |p| {
4216-
return std.fmt.allocPrint(ally, "{s}" ++ s ++ "tmp" ++ s ++ "{x}-{s}", .{ p, rand_int, suffix });
4217-
} else {
4218-
return std.fmt.allocPrint(ally, "tmp" ++ s ++ "{x}-{s}", .{ rand_int, suffix });
4219-
}
4220-
}
4221-
42224222
pub fn addTranslateCCArgs(
42234223
comp: *Compilation,
42244224
arena: Allocator,

src/main.zig

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -657,19 +657,9 @@ fn copyToFile(
657657
in_fd: std.os.fd_t,
658658
dst_dir: Compilation.Directory,
659659
) ![]const u8 {
660-
// the section below is inspired by Compilation.tmpFilePath
661-
const new_name = blk: {
662-
const s = std.fs.path.sep_str;
663-
const rand_int = std.crypto.random.int(u64);
664-
if (dst_dir.path) |p| {
665-
break :blk try std.fmt.allocPrint(arena, "{s}" ++ s ++ "tmp" ++ s ++ "{x}", .{ p, rand_int });
666-
} else {
667-
break :blk try std.fmt.allocPrint(arena, "tmp" ++ s ++ "{x}", .{rand_int});
668-
}
669-
};
660+
const new_name = try dst_dir.tmpFilePath(arena, "waspipe");
670661
try dst_dir.handle.makePath("tmp");
671-
672-
var outfile = try dst_dir.handle.createFile(new_name, std.fs.File.CreateFlags{ .exclusive = true });
662+
var outfile = try dst_dir.handle.createFile(new_name, .{});
673663
defer outfile.close();
674664
errdefer dst_dir.handle.deleteFile(new_name) catch {};
675665

@@ -3724,7 +3714,7 @@ fn cmdTranslateC(comp: *Compilation, arena: Allocator, enable_cache: bool) !void
37243714

37253715
const c_src_basename = fs.path.basename(c_source_file.src_path);
37263716
const dep_basename = try std.fmt.allocPrint(arena, "{s}.d", .{c_src_basename});
3727-
const out_dep_path = try comp.tmpFilePath(arena, dep_basename);
3717+
const out_dep_path = try comp.local_cache_directory.tmpFilePath(arena, dep_basename);
37283718
break :blk out_dep_path;
37293719
};
37303720

0 commit comments

Comments
 (0)