Skip to content

Commit 8825886

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 5b82d20 commit 8825886

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

src/cmd.zig

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub fn main() !void {
104104
current_config.metrics_port = cmd.metrics_port;
105105
current_config.log_file = cmd.log_file;
106106
current_config.tee_logs = cmd.tee_logs;
107-
current_config.validator_dir = try std.fs.realpathAlloc(gpa, cmd.validator_dir);
107+
current_config.validator_dir = try ensureValidatorDir(gpa, cmd.validator_dir);
108108

109109
// If no subcommand was provided, print a friendly header and help information.
110110
const subcmd = cmd.subcmd orelse {
@@ -1292,16 +1292,29 @@ const AllocationMetrics = struct {
12921292
allocated_bytes_sqlite: *Gauge(u64),
12931293
};
12941294

1295-
/// Ensures the validator directory exists.
1296-
/// Returns an error if the directory cannot be accessed.
1297-
fn ensureValidatorDir(logger: Logger, validator_dir: []const u8) !void {
1298-
_ = std.fs.cwd().openDir(validator_dir, .{}) catch |err| {
1299-
logger.err().logf(
1300-
"Cannot create or access validator directory '{s}': {}",
1301-
.{ validator_dir, err },
1302-
);
1303-
return err;
1295+
/// Ensures the validator directory exists. Create it if it does not.
1296+
fn ensureValidatorDir(allocator: std.mem.Allocator, validator_dir: []const u8) ![]const u8 {
1297+
std.fs.cwd().access(validator_dir, .{}) catch |access_err| {
1298+
switch (access_err) {
1299+
error.FileNotFound => {
1300+
std.fs.cwd().makePath(validator_dir) catch |create_err| {
1301+
std.debug.print(
1302+
"Cannot create validator directory '{s}': {}",
1303+
.{ validator_dir, create_err },
1304+
);
1305+
return create_err;
1306+
};
1307+
},
1308+
else => {
1309+
std.debug.print(
1310+
"Cannot access validator directory '{s}': {}",
1311+
.{ validator_dir, access_err },
1312+
);
1313+
return access_err;
1314+
},
1315+
}
13041316
};
1317+
return std.fs.realpathAlloc(allocator, validator_dir);
13051318
}
13061319

13071320
/// Ensures a genesis file is available by either using the provided path
@@ -1435,7 +1448,6 @@ fn validator(
14351448

14361449
const allocator = gpa_metrics.allocator();
14371450

1438-
try ensureValidatorDir(app_base.logger, cfg.validator_dir);
14391451
const genesis_file_path = try ensureGenesis(allocator, cfg, app_base.logger);
14401452
defer allocator.free(genesis_file_path);
14411453

@@ -1732,20 +1744,24 @@ fn replayOffline(
17321744

17331745
const allocator = gpa_metrics.allocator();
17341746

1735-
try ensureValidatorDir(app_base.logger, cfg.validator_dir);
17361747
const genesis_file_path = try ensureGenesis(allocator, cfg, app_base.logger);
17371748
defer allocator.free(genesis_file_path);
17381749

1739-
const snapshot_dir_str = cfg.accounts_db.snapshot_dir;
1740-
var snapshot_dir = try std.fs.cwd().makeOpenPath(snapshot_dir_str, .{ .iterate = true });
1750+
var snapshot_dir = try std.fs.cwd().makeOpenPath(
1751+
cfg.accounts_db.snapshot_dir,
1752+
.{ .iterate = true },
1753+
);
17411754
defer snapshot_dir.close();
17421755

17431756
const ledger_dir = try std.fs.path.join(allocator, &.{ cfg.validator_dir, "ledger" });
17441757
defer allocator.free(ledger_dir);
17451758

17461759
const snapshot_files = try SnapshotFiles.find(allocator, snapshot_dir);
17471760

1748-
const rooted_file = try std.fs.path.joinZ(allocator, &.{ snapshot_dir_str, "accounts.db" });
1761+
const rooted_file = try std.fs.path.joinZ(
1762+
allocator,
1763+
&.{ cfg.accounts_db.snapshot_dir, "accounts.db" },
1764+
);
17491765
defer allocator.free(rooted_file);
17501766

17511767
var rooted_db: sig.accounts_db.Two.Rooted = try .init(rooted_file);
@@ -1858,7 +1874,6 @@ fn shredNetwork(
18581874
app_base.deinit();
18591875
}
18601876

1861-
try ensureValidatorDir(app_base.logger, cfg.validator_dir);
18621877
const genesis_file_path = try ensureGenesis(allocator, cfg, app_base.logger);
18631878
defer allocator.free(genesis_file_path);
18641879
const genesis_config = try GenesisConfig.init(allocator, genesis_file_path);
@@ -2051,7 +2066,6 @@ fn validateSnapshot(allocator: std.mem.Allocator, cfg: config.Cmd) !void {
20512066
app_base.deinit();
20522067
}
20532068

2054-
try ensureValidatorDir(.noop, cfg.validator_dir);
20552069
const genesis_file_path = try ensureGenesis(allocator, cfg, .from(app_base.logger));
20562070
defer allocator.free(genesis_file_path);
20572071

0 commit comments

Comments
 (0)