| ... | @@ -1883,11 +1883,41 @@ pub fn cwd() Dir { | ... | @@ -1883,11 +1883,41 @@ pub fn cwd() Dir { |
| 1883 | } | 1883 | } |
| 1884 | } | 1884 | } |
| 1885 | | 1885 | |
| | 1886 | /// Opens a directory at the given path. The directory is a system resource that remains |
| | 1887 | /// open until `close` is called on the result. |
| | 1888 | /// See `openDirAbsoluteZ` for a function that accepts a null-terminated path. |
| | 1889 | /// |
| | 1890 | /// Asserts that the path parameter has no null bytes. |
| | 1891 | pub fn openDirAbsolute(absolute_path: []const u8, flags: Dir.OpenDirOptions) File.OpenError!Dir { |
| | 1892 | if (builtin.os.tag == .wasi) { |
| | 1893 | @compileError("WASI doesn't have the concept of an absolute directory; use openDir instead for WASI."); |
| | 1894 | } |
| | 1895 | assert(path.isAbsolute(absolute_path)); |
| | 1896 | return cwd().openDir(absolute_path, flags); |
| | 1897 | } |
| | 1898 | |
| | 1899 | /// Same as `openDirAbsolute` but the path parameter is null-terminated. |
| | 1900 | pub fn openDirAbsoluteZ(absolute_path_c: [*:0]const u8, flags: Dir.OpenDirOptions) File.OpenError!Dir { |
| | 1901 | if (builtin.os.tag == .wasi) { |
| | 1902 | @compileError("WASI doesn't have the concept of an absolute directory; use openDir instead for WASI."); |
| | 1903 | } |
| | 1904 | assert(path.isAbsoluteZ(absolute_path_c)); |
| | 1905 | return cwd().openDirZ(absolute_path_c, flags); |
| | 1906 | } |
| | 1907 | /// Same as `openDirAbsolute` but the path parameter is null-terminated. |
| | 1908 | pub fn openDirAbsoluteW(absolute_path_c: [*:0]const u16, flags: Dir.OpenDirOptions) File.OpenError!Dir { |
| | 1909 | if (builtin.os.tag == .wasi) { |
| | 1910 | @compileError("WASI doesn't have the concept of an absolute directory; use openDir instead for WASI."); |
| | 1911 | } |
| | 1912 | assert(path.isAbsoluteWindowsW(absolute_path_c)); |
| | 1913 | return cwd().openDirW(absolute_path_c, flags); |
| | 1914 | } |
| | 1915 | |
| 1886 | /// Opens a file for reading or writing, without attempting to create a new file, based on an absolute path. | 1916 | /// Opens a file for reading or writing, without attempting to create a new file, based on an absolute path. |
| 1887 | /// Call `File.close` to release the resource. | 1917 | /// Call `File.close` to release the resource. |
| 1888 | /// Asserts that the path is absolute. See `Dir.openFile` for a function that | 1918 | /// Asserts that the path is absolute. See `Dir.openFile` for a function that |
| 1889 | /// operates on both absolute and relative paths. | 1919 | /// operates on both absolute and relative paths. |
| 1890 | /// Asserts that the path parameter has no null bytes. See `openFileAbsoluteC` for a function | 1920 | /// Asserts that the path parameter has no null bytes. See `openFileAbsoluteZ` for a function |
| 1891 | /// that accepts a null-terminated path. | 1921 | /// that accepts a null-terminated path. |
| 1892 | pub fn openFileAbsolute(absolute_path: []const u8, flags: File.OpenFlags) File.OpenError!File { | 1922 | pub fn openFileAbsolute(absolute_path: []const u8, flags: File.OpenFlags) File.OpenError!File { |
| 1893 | assert(path.isAbsolute(absolute_path)); | 1923 | assert(path.isAbsolute(absolute_path)); |
| ... | @@ -1908,6 +1938,36 @@ pub fn openFileAbsoluteW(absolute_path_w: []const u16, flags: File.OpenFlags) Fi | ... | @@ -1908,6 +1938,36 @@ pub fn openFileAbsoluteW(absolute_path_w: []const u16, flags: File.OpenFlags) Fi |
| 1908 | return cwd().openFileW(absolute_path_w, flags); | 1938 | return cwd().openFileW(absolute_path_w, flags); |
| 1909 | } | 1939 | } |
| 1910 | | 1940 | |
| | 1941 | /// Test accessing `path`. |
| | 1942 | /// `path` is UTF8-encoded. |
| | 1943 | /// Be careful of Time-Of-Check-Time-Of-Use race conditions when using this function. |
| | 1944 | /// For example, instead of testing if a file exists and then opening it, just |
| | 1945 | /// open it and handle the error for file not found. |
| | 1946 | /// See `accessAbsoluteZ` for a function that accepts a null-terminated path. |
| | 1947 | pub fn accessAbsolute(absolute_path: []const u8, flags: File.OpenFlags) Dir.AccessError!void { |
| | 1948 | if (builtin.os.tag == .wasi) { |
| | 1949 | @compileError("WASI doesn't have the concept of an absolute path; use access instead for WASI."); |
| | 1950 | } |
| | 1951 | assert(path.isAbsolute(absolute_path)); |
| | 1952 | try cwd().access(absolute_path, flags); |
| | 1953 | } |
| | 1954 | /// Same as `accessAbsolute` but the path parameter is null-terminated. |
| | 1955 | pub fn accessAbsoluteZ(absolute_path: [*:0]const u8, flags: File.OpenFlags) Dir.AccessError!void { |
| | 1956 | if (builtin.os.tag == .wasi) { |
| | 1957 | @compileError("WASI doesn't have the concept of an absolute path; use access instead for WASI."); |
| | 1958 | } |
| | 1959 | assert(path.isAbsoluteZ(absolute_path)); |
| | 1960 | try cwd().accessZ(absolute_path, flags); |
| | 1961 | } |
| | 1962 | /// Same as `accessAbsolute` but the path parameter is WTF-16 encoded. |
| | 1963 | pub fn accessAbsoluteW(absolute_path: [*:0]const 16, flags: File.OpenFlags) Dir.AccessError!void { |
| | 1964 | if (builtin.os.tag == .wasi) { |
| | 1965 | @compileError("WASI doesn't have the concept of an absolute path; use access instead for WASI."); |
| | 1966 | } |
| | 1967 | assert(path.isAbsoluteWindowsW(absolute_path)); |
| | 1968 | try cwd().accessW(absolute_path, flags); |
| | 1969 | } |
| | 1970 | |
| 1911 | /// Creates, opens, or overwrites a file with write access, based on an absolute path. | 1971 | /// Creates, opens, or overwrites a file with write access, based on an absolute path. |
| 1912 | /// Call `File.close` to release the resource. | 1972 | /// Call `File.close` to release the resource. |
| 1913 | /// Asserts that the path is absolute. See `Dir.createFile` for a function that | 1973 | /// Asserts that the path is absolute. See `Dir.createFile` for a function that |
| ... | @@ -2252,7 +2312,7 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 { | ... | @@ -2252,7 +2312,7 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 { |
| 2252 | const PATH = std.os.getenvZ("PATH") orelse return error.FileNotFound; | 2312 | const PATH = std.os.getenvZ("PATH") orelse return error.FileNotFound; |
| 2253 | var path_it = mem.tokenize(PATH, &[_]u8{path.delimiter}); | 2313 | var path_it = mem.tokenize(PATH, &[_]u8{path.delimiter}); |
| 2254 | while (path_it.next()) |a_path| { | 2314 | while (path_it.next()) |a_path| { |
| 2255 | var resolved_path_buf: [MAX_PATH_BYTES-1:0]u8 = undefined; | 2315 | var resolved_path_buf: [MAX_PATH_BYTES - 1:0]u8 = undefined; |
| 2256 | const resolved_path = std.fmt.bufPrintZ(&resolved_path_buf, "{s}/{s}", .{ | 2316 | const resolved_path = std.fmt.bufPrintZ(&resolved_path_buf, "{s}/{s}", .{ |
| 2257 | a_path, | 2317 | a_path, |
| 2258 | os.argv[0], | 2318 | os.argv[0], |