mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 05:44:20 +00:00
parent
7b908cb024
commit
efe06c5f31
2 changed files with 43 additions and 29 deletions
|
|
@ -29,7 +29,7 @@ pub const Diagnostics = struct {
|
||||||
allocator: std.mem.Allocator,
|
allocator: std.mem.Allocator,
|
||||||
errors: std.ArrayListUnmanaged(Error) = .{},
|
errors: std.ArrayListUnmanaged(Error) = .{},
|
||||||
|
|
||||||
root_entries: usize = 0,
|
entries: usize = 0,
|
||||||
root_dir: []const u8 = "",
|
root_dir: []const u8 = "",
|
||||||
|
|
||||||
pub const Error = union(enum) {
|
pub const Error = union(enum) {
|
||||||
|
|
@ -48,41 +48,40 @@ pub const Diagnostics = struct {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
fn findRoot(d: *Diagnostics, path: []const u8, kind: FileKind) !void {
|
fn findRoot(d: *Diagnostics, path: []const u8) !void {
|
||||||
if (rootDir(path)) |root_dir| {
|
if (path.len == 0) return;
|
||||||
d.root_entries += 1;
|
|
||||||
if (kind == .directory and d.root_entries == 1) {
|
d.entries += 1;
|
||||||
d.root_dir = try d.allocator.dupe(u8, root_dir);
|
const root_dir = rootDir(path);
|
||||||
return;
|
if (d.entries == 1) {
|
||||||
}
|
d.root_dir = try d.allocator.dupe(u8, root_dir);
|
||||||
d.allocator.free(d.root_dir);
|
return;
|
||||||
d.root_dir = "";
|
|
||||||
}
|
}
|
||||||
|
if (d.root_dir.len == 0 or std.mem.eql(u8, root_dir, d.root_dir))
|
||||||
|
return;
|
||||||
|
d.allocator.free(d.root_dir);
|
||||||
|
d.root_dir = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
// If path is package root returns root_dir name, otherwise null.
|
// Returns root dir of the path, assumes non empty path.
|
||||||
fn rootDir(path: []const u8) ?[]const u8 {
|
fn rootDir(path: []const u8) []const u8 {
|
||||||
if (path.len == 0) return null;
|
|
||||||
|
|
||||||
const start_index: usize = if (path[0] == '/') 1 else 0;
|
const start_index: usize = if (path[0] == '/') 1 else 0;
|
||||||
const end_index: usize = if (path[path.len - 1] == '/') path.len - 1 else path.len;
|
const end_index: usize = if (path[path.len - 1] == '/') path.len - 1 else path.len;
|
||||||
const buf = path[start_index..end_index];
|
const buf = path[start_index..end_index];
|
||||||
return if (std.mem.indexOfScalarPos(u8, buf, 0, '/') == null)
|
if (std.mem.indexOfScalarPos(u8, buf, 0, '/')) |idx| {
|
||||||
buf
|
return buf[0..idx];
|
||||||
else
|
}
|
||||||
null;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
test rootDir {
|
test rootDir {
|
||||||
const expectEqualStrings = testing.expectEqualStrings;
|
const expectEqualStrings = testing.expectEqualStrings;
|
||||||
const expect = testing.expect;
|
try expectEqualStrings("a", rootDir("a"));
|
||||||
|
try expectEqualStrings("b", rootDir("b"));
|
||||||
try expectEqualStrings("a", rootDir("a").?);
|
try expectEqualStrings("c", rootDir("/c"));
|
||||||
try expectEqualStrings("b", rootDir("b").?);
|
try expectEqualStrings("d", rootDir("/d/"));
|
||||||
try expectEqualStrings("c", rootDir("/c").?);
|
try expectEqualStrings("a", rootDir("a/b"));
|
||||||
try expectEqualStrings("d", rootDir("/d/").?);
|
try expectEqualStrings("a", rootDir("a/b/c"));
|
||||||
try expect(rootDir("a/b") == null);
|
|
||||||
try expect(rootDir("") == null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(d: *Diagnostics) void {
|
pub fn deinit(d: *Diagnostics) void {
|
||||||
|
|
@ -625,7 +624,7 @@ pub fn pipeToFileSystem(dir: std.fs.Dir, reader: anytype, options: PipeOptions)
|
||||||
while (try iter.next()) |file| {
|
while (try iter.next()) |file| {
|
||||||
const file_name = stripComponents(file.name, options.strip_components);
|
const file_name = stripComponents(file.name, options.strip_components);
|
||||||
if (options.diagnostics) |d| {
|
if (options.diagnostics) |d| {
|
||||||
try d.findRoot(file_name, file.kind);
|
try d.findRoot(file_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (file.kind) {
|
switch (file.kind) {
|
||||||
|
|
@ -1056,7 +1055,7 @@ test "pipeToFileSystem root_dir" {
|
||||||
|
|
||||||
// there is no root_dir
|
// there is no root_dir
|
||||||
try testing.expectEqual(0, diagnostics.root_dir.len);
|
try testing.expectEqual(0, diagnostics.root_dir.len);
|
||||||
try testing.expectEqual(3, diagnostics.root_entries);
|
try testing.expectEqual(5, diagnostics.entries);
|
||||||
}
|
}
|
||||||
|
|
||||||
// with strip_components = 0
|
// with strip_components = 0
|
||||||
|
|
@ -1078,10 +1077,25 @@ test "pipeToFileSystem root_dir" {
|
||||||
|
|
||||||
// root_dir found
|
// root_dir found
|
||||||
try testing.expectEqualStrings("example", diagnostics.root_dir);
|
try testing.expectEqualStrings("example", diagnostics.root_dir);
|
||||||
try testing.expectEqual(1, diagnostics.root_entries);
|
try testing.expectEqual(6, diagnostics.entries);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "findRoot without explicit root dir" {
|
||||||
|
const data = @embedFile("tar/testdata/19820.tar");
|
||||||
|
var fbs = std.io.fixedBufferStream(data);
|
||||||
|
const reader = fbs.reader();
|
||||||
|
|
||||||
|
var tmp = testing.tmpDir(.{});
|
||||||
|
defer tmp.cleanup();
|
||||||
|
|
||||||
|
var diagnostics: Diagnostics = .{ .allocator = testing.allocator };
|
||||||
|
defer diagnostics.deinit();
|
||||||
|
try pipeToFileSystem(tmp.dir, reader, .{ .diagnostics = &diagnostics });
|
||||||
|
|
||||||
|
try testing.expectEqualStrings("root", diagnostics.root_dir);
|
||||||
|
}
|
||||||
|
|
||||||
fn normalizePath(bytes: []u8) []u8 {
|
fn normalizePath(bytes: []u8) []u8 {
|
||||||
const canonical_sep = std.fs.path.sep_posix;
|
const canonical_sep = std.fs.path.sep_posix;
|
||||||
if (std.fs.path.sep == canonical_sep) return bytes;
|
if (std.fs.path.sep == canonical_sep) return bytes;
|
||||||
|
|
|
||||||
BIN
lib/std/tar/testdata/19820.tar
vendored
Normal file
BIN
lib/std/tar/testdata/19820.tar
vendored
Normal file
Binary file not shown.
Loading…
Add table
Reference in a new issue