unetd: cli: add helper function for getting path to temp files
authorFelix Fietkau <nbd@nbd.name>
Fri, 6 Jun 2025 08:58:39 +0000 (10:58 +0200)
committerFelix Fietkau <nbd@nbd.name>
Fri, 13 Jun 2025 08:17:04 +0000 (10:17 +0200)
Signed-off-by: Felix Fietkau <nbd@nbd.name>
package/network/services/unetd/files/unet.uc

index f89052b113495b9ecda9b0c4fdbc684661c692c4..450a6afdb70f95a76db09d40826058e94bdab8a1 100644 (file)
@@ -51,6 +51,11 @@ function network_get_file_string(f)
        return str;
 }
 
+function file_path(file)
+{
+       return "/dev/fd/" + file.fileno();
+}
+
 function network_keygen(pw_file, args, config, out_file, extra_args)
 {
        let rounds = config.rounds;
@@ -59,7 +64,7 @@ function network_keygen(pw_file, args, config, out_file, extra_args)
 
        if (!out_file) {
                output = mkstemp();
-               out_file = "/dev/fd/" + output.fileno();
+               out_file = file_path(output);
        }
 
        if (extra_args)
@@ -70,7 +75,7 @@ function network_keygen(pw_file, args, config, out_file, extra_args)
 
        if (config.xorkey) {
                xorkey = network_get_string_file(config.xorkey);
-               args += " -x /dev/fd/" + xorkey.fileno();
+               args += " -x " + file_path(xorkey);
        }
 
        pw_file.seek();
@@ -168,14 +173,14 @@ function network_sign_data(ctx, name, network, pw_file, upload)
        writefile(json_file, sprintf("%.J\n", network));
 
        let key_file = mkstemp();
-       let ret = network_keygen(pw_file, '-G', network.config, "/dev/fd/" + key_file.fileno());
+       let ret = network_keygen(pw_file, '-G', network.config, file_path(key_file));
        if (!ret) {
                if (ctx.command_failed)
                        ctx.command_failed("Failed to generate network key");
                return false;
        }
 
-       ret = system([ "unet-tool", "-S", "-K", "/dev/fd/" + key_file.fileno(), "-o", bin_file, json_file ]);
+       ret = system([ "unet-tool", "-S", "-K", file_path(key_file), "-o", bin_file, json_file ]);
        unlink(json_file);
        if (ret != 0) {
                if (ctx.command_failed)
@@ -785,7 +790,7 @@ function network_set_password(ctx, argv, named)
 
        let key_file = network_get_string_file(key);
        delete config.xorkey;
-       config.xorkey = network_keygen(pw_file, '-G -x /dev/fd/' + key_file.fileno(), config);
+       config.xorkey = network_keygen(pw_file, '-G -x ' + file_path(key_file), config);
        key_file.close();
 
        if (!config.xorkey) {