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 {...@@ -193,7 +193,9 @@ pub const AtomicFile = struct {
193 self.* = undefined;193 self.* = undefined;
194 }194 }
195195
196 pub fn finish(self: *AtomicFile) !void {196 pub const FinishError = std.os.RenameError;
197
198 pub fn finish(self: *AtomicFile) FinishError!void {
197 assert(self.file_exists);199 assert(self.file_exists);
198 if (self.file_open) {200 if (self.file_open) {
199 self.file.close();201 self.file.close();
...@@ -2112,17 +2114,13 @@ pub const Dir = struct {...@@ -2112,17 +2114,13 @@ pub const Dir = struct {
2112 return PrevStatus.stale;2114 return PrevStatus.stale;
2113 }2115 }
21142116
2117 pub const CopyFileError = File.OpenError || File.StatError || AtomicFile.InitError || CopyFileRawError || AtomicFile.FinishError;
2118
2115 /// Guaranteed to be atomic.2119 /// Guaranteed to be atomic.
2116 /// On Linux, until https://patchwork.kernel.org/patch/9636735/ is merged and readily available,2120 /// On Linux, until https://patchwork.kernel.org/patch/9636735/ is merged and readily available,
2117 /// there is a possibility of power loss or application termination leaving temporary files present2121 /// there is a possibility of power loss or application termination leaving temporary files present
2118 /// in the same directory as dest_path.2122 /// in the same directory as dest_path.
2119 pub fn copyFile(2123 pub fn copyFile(source_dir: Dir, source_path: []const u8, dest_dir: Dir, dest_path: []const u8, options: CopyFileOptions) CopyFileError!void {
2120 source_dir: Dir,
2121 source_path: []const u8,
2122 dest_dir: Dir,
2123 dest_path: []const u8,
2124 options: CopyFileOptions,
2125 ) !void {
2126 var in_file = try source_dir.openFile(source_path, .{});2124 var in_file = try source_dir.openFile(source_path, .{});
2127 defer in_file.close();2125 defer in_file.close();
21282126
...@@ -2137,7 +2135,7 @@ pub const Dir = struct {...@@ -2137,7 +2135,7 @@ pub const Dir = struct {
2137 defer atomic_file.deinit();2135 defer atomic_file.deinit();
21382136
2139 try copy_file(in_file.handle, atomic_file.file.handle);2137 try copy_file(in_file.handle, atomic_file.file.handle);
2140 return atomic_file.finish();2138 try atomic_file.finish();
2141 }2139 }
21422140
2143 pub const AtomicFileOptions = struct {2141 pub const AtomicFileOptions = struct {
...@@ -2617,12 +2615,12 @@ pub fn realpathAlloc(allocator: Allocator, pathname: []const u8) ![]u8 {...@@ -2617,12 +2615,12 @@ pub fn realpathAlloc(allocator: Allocator, pathname: []const u8) ![]u8 {
2617 return allocator.dupe(u8, try os.realpath(pathname, &buf));2615 return allocator.dupe(u8, try os.realpath(pathname, &buf));
2618}2616}
26192617
2620const CopyFileError = error{SystemResources} || os.CopyFileRangeError || os.SendFileError;2618const CopyFileRawError = error{SystemResources} || os.CopyFileRangeError || os.SendFileError;
26212619
2622// Transfer all the data between two file descriptors in the most efficient way.2620// Transfer all the data between two file descriptors in the most efficient way.
2623// The copy starts at offset 0, the initial offsets are preserved.2621// The copy starts at offset 0, the initial offsets are preserved.
2624// No metadata is transferred over.2622// 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 {
2626 if (comptime builtin.target.isDarwin()) {2624 if (comptime builtin.target.isDarwin()) {
2627 const rc = os.system.fcopyfile(fd_in, fd_out, null, os.system.COPYFILE_DATA);2625 const rc = os.system.fcopyfile(fd_in, fd_out, null, os.system.COPYFILE_DATA);
2628 switch (os.errno(rc)) {2626 switch (os.errno(rc)) {