| ... | @@ -39,6 +39,12 @@ pub const File = @import("file.zig").File; | ... | @@ -39,6 +39,12 @@ pub const File = @import("file.zig").File; |
| 39 | pub const time = @import("time.zig"); | 39 | pub const time = @import("time.zig"); |
| 40 | | 40 | |
| 41 | pub const page_size = 4 * 1024; | 41 | pub const page_size = 4 * 1024; |
| | 42 | pub 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 | }; |
| 42 | | 48 | |
| 43 | pub const UserInfo = @import("get_user_id.zig").UserInfo; | 49 | pub const UserInfo = @import("get_user_id.zig").UserInfo; |
| 44 | pub const getUserInfo = @import("get_user_id.zig").getUserInfo; | 50 | pub 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 translates | 444 | /// 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. |
| 440 | pub fn posixOpen(allocator: *Allocator, file_path: []const u8, flags: u32, perm: usize) PosixOpenError!i32 { | 446 | pub 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'; |
| 443 | | 452 | |
| 444 | return posixOpenC(path_with_null.ptr, flags, perm); | 453 | return posixOpenC(&path_with_null, flags, perm); |
| 445 | } | 454 | } |
| 446 | | 455 | |
| 447 | // TODO https://github.com/ziglang/zig/issues/265 | 456 | // 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. |
| 950 | pub fn copyFile(allocator: *Allocator, source_path: []const u8, dest_path: []const u8) !void { | 959 | pub 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(); |
| 953 | | 962 | |
| 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 present | 980 | /// there is a possibility of power loss or application termination leaving temporary files present |
| 972 | pub fn copyFileMode(allocator: *Allocator, source_path: []const u8, dest_path: []const u8, mode: File.Mode) !void { | 981 | pub 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(); |
| 975 | | 984 | |
| 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 | } |
| 1610 | | 1617 | |
| 1611 | /// Read value of a symbolic link. | 1618 | /// Read value of a symbolic link. |
| 1612 | pub fn readLink(allocator: *Allocator, pathname: []const u8) ![]u8 { | 1619 | pub 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) |
| 1615 | | 1622 | 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'; |
| 1618 | | 1625 | |
| 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 | } |