| ... | @@ -20,10 +20,7 @@ const testing = std.testing; | ... | @@ -20,10 +20,7 @@ const testing = std.testing; |
| 20 | const mem = std.mem; | 20 | const mem = std.mem; |
| 21 | const ascii = std.ascii; | 21 | const ascii = std.ascii; |
| 22 | const Allocator = mem.Allocator; | 22 | const Allocator = mem.Allocator; |
| 23 | const math = std.math; | | |
| 24 | const windows = std.os.windows; | 23 | const windows = std.os.windows; |
| 25 | const os = std.os; | | |
| 26 | const fs = std.fs; | | |
| 27 | const process = std.process; | 24 | const process = std.process; |
| 28 | const native_os = builtin.target.os.tag; | 25 | const native_os = builtin.target.os.tag; |
| 29 | | 26 | |
| ... | @@ -60,11 +57,12 @@ pub const PathType = enum { | ... | @@ -60,11 +57,12 @@ pub const PathType = enum { |
| 60 | posix, | 57 | posix, |
| 61 | | 58 | |
| 62 | /// Returns true if `c` is a valid path separator for the `path_type`. | 59 | /// Returns true if `c` is a valid path separator for the `path_type`. |
| | 60 | /// If `T` is `u16`, `c` is assumed to be little-endian. |
| 63 | pub inline fn isSep(comptime path_type: PathType, comptime T: type, c: T) bool { | 61 | pub inline fn isSep(comptime path_type: PathType, comptime T: type, c: T) bool { |
| 64 | return switch (path_type) { | 62 | return switch (path_type) { |
| 65 | .windows => c == '/' or c == '\\', | 63 | .windows => c == mem.nativeToLittle(T, '/') or c == mem.nativeToLittle(T, '\\'), |
| 66 | .posix => c == '/', | 64 | .posix => c == mem.nativeToLittle(T, '/'), |
| 67 | .uefi => c == '\\', | 65 | .uefi => c == mem.nativeToLittle(T, '\\'), |
| 68 | }; | 66 | }; |
| 69 | } | 67 | } |
| 70 | }; | 68 | }; |
| ... | @@ -221,7 +219,7 @@ test join { | ... | @@ -221,7 +219,7 @@ test join { |
| 221 | try testJoinMaybeZWindows(&[_][]const u8{}, "", zero); | 219 | try testJoinMaybeZWindows(&[_][]const u8{}, "", zero); |
| 222 | try testJoinMaybeZWindows(&[_][]const u8{ "c:\\a\\b", "c" }, "c:\\a\\b\\c", zero); | 220 | try testJoinMaybeZWindows(&[_][]const u8{ "c:\\a\\b", "c" }, "c:\\a\\b\\c", zero); |
| 223 | try testJoinMaybeZWindows(&[_][]const u8{ "c:\\a\\b", "c" }, "c:\\a\\b\\c", zero); | 221 | try testJoinMaybeZWindows(&[_][]const u8{ "c:\\a\\b", "c" }, "c:\\a\\b\\c", zero); |
| 224 | try testJoinMaybeZWindows(&[_][]const u8{ "c:\\a\\b\\", "c" }, "c:\\a\\b\\c", zero); | 222 | try testJoinMaybeZWindows(&[_][]const u8{ "c:\\a\\b\\", "\\c" }, "c:\\a\\b\\c", zero); |
| 225 | | 223 | |
| 226 | try testJoinMaybeZWindows(&[_][]const u8{ "c:\\", "a", "b\\", "c" }, "c:\\a\\b\\c", zero); | 224 | try testJoinMaybeZWindows(&[_][]const u8{ "c:\\", "a", "b\\", "c" }, "c:\\a\\b\\c", zero); |
| 227 | try testJoinMaybeZWindows(&[_][]const u8{ "c:\\a\\", "b\\", "c" }, "c:\\a\\b\\c", zero); | 225 | try testJoinMaybeZWindows(&[_][]const u8{ "c:\\a\\", "b\\", "c" }, "c:\\a\\b\\c", zero); |
| ... | @@ -283,26 +281,16 @@ pub fn isAbsolute(path: []const u8) bool { | ... | @@ -283,26 +281,16 @@ pub fn isAbsolute(path: []const u8) bool { |
| 283 | } | 281 | } |
| 284 | | 282 | |
| 285 | fn isAbsoluteWindowsImpl(comptime T: type, path: []const T) bool { | 283 | fn isAbsoluteWindowsImpl(comptime T: type, path: []const T) bool { |
| 286 | if (path.len < 1) | 284 | return switch (windows.getWin32PathType(T, path)) { |
| 287 | return false; | 285 | // Unambiguously absolute |
| 288 | | 286 | .drive_absolute, .unc_absolute, .local_device, .root_local_device => true, |
| 289 | if (path[0] == '/') | 287 | // Unambiguously relative |
| 290 | return true; | 288 | .relative => false, |
| 291 | | 289 | // Ambiguous, more absolute than relative |
| 292 | if (path[0] == '\\') | 290 | .rooted => true, |
| 293 | return true; | 291 | // Ambiguous, more relative than absolute |
| 294 | | 292 | .drive_relative => false, |
| 295 | if (path.len < 3) | 293 | }; |
| 296 | return false; | | |
| 297 | | | |
| 298 | if (path[1] == ':') { | | |
| 299 | if (path[2] == '/') | | |
| 300 | return true; | | |
| 301 | if (path[2] == '\\') | | |
| 302 | return true; | | |
| 303 | } | | |
| 304 | | | |
| 305 | return false; | | |
| 306 | } | 294 | } |
| 307 | | 295 | |
| 308 | pub fn isAbsoluteWindows(path: []const u8) bool { | 296 | pub fn isAbsoluteWindows(path: []const u8) bool { |
| ... | @@ -347,6 +335,9 @@ test isAbsoluteWindows { | ... | @@ -347,6 +335,9 @@ test isAbsoluteWindows { |
| 347 | try testIsAbsoluteWindows("C:\\Users\\", true); | 335 | try testIsAbsoluteWindows("C:\\Users\\", true); |
| 348 | try testIsAbsoluteWindows("C:cwd/another", false); | 336 | try testIsAbsoluteWindows("C:cwd/another", false); |
| 349 | try testIsAbsoluteWindows("C:cwd\\another", false); | 337 | try testIsAbsoluteWindows("C:cwd\\another", false); |
| | 338 | try testIsAbsoluteWindows("λ:\\", true); |
| | 339 | try testIsAbsoluteWindows("λ:", false); |
| | 340 | try testIsAbsoluteWindows("\u{10000}:\\", false); |
| 350 | try testIsAbsoluteWindows("directory/directory", false); | 341 | try testIsAbsoluteWindows("directory/directory", false); |
| 351 | try testIsAbsoluteWindows("directory\\directory", false); | 342 | try testIsAbsoluteWindows("directory\\directory", false); |
| 352 | try testIsAbsoluteWindows("/usr/local", true); | 343 | try testIsAbsoluteWindows("/usr/local", true); |
| ... | @@ -362,12 +353,17 @@ test isAbsolutePosix { | ... | @@ -362,12 +353,17 @@ test isAbsolutePosix { |
| 362 | | 353 | |
| 363 | fn testIsAbsoluteWindows(path: []const u8, expected_result: bool) !void { | 354 | fn testIsAbsoluteWindows(path: []const u8, expected_result: bool) !void { |
| 364 | try testing.expectEqual(expected_result, isAbsoluteWindows(path)); | 355 | try testing.expectEqual(expected_result, isAbsoluteWindows(path)); |
| | 356 | const path_w = try std.unicode.wtf8ToWtf16LeAllocZ(std.testing.allocator, path); |
| | 357 | defer std.testing.allocator.free(path_w); |
| | 358 | try testing.expectEqual(expected_result, isAbsoluteWindowsW(path_w)); |
| | 359 | try testing.expectEqual(expected_result, isAbsoluteWindowsWtf16(path_w)); |
| 365 | } | 360 | } |
| 366 | | 361 | |
| 367 | fn testIsAbsolutePosix(path: []const u8, expected_result: bool) !void { | 362 | fn testIsAbsolutePosix(path: []const u8, expected_result: bool) !void { |
| 368 | try testing.expectEqual(expected_result, isAbsolutePosix(path)); | 363 | try testing.expectEqual(expected_result, isAbsolutePosix(path)); |
| 369 | } | 364 | } |
| 370 | | 365 | |
| | 366 | /// Deprecated; see `WindowsPath2` |
| 371 | pub const WindowsPath = struct { | 367 | pub const WindowsPath = struct { |
| 372 | is_abs: bool, | 368 | is_abs: bool, |
| 373 | kind: Kind, | 369 | kind: Kind, |
| ... | @@ -380,6 +376,7 @@ pub const WindowsPath = struct { | ... | @@ -380,6 +376,7 @@ pub const WindowsPath = struct { |
| 380 | }; | 376 | }; |
| 381 | }; | 377 | }; |
| 382 | | 378 | |
| | 379 | /// Deprecated; see `parsePathWindows` |
| 383 | pub fn windowsParsePath(path: []const u8) WindowsPath { | 380 | pub fn windowsParsePath(path: []const u8) WindowsPath { |
| 384 | if (path.len >= 2 and path[1] == ':') { | 381 | if (path.len >= 2 and path[1] == ':') { |
| 385 | return WindowsPath{ | 382 | return WindowsPath{ |
| ... | @@ -402,26 +399,18 @@ pub fn windowsParsePath(path: []const u8) WindowsPath { | ... | @@ -402,26 +399,18 @@ pub fn windowsParsePath(path: []const u8) WindowsPath { |
| 402 | .disk_designator = &[_]u8{}, | 399 | .disk_designator = &[_]u8{}, |
| 403 | .is_abs = false, | 400 | .is_abs = false, |
| 404 | }; | 401 | }; |
| 405 | if (path.len < "//a/b".len) { | | |
| 406 | return relative_path; | | |
| 407 | } | | |
| 408 | | | |
| 409 | inline for ("/\\") |this_sep| { | | |
| 410 | const two_sep = [_]u8{ this_sep, this_sep }; | | |
| 411 | if (mem.startsWith(u8, path, &two_sep)) { | | |
| 412 | if (path[2] == this_sep) { | | |
| 413 | return relative_path; | | |
| 414 | } | | |
| 415 | | 402 | |
| 416 | var it = mem.tokenizeAny(u8, path, "/\\"); | 403 | if (path.len >= 2 and PathType.windows.isSep(u8, path[0]) and PathType.windows.isSep(u8, path[1])) { |
| 417 | _ = (it.next() orelse return relative_path); | 404 | const root_end = root_end: { |
| 418 | _ = (it.next() orelse return relative_path); | 405 | var server_end = mem.indexOfAnyPos(u8, path, 2, "/\\") orelse break :root_end path.len; |
| 419 | return WindowsPath{ | 406 | while (server_end < path.len and PathType.windows.isSep(u8, path[server_end])) server_end += 1; |
| 420 | .is_abs = isAbsoluteWindows(path), | 407 | break :root_end mem.indexOfAnyPos(u8, path, server_end, "/\\") orelse path.len; |
| 421 | .kind = WindowsPath.Kind.NetworkShare, | 408 | }; |
| 422 | .disk_designator = path[0..it.index], | 409 | return WindowsPath{ |
| 423 | }; | 410 | .is_abs = true, |
| 424 | } | 411 | .kind = WindowsPath.Kind.NetworkShare, |
| | 412 | .disk_designator = path[0..root_end], |
| | 413 | }; |
| 425 | } | 414 | } |
| 426 | return relative_path; | 415 | return relative_path; |
| 427 | } | 416 | } |
| ... | @@ -446,10 +435,22 @@ test windowsParsePath { | ... | @@ -446,10 +435,22 @@ test windowsParsePath { |
| 446 | try testing.expect(mem.eql(u8, parsed.disk_designator, "\\\\a/b")); | 435 | try testing.expect(mem.eql(u8, parsed.disk_designator, "\\\\a/b")); |
| 447 | } | 436 | } |
| 448 | { | 437 | { |
| 449 | const parsed = windowsParsePath("\\\\a\\"); | 438 | const parsed = windowsParsePath("\\/a\\"); |
| 450 | try testing.expect(!parsed.is_abs); | 439 | try testing.expect(parsed.is_abs); |
| 451 | try testing.expect(parsed.kind == WindowsPath.Kind.None); | 440 | try testing.expect(parsed.kind == WindowsPath.Kind.NetworkShare); |
| 452 | try testing.expect(mem.eql(u8, parsed.disk_designator, "")); | 441 | try testing.expect(mem.eql(u8, parsed.disk_designator, "\\/a\\")); |
| | 442 | } |
| | 443 | { |
| | 444 | const parsed = windowsParsePath("\\\\a\\\\b"); |
| | 445 | try testing.expect(parsed.is_abs); |
| | 446 | try testing.expect(parsed.kind == WindowsPath.Kind.NetworkShare); |
| | 447 | try testing.expect(mem.eql(u8, parsed.disk_designator, "\\\\a\\\\b")); |
| | 448 | } |
| | 449 | { |
| | 450 | const parsed = windowsParsePath("\\\\a\\\\b\\c"); |
| | 451 | try testing.expect(parsed.is_abs); |
| | 452 | try testing.expect(parsed.kind == WindowsPath.Kind.NetworkShare); |
| | 453 | try testing.expect(mem.eql(u8, parsed.disk_designator, "\\\\a\\\\b")); |
| 453 | } | 454 | } |
| 454 | { | 455 | { |
| 455 | const parsed = windowsParsePath("/usr/local"); | 456 | const parsed = windowsParsePath("/usr/local"); |
| ... | @@ -465,6 +466,229 @@ test windowsParsePath { | ... | @@ -465,6 +466,229 @@ test windowsParsePath { |
| 465 | } | 466 | } |
| 466 | } | 467 | } |
| 467 | | 468 | |
| | 469 | /// On Windows, this calls `parsePathWindows` and on POSIX it calls `parsePathPosix`. |
| | 470 | /// |
| | 471 | /// Returns a platform-specific struct with two fields: `root` and `kind`. |
| | 472 | /// The `root` will be a slice of `path` (`/` for POSIX absolute paths, and things |
| | 473 | /// like `C:\`, `\\server\share\`, etc for Windows paths). |
| | 474 | /// If the path is of kind `.relative`, then `root` will be zero-length. |
| | 475 | pub fn parsePath(path: []const u8) switch (native_os) { |
| | 476 | .windows => WindowsPath2(u8), |
| | 477 | else => PosixPath, |
| | 478 | } { |
| | 479 | switch (native_os) { |
| | 480 | .windows => return parsePathWindows(u8, path), |
| | 481 | else => return parsePathPosix(path), |
| | 482 | } |
| | 483 | } |
| | 484 | |
| | 485 | const PosixPath = struct { |
| | 486 | kind: enum { relative, absolute }, |
| | 487 | root: []const u8, |
| | 488 | }; |
| | 489 | |
| | 490 | pub fn parsePathPosix(path: []const u8) PosixPath { |
| | 491 | const abs = isAbsolutePosix(path); |
| | 492 | return .{ |
| | 493 | .kind = if (abs) .absolute else .relative, |
| | 494 | .root = if (abs) path[0..1] else path[0..0], |
| | 495 | }; |
| | 496 | } |
| | 497 | |
| | 498 | test parsePathPosix { |
| | 499 | { |
| | 500 | const parsed = parsePathPosix("a/b"); |
| | 501 | try testing.expectEqual(.relative, parsed.kind); |
| | 502 | try testing.expectEqualStrings("", parsed.root); |
| | 503 | } |
| | 504 | { |
| | 505 | const parsed = parsePathPosix("/a/b"); |
| | 506 | try testing.expectEqual(.absolute, parsed.kind); |
| | 507 | try testing.expectEqualStrings("/", parsed.root); |
| | 508 | } |
| | 509 | { |
| | 510 | const parsed = parsePathPosix("///a/b"); |
| | 511 | try testing.expectEqual(.absolute, parsed.kind); |
| | 512 | try testing.expectEqualStrings("/", parsed.root); |
| | 513 | } |
| | 514 | } |
| | 515 | |
| | 516 | pub fn WindowsPath2(comptime T: type) type { |
| | 517 | return struct { |
| | 518 | kind: windows.Win32PathType, |
| | 519 | root: []const T, |
| | 520 | }; |
| | 521 | } |
| | 522 | |
| | 523 | pub fn parsePathWindows(comptime T: type, path: []const T) WindowsPath2(T) { |
| | 524 | const kind = windows.getWin32PathType(T, path); |
| | 525 | const root = root: switch (kind) { |
| | 526 | .drive_absolute, .drive_relative => { |
| | 527 | const drive_letter_len = getDriveLetter(T, path).len; |
| | 528 | break :root path[0 .. drive_letter_len + @as(usize, if (kind == .drive_absolute) 2 else 1)]; |
| | 529 | }, |
| | 530 | .relative => path[0..0], |
| | 531 | .local_device => path[0..4], |
| | 532 | .root_local_device => path, |
| | 533 | .rooted => path[0..1], |
| | 534 | .unc_absolute => { |
| | 535 | const unc = parseUNC(T, path); |
| | 536 | // There may be any number of path separators between the server and the share, |
| | 537 | // so take that into account by using pointer math to get the difference. |
| | 538 | var root_len = 2 + (unc.share.ptr - unc.server.ptr) + unc.share.len; |
| | 539 | if (unc.sep_after_share) root_len += 1; |
| | 540 | break :root path[0..root_len]; |
| | 541 | }, |
| | 542 | }; |
| | 543 | return .{ |
| | 544 | .kind = kind, |
| | 545 | .root = root, |
| | 546 | }; |
| | 547 | } |
| | 548 | |
| | 549 | test parsePathWindows { |
| | 550 | { |
| | 551 | const path = "//a/b"; |
| | 552 | const parsed = parsePathWindows(u8, path); |
| | 553 | try testing.expectEqual(.unc_absolute, parsed.kind); |
| | 554 | try testing.expectEqualStrings("//a/b", parsed.root); |
| | 555 | try testWindowsParsePathHarmony(path); |
| | 556 | } |
| | 557 | { |
| | 558 | const path = "\\\\a\\b"; |
| | 559 | const parsed = parsePathWindows(u8, path); |
| | 560 | try testing.expectEqual(.unc_absolute, parsed.kind); |
| | 561 | try testing.expectEqualStrings("\\\\a\\b", parsed.root); |
| | 562 | try testWindowsParsePathHarmony(path); |
| | 563 | } |
| | 564 | { |
| | 565 | const path = "\\/a/b/c"; |
| | 566 | const parsed = parsePathWindows(u8, path); |
| | 567 | try testing.expectEqual(.unc_absolute, parsed.kind); |
| | 568 | try testing.expectEqualStrings("\\/a/b/", parsed.root); |
| | 569 | try testWindowsParsePathHarmony(path); |
| | 570 | } |
| | 571 | { |
| | 572 | const path = "\\\\a\\"; |
| | 573 | const parsed = parsePathWindows(u8, path); |
| | 574 | try testing.expectEqual(.unc_absolute, parsed.kind); |
| | 575 | try testing.expectEqualStrings("\\\\a\\", parsed.root); |
| | 576 | try testWindowsParsePathHarmony(path); |
| | 577 | } |
| | 578 | { |
| | 579 | const path = "\\\\a\\b\\"; |
| | 580 | const parsed = parsePathWindows(u8, path); |
| | 581 | try testing.expectEqual(.unc_absolute, parsed.kind); |
| | 582 | try testing.expectEqualStrings("\\\\a\\b\\", parsed.root); |
| | 583 | try testWindowsParsePathHarmony(path); |
| | 584 | } |
| | 585 | { |
| | 586 | const path = "\\\\a\\/b\\/"; |
| | 587 | const parsed = parsePathWindows(u8, path); |
| | 588 | try testing.expectEqual(.unc_absolute, parsed.kind); |
| | 589 | try testing.expectEqualStrings("\\\\a\\/b\\", parsed.root); |
| | 590 | try testWindowsParsePathHarmony(path); |
| | 591 | } |
| | 592 | { |
| | 593 | const path = "\\\\кириллица\\ελληνικά\\português"; |
| | 594 | const parsed = parsePathWindows(u8, path); |
| | 595 | try testing.expectEqual(.unc_absolute, parsed.kind); |
| | 596 | try testing.expectEqualStrings("\\\\кириллица\\ελληνικά\\", parsed.root); |
| | 597 | try testWindowsParsePathHarmony(path); |
| | 598 | } |
| | 599 | { |
| | 600 | const path = "/usr/local"; |
| | 601 | const parsed = parsePathWindows(u8, path); |
| | 602 | try testing.expectEqual(.rooted, parsed.kind); |
| | 603 | try testing.expectEqualStrings("/", parsed.root); |
| | 604 | try testWindowsParsePathHarmony(path); |
| | 605 | } |
| | 606 | { |
| | 607 | const path = "\\\\."; |
| | 608 | const parsed = parsePathWindows(u8, path); |
| | 609 | try testing.expectEqual(.root_local_device, parsed.kind); |
| | 610 | try testing.expectEqualStrings("\\\\.", parsed.root); |
| | 611 | try testWindowsParsePathHarmony(path); |
| | 612 | } |
| | 613 | { |
| | 614 | const path = "\\\\.\\a"; |
| | 615 | const parsed = parsePathWindows(u8, path); |
| | 616 | try testing.expectEqual(.local_device, parsed.kind); |
| | 617 | try testing.expectEqualStrings("\\\\.\\", parsed.root); |
| | 618 | try testWindowsParsePathHarmony(path); |
| | 619 | } |
| | 620 | { |
| | 621 | const path = "c:../"; |
| | 622 | const parsed = parsePathWindows(u8, path); |
| | 623 | try testing.expectEqual(.drive_relative, parsed.kind); |
| | 624 | try testing.expectEqualStrings("c:", parsed.root); |
| | 625 | try testWindowsParsePathHarmony(path); |
| | 626 | } |
| | 627 | { |
| | 628 | const path = "C:\\../"; |
| | 629 | const parsed = parsePathWindows(u8, path); |
| | 630 | try testing.expectEqual(.drive_absolute, parsed.kind); |
| | 631 | try testing.expectEqualStrings("C:\\", parsed.root); |
| | 632 | try testWindowsParsePathHarmony(path); |
| | 633 | } |
| | 634 | { |
| | 635 | // Non-ASCII code point that is encoded as one WTF-16 code unit is considered a valid drive letter |
| | 636 | const path = "€:\\"; |
| | 637 | const parsed = parsePathWindows(u8, path); |
| | 638 | try testing.expectEqual(.drive_absolute, parsed.kind); |
| | 639 | try testing.expectEqualStrings("€:\\", parsed.root); |
| | 640 | try testWindowsParsePathHarmony(path); |
| | 641 | } |
| | 642 | { |
| | 643 | const path = "€:"; |
| | 644 | const parsed = parsePathWindows(u8, path); |
| | 645 | try testing.expectEqual(.drive_relative, parsed.kind); |
| | 646 | try testing.expectEqualStrings("€:", parsed.root); |
| | 647 | try testWindowsParsePathHarmony(path); |
| | 648 | } |
| | 649 | { |
| | 650 | // But code points that are encoded as two WTF-16 code units are not |
| | 651 | const path = "\u{10000}:\\"; |
| | 652 | const parsed = parsePathWindows(u8, path); |
| | 653 | try testing.expectEqual(.relative, parsed.kind); |
| | 654 | try testing.expectEqualStrings("", parsed.root); |
| | 655 | try testWindowsParsePathHarmony(path); |
| | 656 | } |
| | 657 | { |
| | 658 | const path = "\u{10000}:"; |
| | 659 | const parsed = parsePathWindows(u8, path); |
| | 660 | try testing.expectEqual(.relative, parsed.kind); |
| | 661 | try testing.expectEqualStrings("", parsed.root); |
| | 662 | try testWindowsParsePathHarmony(path); |
| | 663 | } |
| | 664 | { |
| | 665 | // Paths are assumed to be in the Win32 namespace, so while this is |
| | 666 | // likely a NT namespace path, it's treated as a rooted path. |
| | 667 | const path = "\\??\\foo"; |
| | 668 | const parsed = parsePathWindows(u8, path); |
| | 669 | try testing.expectEqual(.rooted, parsed.kind); |
| | 670 | try testing.expectEqualStrings("\\", parsed.root); |
| | 671 | try testWindowsParsePathHarmony(path); |
| | 672 | } |
| | 673 | } |
| | 674 | |
| | 675 | fn testWindowsParsePathHarmony(wtf8: []const u8) !void { |
| | 676 | var wtf16_buf: [256]u16 = undefined; |
| | 677 | const wtf16_len = try std.unicode.wtf8ToWtf16Le(&wtf16_buf, wtf8); |
| | 678 | const wtf16 = wtf16_buf[0..wtf16_len]; |
| | 679 | |
| | 680 | const wtf8_parsed = parsePathWindows(u8, wtf8); |
| | 681 | const wtf16_parsed = parsePathWindows(u16, wtf16); |
| | 682 | |
| | 683 | var wtf8_buf: [256]u8 = undefined; |
| | 684 | const wtf16_root_as_wtf8_len = std.unicode.wtf16LeToWtf8(&wtf8_buf, wtf16_parsed.root); |
| | 685 | const wtf16_root_as_wtf8 = wtf8_buf[0..wtf16_root_as_wtf8_len]; |
| | 686 | |
| | 687 | try std.testing.expectEqual(wtf8_parsed.kind, wtf16_parsed.kind); |
| | 688 | try std.testing.expectEqualStrings(wtf8_parsed.root, wtf16_root_as_wtf8); |
| | 689 | } |
| | 690 | |
| | 691 | /// Deprecated; use `parsePath` |
| 468 | pub fn diskDesignator(path: []const u8) []const u8 { | 692 | pub fn diskDesignator(path: []const u8) []const u8 { |
| 469 | if (native_os == .windows) { | 693 | if (native_os == .windows) { |
| 470 | return diskDesignatorWindows(path); | 694 | return diskDesignatorWindows(path); |
| ... | @@ -473,41 +697,172 @@ pub fn diskDesignator(path: []const u8) []const u8 { | ... | @@ -473,41 +697,172 @@ pub fn diskDesignator(path: []const u8) []const u8 { |
| 473 | } | 697 | } |
| 474 | } | 698 | } |
| 475 | | 699 | |
| | 700 | /// Deprecated; use `parsePathWindows` |
| 476 | pub fn diskDesignatorWindows(path: []const u8) []const u8 { | 701 | pub fn diskDesignatorWindows(path: []const u8) []const u8 { |
| 477 | return windowsParsePath(path).disk_designator; | 702 | return windowsParsePath(path).disk_designator; |
| 478 | } | 703 | } |
| 479 | | 704 | |
| 480 | fn networkShareServersEql(ns1: []const u8, ns2: []const u8) bool { | 705 | fn WindowsUNC(comptime T: type) type { |
| 481 | const sep1 = ns1[0]; | 706 | return struct { |
| 482 | const sep2 = ns2[0]; | 707 | server: []const T, |
| | 708 | sep_after_server: bool, |
| | 709 | share: []const T, |
| | 710 | sep_after_share: bool, |
| | 711 | }; |
| | 712 | } |
| 483 | | 713 | |
| 484 | var it1 = mem.tokenizeScalar(u8, ns1, sep1); | 714 | /// Asserts that `path` starts with two path separators |
| 485 | var it2 = mem.tokenizeScalar(u8, ns2, sep2); | 715 | fn parseUNC(comptime T: type, path: []const T) WindowsUNC(T) { |
| | 716 | assert(path.len >= 2 and PathType.windows.isSep(T, path[0]) and PathType.windows.isSep(T, path[1])); |
| | 717 | const any_sep = switch (T) { |
| | 718 | u8 => "/\\", |
| | 719 | u16 => std.unicode.wtf8ToWtf16LeStringLiteral("/\\"), |
| | 720 | else => @compileError("only u8 (WTF-8) and u16 (WTF-16LE) are supported"), |
| | 721 | }; |
| | 722 | // For the server, the first path separator after the initial two is always |
| | 723 | // the terminator of the server name, even if that means the server name is |
| | 724 | // zero-length. |
| | 725 | const server_end = mem.indexOfAnyPos(T, path, 2, any_sep) orelse return .{ |
| | 726 | .server = path[2..path.len], |
| | 727 | .sep_after_server = false, |
| | 728 | .share = path[path.len..path.len], |
| | 729 | .sep_after_share = false, |
| | 730 | }; |
| | 731 | // For the share, there can be any number of path separators between the server |
| | 732 | // and the share, so we want to skip over all of them instead of just looking for |
| | 733 | // the first one. |
| | 734 | var it = std.mem.tokenizeAny(T, path[server_end + 1 ..], any_sep); |
| | 735 | const share = it.next() orelse return .{ |
| | 736 | .server = path[2..server_end], |
| | 737 | .sep_after_server = true, |
| | 738 | .share = path[server_end + 1 .. server_end + 1], |
| | 739 | .sep_after_share = false, |
| | 740 | }; |
| | 741 | return .{ |
| | 742 | .server = path[2..server_end], |
| | 743 | .sep_after_server = true, |
| | 744 | .share = share, |
| | 745 | .sep_after_share = it.index != it.buffer.len, |
| | 746 | }; |
| | 747 | } |
| 486 | | 748 | |
| 487 | return windows.eqlIgnoreCaseWtf8(it1.next().?, it2.next().?); | 749 | test parseUNC { |
| | 750 | { |
| | 751 | const unc = parseUNC(u8, "//"); |
| | 752 | try std.testing.expectEqualStrings("", unc.server); |
| | 753 | try std.testing.expect(!unc.sep_after_server); |
| | 754 | try std.testing.expectEqualStrings("", unc.share); |
| | 755 | try std.testing.expect(!unc.sep_after_share); |
| | 756 | } |
| | 757 | { |
| | 758 | const unc = parseUNC(u8, "\\\\s"); |
| | 759 | try std.testing.expectEqualStrings("s", unc.server); |
| | 760 | try std.testing.expect(!unc.sep_after_server); |
| | 761 | try std.testing.expectEqualStrings("", unc.share); |
| | 762 | try std.testing.expect(!unc.sep_after_share); |
| | 763 | } |
| | 764 | { |
| | 765 | const unc = parseUNC(u8, "\\\\s/"); |
| | 766 | try std.testing.expectEqualStrings("s", unc.server); |
| | 767 | try std.testing.expect(unc.sep_after_server); |
| | 768 | try std.testing.expectEqualStrings("", unc.share); |
| | 769 | try std.testing.expect(!unc.sep_after_share); |
| | 770 | } |
| | 771 | { |
| | 772 | const unc = parseUNC(u8, "\\/server\\share"); |
| | 773 | try std.testing.expectEqualStrings("server", unc.server); |
| | 774 | try std.testing.expect(unc.sep_after_server); |
| | 775 | try std.testing.expectEqualStrings("share", unc.share); |
| | 776 | try std.testing.expect(!unc.sep_after_share); |
| | 777 | } |
| | 778 | { |
| | 779 | const unc = parseUNC(u8, "/\\server\\share/"); |
| | 780 | try std.testing.expectEqualStrings("server", unc.server); |
| | 781 | try std.testing.expect(unc.sep_after_server); |
| | 782 | try std.testing.expectEqualStrings("share", unc.share); |
| | 783 | try std.testing.expect(unc.sep_after_share); |
| | 784 | } |
| | 785 | { |
| | 786 | const unc = parseUNC(u8, "\\\\server/\\share\\/"); |
| | 787 | try std.testing.expectEqualStrings("server", unc.server); |
| | 788 | try std.testing.expect(unc.sep_after_server); |
| | 789 | try std.testing.expectEqualStrings("share", unc.share); |
| | 790 | try std.testing.expect(unc.sep_after_share); |
| | 791 | } |
| | 792 | { |
| | 793 | const unc = parseUNC(u8, "\\\\server\\/\\\\"); |
| | 794 | try std.testing.expectEqualStrings("server", unc.server); |
| | 795 | try std.testing.expect(unc.sep_after_server); |
| | 796 | try std.testing.expectEqualStrings("", unc.share); |
| | 797 | try std.testing.expect(!unc.sep_after_share); |
| | 798 | } |
| 488 | } | 799 | } |
| 489 | | 800 | |
| 490 | fn compareDiskDesignators(kind: WindowsPath.Kind, p1: []const u8, p2: []const u8) bool { | 801 | const DiskDesignatorKind = enum { drive, unc }; |
| | 802 | |
| | 803 | /// `p1` and `p2` are both assumed to be the `kind` provided. |
| | 804 | fn compareDiskDesignators(comptime T: type, kind: DiskDesignatorKind, p1: []const T, p2: []const T) bool { |
| | 805 | const eql = switch (T) { |
| | 806 | u8 => windows.eqlIgnoreCaseWtf8, |
| | 807 | u16 => windows.eqlIgnoreCaseWtf16, |
| | 808 | else => @compileError("only u8 (WTF-8) and u16 (WTF-16LE) is supported"), |
| | 809 | }; |
| 491 | switch (kind) { | 810 | switch (kind) { |
| 492 | WindowsPath.Kind.None => { | 811 | .drive => { |
| 493 | assert(p1.len == 0); | 812 | const drive_letter1 = getDriveLetter(T, p1); |
| 494 | assert(p2.len == 0); | 813 | const drive_letter2 = getDriveLetter(T, p2); |
| 495 | return true; | 814 | |
| 496 | }, | 815 | return eql(drive_letter1, drive_letter2); |
| 497 | WindowsPath.Kind.Drive => { | | |
| 498 | return ascii.toUpper(p1[0]) == ascii.toUpper(p2[0]); | | |
| 499 | }, | 816 | }, |
| 500 | WindowsPath.Kind.NetworkShare => { | 817 | .unc => { |
| 501 | var it1 = mem.tokenizeAny(u8, p1, "/\\"); | 818 | var unc1 = parseUNC(T, p1); |
| 502 | var it2 = mem.tokenizeAny(u8, p2, "/\\"); | 819 | var unc2 = parseUNC(T, p2); |
| 503 | | 820 | |
| 504 | return windows.eqlIgnoreCaseWtf8(it1.next().?, it2.next().?) and windows.eqlIgnoreCaseWtf8(it1.next().?, it2.next().?); | 821 | return eql(unc1.server, unc2.server) and |
| | 822 | eql(unc1.share, unc2.share); |
| 505 | }, | 823 | }, |
| 506 | } | 824 | } |
| 507 | } | 825 | } |
| 508 | | 826 | |
| | 827 | /// `path` is assumed to be drive-relative or drive-absolute. |
| | 828 | fn getDriveLetter(comptime T: type, path: []const T) []const T { |
| | 829 | const len: usize = switch (T) { |
| | 830 | // getWin32PathType will only return .drive_absolute/.drive_relative when there is |
| | 831 | // (1) a valid code point, and (2) a code point < U+10000, so we only need to |
| | 832 | // get the length determined by the first byte. |
| | 833 | u8 => std.unicode.utf8ByteSequenceLength(path[0]) catch unreachable, |
| | 834 | u16 => 1, |
| | 835 | else => @compileError("unsupported type: " ++ @typeName(T)), |
| | 836 | }; |
| | 837 | return path[0..len]; |
| | 838 | } |
| | 839 | |
| | 840 | test compareDiskDesignators { |
| | 841 | try testCompareDiskDesignators(true, .drive, "c:", "C:\\"); |
| | 842 | try testCompareDiskDesignators(true, .drive, "C:\\", "C:"); |
| | 843 | try testCompareDiskDesignators(false, .drive, "C:\\", "D:\\"); |
| | 844 | // Case-insensitivity technically applies to non-ASCII drive letters |
| | 845 | try testCompareDiskDesignators(true, .drive, "λ:\\", "Λ:"); |
| | 846 | |
| | 847 | try testCompareDiskDesignators(true, .unc, "\\\\server", "//server//"); |
| | 848 | try testCompareDiskDesignators(true, .unc, "\\\\server\\\\share", "/\\server/share"); |
| | 849 | try testCompareDiskDesignators(true, .unc, "\\\\server\\\\share", "/\\server/share\\\\foo"); |
| | 850 | try testCompareDiskDesignators(false, .unc, "\\\\server\\sharefoo", "/\\server/share\\foo"); |
| | 851 | try testCompareDiskDesignators(false, .unc, "\\\\serverfoo\\\\share", "//server/share"); |
| | 852 | try testCompareDiskDesignators(false, .unc, "\\\\server\\", "//server/share"); |
| | 853 | } |
| | 854 | |
| | 855 | fn testCompareDiskDesignators(expected_result: bool, kind: DiskDesignatorKind, p1: []const u8, p2: []const u8) !void { |
| | 856 | var wtf16_buf1: [256]u16 = undefined; |
| | 857 | const w1_len = try std.unicode.wtf8ToWtf16Le(&wtf16_buf1, p1); |
| | 858 | var wtf16_buf2: [256]u16 = undefined; |
| | 859 | const w2_len = try std.unicode.wtf8ToWtf16Le(&wtf16_buf2, p2); |
| | 860 | try std.testing.expectEqual(expected_result, compareDiskDesignators(u8, kind, p1, p2)); |
| | 861 | try std.testing.expectEqual(expected_result, compareDiskDesignators(u16, kind, wtf16_buf1[0..w1_len], wtf16_buf2[0..w2_len])); |
| | 862 | } |
| | 863 | |
| 509 | /// On Windows, this calls `resolveWindows` and on POSIX it calls `resolvePosix`. | 864 | /// On Windows, this calls `resolveWindows` and on POSIX it calls `resolvePosix`. |
| 510 | pub fn resolve(allocator: Allocator, paths: []const []const u8) ![]u8 { | 865 | pub fn resolve(allocator: Allocator, paths: []const []const u8) Allocator.Error![]u8 { |
| 511 | if (native_os == .windows) { | 866 | if (native_os == .windows) { |
| 512 | return resolveWindows(allocator, paths); | 867 | return resolveWindows(allocator, paths); |
| 513 | } else { | 868 | } else { |
| ... | @@ -516,184 +871,232 @@ pub fn resolve(allocator: Allocator, paths: []const []const u8) ![]u8 { | ... | @@ -516,184 +871,232 @@ pub fn resolve(allocator: Allocator, paths: []const []const u8) ![]u8 { |
| 516 | } | 871 | } |
| 517 | | 872 | |
| 518 | /// This function is like a series of `cd` statements executed one after another. | 873 | /// This function is like a series of `cd` statements executed one after another. |
| 519 | /// It resolves "." and "..", but will not convert relative path to absolute path, use std.fs.Dir.realpath instead. | 874 | /// It resolves "." and ".." to the best of its ability, but will not convert relative paths to |
| 520 | /// The result does not have a trailing path separator. | 875 | /// an absolute path, use std.fs.Dir.realpath instead. |
| 521 | /// Each drive has its own current working directory. | 876 | /// ".." components may persist in the resolved path if the resolved path is relative or drive-relative. |
| 522 | /// Path separators are canonicalized to '\\' and drives are canonicalized to capital letters. | 877 | /// Path separators are canonicalized to '\\' and drives are canonicalized to capital letters. |
| | 878 | /// |
| | 879 | /// The result will not have a trailing path separator, except for the following scenarios: |
| | 880 | /// - The resolved path is drive-absolute with no components (e.g. `C:\`). |
| | 881 | /// - The resolved path is a UNC path with only a server name, and the input path contained a trailing separator |
| | 882 | /// (e.g. `\\server\`). |
| | 883 | /// - The resolved path is a UNC path with no components after the share name, and the input path contained a |
| | 884 | /// trailing separator (e.g. `\\server\share\`). |
| | 885 | /// |
| | 886 | /// Each drive has its own current working directory, which is only resolved via the paths provided. |
| | 887 | /// In the scenario that the resolved path contains a drive-relative path that can't be resolved using the paths alone, |
| | 888 | /// the result will be a drive-relative path. |
| | 889 | /// Similarly, in the scenario that the resolved path contains a rooted path that can't be resolved using the paths alone, |
| | 890 | /// the result will be a rooted path. |
| | 891 | /// |
| 523 | /// Note: all usage of this function should be audited due to the existence of symlinks. | 892 | /// Note: all usage of this function should be audited due to the existence of symlinks. |
| 524 | /// Without performing actual syscalls, resolving `..` could be incorrect. | 893 | /// Without performing actual syscalls, resolving `..` could be incorrect. |
| 525 | /// This API may break in the future: https://github.com/ziglang/zig/issues/13613 | 894 | /// This API may break in the future: https://github.com/ziglang/zig/issues/13613 |
| 526 | pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 { | 895 | pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) Allocator.Error![]u8 { |
| 527 | assert(paths.len > 0); | 896 | // Avoid heap allocation when paths.len is <= @bitSizeOf(usize) * 2 |
| 528 | | 897 | // (we use `* 3` because stackFallback uses 1 usize as a length) |
| 529 | // determine which disk designator we will result with, if any | 898 | var bit_set_allocator_state = std.heap.stackFallback(@sizeOf(usize) * 3, allocator); |
| 530 | var result_drive_buf = "_:".*; | 899 | const bit_set_allocator = bit_set_allocator_state.get(); |
| 531 | var disk_designator: []const u8 = ""; | 900 | var relevant_paths = try std.bit_set.DynamicBitSetUnmanaged.initEmpty(bit_set_allocator, paths.len); |
| 532 | var drive_kind = WindowsPath.Kind.None; | 901 | defer relevant_paths.deinit(bit_set_allocator); |
| 533 | var have_abs_path = false; | 902 | |
| 534 | var first_index: usize = 0; | 903 | // Iterate the paths backwards, marking the relevant paths along the way. |
| 535 | for (paths, 0..) |p, i| { | 904 | // This also allows us to break from the loop whenever any earlier paths are known to be irrelevant. |
| 536 | const parsed = windowsParsePath(p); | 905 | var first_path_i: usize = paths.len; |
| 537 | if (parsed.is_abs) { | 906 | const effective_root_path: WindowsPath2(u8) = root: { |
| 538 | have_abs_path = true; | 907 | var last_effective_root_path: WindowsPath2(u8) = .{ .kind = .relative, .root = "" }; |
| 539 | first_index = i; | 908 | var last_rooted_path_i: ?usize = null; |
| | 909 | var last_drive_relative_path_i: usize = undefined; |
| | 910 | while (first_path_i > 0) { |
| | 911 | first_path_i -= 1; |
| | 912 | const parsed = parsePathWindows(u8, paths[first_path_i]); |
| | 913 | switch (parsed.kind) { |
| | 914 | .unc_absolute, .root_local_device, .local_device => { |
| | 915 | switch (last_effective_root_path.kind) { |
| | 916 | .rooted => {}, |
| | 917 | .drive_relative => continue, |
| | 918 | else => { |
| | 919 | relevant_paths.set(first_path_i); |
| | 920 | }, |
| | 921 | } |
| | 922 | break :root parsed; |
| | 923 | }, |
| | 924 | .drive_relative, .drive_absolute => { |
| | 925 | switch (last_effective_root_path.kind) { |
| | 926 | .drive_relative => if (!compareDiskDesignators(u8, .drive, parsed.root, last_effective_root_path.root)) { |
| | 927 | continue; |
| | 928 | } else if (last_rooted_path_i != null) { |
| | 929 | break :root .{ .kind = .drive_absolute, .root = parsed.root }; |
| | 930 | }, |
| | 931 | .relative => last_effective_root_path = parsed, |
| | 932 | .rooted => { |
| | 933 | // This is the end of the line, since the rooted path will always be relative |
| | 934 | // to this drive letter, and even if the current path is drive-relative, the |
| | 935 | // rooted-ness makes that irrelevant. |
| | 936 | // |
| | 937 | // Therefore, force the kind of the effective root to be drive-absolute in order to |
| | 938 | // properly resolve a rooted path against a drive-relative one, as the result should |
| | 939 | // always be drive-absolute. |
| | 940 | break :root .{ .kind = .drive_absolute, .root = parsed.root }; |
| | 941 | }, |
| | 942 | .drive_absolute, .unc_absolute, .root_local_device, .local_device => unreachable, |
| | 943 | } |
| | 944 | relevant_paths.set(first_path_i); |
| | 945 | last_drive_relative_path_i = first_path_i; |
| | 946 | if (parsed.kind == .drive_absolute) { |
| | 947 | break :root parsed; |
| | 948 | } |
| | 949 | }, |
| | 950 | .relative => { |
| | 951 | switch (last_effective_root_path.kind) { |
| | 952 | .rooted => continue, |
| | 953 | .relative => last_effective_root_path = parsed, |
| | 954 | else => {}, |
| | 955 | } |
| | 956 | relevant_paths.set(first_path_i); |
| | 957 | }, |
| | 958 | .rooted => { |
| | 959 | switch (last_effective_root_path.kind) { |
| | 960 | .drive_relative => {}, |
| | 961 | .relative => last_effective_root_path = parsed, |
| | 962 | .rooted => continue, |
| | 963 | .drive_absolute, .unc_absolute, .root_local_device, .local_device => unreachable, |
| | 964 | } |
| | 965 | if (last_rooted_path_i == null) { |
| | 966 | last_rooted_path_i = first_path_i; |
| | 967 | relevant_paths.set(first_path_i); |
| | 968 | } |
| | 969 | }, |
| | 970 | } |
| 540 | } | 971 | } |
| 541 | switch (parsed.kind) { | 972 | // After iterating, if the pending effective root is drive-relative then that means |
| 542 | .Drive => { | 973 | // nothing has led to forcing a drive-absolute root (a path that allows resolving the |
| 543 | result_drive_buf[0] = ascii.toUpper(parsed.disk_designator[0]); | 974 | // drive-specific CWD would cause an early break), so we now need to ignore all paths |
| 544 | disk_designator = result_drive_buf[0..]; | 975 | // before the most recent drive-relative one. For example, if we're resolving |
| 545 | drive_kind = WindowsPath.Kind.Drive; | 976 | // { "\\rooted", "relative", "C:drive-relative" } |
| 546 | }, | 977 | // then the `\rooted` and `relative` needs to be ignored since we can't |
| 547 | .NetworkShare => { | 978 | // know what the rooted path is rooted against as that'd require knowing the CWD. |
| 548 | disk_designator = parsed.disk_designator; | 979 | if (last_effective_root_path.kind == .drive_relative) { |
| 549 | drive_kind = WindowsPath.Kind.NetworkShare; | 980 | for (0..last_drive_relative_path_i) |i| { |
| 550 | }, | 981 | relevant_paths.unset(i); |
| 551 | .None => {}, | 982 | } |
| 552 | } | 983 | } |
| 553 | } | 984 | break :root last_effective_root_path; |
| | 985 | }; |
| 554 | | 986 | |
| 555 | // if we will result with a disk designator, loop again to determine | 987 | var result: std.ArrayList(u8) = .empty; |
| 556 | // which is the last time the disk designator is absolutely specified, if any | 988 | defer result.deinit(allocator); |
| 557 | // and count up the max bytes for paths related to this disk designator | 989 | |
| 558 | if (drive_kind != WindowsPath.Kind.None) { | 990 | var want_path_sep_between_root_and_component = false; |
| 559 | have_abs_path = false; | 991 | switch (effective_root_path.kind) { |
| 560 | first_index = 0; | 992 | .root_local_device, .local_device => { |
| 561 | var correct_disk_designator = false; | 993 | try result.ensureUnusedCapacity(allocator, 3); |
| 562 | | 994 | result.appendSliceAssumeCapacity("\\\\"); |
| 563 | for (paths, 0..) |p, i| { | 995 | result.appendAssumeCapacity(effective_root_path.root[2]); // . or ? |
| 564 | const parsed = windowsParsePath(p); | 996 | want_path_sep_between_root_and_component = true; |
| 565 | if (parsed.kind != WindowsPath.Kind.None) { | 997 | }, |
| 566 | if (parsed.kind == drive_kind) { | 998 | .drive_absolute, .drive_relative => { |
| 567 | correct_disk_designator = compareDiskDesignators(drive_kind, disk_designator, parsed.disk_designator); | 999 | try result.ensureUnusedCapacity(allocator, effective_root_path.root.len); |
| 568 | } else { | 1000 | result.appendAssumeCapacity(std.ascii.toUpper(effective_root_path.root[0])); |
| 569 | continue; | 1001 | result.appendAssumeCapacity(':'); |
| 570 | } | 1002 | if (effective_root_path.kind == .drive_absolute) { |
| | 1003 | result.appendAssumeCapacity('\\'); |
| 571 | } | 1004 | } |
| 572 | if (!correct_disk_designator) { | 1005 | }, |
| 573 | continue; | 1006 | .unc_absolute => { |
| | 1007 | const unc = parseUNC(u8, effective_root_path.root); |
| | 1008 | |
| | 1009 | const root_len = len: { |
| | 1010 | var len: usize = 2 + unc.server.len + unc.share.len; |
| | 1011 | if (unc.sep_after_server) len += 1; |
| | 1012 | if (unc.sep_after_share) len += 1; |
| | 1013 | break :len len; |
| | 1014 | }; |
| | 1015 | try result.ensureUnusedCapacity(allocator, root_len); |
| | 1016 | result.appendSliceAssumeCapacity("\\\\"); |
| | 1017 | if (unc.server.len > 0 or unc.sep_after_server) { |
| | 1018 | result.appendSliceAssumeCapacity(unc.server); |
| | 1019 | if (unc.sep_after_server) |
| | 1020 | result.appendAssumeCapacity('\\') |
| | 1021 | else |
| | 1022 | want_path_sep_between_root_and_component = true; |
| 574 | } | 1023 | } |
| 575 | if (parsed.is_abs) { | 1024 | if (unc.share.len > 0) { |
| 576 | first_index = i; | 1025 | result.appendSliceAssumeCapacity(unc.share); |
| 577 | have_abs_path = true; | 1026 | if (unc.sep_after_share) |
| | 1027 | result.appendAssumeCapacity('\\') |
| | 1028 | else |
| | 1029 | want_path_sep_between_root_and_component = true; |
| 578 | } | 1030 | } |
| 579 | } | 1031 | }, |
| | 1032 | .rooted => { |
| | 1033 | try result.append(allocator, '\\'); |
| | 1034 | }, |
| | 1035 | .relative => {}, |
| 580 | } | 1036 | } |
| 581 | | 1037 | |
| 582 | // Allocate result and fill in the disk designator. | 1038 | const root_len = result.items.len; |
| 583 | var result = std.array_list.Managed(u8).init(allocator); | | |
| 584 | defer result.deinit(); | | |
| 585 | | | |
| 586 | const disk_designator_len: usize = l: { | | |
| 587 | if (!have_abs_path) break :l 0; | | |
| 588 | switch (drive_kind) { | | |
| 589 | .Drive => { | | |
| 590 | try result.appendSlice(disk_designator); | | |
| 591 | break :l disk_designator.len; | | |
| 592 | }, | | |
| 593 | .NetworkShare => { | | |
| 594 | var it = mem.tokenizeAny(u8, paths[first_index], "/\\"); | | |
| 595 | const server_name = it.next().?; | | |
| 596 | const other_name = it.next().?; | | |
| 597 | | | |
| 598 | try result.ensureUnusedCapacity(2 + 1 + server_name.len + other_name.len); | | |
| 599 | result.appendSliceAssumeCapacity("\\\\"); | | |
| 600 | result.appendSliceAssumeCapacity(server_name); | | |
| 601 | result.appendAssumeCapacity('\\'); | | |
| 602 | result.appendSliceAssumeCapacity(other_name); | | |
| 603 | | | |
| 604 | break :l result.items.len; | | |
| 605 | }, | | |
| 606 | .None => { | | |
| 607 | break :l 1; | | |
| 608 | }, | | |
| 609 | } | | |
| 610 | }; | | |
| 611 | | | |
| 612 | var correct_disk_designator = true; | | |
| 613 | var negative_count: usize = 0; | 1039 | var negative_count: usize = 0; |
| | 1040 | for (paths[first_path_i..], first_path_i..) |path, i| { |
| | 1041 | if (!relevant_paths.isSet(i)) continue; |
| 614 | | 1042 | |
| 615 | for (paths[first_index..]) |p| { | 1043 | const parsed = parsePathWindows(u8, path); |
| 616 | const parsed = windowsParsePath(p); | 1044 | const skip_len = parsed.root.len; |
| 617 | | 1045 | var it = mem.tokenizeAny(u8, path[skip_len..], "/\\"); |
| 618 | if (parsed.kind != .None) { | | |
| 619 | if (parsed.kind == drive_kind) { | | |
| 620 | const dd = result.items[0..disk_designator_len]; | | |
| 621 | correct_disk_designator = compareDiskDesignators(drive_kind, dd, parsed.disk_designator); | | |
| 622 | } else { | | |
| 623 | continue; | | |
| 624 | } | | |
| 625 | } | | |
| 626 | if (!correct_disk_designator) { | | |
| 627 | continue; | | |
| 628 | } | | |
| 629 | var it = mem.tokenizeAny(u8, p[parsed.disk_designator.len..], "/\\"); | | |
| 630 | while (it.next()) |component| { | 1046 | while (it.next()) |component| { |
| 631 | if (mem.eql(u8, component, ".")) { | 1047 | if (mem.eql(u8, component, ".")) { |
| 632 | continue; | 1048 | continue; |
| 633 | } else if (mem.eql(u8, component, "..")) { | 1049 | } else if (mem.eql(u8, component, "..")) { |
| 634 | if (result.items.len == 0) { | 1050 | if (result.items.len == 0 or (result.items.len == root_len and effective_root_path.kind == .drive_relative)) { |
| 635 | negative_count += 1; | 1051 | negative_count += 1; |
| 636 | continue; | 1052 | continue; |
| 637 | } | 1053 | } |
| 638 | while (true) { | 1054 | while (true) { |
| 639 | if (result.items.len == disk_designator_len) { | 1055 | if (result.items.len == root_len) { |
| 640 | break; | 1056 | break; |
| 641 | } | 1057 | } |
| 642 | const end_with_sep = switch (result.items[result.items.len - 1]) { | 1058 | const end_with_sep = PathType.windows.isSep(u8, result.items[result.items.len - 1]); |
| 643 | '\\', '/' => true, | | |
| 644 | else => false, | | |
| 645 | }; | | |
| 646 | result.items.len -= 1; | 1059 | result.items.len -= 1; |
| 647 | if (end_with_sep or result.items.len == 0) break; | 1060 | if (end_with_sep) break; |
| 648 | } | 1061 | } |
| 649 | } else if (!have_abs_path and result.items.len == 0) { | 1062 | } else if (result.items.len == root_len and !want_path_sep_between_root_and_component) { |
| 650 | try result.appendSlice(component); | 1063 | try result.appendSlice(allocator, component); |
| 651 | } else { | 1064 | } else { |
| 652 | try result.ensureUnusedCapacity(1 + component.len); | 1065 | try result.ensureUnusedCapacity(allocator, 1 + component.len); |
| 653 | result.appendAssumeCapacity('\\'); | 1066 | result.appendAssumeCapacity('\\'); |
| 654 | result.appendSliceAssumeCapacity(component); | 1067 | result.appendSliceAssumeCapacity(component); |
| 655 | } | 1068 | } |
| 656 | } | 1069 | } |
| 657 | } | 1070 | } |
| 658 | | 1071 | |
| 659 | if (disk_designator_len != 0 and result.items.len == disk_designator_len) { | 1072 | if (root_len != 0 and result.items.len == root_len and negative_count == 0) { |
| 660 | try result.append('\\'); | 1073 | return result.toOwnedSlice(allocator); |
| 661 | return result.toOwnedSlice(); | | |
| 662 | } | 1074 | } |
| 663 | | 1075 | |
| 664 | if (result.items.len == 0) { | 1076 | if (result.items.len == root_len) { |
| 665 | if (negative_count == 0) { | 1077 | if (negative_count == 0) { |
| 666 | return allocator.dupe(u8, "."); | 1078 | return allocator.dupe(u8, "."); |
| 667 | } else { | | |
| 668 | const real_result = try allocator.alloc(u8, 3 * negative_count - 1); | | |
| 669 | var count = negative_count - 1; | | |
| 670 | var i: usize = 0; | | |
| 671 | while (count > 0) : (count -= 1) { | | |
| 672 | real_result[i..][0..3].* = "..\\".*; | | |
| 673 | i += 3; | | |
| 674 | } | | |
| 675 | real_result[i..][0..2].* = "..".*; | | |
| 676 | return real_result; | | |
| 677 | } | 1079 | } |
| 678 | } | | |
| 679 | | 1080 | |
| 680 | if (negative_count == 0) { | 1081 | try result.ensureTotalCapacityPrecise(allocator, 3 * negative_count - 1); |
| 681 | return result.toOwnedSlice(); | 1082 | for (0..negative_count - 1) |_| { |
| | 1083 | result.appendSliceAssumeCapacity("..\\"); |
| | 1084 | } |
| | 1085 | result.appendSliceAssumeCapacity(".."); |
| 682 | } else { | 1086 | } else { |
| 683 | const real_result = try allocator.alloc(u8, 3 * negative_count + result.items.len); | 1087 | const dest = try result.addManyAt(allocator, root_len, 3 * negative_count); |
| 684 | var count = negative_count; | 1088 | for (0..negative_count) |i| { |
| 685 | var i: usize = 0; | 1089 | dest[i * 3 ..][0..3].* = "..\\".*; |
| 686 | while (count > 0) : (count -= 1) { | | |
| 687 | real_result[i..][0..3].* = "..\\".*; | | |
| 688 | i += 3; | | |
| 689 | } | 1090 | } |
| 690 | @memcpy(real_result[i..][0..result.items.len], result.items); | | |
| 691 | return real_result; | | |
| 692 | } | 1091 | } |
| | 1092 | |
| | 1093 | return result.toOwnedSlice(allocator); |
| 693 | } | 1094 | } |
| 694 | | 1095 | |
| 695 | /// This function is like a series of `cd` statements executed one after another. | 1096 | /// This function is like a series of `cd` statements executed one after another. |
| 696 | /// It resolves "." and "..", but will not convert relative path to absolute path, use std.fs.Dir.realpath instead. | 1097 | /// It resolves "." and ".." to the best of its ability, but will not convert relative paths to |
| | 1098 | /// an absolute path, use std.fs.Dir.realpath instead. |
| | 1099 | /// ".." components may persist in the resolved path if the resolved path is relative. |
| 697 | /// The result does not have a trailing path separator. | 1100 | /// The result does not have a trailing path separator. |
| 698 | /// This function does not perform any syscalls. Executing this series of path | 1101 | /// This function does not perform any syscalls. Executing this series of path |
| 699 | /// lookups on the actual filesystem may produce different results due to | 1102 | /// lookups on the actual filesystem may produce different results due to |
| ... | @@ -772,10 +1175,14 @@ pub fn resolvePosix(allocator: Allocator, paths: []const []const u8) Allocator.E | ... | @@ -772,10 +1175,14 @@ pub fn resolvePosix(allocator: Allocator, paths: []const []const u8) Allocator.E |
| 772 | } | 1175 | } |
| 773 | | 1176 | |
| 774 | test resolve { | 1177 | test resolve { |
| | 1178 | try testResolveWindows(&[_][]const u8{ "a", "..\\..\\.." }, "..\\.."); |
| | 1179 | try testResolveWindows(&[_][]const u8{ "..", "", "..\\..\\foo" }, "..\\..\\..\\foo"); |
| 775 | try testResolveWindows(&[_][]const u8{ "a\\b\\c\\", "..\\..\\.." }, "."); | 1180 | try testResolveWindows(&[_][]const u8{ "a\\b\\c\\", "..\\..\\.." }, "."); |
| 776 | try testResolveWindows(&[_][]const u8{"."}, "."); | 1181 | try testResolveWindows(&[_][]const u8{"."}, "."); |
| 777 | try testResolveWindows(&[_][]const u8{""}, "."); | 1182 | try testResolveWindows(&[_][]const u8{""}, "."); |
| 778 | | 1183 | |
| | 1184 | try testResolvePosix(&[_][]const u8{ "a", "../../.." }, "../.."); |
| | 1185 | try testResolvePosix(&[_][]const u8{ "..", "", "../../foo" }, "../../../foo"); |
| 779 | try testResolvePosix(&[_][]const u8{ "a/b/c/", "../../.." }, "."); | 1186 | try testResolvePosix(&[_][]const u8{ "a/b/c/", "../../.." }, "."); |
| 780 | try testResolvePosix(&[_][]const u8{"."}, "."); | 1187 | try testResolvePosix(&[_][]const u8{"."}, "."); |
| 781 | try testResolvePosix(&[_][]const u8{""}, "."); | 1188 | try testResolvePosix(&[_][]const u8{""}, "."); |
| ... | @@ -792,22 +1199,81 @@ test resolveWindows { | ... | @@ -792,22 +1199,81 @@ test resolveWindows { |
| 792 | ); | 1199 | ); |
| 793 | | 1200 | |
| 794 | try testResolveWindows(&[_][]const u8{ "c:\\a\\b\\c", "/hi", "ok" }, "C:\\hi\\ok"); | 1201 | try testResolveWindows(&[_][]const u8{ "c:\\a\\b\\c", "/hi", "ok" }, "C:\\hi\\ok"); |
| | 1202 | try testResolveWindows(&[_][]const u8{ "c:\\a\\b\\c\\", ".\\..\\foo" }, "C:\\a\\b\\foo"); |
| 795 | try testResolveWindows(&[_][]const u8{ "c:/blah\\blah", "d:/games", "c:../a" }, "C:\\blah\\a"); | 1203 | try testResolveWindows(&[_][]const u8{ "c:/blah\\blah", "d:/games", "c:../a" }, "C:\\blah\\a"); |
| 796 | try testResolveWindows(&[_][]const u8{ "c:/blah\\blah", "d:/games", "C:../a" }, "C:\\blah\\a"); | 1204 | try testResolveWindows(&[_][]const u8{ "c:/blah\\blah", "d:/games", "C:../a" }, "C:\\blah\\a"); |
| 797 | try testResolveWindows(&[_][]const u8{ "c:/ignore", "d:\\a/b\\c/d", "\\e.exe" }, "D:\\e.exe"); | 1205 | try testResolveWindows(&[_][]const u8{ "c:/ignore", "d:\\a/b\\c/d", "\\e.exe" }, "D:\\e.exe"); |
| 798 | try testResolveWindows(&[_][]const u8{ "c:/ignore", "c:/some/file" }, "C:\\some\\file"); | 1206 | try testResolveWindows(&[_][]const u8{ "c:/ignore", "c:/some/file" }, "C:\\some\\file"); |
| 799 | try testResolveWindows(&[_][]const u8{ "d:/ignore", "d:some/dir//" }, "D:\\ignore\\some\\dir"); | 1207 | // The first path "sets" the CWD, so the drive-relative path is then relative to that. |
| | 1208 | try testResolveWindows(&[_][]const u8{ "d:/foo", "d:some/dir//", "D:another" }, "D:\\foo\\some\\dir\\another"); |
| 800 | try testResolveWindows(&[_][]const u8{ "//server/share", "..", "relative\\" }, "\\\\server\\share\\relative"); | 1209 | try testResolveWindows(&[_][]const u8{ "//server/share", "..", "relative\\" }, "\\\\server\\share\\relative"); |
| 801 | try testResolveWindows(&[_][]const u8{ "\\\\server/share", "..", "relative\\" }, "\\\\server\\share\\relative"); | 1210 | try testResolveWindows(&[_][]const u8{ "\\\\server/share", "..", "relative\\" }, "\\\\server\\share\\relative"); |
| 802 | try testResolveWindows(&[_][]const u8{ "c:/", "//" }, "C:\\"); | 1211 | try testResolveWindows(&[_][]const u8{ "\\\\server/share/ignore", "//server/share/bar" }, "\\\\server\\share\\bar"); |
| 803 | try testResolveWindows(&[_][]const u8{ "c:/", "//dir" }, "C:\\dir"); | 1212 | try testResolveWindows(&[_][]const u8{ "\\/server\\share/", "..", "relative" }, "\\\\server\\share\\relative"); |
| 804 | try testResolveWindows(&[_][]const u8{ "c:/", "//server/share" }, "\\\\server\\share\\"); | 1213 | try testResolveWindows(&[_][]const u8{ "\\\\server\\share", "C:drive-relative" }, "C:drive-relative"); |
| 805 | try testResolveWindows(&[_][]const u8{ "c:/", "//server//share" }, "\\\\server\\share\\"); | 1214 | try testResolveWindows(&[_][]const u8{ "c:/", "//" }, "\\\\"); |
| 806 | try testResolveWindows(&[_][]const u8{ "c:/", "///some//dir" }, "C:\\some\\dir"); | 1215 | try testResolveWindows(&[_][]const u8{ "c:/", "//server" }, "\\\\server"); |
| | 1216 | try testResolveWindows(&[_][]const u8{ "c:/", "//server/share" }, "\\\\server\\share"); |
| | 1217 | try testResolveWindows(&[_][]const u8{ "c:/", "//server//share////" }, "\\\\server\\share\\"); |
| | 1218 | try testResolveWindows(&[_][]const u8{ "c:/", "///some//dir" }, "\\\\\\some\\dir"); |
| | 1219 | try testResolveWindows(&[_][]const u8{ "c:foo", "bar" }, "C:foo\\bar"); |
| 807 | try testResolveWindows(&[_][]const u8{ "C:\\foo\\tmp.3\\", "..\\tmp.3\\cycles\\root.js" }, "C:\\foo\\tmp.3\\cycles\\root.js"); | 1220 | try testResolveWindows(&[_][]const u8{ "C:\\foo\\tmp.3\\", "..\\tmp.3\\cycles\\root.js" }, "C:\\foo\\tmp.3\\cycles\\root.js"); |
| | 1221 | // Drive-relative stays drive-relative if there's nothing to provide the drive-specific CWD |
| | 1222 | try testResolveWindows(&[_][]const u8{ "relative", "d:foo" }, "D:foo"); |
| | 1223 | try testResolveWindows(&[_][]const u8{ "../..\\..", "d:foo" }, "D:foo"); |
| | 1224 | try testResolveWindows(&[_][]const u8{ "../..\\..", "\\rooted", "d:foo" }, "D:foo"); |
| | 1225 | try testResolveWindows(&[_][]const u8{ "C:\\foo", "../..\\..", "\\rooted", "d:foo" }, "D:foo"); |
| | 1226 | try testResolveWindows(&[_][]const u8{ "D:relevant", "../..\\..", "d:foo" }, "D:..\\..\\foo"); |
| | 1227 | try testResolveWindows(&[_][]const u8{ "D:relevant", "../..\\..", "\\\\.\\ignored", "C:\\ignored", "C:ignored", "\\\\ignored", "d:foo" }, "D:..\\..\\foo"); |
| | 1228 | try testResolveWindows(&[_][]const u8{ "ignored", "\\\\.\\ignored", "C:\\ignored", "C:ignored", "\\\\ignored", "d:foo" }, "D:foo"); |
| | 1229 | // Rooted paths remain rooted if there's no absolute path available to resolve the "root" |
| | 1230 | try testResolveWindows(&[_][]const u8{ "/foo", "bar" }, "\\foo\\bar"); |
| | 1231 | // Rooted against a UNC path |
| | 1232 | try testResolveWindows(&[_][]const u8{ "//server/share/ignore", "/foo", "bar" }, "\\\\server\\share\\foo\\bar"); |
| | 1233 | try testResolveWindows(&[_][]const u8{ "//server/share/", "/foo" }, "\\\\server\\share\\foo"); |
| | 1234 | try testResolveWindows(&[_][]const u8{ "//server/share", "/foo" }, "\\\\server\\share\\foo"); |
| | 1235 | try testResolveWindows(&[_][]const u8{ "//server/", "/foo" }, "\\\\server\\foo"); |
| | 1236 | try testResolveWindows(&[_][]const u8{ "//server", "/foo" }, "\\\\server\\foo"); |
| | 1237 | try testResolveWindows(&[_][]const u8{ "//", "/foo" }, "\\\\foo"); |
| | 1238 | // Rooted against a drive-relative path |
| | 1239 | try testResolveWindows(&[_][]const u8{ "C:", "/foo", "bar" }, "C:\\foo\\bar"); |
| | 1240 | try testResolveWindows(&[_][]const u8{ "C:\\ignore", "C:", "/foo", "bar" }, "C:\\foo\\bar"); |
| | 1241 | try testResolveWindows(&[_][]const u8{ "C:\\ignore", "\\foo", "C:bar" }, "C:\\foo\\bar"); |
| | 1242 | // Only the last rooted path is relevant |
| | 1243 | try testResolveWindows(&[_][]const u8{ "\\ignore", "\\foo" }, "\\foo"); |
| | 1244 | try testResolveWindows(&[_][]const u8{ "c:ignore", "ignore", "\\ignore", "\\foo" }, "C:\\foo"); |
| | 1245 | // Rooted is only relevant to a drive-relative if there's a previous drive-* path |
| | 1246 | try testResolveWindows(&[_][]const u8{ "\\ignore", "C:foo" }, "C:foo"); |
| | 1247 | try testResolveWindows(&[_][]const u8{ "\\ignore", "\\ignore2", "C:foo" }, "C:foo"); |
| | 1248 | try testResolveWindows(&[_][]const u8{ "c:ignore", "\\ignore", "\\rooted", "C:foo" }, "C:\\rooted\\foo"); |
| | 1249 | try testResolveWindows(&[_][]const u8{ "c:\\ignore", "\\ignore", "\\rooted", "C:foo" }, "C:\\rooted\\foo"); |
| | 1250 | try testResolveWindows(&[_][]const u8{ "d:\\ignore", "\\ignore", "\\ignore2", "C:foo" }, "C:foo"); |
| | 1251 | // Root local device paths |
| | 1252 | try testResolveWindows(&[_][]const u8{"\\/."}, "\\\\."); |
| | 1253 | try testResolveWindows(&[_][]const u8{ "\\/.", "C:drive-relative" }, "C:drive-relative"); |
| | 1254 | try testResolveWindows(&[_][]const u8{"/\\?"}, "\\\\?"); |
| | 1255 | try testResolveWindows(&[_][]const u8{ "ignore", "c:\\ignore", "\\\\.", "foo" }, "\\\\.\\foo"); |
| | 1256 | try testResolveWindows(&[_][]const u8{ "ignore", "c:\\ignore", "\\\\?", "foo" }, "\\\\?\\foo"); |
| | 1257 | try testResolveWindows(&[_][]const u8{ "ignore", "c:\\ignore", "//.", "ignore", "\\foo" }, "\\\\.\\foo"); |
| | 1258 | try testResolveWindows(&[_][]const u8{ "ignore", "c:\\ignore", "\\\\?", "ignore", "\\foo" }, "\\\\?\\foo"); |
| 808 | | 1259 | |
| 809 | // Keep relative paths relative. | 1260 | // Keep relative paths relative. |
| 810 | try testResolveWindows(&[_][]const u8{"a/b"}, "a\\b"); | 1261 | try testResolveWindows(&[_][]const u8{"a/b"}, "a\\b"); |
| | 1262 | try testResolveWindows(&[_][]const u8{".."}, ".."); |
| | 1263 | try testResolveWindows(&[_][]const u8{"../.."}, "..\\.."); |
| | 1264 | try testResolveWindows(&[_][]const u8{ "C:foo", "../.." }, "C:.."); |
| | 1265 | try testResolveWindows(&[_][]const u8{ "d:foo", "../..\\.." }, "D:..\\.."); |
| | 1266 | |
| | 1267 | // Local device paths treat the \\.\ or \\?\ as the "root", everything afterwards is treated as a regular component. |
| | 1268 | try testResolveWindows(&[_][]const u8{ "\\\\?\\C:\\foo", "../bar", "baz" }, "\\\\?\\C:\\bar\\baz"); |
| | 1269 | try testResolveWindows(&[_][]const u8{ "\\\\.\\C:/foo", "../../../../bar", "baz" }, "\\\\.\\bar\\baz"); |
| | 1270 | try testResolveWindows(&[_][]const u8{ "//./C:/foo", "../../../../bar", "baz" }, "\\\\.\\bar\\baz"); |
| | 1271 | try testResolveWindows(&[_][]const u8{ "\\\\.\\foo", ".." }, "\\\\."); |
| | 1272 | try testResolveWindows(&[_][]const u8{ "\\\\.\\foo", "..\\.." }, "\\\\."); |
| | 1273 | |
| | 1274 | // Paths are assumed to be Win32, so paths that are likely NT paths are treated as a rooted path. |
| | 1275 | try testResolveWindows(&[_][]const u8{ "\\??\\C:\\foo", "/bar", "baz" }, "\\bar\\baz"); |
| | 1276 | try testResolveWindows(&[_][]const u8{ "C:\\", "\\??\\C:\\foo", "bar" }, "C:\\??\\C:\\foo\\bar"); |
| 811 | } | 1277 | } |
| 812 | | 1278 | |
| 813 | test resolvePosix { | 1279 | test resolvePosix { |
| ... | @@ -855,63 +1321,18 @@ pub fn dirname(path: []const u8) ?[]const u8 { | ... | @@ -855,63 +1321,18 @@ pub fn dirname(path: []const u8) ?[]const u8 { |
| 855 | } | 1321 | } |
| 856 | | 1322 | |
| 857 | pub fn dirnameWindows(path: []const u8) ?[]const u8 { | 1323 | pub fn dirnameWindows(path: []const u8) ?[]const u8 { |
| 858 | if (path.len == 0) | 1324 | return dirnameInner(.windows, path); |
| 859 | return null; | | |
| 860 | | | |
| 861 | const root_slice = diskDesignatorWindows(path); | | |
| 862 | if (path.len == root_slice.len) | | |
| 863 | return null; | | |
| 864 | | | |
| 865 | const have_root_slash = path.len > root_slice.len and (path[root_slice.len] == '/' or path[root_slice.len] == '\\'); | | |
| 866 | | | |
| 867 | var end_index: usize = path.len - 1; | | |
| 868 | | | |
| 869 | while (path[end_index] == '/' or path[end_index] == '\\') { | | |
| 870 | if (end_index == 0) | | |
| 871 | return null; | | |
| 872 | end_index -= 1; | | |
| 873 | } | | |
| 874 | | | |
| 875 | while (path[end_index] != '/' and path[end_index] != '\\') { | | |
| 876 | if (end_index == 0) | | |
| 877 | return null; | | |
| 878 | end_index -= 1; | | |
| 879 | } | | |
| 880 | | | |
| 881 | if (have_root_slash and end_index == root_slice.len) { | | |
| 882 | end_index += 1; | | |
| 883 | } | | |
| 884 | | | |
| 885 | if (end_index == 0) | | |
| 886 | return null; | | |
| 887 | | | |
| 888 | return path[0..end_index]; | | |
| 889 | } | 1325 | } |
| 890 | | 1326 | |
| 891 | pub fn dirnamePosix(path: []const u8) ?[]const u8 { | 1327 | pub fn dirnamePosix(path: []const u8) ?[]const u8 { |
| 892 | if (path.len == 0) | 1328 | return dirnameInner(.posix, path); |
| 893 | return null; | 1329 | } |
| 894 | | | |
| 895 | var end_index: usize = path.len - 1; | | |
| 896 | while (path[end_index] == '/') { | | |
| 897 | if (end_index == 0) | | |
| 898 | return null; | | |
| 899 | end_index -= 1; | | |
| 900 | } | | |
| 901 | | | |
| 902 | while (path[end_index] != '/') { | | |
| 903 | if (end_index == 0) | | |
| 904 | return null; | | |
| 905 | end_index -= 1; | | |
| 906 | } | | |
| 907 | | | |
| 908 | if (end_index == 0 and path[0] == '/') | | |
| 909 | return path[0..1]; | | |
| 910 | | | |
| 911 | if (end_index == 0) | | |
| 912 | return null; | | |
| 913 | | 1330 | |
| 914 | return path[0..end_index]; | 1331 | fn dirnameInner(comptime path_type: PathType, path: []const u8) ?[]const u8 { |
| | 1332 | var it = ComponentIterator(path_type, u8).init(path); |
| | 1333 | _ = it.last() orelse return null; |
| | 1334 | const up = it.previous() orelse return it.root(); |
| | 1335 | return up.path; |
| 915 | } | 1336 | } |
| 916 | | 1337 | |
| 917 | test dirnamePosix { | 1338 | test dirnamePosix { |
| ... | @@ -930,11 +1351,12 @@ test dirnamePosix { | ... | @@ -930,11 +1351,12 @@ test dirnamePosix { |
| 930 | | 1351 | |
| 931 | test dirnameWindows { | 1352 | test dirnameWindows { |
| 932 | try testDirnameWindows("c:\\", null); | 1353 | try testDirnameWindows("c:\\", null); |
| | 1354 | try testDirnameWindows("c:\\\\", null); |
| 933 | try testDirnameWindows("c:\\foo", "c:\\"); | 1355 | try testDirnameWindows("c:\\foo", "c:\\"); |
| 934 | try testDirnameWindows("c:\\foo\\", "c:\\"); | 1356 | try testDirnameWindows("c:\\\\foo\\", "c:\\"); |
| 935 | try testDirnameWindows("c:\\foo\\bar", "c:\\foo"); | 1357 | try testDirnameWindows("c:\\foo\\bar", "c:\\foo"); |
| 936 | try testDirnameWindows("c:\\foo\\bar\\", "c:\\foo"); | 1358 | try testDirnameWindows("c:\\foo\\bar\\", "c:\\foo"); |
| 937 | try testDirnameWindows("c:\\foo\\bar\\baz", "c:\\foo\\bar"); | 1359 | try testDirnameWindows("c:\\\\foo\\bar\\baz", "c:\\\\foo\\bar"); |
| 938 | try testDirnameWindows("\\", null); | 1360 | try testDirnameWindows("\\", null); |
| 939 | try testDirnameWindows("\\foo", "\\"); | 1361 | try testDirnameWindows("\\foo", "\\"); |
| 940 | try testDirnameWindows("\\foo\\", "\\"); | 1362 | try testDirnameWindows("\\foo\\", "\\"); |
| ... | @@ -942,19 +1364,30 @@ test dirnameWindows { | ... | @@ -942,19 +1364,30 @@ test dirnameWindows { |
| 942 | try testDirnameWindows("\\foo\\bar\\", "\\foo"); | 1364 | try testDirnameWindows("\\foo\\bar\\", "\\foo"); |
| 943 | try testDirnameWindows("\\foo\\bar\\baz", "\\foo\\bar"); | 1365 | try testDirnameWindows("\\foo\\bar\\baz", "\\foo\\bar"); |
| 944 | try testDirnameWindows("c:", null); | 1366 | try testDirnameWindows("c:", null); |
| 945 | try testDirnameWindows("c:foo", null); | 1367 | try testDirnameWindows("c:foo", "c:"); |
| 946 | try testDirnameWindows("c:foo\\", null); | 1368 | try testDirnameWindows("c:foo\\", "c:"); |
| 947 | try testDirnameWindows("c:foo\\bar", "c:foo"); | 1369 | try testDirnameWindows("c:foo\\bar", "c:foo"); |
| 948 | try testDirnameWindows("c:foo\\bar\\", "c:foo"); | 1370 | try testDirnameWindows("c:foo\\bar\\", "c:foo"); |
| 949 | try testDirnameWindows("c:foo\\bar\\baz", "c:foo\\bar"); | 1371 | try testDirnameWindows("c:foo\\bar\\baz", "c:foo\\bar"); |
| 950 | try testDirnameWindows("file:stream", null); | 1372 | try testDirnameWindows("file:stream", null); |
| 951 | try testDirnameWindows("dir\\file:stream", "dir"); | 1373 | try testDirnameWindows("dir\\file:stream", "dir"); |
| 952 | try testDirnameWindows("\\\\unc\\share", null); | 1374 | try testDirnameWindows("\\\\unc\\share", null); |
| | 1375 | try testDirnameWindows("\\\\unc\\share\\\\", null); |
| 953 | try testDirnameWindows("\\\\unc\\share\\foo", "\\\\unc\\share\\"); | 1376 | try testDirnameWindows("\\\\unc\\share\\foo", "\\\\unc\\share\\"); |
| 954 | try testDirnameWindows("\\\\unc\\share\\foo\\", "\\\\unc\\share\\"); | 1377 | try testDirnameWindows("\\\\unc\\share\\foo\\", "\\\\unc\\share\\"); |
| 955 | try testDirnameWindows("\\\\unc\\share\\foo\\bar", "\\\\unc\\share\\foo"); | 1378 | try testDirnameWindows("\\\\unc\\share\\foo\\bar", "\\\\unc\\share\\foo"); |
| 956 | try testDirnameWindows("\\\\unc\\share\\foo\\bar\\", "\\\\unc\\share\\foo"); | 1379 | try testDirnameWindows("\\\\unc\\share\\foo\\bar\\", "\\\\unc\\share\\foo"); |
| 957 | try testDirnameWindows("\\\\unc\\share\\foo\\bar\\baz", "\\\\unc\\share\\foo\\bar"); | 1380 | try testDirnameWindows("\\\\unc\\share\\foo\\bar\\baz", "\\\\unc\\share\\foo\\bar"); |
| | 1381 | try testDirnameWindows("\\\\.", null); |
| | 1382 | try testDirnameWindows("\\\\.\\", null); |
| | 1383 | try testDirnameWindows("\\\\.\\device", "\\\\.\\"); |
| | 1384 | try testDirnameWindows("\\\\.\\device\\", "\\\\.\\"); |
| | 1385 | try testDirnameWindows("\\\\.\\device\\foo", "\\\\.\\device"); |
| | 1386 | try testDirnameWindows("\\\\?", null); |
| | 1387 | try testDirnameWindows("\\\\?\\", null); |
| | 1388 | try testDirnameWindows("\\\\?\\device", "\\\\?\\"); |
| | 1389 | try testDirnameWindows("\\\\?\\device\\", "\\\\?\\"); |
| | 1390 | try testDirnameWindows("\\\\?\\device\\foo", "\\\\?\\device"); |
| 958 | try testDirnameWindows("/a/b/", "/a"); | 1391 | try testDirnameWindows("/a/b/", "/a"); |
| 959 | try testDirnameWindows("/a/b", "/a"); | 1392 | try testDirnameWindows("/a/b", "/a"); |
| 960 | try testDirnameWindows("/a", "/"); | 1393 | try testDirnameWindows("/a", "/"); |
| ... | @@ -974,7 +1407,7 @@ fn testDirnamePosix(input: []const u8, expected_output: ?[]const u8) !void { | ... | @@ -974,7 +1407,7 @@ fn testDirnamePosix(input: []const u8, expected_output: ?[]const u8) !void { |
| 974 | | 1407 | |
| 975 | fn testDirnameWindows(input: []const u8, expected_output: ?[]const u8) !void { | 1408 | fn testDirnameWindows(input: []const u8, expected_output: ?[]const u8) !void { |
| 976 | if (dirnameWindows(input)) |output| { | 1409 | if (dirnameWindows(input)) |output| { |
| 977 | try testing.expect(mem.eql(u8, output, expected_output.?)); | 1410 | try testing.expectEqualStrings(expected_output.?, output); |
| 978 | } else { | 1411 | } else { |
| 979 | try testing.expect(expected_output == null); | 1412 | try testing.expect(expected_output == null); |
| 980 | } | 1413 | } |
| ... | @@ -989,56 +1422,17 @@ pub fn basename(path: []const u8) []const u8 { | ... | @@ -989,56 +1422,17 @@ pub fn basename(path: []const u8) []const u8 { |
| 989 | } | 1422 | } |
| 990 | | 1423 | |
| 991 | pub fn basenamePosix(path: []const u8) []const u8 { | 1424 | pub fn basenamePosix(path: []const u8) []const u8 { |
| 992 | if (path.len == 0) | 1425 | return basenameInner(.posix, path); |
| 993 | return &[_]u8{}; | | |
| 994 | | | |
| 995 | var end_index: usize = path.len - 1; | | |
| 996 | while (path[end_index] == '/') { | | |
| 997 | if (end_index == 0) | | |
| 998 | return &[_]u8{}; | | |
| 999 | end_index -= 1; | | |
| 1000 | } | | |
| 1001 | var start_index: usize = end_index; | | |
| 1002 | end_index += 1; | | |
| 1003 | while (path[start_index] != '/') { | | |
| 1004 | if (start_index == 0) | | |
| 1005 | return path[0..end_index]; | | |
| 1006 | start_index -= 1; | | |
| 1007 | } | | |
| 1008 | | | |
| 1009 | return path[start_index + 1 .. end_index]; | | |
| 1010 | } | 1426 | } |
| 1011 | | 1427 | |
| 1012 | pub fn basenameWindows(path: []const u8) []const u8 { | 1428 | pub fn basenameWindows(path: []const u8) []const u8 { |
| 1013 | if (path.len == 0) | 1429 | return basenameInner(.windows, path); |
| 1014 | return &[_]u8{}; | 1430 | } |
| 1015 | | | |
| 1016 | var end_index: usize = path.len - 1; | | |
| 1017 | while (true) { | | |
| 1018 | const byte = path[end_index]; | | |
| 1019 | if (byte == '/' or byte == '\\') { | | |
| 1020 | if (end_index == 0) | | |
| 1021 | return &[_]u8{}; | | |
| 1022 | end_index -= 1; | | |
| 1023 | continue; | | |
| 1024 | } | | |
| 1025 | if (byte == ':' and end_index == 1) { | | |
| 1026 | return &[_]u8{}; | | |
| 1027 | } | | |
| 1028 | break; | | |
| 1029 | } | | |
| 1030 | | | |
| 1031 | var start_index: usize = end_index; | | |
| 1032 | end_index += 1; | | |
| 1033 | while (path[start_index] != '/' and path[start_index] != '\\' and | | |
| 1034 | !(path[start_index] == ':' and start_index == 1)) | | |
| 1035 | { | | |
| 1036 | if (start_index == 0) | | |
| 1037 | return path[0..end_index]; | | |
| 1038 | start_index -= 1; | | |
| 1039 | } | | |
| 1040 | | 1431 | |
| 1041 | return path[start_index + 1 .. end_index]; | 1432 | fn basenameInner(comptime path_type: PathType, path: []const u8) []const u8 { |
| | 1433 | var it = ComponentIterator(path_type, u8).init(path); |
| | 1434 | const last = it.last() orelse return &[_]u8{}; |
| | 1435 | return last.name; |
| 1042 | } | 1436 | } |
| 1043 | | 1437 | |
| 1044 | test basename { | 1438 | test basename { |
| ... | @@ -1053,7 +1447,9 @@ test basename { | ... | @@ -1053,7 +1447,9 @@ test basename { |
| 1053 | try testBasename("/aaa/", "aaa"); | 1447 | try testBasename("/aaa/", "aaa"); |
| 1054 | try testBasename("/aaa/b", "b"); | 1448 | try testBasename("/aaa/b", "b"); |
| 1055 | try testBasename("/a/b", "b"); | 1449 | try testBasename("/a/b", "b"); |
| 1056 | try testBasename("//a", "a"); | 1450 | |
| | 1451 | // For Windows, this is a UNC path that only has a server name component. |
| | 1452 | try testBasename("//a", if (native_os == .windows) "" else "a"); |
| 1057 | | 1453 | |
| 1058 | try testBasenamePosix("\\dir\\basename.ext", "\\dir\\basename.ext"); | 1454 | try testBasenamePosix("\\dir\\basename.ext", "\\dir\\basename.ext"); |
| 1059 | try testBasenamePosix("\\basename.ext", "\\basename.ext"); | 1455 | try testBasenamePosix("\\basename.ext", "\\basename.ext"); |
| ... | @@ -1076,6 +1472,12 @@ test basename { | ... | @@ -1076,6 +1472,12 @@ test basename { |
| 1076 | try testBasenameWindows("C:basename.ext", "basename.ext"); | 1472 | try testBasenameWindows("C:basename.ext", "basename.ext"); |
| 1077 | try testBasenameWindows("C:basename.ext\\", "basename.ext"); | 1473 | try testBasenameWindows("C:basename.ext\\", "basename.ext"); |
| 1078 | try testBasenameWindows("C:basename.ext\\\\", "basename.ext"); | 1474 | try testBasenameWindows("C:basename.ext\\\\", "basename.ext"); |
| | 1475 | try testBasenameWindows("\\\\.", ""); |
| | 1476 | try testBasenameWindows("\\\\.\\", ""); |
| | 1477 | try testBasenameWindows("\\\\.\\basename.ext", "basename.ext"); |
| | 1478 | try testBasenameWindows("\\\\?", ""); |
| | 1479 | try testBasenameWindows("\\\\?\\", ""); |
| | 1480 | try testBasenameWindows("\\\\?\\basename.ext", "basename.ext"); |
| 1079 | try testBasenameWindows("C:foo", "foo"); | 1481 | try testBasenameWindows("C:foo", "foo"); |
| 1080 | try testBasenameWindows("file:stream", "file:stream"); | 1482 | try testBasenameWindows("file:stream", "file:stream"); |
| 1081 | } | 1483 | } |
| ... | @@ -1092,11 +1494,15 @@ fn testBasenameWindows(input: []const u8, expected_output: []const u8) !void { | ... | @@ -1092,11 +1494,15 @@ fn testBasenameWindows(input: []const u8, expected_output: []const u8) !void { |
| 1092 | try testing.expectEqualSlices(u8, expected_output, basenameWindows(input)); | 1494 | try testing.expectEqualSlices(u8, expected_output, basenameWindows(input)); |
| 1093 | } | 1495 | } |
| 1094 | | 1496 | |
| | 1497 | pub const RelativeError = std.process.GetCwdAllocError; |
| | 1498 | |
| 1095 | /// Returns the relative path from `from` to `to`. If `from` and `to` each | 1499 | /// Returns the relative path from `from` to `to`. If `from` and `to` each |
| 1096 | /// resolve to the same path (after calling `resolve` on each), a zero-length | 1500 | /// resolve to the same path (after calling `resolve` on each), a zero-length |
| 1097 | /// string is returned. | 1501 | /// string is returned. |
| 1098 | /// On Windows this canonicalizes the drive to a capital letter and paths to `\\`. | 1502 | /// On Windows, the result is not guaranteed to be relative, as the paths may be |
| 1099 | pub fn relative(allocator: Allocator, from: []const u8, to: []const u8) ![]u8 { | 1503 | /// on different volumes. In that case, the result will be the canonicalized absolute |
| | 1504 | /// path of `to`. |
| | 1505 | pub fn relative(allocator: Allocator, from: []const u8, to: []const u8) RelativeError![]u8 { |
| 1100 | if (native_os == .windows) { | 1506 | if (native_os == .windows) { |
| 1101 | return relativeWindows(allocator, from, to); | 1507 | return relativeWindows(allocator, from, to); |
| 1102 | } else { | 1508 | } else { |
| ... | @@ -1105,30 +1511,53 @@ pub fn relative(allocator: Allocator, from: []const u8, to: []const u8) ![]u8 { | ... | @@ -1105,30 +1511,53 @@ pub fn relative(allocator: Allocator, from: []const u8, to: []const u8) ![]u8 { |
| 1105 | } | 1511 | } |
| 1106 | | 1512 | |
| 1107 | pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) ![]u8 { | 1513 | pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) ![]u8 { |
| 1108 | const cwd = try process.getCwdAlloc(allocator); | 1514 | if (native_os != .windows) @compileError("this function relies on Windows-specific semantics"); |
| 1109 | defer allocator.free(cwd); | | |
| 1110 | const resolved_from = try resolveWindows(allocator, &[_][]const u8{ cwd, from }); | | |
| 1111 | defer allocator.free(resolved_from); | | |
| 1112 | | 1515 | |
| | 1516 | const parsed_from = parsePathWindows(u8, from); |
| | 1517 | const parsed_to = parsePathWindows(u8, to); |
| | 1518 | |
| | 1519 | const result_is_always_to = x: { |
| | 1520 | if (parsed_from.kind != parsed_to.kind) { |
| | 1521 | break :x false; |
| | 1522 | } |
| | 1523 | switch (parsed_from.kind) { |
| | 1524 | .drive_relative, .drive_absolute => { |
| | 1525 | break :x !compareDiskDesignators(u8, .drive, parsed_from.root, parsed_to.root); |
| | 1526 | }, |
| | 1527 | .unc_absolute => { |
| | 1528 | break :x !compareDiskDesignators(u8, .unc, parsed_from.root, parsed_to.root); |
| | 1529 | }, |
| | 1530 | .relative, .rooted, .local_device => break :x false, |
| | 1531 | .root_local_device => break :x true, |
| | 1532 | } |
| | 1533 | }; |
| | 1534 | |
| | 1535 | if (result_is_always_to) { |
| | 1536 | return windowsResolveAgainstCwd(allocator, to, parsed_to); |
| | 1537 | } |
| | 1538 | |
| | 1539 | const resolved_from = try windowsResolveAgainstCwd(allocator, from, parsed_from); |
| | 1540 | defer allocator.free(resolved_from); |
| 1113 | var clean_up_resolved_to = true; | 1541 | var clean_up_resolved_to = true; |
| 1114 | const resolved_to = try resolveWindows(allocator, &[_][]const u8{ cwd, to }); | 1542 | const resolved_to = try windowsResolveAgainstCwd(allocator, to, parsed_to); |
| 1115 | defer if (clean_up_resolved_to) allocator.free(resolved_to); | 1543 | defer if (clean_up_resolved_to) allocator.free(resolved_to); |
| 1116 | | 1544 | |
| 1117 | const parsed_from = windowsParsePath(resolved_from); | 1545 | const parsed_resolved_from = parsePathWindows(u8, resolved_from); |
| 1118 | const parsed_to = windowsParsePath(resolved_to); | 1546 | const parsed_resolved_to = parsePathWindows(u8, resolved_to); |
| | 1547 | |
| 1119 | const result_is_to = x: { | 1548 | const result_is_to = x: { |
| 1120 | if (parsed_from.kind != parsed_to.kind) { | 1549 | if (parsed_resolved_from.kind != parsed_resolved_to.kind) { |
| 1121 | break :x true; | 1550 | break :x true; |
| 1122 | } else switch (parsed_from.kind) { | 1551 | } |
| 1123 | .NetworkShare => { | 1552 | switch (parsed_resolved_from.kind) { |
| 1124 | break :x !networkShareServersEql(parsed_to.disk_designator, parsed_from.disk_designator); | 1553 | .drive_absolute, .drive_relative => { |
| 1125 | }, | 1554 | break :x !compareDiskDesignators(u8, .drive, parsed_resolved_from.root, parsed_resolved_to.root); |
| 1126 | .Drive => { | | |
| 1127 | break :x ascii.toUpper(parsed_from.disk_designator[0]) != ascii.toUpper(parsed_to.disk_designator[0]); | | |
| 1128 | }, | 1555 | }, |
| 1129 | .None => { | 1556 | .unc_absolute => { |
| 1130 | break :x false; | 1557 | break :x !compareDiskDesignators(u8, .unc, parsed_resolved_from.root, parsed_resolved_to.root); |
| 1131 | }, | 1558 | }, |
| | 1559 | .relative, .rooted, .local_device => break :x false, |
| | 1560 | .root_local_device => break :x true, |
| 1132 | } | 1561 | } |
| 1133 | }; | 1562 | }; |
| 1134 | | 1563 | |
| ... | @@ -1137,8 +1566,8 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) ! | ... | @@ -1137,8 +1566,8 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) ! |
| 1137 | return resolved_to; | 1566 | return resolved_to; |
| 1138 | } | 1567 | } |
| 1139 | | 1568 | |
| 1140 | var from_it = mem.tokenizeAny(u8, resolved_from, "/\\"); | 1569 | var from_it = mem.tokenizeAny(u8, resolved_from[parsed_resolved_from.root.len..], "/\\"); |
| 1141 | var to_it = mem.tokenizeAny(u8, resolved_to, "/\\"); | 1570 | var to_it = mem.tokenizeAny(u8, resolved_to[parsed_resolved_to.root.len..], "/\\"); |
| 1142 | while (true) { | 1571 | while (true) { |
| 1143 | const from_component = from_it.next() orelse return allocator.dupe(u8, to_it.rest()); | 1572 | const from_component = from_it.next() orelse return allocator.dupe(u8, to_it.rest()); |
| 1144 | const to_rest = to_it.rest(); | 1573 | const to_rest = to_it.rest(); |
| ... | @@ -1170,11 +1599,101 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) ! | ... | @@ -1170,11 +1599,101 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) ! |
| 1170 | | 1599 | |
| 1171 | return allocator.realloc(result, result_index); | 1600 | return allocator.realloc(result, result_index); |
| 1172 | } | 1601 | } |
| 1173 | | | |
| 1174 | return [_]u8{}; | 1602 | return [_]u8{}; |
| 1175 | } | 1603 | } |
| 1176 | | 1604 | |
| | 1605 | fn windowsResolveAgainstCwd(allocator: Allocator, path: []const u8, parsed: WindowsPath2(u8)) ![]u8 { |
| | 1606 | // Space for 256 WTF-16 code units; potentially 3 WTF-8 bytes per WTF-16 code unit |
| | 1607 | var temp_allocator_state = std.heap.stackFallback(256 * 3, allocator); |
| | 1608 | return switch (parsed.kind) { |
| | 1609 | .drive_absolute, |
| | 1610 | .unc_absolute, |
| | 1611 | .root_local_device, |
| | 1612 | .local_device, |
| | 1613 | => try resolveWindows(allocator, &.{path}), |
| | 1614 | .relative => blk: { |
| | 1615 | const temp_allocator = temp_allocator_state.get(); |
| | 1616 | |
| | 1617 | const peb_cwd = windows.peb().ProcessParameters.CurrentDirectory.DosPath; |
| | 1618 | const cwd_w = (peb_cwd.Buffer.?)[0 .. peb_cwd.Length / 2]; |
| | 1619 | |
| | 1620 | const wtf8_len = std.unicode.calcWtf8Len(cwd_w); |
| | 1621 | const wtf8_buf = try temp_allocator.alloc(u8, wtf8_len); |
| | 1622 | defer temp_allocator.free(wtf8_buf); |
| | 1623 | assert(std.unicode.wtf16LeToWtf8(wtf8_buf, cwd_w) == wtf8_len); |
| | 1624 | |
| | 1625 | break :blk try resolveWindows(allocator, &.{ wtf8_buf, path }); |
| | 1626 | }, |
| | 1627 | .rooted => blk: { |
| | 1628 | const peb_cwd = windows.peb().ProcessParameters.CurrentDirectory.DosPath; |
| | 1629 | const cwd_w = (peb_cwd.Buffer.?)[0 .. peb_cwd.Length / 2]; |
| | 1630 | const parsed_cwd = parsePathWindows(u16, cwd_w); |
| | 1631 | switch (parsed_cwd.kind) { |
| | 1632 | .drive_absolute => { |
| | 1633 | var drive_buf = "_:\\".*; |
| | 1634 | drive_buf[0] = @truncate(cwd_w[0]); |
| | 1635 | break :blk try resolveWindows(allocator, &.{ &drive_buf, path }); |
| | 1636 | }, |
| | 1637 | .unc_absolute => { |
| | 1638 | const temp_allocator = temp_allocator_state.get(); |
| | 1639 | var root_buf = try temp_allocator.alloc(u8, parsed_cwd.root.len * 3); |
| | 1640 | defer temp_allocator.free(root_buf); |
| | 1641 | |
| | 1642 | const wtf8_len = std.unicode.wtf16LeToWtf8(root_buf, parsed_cwd.root); |
| | 1643 | const root = root_buf[0..wtf8_len]; |
| | 1644 | break :blk try resolveWindows(allocator, &.{ root, path }); |
| | 1645 | }, |
| | 1646 | // Effectively a malformed CWD, give up and just return a normalized path |
| | 1647 | else => break :blk try resolveWindows(allocator, &.{path}), |
| | 1648 | } |
| | 1649 | }, |
| | 1650 | .drive_relative => blk: { |
| | 1651 | const temp_allocator = temp_allocator_state.get(); |
| | 1652 | const drive_cwd = drive_cwd: { |
| | 1653 | const peb_cwd = windows.peb().ProcessParameters.CurrentDirectory.DosPath; |
| | 1654 | const cwd_w = (peb_cwd.Buffer.?)[0 .. peb_cwd.Length / 2]; |
| | 1655 | const parsed_cwd = parsePathWindows(u16, cwd_w); |
| | 1656 | |
| | 1657 | if (parsed_cwd.kind == .drive_absolute) { |
| | 1658 | const drive_letter_w = parsed_cwd.root[0]; |
| | 1659 | const drive_letters_match = drive_letter_w <= 0x7F and |
| | 1660 | ascii.toUpper(@intCast(drive_letter_w)) == ascii.toUpper(parsed.root[0]); |
| | 1661 | if (drive_letters_match) { |
| | 1662 | const wtf8_len = std.unicode.calcWtf8Len(cwd_w); |
| | 1663 | const wtf8_buf = try temp_allocator.alloc(u8, wtf8_len); |
| | 1664 | assert(std.unicode.wtf16LeToWtf8(wtf8_buf, cwd_w) == wtf8_len); |
| | 1665 | break :drive_cwd wtf8_buf[0..]; |
| | 1666 | } |
| | 1667 | |
| | 1668 | // Per-drive CWD's are stored in special semi-hidden environment variables |
| | 1669 | // of the format `=<drive-letter>:`, e.g. `=C:`. This type of CWD is |
| | 1670 | // purely a shell concept, so there's no guarantee that it'll be set |
| | 1671 | // or that it'll even be accurate. |
| | 1672 | var key_buf = std.unicode.wtf8ToWtf16LeStringLiteral("=_:").*; |
| | 1673 | key_buf[1] = parsed.root[0]; |
| | 1674 | if (std.process.getenvW(&key_buf)) |drive_cwd_w| { |
| | 1675 | const wtf8_len = std.unicode.calcWtf8Len(drive_cwd_w); |
| | 1676 | const wtf8_buf = try temp_allocator.alloc(u8, wtf8_len); |
| | 1677 | assert(std.unicode.wtf16LeToWtf8(wtf8_buf, drive_cwd_w) == wtf8_len); |
| | 1678 | break :drive_cwd wtf8_buf[0..]; |
| | 1679 | } |
| | 1680 | } |
| | 1681 | |
| | 1682 | const drive_buf = try temp_allocator.alloc(u8, 3); |
| | 1683 | drive_buf[0] = parsed.root[0]; |
| | 1684 | drive_buf[1] = ':'; |
| | 1685 | drive_buf[2] = '\\'; |
| | 1686 | break :drive_cwd drive_buf; |
| | 1687 | }; |
| | 1688 | defer temp_allocator.free(drive_cwd); |
| | 1689 | break :blk try resolveWindows(allocator, &.{ drive_cwd, path }); |
| | 1690 | }, |
| | 1691 | }; |
| | 1692 | } |
| | 1693 | |
| 1177 | pub fn relativePosix(allocator: Allocator, from: []const u8, to: []const u8) ![]u8 { | 1694 | pub fn relativePosix(allocator: Allocator, from: []const u8, to: []const u8) ![]u8 { |
| | 1695 | if (native_os == .windows) @compileError("this function relies on semantics that do not apply to Windows"); |
| | 1696 | |
| 1178 | const cwd = try process.getCwdAlloc(allocator); | 1697 | const cwd = try process.getCwdAlloc(allocator); |
| 1179 | defer allocator.free(cwd); | 1698 | defer allocator.free(cwd); |
| 1180 | const resolved_from = try resolvePosix(allocator, &[_][]const u8{ cwd, from }); | 1699 | const resolved_from = try resolvePosix(allocator, &[_][]const u8{ cwd, from }); |
| ... | @@ -1217,51 +1736,59 @@ pub fn relativePosix(allocator: Allocator, from: []const u8, to: []const u8) ![] | ... | @@ -1217,51 +1736,59 @@ pub fn relativePosix(allocator: Allocator, from: []const u8, to: []const u8) ![] |
| 1217 | } | 1736 | } |
| 1218 | | 1737 | |
| 1219 | test relative { | 1738 | test relative { |
| 1220 | try testRelativeWindows("c:/blah\\blah", "d:/games", "D:\\games"); | 1739 | if (native_os == .windows) { |
| 1221 | try testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa", ".."); | 1740 | try testRelativeWindows("c:/blah\\blah", "d:/games", "D:\\games"); |
| 1222 | try testRelativeWindows("c:/aaaa/bbbb", "c:/cccc", "..\\..\\cccc"); | 1741 | try testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa", ".."); |
| 1223 | try testRelativeWindows("c:/aaaa/bbbb", "C:/aaaa/bbbb", ""); | 1742 | try testRelativeWindows("c:/aaaa/bbbb", "c:/cccc", "..\\..\\cccc"); |
| 1224 | try testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa/cccc", "..\\cccc"); | 1743 | try testRelativeWindows("c:/aaaa/bbbb", "C:/aaaa/bbbb", ""); |
| 1225 | try testRelativeWindows("c:/aaaa/", "c:/aaaa/cccc", "cccc"); | 1744 | try testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa/cccc", "..\\cccc"); |
| 1226 | try testRelativeWindows("c:/", "c:\\aaaa\\bbbb", "aaaa\\bbbb"); | 1745 | try testRelativeWindows("c:/aaaa/", "c:/aaaa/cccc", "cccc"); |
| 1227 | try testRelativeWindows("c:/aaaa/bbbb", "d:\\", "D:\\"); | 1746 | try testRelativeWindows("c:/", "c:\\aaaa\\bbbb", "aaaa\\bbbb"); |
| 1228 | try testRelativeWindows("c:/AaAa/bbbb", "c:/aaaa/bbbb", ""); | 1747 | try testRelativeWindows("c:/aaaa/bbbb", "d:\\", "D:\\"); |
| 1229 | try testRelativeWindows("c:/aaaaa/", "c:/aaaa/cccc", "..\\aaaa\\cccc"); | 1748 | try testRelativeWindows("c:/AaAa/bbbb", "c:/aaaa/bbbb", ""); |
| 1230 | try testRelativeWindows("C:\\foo\\bar\\baz\\quux", "C:\\", "..\\..\\..\\.."); | 1749 | try testRelativeWindows("c:/aaaaa/", "c:/aaaa/cccc", "..\\aaaa\\cccc"); |
| 1231 | try testRelativeWindows("C:\\foo\\test", "C:\\foo\\test\\bar\\package.json", "bar\\package.json"); | 1750 | try testRelativeWindows("C:\\foo\\bar\\baz\\quux", "C:\\", "..\\..\\..\\.."); |
| 1232 | try testRelativeWindows("C:\\foo\\bar\\baz-quux", "C:\\foo\\bar\\baz", "..\\baz"); | 1751 | try testRelativeWindows("C:\\foo\\test", "C:\\foo\\test\\bar\\package.json", "bar\\package.json"); |
| 1233 | try testRelativeWindows("C:\\foo\\bar\\baz", "C:\\foo\\bar\\baz-quux", "..\\baz-quux"); | 1752 | try testRelativeWindows("C:\\foo\\bar\\baz-quux", "C:\\foo\\bar\\baz", "..\\baz"); |
| 1234 | try testRelativeWindows("\\\\foo\\bar", "\\\\foo\\bar\\baz", "baz"); | 1753 | try testRelativeWindows("C:\\foo\\bar\\baz", "C:\\foo\\bar\\baz-quux", "..\\baz-quux"); |
| 1235 | try testRelativeWindows("\\\\foo\\bar\\baz", "\\\\foo\\bar", ".."); | 1754 | try testRelativeWindows("\\\\foo\\bar", "\\\\foo\\bar\\baz", "baz"); |
| 1236 | try testRelativeWindows("\\\\foo\\bar\\baz-quux", "\\\\foo\\bar\\baz", "..\\baz"); | 1755 | try testRelativeWindows("\\\\foo\\bar\\baz", "\\\\foo\\bar", ".."); |
| 1237 | try testRelativeWindows("\\\\foo\\bar\\baz", "\\\\foo\\bar\\baz-quux", "..\\baz-quux"); | 1756 | try testRelativeWindows("\\\\foo\\bar\\baz-quux", "\\\\foo\\bar\\baz", "..\\baz"); |
| 1238 | try testRelativeWindows("C:\\baz-quux", "C:\\baz", "..\\baz"); | 1757 | try testRelativeWindows("\\\\foo/bar\\baz-quux", "//foo\\bar/baz", "..\\baz"); |
| 1239 | try testRelativeWindows("C:\\baz", "C:\\baz-quux", "..\\baz-quux"); | 1758 | try testRelativeWindows("\\\\foo\\bar\\baz", "\\\\foo\\bar\\baz-quux", "..\\baz-quux"); |
| 1240 | try testRelativeWindows("\\\\foo\\baz-quux", "\\\\foo\\baz", "..\\baz"); | 1759 | try testRelativeWindows("C:\\baz-quux", "C:\\baz", "..\\baz"); |
| 1241 | try testRelativeWindows("\\\\foo\\baz", "\\\\foo\\baz-quux", "..\\baz-quux"); | 1760 | try testRelativeWindows("C:\\baz", "C:\\baz-quux", "..\\baz-quux"); |
| 1242 | try testRelativeWindows("C:\\baz", "\\\\foo\\bar\\baz", "\\\\foo\\bar\\baz"); | 1761 | try testRelativeWindows("\\\\foo\\baz-quux", "\\\\foo\\baz", "\\\\foo\\baz"); |
| 1243 | try testRelativeWindows("\\\\foo\\bar\\baz", "C:\\baz", "C:\\baz"); | 1762 | try testRelativeWindows("\\\\foo\\baz", "\\\\foo\\baz-quux", "\\\\foo\\baz-quux"); |
| 1244 | | 1763 | try testRelativeWindows("C:\\baz", "\\\\foo\\bar\\baz", "\\\\foo\\bar\\baz"); |
| 1245 | try testRelativeWindows("a/b/c", "a\\b", ".."); | 1764 | try testRelativeWindows("\\\\foo\\bar\\baz", "C:\\baz", "C:\\baz"); |
| 1246 | try testRelativeWindows("a/b/c", "a", "..\\.."); | 1765 | |
| 1247 | try testRelativeWindows("a/b/c", "a\\b\\c\\d", "d"); | 1766 | try testRelativeWindows("c:blah\\blah", "c:foo", "..\\..\\foo"); |
| 1248 | | 1767 | try testRelativeWindows("c:foo", "c:foo\\bar", "bar"); |
| 1249 | try testRelativeWindows("\\\\FOO\\bar\\baz", "\\\\foo\\BAR\\BAZ", ""); | 1768 | try testRelativeWindows("\\blah\\blah", "\\foo", "..\\..\\foo"); |
| 1250 | // Unicode-aware case-insensitive path comparison | 1769 | try testRelativeWindows("\\foo", "\\foo\\bar", "bar"); |
| 1251 | try testRelativeWindows("\\\\кириллица\\ελληνικά\\português", "\\\\КИРИЛЛИЦА\\ΕΛΛΗΝΙΚΆ\\PORTUGUÊS", ""); | 1770 | |
| 1252 | | 1771 | try testRelativeWindows("a/b/c", "a\\b", ".."); |
| 1253 | try testRelativePosix("/var/lib", "/var", ".."); | 1772 | try testRelativeWindows("a/b/c", "a", "..\\.."); |
| 1254 | try testRelativePosix("/var/lib", "/bin", "../../bin"); | 1773 | try testRelativeWindows("a/b/c", "a\\b\\c\\d", "d"); |
| 1255 | try testRelativePosix("/var/lib", "/var/lib", ""); | 1774 | |
| 1256 | try testRelativePosix("/var/lib", "/var/apache", "../apache"); | 1775 | try testRelativeWindows("\\\\FOO\\bar\\baz", "\\\\foo\\BAR\\BAZ", ""); |
| 1257 | try testRelativePosix("/var/", "/var/lib", "lib"); | 1776 | // Unicode-aware case-insensitive path comparison |
| 1258 | try testRelativePosix("/", "/var/lib", "var/lib"); | 1777 | try testRelativeWindows("\\\\кириллица\\ελληνικά\\português", "\\\\КИРИЛЛИЦА\\ΕΛΛΗΝΙΚΆ\\PORTUGUÊS", ""); |
| 1259 | try testRelativePosix("/foo/test", "/foo/test/bar/package.json", "bar/package.json"); | 1778 | } else { |
| 1260 | try testRelativePosix("/Users/a/web/b/test/mails", "/Users/a/web/b", "../.."); | 1779 | try testRelativePosix("/var/lib", "/var", ".."); |
| 1261 | try testRelativePosix("/foo/bar/baz-quux", "/foo/bar/baz", "../baz"); | 1780 | try testRelativePosix("/var/lib", "/bin", "../../bin"); |
| 1262 | try testRelativePosix("/foo/bar/baz", "/foo/bar/baz-quux", "../baz-quux"); | 1781 | try testRelativePosix("/var/lib", "/var/lib", ""); |
| 1263 | try testRelativePosix("/baz-quux", "/baz", "../baz"); | 1782 | try testRelativePosix("/var/lib", "/var/apache", "../apache"); |
| 1264 | try testRelativePosix("/baz", "/baz-quux", "../baz-quux"); | 1783 | try testRelativePosix("/var/", "/var/lib", "lib"); |
| | 1784 | try testRelativePosix("/", "/var/lib", "var/lib"); |
| | 1785 | try testRelativePosix("/foo/test", "/foo/test/bar/package.json", "bar/package.json"); |
| | 1786 | try testRelativePosix("/Users/a/web/b/test/mails", "/Users/a/web/b", "../.."); |
| | 1787 | try testRelativePosix("/foo/bar/baz-quux", "/foo/bar/baz", "../baz"); |
| | 1788 | try testRelativePosix("/foo/bar/baz", "/foo/bar/baz-quux", "../baz-quux"); |
| | 1789 | try testRelativePosix("/baz-quux", "/baz", "../baz"); |
| | 1790 | try testRelativePosix("/baz", "/baz-quux", "../baz-quux"); |
| | 1791 | } |
| 1265 | } | 1792 | } |
| 1266 | | 1793 | |
| 1267 | fn testRelativePosix(from: []const u8, to: []const u8, expected_output: []const u8) !void { | 1794 | fn testRelativePosix(from: []const u8, to: []const u8, expected_output: []const u8) !void { |
| ... | @@ -1391,7 +1918,10 @@ test stem { | ... | @@ -1391,7 +1918,10 @@ test stem { |
| 1391 | pub fn ComponentIterator(comptime path_type: PathType, comptime T: type) type { | 1918 | pub fn ComponentIterator(comptime path_type: PathType, comptime T: type) type { |
| 1392 | return struct { | 1919 | return struct { |
| 1393 | path: []const T, | 1920 | path: []const T, |
| 1394 | root_end_index: usize = 0, | 1921 | /// Length of the root with at most one trailing path separator included (e.g. `C:/`). |
| | 1922 | root_len: usize, |
| | 1923 | /// Length of the root with all trailing path separators included (e.g. `C://///`). |
| | 1924 | root_end_index: usize, |
| 1395 | start_index: usize = 0, | 1925 | start_index: usize = 0, |
| 1396 | end_index: usize = 0, | 1926 | end_index: usize = 0, |
| 1397 | | 1927 | |
| ... | @@ -1406,100 +1936,45 @@ pub fn ComponentIterator(comptime path_type: PathType, comptime T: type) type { | ... | @@ -1406,100 +1936,45 @@ pub fn ComponentIterator(comptime path_type: PathType, comptime T: type) type { |
| 1406 | path: []const T, | 1936 | path: []const T, |
| 1407 | }; | 1937 | }; |
| 1408 | | 1938 | |
| 1409 | const InitError = switch (path_type) { | | |
| 1410 | .windows => error{BadPathName}, | | |
| 1411 | else => error{}, | | |
| 1412 | }; | | |
| 1413 | | | |
| 1414 | /// After `init`, `next` will return the first component after the root | 1939 | /// After `init`, `next` will return the first component after the root |
| 1415 | /// (there is no need to call `first` after `init`). | 1940 | /// (there is no need to call `first` after `init`). |
| 1416 | /// To iterate backwards (from the end of the path to the beginning), call `last` | 1941 | /// To iterate backwards (from the end of the path to the beginning), call `last` |
| 1417 | /// after `init` and then iterate via `previous` calls. | 1942 | /// after `init` and then iterate via `previous` calls. |
| 1418 | /// For Windows paths, `error.BadPathName` is returned if the `path` has an explicit | 1943 | /// For Windows paths, paths are assumed to be in the Win32 namespace. |
| 1419 | /// namespace prefix (`\\.\`, `\\?\`, or `\??\`) or if it is a UNC path with more | 1944 | pub fn init(path: []const T) Self { |
| 1420 | /// than two path separators at the beginning. | 1945 | const root_len: usize = switch (path_type) { |
| 1421 | pub fn init(path: []const T) InitError!Self { | | |
| 1422 | const root_end_index: usize = switch (path_type) { | | |
| 1423 | .posix, .uefi => posix: { | 1946 | .posix, .uefi => posix: { |
| 1424 | // Root on UEFI and POSIX only differs by the path separator | 1947 | // Root on UEFI and POSIX only differs by the path separator |
| 1425 | var root_end_index: usize = 0; | 1948 | break :posix if (path.len > 0 and path_type.isSep(T, path[0])) 1 else 0; |
| 1426 | while (true) : (root_end_index += 1) { | | |
| 1427 | if (root_end_index >= path.len or !path_type.isSep(T, path[root_end_index])) { | | |
| 1428 | break; | | |
| 1429 | } | | |
| 1430 | } | | |
| 1431 | break :posix root_end_index; | | |
| 1432 | }, | 1949 | }, |
| 1433 | .windows => windows: { | 1950 | .windows => windows: { |
| 1434 | // Namespaces other than the Win32 file namespace are tricky | 1951 | break :windows parsePathWindows(T, path).root.len; |
| 1435 | // and basically impossible to determine a 'root' for, since it's | | |
| 1436 | // possible to construct an effectively arbitrarily long 'root', | | |
| 1437 | // e.g. `\\.\GLOBALROOT\??\UNC\localhost\C$\foo` is a | | |
| 1438 | // possible path that would be effectively equivalent to | | |
| 1439 | // `C:\foo`, and the `GLOBALROOT\??\` part can also be recursive, | | |
| 1440 | // so `GLOBALROOT\??\GLOBALROOT\??\...` would work for any number | | |
| 1441 | // of repetitions. Therefore, paths with an explicit namespace prefix | | |
| 1442 | // (\\.\, \??\, \\?\) are not allowed here. | | |
| 1443 | if (std.os.windows.getNamespacePrefix(T, path) != .none) { | | |
| 1444 | return error.BadPathName; | | |
| 1445 | } | | |
| 1446 | const windows_path_type = std.os.windows.getUnprefixedPathType(T, path); | | |
| 1447 | break :windows switch (windows_path_type) { | | |
| 1448 | .relative => 0, | | |
| 1449 | .root_local_device => path.len, | | |
| 1450 | .rooted => 1, | | |
| 1451 | .unc_absolute => unc: { | | |
| 1452 | var end_index: usize = 2; | | |
| 1453 | // Any extra separators between the first two and the server name are not allowed | | |
| 1454 | // and will always lead to STATUS_OBJECT_PATH_INVALID if it is attempted | | |
| 1455 | // to be used. | | |
| 1456 | if (end_index < path.len and path_type.isSep(T, path[end_index])) { | | |
| 1457 | return error.BadPathName; | | |
| 1458 | } | | |
| 1459 | // Server | | |
| 1460 | while (end_index < path.len and !path_type.isSep(T, path[end_index])) { | | |
| 1461 | end_index += 1; | | |
| 1462 | } | | |
| 1463 | // Slash(es) after server | | |
| 1464 | while (end_index < path.len and path_type.isSep(T, path[end_index])) { | | |
| 1465 | end_index += 1; | | |
| 1466 | } | | |
| 1467 | // Share | | |
| 1468 | while (end_index < path.len and !path_type.isSep(T, path[end_index])) { | | |
| 1469 | end_index += 1; | | |
| 1470 | } | | |
| 1471 | // Slash(es) after share | | |
| 1472 | while (end_index < path.len and path_type.isSep(T, path[end_index])) { | | |
| 1473 | end_index += 1; | | |
| 1474 | } | | |
| 1475 | break :unc end_index; | | |
| 1476 | }, | | |
| 1477 | .drive_absolute => drive: { | | |
| 1478 | var end_index: usize = 3; | | |
| 1479 | while (end_index < path.len and path_type.isSep(T, path[end_index])) { | | |
| 1480 | end_index += 1; | | |
| 1481 | } | | |
| 1482 | break :drive end_index; | | |
| 1483 | }, | | |
| 1484 | .drive_relative => 2, | | |
| 1485 | }; | | |
| 1486 | }, | 1952 | }, |
| 1487 | }; | 1953 | }; |
| | 1954 | // If there are repeated path separators directly after the root, |
| | 1955 | // keep track of that info so that they don't have to be dealt with when |
| | 1956 | // iterating components. |
| | 1957 | var root_end_index = root_len; |
| | 1958 | for (path[root_len..]) |c| { |
| | 1959 | if (!path_type.isSep(T, c)) break; |
| | 1960 | root_end_index += 1; |
| | 1961 | } |
| 1488 | return .{ | 1962 | return .{ |
| 1489 | .path = path, | 1963 | .path = path, |
| | 1964 | .root_len = root_len, |
| 1490 | .root_end_index = root_end_index, | 1965 | .root_end_index = root_end_index, |
| 1491 | .start_index = root_end_index, | 1966 | .start_index = root_end_index, |
| 1492 | .end_index = root_end_index, | 1967 | .end_index = root_end_index, |
| 1493 | }; | 1968 | }; |
| 1494 | } | 1969 | } |
| 1495 | | 1970 | |
| 1496 | /// Returns the root of the path if it is an absolute path, or null otherwise. | 1971 | /// Returns the root of the path if it is not a relative path, or null otherwise. |
| 1497 | /// For POSIX paths, this will be `/`. | 1972 | /// For POSIX paths, this will be `/`. |
| 1498 | /// For Windows paths, this will be something like `C:\`, `\\server\share\`, etc. | 1973 | /// For Windows paths, this will be something like `C:\`, `\\server\share\`, etc. |
| 1499 | /// For UEFI paths, this will be `\`. | 1974 | /// For UEFI paths, this will be `\`. |
| 1500 | pub fn root(self: Self) ?[]const T { | 1975 | pub fn root(self: Self) ?[]const T { |
| 1501 | if (self.root_end_index == 0) return null; | 1976 | if (self.root_end_index == 0) return null; |
| 1502 | return self.path[0..self.root_end_index]; | 1977 | return self.path[0..self.root_len]; |
| 1503 | } | 1978 | } |
| 1504 | | 1979 | |
| 1505 | /// Returns the first component (from the beginning of the path). | 1980 | /// Returns the first component (from the beginning of the path). |
| ... | @@ -1614,7 +2089,7 @@ pub const NativeComponentIterator = ComponentIterator(switch (native_os) { | ... | @@ -1614,7 +2089,7 @@ pub const NativeComponentIterator = ComponentIterator(switch (native_os) { |
| 1614 | else => .posix, | 2089 | else => .posix, |
| 1615 | }, u8); | 2090 | }, u8); |
| 1616 | | 2091 | |
| 1617 | pub fn componentIterator(path: []const u8) !NativeComponentIterator { | 2092 | pub fn componentIterator(path: []const u8) NativeComponentIterator { |
| 1618 | return NativeComponentIterator.init(path); | 2093 | return NativeComponentIterator.init(path); |
| 1619 | } | 2094 | } |
| 1620 | | 2095 | |
| ... | @@ -1622,8 +2097,9 @@ test "ComponentIterator posix" { | ... | @@ -1622,8 +2097,9 @@ test "ComponentIterator posix" { |
| 1622 | const PosixComponentIterator = ComponentIterator(.posix, u8); | 2097 | const PosixComponentIterator = ComponentIterator(.posix, u8); |
| 1623 | { | 2098 | { |
| 1624 | const path = "a/b/c/"; | 2099 | const path = "a/b/c/"; |
| 1625 | var it = try PosixComponentIterator.init(path); | 2100 | var it = PosixComponentIterator.init(path); |
| 1626 | try std.testing.expectEqual(@as(usize, 0), it.root_end_index); | 2101 | try std.testing.expectEqual(0, it.root_len); |
| | 2102 | try std.testing.expectEqual(0, it.root_end_index); |
| 1627 | try std.testing.expect(null == it.root()); | 2103 | try std.testing.expect(null == it.root()); |
| 1628 | { | 2104 | { |
| 1629 | try std.testing.expect(null == it.previous()); | 2105 | try std.testing.expect(null == it.previous()); |
| ... | @@ -1669,8 +2145,9 @@ test "ComponentIterator posix" { | ... | @@ -1669,8 +2145,9 @@ test "ComponentIterator posix" { |
| 1669 | | 2145 | |
| 1670 | { | 2146 | { |
| 1671 | const path = "/a/b/c/"; | 2147 | const path = "/a/b/c/"; |
| 1672 | var it = try PosixComponentIterator.init(path); | 2148 | var it = PosixComponentIterator.init(path); |
| 1673 | try std.testing.expectEqual(@as(usize, 1), it.root_end_index); | 2149 | try std.testing.expectEqual(1, it.root_len); |
| | 2150 | try std.testing.expectEqual(1, it.root_end_index); |
| 1674 | try std.testing.expectEqualStrings("/", it.root().?); | 2151 | try std.testing.expectEqualStrings("/", it.root().?); |
| 1675 | { | 2152 | { |
| 1676 | try std.testing.expect(null == it.previous()); | 2153 | try std.testing.expect(null == it.previous()); |
| ... | @@ -1714,10 +2191,59 @@ test "ComponentIterator posix" { | ... | @@ -1714,10 +2191,59 @@ test "ComponentIterator posix" { |
| 1714 | } | 2191 | } |
| 1715 | } | 2192 | } |
| 1716 | | 2193 | |
| | 2194 | { |
| | 2195 | const path = "////a///b///c////"; |
| | 2196 | var it = PosixComponentIterator.init(path); |
| | 2197 | try std.testing.expectEqual(1, it.root_len); |
| | 2198 | try std.testing.expectEqual(4, it.root_end_index); |
| | 2199 | try std.testing.expectEqualStrings("/", it.root().?); |
| | 2200 | { |
| | 2201 | try std.testing.expect(null == it.previous()); |
| | 2202 | |
| | 2203 | const first_via_next = it.next().?; |
| | 2204 | try std.testing.expectEqualStrings("a", first_via_next.name); |
| | 2205 | try std.testing.expectEqualStrings("////a", first_via_next.path); |
| | 2206 | |
| | 2207 | const first = it.first().?; |
| | 2208 | try std.testing.expectEqualStrings("a", first.name); |
| | 2209 | try std.testing.expectEqualStrings("////a", first.path); |
| | 2210 | |
| | 2211 | try std.testing.expect(null == it.previous()); |
| | 2212 | |
| | 2213 | const second = it.next().?; |
| | 2214 | try std.testing.expectEqualStrings("b", second.name); |
| | 2215 | try std.testing.expectEqualStrings("////a///b", second.path); |
| | 2216 | |
| | 2217 | const third = it.next().?; |
| | 2218 | try std.testing.expectEqualStrings("c", third.name); |
| | 2219 | try std.testing.expectEqualStrings("////a///b///c", third.path); |
| | 2220 | |
| | 2221 | try std.testing.expect(null == it.next()); |
| | 2222 | } |
| | 2223 | { |
| | 2224 | const last = it.last().?; |
| | 2225 | try std.testing.expectEqualStrings("c", last.name); |
| | 2226 | try std.testing.expectEqualStrings("////a///b///c", last.path); |
| | 2227 | |
| | 2228 | try std.testing.expect(null == it.next()); |
| | 2229 | |
| | 2230 | const second_to_last = it.previous().?; |
| | 2231 | try std.testing.expectEqualStrings("b", second_to_last.name); |
| | 2232 | try std.testing.expectEqualStrings("////a///b", second_to_last.path); |
| | 2233 | |
| | 2234 | const third_to_last = it.previous().?; |
| | 2235 | try std.testing.expectEqualStrings("a", third_to_last.name); |
| | 2236 | try std.testing.expectEqualStrings("////a", third_to_last.path); |
| | 2237 | |
| | 2238 | try std.testing.expect(null == it.previous()); |
| | 2239 | } |
| | 2240 | } |
| | 2241 | |
| 1717 | { | 2242 | { |
| 1718 | const path = "/"; | 2243 | const path = "/"; |
| 1719 | var it = try PosixComponentIterator.init(path); | 2244 | var it = PosixComponentIterator.init(path); |
| 1720 | try std.testing.expectEqual(@as(usize, 1), it.root_end_index); | 2245 | try std.testing.expectEqual(1, it.root_len); |
| | 2246 | try std.testing.expectEqual(1, it.root_end_index); |
| 1721 | try std.testing.expectEqualStrings("/", it.root().?); | 2247 | try std.testing.expectEqualStrings("/", it.root().?); |
| 1722 | | 2248 | |
| 1723 | try std.testing.expect(null == it.first()); | 2249 | try std.testing.expect(null == it.first()); |
| ... | @@ -1733,8 +2259,9 @@ test "ComponentIterator posix" { | ... | @@ -1733,8 +2259,9 @@ test "ComponentIterator posix" { |
| 1733 | | 2259 | |
| 1734 | { | 2260 | { |
| 1735 | const path = ""; | 2261 | const path = ""; |
| 1736 | var it = try PosixComponentIterator.init(path); | 2262 | var it = PosixComponentIterator.init(path); |
| 1737 | try std.testing.expectEqual(@as(usize, 0), it.root_end_index); | 2263 | try std.testing.expectEqual(0, it.root_len); |
| | 2264 | try std.testing.expectEqual(0, it.root_end_index); |
| 1738 | try std.testing.expect(null == it.root()); | 2265 | try std.testing.expect(null == it.root()); |
| 1739 | | 2266 | |
| 1740 | try std.testing.expect(null == it.first()); | 2267 | try std.testing.expect(null == it.first()); |
| ... | @@ -1753,8 +2280,9 @@ test "ComponentIterator windows" { | ... | @@ -1753,8 +2280,9 @@ test "ComponentIterator windows" { |
| 1753 | const WindowsComponentIterator = ComponentIterator(.windows, u8); | 2280 | const WindowsComponentIterator = ComponentIterator(.windows, u8); |
| 1754 | { | 2281 | { |
| 1755 | const path = "a/b\\c//"; | 2282 | const path = "a/b\\c//"; |
| 1756 | var it = try WindowsComponentIterator.init(path); | 2283 | var it = WindowsComponentIterator.init(path); |
| 1757 | try std.testing.expectEqual(@as(usize, 0), it.root_end_index); | 2284 | try std.testing.expectEqual(0, it.root_len); |
| | 2285 | try std.testing.expectEqual(0, it.root_end_index); |
| 1758 | try std.testing.expect(null == it.root()); | 2286 | try std.testing.expect(null == it.root()); |
| 1759 | { | 2287 | { |
| 1760 | try std.testing.expect(null == it.previous()); | 2288 | try std.testing.expect(null == it.previous()); |
| ... | @@ -1800,8 +2328,9 @@ test "ComponentIterator windows" { | ... | @@ -1800,8 +2328,9 @@ test "ComponentIterator windows" { |
| 1800 | | 2328 | |
| 1801 | { | 2329 | { |
| 1802 | const path = "C:\\a/b/c/"; | 2330 | const path = "C:\\a/b/c/"; |
| 1803 | var it = try WindowsComponentIterator.init(path); | 2331 | var it = WindowsComponentIterator.init(path); |
| 1804 | try std.testing.expectEqual(@as(usize, 3), it.root_end_index); | 2332 | try std.testing.expectEqual(3, it.root_len); |
| | 2333 | try std.testing.expectEqual(3, it.root_end_index); |
| 1805 | try std.testing.expectEqualStrings("C:\\", it.root().?); | 2334 | try std.testing.expectEqualStrings("C:\\", it.root().?); |
| 1806 | { | 2335 | { |
| 1807 | const first = it.first().?; | 2336 | const first = it.first().?; |
| ... | @@ -1835,10 +2364,49 @@ test "ComponentIterator windows" { | ... | @@ -1835,10 +2364,49 @@ test "ComponentIterator windows" { |
| 1835 | } | 2364 | } |
| 1836 | } | 2365 | } |
| 1837 | | 2366 | |
| | 2367 | { |
| | 2368 | const path = "C:\\\\//a/\\/\\b///c////"; |
| | 2369 | var it = WindowsComponentIterator.init(path); |
| | 2370 | try std.testing.expectEqual(3, it.root_len); |
| | 2371 | try std.testing.expectEqual(6, it.root_end_index); |
| | 2372 | try std.testing.expectEqualStrings("C:\\", it.root().?); |
| | 2373 | { |
| | 2374 | const first = it.first().?; |
| | 2375 | try std.testing.expectEqualStrings("a", first.name); |
| | 2376 | try std.testing.expectEqualStrings("C:\\\\//a", first.path); |
| | 2377 | |
| | 2378 | const second = it.next().?; |
| | 2379 | try std.testing.expectEqualStrings("b", second.name); |
| | 2380 | try std.testing.expectEqualStrings("C:\\\\//a/\\/\\b", second.path); |
| | 2381 | |
| | 2382 | const third = it.next().?; |
| | 2383 | try std.testing.expectEqualStrings("c", third.name); |
| | 2384 | try std.testing.expectEqualStrings("C:\\\\//a/\\/\\b///c", third.path); |
| | 2385 | |
| | 2386 | try std.testing.expect(null == it.next()); |
| | 2387 | } |
| | 2388 | { |
| | 2389 | const last = it.last().?; |
| | 2390 | try std.testing.expectEqualStrings("c", last.name); |
| | 2391 | try std.testing.expectEqualStrings("C:\\\\//a/\\/\\b///c", last.path); |
| | 2392 | |
| | 2393 | const second_to_last = it.previous().?; |
| | 2394 | try std.testing.expectEqualStrings("b", second_to_last.name); |
| | 2395 | try std.testing.expectEqualStrings("C:\\\\//a/\\/\\b", second_to_last.path); |
| | 2396 | |
| | 2397 | const third_to_last = it.previous().?; |
| | 2398 | try std.testing.expectEqualStrings("a", third_to_last.name); |
| | 2399 | try std.testing.expectEqualStrings("C:\\\\//a", third_to_last.path); |
| | 2400 | |
| | 2401 | try std.testing.expect(null == it.previous()); |
| | 2402 | } |
| | 2403 | } |
| | 2404 | |
| 1838 | { | 2405 | { |
| 1839 | const path = "/"; | 2406 | const path = "/"; |
| 1840 | var it = try WindowsComponentIterator.init(path); | 2407 | var it = WindowsComponentIterator.init(path); |
| 1841 | try std.testing.expectEqual(@as(usize, 1), it.root_end_index); | 2408 | try std.testing.expectEqual(1, it.root_len); |
| | 2409 | try std.testing.expectEqual(1, it.root_end_index); |
| 1842 | try std.testing.expectEqualStrings("/", it.root().?); | 2410 | try std.testing.expectEqualStrings("/", it.root().?); |
| 1843 | | 2411 | |
| 1844 | try std.testing.expect(null == it.first()); | 2412 | try std.testing.expect(null == it.first()); |
| ... | @@ -1854,8 +2422,9 @@ test "ComponentIterator windows" { | ... | @@ -1854,8 +2422,9 @@ test "ComponentIterator windows" { |
| 1854 | | 2422 | |
| 1855 | { | 2423 | { |
| 1856 | const path = ""; | 2424 | const path = ""; |
| 1857 | var it = try WindowsComponentIterator.init(path); | 2425 | var it = WindowsComponentIterator.init(path); |
| 1858 | try std.testing.expectEqual(@as(usize, 0), it.root_end_index); | 2426 | try std.testing.expectEqual(0, it.root_len); |
| | 2427 | try std.testing.expectEqual(0, it.root_end_index); |
| 1859 | try std.testing.expect(null == it.root()); | 2428 | try std.testing.expect(null == it.root()); |
| 1860 | | 2429 | |
| 1861 | try std.testing.expect(null == it.first()); | 2430 | try std.testing.expect(null == it.first()); |
| ... | @@ -1871,17 +2440,13 @@ test "ComponentIterator windows" { | ... | @@ -1871,17 +2440,13 @@ test "ComponentIterator windows" { |
| 1871 | } | 2440 | } |
| 1872 | | 2441 | |
| 1873 | test "ComponentIterator windows WTF-16" { | 2442 | test "ComponentIterator windows WTF-16" { |
| 1874 | // TODO: Fix on big endian architectures | | |
| 1875 | if (builtin.cpu.arch.endian() != .little) { | | |
| 1876 | return error.SkipZigTest; | | |
| 1877 | } | | |
| 1878 | | | |
| 1879 | const WindowsComponentIterator = ComponentIterator(.windows, u16); | 2443 | const WindowsComponentIterator = ComponentIterator(.windows, u16); |
| 1880 | const L = std.unicode.utf8ToUtf16LeStringLiteral; | 2444 | const L = std.unicode.utf8ToUtf16LeStringLiteral; |
| 1881 | | 2445 | |
| 1882 | const path = L("C:\\a/b/c/"); | 2446 | const path = L("C:\\a/b/c/"); |
| 1883 | var it = try WindowsComponentIterator.init(path); | 2447 | var it = WindowsComponentIterator.init(path); |
| 1884 | try std.testing.expectEqual(@as(usize, 3), it.root_end_index); | 2448 | try std.testing.expectEqual(3, it.root_len); |
| | 2449 | try std.testing.expectEqual(3, it.root_end_index); |
| 1885 | try std.testing.expectEqualSlices(u16, L("C:\\"), it.root().?); | 2450 | try std.testing.expectEqualSlices(u16, L("C:\\"), it.root().?); |
| 1886 | { | 2451 | { |
| 1887 | const first = it.first().?; | 2452 | const first = it.first().?; |
| ... | @@ -1918,55 +2483,60 @@ test "ComponentIterator windows WTF-16" { | ... | @@ -1918,55 +2483,60 @@ test "ComponentIterator windows WTF-16" { |
| 1918 | test "ComponentIterator roots" { | 2483 | test "ComponentIterator roots" { |
| 1919 | // UEFI | 2484 | // UEFI |
| 1920 | { | 2485 | { |
| 1921 | var it = try ComponentIterator(.uefi, u8).init("\\\\a"); | 2486 | var it = ComponentIterator(.uefi, u8).init("\\\\a"); |
| 1922 | try std.testing.expectEqualStrings("\\\\", it.root().?); | 2487 | try std.testing.expectEqualStrings("\\", it.root().?); |
| 1923 | | 2488 | |
| 1924 | it = try ComponentIterator(.uefi, u8).init("//a"); | 2489 | it = ComponentIterator(.uefi, u8).init("//a"); |
| 1925 | try std.testing.expect(null == it.root()); | 2490 | try std.testing.expect(null == it.root()); |
| 1926 | } | 2491 | } |
| 1927 | // POSIX | 2492 | // POSIX |
| 1928 | { | 2493 | { |
| 1929 | var it = try ComponentIterator(.posix, u8).init("//a"); | 2494 | var it = ComponentIterator(.posix, u8).init("//a"); |
| 1930 | try std.testing.expectEqualStrings("//", it.root().?); | 2495 | try std.testing.expectEqualStrings("/", it.root().?); |
| 1931 | | 2496 | |
| 1932 | it = try ComponentIterator(.posix, u8).init("\\\\a"); | 2497 | it = ComponentIterator(.posix, u8).init("\\\\a"); |
| 1933 | try std.testing.expect(null == it.root()); | 2498 | try std.testing.expect(null == it.root()); |
| 1934 | } | 2499 | } |
| 1935 | // Windows | 2500 | // Windows |
| 1936 | { | 2501 | { |
| 1937 | // Drive relative | 2502 | // Drive relative |
| 1938 | var it = try ComponentIterator(.windows, u8).init("C:a"); | 2503 | var it = ComponentIterator(.windows, u8).init("C:a"); |
| 1939 | try std.testing.expectEqualStrings("C:", it.root().?); | 2504 | try std.testing.expectEqualStrings("C:", it.root().?); |
| 1940 | | 2505 | |
| 1941 | // Drive absolute | 2506 | // Drive absolute |
| 1942 | it = try ComponentIterator(.windows, u8).init("C://a"); | 2507 | it = ComponentIterator(.windows, u8).init("C:/a"); |
| 1943 | try std.testing.expectEqualStrings("C://", it.root().?); | 2508 | try std.testing.expectEqualStrings("C:/", it.root().?); |
| 1944 | it = try ComponentIterator(.windows, u8).init("C:\\a"); | 2509 | it = ComponentIterator(.windows, u8).init("C:\\a"); |
| 1945 | try std.testing.expectEqualStrings("C:\\", it.root().?); | 2510 | try std.testing.expectEqualStrings("C:\\", it.root().?); |
| | 2511 | it = ComponentIterator(.windows, u8).init("C:///a"); |
| | 2512 | try std.testing.expectEqualStrings("C:/", it.root().?); |
| 1946 | | 2513 | |
| 1947 | // Rooted | 2514 | // Rooted |
| 1948 | it = try ComponentIterator(.windows, u8).init("\\a"); | 2515 | it = ComponentIterator(.windows, u8).init("\\a"); |
| 1949 | try std.testing.expectEqualStrings("\\", it.root().?); | 2516 | try std.testing.expectEqualStrings("\\", it.root().?); |
| 1950 | it = try ComponentIterator(.windows, u8).init("/a"); | 2517 | it = ComponentIterator(.windows, u8).init("/a"); |
| 1951 | try std.testing.expectEqualStrings("/", it.root().?); | 2518 | try std.testing.expectEqualStrings("/", it.root().?); |
| 1952 | | 2519 | |
| 1953 | // Root local device | 2520 | // Root local device |
| 1954 | it = try ComponentIterator(.windows, u8).init("\\\\."); | 2521 | it = ComponentIterator(.windows, u8).init("\\\\."); |
| 1955 | try std.testing.expectEqualStrings("\\\\.", it.root().?); | 2522 | try std.testing.expectEqualStrings("\\\\.", it.root().?); |
| 1956 | it = try ComponentIterator(.windows, u8).init("//?"); | 2523 | it = ComponentIterator(.windows, u8).init("//?"); |
| 1957 | try std.testing.expectEqualStrings("//?", it.root().?); | 2524 | try std.testing.expectEqualStrings("//?", it.root().?); |
| 1958 | | 2525 | |
| 1959 | // UNC absolute | 2526 | // UNC absolute |
| 1960 | it = try ComponentIterator(.windows, u8).init("//"); | 2527 | it = ComponentIterator(.windows, u8).init("//"); |
| 1961 | try std.testing.expectEqualStrings("//", it.root().?); | 2528 | try std.testing.expectEqualStrings("//", it.root().?); |
| 1962 | it = try ComponentIterator(.windows, u8).init("\\\\a"); | 2529 | it = ComponentIterator(.windows, u8).init("\\\\a"); |
| 1963 | try std.testing.expectEqualStrings("\\\\a", it.root().?); | 2530 | try std.testing.expectEqualStrings("\\\\a", it.root().?); |
| 1964 | it = try ComponentIterator(.windows, u8).init("\\\\a\\b\\\\c"); | 2531 | it = ComponentIterator(.windows, u8).init("\\\\a\\b\\\\c"); |
| 1965 | try std.testing.expectEqualStrings("\\\\a\\b\\\\", it.root().?); | 2532 | try std.testing.expectEqualStrings("\\\\a\\b\\", it.root().?); |
| 1966 | it = try ComponentIterator(.windows, u8).init("//a"); | 2533 | it = ComponentIterator(.windows, u8).init("//a"); |
| 1967 | try std.testing.expectEqualStrings("//a", it.root().?); | 2534 | try std.testing.expectEqualStrings("//a", it.root().?); |
| 1968 | it = try ComponentIterator(.windows, u8).init("//a/b//c"); | 2535 | it = ComponentIterator(.windows, u8).init("//a/b//c"); |
| 1969 | try std.testing.expectEqualStrings("//a/b//", it.root().?); | 2536 | try std.testing.expectEqualStrings("//a/b/", it.root().?); |
| | 2537 | // Malformed UNC path with empty server name |
| | 2538 | it = ComponentIterator(.windows, u8).init("\\\\\\a\\b\\c"); |
| | 2539 | try std.testing.expectEqualStrings("\\\\\\a\\", it.root().?); |
| 1970 | } | 2540 | } |
| 1971 | } | 2541 | } |
| 1972 | | 2542 | |