2017-12-23 19:08:53 -08:00
|
|
|
const std = @import("../index.zig");
|
2017-05-01 10:12:38 -07:00
|
|
|
const builtin = @import("builtin");
|
|
|
|
const Os = builtin.Os;
|
2017-12-23 19:08:53 -08:00
|
|
|
const debug = std.debug;
|
2017-04-17 03:45:44 -07:00
|
|
|
const assert = debug.assert;
|
2017-12-23 19:08:53 -08:00
|
|
|
const mem = std.mem;
|
|
|
|
const fmt = std.fmt;
|
2017-04-17 03:45:44 -07:00
|
|
|
const Allocator = mem.Allocator;
|
2017-12-23 19:08:53 -08:00
|
|
|
const os = std.os;
|
|
|
|
const math = std.math;
|
2017-04-30 15:56:24 -07:00
|
|
|
const posix = os.posix;
|
2017-10-09 11:21:35 -07:00
|
|
|
const windows = os.windows;
|
2017-12-23 19:08:53 -08:00
|
|
|
const cstr = std.cstr;
|
2017-04-17 03:45:44 -07:00
|
|
|
|
2017-10-05 21:27:15 -07:00
|
|
|
pub const sep_windows = '\\';
|
|
|
|
pub const sep_posix = '/';
|
|
|
|
pub const sep = if (is_windows) sep_windows else sep_posix;
|
|
|
|
|
|
|
|
pub const delimiter_windows = ';';
|
|
|
|
pub const delimiter_posix = ':';
|
|
|
|
pub const delimiter = if (is_windows) delimiter_windows else delimiter_posix;
|
|
|
|
|
|
|
|
const is_windows = builtin.os == builtin.Os.windows;
|
2017-04-20 22:56:12 -07:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn isSep(byte: u8) bool {
|
2018-01-03 01:55:16 -08:00
|
|
|
if (is_windows) {
|
|
|
|
return byte == '/' or byte == '\\';
|
|
|
|
} else {
|
|
|
|
return byte == '/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 22:56:12 -07:00
|
|
|
/// Naively combines a series of paths with the native path seperator.
|
2017-04-17 03:45:44 -07:00
|
|
|
/// Allocates memory for the result, which must be freed by the caller.
|
2018-01-31 19:48:40 -08:00
|
|
|
pub fn join(allocator: &Allocator, paths: ...) ![]u8 {
|
2017-10-05 21:27:15 -07:00
|
|
|
if (is_windows) {
|
|
|
|
return joinWindows(allocator, paths);
|
|
|
|
} else {
|
|
|
|
return joinPosix(allocator, paths);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 19:48:40 -08:00
|
|
|
pub fn joinWindows(allocator: &Allocator, paths: ...) ![]u8 {
|
2017-10-05 21:27:15 -07:00
|
|
|
return mem.join(allocator, sep_windows, paths);
|
|
|
|
}
|
|
|
|
|
2018-01-31 19:48:40 -08:00
|
|
|
pub fn joinPosix(allocator: &Allocator, paths: ...) ![]u8 {
|
2017-10-05 21:27:15 -07:00
|
|
|
return mem.join(allocator, sep_posix, paths);
|
2017-04-17 03:45:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "os.path.join" {
|
2018-01-08 21:07:01 -08:00
|
|
|
assert(mem.eql(u8, try joinWindows(debug.global_allocator, "c:\\a\\b", "c"), "c:\\a\\b\\c"));
|
|
|
|
assert(mem.eql(u8, try joinWindows(debug.global_allocator, "c:\\a\\b\\", "c"), "c:\\a\\b\\c"));
|
2017-10-05 21:27:15 -07:00
|
|
|
|
2018-01-08 21:07:01 -08:00
|
|
|
assert(mem.eql(u8, try joinWindows(debug.global_allocator, "c:\\", "a", "b\\", "c"), "c:\\a\\b\\c"));
|
|
|
|
assert(mem.eql(u8, try joinWindows(debug.global_allocator, "c:\\a\\", "b\\", "c"), "c:\\a\\b\\c"));
|
2017-10-05 21:27:15 -07:00
|
|
|
|
2018-05-28 17:23:55 -07:00
|
|
|
assert(mem.eql(u8, try joinWindows(debug.global_allocator, "c:\\home\\andy\\dev\\zig\\build\\lib\\zig\\std", "io.zig"), "c:\\home\\andy\\dev\\zig\\build\\lib\\zig\\std\\io.zig"));
|
2017-10-05 21:27:15 -07:00
|
|
|
|
2018-01-08 21:07:01 -08:00
|
|
|
assert(mem.eql(u8, try joinPosix(debug.global_allocator, "/a/b", "c"), "/a/b/c"));
|
|
|
|
assert(mem.eql(u8, try joinPosix(debug.global_allocator, "/a/b/", "c"), "/a/b/c"));
|
2017-04-18 22:13:15 -07:00
|
|
|
|
2018-01-08 21:07:01 -08:00
|
|
|
assert(mem.eql(u8, try joinPosix(debug.global_allocator, "/", "a", "b/", "c"), "/a/b/c"));
|
|
|
|
assert(mem.eql(u8, try joinPosix(debug.global_allocator, "/a/", "b/", "c"), "/a/b/c"));
|
2017-04-24 09:01:19 -07:00
|
|
|
|
2018-05-28 17:23:55 -07:00
|
|
|
assert(mem.eql(u8, try joinPosix(debug.global_allocator, "/home/andy/dev/zig/build/lib/zig/std", "io.zig"), "/home/andy/dev/zig/build/lib/zig/std/io.zig"));
|
2017-04-18 22:13:15 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn isAbsolute(path: []const u8) bool {
|
2017-10-05 21:27:15 -07:00
|
|
|
if (is_windows) {
|
|
|
|
return isAbsoluteWindows(path);
|
|
|
|
} else {
|
|
|
|
return isAbsolutePosix(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn isAbsoluteWindows(path: []const u8) bool {
|
2017-10-05 21:27:15 -07:00
|
|
|
if (path[0] == '/')
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (path[0] == '\\') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (path.len < 3) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (path[1] == ':') {
|
|
|
|
if (path[2] == '/')
|
|
|
|
return true;
|
|
|
|
if (path[2] == '\\')
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn isAbsolutePosix(path: []const u8) bool {
|
2017-10-05 21:27:15 -07:00
|
|
|
return path[0] == sep_posix;
|
|
|
|
}
|
|
|
|
|
|
|
|
test "os.path.isAbsoluteWindows" {
|
|
|
|
testIsAbsoluteWindows("/", true);
|
|
|
|
testIsAbsoluteWindows("//", true);
|
|
|
|
testIsAbsoluteWindows("//server", true);
|
|
|
|
testIsAbsoluteWindows("//server/file", true);
|
|
|
|
testIsAbsoluteWindows("\\\\server\\file", true);
|
|
|
|
testIsAbsoluteWindows("\\\\server", true);
|
|
|
|
testIsAbsoluteWindows("\\\\", true);
|
|
|
|
testIsAbsoluteWindows("c", false);
|
|
|
|
testIsAbsoluteWindows("c:", false);
|
|
|
|
testIsAbsoluteWindows("c:\\", true);
|
|
|
|
testIsAbsoluteWindows("c:/", true);
|
|
|
|
testIsAbsoluteWindows("c://", true);
|
|
|
|
testIsAbsoluteWindows("C:/Users/", true);
|
|
|
|
testIsAbsoluteWindows("C:\\Users\\", true);
|
|
|
|
testIsAbsoluteWindows("C:cwd/another", false);
|
|
|
|
testIsAbsoluteWindows("C:cwd\\another", false);
|
|
|
|
testIsAbsoluteWindows("directory/directory", false);
|
|
|
|
testIsAbsoluteWindows("directory\\directory", false);
|
2017-12-23 16:42:19 -08:00
|
|
|
testIsAbsoluteWindows("/usr/local", true);
|
2017-10-05 21:27:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "os.path.isAbsolutePosix" {
|
|
|
|
testIsAbsolutePosix("/home/foo", true);
|
|
|
|
testIsAbsolutePosix("/home/foo/..", true);
|
|
|
|
testIsAbsolutePosix("bar/", false);
|
|
|
|
testIsAbsolutePosix("./baz", false);
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn testIsAbsoluteWindows(path: []const u8, expected_result: bool) void {
|
2017-10-05 21:27:15 -07:00
|
|
|
assert(isAbsoluteWindows(path) == expected_result);
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn testIsAbsolutePosix(path: []const u8, expected_result: bool) void {
|
2017-10-05 21:27:15 -07:00
|
|
|
assert(isAbsolutePosix(path) == expected_result);
|
|
|
|
}
|
|
|
|
|
2017-12-23 16:42:19 -08:00
|
|
|
pub const WindowsPath = struct {
|
|
|
|
is_abs: bool,
|
|
|
|
kind: Kind,
|
|
|
|
disk_designator: []const u8,
|
2017-10-05 21:27:15 -07:00
|
|
|
|
2017-12-23 16:42:19 -08:00
|
|
|
pub const Kind = enum {
|
|
|
|
None,
|
|
|
|
Drive,
|
|
|
|
NetworkShare,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn windowsParsePath(path: []const u8) WindowsPath {
|
2017-12-23 16:42:19 -08:00
|
|
|
if (path.len >= 2 and path[1] == ':') {
|
2018-05-28 17:23:55 -07:00
|
|
|
return WindowsPath{
|
2017-12-23 16:42:19 -08:00
|
|
|
.is_abs = isAbsoluteWindows(path),
|
|
|
|
.kind = WindowsPath.Kind.Drive,
|
|
|
|
.disk_designator = path[0..2],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (path.len >= 1 and (path[0] == '/' or path[0] == '\\') and
|
2018-05-28 17:23:55 -07:00
|
|
|
(path.len == 1 or (path[1] != '/' and path[1] != '\\')))
|
2017-12-23 16:42:19 -08:00
|
|
|
{
|
2018-05-28 17:23:55 -07:00
|
|
|
return WindowsPath{
|
2017-12-23 16:42:19 -08:00
|
|
|
.is_abs = true,
|
|
|
|
.kind = WindowsPath.Kind.None,
|
|
|
|
.disk_designator = path[0..0],
|
|
|
|
};
|
|
|
|
}
|
2018-05-28 17:23:55 -07:00
|
|
|
const relative_path = WindowsPath{
|
2017-12-23 16:42:19 -08:00
|
|
|
.kind = WindowsPath.Kind.None,
|
|
|
|
.disk_designator = []u8{},
|
|
|
|
.is_abs = false,
|
|
|
|
};
|
|
|
|
if (path.len < "//a/b".len) {
|
|
|
|
return relative_path;
|
|
|
|
}
|
2017-10-05 21:27:15 -07:00
|
|
|
|
2017-10-06 14:21:21 -07:00
|
|
|
// TODO when I combined these together with `inline for` the compiler crashed
|
2017-10-05 21:27:15 -07:00
|
|
|
{
|
|
|
|
const this_sep = '/';
|
2018-05-28 17:23:55 -07:00
|
|
|
const two_sep = []u8{ this_sep, this_sep };
|
2017-10-05 21:27:15 -07:00
|
|
|
if (mem.startsWith(u8, path, two_sep)) {
|
2017-12-23 16:42:19 -08:00
|
|
|
if (path[2] == this_sep) {
|
|
|
|
return relative_path;
|
|
|
|
}
|
2017-10-06 14:21:21 -07:00
|
|
|
|
|
|
|
var it = mem.split(path, []u8{this_sep});
|
2017-12-23 16:42:19 -08:00
|
|
|
_ = (it.next() ?? return relative_path);
|
|
|
|
_ = (it.next() ?? return relative_path);
|
2018-05-28 17:23:55 -07:00
|
|
|
return WindowsPath{
|
2017-12-23 16:42:19 -08:00
|
|
|
.is_abs = isAbsoluteWindows(path),
|
|
|
|
.kind = WindowsPath.Kind.NetworkShare,
|
|
|
|
.disk_designator = path[0..it.index],
|
|
|
|
};
|
2017-10-05 21:27:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const this_sep = '\\';
|
2018-05-28 17:23:55 -07:00
|
|
|
const two_sep = []u8{ this_sep, this_sep };
|
2017-10-05 21:27:15 -07:00
|
|
|
if (mem.startsWith(u8, path, two_sep)) {
|
2017-12-23 16:42:19 -08:00
|
|
|
if (path[2] == this_sep) {
|
|
|
|
return relative_path;
|
|
|
|
}
|
2017-10-06 14:21:21 -07:00
|
|
|
|
|
|
|
var it = mem.split(path, []u8{this_sep});
|
2017-12-23 16:42:19 -08:00
|
|
|
_ = (it.next() ?? return relative_path);
|
|
|
|
_ = (it.next() ?? return relative_path);
|
2018-05-28 17:23:55 -07:00
|
|
|
return WindowsPath{
|
2017-12-23 16:42:19 -08:00
|
|
|
.is_abs = isAbsoluteWindows(path),
|
|
|
|
.kind = WindowsPath.Kind.NetworkShare,
|
|
|
|
.disk_designator = path[0..it.index],
|
|
|
|
};
|
2017-10-05 21:27:15 -07:00
|
|
|
}
|
|
|
|
}
|
2017-12-23 16:42:19 -08:00
|
|
|
return relative_path;
|
2017-10-05 21:27:15 -07:00
|
|
|
}
|
|
|
|
|
2017-12-23 16:42:19 -08:00
|
|
|
test "os.path.windowsParsePath" {
|
|
|
|
{
|
|
|
|
const parsed = windowsParsePath("//a/b");
|
|
|
|
assert(parsed.is_abs);
|
|
|
|
assert(parsed.kind == WindowsPath.Kind.NetworkShare);
|
|
|
|
assert(mem.eql(u8, parsed.disk_designator, "//a/b"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const parsed = windowsParsePath("\\\\a\\b");
|
|
|
|
assert(parsed.is_abs);
|
|
|
|
assert(parsed.kind == WindowsPath.Kind.NetworkShare);
|
|
|
|
assert(mem.eql(u8, parsed.disk_designator, "\\\\a\\b"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const parsed = windowsParsePath("\\\\a\\");
|
|
|
|
assert(!parsed.is_abs);
|
|
|
|
assert(parsed.kind == WindowsPath.Kind.None);
|
|
|
|
assert(mem.eql(u8, parsed.disk_designator, ""));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const parsed = windowsParsePath("/usr/local");
|
|
|
|
assert(parsed.is_abs);
|
|
|
|
assert(parsed.kind == WindowsPath.Kind.None);
|
|
|
|
assert(mem.eql(u8, parsed.disk_designator, ""));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const parsed = windowsParsePath("c:../");
|
|
|
|
assert(!parsed.is_abs);
|
|
|
|
assert(parsed.kind == WindowsPath.Kind.Drive);
|
|
|
|
assert(mem.eql(u8, parsed.disk_designator, "c:"));
|
|
|
|
}
|
2017-10-05 21:27:15 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn diskDesignator(path: []const u8) []const u8 {
|
2017-10-08 18:52:26 -07:00
|
|
|
if (is_windows) {
|
|
|
|
return diskDesignatorWindows(path);
|
|
|
|
} else {
|
2017-10-08 18:40:33 -07:00
|
|
|
return "";
|
2017-10-08 18:52:26 -07:00
|
|
|
}
|
|
|
|
}
|
2017-10-05 21:27:15 -07:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn diskDesignatorWindows(path: []const u8) []const u8 {
|
2017-12-23 16:42:19 -08:00
|
|
|
return windowsParsePath(path).disk_designator;
|
2017-10-05 21:27:15 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn networkShareServersEql(ns1: []const u8, ns2: []const u8) bool {
|
2017-10-08 18:16:10 -07:00
|
|
|
const sep1 = ns1[0];
|
|
|
|
const sep2 = ns2[0];
|
|
|
|
|
|
|
|
var it1 = mem.split(ns1, []u8{sep1});
|
|
|
|
var it2 = mem.split(ns2, []u8{sep2});
|
|
|
|
|
2017-12-23 16:42:19 -08:00
|
|
|
// TODO ASCII is wrong, we actually need full unicode support to compare paths.
|
2017-10-08 18:16:10 -07:00
|
|
|
return asciiEqlIgnoreCase(??it1.next(), ??it2.next());
|
2017-10-05 21:27:15 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn compareDiskDesignators(kind: WindowsPath.Kind, p1: []const u8, p2: []const u8) bool {
|
2017-12-23 16:42:19 -08:00
|
|
|
switch (kind) {
|
|
|
|
WindowsPath.Kind.None => {
|
|
|
|
assert(p1.len == 0);
|
|
|
|
assert(p2.len == 0);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
WindowsPath.Kind.Drive => {
|
|
|
|
return asciiUpper(p1[0]) == asciiUpper(p2[0]);
|
|
|
|
},
|
|
|
|
WindowsPath.Kind.NetworkShare => {
|
|
|
|
const sep1 = p1[0];
|
|
|
|
const sep2 = p2[0];
|
|
|
|
|
|
|
|
var it1 = mem.split(p1, []u8{sep1});
|
|
|
|
var it2 = mem.split(p2, []u8{sep2});
|
|
|
|
|
|
|
|
// TODO ASCII is wrong, we actually need full unicode support to compare paths.
|
|
|
|
return asciiEqlIgnoreCase(??it1.next(), ??it2.next()) and asciiEqlIgnoreCase(??it1.next(), ??it2.next());
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn asciiUpper(byte: u8) u8 {
|
2017-10-05 21:27:15 -07:00
|
|
|
return switch (byte) {
|
2018-05-28 17:23:55 -07:00
|
|
|
'a'...'z' => 'A' + (byte - 'a'),
|
2017-10-05 21:27:15 -07:00
|
|
|
else => byte,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn asciiEqlIgnoreCase(s1: []const u8, s2: []const u8) bool {
|
2017-10-08 18:16:10 -07:00
|
|
|
if (s1.len != s2.len)
|
|
|
|
return false;
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < s1.len) : (i += 1) {
|
|
|
|
if (asciiUpper(s1[i]) != asciiUpper(s2[i]))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-06 14:21:21 -07:00
|
|
|
/// Converts the command line arguments into a slice and calls `resolveSlice`.
|
2018-01-31 19:48:40 -08:00
|
|
|
pub fn resolve(allocator: &Allocator, args: ...) ![]u8 {
|
2017-04-20 22:56:12 -07:00
|
|
|
var paths: [args.len][]const u8 = undefined;
|
|
|
|
comptime var arg_i = 0;
|
2017-05-03 15:12:07 -07:00
|
|
|
inline while (arg_i < args.len) : (arg_i += 1) {
|
2017-04-20 22:56:12 -07:00
|
|
|
paths[arg_i] = args[arg_i];
|
|
|
|
}
|
|
|
|
return resolveSlice(allocator, paths);
|
|
|
|
}
|
|
|
|
|
2017-10-06 14:21:21 -07:00
|
|
|
/// On Windows, this calls `resolveWindows` and on POSIX it calls `resolvePosix`.
|
2018-01-31 19:48:40 -08:00
|
|
|
pub fn resolveSlice(allocator: &Allocator, paths: []const []const u8) ![]u8 {
|
2017-10-05 21:27:15 -07:00
|
|
|
if (is_windows) {
|
|
|
|
return resolveWindows(allocator, paths);
|
|
|
|
} else {
|
|
|
|
return resolvePosix(allocator, paths);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-06 14:21:21 -07:00
|
|
|
/// This function is like a series of `cd` statements executed one after another.
|
|
|
|
/// It resolves "." and "..".
|
|
|
|
/// The result does not have a trailing path separator.
|
|
|
|
/// If all paths are relative it uses the current working directory as a starting point.
|
|
|
|
/// Each drive has its own current working directory.
|
|
|
|
/// Path separators are canonicalized to '\\' and drives are canonicalized to capital letters.
|
2018-01-31 19:48:40 -08:00
|
|
|
pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) ![]u8 {
|
2017-10-05 21:27:15 -07:00
|
|
|
if (paths.len == 0) {
|
|
|
|
assert(is_windows); // resolveWindows called on non windows can't use getCwd
|
|
|
|
return os.getCwd(allocator);
|
|
|
|
}
|
|
|
|
|
2017-12-23 16:42:19 -08:00
|
|
|
// determine which disk designator we will result with, if any
|
|
|
|
var result_drive_buf = "_:";
|
|
|
|
var result_disk_designator: []const u8 = "";
|
|
|
|
var have_drive_kind = WindowsPath.Kind.None;
|
|
|
|
var have_abs_path = false;
|
2017-10-05 21:27:15 -07:00
|
|
|
var first_index: usize = 0;
|
|
|
|
var max_size: usize = 0;
|
|
|
|
for (paths) |p, i| {
|
2017-12-23 16:42:19 -08:00
|
|
|
const parsed = windowsParsePath(p);
|
|
|
|
if (parsed.is_abs) {
|
|
|
|
have_abs_path = true;
|
2017-10-05 21:27:15 -07:00
|
|
|
first_index = i;
|
2017-12-23 16:42:19 -08:00
|
|
|
max_size = result_disk_designator.len;
|
2017-10-05 21:27:15 -07:00
|
|
|
}
|
2017-12-23 16:42:19 -08:00
|
|
|
switch (parsed.kind) {
|
|
|
|
WindowsPath.Kind.Drive => {
|
|
|
|
result_drive_buf[0] = asciiUpper(parsed.disk_designator[0]);
|
|
|
|
result_disk_designator = result_drive_buf[0..];
|
|
|
|
have_drive_kind = WindowsPath.Kind.Drive;
|
|
|
|
},
|
|
|
|
WindowsPath.Kind.NetworkShare => {
|
|
|
|
result_disk_designator = parsed.disk_designator;
|
|
|
|
have_drive_kind = WindowsPath.Kind.NetworkShare;
|
|
|
|
},
|
|
|
|
WindowsPath.Kind.None => {},
|
2017-10-05 21:27:15 -07:00
|
|
|
}
|
|
|
|
max_size += p.len + 1;
|
|
|
|
}
|
|
|
|
|
2017-12-23 16:42:19 -08:00
|
|
|
// if we will result with a disk designator, loop again to determine
|
|
|
|
// which is the last time the disk designator is absolutely specified, if any
|
|
|
|
// and count up the max bytes for paths related to this disk designator
|
|
|
|
if (have_drive_kind != WindowsPath.Kind.None) {
|
|
|
|
have_abs_path = false;
|
2017-10-05 21:27:15 -07:00
|
|
|
first_index = 0;
|
2017-12-23 16:42:19 -08:00
|
|
|
max_size = result_disk_designator.len;
|
|
|
|
var correct_disk_designator = false;
|
2017-10-05 21:27:15 -07:00
|
|
|
|
|
|
|
for (paths) |p, i| {
|
2017-12-23 16:42:19 -08:00
|
|
|
const parsed = windowsParsePath(p);
|
|
|
|
if (parsed.kind != WindowsPath.Kind.None) {
|
|
|
|
if (parsed.kind == have_drive_kind) {
|
2018-05-28 17:23:55 -07:00
|
|
|
correct_disk_designator = compareDiskDesignators(have_drive_kind, result_disk_designator, parsed.disk_designator);
|
2017-12-23 16:42:19 -08:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-05 21:27:15 -07:00
|
|
|
}
|
2017-12-23 16:42:19 -08:00
|
|
|
if (!correct_disk_designator) {
|
2017-10-05 21:27:15 -07:00
|
|
|
continue;
|
|
|
|
}
|
2017-12-23 16:42:19 -08:00
|
|
|
if (parsed.is_abs) {
|
2017-10-05 21:27:15 -07:00
|
|
|
first_index = i;
|
2017-12-23 16:42:19 -08:00
|
|
|
max_size = result_disk_designator.len;
|
|
|
|
have_abs_path = true;
|
2017-10-05 21:27:15 -07:00
|
|
|
}
|
|
|
|
max_size += p.len + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-23 16:42:19 -08:00
|
|
|
// Allocate result and fill in the disk designator, calling getCwd if we have to.
|
2017-10-05 21:27:15 -07:00
|
|
|
var result: []u8 = undefined;
|
|
|
|
var result_index: usize = 0;
|
2017-10-06 14:21:21 -07:00
|
|
|
|
2017-12-23 16:42:19 -08:00
|
|
|
if (have_abs_path) {
|
|
|
|
switch (have_drive_kind) {
|
|
|
|
WindowsPath.Kind.Drive => {
|
2018-01-07 13:51:46 -08:00
|
|
|
result = try allocator.alloc(u8, max_size);
|
2017-12-23 16:42:19 -08:00
|
|
|
|
|
|
|
mem.copy(u8, result, result_disk_designator);
|
|
|
|
result_index += result_disk_designator.len;
|
|
|
|
},
|
|
|
|
WindowsPath.Kind.NetworkShare => {
|
2018-01-07 13:51:46 -08:00
|
|
|
result = try allocator.alloc(u8, max_size);
|
2017-12-23 16:42:19 -08:00
|
|
|
var it = mem.split(paths[first_index], "/\\");
|
|
|
|
const server_name = ??it.next();
|
|
|
|
const other_name = ??it.next();
|
|
|
|
|
|
|
|
result[result_index] = '\\';
|
|
|
|
result_index += 1;
|
|
|
|
result[result_index] = '\\';
|
|
|
|
result_index += 1;
|
|
|
|
mem.copy(u8, result[result_index..], server_name);
|
|
|
|
result_index += server_name.len;
|
|
|
|
result[result_index] = '\\';
|
|
|
|
result_index += 1;
|
|
|
|
mem.copy(u8, result[result_index..], other_name);
|
|
|
|
result_index += other_name.len;
|
2018-05-28 17:23:55 -07:00
|
|
|
|
2017-12-23 16:42:19 -08:00
|
|
|
result_disk_designator = result[0..result_index];
|
|
|
|
},
|
|
|
|
WindowsPath.Kind.None => {
|
|
|
|
assert(is_windows); // resolveWindows called on non windows can't use getCwd
|
2018-01-07 13:51:46 -08:00
|
|
|
const cwd = try os.getCwd(allocator);
|
2017-12-23 16:42:19 -08:00
|
|
|
defer allocator.free(cwd);
|
|
|
|
const parsed_cwd = windowsParsePath(cwd);
|
2018-01-07 13:51:46 -08:00
|
|
|
result = try allocator.alloc(u8, max_size + parsed_cwd.disk_designator.len + 1);
|
2017-12-23 16:42:19 -08:00
|
|
|
mem.copy(u8, result, parsed_cwd.disk_designator);
|
|
|
|
result_index += parsed_cwd.disk_designator.len;
|
|
|
|
result_disk_designator = result[0..parsed_cwd.disk_designator.len];
|
|
|
|
if (parsed_cwd.kind == WindowsPath.Kind.Drive) {
|
|
|
|
result[0] = asciiUpper(result[0]);
|
|
|
|
}
|
|
|
|
have_drive_kind = parsed_cwd.kind;
|
|
|
|
},
|
2017-10-06 14:21:21 -07:00
|
|
|
}
|
2017-10-05 21:27:15 -07:00
|
|
|
} else {
|
|
|
|
assert(is_windows); // resolveWindows called on non windows can't use getCwd
|
2017-12-23 16:42:19 -08:00
|
|
|
// TODO call get cwd for the result_disk_designator instead of the global one
|
2018-01-07 13:51:46 -08:00
|
|
|
const cwd = try os.getCwd(allocator);
|
2017-10-05 21:27:15 -07:00
|
|
|
defer allocator.free(cwd);
|
2017-12-23 16:42:19 -08:00
|
|
|
|
2018-01-07 13:51:46 -08:00
|
|
|
result = try allocator.alloc(u8, max_size + cwd.len + 1);
|
2017-12-23 16:42:19 -08:00
|
|
|
|
2017-10-05 21:27:15 -07:00
|
|
|
mem.copy(u8, result, cwd);
|
|
|
|
result_index += cwd.len;
|
2017-12-23 16:42:19 -08:00
|
|
|
const parsed_cwd = windowsParsePath(result[0..result_index]);
|
|
|
|
result_disk_designator = parsed_cwd.disk_designator;
|
|
|
|
if (parsed_cwd.kind == WindowsPath.Kind.Drive) {
|
|
|
|
result[0] = asciiUpper(result[0]);
|
|
|
|
}
|
|
|
|
have_drive_kind = parsed_cwd.kind;
|
2017-10-05 21:27:15 -07:00
|
|
|
}
|
2018-01-23 20:08:09 -08:00
|
|
|
errdefer allocator.free(result);
|
2017-10-05 21:27:15 -07:00
|
|
|
|
2017-12-23 16:42:19 -08:00
|
|
|
// Now we know the disk designator to use, if any, and what kind it is. And our result
|
|
|
|
// is big enough to append all the paths to.
|
|
|
|
var correct_disk_designator = true;
|
2017-10-05 21:27:15 -07:00
|
|
|
for (paths[first_index..]) |p, i| {
|
2017-12-23 16:42:19 -08:00
|
|
|
const parsed = windowsParsePath(p);
|
|
|
|
|
|
|
|
if (parsed.kind != WindowsPath.Kind.None) {
|
|
|
|
if (parsed.kind == have_drive_kind) {
|
2018-05-28 17:23:55 -07:00
|
|
|
correct_disk_designator = compareDiskDesignators(have_drive_kind, result_disk_designator, parsed.disk_designator);
|
2017-12-23 16:42:19 -08:00
|
|
|
} else {
|
2017-10-05 21:27:15 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2017-12-23 16:42:19 -08:00
|
|
|
if (!correct_disk_designator) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var it = mem.split(p[parsed.disk_designator.len..], "/\\");
|
2017-10-05 21:27:15 -07:00
|
|
|
while (it.next()) |component| {
|
|
|
|
if (mem.eql(u8, component, ".")) {
|
|
|
|
continue;
|
|
|
|
} else if (mem.eql(u8, component, "..")) {
|
|
|
|
while (true) {
|
2017-12-23 16:42:19 -08:00
|
|
|
if (result_index == 0 or result_index == result_disk_designator.len)
|
2017-10-05 21:27:15 -07:00
|
|
|
break;
|
|
|
|
result_index -= 1;
|
|
|
|
if (result[result_index] == '\\' or result[result_index] == '/')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2017-10-06 14:21:21 -07:00
|
|
|
result[result_index] = sep_windows;
|
2017-10-05 21:27:15 -07:00
|
|
|
result_index += 1;
|
|
|
|
mem.copy(u8, result[result_index..], component);
|
|
|
|
result_index += component.len;
|
|
|
|
}
|
|
|
|
}
|
2017-05-22 21:26:12 -07:00
|
|
|
}
|
2017-10-05 21:27:15 -07:00
|
|
|
|
2017-12-23 16:42:19 -08:00
|
|
|
if (result_index == result_disk_designator.len) {
|
2017-10-06 14:21:21 -07:00
|
|
|
result[result_index] = '\\';
|
|
|
|
result_index += 1;
|
|
|
|
}
|
|
|
|
|
2017-10-05 21:27:15 -07:00
|
|
|
return result[0..result_index];
|
|
|
|
}
|
|
|
|
|
2017-10-06 14:21:21 -07:00
|
|
|
/// This function is like a series of `cd` statements executed one after another.
|
|
|
|
/// It resolves "." and "..".
|
|
|
|
/// The result does not have a trailing path separator.
|
|
|
|
/// If all paths are relative it uses the current working directory as a starting point.
|
2018-01-31 19:48:40 -08:00
|
|
|
pub fn resolvePosix(allocator: &Allocator, paths: []const []const u8) ![]u8 {
|
2017-10-05 21:27:15 -07:00
|
|
|
if (paths.len == 0) {
|
|
|
|
assert(!is_windows); // resolvePosix called on windows can't use getCwd
|
2017-04-20 22:56:12 -07:00
|
|
|
return os.getCwd(allocator);
|
2017-10-05 21:27:15 -07:00
|
|
|
}
|
2017-04-18 22:13:15 -07:00
|
|
|
|
2017-04-20 22:56:12 -07:00
|
|
|
var first_index: usize = 0;
|
|
|
|
var have_abs = false;
|
|
|
|
var max_size: usize = 0;
|
|
|
|
for (paths) |p, i| {
|
2017-10-05 21:27:15 -07:00
|
|
|
if (isAbsolutePosix(p)) {
|
2017-04-20 22:56:12 -07:00
|
|
|
first_index = i;
|
|
|
|
have_abs = true;
|
|
|
|
max_size = 0;
|
|
|
|
}
|
|
|
|
max_size += p.len + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
var result: []u8 = undefined;
|
|
|
|
var result_index: usize = 0;
|
|
|
|
|
|
|
|
if (have_abs) {
|
2018-01-07 13:51:46 -08:00
|
|
|
result = try allocator.alloc(u8, max_size);
|
2017-04-20 22:56:12 -07:00
|
|
|
} else {
|
2017-10-05 21:27:15 -07:00
|
|
|
assert(!is_windows); // resolvePosix called on windows can't use getCwd
|
2018-01-07 13:51:46 -08:00
|
|
|
const cwd = try os.getCwd(allocator);
|
2017-04-20 22:56:12 -07:00
|
|
|
defer allocator.free(cwd);
|
2018-01-07 13:51:46 -08:00
|
|
|
result = try allocator.alloc(u8, max_size + cwd.len + 1);
|
2017-04-20 22:56:12 -07:00
|
|
|
mem.copy(u8, result, cwd);
|
|
|
|
result_index += cwd.len;
|
|
|
|
}
|
2018-01-23 20:08:09 -08:00
|
|
|
errdefer allocator.free(result);
|
2017-04-20 22:56:12 -07:00
|
|
|
|
2017-05-19 07:39:59 -07:00
|
|
|
for (paths[first_index..]) |p, i| {
|
2017-10-05 21:27:15 -07:00
|
|
|
var it = mem.split(p, "/");
|
2017-05-04 07:37:19 -07:00
|
|
|
while (it.next()) |component| {
|
2017-04-20 22:56:12 -07:00
|
|
|
if (mem.eql(u8, component, ".")) {
|
|
|
|
continue;
|
|
|
|
} else if (mem.eql(u8, component, "..")) {
|
|
|
|
while (true) {
|
|
|
|
if (result_index == 0)
|
|
|
|
break;
|
|
|
|
result_index -= 1;
|
|
|
|
if (result[result_index] == '/')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result[result_index] = '/';
|
|
|
|
result_index += 1;
|
2017-05-19 07:39:59 -07:00
|
|
|
mem.copy(u8, result[result_index..], component);
|
2017-04-20 22:56:12 -07:00
|
|
|
result_index += component.len;
|
|
|
|
}
|
2017-04-18 22:13:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 22:56:12 -07:00
|
|
|
if (result_index == 0) {
|
|
|
|
result[0] = '/';
|
|
|
|
result_index += 1;
|
|
|
|
}
|
|
|
|
|
2017-05-19 07:39:59 -07:00
|
|
|
return result[0..result_index];
|
2017-04-20 22:56:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "os.path.resolve" {
|
2018-01-08 21:07:01 -08:00
|
|
|
const cwd = try os.getCwd(debug.global_allocator);
|
2017-10-05 21:27:15 -07:00
|
|
|
if (is_windows) {
|
2017-12-23 16:42:19 -08:00
|
|
|
if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) {
|
|
|
|
cwd[0] = asciiUpper(cwd[0]);
|
|
|
|
}
|
2017-10-05 21:27:15 -07:00
|
|
|
assert(mem.eql(u8, testResolveWindows([][]const u8{"."}), cwd));
|
|
|
|
} else {
|
2018-05-28 17:23:55 -07:00
|
|
|
assert(mem.eql(u8, testResolvePosix([][]const u8{ "a/b/c/", "../../.." }), cwd));
|
2017-10-05 21:27:15 -07:00
|
|
|
assert(mem.eql(u8, testResolvePosix([][]const u8{"."}), cwd));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test "os.path.resolveWindows" {
|
2017-12-23 16:42:19 -08:00
|
|
|
if (is_windows) {
|
2018-01-08 21:07:01 -08:00
|
|
|
const cwd = try os.getCwd(debug.global_allocator);
|
2017-12-23 16:42:19 -08:00
|
|
|
const parsed_cwd = windowsParsePath(cwd);
|
|
|
|
{
|
2018-05-28 17:23:55 -07:00
|
|
|
const result = testResolveWindows([][]const u8{ "/usr/local", "lib\\zig\\std\\array_list.zig" });
|
|
|
|
const expected = try join(debug.global_allocator, parsed_cwd.disk_designator, "usr\\local\\lib\\zig\\std\\array_list.zig");
|
2017-12-23 16:42:19 -08:00
|
|
|
if (parsed_cwd.kind == WindowsPath.Kind.Drive) {
|
|
|
|
expected[0] = asciiUpper(parsed_cwd.disk_designator[0]);
|
|
|
|
}
|
|
|
|
assert(mem.eql(u8, result, expected));
|
|
|
|
}
|
|
|
|
{
|
2018-05-28 17:23:55 -07:00
|
|
|
const result = testResolveWindows([][]const u8{ "usr/local", "lib\\zig" });
|
2018-01-08 21:07:01 -08:00
|
|
|
const expected = try join(debug.global_allocator, cwd, "usr\\local\\lib\\zig");
|
2017-12-23 16:42:19 -08:00
|
|
|
if (parsed_cwd.kind == WindowsPath.Kind.Drive) {
|
|
|
|
expected[0] = asciiUpper(parsed_cwd.disk_designator[0]);
|
|
|
|
}
|
|
|
|
assert(mem.eql(u8, result, expected));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 17:23:55 -07:00
|
|
|
assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:\\a\\b\\c", "/hi", "ok" }), "C:\\hi\\ok"));
|
|
|
|
assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/blah\\blah", "d:/games", "c:../a" }), "C:\\blah\\a"));
|
|
|
|
assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/blah\\blah", "d:/games", "C:../a" }), "C:\\blah\\a"));
|
|
|
|
assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/ignore", "d:\\a/b\\c/d", "\\e.exe" }), "D:\\e.exe"));
|
|
|
|
assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/ignore", "c:/some/file" }), "C:\\some\\file"));
|
|
|
|
assert(mem.eql(u8, testResolveWindows([][]const u8{ "d:/ignore", "d:some/dir//" }), "D:\\ignore\\some\\dir"));
|
|
|
|
assert(mem.eql(u8, testResolveWindows([][]const u8{ "//server/share", "..", "relative\\" }), "\\\\server\\share\\relative"));
|
|
|
|
assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/", "//" }), "C:\\"));
|
|
|
|
assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/", "//dir" }), "C:\\dir"));
|
|
|
|
assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/", "//server/share" }), "\\\\server\\share\\"));
|
|
|
|
assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/", "//server//share" }), "\\\\server\\share\\"));
|
|
|
|
assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/", "///some//dir" }), "C:\\some\\dir"));
|
|
|
|
assert(mem.eql(u8, testResolveWindows([][]const u8{ "C:\\foo\\tmp.3\\", "..\\tmp.3\\cycles\\root.js" }), "C:\\foo\\tmp.3\\cycles\\root.js"));
|
2017-10-05 21:27:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "os.path.resolvePosix" {
|
2018-05-28 17:23:55 -07:00
|
|
|
assert(mem.eql(u8, testResolvePosix([][]const u8{ "/a/b", "c" }), "/a/b/c"));
|
|
|
|
assert(mem.eql(u8, testResolvePosix([][]const u8{ "/a/b", "c", "//d", "e///" }), "/d/e"));
|
|
|
|
assert(mem.eql(u8, testResolvePosix([][]const u8{ "/a/b/c", "..", "../" }), "/a"));
|
|
|
|
assert(mem.eql(u8, testResolvePosix([][]const u8{ "/", "..", ".." }), "/"));
|
2017-10-05 21:27:15 -07:00
|
|
|
assert(mem.eql(u8, testResolvePosix([][]const u8{"/a/b/c/"}), "/a/b/c"));
|
|
|
|
|
2018-05-28 17:23:55 -07:00
|
|
|
assert(mem.eql(u8, testResolvePosix([][]const u8{ "/var/lib", "../", "file/" }), "/var/file"));
|
|
|
|
assert(mem.eql(u8, testResolvePosix([][]const u8{ "/var/lib", "/../", "file/" }), "/file"));
|
|
|
|
assert(mem.eql(u8, testResolvePosix([][]const u8{ "/some/dir", ".", "/absolute/" }), "/absolute"));
|
|
|
|
assert(mem.eql(u8, testResolvePosix([][]const u8{ "/foo/tmp.3/", "../tmp.3/cycles/root.js" }), "/foo/tmp.3/cycles/root.js"));
|
2017-10-05 21:27:15 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn testResolveWindows(paths: []const []const u8) []u8 {
|
2018-01-08 21:07:01 -08:00
|
|
|
return resolveWindows(debug.global_allocator, paths) catch unreachable;
|
2017-04-20 22:56:12 -07:00
|
|
|
}
|
2017-10-05 21:27:15 -07:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn testResolvePosix(paths: []const []const u8) []u8 {
|
2018-01-08 21:07:01 -08:00
|
|
|
return resolvePosix(debug.global_allocator, paths) catch unreachable;
|
2017-04-20 22:56:12 -07:00
|
|
|
}
|
|
|
|
|
2018-05-29 14:38:50 -07:00
|
|
|
/// If the path is a file in the current directory (no directory component)
|
|
|
|
/// then the returned slice has .len = 0.
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn dirname(path: []const u8) []const u8 {
|
2017-10-05 21:27:15 -07:00
|
|
|
if (is_windows) {
|
|
|
|
return dirnameWindows(path);
|
|
|
|
} else {
|
|
|
|
return dirnamePosix(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn dirnameWindows(path: []const u8) []const u8 {
|
2017-10-05 21:27:15 -07:00
|
|
|
if (path.len == 0)
|
|
|
|
return path[0..0];
|
|
|
|
|
2017-10-08 18:52:26 -07:00
|
|
|
const root_slice = diskDesignatorWindows(path);
|
2017-10-06 14:21:21 -07:00
|
|
|
if (path.len == root_slice.len)
|
2017-10-05 21:27:15 -07:00
|
|
|
return path;
|
|
|
|
|
2017-10-06 14:21:21 -07:00
|
|
|
const have_root_slash = path.len > root_slice.len and (path[root_slice.len] == '/' or path[root_slice.len] == '\\');
|
2017-10-05 21:27:15 -07:00
|
|
|
|
|
|
|
var end_index: usize = path.len - 1;
|
|
|
|
|
2017-10-06 14:21:21 -07:00
|
|
|
while ((path[end_index] == '/' or path[end_index] == '\\') and end_index > root_slice.len) {
|
2017-10-05 21:27:15 -07:00
|
|
|
if (end_index == 0)
|
|
|
|
return path[0..0];
|
|
|
|
end_index -= 1;
|
|
|
|
}
|
|
|
|
|
2017-10-06 14:21:21 -07:00
|
|
|
while (path[end_index] != '/' and path[end_index] != '\\' and end_index > root_slice.len) {
|
2017-10-05 21:27:15 -07:00
|
|
|
if (end_index == 0)
|
|
|
|
return path[0..0];
|
|
|
|
end_index -= 1;
|
|
|
|
}
|
|
|
|
|
2017-10-06 14:21:21 -07:00
|
|
|
if (have_root_slash and end_index == root_slice.len) {
|
2017-10-05 21:27:15 -07:00
|
|
|
end_index += 1;
|
2017-05-22 21:26:12 -07:00
|
|
|
}
|
2017-10-05 21:27:15 -07:00
|
|
|
|
|
|
|
return path[0..end_index];
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn dirnamePosix(path: []const u8) []const u8 {
|
2017-04-20 22:56:12 -07:00
|
|
|
if (path.len == 0)
|
2017-05-19 07:39:59 -07:00
|
|
|
return path[0..0];
|
2017-10-05 21:27:15 -07:00
|
|
|
|
2017-04-20 22:56:12 -07:00
|
|
|
var end_index: usize = path.len - 1;
|
|
|
|
while (path[end_index] == '/') {
|
|
|
|
if (end_index == 0)
|
2017-05-19 07:39:59 -07:00
|
|
|
return path[0..1];
|
2017-04-20 22:56:12 -07:00
|
|
|
end_index -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (path[end_index] != '/') {
|
|
|
|
if (end_index == 0)
|
2017-05-19 07:39:59 -07:00
|
|
|
return path[0..0];
|
2017-04-20 22:56:12 -07:00
|
|
|
end_index -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end_index == 0 and path[end_index] == '/')
|
2017-05-19 07:39:59 -07:00
|
|
|
return path[0..1];
|
2017-04-20 22:56:12 -07:00
|
|
|
|
2017-05-19 07:39:59 -07:00
|
|
|
return path[0..end_index];
|
2017-04-20 22:56:12 -07:00
|
|
|
}
|
|
|
|
|
2017-10-05 21:27:15 -07:00
|
|
|
test "os.path.dirnamePosix" {
|
|
|
|
testDirnamePosix("/a/b/c", "/a/b");
|
|
|
|
testDirnamePosix("/a/b/c///", "/a/b");
|
|
|
|
testDirnamePosix("/a", "/");
|
|
|
|
testDirnamePosix("/", "/");
|
|
|
|
testDirnamePosix("////", "/");
|
|
|
|
testDirnamePosix("", "");
|
|
|
|
testDirnamePosix("a", "");
|
|
|
|
testDirnamePosix("a/", "");
|
|
|
|
testDirnamePosix("a//", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
test "os.path.dirnameWindows" {
|
|
|
|
testDirnameWindows("c:\\", "c:\\");
|
|
|
|
testDirnameWindows("c:\\foo", "c:\\");
|
|
|
|
testDirnameWindows("c:\\foo\\", "c:\\");
|
|
|
|
testDirnameWindows("c:\\foo\\bar", "c:\\foo");
|
|
|
|
testDirnameWindows("c:\\foo\\bar\\", "c:\\foo");
|
|
|
|
testDirnameWindows("c:\\foo\\bar\\baz", "c:\\foo\\bar");
|
|
|
|
testDirnameWindows("\\", "\\");
|
|
|
|
testDirnameWindows("\\foo", "\\");
|
|
|
|
testDirnameWindows("\\foo\\", "\\");
|
|
|
|
testDirnameWindows("\\foo\\bar", "\\foo");
|
|
|
|
testDirnameWindows("\\foo\\bar\\", "\\foo");
|
|
|
|
testDirnameWindows("\\foo\\bar\\baz", "\\foo\\bar");
|
|
|
|
testDirnameWindows("c:", "c:");
|
|
|
|
testDirnameWindows("c:foo", "c:");
|
|
|
|
testDirnameWindows("c:foo\\", "c:");
|
|
|
|
testDirnameWindows("c:foo\\bar", "c:foo");
|
|
|
|
testDirnameWindows("c:foo\\bar\\", "c:foo");
|
|
|
|
testDirnameWindows("c:foo\\bar\\baz", "c:foo\\bar");
|
|
|
|
testDirnameWindows("file:stream", "");
|
|
|
|
testDirnameWindows("dir\\file:stream", "dir");
|
|
|
|
testDirnameWindows("\\\\unc\\share", "\\\\unc\\share");
|
|
|
|
testDirnameWindows("\\\\unc\\share\\foo", "\\\\unc\\share\\");
|
|
|
|
testDirnameWindows("\\\\unc\\share\\foo\\", "\\\\unc\\share\\");
|
|
|
|
testDirnameWindows("\\\\unc\\share\\foo\\bar", "\\\\unc\\share\\foo");
|
|
|
|
testDirnameWindows("\\\\unc\\share\\foo\\bar\\", "\\\\unc\\share\\foo");
|
|
|
|
testDirnameWindows("\\\\unc\\share\\foo\\bar\\baz", "\\\\unc\\share\\foo\\bar");
|
|
|
|
testDirnameWindows("/a/b/", "/a");
|
|
|
|
testDirnameWindows("/a/b", "/a");
|
|
|
|
testDirnameWindows("/a", "/");
|
|
|
|
testDirnameWindows("", "");
|
|
|
|
testDirnameWindows("/", "/");
|
|
|
|
testDirnameWindows("////", "/");
|
|
|
|
testDirnameWindows("foo", "");
|
2017-04-20 22:56:12 -07:00
|
|
|
}
|
2017-10-05 21:27:15 -07:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn testDirnamePosix(input: []const u8, expected_output: []const u8) void {
|
2017-10-05 21:27:15 -07:00
|
|
|
assert(mem.eql(u8, dirnamePosix(input), expected_output));
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn testDirnameWindows(input: []const u8, expected_output: []const u8) void {
|
2017-10-05 21:27:15 -07:00
|
|
|
assert(mem.eql(u8, dirnameWindows(input), expected_output));
|
2017-04-17 03:45:44 -07:00
|
|
|
}
|
2017-04-29 16:23:33 -07:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn basename(path: []const u8) []const u8 {
|
2017-10-08 16:18:47 -07:00
|
|
|
if (is_windows) {
|
|
|
|
return basenameWindows(path);
|
|
|
|
} else {
|
|
|
|
return basenamePosix(path);
|
2017-05-22 21:26:12 -07:00
|
|
|
}
|
2017-10-08 16:18:47 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn basenamePosix(path: []const u8) []const u8 {
|
2017-04-30 15:56:24 -07:00
|
|
|
if (path.len == 0)
|
|
|
|
return []u8{};
|
|
|
|
|
|
|
|
var end_index: usize = path.len - 1;
|
|
|
|
while (path[end_index] == '/') {
|
|
|
|
if (end_index == 0)
|
|
|
|
return []u8{};
|
|
|
|
end_index -= 1;
|
|
|
|
}
|
|
|
|
var start_index: usize = end_index;
|
|
|
|
end_index += 1;
|
|
|
|
while (path[start_index] != '/') {
|
|
|
|
if (start_index == 0)
|
2017-05-19 07:39:59 -07:00
|
|
|
return path[0..end_index];
|
2017-04-30 15:56:24 -07:00
|
|
|
start_index -= 1;
|
|
|
|
}
|
|
|
|
|
2018-05-30 13:09:11 -07:00
|
|
|
return path[start_index + 1 .. end_index];
|
2017-04-30 15:56:24 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn basenameWindows(path: []const u8) []const u8 {
|
2017-10-08 16:18:47 -07:00
|
|
|
if (path.len == 0)
|
|
|
|
return []u8{};
|
|
|
|
|
|
|
|
var end_index: usize = path.len - 1;
|
|
|
|
while (true) {
|
|
|
|
const byte = path[end_index];
|
|
|
|
if (byte == '/' or byte == '\\') {
|
|
|
|
if (end_index == 0)
|
|
|
|
return []u8{};
|
|
|
|
end_index -= 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (byte == ':' and end_index == 1) {
|
|
|
|
return []u8{};
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
var start_index: usize = end_index;
|
|
|
|
end_index += 1;
|
|
|
|
while (path[start_index] != '/' and path[start_index] != '\\' and
|
|
|
|
!(path[start_index] == ':' and start_index == 1))
|
|
|
|
{
|
|
|
|
if (start_index == 0)
|
|
|
|
return path[0..end_index];
|
|
|
|
start_index -= 1;
|
|
|
|
}
|
|
|
|
|
2018-05-30 13:09:11 -07:00
|
|
|
return path[start_index + 1 .. end_index];
|
2017-10-08 16:18:47 -07:00
|
|
|
}
|
|
|
|
|
2017-04-30 15:56:24 -07:00
|
|
|
test "os.path.basename" {
|
|
|
|
testBasename("", "");
|
|
|
|
testBasename("/", "");
|
|
|
|
testBasename("/dir/basename.ext", "basename.ext");
|
|
|
|
testBasename("/basename.ext", "basename.ext");
|
|
|
|
testBasename("basename.ext", "basename.ext");
|
|
|
|
testBasename("basename.ext/", "basename.ext");
|
|
|
|
testBasename("basename.ext//", "basename.ext");
|
|
|
|
testBasename("/aaa/bbb", "bbb");
|
|
|
|
testBasename("/aaa/", "aaa");
|
|
|
|
testBasename("/aaa/b", "b");
|
|
|
|
testBasename("/a/b", "b");
|
|
|
|
testBasename("//a", "a");
|
2017-10-08 16:18:47 -07:00
|
|
|
|
|
|
|
testBasenamePosix("\\dir\\basename.ext", "\\dir\\basename.ext");
|
|
|
|
testBasenamePosix("\\basename.ext", "\\basename.ext");
|
|
|
|
testBasenamePosix("basename.ext", "basename.ext");
|
|
|
|
testBasenamePosix("basename.ext\\", "basename.ext\\");
|
|
|
|
testBasenamePosix("basename.ext\\\\", "basename.ext\\\\");
|
|
|
|
testBasenamePosix("foo", "foo");
|
|
|
|
|
|
|
|
testBasenameWindows("\\dir\\basename.ext", "basename.ext");
|
|
|
|
testBasenameWindows("\\basename.ext", "basename.ext");
|
|
|
|
testBasenameWindows("basename.ext", "basename.ext");
|
|
|
|
testBasenameWindows("basename.ext\\", "basename.ext");
|
|
|
|
testBasenameWindows("basename.ext\\\\", "basename.ext");
|
|
|
|
testBasenameWindows("foo", "foo");
|
|
|
|
testBasenameWindows("C:", "");
|
|
|
|
testBasenameWindows("C:.", ".");
|
|
|
|
testBasenameWindows("C:\\", "");
|
|
|
|
testBasenameWindows("C:\\dir\\base.ext", "base.ext");
|
|
|
|
testBasenameWindows("C:\\basename.ext", "basename.ext");
|
|
|
|
testBasenameWindows("C:basename.ext", "basename.ext");
|
|
|
|
testBasenameWindows("C:basename.ext\\", "basename.ext");
|
|
|
|
testBasenameWindows("C:basename.ext\\\\", "basename.ext");
|
|
|
|
testBasenameWindows("C:foo", "foo");
|
|
|
|
testBasenameWindows("file:stream", "file:stream");
|
2017-04-30 15:56:24 -07:00
|
|
|
}
|
2017-10-08 16:18:47 -07:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn testBasename(input: []const u8, expected_output: []const u8) void {
|
2017-04-30 15:56:24 -07:00
|
|
|
assert(mem.eql(u8, basename(input), expected_output));
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn testBasenamePosix(input: []const u8, expected_output: []const u8) void {
|
2017-10-08 16:18:47 -07:00
|
|
|
assert(mem.eql(u8, basenamePosix(input), expected_output));
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn testBasenameWindows(input: []const u8, expected_output: []const u8) void {
|
2017-10-08 16:18:47 -07:00
|
|
|
assert(mem.eql(u8, basenameWindows(input), expected_output));
|
|
|
|
}
|
|
|
|
|
2017-10-08 18:16:10 -07:00
|
|
|
/// Returns the relative path from `from` to `to`. If `from` and `to` each
|
|
|
|
/// resolve to the same path (after calling `resolve` on each), a zero-length
|
2017-04-29 16:23:33 -07:00
|
|
|
/// string is returned.
|
2017-10-08 18:16:10 -07:00
|
|
|
/// On Windows this canonicalizes the drive to a capital letter and paths to `\\`.
|
2018-01-31 19:48:40 -08:00
|
|
|
pub fn relative(allocator: &Allocator, from: []const u8, to: []const u8) ![]u8 {
|
2017-10-05 21:27:15 -07:00
|
|
|
if (is_windows) {
|
2017-10-08 18:16:10 -07:00
|
|
|
return relativeWindows(allocator, from, to);
|
2017-10-05 21:27:15 -07:00
|
|
|
} else {
|
2017-10-08 18:16:10 -07:00
|
|
|
return relativePosix(allocator, from, to);
|
2017-05-22 21:26:12 -07:00
|
|
|
}
|
2017-10-05 21:27:15 -07:00
|
|
|
}
|
|
|
|
|
2018-01-31 19:48:40 -08:00
|
|
|
pub fn relativeWindows(allocator: &Allocator, from: []const u8, to: []const u8) ![]u8 {
|
2018-01-07 13:51:46 -08:00
|
|
|
const resolved_from = try resolveWindows(allocator, [][]const u8{from});
|
2017-10-08 18:16:10 -07:00
|
|
|
defer allocator.free(resolved_from);
|
|
|
|
|
|
|
|
var clean_up_resolved_to = true;
|
2018-01-07 13:51:46 -08:00
|
|
|
const resolved_to = try resolveWindows(allocator, [][]const u8{to});
|
2017-10-08 18:16:10 -07:00
|
|
|
defer if (clean_up_resolved_to) allocator.free(resolved_to);
|
|
|
|
|
2017-12-23 16:42:19 -08:00
|
|
|
const parsed_from = windowsParsePath(resolved_from);
|
|
|
|
const parsed_to = windowsParsePath(resolved_to);
|
|
|
|
const result_is_to = x: {
|
|
|
|
if (parsed_from.kind != parsed_to.kind) {
|
|
|
|
break :x true;
|
|
|
|
} else switch (parsed_from.kind) {
|
|
|
|
WindowsPath.Kind.NetworkShare => {
|
|
|
|
break :x !networkShareServersEql(parsed_to.disk_designator, parsed_from.disk_designator);
|
|
|
|
},
|
|
|
|
WindowsPath.Kind.Drive => {
|
|
|
|
break :x asciiUpper(parsed_from.disk_designator[0]) != asciiUpper(parsed_to.disk_designator[0]);
|
|
|
|
},
|
|
|
|
else => unreachable,
|
|
|
|
}
|
|
|
|
};
|
2017-12-21 21:50:30 -08:00
|
|
|
|
2017-10-08 18:16:10 -07:00
|
|
|
if (result_is_to) {
|
|
|
|
clean_up_resolved_to = false;
|
|
|
|
return resolved_to;
|
|
|
|
}
|
|
|
|
|
|
|
|
var from_it = mem.split(resolved_from, "/\\");
|
|
|
|
var to_it = mem.split(resolved_to, "/\\");
|
|
|
|
while (true) {
|
|
|
|
const from_component = from_it.next() ?? return mem.dupe(allocator, u8, to_it.rest());
|
|
|
|
const to_rest = to_it.rest();
|
|
|
|
if (to_it.next()) |to_component| {
|
|
|
|
// TODO ASCII is wrong, we actually need full unicode support to compare paths.
|
|
|
|
if (asciiEqlIgnoreCase(from_component, to_component))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var up_count: usize = 1;
|
|
|
|
while (from_it.next()) |_| {
|
|
|
|
up_count += 1;
|
|
|
|
}
|
|
|
|
const up_index_end = up_count * "..\\".len;
|
2018-01-07 13:51:46 -08:00
|
|
|
const result = try allocator.alloc(u8, up_index_end + to_rest.len);
|
2018-01-23 20:08:09 -08:00
|
|
|
errdefer allocator.free(result);
|
2017-10-08 18:16:10 -07:00
|
|
|
|
|
|
|
var result_index: usize = 0;
|
|
|
|
while (result_index < up_index_end) {
|
|
|
|
result[result_index] = '.';
|
|
|
|
result_index += 1;
|
|
|
|
result[result_index] = '.';
|
|
|
|
result_index += 1;
|
|
|
|
result[result_index] = '\\';
|
|
|
|
result_index += 1;
|
|
|
|
}
|
|
|
|
// shave off the trailing slash
|
|
|
|
result_index -= 1;
|
|
|
|
|
|
|
|
var rest_it = mem.split(to_rest, "/\\");
|
|
|
|
while (rest_it.next()) |to_component| {
|
|
|
|
result[result_index] = '\\';
|
|
|
|
result_index += 1;
|
|
|
|
mem.copy(u8, result[result_index..], to_component);
|
|
|
|
result_index += to_component.len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result[0..result_index];
|
|
|
|
}
|
|
|
|
|
|
|
|
return []u8{};
|
2017-10-05 21:27:15 -07:00
|
|
|
}
|
|
|
|
|
2018-01-31 19:48:40 -08:00
|
|
|
pub fn relativePosix(allocator: &Allocator, from: []const u8, to: []const u8) ![]u8 {
|
2018-01-07 13:51:46 -08:00
|
|
|
const resolved_from = try resolvePosix(allocator, [][]const u8{from});
|
2017-04-29 16:23:33 -07:00
|
|
|
defer allocator.free(resolved_from);
|
|
|
|
|
2018-01-07 13:51:46 -08:00
|
|
|
const resolved_to = try resolvePosix(allocator, [][]const u8{to});
|
2017-04-29 16:23:33 -07:00
|
|
|
defer allocator.free(resolved_to);
|
|
|
|
|
2017-10-06 14:21:21 -07:00
|
|
|
var from_it = mem.split(resolved_from, "/");
|
|
|
|
var to_it = mem.split(resolved_to, "/");
|
2017-04-29 16:23:33 -07:00
|
|
|
while (true) {
|
|
|
|
const from_component = from_it.next() ?? return mem.dupe(allocator, u8, to_it.rest());
|
|
|
|
const to_rest = to_it.rest();
|
2017-05-03 14:23:11 -07:00
|
|
|
if (to_it.next()) |to_component| {
|
2017-04-29 16:23:33 -07:00
|
|
|
if (mem.eql(u8, from_component, to_component))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var up_count: usize = 1;
|
2017-05-04 07:37:19 -07:00
|
|
|
while (from_it.next()) |_| {
|
2017-04-29 16:23:33 -07:00
|
|
|
up_count += 1;
|
|
|
|
}
|
|
|
|
const up_index_end = up_count * "../".len;
|
2018-01-07 13:51:46 -08:00
|
|
|
const result = try allocator.alloc(u8, up_index_end + to_rest.len);
|
2018-01-23 20:08:09 -08:00
|
|
|
errdefer allocator.free(result);
|
2017-04-29 16:23:33 -07:00
|
|
|
|
|
|
|
var result_index: usize = 0;
|
|
|
|
while (result_index < up_index_end) {
|
|
|
|
result[result_index] = '.';
|
|
|
|
result_index += 1;
|
|
|
|
result[result_index] = '.';
|
|
|
|
result_index += 1;
|
|
|
|
result[result_index] = '/';
|
|
|
|
result_index += 1;
|
|
|
|
}
|
|
|
|
if (to_rest.len == 0) {
|
|
|
|
// shave off the trailing slash
|
2018-05-30 13:09:11 -07:00
|
|
|
return result[0 .. result_index - 1];
|
2017-04-29 16:23:33 -07:00
|
|
|
}
|
|
|
|
|
2017-05-19 07:39:59 -07:00
|
|
|
mem.copy(u8, result[result_index..], to_rest);
|
2017-04-29 16:23:33 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return []u8{};
|
|
|
|
}
|
|
|
|
|
|
|
|
test "os.path.relative" {
|
2017-10-08 18:16:10 -07:00
|
|
|
testRelativeWindows("c:/blah\\blah", "d:/games", "D:\\games");
|
|
|
|
testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa", "..");
|
|
|
|
testRelativeWindows("c:/aaaa/bbbb", "c:/cccc", "..\\..\\cccc");
|
|
|
|
testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa/bbbb", "");
|
|
|
|
testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa/cccc", "..\\cccc");
|
|
|
|
testRelativeWindows("c:/aaaa/", "c:/aaaa/cccc", "cccc");
|
|
|
|
testRelativeWindows("c:/", "c:\\aaaa\\bbbb", "aaaa\\bbbb");
|
|
|
|
testRelativeWindows("c:/aaaa/bbbb", "d:\\", "D:\\");
|
|
|
|
testRelativeWindows("c:/AaAa/bbbb", "c:/aaaa/bbbb", "");
|
|
|
|
testRelativeWindows("c:/aaaaa/", "c:/aaaa/cccc", "..\\aaaa\\cccc");
|
|
|
|
testRelativeWindows("C:\\foo\\bar\\baz\\quux", "C:\\", "..\\..\\..\\..");
|
|
|
|
testRelativeWindows("C:\\foo\\test", "C:\\foo\\test\\bar\\package.json", "bar\\package.json");
|
|
|
|
testRelativeWindows("C:\\foo\\bar\\baz-quux", "C:\\foo\\bar\\baz", "..\\baz");
|
|
|
|
testRelativeWindows("C:\\foo\\bar\\baz", "C:\\foo\\bar\\baz-quux", "..\\baz-quux");
|
|
|
|
testRelativeWindows("\\\\foo\\bar", "\\\\foo\\bar\\baz", "baz");
|
|
|
|
testRelativeWindows("\\\\foo\\bar\\baz", "\\\\foo\\bar", "..");
|
|
|
|
testRelativeWindows("\\\\foo\\bar\\baz-quux", "\\\\foo\\bar\\baz", "..\\baz");
|
|
|
|
testRelativeWindows("\\\\foo\\bar\\baz", "\\\\foo\\bar\\baz-quux", "..\\baz-quux");
|
|
|
|
testRelativeWindows("C:\\baz-quux", "C:\\baz", "..\\baz");
|
|
|
|
testRelativeWindows("C:\\baz", "C:\\baz-quux", "..\\baz-quux");
|
|
|
|
testRelativeWindows("\\\\foo\\baz-quux", "\\\\foo\\baz", "..\\baz");
|
|
|
|
testRelativeWindows("\\\\foo\\baz", "\\\\foo\\baz-quux", "..\\baz-quux");
|
|
|
|
testRelativeWindows("C:\\baz", "\\\\foo\\bar\\baz", "\\\\foo\\bar\\baz");
|
|
|
|
testRelativeWindows("\\\\foo\\bar\\baz", "C:\\baz", "C:\\baz");
|
|
|
|
|
|
|
|
testRelativePosix("/var/lib", "/var", "..");
|
|
|
|
testRelativePosix("/var/lib", "/bin", "../../bin");
|
|
|
|
testRelativePosix("/var/lib", "/var/lib", "");
|
|
|
|
testRelativePosix("/var/lib", "/var/apache", "../apache");
|
|
|
|
testRelativePosix("/var/", "/var/lib", "lib");
|
|
|
|
testRelativePosix("/", "/var/lib", "var/lib");
|
|
|
|
testRelativePosix("/foo/test", "/foo/test/bar/package.json", "bar/package.json");
|
|
|
|
testRelativePosix("/Users/a/web/b/test/mails", "/Users/a/web/b", "../..");
|
|
|
|
testRelativePosix("/foo/bar/baz-quux", "/foo/bar/baz", "../baz");
|
|
|
|
testRelativePosix("/foo/bar/baz", "/foo/bar/baz-quux", "../baz-quux");
|
|
|
|
testRelativePosix("/baz-quux", "/baz", "../baz");
|
|
|
|
testRelativePosix("/baz", "/baz-quux", "../baz-quux");
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn testRelativePosix(from: []const u8, to: []const u8, expected_output: []const u8) void {
|
2018-01-08 21:07:01 -08:00
|
|
|
const result = relativePosix(debug.global_allocator, from, to) catch unreachable;
|
2017-10-08 18:16:10 -07:00
|
|
|
assert(mem.eql(u8, result, expected_output));
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []const u8) void {
|
2018-01-08 21:07:01 -08:00
|
|
|
const result = relativeWindows(debug.global_allocator, from, to) catch unreachable;
|
2017-04-29 16:23:33 -07:00
|
|
|
assert(mem.eql(u8, result, expected_output));
|
|
|
|
}
|
2017-04-30 15:56:24 -07:00
|
|
|
|
|
|
|
/// Return the canonicalized absolute pathname.
|
|
|
|
/// Expands all symbolic links and resolves references to `.`, `..`, and
|
|
|
|
/// extra `/` characters in ::pathname.
|
|
|
|
/// Caller must deallocate result.
|
2018-01-31 19:48:40 -08:00
|
|
|
pub fn real(allocator: &Allocator, pathname: []const u8) ![]u8 {
|
2017-08-27 14:16:42 -07:00
|
|
|
switch (builtin.os) {
|
2017-10-09 11:21:35 -07:00
|
|
|
Os.windows => {
|
2018-01-07 13:51:46 -08:00
|
|
|
const pathname_buf = try allocator.alloc(u8, pathname.len + 1);
|
2017-10-09 11:21:35 -07:00
|
|
|
defer allocator.free(pathname_buf);
|
|
|
|
|
|
|
|
mem.copy(u8, pathname_buf, pathname);
|
|
|
|
pathname_buf[pathname.len] = 0;
|
|
|
|
|
2018-05-28 17:23:55 -07:00
|
|
|
const h_file = windows.CreateFileA(pathname_buf.ptr, windows.GENERIC_READ, windows.FILE_SHARE_READ, null, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL, null);
|
2017-10-09 11:21:35 -07:00
|
|
|
if (h_file == windows.INVALID_HANDLE_VALUE) {
|
|
|
|
const err = windows.GetLastError();
|
|
|
|
return switch (err) {
|
|
|
|
windows.ERROR.FILE_NOT_FOUND => error.FileNotFound,
|
|
|
|
windows.ERROR.ACCESS_DENIED => error.AccessDenied,
|
|
|
|
windows.ERROR.FILENAME_EXCED_RANGE => error.NameTooLong,
|
2017-10-15 13:45:43 -07:00
|
|
|
else => os.unexpectedErrorWindows(err),
|
2017-10-09 11:21:35 -07:00
|
|
|
};
|
|
|
|
}
|
2017-10-31 01:47:55 -07:00
|
|
|
defer os.close(h_file);
|
2018-01-07 13:51:46 -08:00
|
|
|
var buf = try allocator.alloc(u8, 256);
|
2018-01-23 20:08:09 -08:00
|
|
|
errdefer allocator.free(buf);
|
2017-10-09 11:21:35 -07:00
|
|
|
while (true) {
|
2018-01-07 14:28:20 -08:00
|
|
|
const buf_len = math.cast(windows.DWORD, buf.len) catch return error.NameTooLong;
|
2017-10-09 11:21:35 -07:00
|
|
|
const result = windows.GetFinalPathNameByHandleA(h_file, buf.ptr, buf_len, windows.VOLUME_NAME_DOS);
|
|
|
|
|
|
|
|
if (result == 0) {
|
|
|
|
const err = windows.GetLastError();
|
|
|
|
return switch (err) {
|
|
|
|
windows.ERROR.PATH_NOT_FOUND => error.FileNotFound,
|
|
|
|
windows.ERROR.NOT_ENOUGH_MEMORY => error.OutOfMemory,
|
|
|
|
windows.ERROR.INVALID_PARAMETER => unreachable,
|
2017-10-15 13:45:43 -07:00
|
|
|
else => os.unexpectedErrorWindows(err),
|
2017-10-09 11:21:35 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result > buf.len) {
|
2018-01-07 13:51:46 -08:00
|
|
|
buf = try allocator.realloc(u8, buf, result);
|
2017-10-09 11:21:35 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// windows returns \\?\ prepended to the path
|
|
|
|
// we strip it because nobody wants \\?\ prepended to their path
|
2017-12-21 21:50:30 -08:00
|
|
|
const final_len = x: {
|
|
|
|
if (result > 4 and mem.startsWith(u8, buf, "\\\\?\\")) {
|
|
|
|
var i: usize = 4;
|
|
|
|
while (i < result) : (i += 1) {
|
|
|
|
buf[i - 4] = buf[i];
|
|
|
|
}
|
|
|
|
break :x result - 4;
|
|
|
|
} else {
|
|
|
|
break :x result;
|
2017-10-09 11:21:35 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return allocator.shrink(u8, buf, final_len);
|
|
|
|
}
|
|
|
|
},
|
2018-01-06 20:10:53 -08:00
|
|
|
Os.macosx, Os.ios => {
|
2017-08-27 14:16:42 -07:00
|
|
|
// TODO instead of calling the libc function here, port the implementation
|
|
|
|
// to Zig, and then remove the NameTooLong error possibility.
|
2018-01-07 13:51:46 -08:00
|
|
|
const pathname_buf = try allocator.alloc(u8, pathname.len + 1);
|
2017-08-27 14:16:42 -07:00
|
|
|
defer allocator.free(pathname_buf);
|
|
|
|
|
2018-01-07 13:51:46 -08:00
|
|
|
const result_buf = try allocator.alloc(u8, posix.PATH_MAX);
|
2018-01-23 20:08:09 -08:00
|
|
|
errdefer allocator.free(result_buf);
|
2017-08-27 14:16:42 -07:00
|
|
|
|
|
|
|
mem.copy(u8, pathname_buf, pathname);
|
|
|
|
pathname_buf[pathname.len] = 0;
|
|
|
|
|
|
|
|
const err = posix.getErrno(posix.realpath(pathname_buf.ptr, result_buf.ptr));
|
|
|
|
if (err > 0) {
|
|
|
|
return switch (err) {
|
|
|
|
posix.EINVAL => unreachable,
|
|
|
|
posix.EBADF => unreachable,
|
|
|
|
posix.EFAULT => unreachable,
|
|
|
|
posix.EACCES => error.AccessDenied,
|
|
|
|
posix.ENOENT => error.FileNotFound,
|
|
|
|
posix.ENOTSUP => error.NotSupported,
|
|
|
|
posix.ENOTDIR => error.NotDir,
|
|
|
|
posix.ENAMETOOLONG => error.NameTooLong,
|
|
|
|
posix.ELOOP => error.SymLinkLoop,
|
|
|
|
posix.EIO => error.InputOutput,
|
2017-10-15 13:45:43 -07:00
|
|
|
else => os.unexpectedErrorPosix(err),
|
2017-08-27 14:16:42 -07:00
|
|
|
};
|
|
|
|
}
|
2017-10-10 08:31:53 -07:00
|
|
|
return allocator.shrink(u8, result_buf, cstr.len(result_buf.ptr));
|
2017-08-27 14:16:42 -07:00
|
|
|
},
|
|
|
|
Os.linux => {
|
2018-05-28 17:23:55 -07:00
|
|
|
const fd = try os.posixOpen(allocator, pathname, posix.O_PATH | posix.O_NONBLOCK | posix.O_CLOEXEC, 0);
|
2017-10-31 01:47:55 -07:00
|
|
|
defer os.close(fd);
|
2017-08-27 14:16:42 -07:00
|
|
|
|
|
|
|
var buf: ["/proc/self/fd/-2147483648".len]u8 = undefined;
|
2018-01-08 21:07:01 -08:00
|
|
|
const proc_path = fmt.bufPrint(buf[0..], "/proc/self/fd/{}", fd) catch unreachable;
|
2017-08-27 14:16:42 -07:00
|
|
|
|
|
|
|
return os.readLink(allocator, proc_path);
|
|
|
|
},
|
2017-12-03 17:43:56 -08:00
|
|
|
else => @compileError("TODO implement os.path.real for " ++ @tagName(builtin.os)),
|
2017-05-22 21:26:12 -07:00
|
|
|
}
|
2017-04-30 15:56:24 -07:00
|
|
|
}
|
2017-10-09 11:21:35 -07:00
|
|
|
|
|
|
|
test "os.path.real" {
|
|
|
|
// at least call it so it gets compiled
|
2017-11-10 11:02:45 -08:00
|
|
|
_ = real(debug.global_allocator, "some_path");
|
2017-10-09 11:21:35 -07:00
|
|
|
}
|