mirror of
https://github.com/ziglang/zig.git
synced 2024-11-30 09:02:32 +00:00
d3a163f868
Adds a variant to the LazyPath union representing a parent directory of a generated path. ```zig const LazyPath = union(enum) { generated_dirname: struct { generated: *const GeneratedFile, up: usize, }, // ... } ``` These can be constructed with the new method: ```zig pub fn dirname(self: LazyPath) LazyPath ``` For the cases where the LazyPath is already known (`.path`, `.cwd_relative`, and `dependency`) this is evaluated right away. For dirnames of generated files and their dirnames, this is evaluated at getPath time. dirname calls can be chained, but for safety, they are not allowed to escape outside a root defined for each case: - path: This is relative to the build root, so dirname can't escape outside the build root. - generated: Can't escape the zig-cache. - cwd_relative: This can be a relative or absolute path. If relative, can't escape the current directory, and if absolute, can't go beyond root (/). - dependency: Can't escape the dependency's root directory. Testing: I've included a standalone case for many of the happy cases. I couldn't find an easy way to test the negatives, though, because tests cannot yet expect panics.
51 lines
1.2 KiB
Zig
51 lines
1.2 KiB
Zig
//! Checks that the basename of the given path matches a string.
|
|
//!
|
|
//! Usage:
|
|
//!
|
|
//! ```
|
|
//! has_basename <path> <basename>
|
|
//! ```
|
|
//!
|
|
//! <path> must be absolute.
|
|
//!
|
|
//! Returns a non-zero exit code if basename
|
|
//! does not match the given string.
|
|
|
|
const std = @import("std");
|
|
|
|
pub fn main() !void {
|
|
var arena_state = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
|
const arena = arena_state.allocator();
|
|
defer arena_state.deinit();
|
|
|
|
try run(arena);
|
|
}
|
|
|
|
fn run(allocator: std.mem.Allocator) !void {
|
|
var args = try std.process.argsWithAllocator(allocator);
|
|
defer args.deinit();
|
|
_ = args.next() orelse unreachable; // skip binary name
|
|
|
|
const path = args.next() orelse {
|
|
std.log.err("missing <path> argument", .{});
|
|
return error.BadUsage;
|
|
};
|
|
|
|
if (!std.fs.path.isAbsolute(path)) {
|
|
std.log.err("path must be absolute", .{});
|
|
return error.BadUsage;
|
|
}
|
|
|
|
const basename = args.next() orelse {
|
|
std.log.err("missing <basename> argument", .{});
|
|
return error.BadUsage;
|
|
};
|
|
|
|
const actual_basename = std.fs.path.basename(path);
|
|
if (std.mem.eql(u8, actual_basename, basename)) {
|
|
return;
|
|
}
|
|
|
|
return error.NotEqual;
|
|
}
|