| ... | ... | @@ -467,55 +467,49 @@ pub fn resolve(allocator: Allocator, paths: []const []const u8) ![]u8 { |
| 467 | 467 | /// Path separators are canonicalized to '\\' and drives are canonicalized to capital letters. |
| 468 | 468 | /// Note: all usage of this function should be audited due to the existence of symlinks. |
| 469 | 469 | /// Without performing actual syscalls, resolving `..` could be incorrect. |
| 470 | /// This API may break in the future: https://github.com/ziglang/zig/issues/13613 |
| 470 | 471 | pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 { |
| 471 | | if (paths.len == 0) { |
| 472 | | assert(native_os == .windows); // resolveWindows called on non windows can't use getCwd |
| 473 | | return process.getCwdAlloc(allocator); |
| 474 | | } |
| 472 | assert(paths.len > 0); |
| 475 | 473 | |
| 476 | 474 | // determine which disk designator we will result with, if any |
| 477 | 475 | var result_drive_buf = "_:".*; |
| 478 | | var result_disk_designator: []const u8 = ""; |
| 479 | | var have_drive_kind = WindowsPath.Kind.None; |
| 476 | var disk_designator: []const u8 = ""; |
| 477 | var drive_kind = WindowsPath.Kind.None; |
| 480 | 478 | var have_abs_path = false; |
| 481 | 479 | var first_index: usize = 0; |
| 482 | | var max_size: usize = 0; |
| 483 | 480 | for (paths) |p, i| { |
| 484 | 481 | const parsed = windowsParsePath(p); |
| 485 | 482 | if (parsed.is_abs) { |
| 486 | 483 | have_abs_path = true; |
| 487 | 484 | first_index = i; |
| 488 | | max_size = result_disk_designator.len; |
| 489 | 485 | } |
| 490 | 486 | switch (parsed.kind) { |
| 491 | | WindowsPath.Kind.Drive => { |
| 487 | .Drive => { |
| 492 | 488 | result_drive_buf[0] = ascii.toUpper(parsed.disk_designator[0]); |
| 493 | | result_disk_designator = result_drive_buf[0..]; |
| 494 | | have_drive_kind = WindowsPath.Kind.Drive; |
| 489 | disk_designator = result_drive_buf[0..]; |
| 490 | drive_kind = WindowsPath.Kind.Drive; |
| 495 | 491 | }, |
| 496 | | WindowsPath.Kind.NetworkShare => { |
| 497 | | result_disk_designator = parsed.disk_designator; |
| 498 | | have_drive_kind = WindowsPath.Kind.NetworkShare; |
| 492 | .NetworkShare => { |
| 493 | disk_designator = parsed.disk_designator; |
| 494 | drive_kind = WindowsPath.Kind.NetworkShare; |
| 499 | 495 | }, |
| 500 | | WindowsPath.Kind.None => {}, |
| 496 | .None => {}, |
| 501 | 497 | } |
| 502 | | max_size += p.len + 1; |
| 503 | 498 | } |
| 504 | 499 | |
| 505 | 500 | // if we will result with a disk designator, loop again to determine |
| 506 | 501 | // which is the last time the disk designator is absolutely specified, if any |
| 507 | 502 | // and count up the max bytes for paths related to this disk designator |
| 508 | | if (have_drive_kind != WindowsPath.Kind.None) { |
| 503 | if (drive_kind != WindowsPath.Kind.None) { |
| 509 | 504 | have_abs_path = false; |
| 510 | 505 | first_index = 0; |
| 511 | | max_size = result_disk_designator.len; |
| 512 | 506 | var correct_disk_designator = false; |
| 513 | 507 | |
| 514 | 508 | for (paths) |p, i| { |
| 515 | 509 | const parsed = windowsParsePath(p); |
| 516 | 510 | if (parsed.kind != WindowsPath.Kind.None) { |
| 517 | | if (parsed.kind == have_drive_kind) { |
| 518 | | correct_disk_designator = compareDiskDesignators(have_drive_kind, result_disk_designator, parsed.disk_designator); |
| 511 | if (parsed.kind == drive_kind) { |
| 512 | correct_disk_designator = compareDiskDesignators(drive_kind, disk_designator, parsed.disk_designator); |
| 519 | 513 | } else { |
| 520 | 514 | continue; |
| 521 | 515 | } |
| ... | ... | @@ -525,92 +519,51 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 { |
| 525 | 519 | } |
| 526 | 520 | if (parsed.is_abs) { |
| 527 | 521 | first_index = i; |
| 528 | | max_size = result_disk_designator.len; |
| 529 | 522 | have_abs_path = true; |
| 530 | 523 | } |
| 531 | | max_size += p.len + 1; |
| 532 | 524 | } |
| 533 | 525 | } |
| 534 | 526 | |
| 535 | | // Allocate result and fill in the disk designator, calling getCwd if we have to. |
| 536 | | var result: []u8 = undefined; |
| 537 | | var result_index: usize = 0; |
| 538 | | |
| 539 | | if (have_abs_path) { |
| 540 | | switch (have_drive_kind) { |
| 541 | | WindowsPath.Kind.Drive => { |
| 542 | | result = try allocator.alloc(u8, max_size); |
| 527 | // Allocate result and fill in the disk designator. |
| 528 | var result = std.ArrayList(u8).init(allocator); |
| 529 | defer result.deinit(); |
| 543 | 530 | |
| 544 | | mem.copy(u8, result, result_disk_designator); |
| 545 | | result_index += result_disk_designator.len; |
| 531 | const disk_designator_len: usize = l: { |
| 532 | if (!have_abs_path) break :l 0; |
| 533 | switch (drive_kind) { |
| 534 | .Drive => { |
| 535 | try result.appendSlice(disk_designator); |
| 536 | break :l disk_designator.len; |
| 546 | 537 | }, |
| 547 | | WindowsPath.Kind.NetworkShare => { |
| 548 | | result = try allocator.alloc(u8, max_size); |
| 538 | .NetworkShare => { |
| 549 | 539 | var it = mem.tokenize(u8, paths[first_index], "/\\"); |
| 550 | 540 | const server_name = it.next().?; |
| 551 | 541 | const other_name = it.next().?; |
| 552 | 542 | |
| 553 | | result[result_index] = '\\'; |
| 554 | | result_index += 1; |
| 555 | | result[result_index] = '\\'; |
| 556 | | result_index += 1; |
| 557 | | mem.copy(u8, result[result_index..], server_name); |
| 558 | | result_index += server_name.len; |
| 559 | | result[result_index] = '\\'; |
| 560 | | result_index += 1; |
| 561 | | mem.copy(u8, result[result_index..], other_name); |
| 562 | | result_index += other_name.len; |
| 563 | | |
| 564 | | result_disk_designator = result[0..result_index]; |
| 543 | try result.ensureUnusedCapacity(2 + 1 + server_name.len + other_name.len); |
| 544 | result.appendSliceAssumeCapacity("\\\\"); |
| 545 | result.appendSliceAssumeCapacity(server_name); |
| 546 | result.appendAssumeCapacity('\\'); |
| 547 | result.appendSliceAssumeCapacity(other_name); |
| 548 | |
| 549 | break :l result.items.len; |
| 565 | 550 | }, |
| 566 | | WindowsPath.Kind.None => { |
| 567 | | assert(native_os == .windows); // resolveWindows called on non windows can't use getCwd |
| 568 | | const cwd = try process.getCwdAlloc(allocator); |
| 569 | | defer allocator.free(cwd); |
| 570 | | const parsed_cwd = windowsParsePath(cwd); |
| 571 | | result = try allocator.alloc(u8, max_size + parsed_cwd.disk_designator.len + 1); |
| 572 | | mem.copy(u8, result, parsed_cwd.disk_designator); |
| 573 | | result_index += parsed_cwd.disk_designator.len; |
| 574 | | result_disk_designator = result[0..parsed_cwd.disk_designator.len]; |
| 575 | | if (parsed_cwd.kind == WindowsPath.Kind.Drive) { |
| 576 | | result[0] = ascii.toUpper(result[0]); |
| 577 | | } |
| 578 | | have_drive_kind = parsed_cwd.kind; |
| 551 | .None => { |
| 552 | break :l 1; |
| 579 | 553 | }, |
| 580 | 554 | } |
| 581 | | } else { |
| 582 | | assert(native_os == .windows); // resolveWindows called on non windows can't use getCwd |
| 583 | | // TODO call get cwd for the result_disk_designator instead of the global one |
| 584 | | const cwd = try process.getCwdAlloc(allocator); |
| 585 | | defer allocator.free(cwd); |
| 586 | | |
| 587 | | result = try allocator.alloc(u8, max_size + cwd.len + 1); |
| 588 | | |
| 589 | | mem.copy(u8, result, cwd); |
| 590 | | result_index += cwd.len; |
| 591 | | const parsed_cwd = windowsParsePath(result[0..result_index]); |
| 592 | | result_disk_designator = parsed_cwd.disk_designator; |
| 593 | | if (parsed_cwd.kind == WindowsPath.Kind.Drive) { |
| 594 | | result[0] = ascii.toUpper(result[0]); |
| 595 | | // Remove the trailing slash if present, eg. if the cwd is a root |
| 596 | | // directory. |
| 597 | | if (cwd.len > 0 and cwd[cwd.len - 1] == sep_windows) { |
| 598 | | result_index -= 1; |
| 599 | | } |
| 600 | | } |
| 601 | | have_drive_kind = parsed_cwd.kind; |
| 602 | | } |
| 603 | | errdefer allocator.free(result); |
| 555 | }; |
| 604 | 556 | |
| 605 | | // Now we know the disk designator to use, if any, and what kind it is. And our result |
| 606 | | // is big enough to append all the paths to. |
| 607 | 557 | var correct_disk_designator = true; |
| 558 | var negative_count: usize = 0; |
| 559 | |
| 608 | 560 | for (paths[first_index..]) |p| { |
| 609 | 561 | const parsed = windowsParsePath(p); |
| 610 | 562 | |
| 611 | | if (parsed.kind != WindowsPath.Kind.None) { |
| 612 | | if (parsed.kind == have_drive_kind) { |
| 613 | | correct_disk_designator = compareDiskDesignators(have_drive_kind, result_disk_designator, parsed.disk_designator); |
| 563 | if (parsed.kind != .None) { |
| 564 | if (parsed.kind == drive_kind) { |
| 565 | const dd = result.items[0..disk_designator_len]; |
| 566 | correct_disk_designator = compareDiskDesignators(drive_kind, dd, parsed.disk_designator); |
| 614 | 567 | } else { |
| 615 | 568 | continue; |
| 616 | 569 | } |
| ... | ... | @@ -619,154 +572,167 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 { |
| 619 | 572 | continue; |
| 620 | 573 | } |
| 621 | 574 | var it = mem.tokenize(u8, p[parsed.disk_designator.len..], "/\\"); |
| 622 | | while (it.next()) |component| { |
| 575 | component: while (it.next()) |component| { |
| 623 | 576 | if (mem.eql(u8, component, ".")) { |
| 624 | 577 | continue; |
| 625 | 578 | } else if (mem.eql(u8, component, "..")) { |
| 626 | 579 | while (true) { |
| 627 | | if (result_index == 0 or result_index == result_disk_designator.len) |
| 628 | | break; |
| 629 | | result_index -= 1; |
| 630 | | if (result[result_index] == '\\' or result[result_index] == '/') |
| 580 | if (result.items.len == 0) { |
| 581 | negative_count += 1; |
| 582 | continue :component; |
| 583 | } |
| 584 | if (result.items.len == disk_designator_len) { |
| 631 | 585 | break; |
| 586 | } |
| 587 | const end_with_sep = switch (result.items[result.items.len - 1]) { |
| 588 | '\\', '/' => true, |
| 589 | else => false, |
| 590 | }; |
| 591 | result.items.len -= 1; |
| 592 | if (end_with_sep) break; |
| 632 | 593 | } |
| 594 | } else if (!have_abs_path and result.items.len == 0) { |
| 595 | try result.appendSlice(component); |
| 633 | 596 | } else { |
| 634 | | result[result_index] = sep_windows; |
| 635 | | result_index += 1; |
| 636 | | mem.copy(u8, result[result_index..], component); |
| 637 | | result_index += component.len; |
| 597 | try result.ensureUnusedCapacity(1 + component.len); |
| 598 | result.appendAssumeCapacity('\\'); |
| 599 | result.appendSliceAssumeCapacity(component); |
| 638 | 600 | } |
| 639 | 601 | } |
| 640 | 602 | } |
| 641 | 603 | |
| 642 | | if (result_index == result_disk_designator.len) { |
| 643 | | result[result_index] = '\\'; |
| 644 | | result_index += 1; |
| 604 | if (disk_designator_len != 0 and result.items.len == disk_designator_len) { |
| 605 | try result.append('\\'); |
| 606 | return result.toOwnedSlice(); |
| 607 | } |
| 608 | |
| 609 | if (result.items.len == 0) { |
| 610 | if (negative_count == 0) { |
| 611 | return allocator.dupe(u8, "."); |
| 612 | } else { |
| 613 | const real_result = try allocator.alloc(u8, 3 * negative_count - 1); |
| 614 | var count = negative_count - 1; |
| 615 | var i: usize = 0; |
| 616 | while (count > 0) : (count -= 1) { |
| 617 | real_result[i..][0..3].* = "..\\".*; |
| 618 | i += 3; |
| 619 | } |
| 620 | real_result[i..][0..2].* = "..".*; |
| 621 | return real_result; |
| 622 | } |
| 645 | 623 | } |
| 646 | 624 | |
| 647 | | return allocator.shrink(result, result_index); |
| 625 | if (negative_count == 0) { |
| 626 | return result.toOwnedSlice(); |
| 627 | } else { |
| 628 | const real_result = try allocator.alloc(u8, 3 * negative_count + result.items.len); |
| 629 | var count = negative_count; |
| 630 | var i: usize = 0; |
| 631 | while (count > 0) : (count -= 1) { |
| 632 | real_result[i..][0..3].* = "..\\".*; |
| 633 | i += 3; |
| 634 | } |
| 635 | mem.copy(u8, real_result[i..], result.items); |
| 636 | return real_result; |
| 637 | } |
| 648 | 638 | } |
| 649 | 639 | |
| 650 | 640 | /// This function is like a series of `cd` statements executed one after another. |
| 651 | 641 | /// It resolves "." and "..". |
| 652 | 642 | /// The result does not have a trailing path separator. |
| 653 | | /// If all paths are relative it uses the current working directory as a starting point. |
| 654 | | /// Note: all usage of this function should be audited due to the existence of symlinks. |
| 655 | | /// Without performing actual syscalls, resolving `..` could be incorrect. |
| 656 | | pub fn resolvePosix(allocator: Allocator, paths: []const []const u8) ![]u8 { |
| 657 | | if (paths.len == 0) { |
| 658 | | assert(native_os != .windows); // resolvePosix called on windows can't use getCwd |
| 659 | | return process.getCwdAlloc(allocator); |
| 660 | | } |
| 643 | /// This function does not perform any syscalls. Executing this series of path |
| 644 | /// lookups on the actual filesystem may produce different results due to |
| 645 | /// symlinks. |
| 646 | pub fn resolvePosix(allocator: Allocator, paths: []const []const u8) Allocator.Error![]u8 { |
| 647 | assert(paths.len > 0); |
| 661 | 648 | |
| 662 | | var first_index: usize = 0; |
| 663 | | var have_abs = false; |
| 664 | | var max_size: usize = 0; |
| 665 | | for (paths) |p, i| { |
| 666 | | if (isAbsolutePosix(p)) { |
| 667 | | first_index = i; |
| 668 | | have_abs = true; |
| 669 | | max_size = 0; |
| 670 | | } |
| 671 | | max_size += p.len + 1; |
| 672 | | } |
| 673 | | |
| 674 | | var result: []u8 = undefined; |
| 675 | | var result_index: usize = 0; |
| 649 | var result = std.ArrayList(u8).init(allocator); |
| 650 | defer result.deinit(); |
| 676 | 651 | |
| 677 | | if (have_abs) { |
| 678 | | result = try allocator.alloc(u8, max_size); |
| 679 | | } else { |
| 680 | | assert(native_os != .windows); // resolvePosix called on windows can't use getCwd |
| 681 | | const cwd = try process.getCwdAlloc(allocator); |
| 682 | | defer allocator.free(cwd); |
| 683 | | result = try allocator.alloc(u8, max_size + cwd.len + 1); |
| 684 | | mem.copy(u8, result, cwd); |
| 685 | | result_index += cwd.len; |
| 686 | | } |
| 687 | | errdefer allocator.free(result); |
| 652 | var negative_count: usize = 0; |
| 653 | var is_abs = false; |
| 688 | 654 | |
| 689 | | for (paths[first_index..]) |p| { |
| 655 | for (paths) |p| { |
| 656 | if (isAbsolutePosix(p)) { |
| 657 | is_abs = true; |
| 658 | negative_count = 0; |
| 659 | result.clearRetainingCapacity(); |
| 660 | } |
| 690 | 661 | var it = mem.tokenize(u8, p, "/"); |
| 691 | | while (it.next()) |component| { |
| 662 | component: while (it.next()) |component| { |
| 692 | 663 | if (mem.eql(u8, component, ".")) { |
| 693 | 664 | continue; |
| 694 | 665 | } else if (mem.eql(u8, component, "..")) { |
| 695 | 666 | while (true) { |
| 696 | | if (result_index == 0) |
| 697 | | break; |
| 698 | | result_index -= 1; |
| 699 | | if (result[result_index] == '/') |
| 700 | | break; |
| 667 | if (result.items.len == 0) { |
| 668 | negative_count += @boolToInt(!is_abs); |
| 669 | continue :component; |
| 670 | } |
| 671 | const ends_with_slash = result.items[result.items.len - 1] == '/'; |
| 672 | result.items.len -= 1; |
| 673 | if (ends_with_slash) break; |
| 701 | 674 | } |
| 675 | } else if (result.items.len > 0 or is_abs) { |
| 676 | try result.ensureUnusedCapacity(1 + component.len); |
| 677 | result.appendAssumeCapacity('/'); |
| 678 | result.appendSliceAssumeCapacity(component); |
| 702 | 679 | } else { |
| 703 | | result[result_index] = '/'; |
| 704 | | result_index += 1; |
| 705 | | mem.copy(u8, result[result_index..], component); |
| 706 | | result_index += component.len; |
| 680 | try result.appendSlice(component); |
| 707 | 681 | } |
| 708 | 682 | } |
| 709 | 683 | } |
| 710 | 684 | |
| 711 | | if (result_index == 0) { |
| 712 | | result[0] = '/'; |
| 713 | | result_index += 1; |
| 685 | if (result.items.len == 0) { |
| 686 | if (is_abs) { |
| 687 | return allocator.dupe(u8, "/"); |
| 688 | } |
| 689 | if (negative_count == 0) { |
| 690 | return allocator.dupe(u8, "."); |
| 691 | } else { |
| 692 | const real_result = try allocator.alloc(u8, 3 * negative_count - 1); |
| 693 | var count = negative_count - 1; |
| 694 | var i: usize = 0; |
| 695 | while (count > 0) : (count -= 1) { |
| 696 | real_result[i..][0..3].* = "../".*; |
| 697 | i += 3; |
| 698 | } |
| 699 | real_result[i..][0..2].* = "..".*; |
| 700 | return real_result; |
| 701 | } |
| 714 | 702 | } |
| 715 | 703 | |
| 716 | | return allocator.shrink(result, result_index); |
| 704 | if (negative_count == 0) { |
| 705 | return result.toOwnedSlice(); |
| 706 | } else { |
| 707 | const real_result = try allocator.alloc(u8, 3 * negative_count + result.items.len); |
| 708 | var count = negative_count; |
| 709 | var i: usize = 0; |
| 710 | while (count > 0) : (count -= 1) { |
| 711 | real_result[i..][0..3].* = "../".*; |
| 712 | i += 3; |
| 713 | } |
| 714 | mem.copy(u8, real_result[i..], result.items); |
| 715 | return real_result; |
| 716 | } |
| 717 | 717 | } |
| 718 | 718 | |
| 719 | 719 | test "resolve" { |
| 720 | | if (native_os == .wasi and builtin.link_libc) return error.SkipZigTest; |
| 721 | | if (native_os == .wasi and !builtin.link_libc) try os.initPreopensWasi(std.heap.page_allocator, "/"); |
| 720 | try testResolveWindows(&[_][]const u8{ "a\\b\\c\\", "..\\..\\.." }, ".."); |
| 721 | try testResolveWindows(&[_][]const u8{"."}, "."); |
| 722 | 722 | |
| 723 | | const cwd = try process.getCwdAlloc(testing.allocator); |
| 724 | | defer testing.allocator.free(cwd); |
| 725 | | if (native_os == .windows) { |
| 726 | | if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) { |
| 727 | | cwd[0] = ascii.toUpper(cwd[0]); |
| 728 | | } |
| 729 | | try testResolveWindows(&[_][]const u8{"."}, cwd); |
| 730 | | } else { |
| 731 | | try testResolvePosix(&[_][]const u8{ "a/b/c/", "../../.." }, cwd); |
| 732 | | try testResolvePosix(&[_][]const u8{"."}, cwd); |
| 733 | | } |
| 723 | try testResolvePosix(&[_][]const u8{ "a/b/c/", "../../.." }, ".."); |
| 724 | try testResolvePosix(&[_][]const u8{"."}, "."); |
| 734 | 725 | } |
| 735 | 726 | |
| 736 | 727 | test "resolveWindows" { |
| 737 | | if (builtin.target.cpu.arch == .aarch64) { |
| 738 | | // TODO https://github.com/ziglang/zig/issues/3288 |
| 739 | | return error.SkipZigTest; |
| 740 | | } |
| 741 | | if (native_os == .wasi and builtin.link_libc) return error.SkipZigTest; |
| 742 | | if (native_os == .wasi and !builtin.link_libc) try os.initPreopensWasi(std.heap.page_allocator, "/"); |
| 743 | | if (native_os == .windows) { |
| 744 | | const cwd = try process.getCwdAlloc(testing.allocator); |
| 745 | | defer testing.allocator.free(cwd); |
| 746 | | const parsed_cwd = windowsParsePath(cwd); |
| 747 | | { |
| 748 | | const expected = try join(testing.allocator, &[_][]const u8{ |
| 749 | | parsed_cwd.disk_designator, |
| 750 | | "usr\\local\\lib\\zig\\std\\array_list.zig", |
| 751 | | }); |
| 752 | | defer testing.allocator.free(expected); |
| 753 | | if (parsed_cwd.kind == WindowsPath.Kind.Drive) { |
| 754 | | expected[0] = ascii.toUpper(parsed_cwd.disk_designator[0]); |
| 755 | | } |
| 756 | | try testResolveWindows(&[_][]const u8{ "/usr/local", "lib\\zig\\std\\array_list.zig" }, expected); |
| 757 | | } |
| 758 | | { |
| 759 | | const expected = try join(testing.allocator, &[_][]const u8{ |
| 760 | | cwd, |
| 761 | | "usr\\local\\lib\\zig", |
| 762 | | }); |
| 763 | | defer testing.allocator.free(expected); |
| 764 | | if (parsed_cwd.kind == WindowsPath.Kind.Drive) { |
| 765 | | expected[0] = ascii.toUpper(parsed_cwd.disk_designator[0]); |
| 766 | | } |
| 767 | | try testResolveWindows(&[_][]const u8{ "usr/local", "lib\\zig" }, expected); |
| 768 | | } |
| 769 | | } |
| 728 | try testResolveWindows( |
| 729 | &[_][]const u8{ "Z:\\", "/usr/local", "lib\\zig\\std\\array_list.zig" }, |
| 730 | "Z:\\usr\\local\\lib\\zig\\std\\array_list.zig", |
| 731 | ); |
| 732 | try testResolveWindows( |
| 733 | &[_][]const u8{ "z:\\", "usr/local", "lib\\zig" }, |
| 734 | "Z:\\usr\\local\\lib\\zig", |
| 735 | ); |
| 770 | 736 | |
| 771 | 737 | try testResolveWindows(&[_][]const u8{ "c:\\a\\b\\c", "/hi", "ok" }, "C:\\hi\\ok"); |
| 772 | 738 | try testResolveWindows(&[_][]const u8{ "c:/blah\\blah", "d:/games", "c:../a" }, "C:\\blah\\a"); |
| ... | ... | @@ -781,12 +747,12 @@ test "resolveWindows" { |
| 781 | 747 | try testResolveWindows(&[_][]const u8{ "c:/", "//server//share" }, "\\\\server\\share\\"); |
| 782 | 748 | try testResolveWindows(&[_][]const u8{ "c:/", "///some//dir" }, "C:\\some\\dir"); |
| 783 | 749 | try testResolveWindows(&[_][]const u8{ "C:\\foo\\tmp.3\\", "..\\tmp.3\\cycles\\root.js" }, "C:\\foo\\tmp.3\\cycles\\root.js"); |
| 750 | |
| 751 | // Keep relative paths relative. |
| 752 | try testResolveWindows(&[_][]const u8{"a/b"}, "a\\b"); |
| 784 | 753 | } |
| 785 | 754 | |
| 786 | 755 | test "resolvePosix" { |
| 787 | | if (native_os == .wasi and builtin.link_libc) return error.SkipZigTest; |
| 788 | | if (native_os == .wasi and !builtin.link_libc) try os.initPreopensWasi(std.heap.page_allocator, "/"); |
| 789 | | |
| 790 | 756 | try testResolvePosix(&[_][]const u8{ "/a/b", "c" }, "/a/b/c"); |
| 791 | 757 | try testResolvePosix(&[_][]const u8{ "/a/b", "c", "//d", "e///" }, "/d/e"); |
| 792 | 758 | try testResolvePosix(&[_][]const u8{ "/a/b/c", "..", "../" }, "/a"); |
| ... | ... | @@ -797,18 +763,21 @@ test "resolvePosix" { |
| 797 | 763 | try testResolvePosix(&[_][]const u8{ "/var/lib", "/../", "file/" }, "/file"); |
| 798 | 764 | try testResolvePosix(&[_][]const u8{ "/some/dir", ".", "/absolute/" }, "/absolute"); |
| 799 | 765 | try testResolvePosix(&[_][]const u8{ "/foo/tmp.3/", "../tmp.3/cycles/root.js" }, "/foo/tmp.3/cycles/root.js"); |
| 766 | |
| 767 | // Keep relative paths relative. |
| 768 | try testResolvePosix(&[_][]const u8{"a/b"}, "a/b"); |
| 800 | 769 | } |
| 801 | 770 | |
| 802 | 771 | fn testResolveWindows(paths: []const []const u8, expected: []const u8) !void { |
| 803 | 772 | const actual = try resolveWindows(testing.allocator, paths); |
| 804 | 773 | defer testing.allocator.free(actual); |
| 805 | | try testing.expect(mem.eql(u8, actual, expected)); |
| 774 | try testing.expectEqualStrings(expected, actual); |
| 806 | 775 | } |
| 807 | 776 | |
| 808 | 777 | fn testResolvePosix(paths: []const []const u8, expected: []const u8) !void { |
| 809 | 778 | const actual = try resolvePosix(testing.allocator, paths); |
| 810 | 779 | defer testing.allocator.free(actual); |
| 811 | | try testing.expect(mem.eql(u8, actual, expected)); |
| 780 | try testing.expectEqualStrings(expected, actual); |
| 812 | 781 | } |
| 813 | 782 | |
| 814 | 783 | /// Strip the last component from a file path. |
| ... | ... | @@ -1089,13 +1058,15 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) ! |
| 1089 | 1058 | if (parsed_from.kind != parsed_to.kind) { |
| 1090 | 1059 | break :x true; |
| 1091 | 1060 | } else switch (parsed_from.kind) { |
| 1092 | | WindowsPath.Kind.NetworkShare => { |
| 1061 | .NetworkShare => { |
| 1093 | 1062 | break :x !networkShareServersEql(parsed_to.disk_designator, parsed_from.disk_designator); |
| 1094 | 1063 | }, |
| 1095 | | WindowsPath.Kind.Drive => { |
| 1064 | .Drive => { |
| 1096 | 1065 | break :x ascii.toUpper(parsed_from.disk_designator[0]) != ascii.toUpper(parsed_to.disk_designator[0]); |
| 1097 | 1066 | }, |
| 1098 | | else => unreachable, |
| 1067 | .None => { |
| 1068 | break :x false; |
| 1069 | }, |
| 1099 | 1070 | } |
| 1100 | 1071 | }; |
| 1101 | 1072 | |
| ... | ... | @@ -1194,13 +1165,6 @@ pub fn relativePosix(allocator: Allocator, from: []const u8, to: []const u8) ![] |
| 1194 | 1165 | } |
| 1195 | 1166 | |
| 1196 | 1167 | test "relative" { |
| 1197 | | if (builtin.target.cpu.arch == .aarch64) { |
| 1198 | | // TODO https://github.com/ziglang/zig/issues/3288 |
| 1199 | | return error.SkipZigTest; |
| 1200 | | } |
| 1201 | | if (native_os == .wasi and builtin.link_libc) return error.SkipZigTest; |
| 1202 | | if (native_os == .wasi and !builtin.link_libc) try os.initPreopensWasi(std.heap.page_allocator, "/"); |
| 1203 | | |
| 1204 | 1168 | try testRelativeWindows("c:/blah\\blah", "d:/games", "D:\\games"); |
| 1205 | 1169 | try testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa", ".."); |
| 1206 | 1170 | try testRelativeWindows("c:/aaaa/bbbb", "c:/cccc", "..\\..\\cccc"); |
| ... | ... | @@ -1226,6 +1190,10 @@ test "relative" { |
| 1226 | 1190 | try testRelativeWindows("C:\\baz", "\\\\foo\\bar\\baz", "\\\\foo\\bar\\baz"); |
| 1227 | 1191 | try testRelativeWindows("\\\\foo\\bar\\baz", "C:\\baz", "C:\\baz"); |
| 1228 | 1192 | |
| 1193 | try testRelativeWindows("a/b/c", "a\\b", ".."); |
| 1194 | try testRelativeWindows("a/b/c", "a", "..\\.."); |
| 1195 | try testRelativeWindows("a/b/c", "a\\b\\c\\d", "d"); |
| 1196 | |
| 1229 | 1197 | try testRelativePosix("/var/lib", "/var", ".."); |
| 1230 | 1198 | try testRelativePosix("/var/lib", "/bin", "../../bin"); |
| 1231 | 1199 | try testRelativePosix("/var/lib", "/var/lib", ""); |
| ... | ... | @@ -1243,13 +1211,13 @@ test "relative" { |
| 1243 | 1211 | fn testRelativePosix(from: []const u8, to: []const u8, expected_output: []const u8) !void { |
| 1244 | 1212 | const result = try relativePosix(testing.allocator, from, to); |
| 1245 | 1213 | defer testing.allocator.free(result); |
| 1246 | | try testing.expectEqualSlices(u8, expected_output, result); |
| 1214 | try testing.expectEqualStrings(expected_output, result); |
| 1247 | 1215 | } |
| 1248 | 1216 | |
| 1249 | 1217 | fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []const u8) !void { |
| 1250 | 1218 | const result = try relativeWindows(testing.allocator, from, to); |
| 1251 | 1219 | defer testing.allocator.free(result); |
| 1252 | | try testing.expectEqualSlices(u8, expected_output, result); |
| 1220 | try testing.expectEqualStrings(expected_output, result); |
| 1253 | 1221 | } |
| 1254 | 1222 | |
| 1255 | 1223 | /// Returns the extension of the file name (if any). |