| author | |
| committer | |
| log | 6943cefebf94631ff02d6e28436c07f4030924c6 |
| tree | 0f74832cd4549b4852299639cb2fb90fdb66a4cc |
| parent | 2219cc061229bdc69314616869ad731b592d9e96 |
for when there is no directory component. Makes it harder
to write bugs.
closes #10175 files changed, 51 insertions(+), 35 deletions(-)
src-self-hosted/introspect.zig+1-1| ... | ... | @@ -27,7 +27,7 @@ pub fn findZigLibDir(allocator: *mem.Allocator) ![]u8 { |
| 27 | 27 | |
| 28 | 28 | var cur_path: []const u8 = self_exe_path; |
| 29 | 29 | while (true) { |
| 30 | const test_dir = os.path.dirname(cur_path); | |
| 30 | const test_dir = os.path.dirname(cur_path) orelse "."; | |
| 31 | 31 | |
| 32 | 32 | if (mem.eql(u8, test_dir, cur_path)) { |
| 33 | 33 | break; |
src-self-hosted/main.zig+1-1| ... | ... | @@ -249,7 +249,7 @@ fn cmdBuild(allocator: *Allocator, args: []const []const u8) !void { |
| 249 | 249 | defer build_args.deinit(); |
| 250 | 250 | |
| 251 | 251 | const build_file_basename = os.path.basename(build_file_abs); |
| 252 | const build_file_dirname = os.path.dirname(build_file_abs); | |
| 252 | const build_file_dirname = os.path.dirname(build_file_abs) orelse "."; | |
| 253 | 253 | |
| 254 | 254 | var full_cache_dir: []u8 = undefined; |
| 255 | 255 | if (flags.single("cache-dir")) |cache_dir| { |
std/build.zig+9-7| ... | ... | @@ -617,7 +617,7 @@ pub const Builder = struct { |
| 617 | 617 | warn("cp {} {}\n", source_path, dest_path); |
| 618 | 618 | } |
| 619 | 619 | |
| 620 | const dirname = os.path.dirname(dest_path); | |
| 620 | const dirname = os.path.dirname(dest_path) orelse "."; | |
| 621 | 621 | const abs_source_path = self.pathFromRoot(source_path); |
| 622 | 622 | os.makePath(self.allocator, dirname) catch |err| { |
| 623 | 623 | warn("Unable to create path {}: {}\n", dirname, @errorName(err)); |
| ... | ... | @@ -1395,8 +1395,9 @@ pub const LibExeObjStep = struct { |
| 1395 | 1395 | cc_args.append(abs_source_file) catch unreachable; |
| 1396 | 1396 | |
| 1397 | 1397 | const cache_o_src = os.path.join(builder.allocator, builder.cache_root, source_file) catch unreachable; |
| 1398 | const cache_o_dir = os.path.dirname(cache_o_src); | |
| 1399 | try builder.makePath(cache_o_dir); | |
| 1398 | if (os.path.dirname(cache_o_src)) |cache_o_dir| { | |
| 1399 | try builder.makePath(cache_o_dir); | |
| 1400 | } | |
| 1400 | 1401 | const cache_o_file = builder.fmt("{}{}", cache_o_src, self.target.oFileExt()); |
| 1401 | 1402 | cc_args.append("-o") catch unreachable; |
| 1402 | 1403 | cc_args.append(builder.pathFromRoot(cache_o_file)) catch unreachable; |
| ... | ... | @@ -1509,8 +1510,9 @@ pub const LibExeObjStep = struct { |
| 1509 | 1510 | cc_args.append(abs_source_file) catch unreachable; |
| 1510 | 1511 | |
| 1511 | 1512 | const cache_o_src = os.path.join(builder.allocator, builder.cache_root, source_file) catch unreachable; |
| 1512 | const cache_o_dir = os.path.dirname(cache_o_src); | |
| 1513 | try builder.makePath(cache_o_dir); | |
| 1513 | if (os.path.dirname(cache_o_src)) |cache_o_dir| { | |
| 1514 | try builder.makePath(cache_o_dir); | |
| 1515 | } | |
| 1514 | 1516 | const cache_o_file = builder.fmt("{}{}", cache_o_src, self.target.oFileExt()); |
| 1515 | 1517 | cc_args.append("-o") catch unreachable; |
| 1516 | 1518 | cc_args.append(builder.pathFromRoot(cache_o_file)) catch unreachable; |
| ... | ... | @@ -1855,7 +1857,7 @@ pub const WriteFileStep = struct { |
| 1855 | 1857 | fn make(step: *Step) !void { |
| 1856 | 1858 | const self = @fieldParentPtr(WriteFileStep, "step", step); |
| 1857 | 1859 | const full_path = self.builder.pathFromRoot(self.file_path); |
| 1858 | const full_path_dir = os.path.dirname(full_path); | |
| 1860 | const full_path_dir = os.path.dirname(full_path) orelse "."; | |
| 1859 | 1861 | os.makePath(self.builder.allocator, full_path_dir) catch |err| { |
| 1860 | 1862 | warn("unable to make path {}: {}\n", full_path_dir, @errorName(err)); |
| 1861 | 1863 | return err; |
| ... | ... | @@ -1945,7 +1947,7 @@ pub const Step = struct { |
| 1945 | 1947 | }; |
| 1946 | 1948 | |
| 1947 | 1949 | fn doAtomicSymLinks(allocator: *Allocator, output_path: []const u8, filename_major_only: []const u8, filename_name_only: []const u8) !void { |
| 1948 | const out_dir = os.path.dirname(output_path); | |
| 1950 | const out_dir = os.path.dirname(output_path) orelse "."; | |
| 1949 | 1951 | const out_basename = os.path.basename(output_path); |
| 1950 | 1952 | // sym link for libfoo.so.1 to libfoo.so.1.2.3 |
| 1951 | 1953 | const major_only_path = os.path.join(allocator, out_dir, filename_major_only) catch unreachable; |
std/os/index.zig+6-6| ... | ... | @@ -714,7 +714,7 @@ pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path: |
| 714 | 714 | else => return err, // TODO zig should know this set does not include PathAlreadyExists |
| 715 | 715 | } |
| 716 | 716 | |
| 717 | const dirname = os.path.dirname(new_path); | |
| 717 | const dirname = os.path.dirname(new_path) orelse "."; | |
| 718 | 718 | |
| 719 | 719 | var rand_buf: [12]u8 = undefined; |
| 720 | 720 | const tmp_path = try allocator.alloc(u8, dirname.len + 1 + base64.Base64Encoder.calcSize(rand_buf.len)); |
| ... | ... | @@ -860,14 +860,14 @@ pub const AtomicFile = struct { |
| 860 | 860 | |
| 861 | 861 | var rand_buf: [12]u8 = undefined; |
| 862 | 862 | |
| 863 | const dirname_component_len = if (dirname.len == 0) 0 else dirname.len + 1; | |
| 863 | const dirname_component_len = if (dirname) |d| d.len + 1 else 0; | |
| 864 | 864 | const tmp_path = try allocator.alloc(u8, dirname_component_len + |
| 865 | 865 | base64.Base64Encoder.calcSize(rand_buf.len)); |
| 866 | 866 | errdefer allocator.free(tmp_path); |
| 867 | 867 | |
| 868 | if (dirname.len != 0) { | |
| 869 | mem.copy(u8, tmp_path[0..], dirname); | |
| 870 | tmp_path[dirname.len] = os.path.sep; | |
| 868 | if (dirname) |dir| { | |
| 869 | mem.copy(u8, tmp_path[0..], dir); | |
| 870 | tmp_path[dir.len] = os.path.sep; | |
| 871 | 871 | } |
| 872 | 872 | |
| 873 | 873 | while (true) { |
| ... | ... | @@ -1965,7 +1965,7 @@ pub fn selfExeDirPath(allocator: *mem.Allocator) ![]u8 { |
| 1965 | 1965 | // the executable was in when it was run. |
| 1966 | 1966 | const full_exe_path = try readLink(allocator, "/proc/self/exe"); |
| 1967 | 1967 | errdefer allocator.free(full_exe_path); |
| 1968 | const dir = path.dirname(full_exe_path); | |
| 1968 | const dir = path.dirname(full_exe_path) orelse "."; | |
| 1969 | 1969 | return allocator.shrink(u8, full_exe_path, dir.len); |
| 1970 | 1970 | }, |
| 1971 | 1971 | Os.windows, Os.macosx, Os.ios => { |
std/os/path.zig+34-20| ... | ... | @@ -648,8 +648,8 @@ fn testResolvePosix(paths: []const []const u8) []u8 { |
| 648 | 648 | } |
| 649 | 649 | |
| 650 | 650 | /// If the path is a file in the current directory (no directory component) |
| 651 | /// then the returned slice has .len = 0. | |
| 652 | pub fn dirname(path: []const u8) []const u8 { | |
| 651 | /// then returns null | |
| 652 | pub fn dirname(path: []const u8) ?[]const u8 { | |
| 653 | 653 | if (is_windows) { |
| 654 | 654 | return dirnameWindows(path); |
| 655 | 655 | } else { |
| ... | ... | @@ -657,9 +657,9 @@ pub fn dirname(path: []const u8) []const u8 { |
| 657 | 657 | } |
| 658 | 658 | } |
| 659 | 659 | |
| 660 | pub fn dirnameWindows(path: []const u8) []const u8 { | |
| 660 | pub fn dirnameWindows(path: []const u8) ?[]const u8 { | |
| 661 | 661 | if (path.len == 0) |
| 662 | return path[0..0]; | |
| 662 | return null; | |
| 663 | 663 | |
| 664 | 664 | const root_slice = diskDesignatorWindows(path); |
| 665 | 665 | if (path.len == root_slice.len) |
| ... | ... | @@ -671,13 +671,13 @@ pub fn dirnameWindows(path: []const u8) []const u8 { |
| 671 | 671 | |
| 672 | 672 | while ((path[end_index] == '/' or path[end_index] == '\\') and end_index > root_slice.len) { |
| 673 | 673 | if (end_index == 0) |
| 674 | return path[0..0]; | |
| 674 | return null; | |
| 675 | 675 | end_index -= 1; |
| 676 | 676 | } |
| 677 | 677 | |
| 678 | 678 | while (path[end_index] != '/' and path[end_index] != '\\' and end_index > root_slice.len) { |
| 679 | 679 | if (end_index == 0) |
| 680 | return path[0..0]; | |
| 680 | return null; | |
| 681 | 681 | end_index -= 1; |
| 682 | 682 | } |
| 683 | 683 | |
| ... | ... | @@ -685,12 +685,15 @@ pub fn dirnameWindows(path: []const u8) []const u8 { |
| 685 | 685 | end_index += 1; |
| 686 | 686 | } |
| 687 | 687 | |
| 688 | if (end_index == 0) | |
| 689 | return null; | |
| 690 | ||
| 688 | 691 | return path[0..end_index]; |
| 689 | 692 | } |
| 690 | 693 | |
| 691 | pub fn dirnamePosix(path: []const u8) []const u8 { | |
| 694 | pub fn dirnamePosix(path: []const u8) ?[]const u8 { | |
| 692 | 695 | if (path.len == 0) |
| 693 | return path[0..0]; | |
| 696 | return null; | |
| 694 | 697 | |
| 695 | 698 | var end_index: usize = path.len - 1; |
| 696 | 699 | while (path[end_index] == '/') { |
| ... | ... | @@ -701,13 +704,16 @@ pub fn dirnamePosix(path: []const u8) []const u8 { |
| 701 | 704 | |
| 702 | 705 | while (path[end_index] != '/') { |
| 703 | 706 | if (end_index == 0) |
| 704 | return path[0..0]; | |
| 707 | return null; | |
| 705 | 708 | end_index -= 1; |
| 706 | 709 | } |
| 707 | 710 | |
| 708 | 711 | if (end_index == 0 and path[end_index] == '/') |
| 709 | 712 | return path[0..1]; |
| 710 | 713 | |
| 714 | if (end_index == 0) | |
| 715 | return null; | |
| 716 | ||
| 711 | 717 | return path[0..end_index]; |
| 712 | 718 | } |
| 713 | 719 | |
| ... | ... | @@ -717,10 +723,10 @@ test "os.path.dirnamePosix" { |
| 717 | 723 | testDirnamePosix("/a", "/"); |
| 718 | 724 | testDirnamePosix("/", "/"); |
| 719 | 725 | testDirnamePosix("////", "/"); |
| 720 | testDirnamePosix("", ""); | |
| 721 | testDirnamePosix("a", ""); | |
| 722 | testDirnamePosix("a/", ""); | |
| 723 | testDirnamePosix("a//", ""); | |
| 726 | testDirnamePosix("", null); | |
| 727 | testDirnamePosix("a", null); | |
| 728 | testDirnamePosix("a/", null); | |
| 729 | testDirnamePosix("a//", null); | |
| 724 | 730 | } |
| 725 | 731 | |
| 726 | 732 | test "os.path.dirnameWindows" { |
| ... | ... | @@ -742,7 +748,7 @@ test "os.path.dirnameWindows" { |
| 742 | 748 | testDirnameWindows("c:foo\\bar", "c:foo"); |
| 743 | 749 | testDirnameWindows("c:foo\\bar\\", "c:foo"); |
| 744 | 750 | testDirnameWindows("c:foo\\bar\\baz", "c:foo\\bar"); |
| 745 | testDirnameWindows("file:stream", ""); | |
| 751 | testDirnameWindows("file:stream", null); | |
| 746 | 752 | testDirnameWindows("dir\\file:stream", "dir"); |
| 747 | 753 | testDirnameWindows("\\\\unc\\share", "\\\\unc\\share"); |
| 748 | 754 | testDirnameWindows("\\\\unc\\share\\foo", "\\\\unc\\share\\"); |
| ... | ... | @@ -753,18 +759,26 @@ test "os.path.dirnameWindows" { |
| 753 | 759 | testDirnameWindows("/a/b/", "/a"); |
| 754 | 760 | testDirnameWindows("/a/b", "/a"); |
| 755 | 761 | testDirnameWindows("/a", "/"); |
| 756 | testDirnameWindows("", ""); | |
| 762 | testDirnameWindows("", null); | |
| 757 | 763 | testDirnameWindows("/", "/"); |
| 758 | 764 | testDirnameWindows("////", "/"); |
| 759 | testDirnameWindows("foo", ""); | |
| 765 | testDirnameWindows("foo", null); | |
| 760 | 766 | } |
| 761 | 767 | |
| 762 | fn testDirnamePosix(input: []const u8, expected_output: []const u8) void { | |
| 763 | assert(mem.eql(u8, dirnamePosix(input), expected_output)); | |
| 768 | fn testDirnamePosix(input: []const u8, expected_output: ?[]const u8) void { | |
| 769 | if (dirnamePosix(input)) |output| { | |
| 770 | assert(mem.eql(u8, output, expected_output.?)); | |
| 771 | } else { | |
| 772 | assert(expected_output == null); | |
| 773 | } | |
| 764 | 774 | } |
| 765 | 775 | |
| 766 | fn testDirnameWindows(input: []const u8, expected_output: []const u8) void { | |
| 767 | assert(mem.eql(u8, dirnameWindows(input), expected_output)); | |
| 776 | fn testDirnameWindows(input: []const u8, expected_output: ?[]const u8) void { | |
| 777 | if (dirnameWindows(input)) |output| { | |
| 778 | assert(mem.eql(u8, output, expected_output.?)); | |
| 779 | } else { | |
| 780 | assert(expected_output == null); | |
| 781 | } | |
| 768 | 782 | } |
| 769 | 783 | |
| 770 | 784 | pub fn basename(path: []const u8) []const u8 { |