From c2f95acbc702f123ae6862dcfdaf37071a9f8383 Mon Sep 17 00:00:00 2001 From: Stephen McKinney Date: Thu, 5 Nov 2020 15:29:26 -0600 Subject: [PATCH] Improvements to the progress callback: Remove _t from typedef. Add total sectors parameter. Remove unneeded pos parameter from mvhd_create_fixed. Remove progress callbacks from dynamic/diff creation funcitons. --- src/minivhd.h | 13 +++++------ src/minivhd_convert.c | 7 +++--- src/minivhd_create.c | 51 +++++++++++++++++-------------------------- src/minivhd_create.h | 2 +- 4 files changed, 29 insertions(+), 44 deletions(-) diff --git a/src/minivhd.h b/src/minivhd.h index aee8d98..59dc838 100644 --- a/src/minivhd.h +++ b/src/minivhd.h @@ -33,7 +33,7 @@ typedef struct MVHDGeom { typedef struct MVHDMeta MVHDMeta; -typedef void (*mvhd_progress_callback_t)(int64_t progress); +typedef void (*mvhd_progress_callback)(uint32_t current_sector, uint32_t total_sectors); /** * \brief Output a string from a MiniVHD error number @@ -79,26 +79,24 @@ MVHDMeta* mvhd_open(const char* path, bool readonly, int* err); * * \param [in] path is the absolute path to the image to create * \param [in] geom is the HDD geometry of the image to create. Determines final image size - * \param [out] pos stores the current sector being written to disk * \param [out] err indicates what error occurred, if any * \param [out] progress_callback optional; if not NULL, gets called to indicate progress on the creation operation * * \retval 0 if success * \retval < 0 if an error occurrs. Check value of *err for actual error */ -MVHDMeta* mvhd_create_fixed(const char* path, MVHDGeom geom, int* pos, int* err, mvhd_progress_callback_t progress_callback); +MVHDMeta* mvhd_create_fixed(const char* path, MVHDGeom geom, int* err, mvhd_progress_callback progress_callback); /** * \brief Create sparse (dynamic) VHD image. * * \param [in] path is the absolute path to the VHD file to create * \param [in] geom is the HDD geometry of the image to create. Determines final image size - * \param [out] err indicates what error occurred, if any - * \param [out] progress_callback optional; if not NULL, gets called to indicate progress on the creation operation + * \param [out] err indicates what error occurred, if any * * \return NULL if an error occurrs. Check value of *err for actual error. Otherwise returns pointer to a MVHDMeta struct */ -MVHDMeta* mvhd_create_sparse(const char* path, MVHDGeom geom, int* err, mvhd_progress_callback_t progress_callback); +MVHDMeta* mvhd_create_sparse(const char* path, MVHDGeom geom, int* err); /** * \brief Create differencing VHD imagee. @@ -106,11 +104,10 @@ MVHDMeta* mvhd_create_sparse(const char* path, MVHDGeom geom, int* err, mvhd_pro * \param [in] path is the absolute path to the VHD file to create * \param [in] par_path is the absolute path to a parent image. If NULL, a sparse image is created, otherwise create a differencing image * \param [out] err indicates what error occurred, if any - * \param [out] progress_callback optional; if not NULL, gets called to indicate progress on the creation operation * * \return NULL if an error occurrs. Check value of *err for actual error. Otherwise returns pointer to a MVHDMeta struct */ -MVHDMeta* mvhd_create_diff(const char* path, const char* par_path, int* err, mvhd_progress_callback_t progress_callback); +MVHDMeta* mvhd_create_diff(const char* path, const char* par_path, int* err); /** * \brief Safely close a VHD image diff --git a/src/minivhd_convert.c b/src/minivhd_convert.c index 24f3a43..067863c 100644 --- a/src/minivhd_convert.c +++ b/src/minivhd_convert.c @@ -39,9 +39,8 @@ MVHDMeta* mvhd_convert_to_vhd_fixed(const char* utf8_raw_path, const char* utf8_ FILE *raw_img = mvhd_open_existing_raw_img(utf8_raw_path, &geom, err); if (raw_img == NULL) { return NULL; - } - int pos; - MVHDMeta *vhdm = mvhd_create_fixed_raw(utf8_vhd_path, raw_img, &geom, &pos, err, NULL); + } + MVHDMeta *vhdm = mvhd_create_fixed_raw(utf8_vhd_path, raw_img, &geom, err, NULL); if (vhdm == NULL) { return NULL; } @@ -54,7 +53,7 @@ MVHDMeta* mvhd_convert_to_vhd_sparse(const char* utf8_raw_path, const char* utf8 if (raw_img == NULL) { return NULL; } - vhdm = mvhd_create_sparse(utf8_vhd_path, geom, err, NULL); + vhdm = mvhd_create_sparse(utf8_vhd_path, geom, err); if (vhdm == NULL) { goto end; } diff --git a/src/minivhd_create.c b/src/minivhd_create.c index 9696d81..0215135 100644 --- a/src/minivhd_create.c +++ b/src/minivhd_create.c @@ -21,7 +21,7 @@ static int mvhd_gen_par_loc(MVHDSparseHeader* header, mvhd_utf16* w2ku_path, mvhd_utf16* w2ru_path, MVHDError* err); -static MVHDMeta* mvhd_create_sparse_diff(const char* path, const char* par_path, MVHDGeom* geom, int* err, mvhd_progress_callback_t progress_callback); +static MVHDMeta* mvhd_create_sparse_diff(const char* path, const char* par_path, MVHDGeom* geom, int* err); /** * \brief Populate a VHD footer @@ -158,8 +158,8 @@ end: return rv; } -MVHDMeta* mvhd_create_fixed(const char* path, MVHDGeom geom, int* pos, int* err, mvhd_progress_callback_t progress_callback) { - return mvhd_create_fixed_raw(path, NULL, &geom, pos, err, progress_callback); +MVHDMeta* mvhd_create_fixed(const char* path, MVHDGeom geom, int* err, mvhd_progress_callback progress_callback) { + return mvhd_create_fixed_raw(path, NULL, &geom, err, progress_callback); } /** @@ -170,7 +170,7 @@ MVHDMeta* mvhd_create_fixed(const char* path, MVHDGeom geom, int* pos, int* err, * * \param [in] raw_image file handle to a raw disk image to populate VHD */ -MVHDMeta* mvhd_create_fixed_raw(const char* path, FILE* raw_img, MVHDGeom* geom, int* pos, int* err, mvhd_progress_callback_t progress_callback) { +MVHDMeta* mvhd_create_fixed_raw(const char* path, FILE* raw_img, MVHDGeom* geom, int* err, mvhd_progress_callback progress_callback) { uint8_t img_data[MVHD_SECTOR_SIZE] = {0}; uint8_t footer_buff[MVHD_FOOTER_SIZE] = {0}; MVHDMeta* vhdm = calloc(1, sizeof *vhdm); @@ -191,9 +191,10 @@ MVHDMeta* mvhd_create_fixed_raw(const char* path, FILE* raw_img, MVHDGeom* geom, goto cleanup_vhdm; } mvhd_fseeko64(f, 0, SEEK_SET); - uint32_t size_sectors, s; + uint32_t size_sectors = mvhd_calc_size_sectors(geom); + uint32_t s; if (progress_callback) - progress_callback(0); + progress_callback(0, size_sectors); if (raw_img != NULL) { mvhd_fseeko64(raw_img, 0, SEEK_END); uint64_t raw_size = (uint64_t)mvhd_ftello64(raw_img); @@ -202,28 +203,20 @@ MVHDMeta* mvhd_create_fixed_raw(const char* path, FILE* raw_img, MVHDGeom* geom, *err = MVHD_ERR_CONV_SIZE; goto cleanup_vhdm; } - mvhd_gen_footer(&vhdm->footer, geom, MVHD_TYPE_FIXED, 0); - size_sectors = mvhd_calc_size_sectors(geom); + mvhd_gen_footer(&vhdm->footer, geom, MVHD_TYPE_FIXED, 0); mvhd_fseeko64(raw_img, 0, SEEK_SET); - for (s = 0; s < size_sectors; s++) { - if (pos != NULL) { - *pos = (int)s; - } + for (s = 0; s < size_sectors; s++) { fread(img_data, sizeof img_data, 1, raw_img); fwrite(img_data, sizeof img_data, 1, f); if (progress_callback) - progress_callback(s); + progress_callback(s + 1, size_sectors); } } else { - mvhd_gen_footer(&vhdm->footer, geom, MVHD_TYPE_FIXED, 0); - size_sectors = mvhd_calc_size_sectors(geom); - for (s = 0; s < size_sectors; s++) { - if (pos != NULL) { - *pos = (int)s; - } + mvhd_gen_footer(&vhdm->footer, geom, MVHD_TYPE_FIXED, 0); + for (s = 0; s < size_sectors; s++) { fwrite(img_data, sizeof img_data, 1, f); if (progress_callback) - progress_callback(s); + progress_callback(s + 1, size_sectors); } } mvhd_footer_to_buffer(&vhdm->footer, footer_buff); @@ -251,7 +244,7 @@ end: * * \return NULL if an error occurrs. Check value of *err for actual error. Otherwise returns pointer to a MVHDMeta struct */ -static MVHDMeta* mvhd_create_sparse_diff(const char* path, const char* par_path, MVHDGeom* geom, int* err, mvhd_progress_callback_t progress_callback) { +static MVHDMeta* mvhd_create_sparse_diff(const char* path, const char* par_path, MVHDGeom* geom, int* err) { uint8_t footer_buff[MVHD_FOOTER_SIZE] = {0}; uint8_t sparse_buff[MVHD_SPARSE_SIZE] = {0}; uint8_t bat_sect[MVHD_SECTOR_SIZE]; @@ -285,10 +278,8 @@ static MVHDMeta* mvhd_create_sparse_diff(const char* path, const char* par_path, } else if (geom == NULL) { *err = MVHD_ERR_INVALID_GEOM; goto cleanup_vhdm; - } + } - if (progress_callback) - progress_callback(0); FILE* f = mvhd_fopen(path, "wb+", err); if (f == NULL) { goto cleanup_vhdm; @@ -398,16 +389,14 @@ end: if (w2ku_path != NULL) free(w2ku_path); if (w2ru_path != NULL) - free(w2ru_path); - if (progress_callback) - progress_callback(mvhd_calc_size_sectors(geom) - 1); + free(w2ru_path); return vhdm; } -MVHDMeta* mvhd_create_sparse(const char* path, MVHDGeom geom, int* err, mvhd_progress_callback_t progress_callback) { - return mvhd_create_sparse_diff(path, NULL, &geom, err, progress_callback); +MVHDMeta* mvhd_create_sparse(const char* path, MVHDGeom geom, int* err) { + return mvhd_create_sparse_diff(path, NULL, &geom, err); } -MVHDMeta* mvhd_create_diff(const char* path, const char* par_path, int* err, mvhd_progress_callback_t progress_callback) { - return mvhd_create_sparse_diff(path, par_path, NULL, err, progress_callback); +MVHDMeta* mvhd_create_diff(const char* path, const char* par_path, int* err) { + return mvhd_create_sparse_diff(path, par_path, NULL, err); } \ No newline at end of file diff --git a/src/minivhd_create.h b/src/minivhd_create.h index f07520f..3db1f3f 100644 --- a/src/minivhd_create.h +++ b/src/minivhd_create.h @@ -3,6 +3,6 @@ #include #include "minivhd.h" -MVHDMeta* mvhd_create_fixed_raw(const char* path, FILE* raw_img, MVHDGeom* geom, int* pos, int* err, mvhd_progress_callback_t progress_callback); +MVHDMeta* mvhd_create_fixed_raw(const char* path, FILE* raw_img, MVHDGeom* geom, int* err, mvhd_progress_callback progress_callback); #endif \ No newline at end of file