aboutsummaryrefslogtreecommitdiff
path: root/test/standalone/install_headers/check_exists.zig
blob: 62706749aac0398f7256fd6f1b5b3d5c652e6346 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const std = @import("std");

/// Checks the existence of files relative to cwd.
/// A path starting with ! should not exist.
pub fn main() !void {
    var arena_state = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena_state.deinit();

    const arena = arena_state.allocator();

    var arg_it = try std.process.argsWithAllocator(arena);
    _ = arg_it.next();

    const cwd = std.fs.cwd();
    const cwd_realpath = try cwd.realpathAlloc(arena, ".");

    while (arg_it.next()) |file_path| {
        if (file_path.len > 0 and file_path[0] == '!') {
            errdefer std.log.err(
                "exclusive file check '{s}{c}{s}' failed",
                .{ cwd_realpath, std.fs.path.sep, file_path[1..] },
            );
            if (std.fs.cwd().statFile(file_path[1..])) |_| {
                return error.FileFound;
            } else |err| switch (err) {
                error.FileNotFound => {},
                else => return err,
            }
        } else {
            errdefer std.log.err(
                "inclusive file check '{s}{c}{s}' failed",
                .{ cwd_realpath, std.fs.path.sep, file_path },
            );
            _ = try std.fs.cwd().statFile(file_path);
        }
    }
}