core: don't read config files that are not files

This commit is contained in:
Jeffrey C. Ollie
2024-12-22 14:53:37 -06:00
parent eb7b056fba
commit 5d38336823

View File

@ -2546,9 +2546,21 @@ pub fn loadIter(
pub fn loadFile(self: *Config, alloc: Allocator, path: []const u8) !void {
assert(std.fs.path.isAbsolute(path));
var file = try std.fs.cwd().openFile(path, .{});
var file = try std.fs.openFileAbsolute(path, .{});
defer file.close();
const stat = try file.stat();
switch (stat.kind) {
.file => {},
else => |kind| {
log.warn("config-file {s}: not reading because file type is {s}", .{
path,
@tagName(kind),
});
return;
},
}
std.log.info("reading configuration file path={s}", .{path});
var buf_reader = std.io.bufferedReader(file.reader());
@ -2727,8 +2739,6 @@ pub fn loadRecursiveFiles(self: *Config, alloc_gpa: Allocator) !void {
var loaded = std.StringHashMap(void).init(alloc_gpa);
defer loaded.deinit();
const cwd = std.fs.cwd();
// We need to insert all of our loaded config-file values
// PRIOR to the "-e" in our replay steps, since everything
// after "-e" becomes an "initial-command". To do this, we
@ -2776,7 +2786,7 @@ pub fn loadRecursiveFiles(self: *Config, alloc_gpa: Allocator) !void {
continue;
}
var file = cwd.openFile(path, .{}) catch |err| {
var file = std.fs.openFileAbsolute(path, .{}) catch |err| {
if (err != error.FileNotFound or !optional) {
try self._diagnostics.append(arena_alloc, .{
.message = try std.fmt.allocPrintZ(
@ -2790,6 +2800,21 @@ pub fn loadRecursiveFiles(self: *Config, alloc_gpa: Allocator) !void {
};
defer file.close();
const stat = try file.stat();
switch (stat.kind) {
.file => {},
else => |kind| {
try self._diagnostics.append(arena_alloc, .{
.message = try std.fmt.allocPrintZ(
arena_alloc,
"config-file {s}: not reading because file type is {s}",
.{ path, @tagName(kind) },
),
});
continue;
},
}
log.info("loading config-file path={s}", .{path});
var buf_reader = std.io.bufferedReader(file.reader());
const reader = buf_reader.reader();