authorgravatar for shawn@git.icuShawn Landden <shawn@git.icu> 2018-08-19 21:40:14-07:00
committergravatar for shawn@git.icuShawn Landden <shawn@git.icu> 2018-08-19 21:42:48-07:00
logbb93886791f81830eae1951cc3d89a6992067b55
treef339276222262dff1abf0cac7da55398d3ee5b75
parent53b18b079189cce355767ce4fde4fc586f0d3248

do not use an allocator when we don't need to because of the existance of PATH_MAX


5 files changed, 30 insertions(+), 25 deletions(-)

std/debug/index.zig+1-1
......@@ -341,7 +341,7 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) !*ElfStackTrace {
341341}
342342
343343fn printLineFromFile(allocator: *mem.Allocator, out_stream: var, line_info: *const LineInfo) !void {
344 var f = try os.File.openRead(allocator, line_info.file_name);
344 var f = try os.File.openRead(line_info.file_name);
345345 defer f.close();
346346 // TODO fstat and make sure that the file has the correct size
347347
std/io_test.zig+1-1
......@@ -28,7 +28,7 @@ test "write a file, read it, then delete it" {
2828 try buf_stream.flush();
2929 }
3030 {
31 var file = try os.File.openRead(allocator, tmp_file_name);
31 var file = try os.File.openRead(tmp_file_name);
3232 defer file.close();
3333
3434 const file_size = try file.getEndPos();
std/os/file.zig+3-5
......@@ -29,14 +29,13 @@ pub const File = struct {
2929
3030 /// `path` needs to be copied in memory to add a null terminating byte, hence the allocator.
3131 /// Call close to clean up.
32 pub fn openRead(allocator: *mem.Allocator, path: []const u8) OpenError!File {
32 pub fn openRead(path: []const u8) OpenError!File {
3333 if (is_posix) {
3434 const flags = posix.O_LARGEFILE | posix.O_RDONLY;
35 const fd = try os.posixOpen(allocator, path, flags, 0);
35 const fd = try os.posixOpen(path, flags, 0);
3636 return openHandle(fd);
3737 } else if (is_windows) {
3838 const handle = try os.windowsOpen(
39 allocator,
4039 path,
4140 windows.GENERIC_READ,
4241 windows.FILE_SHARE_READ,
......@@ -61,11 +60,10 @@ pub const File = struct {
6160 pub fn openWriteMode(allocator: *mem.Allocator, path: []const u8, file_mode: Mode) OpenError!File {
6261 if (is_posix) {
6362 const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_TRUNC;
64 const fd = try os.posixOpen(allocator, path, flags, file_mode);
63 const fd = try os.posixOpen(path, flags, file_mode);
6564 return openHandle(fd);
6665 } else if (is_windows) {
6766 const handle = try os.windowsOpen(
68 allocator,
6967 path,
7068 windows.GENERIC_WRITE,
7169 windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE,
std/os/index.zig+24-17
......@@ -39,6 +39,12 @@ pub const File = @import("file.zig").File;
3939pub const time = @import("time.zig");
4040
4141pub const page_size = 4 * 1024;
42pub const PATH_MAX = switch (builtin.os) {
43 Os.linux => linux.PATH_MAX,
44 Os.macosx, Os.ios => darwin.PATH_MAX,
45 else => @compileError("Unsupported OS"),
46 // https://msdn.microsoft.com/en-us/library/930f87yf.aspx
47};
4248
4349pub const UserInfo = @import("get_user_id.zig").UserInfo;
4450pub const getUserInfo = @import("get_user_id.zig").getUserInfo;
......@@ -437,11 +443,14 @@ pub const PosixOpenError = error{
437443/// ::file_path needs to be copied in memory to add a null terminating byte.
438444/// Calls POSIX open, keeps trying if it gets interrupted, and translates
439445/// the return value into zig errors.
440pub fn posixOpen(allocator: *Allocator, file_path: []const u8, flags: u32, perm: usize) PosixOpenError!i32 {
441 const path_with_null = try cstr.addNullByte(allocator, file_path);
442 defer allocator.free(path_with_null);
446pub fn posixOpen(file_path: []const u8, flags: u32, perm: usize) PosixOpenError!i32 {
447 var path_with_null: [PATH_MAX]u8 = undefined;
448 if (file_path.len > PATH_MAX - 1)
449 return error.NameTooLong;
450 mem.copy(u8, path_with_null[0..PATH_MAX - 1], file_path);
451 path_with_null[file_path.len] = '\x00';
443452
444 return posixOpenC(path_with_null.ptr, flags, perm);
453 return posixOpenC(&path_with_null, flags, perm);
445454}
446455
447456// TODO https://github.com/ziglang/zig/issues/265
......@@ -948,7 +957,7 @@ pub fn deleteFilePosix(allocator: *Allocator, file_path: []const u8) !void {
948957/// in the same directory as dest_path.
949958/// Destination file will have the same mode as the source file.
950959pub fn copyFile(allocator: *Allocator, source_path: []const u8, dest_path: []const u8) !void {
951 var in_file = try os.File.openRead(allocator, source_path);
960 var in_file = try os.File.openRead(source_path);
952961 defer in_file.close();
953962
954963 const mode = try in_file.mode();
......@@ -970,7 +979,7 @@ pub fn copyFile(allocator: *Allocator, source_path: []const u8, dest_path: []con
970979/// merged and readily available,
971980/// there is a possibility of power loss or application termination leaving temporary files present
972981pub fn copyFileMode(allocator: *Allocator, source_path: []const u8, dest_path: []const u8, mode: File.Mode) !void {
973 var in_file = try os.File.openRead(allocator, source_path);
982 var in_file = try os.File.openRead(source_path);
974983 defer in_file.close();
975984
976985 var atomic_file = try AtomicFile.init(allocator, dest_path, mode);
......@@ -1400,7 +1409,6 @@ pub const Dir = struct {
14001409 },
14011410 Os.macosx, Os.ios => Handle{
14021411 .fd = try posixOpen(
1403 allocator,
14041412 dir_path,
14051413 posix.O_RDONLY | posix.O_NONBLOCK | posix.O_DIRECTORY | posix.O_CLOEXEC,
14061414 0,
......@@ -1412,7 +1420,6 @@ pub const Dir = struct {
14121420 },
14131421 Os.linux => Handle{
14141422 .fd = try posixOpen(
1415 allocator,
14161423 dir_path,
14171424 posix.O_RDONLY | posix.O_DIRECTORY | posix.O_CLOEXEC,
14181425 0,
......@@ -1609,17 +1616,17 @@ pub fn changeCurDir(allocator: *Allocator, dir_path: []const u8) !void {
16091616}
16101617
16111618/// Read value of a symbolic link.
1612pub fn readLink(allocator: *Allocator, pathname: []const u8) ![]u8 {
1613 const path_buf = try allocator.alloc(u8, pathname.len + 1);
1614 defer allocator.free(path_buf);
1615
1616 mem.copy(u8, path_buf, pathname);
1617 path_buf[pathname.len] = 0;
1619pub fn readLink(allocator: *Allocator, file_path: []const u8) ![]u8 {
1620 var path_with_null: [PATH_MAX]u8 = undefined;
1621 if (file_path.len > PATH_MAX - 1)
1622 return error.NameTooLong;
1623 mem.copy(u8, path_with_null[0..PATH_MAX - 1], file_path);
1624 path_with_null[file_path.len] = '\x00';
16181625
16191626 var result_buf = try allocator.alloc(u8, 1024);
16201627 errdefer allocator.free(result_buf);
16211628 while (true) {
1622 const ret_val = posix.readlink(path_buf.ptr, result_buf.ptr, result_buf.len);
1629 const ret_val = posix.readlink(&path_with_null, result_buf.ptr, result_buf.len);
16231630 const err = posix.getErrno(ret_val);
16241631 if (err > 0) {
16251632 return switch (err) {
......@@ -2028,13 +2035,13 @@ pub fn openSelfExe() !os.File {
20282035 const proc_file_path = "/proc/self/exe";
20292036 var fixed_buffer_mem: [proc_file_path.len + 1]u8 = undefined;
20302037 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
2031 return os.File.openRead(&fixed_allocator.allocator, proc_file_path);
2038 return os.File.openRead(proc_file_path);
20322039 },
20332040 Os.macosx, Os.ios => {
20342041 var fixed_buffer_mem: [darwin.PATH_MAX * 2]u8 = undefined;
20352042 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
20362043 const self_exe_path = try selfExePath(&fixed_allocator.allocator);
2037 return os.File.openRead(&fixed_allocator.allocator, self_exe_path);
2044 return os.File.openRead(self_exe_path);
20382045 },
20392046 else => @compileError("Unsupported OS"),
20402047 }
std/os/path.zig+1-1
......@@ -1166,7 +1166,7 @@ pub fn real(allocator: *Allocator, pathname: []const u8) ![]u8 {
11661166 return allocator.shrink(u8, result_buf, cstr.len(result_buf.ptr));
11671167 },
11681168 Os.linux => {
1169 const fd = try os.posixOpen(allocator, pathname, posix.O_PATH | posix.O_NONBLOCK | posix.O_CLOEXEC, 0);
1169 const fd = try os.posixOpen(pathname, posix.O_PATH | posix.O_NONBLOCK | posix.O_CLOEXEC, 0);
11701170 defer os.close(fd);
11711171
11721172 var buf: ["/proc/self/fd/-2147483648".len]u8 = undefined;