authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-16 19:28:35-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-23 02:37:11-07:00
log7036644ed2a50e4b10f45b8fce23ce5258e5f58f
tree18960851c8f9553a2c72ccc95d9910dc6658987f
parent511acc167f4cea39cb780ad93a36dfd0e64e4417

std.http.Client: remove advisory file lock on fetch

This is not an appropriate place to put this code. It belongs in the caller's code, if at all.

1 files changed, 15 insertions(+), 22 deletions(-)

lib/std/http/Client.zig+15-22
...@@ -1648,32 +1648,25 @@ pub fn fetch(client: *Client, allocator: Allocator, options: FetchOptions) !Fetc...@@ -1648,32 +1648,25 @@ pub fn fetch(client: *Client, allocator: Allocator, options: FetchOptions) !Fetc
1648 });1648 });
1649 defer req.deinit();1649 defer req.deinit();
16501650
1651 { // Block to maintain lock of file to attempt to prevent a race condition where another process modifies the file while we are reading it.1651 switch (options.payload) {
1652 // This relies on other processes actually obeying the advisory lock, which is not guaranteed.1652 .string => |str| req.transfer_encoding = .{ .content_length = str.len },
1653 if (options.payload == .file) try options.payload.file.lock(.shared);1653 .file => |file| req.transfer_encoding = .{ .content_length = (try file.stat()).size },
1654 defer if (options.payload == .file) options.payload.file.unlock();1654 .none => {},
16551655 }
1656 switch (options.payload) {
1657 .string => |str| req.transfer_encoding = .{ .content_length = str.len },
1658 .file => |file| req.transfer_encoding = .{ .content_length = (try file.stat()).size },
1659 .none => {},
1660 }
1661
1662 try req.send(.{ .raw_uri = options.raw_uri });
16631656
1664 switch (options.payload) {1657 try req.send(.{ .raw_uri = options.raw_uri });
1665 .string => |str| try req.writeAll(str),
1666 .file => |file| {
1667 try file.seekTo(0);
1668 var fifo = std.fifo.LinearFifo(u8, .{ .Static = 8192 }).init();
1669 try fifo.pump(file.reader(), req.writer());
1670 },
1671 .none => {},
1672 }
16731658
1674 try req.finish();1659 switch (options.payload) {
1660 .string => |str| try req.writeAll(str),
1661 .file => |file| {
1662 try file.seekTo(0);
1663 var fifo = std.fifo.LinearFifo(u8, .{ .Static = 8192 }).init();
1664 try fifo.pump(file.reader(), req.writer());
1665 },
1666 .none => {},
1675 }1667 }
16761668
1669 try req.finish();
1677 try req.wait();1670 try req.wait();
16781671
1679 var res: FetchResult = .{1672 var res: FetchResult = .{