authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-30 19:41:51 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-30 19:41:51 -07:00
log8e1b1c5e62ebd258f7d6a511f0b5ab9c575dcd27
tree32553c1d11823ecdee7a6393b1179141717d380d
parent3c0230f579a7d41fa1eabe0089930f1655694012
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

Dir: add copyFile


1 files changed, 21 insertions(+), 0 deletions(-)

Dir.zig+21
......@@ -413,3 +413,24 @@ pub fn existsDir(self: Dir, sub_path: [:0]const u8) !bool {
413413 dir.close();
414414 return true;
415415}
416
417pub fn copyFile(source_dir: Dir, source_path: [:0]const u8, dest_dir: Dir, dest_path: [:0]const u8) !void {
418 const source_file = try source_dir.openFile(source_path, .{});
419 defer source_file.close();
420 const source_stat = try source_file.stat();
421
422 const dest_file = try dest_dir.createFile(dest_path, .{ .exclusive = true });
423 defer dest_file.close();
424
425 if (os == .linux) {
426 var off: u64 = 0;
427 var off_in: sys.off_t = 0;
428 var off_out: sys.off_t = 0;
429 while (true) {
430 const amt = try sys.copy_file_range(@intFromEnum(source_file.fd), &off_in, @intFromEnum(dest_file.fd), &off_out, source_stat.size - off, 0);
431 off += amt;
432 if (amt == 0) break;
433 }
434 return;
435 }
436}