diff --git a/LICENSE b/LICENSE index 7f46823..9aa5af4 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright 2019-2020 dd86k +Copyright 2019-2021 dd86k Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above diff --git a/src/main.c b/src/main.c index 943a468..2ac6fd2 100644 --- a/src/main.c +++ b/src/main.c @@ -9,6 +9,7 @@ #define __DATETIME__ __DATE__ " " __TIME__ #define PROJECT_VERSION "0.0.0" +#define COPYRIGHT "Copyright (c) 2019-2021 dd86k " #include #include @@ -19,6 +20,7 @@ #include "utils/hash.h" #include "vvd.h" +//TODO: Move "unit" tests into its own compilation unit #ifdef DEBUG #include #include "fs/gpt.h" @@ -149,7 +151,7 @@ #ifdef __VERSION__ "Compiler: " __VERSION__ "\n" #endif - "MIT License: Copyright (c) 2019-2020 dd86k \n" + "MIT License: " COPYRIGHT "\n" "Project page: \n" "Defines: " #ifdef DEBUG @@ -174,7 +176,7 @@ static void license() { puts( - "Copyright 2019-2020 dd86k \n" + COPYRIGHT "\n" "\n" "Permission to use, copy, modify, and/or distribute this software for any\n" "purpose with or without fee is hereby granted, provided that the above\n" @@ -209,14 +211,14 @@ * \returns VDISK_FORMAT_* enumeration value */ static int vdextauto(const oschar *path) { - if (extcmp(path, osstr("vdi"))) return VDISK_FORMAT_VDI; - if (extcmp(path, osstr("vmdk"))) return VDISK_FORMAT_VMDK; - if (extcmp(path, osstr("vhd"))) return VDISK_FORMAT_VHD; - if (extcmp(path, osstr("vhdx"))) return VDISK_FORMAT_VHDX; - if (extcmp(path, osstr("qed"))) return VDISK_FORMAT_QED; + if (extcmp(path, osstr("vdi"))) return VDISK_FORMAT_VDI; + if (extcmp(path, osstr("vmdk"))) return VDISK_FORMAT_VMDK; + if (extcmp(path, osstr("vhd"))) return VDISK_FORMAT_VHD; + if (extcmp(path, osstr("vhdx"))) return VDISK_FORMAT_VHDX; + if (extcmp(path, osstr("qed"))) return VDISK_FORMAT_QED; if (extcmp(path, osstr("qcow")) || extcmp(path, osstr("qcow2"))) return VDISK_FORMAT_QCOW; - if (extcmp(path, osstr("hdd"))) return VDISK_FORMAT_PHDD; + if (extcmp(path, osstr("hdd"))) return VDISK_FORMAT_PHDD; return VDISK_FORMAT_NONE; } @@ -282,7 +284,7 @@ // // vvd_info flags // - if (oscmp(arg, osstr("--info-raw")) == 0) { + if (oscmp(arg, osstr("--info-full")) == 0) { mflags |= VVD_INFO_RAW; continue; } @@ -301,7 +303,7 @@ const oschar *action = argv[1]; // - // Operations + // Main operation actions // if (oscmp(action, osstr("info")) == 0) { @@ -310,7 +312,7 @@ return EXIT_FAILURE; } if (vdisk_open(&vdin, defopt, oflags)) { - vdisk_perror(&vdin); + vvd_perror(&vdin); return vdin.err.num; } return vvd_info(&vdin, mflags); @@ -322,7 +324,7 @@ return EXIT_FAILURE; } if (vdisk_open(&vdin, defopt, oflags)) { - vdisk_perror(&vdin); + vvd_perror(&vdin); return vdin.err.num; } return vvd_map(&vdin, 0); @@ -334,11 +336,11 @@ return EXIT_FAILURE; } if (vdisk_open(&vdin, defopt, 0)) { - vdisk_perror(&vdin); + vvd_perror(&vdin); return vdin.err.num; } if (vvd_compact(&vdin, 0)) { - vdisk_perror(&vdin); + vvd_perror(&vdin); return vdin.err.num; } return EXIT_FAILURE; @@ -419,13 +421,12 @@ return EXIT_FAILURE; } UID uid; - int r; #ifdef _WIN32 char buffer[200]; wcstombs(buffer, defopt, 200); - r = uid_parse(&uid, buffer, UID_GUID); + int r = uid_parse(&uid, buffer, UID_GUID); #else - r = uid_parse(&uid, defopt, UID_GUID); + int r = uid_parse(&uid, defopt, UID_GUID); #endif if (r) { if (r < 0) @@ -451,6 +452,7 @@ } if (oscmp(action, osstr("version")) == 0 || oscmp(action, osstr("--version")) == 0) version(); + //TODO: Help system in its own module if (oscmp(action, osstr("help")) == 0 || oscmp(action, osstr("--help")) == 0) help(); if (oscmp(action, osstr("license")) == 0 || oscmp(action, osstr("--license")) == 0) diff --git a/src/utils/hash.c b/src/utils/hash.c index 79bdb5e..99a5b16 100644 --- a/src/utils/hash.c +++ b/src/utils/hash.c @@ -1,3 +1,7 @@ +/** + * XXHash32 implementation + */ + #include #include #include diff --git a/src/vdisk/fmt/vdi.c b/src/vdisk/fmt/vdi.c index abedea3..de1f2ff 100644 --- a/src/vdisk/fmt/vdi.c +++ b/src/vdisk/fmt/vdi.c @@ -185,7 +185,7 @@ // vdisk_vdi_compact // -int vdisk_vdi_compact(VDISK *vd, void(*cb)(uint32_t type, void *data)) { +int vdisk_vdi_compact(VDISK *vd) { if (vd->vdi->hdr.majorver != 1) return VDISK_ERROR(vd, VVD_EVDVERSION); if (vd->vdi->v1.type != VDI_DISK_DYN) diff --git a/src/vdisk/fmt/vdi.h b/src/vdisk/fmt/vdi.h index 340c3c7..391059e 100644 --- a/src/vdisk/fmt/vdi.h +++ b/src/vdisk/fmt/vdi.h @@ -120,4 +120,4 @@ int vdisk_vdi_read_sector(struct VDISK *vd, void *buffer, uint64_t index); -int vdisk_vdi_compact(struct VDISK *vd, void(*cb)(uint32_t type, void *data)); +int vdisk_vdi_compact(struct VDISK *vd); diff --git a/src/vdisk/vdisk.c b/src/vdisk/vdisk.c index f27781f..6af3fd2 100644 --- a/src/vdisk/vdisk.c +++ b/src/vdisk/vdisk.c @@ -7,7 +7,7 @@ #include "vdisk/vdisk.h" // -// VDISK_ERROR +// Internal functions // int vdisk_i_err(VDISK *vd, int e, int l, const char *f) { @@ -275,10 +275,10 @@ // vdisk_op_compact // -int vdisk_op_compact(VDISK *vd, void(*cb)(uint32_t, void*)) { +int vdisk_op_compact(VDISK *vd) { switch (vd->format) { case VDISK_FORMAT_VDI: - return vdisk_vdi_compact(vd, cb); + return vdisk_vdi_compact(vd); default: return VDISK_ERROR(vd, VVD_EVDFORMAT); } @@ -337,18 +337,3 @@ return "Unknown error happened"; } } - -// -// vdisk_perror -// - -#if _WIN32 - #define ERRFMT "%08X" -#else - #define ERRFMT "%d" -#endif - -void vdisk_perror(VDISK *vd) { - fprintf(stderr, "%s@%u: (" ERRFMT ") %s\n", - vd->err.func, vd->err.line, vd->err.num, vdisk_error(vd)); -} diff --git a/src/vdisk/vdisk.h b/src/vdisk/vdisk.h index 94b6f46..864335f 100644 --- a/src/vdisk/vdisk.h +++ b/src/vdisk/vdisk.h @@ -259,7 +259,7 @@ /** * */ -int vdisk_op_compact(VDISK *vd, void(*cb)(uint32_t, void*)); +int vdisk_op_compact(VDISK *vd); /** * @@ -275,9 +275,3 @@ * error is set to VVD_EOS, the error message will come from the OS (or CRT). */ const char* vdisk_error(VDISK *vd); - -/** - * Print to stdout, with the name of the function, a message with the last - * value set to vdisk_errno. - */ -void vdisk_perror(VDISK *vd); diff --git a/src/vvd.c b/src/vvd.c index 2aaba79..a1fbd04 100644 --- a/src/vvd.c +++ b/src/vvd.c @@ -10,6 +10,8 @@ #include "fs/mbr.h" #include "fs/gpt.h" +//TODO: Consider a "log" module + // // Global variables // @@ -24,37 +26,53 @@ uint32_t g_flags; // -// vvd_cb_progress +// vvd_perror // -void vvd_cb_progress(uint32_t type, void *data) { - switch (type) { - case VVD_NOTIF_DONE: - if (g_flags & VVD_PROGRESS) - if (os_pfinish(&g_progress)) { - fputs("os_pfinish: Could not finish progress bar", stderr); - exit(1); - } - return; - case VVD_NOTIF_VDISK_CREATED_TYPE_NAME: - printf("%s\n", data); - return; - case VVD_NOTIF_VDISK_TOTAL_BLOCKS: - case VVD_NOTIF_VDISK_TOTAL_BLOCKS64: - if (g_flags & VVD_PROGRESS) - if (os_pinit(&g_progress, PROG_MODE_POURCENT, 0)) { - fputs("os_pinit: Could not init progress bar\n", stderr); - exit(1); - } - return; - case VVD_NOTIF_VDISK_CURRENT_BLOCK: - case VVD_NOTIF_VDISK_CURRENT_BLOCK64: - if (g_flags & VVD_PROGRESS) - if (os_pupdate(&g_progress, 0)) { - fputs("os_pinit: Could not update progress bar\n", stderr); - exit(1); - } - return; +#if _WIN32 + #define ERRFMT "%08X" +#else + #define ERRFMT "%d" +#endif + +void vvd_perror(VDISK *vd) { + fprintf(stderr, "%s@%u: (" ERRFMT ") %s\n", + vd->err.func, vd->err.line, vd->err.num, vdisk_error(vd)); +} + +// +// Callback functions +// + +// +void vvd_cb_vdisk_created(VDISK *disk) { + printf("Disk type %s created\n", vdisk_str(disk)); +} + +// +void vvd_cb_current_blocks(uint64_t total) { + if (g_flags & VVD_PROGRESS) + if (os_pupdate(&g_progress, 0)) { + fputs("os_pinit: Could not update progress bar\n", stderr); + exit(1); + } +} + +// +void vvd_cb_total_blocks(uint64_t total) { + if (g_flags & VVD_PROGRESS) + if (os_pinit(&g_progress, PROG_MODE_POURCENT, 0)) { + fputs("os_pinit: Could not init progress bar\n", stderr); + exit(1); + } +} + +// +void vvd_cb_done() { + if (g_flags & VVD_PROGRESS) + if (os_pfinish(&g_progress)) { + fputs("os_pfinish: Could not finish progress bar", stderr); + exit(1); } } @@ -536,7 +554,7 @@ printf("QEMU Enhanced Disk, %s\n", disksize); } break; - case VDISK_FORMAT_RAW: goto L_NO_NL; // No header info + case VDISK_FORMAT_RAW: goto L_MBR; // No header info default: fputs("vvd_info: Format not supported\n", stderr); return VVD_EVDFORMAT; @@ -556,7 +574,7 @@ GPT gpt; } label; -L_NO_NL: +L_MBR: if (vdisk_read_sector(vd, &label, 0)) return EXIT_SUCCESS; if (label.mbr.sig != MBR_SIG) return EXIT_SUCCESS; @@ -712,7 +730,7 @@ int vvd_new(const oschar *path, uint32_t format, uint64_t capacity, uint32_t flags) { VDISK vd; if (vdisk_create(&vd, path, format, capacity, flags)) { - vdisk_perror(&vd); + vvd_perror(&vd); return vd.err.num; } printf("vvd_new: %s disk created successfully\n", vdisk_str(&vd)); @@ -726,8 +744,8 @@ int vvd_compact(VDISK *vd, uint32_t flags) { puts("vvd_compact: [warning] This function is still work in progress"); g_flags = flags; - if (vdisk_op_compact(vd, vvd_cb_progress)) { - vdisk_perror(vd); + if (vdisk_op_compact(vd)) { + vvd_perror(vd); return vd->err.num; } return EXIT_SUCCESS; diff --git a/src/vvd.h b/src/vvd.h index 91dcdf1..4eb12e0 100644 --- a/src/vvd.h +++ b/src/vvd.h @@ -20,6 +20,11 @@ }; /** + * Print VDISK error. + */ +void vvd_perror(VDISK *vd); + +/** * Print VDISK information to stdout. * * This includes information about the VDISK format and type, MBR, GPT, and