authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-20 17:57:49-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-20 17:57:49-04:00
log302936309a30c9c0bcfe222ec1de470b36c18a06
treea8ac719bc3675587884a5a6a569f3a44a9992a82
parent9e9dce76ffeae54d41ad6485a4fbaf9cd6610c1f
parentbb93886791f81830eae1951cc3d89a6992067b55

Merge branch 'path_max' of https://github.com/shawnl/zig into shawnl-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 {...@@ -341,7 +341,7 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) !*ElfStackTrace {
341}341}
342342
343fn printLineFromFile(allocator: *mem.Allocator, out_stream: var, line_info: *const LineInfo) !void {343fn 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);
345 defer f.close();345 defer f.close();
346 // TODO fstat and make sure that the file has the correct size346 // 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" {...@@ -28,7 +28,7 @@ test "write a file, read it, then delete it" {
28 try buf_stream.flush();28 try buf_stream.flush();
29 }29 }
30 {30 {
31 var file = try os.File.openRead(allocator, tmp_file_name);31 var file = try os.File.openRead(tmp_file_name);
32 defer file.close();32 defer file.close();
3333
34 const file_size = try file.getEndPos();34 const file_size = try file.getEndPos();
std/os/file.zig+3-5
...@@ -29,14 +29,13 @@ pub const File = struct {...@@ -29,14 +29,13 @@ pub const File = struct {
2929
30 /// `path` needs to be copied in memory to add a null terminating byte, hence the allocator.30 /// `path` needs to be copied in memory to add a null terminating byte, hence the allocator.
31 /// Call close to clean up.31 /// 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 {
33 if (is_posix) {33 if (is_posix) {
34 const flags = posix.O_LARGEFILE | posix.O_RDONLY;34 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);
36 return openHandle(fd);36 return openHandle(fd);
37 } else if (is_windows) {37 } else if (is_windows) {
38 const handle = try os.windowsOpen(38 const handle = try os.windowsOpen(
39 allocator,
40 path,39 path,
41 windows.GENERIC_READ,40 windows.GENERIC_READ,
42 windows.FILE_SHARE_READ,41 windows.FILE_SHARE_READ,
...@@ -61,11 +60,10 @@ pub const File = struct {...@@ -61,11 +60,10 @@ pub const File = struct {
61 pub fn openWriteMode(allocator: *mem.Allocator, path: []const u8, file_mode: Mode) OpenError!File {60 pub fn openWriteMode(allocator: *mem.Allocator, path: []const u8, file_mode: Mode) OpenError!File {
62 if (is_posix) {61 if (is_posix) {
63 const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_TRUNC;62 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);
65 return openHandle(fd);64 return openHandle(fd);
66 } else if (is_windows) {65 } else if (is_windows) {
67 const handle = try os.windowsOpen(66 const handle = try os.windowsOpen(
68 allocator,
69 path,67 path,
70 windows.GENERIC_WRITE,68 windows.GENERIC_WRITE,
71 windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE,69 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;...@@ -39,6 +39,12 @@ pub const File = @import("file.zig").File;
39pub const time = @import("time.zig");39pub const time = @import("time.zig");
4040
41pub const page_size = 4 * 1024;41pub 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
43pub const UserInfo = @import("get_user_id.zig").UserInfo;49pub const UserInfo = @import("get_user_id.zig").UserInfo;
44pub const getUserInfo = @import("get_user_id.zig").getUserInfo;50pub const getUserInfo = @import("get_user_id.zig").getUserInfo;
...@@ -437,11 +443,14 @@ pub const PosixOpenError = error{...@@ -437,11 +443,14 @@ pub const PosixOpenError = error{
437/// ::file_path needs to be copied in memory to add a null terminating byte.443/// ::file_path needs to be copied in memory to add a null terminating byte.
438/// Calls POSIX open, keeps trying if it gets interrupted, and translates444/// Calls POSIX open, keeps trying if it gets interrupted, and translates
439/// the return value into zig errors.445/// the return value into zig errors.
440pub fn posixOpen(allocator: *Allocator, file_path: []const u8, flags: u32, perm: usize) PosixOpenError!i32 {446pub fn posixOpen(file_path: []const u8, flags: u32, perm: usize) PosixOpenError!i32 {
441 const path_with_null = try cstr.addNullByte(allocator, file_path);447 var path_with_null: [PATH_MAX]u8 = undefined;
442 defer allocator.free(path_with_null);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);
445}454}
446455
447// TODO https://github.com/ziglang/zig/issues/265456// TODO https://github.com/ziglang/zig/issues/265
...@@ -948,7 +957,7 @@ pub fn deleteFilePosix(allocator: *Allocator, file_path: []const u8) !void {...@@ -948,7 +957,7 @@ pub fn deleteFilePosix(allocator: *Allocator, file_path: []const u8) !void {
948/// in the same directory as dest_path.957/// in the same directory as dest_path.
949/// Destination file will have the same mode as the source file.958/// Destination file will have the same mode as the source file.
950pub fn copyFile(allocator: *Allocator, source_path: []const u8, dest_path: []const u8) !void {959pub 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);
952 defer in_file.close();961 defer in_file.close();
953962
954 const mode = try in_file.mode();963 const mode = try in_file.mode();
...@@ -970,7 +979,7 @@ pub fn copyFile(allocator: *Allocator, source_path: []const u8, dest_path: []con...@@ -970,7 +979,7 @@ pub fn copyFile(allocator: *Allocator, source_path: []const u8, dest_path: []con
970/// merged and readily available,979/// merged and readily available,
971/// there is a possibility of power loss or application termination leaving temporary files present980/// there is a possibility of power loss or application termination leaving temporary files present
972pub fn copyFileMode(allocator: *Allocator, source_path: []const u8, dest_path: []const u8, mode: File.Mode) !void {981pub 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);
974 defer in_file.close();983 defer in_file.close();
975984
976 var atomic_file = try AtomicFile.init(allocator, dest_path, mode);985 var atomic_file = try AtomicFile.init(allocator, dest_path, mode);
...@@ -1400,7 +1409,6 @@ pub const Dir = struct {...@@ -1400,7 +1409,6 @@ pub const Dir = struct {
1400 },1409 },
1401 Os.macosx, Os.ios => Handle{1410 Os.macosx, Os.ios => Handle{
1402 .fd = try posixOpen(1411 .fd = try posixOpen(
1403 allocator,
1404 dir_path,1412 dir_path,
1405 posix.O_RDONLY | posix.O_NONBLOCK | posix.O_DIRECTORY | posix.O_CLOEXEC,1413 posix.O_RDONLY | posix.O_NONBLOCK | posix.O_DIRECTORY | posix.O_CLOEXEC,
1406 0,1414 0,
...@@ -1412,7 +1420,6 @@ pub const Dir = struct {...@@ -1412,7 +1420,6 @@ pub const Dir = struct {
1412 },1420 },
1413 Os.linux => Handle{1421 Os.linux => Handle{
1414 .fd = try posixOpen(1422 .fd = try posixOpen(
1415 allocator,
1416 dir_path,1423 dir_path,
1417 posix.O_RDONLY | posix.O_DIRECTORY | posix.O_CLOEXEC,1424 posix.O_RDONLY | posix.O_DIRECTORY | posix.O_CLOEXEC,
1418 0,1425 0,
...@@ -1609,17 +1616,17 @@ pub fn changeCurDir(allocator: *Allocator, dir_path: []const u8) !void {...@@ -1609,17 +1616,17 @@ pub fn changeCurDir(allocator: *Allocator, dir_path: []const u8) !void {
1609}1616}
16101617
1611/// Read value of a symbolic link.1618/// Read value of a symbolic link.
1612pub fn readLink(allocator: *Allocator, pathname: []const u8) ![]u8 {1619pub fn readLink(allocator: *Allocator, file_path: []const u8) ![]u8 {
1613 const path_buf = try allocator.alloc(u8, pathname.len + 1);1620 var path_with_null: [PATH_MAX]u8 = undefined;
1614 defer allocator.free(path_buf);1621 if (file_path.len > PATH_MAX - 1)
16151622 return error.NameTooLong;
1616 mem.copy(u8, path_buf, pathname);1623 mem.copy(u8, path_with_null[0..PATH_MAX - 1], file_path);
1617 path_buf[pathname.len] = 0;1624 path_with_null[file_path.len] = '\x00';
16181625
1619 var result_buf = try allocator.alloc(u8, 1024);1626 var result_buf = try allocator.alloc(u8, 1024);
1620 errdefer allocator.free(result_buf);1627 errdefer allocator.free(result_buf);
1621 while (true) {1628 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);
1623 const err = posix.getErrno(ret_val);1630 const err = posix.getErrno(ret_val);
1624 if (err > 0) {1631 if (err > 0) {
1625 return switch (err) {1632 return switch (err) {
...@@ -2028,13 +2035,13 @@ pub fn openSelfExe() !os.File {...@@ -2028,13 +2035,13 @@ pub fn openSelfExe() !os.File {
2028 const proc_file_path = "/proc/self/exe";2035 const proc_file_path = "/proc/self/exe";
2029 var fixed_buffer_mem: [proc_file_path.len + 1]u8 = undefined;2036 var fixed_buffer_mem: [proc_file_path.len + 1]u8 = undefined;
2030 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);2037 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);
2032 },2039 },
2033 Os.macosx, Os.ios => {2040 Os.macosx, Os.ios => {
2034 var fixed_buffer_mem: [darwin.PATH_MAX * 2]u8 = undefined;2041 var fixed_buffer_mem: [darwin.PATH_MAX * 2]u8 = undefined;
2035 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);2042 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
2036 const self_exe_path = try selfExePath(&fixed_allocator.allocator);2043 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);
2038 },2045 },
2039 else => @compileError("Unsupported OS"),2046 else => @compileError("Unsupported OS"),
2040 }2047 }
std/os/path.zig+1-1
...@@ -1166,7 +1166,7 @@ pub fn real(allocator: *Allocator, pathname: []const u8) ![]u8 {...@@ -1166,7 +1166,7 @@ pub fn real(allocator: *Allocator, pathname: []const u8) ![]u8 {
1166 return allocator.shrink(u8, result_buf, cstr.len(result_buf.ptr));1166 return allocator.shrink(u8, result_buf, cstr.len(result_buf.ptr));
1167 },1167 },
1168 Os.linux => {1168 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);
1170 defer os.close(fd);1170 defer os.close(fd);
11711171
1172 var buf: ["/proc/self/fd/-2147483648".len]u8 = undefined;1172 var buf: ["/proc/self/fd/-2147483648".len]u8 = undefined;