Skip to content

Commit 5a8499e

Browse files
committed
Support ~ in virtio-blk paths and improve error handling
This commit allows the virtio block device to resolve disk image paths starting with ~ to the user's home directory, making it more convenient. Additionally, it adds error handling for cases where the -x vblk: option is used without specifying a path.
1 parent a657a17 commit 5a8499e

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

src/riscv.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,11 @@ riscv_t *rv_create(riscv_user_t rv_attr)
681681
/* Currently, only used for block image path and permission */
682682
#define MAX_OPTS 2
683683
char *vblk_device_str = attr->data.system.vblk_device[i];
684+
if (!vblk_device_str[0]) {
685+
rv_log_error("Disk path cannot be empty");
686+
exit(EXIT_FAILURE);
687+
}
688+
684689
char *vblk_opts[MAX_OPTS] = {NULL};
685690
int vblk_opt_idx = 0;
686691
char *opt = strtok(vblk_device_str, ",");
@@ -692,10 +697,34 @@ riscv_t *rv_create(riscv_user_t rv_attr)
692697
vblk_opts[vblk_opt_idx++] = opt;
693698
opt = strtok(NULL, ",");
694699
}
695-
char *vblk_device = vblk_opts[0];
696-
char *vblk_readonly = vblk_opts[1];
697700

701+
char *vblk_device;
702+
char *vblk_readonly = vblk_opts[1];
698703
bool readonly = false;
704+
705+
if (vblk_opts[0][0] == '~') {
706+
/* HOME environment variable should be common in macOS and Linux
707+
* distribution and it is set by the login program
708+
*/
709+
const char *home = getenv("HOME");
710+
if (!home) {
711+
rv_log_error(
712+
"HOME environment variable is not set, cannot access "
713+
"the disk %s",
714+
vblk_opts[0]);
715+
exit(EXIT_FAILURE);
716+
}
717+
718+
vblk_device = malloc(strlen(vblk_opts[0]) - 1 /* skip ~ */ +
719+
strlen(home) + 1);
720+
assert(vblk_device);
721+
722+
strcpy(vblk_device, home);
723+
strcat(vblk_device, vblk_opts[0] + 1 /* skip ~ */);
724+
} else {
725+
vblk_device = vblk_opts[0];
726+
}
727+
699728
if (vblk_readonly) {
700729
if (strcmp(vblk_readonly, "readonly") != 0) {
701730
rv_log_error("Unknown vblk option: %s", vblk_readonly);
@@ -708,6 +737,9 @@ riscv_t *rv_create(riscv_user_t rv_attr)
708737
attr->vblk[i]->ram = (uint32_t *) attr->mem->mem_base;
709738
attr->disk[i] =
710739
virtio_blk_init(attr->vblk[i], vblk_device, readonly);
740+
741+
if (vblk_opts[0][0] == '~')
742+
free(vblk_device);
711743
}
712744
}
713745

0 commit comments

Comments
 (0)