Skip to content

Instantly share code, notes, and snippets.

@Jarred-Sumner
Created April 27, 2023 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jarred-Sumner/b37b93399b63cbfd86e908c59a0a37df to your computer and use it in GitHub Desktop.
Save Jarred-Sumner/b37b93399b63cbfd86e908c59a0a37df to your computer and use it in GitHub Desktop.
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