Skip to content

Commit faa16dd

Browse files
author
Ubuntu
committed
fix(validator-dir): fix realpathAlloc failure if dir doesn't exist
- allow ensureValidatorDir to create directory if it does not exist - move ensureValidatorDir dir to outside sub command
1 parent 2021d65 commit faa16dd

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

src/cmd.zig

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub fn main() !void {
103103
current_config.metrics_port = cmd.metrics_port;
104104
current_config.log_file = cmd.log_file;
105105
current_config.tee_logs = cmd.tee_logs;
106-
current_config.validator_dir = try std.fs.realpathAlloc(gpa, cmd.validator_dir);
106+
current_config.validator_dir = try ensureValidatorDir(gpa, cmd.validator_dir);
107107

108108
// If no subcommand was provided, print a friendly header and help information.
109109
const subcmd = cmd.subcmd orelse {
@@ -1183,16 +1183,29 @@ const Cmd = struct {
11831183
};
11841184
};
11851185

1186-
/// Ensures the validator directory exists.
1187-
/// Returns an error if the directory cannot be accessed.
1188-
fn ensureValidatorDir(logger: Logger, validator_dir: []const u8) !void {
1189-
_ = std.fs.cwd().openDir(validator_dir, .{}) catch |err| {
1190-
logger.err().logf(
1191-
"Cannot create or access validator directory '{s}': {}",
1192-
.{ validator_dir, err },
1193-
);
1194-
return err;
1186+
/// Ensures the validator directory exists. Create it if it does not.
1187+
fn ensureValidatorDir(allocator: std.mem.Allocator, validator_dir: []const u8) ![]const u8 {
1188+
std.fs.cwd().access(validator_dir, .{}) catch |access_err| {
1189+
switch (access_err) {
1190+
error.FileNotFound => {
1191+
std.fs.cwd().makePath(validator_dir) catch |create_err| {
1192+
std.debug.print(
1193+
"Cannot create validator directory '{s}': {}",
1194+
.{ validator_dir, create_err },
1195+
);
1196+
return create_err;
1197+
};
1198+
},
1199+
else => {
1200+
std.debug.print(
1201+
"Cannot access validator directory '{s}': {}",
1202+
.{ validator_dir, access_err },
1203+
);
1204+
return access_err;
1205+
},
1206+
}
11951207
};
1208+
return std.fs.realpathAlloc(allocator, validator_dir);
11961209
}
11971210

11981211
/// Ensures a genesis file is available by either using the provided path
@@ -1317,7 +1330,6 @@ fn validator(
13171330

13181331
app_base.logger.info().logf("starting validator with cfg: {}", .{cfg});
13191332

1320-
try ensureValidatorDir(app_base.logger, cfg.validator_dir);
13211333
const genesis_file_path = try ensureGenesis(allocator, cfg, app_base.logger);
13221334
defer allocator.free(genesis_file_path);
13231335

@@ -1577,7 +1589,6 @@ fn replayOffline(
15771589

15781590
app_base.logger.info().logf("starting replay-offline with cfg: {}", .{cfg});
15791591

1580-
try ensureValidatorDir(app_base.logger, cfg.validator_dir);
15811592
const genesis_file_path = try ensureGenesis(allocator, cfg, app_base.logger);
15821593
defer allocator.free(genesis_file_path);
15831594

@@ -1678,7 +1689,6 @@ fn shredNetwork(
16781689
app_base.deinit();
16791690
}
16801691

1681-
try ensureValidatorDir(app_base.logger, cfg.validator_dir);
16821692
const genesis_file_path = try ensureGenesis(allocator, cfg, app_base.logger);
16831693
defer allocator.free(genesis_file_path);
16841694
const genesis_config = try GenesisConfig.init(allocator, genesis_file_path);
@@ -1871,7 +1881,6 @@ fn validateSnapshot(allocator: std.mem.Allocator, cfg: config.Cmd) !void {
18711881
app_base.deinit();
18721882
}
18731883

1874-
try ensureValidatorDir(.noop, cfg.validator_dir);
18751884
const genesis_file_path = try ensureGenesis(allocator, cfg, .from(app_base.logger));
18761885
defer allocator.free(genesis_file_path);
18771886

0 commit comments

Comments
 (0)