raw disk: add optional -w flag to open image for writing

shell: support quoted arguments in split_args for filenames with spaces
This commit is contained in:
2026-08-22 18:03:11 +00:00
committed by dunkaist
parent 1165ede576
commit 8833f7a2e0
6 changed files with 21 additions and 11 deletions
+14 -4
View File
@@ -390,8 +390,13 @@ highlighter(ic_highlight_env_t *henv, const char *input, void *arg) {
static int
split_args(char *s, char **argv) {
int argc = -1;
for (; (argv[++argc] = strtok(s, " \t\n\r")) != NULL; s = NULL);
int argc = 0;
while (*(s += strspn(s, " \t\n\r"))) {
if (*s == '"') { argv[argc++] = ++s; s += strcspn(s, "\""); }
else { argv[argc++] = s; s += strcspn(s, " \t\n\r"); }
if (*s) *s++ = '\0';
}
argv[argc] = NULL;
return argc;
}
@@ -699,6 +704,7 @@ cmd_disk_add(struct shell_ctx *ctx, int argc, char **argv) {
"usage: disk_add <file> <name> [option]...\n"
" <file> absolute or relative path\n"
" <name> disk name, e.g. hd0 or rd\n"
" -w writable open disk image for writing\n"
" -c cache_size size of disk cache in bytes\n";
if (argc < 3) {
fputs(usage, ctx->fout);
@@ -706,12 +712,16 @@ cmd_disk_add(struct shell_ctx *ctx, int argc, char **argv) {
}
size_t cache_size = 0;
int adjust_cache_size = 0;
int writable = 0;
int opt;
optparse_init(&ctx->opts, argv);
const char *file_name = optparse_arg(&ctx->opts);
const char *disk_name = optparse_arg(&ctx->opts);
while ((opt = optparse(&ctx->opts, "c:")) != -1) {
while ((opt = optparse(&ctx->opts, "wc:")) != -1) {
switch (opt) {
case 'w':
writable = 1;
break;
case 'c':
cache_size = strtoul(ctx->opts.optarg, NULL, 0);
adjust_cache_size = 1;
@@ -722,7 +732,7 @@ cmd_disk_add(struct shell_ctx *ctx, int argc, char **argv) {
}
}
struct vdisk *umka_disk = vdisk_init(file_name, adjust_cache_size,
struct vdisk *umka_disk = vdisk_init(file_name, writable, adjust_cache_size,
cache_size, ctx->io);
if (umka_disk) {
COVERAGE_ON();
+1 -1
View File
@@ -170,7 +170,7 @@ main(int argc, char *argv[]) {
struct umka_fuse_ctx *ctx = umka_fuse_init();
umka_boot();
struct vdisk *umka_disk = vdisk_init(argv[2], 1, 0u, ctx->io);
struct vdisk *umka_disk = vdisk_init(argv[2], 0, 1, 0u, ctx->io);
struct disk *disk = disk_add(&umka_disk->diskfunc, "hd0", umka_disk, 0);
disk_media_changed(disk, 1);
return fuse_main(argc-1, argv, &umka_oper, ctx);
+2 -2
View File
@@ -43,7 +43,7 @@ vdisk_adjust_cache_size(void *userdata, size_t suggested_size) {
}
struct vdisk*
vdisk_init(const char *fname, const int adjust_cache_size,
vdisk_init(const char *fname, int writable, const int adjust_cache_size,
const size_t cache_size, const void *io) {
size_t fname_len = strlen(fname);
size_t dot_raw_len = strlen(RAW_SUFFIX);
@@ -54,7 +54,7 @@ vdisk_init(const char *fname, const int adjust_cache_size,
&& !strcmp(fname + fname_len - dot_raw_len, RAW_SUFFIX))
|| (fname_len > dot_iso_len
&& !strcmp(fname + fname_len - dot_iso_len, ISO_SUFFIX))) {
disk = (struct vdisk*)vdisk_init_raw(fname, io);
disk = (struct vdisk*)vdisk_init_raw(fname, writable, io);
} else if (fname_len > dot_qcow2_len
&& !strcmp(fname + fname_len - dot_qcow2_len, QCOW2_SUFFIX)) {
disk = (struct vdisk*)vdisk_init_qcow2(fname, io);
+1 -1
View File
@@ -23,7 +23,7 @@ struct vdisk {
};
struct vdisk*
vdisk_init(const char *fname, const int adjust_cache_size,
vdisk_init(const char *fname, int writable, const int adjust_cache_size,
const size_t cache_size, const void *io);
#endif // VDISK_H_INCLUDED
+2 -2
View File
@@ -57,8 +57,8 @@ vdisk_raw_write(void *userdata, void *buffer, off_t startsector,
}
struct vdisk*
vdisk_init_raw(const char *fname, const struct umka_io *io) {
int fd = open(fname, O_RDONLY | O_BINARY);
vdisk_init_raw(const char *fname, int writable, const struct umka_io *io) {
int fd = open(fname, (writable ? O_RDWR : O_RDONLY) | O_BINARY);
if (!fd) {
printf("[vdisk.raw]: can't open file '%s': %s\n", fname, strerror(errno));
return NULL;
+1 -1
View File
@@ -18,6 +18,6 @@
#define ISO_SUFFIX ".iso"
struct vdisk*
vdisk_init_raw(const char *fname, const struct umka_io *io);
vdisk_init_raw(const char *fname, int writable, const struct umka_io *io);
#endif // VDISK_RAW_H_INCLUDED