aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-06-18 21:09:32 -0400
committerAndrew Kelley <andrew@ziglang.org>2020-06-18 21:09:32 -0400
commitb9e3df92db1b5cb137d880a7612b6ee2e7e1f60e (patch)
tree19cf169ef5797b19337ec1ba578097f6c2d01a08 /lib/std
parentbd17a373cc20c919be9fa279a4420033e959ca92 (diff)
parentf7bcc8e04087e2308582d8b6f068e2e71b65bef9 (diff)
downloadzig-b9e3df92db1b5cb137d880a7612b6ee2e7e1f60e.tar.gz
zig-b9e3df92db1b5cb137d880a7612b6ee2e7e1f60e.zip
Merge branch 'deingithub-fmt-mode-thingybob'
closes #5617 closes #5616
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/fs.zig9
-rw-r--r--lib/std/fs/file.zig29
2 files changed, 30 insertions, 8 deletions
diff --git a/lib/std/fs.zig b/lib/std/fs.zig
index 262aa4872d..3ff4819ac5 100644
--- a/lib/std/fs.zig
+++ b/lib/std/fs.zig
@@ -1229,14 +1229,9 @@ pub const Dir = struct {
var file = try self.openFile(file_path, .{});
defer file.close();
- const size = math.cast(usize, try file.getEndPos()) catch math.maxInt(usize);
- if (size > max_bytes) return error.FileTooBig;
+ const stat_size = try file.getEndPos();
- const buf = try allocator.allocWithOptions(u8, size, alignment, optional_sentinel);
- errdefer allocator.free(buf);
-
- try file.inStream().readNoEof(buf);
- return buf;
+ return file.readAllAllocOptions(allocator, stat_size, max_bytes, alignment, optional_sentinel);
}
pub const DeleteTreeError = error{
diff --git a/lib/std/fs/file.zig b/lib/std/fs/file.zig
index d950a1cfa4..0a3c1b5ab7 100644
--- a/lib/std/fs/file.zig
+++ b/lib/std/fs/file.zig
@@ -209,7 +209,7 @@ pub const File = struct {
/// TODO: integrate with async I/O
pub fn mode(self: File) ModeError!Mode {
if (builtin.os.tag == .windows) {
- return {};
+ return 0;
}
return (try self.stat()).mode;
}
@@ -306,6 +306,33 @@ pub const File = struct {
try os.futimens(self.handle, &times);
}
+ /// On success, caller owns returned buffer.
+ /// If the file is larger than `max_bytes`, returns `error.FileTooBig`.
+ pub fn readAllAlloc(self: File, allocator: *mem.Allocator, stat_size: u64, max_bytes: usize) ![]u8 {
+ return self.readAllAllocOptions(allocator, stat_size, max_bytes, @alignOf(u8), null);
+ }
+
+ /// On success, caller owns returned buffer.
+ /// If the file is larger than `max_bytes`, returns `error.FileTooBig`.
+ /// Allows specifying alignment and a sentinel value.
+ pub fn readAllAllocOptions(
+ self: File,
+ allocator: *mem.Allocator,
+ stat_size: u64,
+ max_bytes: usize,
+ comptime alignment: u29,
+ comptime optional_sentinel: ?u8,
+ ) !(if (optional_sentinel) |s| [:s]align(alignment) u8 else []align(alignment) u8) {
+ const size = math.cast(usize, stat_size) catch math.maxInt(usize);
+ if (size > max_bytes) return error.FileTooBig;
+
+ const buf = try allocator.allocWithOptions(u8, size, alignment, optional_sentinel);
+ errdefer allocator.free(buf);
+
+ try self.inStream().readNoEof(buf);
+ return buf;
+ }
+
pub const ReadError = os.ReadError;
pub const PReadError = os.PReadError;