authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-11 13:06:30-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-11 13:06:30-04:00
logd96b6c0d9f7a4fc6e7e28dd92d18e548f00885de
tree1c2774d9d9ffe279fa1ae330eec529dbdc9cb994
parented13cffca4c42718cc7239faf7aab642ac67ef77
signaturelock-open Commit is signed but in an unrecognized format.

fix footguns in File readAll functions


4 files changed, 75 insertions(+), 37 deletions(-)

lib/std/fs.zig+12-21
......@@ -96,6 +96,7 @@ pub fn updateFile(source_path: []const u8, dest_path: []const u8) !PrevStatus {
9696/// atime, and mode of the source file so that the next call to `updateFile` will not need a copy.
9797/// Returns the previous status of the file before updating.
9898/// If any of the directories do not exist for dest_path, they are created.
99/// TODO rework this to integrate with Dir
99100pub fn updateFileMode(source_path: []const u8, dest_path: []const u8, mode: ?File.Mode) !PrevStatus {
100101 const my_cwd = cwd();
101102
......@@ -141,29 +142,25 @@ pub fn updateFileMode(source_path: []const u8, dest_path: []const u8, mode: ?Fil
141142/// there is a possibility of power loss or application termination leaving temporary files present
142143/// in the same directory as dest_path.
143144/// Destination file will have the same mode as the source file.
145/// TODO rework this to integrate with Dir
144146pub fn copyFile(source_path: []const u8, dest_path: []const u8) !void {
145147 var in_file = try cwd().openFile(source_path, .{});
146148 defer in_file.close();
147149
148 const mode = try in_file.mode();
149 const in_stream = &in_file.inStream().stream;
150 const stat = try in_file.stat();
150151
151 var atomic_file = try AtomicFile.init(dest_path, mode);
152 var atomic_file = try AtomicFile.init(dest_path, stat.mode);
152153 defer atomic_file.deinit();
153154
154 var buf: [mem.page_size]u8 = undefined;
155 while (true) {
156 const amt = try in_stream.readFull(buf[0..]);
157 try atomic_file.file.write(buf[0..amt]);
158 if (amt != buf.len) {
159 return atomic_file.finish();
160 }
161 }
155 try atomic_file.file.writeFileAll(in_file, .{ .in_len = stat.size });
156 return atomic_file.finish();
162157}
163158
164/// Guaranteed to be atomic. However until https://patchwork.kernel.org/patch/9636735/ is
165/// merged and readily available,
159/// Guaranteed to be atomic.
160/// On Linux, until https://patchwork.kernel.org/patch/9636735/ is merged and readily available,
166161/// there is a possibility of power loss or application termination leaving temporary files present
162/// in the same directory as dest_path.
163/// TODO rework this to integrate with Dir
167164pub fn copyFileMode(source_path: []const u8, dest_path: []const u8, mode: File.Mode) !void {
168165 var in_file = try cwd().openFile(source_path, .{});
169166 defer in_file.close();
......@@ -171,14 +168,8 @@ pub fn copyFileMode(source_path: []const u8, dest_path: []const u8, mode: File.M
171168 var atomic_file = try AtomicFile.init(dest_path, mode);
172169 defer atomic_file.deinit();
173170
174 var buf: [mem.page_size * 6]u8 = undefined;
175 while (true) {
176 const amt = try in_file.read(buf[0..]);
177 try atomic_file.file.write(buf[0..amt]);
178 if (amt != buf.len) {
179 return atomic_file.finish();
180 }
181 }
171 try atomic_file.file.writeFileAll(in_file, .{});
172 return atomic_file.finish();
182173}
183174
184175/// TODO update this API to avoid a getrandom syscall for every operation. It
lib/std/fs/file.zig+33-9
......@@ -250,11 +250,16 @@ pub const File = struct {
250250 }
251251 }
252252
253 pub fn readAll(self: File, buffer: []u8) ReadError!void {
253 /// Returns the number of bytes read. If the number read is smaller than `buffer.len`, it
254 /// means the file reached the end. Reaching the end of a file is not an error condition.
255 pub fn readAll(self: File, buffer: []u8) ReadError!usize {
254256 var index: usize = 0;
255 while (index < buffer.len) {
256 index += try self.read(buffer[index..]);
257 while (index != buffer.len) {
258 const amt = try self.read(buffer[index..]);
259 if (amt == 0) break;
260 index += amt;
257261 }
262 return index;
258263 }
259264
260265 pub fn pread(self: File, buffer: []u8, offset: u64) PReadError!usize {
......@@ -265,11 +270,16 @@ pub const File = struct {
265270 }
266271 }
267272
268 pub fn preadAll(self: File, buffer: []u8, offset: u64) PReadError!void {
273 /// Returns the number of bytes read. If the number read is smaller than `buffer.len`, it
274 /// means the file reached the end. Reaching the end of a file is not an error condition.
275 pub fn preadAll(self: File, buffer: []u8, offset: u64) PReadError!usize {
269276 var index: usize = 0;
270 while (index < buffer.len) {
271 index += try self.pread(buffer[index..], offset + index);
277 while (index != buffer.len) {
278 const amt = try self.pread(buffer[index..], offset + index);
279 if (amt == 0) break;
280 index += amt;
272281 }
282 return index;
273283 }
274284
275285 pub fn readv(self: File, iovecs: []const os.iovec) ReadError!usize {
......@@ -280,19 +290,27 @@ pub const File = struct {
280290 }
281291 }
282292
293 /// Returns the number of bytes read. If the number read is smaller than the total bytes
294 /// from all the buffers, it means the file reached the end. Reaching the end of a file
295 /// is not an error condition.
283296 /// The `iovecs` parameter is mutable because this function needs to mutate the fields in
284297 /// order to handle partial reads from the underlying OS layer.
285 pub fn readvAll(self: File, iovecs: []os.iovec) ReadError!void {
298 pub fn readvAll(self: File, iovecs: []os.iovec) ReadError!usize {
286299 if (iovecs.len == 0) return;
287300
288301 var i: usize = 0;
302 var off: usize = 0;
289303 while (true) {
290304 var amt = try self.readv(iovecs[i..]);
305 var eof = amt == 0;
306 off += amt;
291307 while (amt >= iovecs[i].iov_len) {
292308 amt -= iovecs[i].iov_len;
293309 i += 1;
294 if (i >= iovecs.len) return;
310 if (i >= iovecs.len) return off;
311 eof = false;
295312 }
313 if (eof) return off;
296314 iovecs[i].iov_base += amt;
297315 iovecs[i].iov_len -= amt;
298316 }
......@@ -306,6 +324,9 @@ pub const File = struct {
306324 }
307325 }
308326
327 /// Returns the number of bytes read. If the number read is smaller than the total bytes
328 /// from all the buffers, it means the file reached the end. Reaching the end of a file
329 /// is not an error condition.
309330 /// The `iovecs` parameter is mutable because this function needs to mutate the fields in
310331 /// order to handle partial reads from the underlying OS layer.
311332 pub fn preadvAll(self: File, iovecs: []const os.iovec, offset: u64) PReadError!void {
......@@ -315,12 +336,15 @@ pub const File = struct {
315336 var off: usize = 0;
316337 while (true) {
317338 var amt = try self.preadv(iovecs[i..], offset + off);
339 var eof = amt == 0;
318340 off += amt;
319341 while (amt >= iovecs[i].iov_len) {
320342 amt -= iovecs[i].iov_len;
321343 i += 1;
322 if (i >= iovecs.len) return;
344 if (i >= iovecs.len) return off;
345 eof = false;
323346 }
347 if (eof) return off;
324348 iovecs[i].iov_base += amt;
325349 iovecs[i].iov_len -= amt;
326350 }
lib/std/io/in_stream.zig+1-4
......@@ -28,10 +28,7 @@ pub fn InStream(
2828 return readFn(self.context, buffer);
2929 }
3030
31 /// Deprecated: use `readAll`.
32 pub const readFull = readAll;
33
34 /// Returns the number of bytes read. If the number read is smaller than buf.len, it
31 /// Returns the number of bytes read. If the number read is smaller than `buffer.len`, it
3532 /// means the stream reached the end. Reaching the end of a stream is not an error
3633 /// condition.
3734 pub fn readAll(self: Self, buffer: []u8) Error!usize {
lib/std/os/test.zig+29-3
......@@ -95,15 +95,41 @@ test "sendfile" {
9595 },
9696 };
9797
98 var written_buf: [header1.len + header2.len + 10 + trailer1.len + trailer2.len]u8 = undefined;
98 var written_buf: [100]u8 = undefined;
9999 try dest_file.writeFileAll(src_file, .{
100100 .in_offset = 1,
101101 .in_len = 10,
102102 .headers_and_trailers = &hdtr,
103103 .header_count = 2,
104104 });
105 try dest_file.preadAll(&written_buf, 0);
106 expect(mem.eql(u8, &written_buf, "header1\nsecond header\nine1\nsecontrailer1\nsecond trailer\n"));
105 const amt = try dest_file.preadAll(&written_buf, 0);
106 expect(mem.eql(u8, written_buf[0..amt], "header1\nsecond header\nine1\nsecontrailer1\nsecond trailer\n"));
107}
108
109test "fs.copyFile" {
110 const data = "u6wj+JmdF3qHsFPE BUlH2g4gJCmEz0PP";
111 const src_file = "tmp_test_copy_file.txt";
112 const dest_file = "tmp_test_copy_file2.txt";
113 const dest_file2 = "tmp_test_copy_file3.txt";
114
115 try fs.cwd().writeFile(src_file, data);
116 defer fs.cwd().deleteFile(src_file) catch {};
117
118 try fs.copyFile(src_file, dest_file);
119 defer fs.cwd().deleteFile(dest_file) catch {};
120
121 try fs.copyFileMode(src_file, dest_file2, File.default_mode);
122 defer fs.cwd().deleteFile(dest_file2) catch {};
123
124 try expectFileContents(dest_file, data);
125 try expectFileContents(dest_file2, data);
126}
127
128fn expectFileContents(file_path: []const u8, data: []const u8) !void {
129 const contents = try fs.cwd().readFileAlloc(testing.allocator, file_path, 1000);
130 defer testing.allocator.free(contents);
131
132 testing.expectEqualSlices(u8, data, contents);
107133}
108134
109135test "std.Thread.getCurrentId" {