diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-08-20 17:57:49 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-08-20 17:57:49 -0400 |
| commit | 302936309a30c9c0bcfe222ec1de470b36c18a06 (patch) | |
| tree | a8ac719bc3675587884a5a6a569f3a44a9992a82 /std | |
| parent | 9e9dce76ffeae54d41ad6485a4fbaf9cd6610c1f (diff) | |
| parent | bb93886791f81830eae1951cc3d89a6992067b55 (diff) | |
| download | zig-302936309a30c9c0bcfe222ec1de470b36c18a06.tar.gz zig-302936309a30c9c0bcfe222ec1de470b36c18a06.zip | |
Merge branch 'path_max' of https://github.com/shawnl/zig into shawnl-path_max
Diffstat (limited to 'std')
| -rw-r--r-- | std/debug/index.zig | 2 | ||||
| -rw-r--r-- | std/io_test.zig | 2 | ||||
| -rw-r--r-- | std/os/file.zig | 8 | ||||
| -rw-r--r-- | std/os/index.zig | 41 | ||||
| -rw-r--r-- | std/os/path.zig | 2 |
5 files changed, 30 insertions, 25 deletions
diff --git a/std/debug/index.zig b/std/debug/index.zig index f06da85f54..748aa2b969 100644 --- a/std/debug/index.zig +++ b/std/debug/index.zig @@ -341,7 +341,7 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) !*ElfStackTrace { } fn printLineFromFile(allocator: *mem.Allocator, out_stream: var, line_info: *const LineInfo) !void { - var f = try os.File.openRead(allocator, line_info.file_name); + var f = try os.File.openRead(line_info.file_name); defer f.close(); // TODO fstat and make sure that the file has the correct size diff --git a/std/io_test.zig b/std/io_test.zig index 56f8a9a6ad..fb0f850095 100644 --- a/std/io_test.zig +++ b/std/io_test.zig @@ -28,7 +28,7 @@ test "write a file, read it, then delete it" { try buf_stream.flush(); } { - var file = try os.File.openRead(allocator, tmp_file_name); + var file = try os.File.openRead(tmp_file_name); defer file.close(); const file_size = try file.getEndPos(); diff --git a/std/os/file.zig b/std/os/file.zig index 074547193c..df79ab4adc 100644 --- a/std/os/file.zig +++ b/std/os/file.zig @@ -29,14 +29,13 @@ pub const File = struct { /// `path` needs to be copied in memory to add a null terminating byte, hence the allocator. /// Call close to clean up. - pub fn openRead(allocator: *mem.Allocator, path: []const u8) OpenError!File { + pub fn openRead(path: []const u8) OpenError!File { if (is_posix) { const flags = posix.O_LARGEFILE | posix.O_RDONLY; - const fd = try os.posixOpen(allocator, path, flags, 0); + const fd = try os.posixOpen(path, flags, 0); return openHandle(fd); } else if (is_windows) { const handle = try os.windowsOpen( - allocator, path, windows.GENERIC_READ, windows.FILE_SHARE_READ, @@ -61,11 +60,10 @@ pub const File = struct { pub fn openWriteMode(allocator: *mem.Allocator, path: []const u8, file_mode: Mode) OpenError!File { if (is_posix) { const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_TRUNC; - const fd = try os.posixOpen(allocator, path, flags, file_mode); + const fd = try os.posixOpen(path, flags, file_mode); return openHandle(fd); } else if (is_windows) { const handle = try os.windowsOpen( - allocator, path, windows.GENERIC_WRITE, windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE, diff --git a/std/os/index.zig b/std/os/index.zig index caef6d1e37..da18e8e47b 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -39,6 +39,12 @@ pub const File = @import("file.zig").File; pub const time = @import("time.zig"); pub const page_size = 4 * 1024; +pub const PATH_MAX = switch (builtin.os) { + Os.linux => linux.PATH_MAX, + Os.macosx, Os.ios => darwin.PATH_MAX, + else => @compileError("Unsupported OS"), + // https://msdn.microsoft.com/en-us/library/930f87yf.aspx +}; pub const UserInfo = @import("get_user_id.zig").UserInfo; pub const getUserInfo = @import("get_user_id.zig").getUserInfo; @@ -437,11 +443,14 @@ pub const PosixOpenError = error{ /// ::file_path needs to be copied in memory to add a null terminating byte. /// Calls POSIX open, keeps trying if it gets interrupted, and translates /// the return value into zig errors. -pub fn posixOpen(allocator: *Allocator, file_path: []const u8, flags: u32, perm: usize) PosixOpenError!i32 { - const path_with_null = try cstr.addNullByte(allocator, file_path); - defer allocator.free(path_with_null); +pub fn posixOpen(file_path: []const u8, flags: u32, perm: usize) PosixOpenError!i32 { + var path_with_null: [PATH_MAX]u8 = undefined; + if (file_path.len > PATH_MAX - 1) + return error.NameTooLong; + mem.copy(u8, path_with_null[0..PATH_MAX - 1], file_path); + path_with_null[file_path.len] = '\x00'; - return posixOpenC(path_with_null.ptr, flags, perm); + return posixOpenC(&path_with_null, flags, perm); } // TODO https://github.com/ziglang/zig/issues/265 @@ -948,7 +957,7 @@ pub fn deleteFilePosix(allocator: *Allocator, file_path: []const u8) !void { /// in the same directory as dest_path. /// Destination file will have the same mode as the source file. pub fn copyFile(allocator: *Allocator, source_path: []const u8, dest_path: []const u8) !void { - var in_file = try os.File.openRead(allocator, source_path); + var in_file = try os.File.openRead(source_path); defer in_file.close(); const mode = try in_file.mode(); @@ -970,7 +979,7 @@ pub fn copyFile(allocator: *Allocator, source_path: []const u8, dest_path: []con /// merged and readily available, /// there is a possibility of power loss or application termination leaving temporary files present pub fn copyFileMode(allocator: *Allocator, source_path: []const u8, dest_path: []const u8, mode: File.Mode) !void { - var in_file = try os.File.openRead(allocator, source_path); + var in_file = try os.File.openRead(source_path); defer in_file.close(); var atomic_file = try AtomicFile.init(allocator, dest_path, mode); @@ -1400,7 +1409,6 @@ pub const Dir = struct { }, Os.macosx, Os.ios => Handle{ .fd = try posixOpen( - allocator, dir_path, posix.O_RDONLY | posix.O_NONBLOCK | posix.O_DIRECTORY | posix.O_CLOEXEC, 0, @@ -1412,7 +1420,6 @@ pub const Dir = struct { }, Os.linux => Handle{ .fd = try posixOpen( - allocator, dir_path, posix.O_RDONLY | posix.O_DIRECTORY | posix.O_CLOEXEC, 0, @@ -1609,17 +1616,17 @@ pub fn changeCurDir(allocator: *Allocator, dir_path: []const u8) !void { } /// Read value of a symbolic link. -pub fn readLink(allocator: *Allocator, pathname: []const u8) ![]u8 { - const path_buf = try allocator.alloc(u8, pathname.len + 1); - defer allocator.free(path_buf); - - mem.copy(u8, path_buf, pathname); - path_buf[pathname.len] = 0; +pub fn readLink(allocator: *Allocator, file_path: []const u8) ![]u8 { + var path_with_null: [PATH_MAX]u8 = undefined; + if (file_path.len > PATH_MAX - 1) + return error.NameTooLong; + mem.copy(u8, path_with_null[0..PATH_MAX - 1], file_path); + path_with_null[file_path.len] = '\x00'; var result_buf = try allocator.alloc(u8, 1024); errdefer allocator.free(result_buf); while (true) { - const ret_val = posix.readlink(path_buf.ptr, result_buf.ptr, result_buf.len); + const ret_val = posix.readlink(&path_with_null, result_buf.ptr, result_buf.len); const err = posix.getErrno(ret_val); if (err > 0) { return switch (err) { @@ -2028,13 +2035,13 @@ pub fn openSelfExe() !os.File { const proc_file_path = "/proc/self/exe"; var fixed_buffer_mem: [proc_file_path.len + 1]u8 = undefined; var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); - return os.File.openRead(&fixed_allocator.allocator, proc_file_path); + return os.File.openRead(proc_file_path); }, Os.macosx, Os.ios => { var fixed_buffer_mem: [darwin.PATH_MAX * 2]u8 = undefined; var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); const self_exe_path = try selfExePath(&fixed_allocator.allocator); - return os.File.openRead(&fixed_allocator.allocator, self_exe_path); + return os.File.openRead(self_exe_path); }, else => @compileError("Unsupported OS"), } diff --git a/std/os/path.zig b/std/os/path.zig index 23c217b295..8c11126d9b 100644 --- a/std/os/path.zig +++ b/std/os/path.zig @@ -1166,7 +1166,7 @@ pub fn real(allocator: *Allocator, pathname: []const u8) ![]u8 { return allocator.shrink(u8, result_buf, cstr.len(result_buf.ptr)); }, Os.linux => { - const fd = try os.posixOpen(allocator, pathname, posix.O_PATH | posix.O_NONBLOCK | posix.O_CLOEXEC, 0); + const fd = try os.posixOpen(pathname, posix.O_PATH | posix.O_NONBLOCK | posix.O_CLOEXEC, 0); defer os.close(fd); var buf: ["/proc/self/fd/-2147483648".len]u8 = undefined; |
