authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-01-29 10:48:36-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-01-29 20:48:36+02:00
log5e60ee41272579bc5fef3d95c59180df6ab824c1
treef21600c1b796fec5f87903e6559006dd3179ce94
parentfedff060790733b85582b733cfea9fefb168deb6
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std: define static error set for fs.Dir.copyFile


1 files changed, 9 insertions(+), 11 deletions(-)

lib/std/fs.zig+9-11
......@@ -193,7 +193,9 @@ pub const AtomicFile = struct {
193193 self.* = undefined;
194194 }
195195
196 pub fn finish(self: *AtomicFile) !void {
196 pub const FinishError = std.os.RenameError;
197
198 pub fn finish(self: *AtomicFile) FinishError!void {
197199 assert(self.file_exists);
198200 if (self.file_open) {
199201 self.file.close();
......@@ -2112,17 +2114,13 @@ pub const Dir = struct {
21122114 return PrevStatus.stale;
21132115 }
21142116
2117 pub const CopyFileError = File.OpenError || File.StatError || AtomicFile.InitError || CopyFileRawError || AtomicFile.FinishError;
2118
21152119 /// Guaranteed to be atomic.
21162120 /// On Linux, until https://patchwork.kernel.org/patch/9636735/ is merged and readily available,
21172121 /// there is a possibility of power loss or application termination leaving temporary files present
21182122 /// in the same directory as dest_path.
2119 pub fn copyFile(
2120 source_dir: Dir,
2121 source_path: []const u8,
2122 dest_dir: Dir,
2123 dest_path: []const u8,
2124 options: CopyFileOptions,
2125 ) !void {
2123 pub fn copyFile(source_dir: Dir, source_path: []const u8, dest_dir: Dir, dest_path: []const u8, options: CopyFileOptions) CopyFileError!void {
21262124 var in_file = try source_dir.openFile(source_path, .{});
21272125 defer in_file.close();
21282126
......@@ -2137,7 +2135,7 @@ pub const Dir = struct {
21372135 defer atomic_file.deinit();
21382136
21392137 try copy_file(in_file.handle, atomic_file.file.handle);
2140 return atomic_file.finish();
2138 try atomic_file.finish();
21412139 }
21422140
21432141 pub const AtomicFileOptions = struct {
......@@ -2617,12 +2615,12 @@ pub fn realpathAlloc(allocator: Allocator, pathname: []const u8) ![]u8 {
26172615 return allocator.dupe(u8, try os.realpath(pathname, &buf));
26182616}
26192617
2620const CopyFileError = error{SystemResources} || os.CopyFileRangeError || os.SendFileError;
2618const CopyFileRawError = error{SystemResources} || os.CopyFileRangeError || os.SendFileError;
26212619
26222620// Transfer all the data between two file descriptors in the most efficient way.
26232621// The copy starts at offset 0, the initial offsets are preserved.
26242622// No metadata is transferred over.
2625fn copy_file(fd_in: os.fd_t, fd_out: os.fd_t) CopyFileError!void {
2623fn copy_file(fd_in: os.fd_t, fd_out: os.fd_t) CopyFileRawError!void {
26262624 if (comptime builtin.target.isDarwin()) {
26272625 const rc = os.system.fcopyfile(fd_in, fd_out, null, os.system.COPYFILE_DATA);
26282626 switch (os.errno(rc)) {