Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File/Dir deinit invalidates, close doesn't #10213

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/std/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ pub const Builder = struct {
warn("truncate {s}\n", .{dest_path});
}
const cwd = fs.cwd();
var src_file = cwd.createFile(dest_path, .{}) catch |err| switch (err) {
const src_file = cwd.createFile(dest_path, .{}) catch |err| switch (err) {
error.FileNotFound => blk: {
if (fs.path.dirname(dest_path)) |dirname| {
try cwd.makePath(dirname);
Expand Down Expand Up @@ -2735,13 +2735,13 @@ pub const LibExeObjStep = struct {
const build_output_dir = mem.trimRight(u8, output_dir_nl, "\r\n");

if (self.output_dir) |output_dir| {
var src_dir = try std.fs.cwd().openDir(build_output_dir, .{ .iterate = true });
const src_dir = try std.fs.cwd().openDir(build_output_dir, .{ .iterate = true });
defer src_dir.close();

// Create the output directory if it doesn't exist.
try std.fs.cwd().makePath(output_dir);

var dest_dir = try std.fs.cwd().openDir(output_dir, .{});
const dest_dir = try std.fs.cwd().openDir(output_dir, .{});
defer dest_dir.close();

var it = src_dir.iterate();
Expand Down Expand Up @@ -2951,7 +2951,7 @@ pub const InstallDirStep = struct {
const self = @fieldParentPtr(InstallDirStep, "step", step);
const dest_prefix = self.builder.getInstallPath(self.options.install_dir, self.options.install_subdir);
const full_src_dir = self.builder.pathFromRoot(self.options.source_dir);
var src_dir = try std.fs.cwd().openDir(full_src_dir, .{ .iterate = true });
const src_dir = try std.fs.cwd().openDir(full_src_dir, .{ .iterate = true });
defer src_dir.close();
var it = try src_dir.walk(self.builder.allocator);
next_entry: while (try it.next()) |entry| {
Expand Down
4 changes: 2 additions & 2 deletions lib/std/build/InstallRawStep.zig
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ fn containsValidAddressRange(segments: []*BinaryElfSegment) bool {
}

fn emitRaw(allocator: *Allocator, elf_path: []const u8, raw_path: []const u8, format: RawFormat) !void {
var elf_file = try fs.cwd().openFile(elf_path, .{});
const elf_file = try fs.cwd().openFile(elf_path, .{});
defer elf_file.close();

var out_file = try fs.cwd().createFile(raw_path, .{});
const out_file = try fs.cwd().createFile(raw_path, .{});
defer out_file.close();

var binary_elf_output = try BinaryElfOutput.parse(allocator, elf_file);
Expand Down
2 changes: 1 addition & 1 deletion lib/std/build/WriteFileStep.zig
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn make(step: *Step) !void {
warn("unable to make path {s}: {s}\n", .{ self.output_dir, @errorName(err) });
return err;
};
var dir = try fs.cwd().openDir(self.output_dir, .{});
const dir = try fs.cwd().openDir(self.output_dir, .{});
defer dir.close();
{
var it = self.files.first;
Expand Down
2 changes: 1 addition & 1 deletion lib/std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ fn readMachODebugInfo(allocator: *mem.Allocator, macho_file: File) !ModuleDebugI
fn printLineFromFileAnyOs(out_stream: anytype, line_info: LineInfo) !void {
// Need this to always block even in async I/O mode, because this could potentially
// be called from e.g. the event loop code crashing.
var f = try fs.cwd().openFile(line_info.file_name, .{ .intended_io_mode = .blocking });
const f = try fs.cwd().openFile(line_info.file_name, .{ .intended_io_mode = .blocking });
defer f.close();
// TODO fstat and make sure that the file has the correct size

Expand Down
25 changes: 15 additions & 10 deletions lib/std/fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ pub const AtomicFile = struct {
pub fn finish(self: *AtomicFile) !void {
assert(self.file_exists);
if (self.file_open) {
self.file.close();
self.file.deinit();
self.file_open = false;
}
try os.renameat(self.dir.fd, self.tmp_path_buf[0..], self.dir.fd, self.dest_basename);
Expand Down Expand Up @@ -827,7 +827,7 @@ pub const Dir = struct {
}
try self.name_buffer.appendSlice(base.name);
if (base.kind == .Directory) {
var new_dir = top.iter.dir.openDir(base.name, .{ .iterate = true }) catch |err| switch (err) {
const new_dir = top.iter.dir.openDir(base.name, .{ .iterate = true }) catch |err| switch (err) {
error.NameTooLong => unreachable, // no path sep in base.name
else => |e| return e,
};
Expand Down Expand Up @@ -905,12 +905,17 @@ pub const Dir = struct {
DeviceBusy,
} || os.UnexpectedError;

pub fn close(self: *Dir) void {
pub fn close(self: Dir) void {
if (need_async_thread) {
std.event.Loop.instance.?.close(self.fd);
} else {
os.close(self.fd);
}
}

/// Close and invalidate the directory.
pub fn deinit(self: *Dir) void {
self.close();
self.* = undefined;
}

Expand Down Expand Up @@ -1806,7 +1811,7 @@ pub const Dir = struct {
/// it exactly fits the buffer, or it could mean the buffer was not big enough for the
/// entire file.
pub fn readFile(self: Dir, file_path: []const u8, buffer: []u8) ![]u8 {
var file = try self.openFile(file_path, .{});
const file = try self.openFile(file_path, .{});
defer file.close();

const end_index = try file.readAll(buffer);
Expand All @@ -1833,7 +1838,7 @@ pub const Dir = struct {
comptime alignment: u29,
comptime optional_sentinel: ?u8,
) !(if (optional_sentinel) |s| [:s]align(alignment) u8 else []align(alignment) u8) {
var file = try self.openFile(file_path, .{});
const file = try self.openFile(file_path, .{});
defer file.close();

// If the file size doesn't fit a usize it'll be certainly greater than
Expand Down Expand Up @@ -2024,7 +2029,7 @@ pub const Dir = struct {
/// Writes content to the file system, creating a new file if it does not exist, truncating
/// if it already exists.
pub fn writeFile(self: Dir, sub_path: []const u8, data: []const u8) !void {
var file = try self.createFile(sub_path, .{});
const file = try self.createFile(sub_path, .{});
defer file.close();
try file.writeAll(data);
}
Expand Down Expand Up @@ -2086,14 +2091,14 @@ pub const Dir = struct {
dest_path: []const u8,
options: CopyFileOptions,
) !PrevStatus {
var src_file = try source_dir.openFile(source_path, .{});
const src_file = try source_dir.openFile(source_path, .{});
defer src_file.close();

const src_stat = try src_file.stat();
const actual_mode = options.override_mode orelse src_stat.mode;
check_dest_stat: {
const dest_stat = blk: {
var dest_file = dest_dir.openFile(dest_path, .{}) catch |err| switch (err) {
const dest_file = dest_dir.openFile(dest_path, .{}) catch |err| switch (err) {
error.FileNotFound => break :check_dest_stat,
else => |e| return e,
};
Expand Down Expand Up @@ -2134,7 +2139,7 @@ pub const Dir = struct {
dest_path: []const u8,
options: CopyFileOptions,
) !void {
var in_file = try source_dir.openFile(source_path, .{});
const in_file = try source_dir.openFile(source_path, .{});
defer in_file.close();

var size: ?u64 = null;
Expand Down Expand Up @@ -2370,7 +2375,7 @@ pub fn deleteTreeAbsolute(absolute_path: []const u8) !void {
CannotDeleteRootDirectory,
}.CannotDeleteRootDirectory;

var dir = try cwd().openDir(dirname, .{});
const dir = try cwd().openDir(dirname, .{});
defer dir.close();

return dir.deleteTree(path.basename(absolute_path));
Expand Down
6 changes: 6 additions & 0 deletions lib/std/fs/file.zig
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ pub const File = struct {
}
}

/// Close and invalidate the file.
pub fn deinit(self: *File) void {
self.close();
self.* = undefined;
}

/// Test whether the file refers to a terminal.
/// See also `supportsAnsiEscapeCodes`.
pub fn isTty(self: File) bool {
Expand Down
Loading