Created
April 27, 2023 09:32
-
-
Save Jarred-Sumner/b37b93399b63cbfd86e908c59a0a37df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
pub fn preallocate_file(fd: std.os.fd_t, offset: std.os.off_t, len: std.os.off_t) void { | |
_ = std.os.linux.fallocate(fd, 0, @intCast(i64, offset), len); | |
} | |
pub fn main() anyerror!void { | |
const argv = std.process.argsAlloc(std.heap.c_allocator) catch unreachable; | |
if (argv.len < 3) { | |
std.log.warn("Usage: {s} <length> <file> [--preallocate]\n", .{argv[0]}); | |
return; | |
} | |
const length = try std.fmt.parseInt(usize, argv[1], 10); | |
const file = try std.fs.cwd().createFileZ(argv[2], .{}); | |
const should_preallocate = std.mem.eql(u8, argv[argv.len - 1], "--preallocate"); | |
const bytes = try std.heap.c_allocator.alloc(u8, length); | |
if (should_preallocate) { | |
preallocate_file(file.handle, 0, @intCast(std.os.off_t, length)); | |
} | |
try file.writeAll(bytes); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment