Wrap the source code at 80 characters. In the create_virtual_floppy() function, we have a complex calculation that is not really possible to squeeze under 80 characters in width, without extracting part of it to a local variable. The problem is *naming* the new variable, plus the *type* of the new variable.
By inspecting the rest of the function, I decided that the expression being extracted, namely DIV_ROUND_UP ((data_size / CLUSTER_SIZE + 2) * 4, CLUSTER_SIZE) should be called "fat_clusters". The rest of the function calculates the same quantity, only in multiple steps; the result is then assigned to "floppy->fat_clusters". Regarding the type of the new local variable: "data_size" is "uint64_t", and everything else in the expression is "int", so the result is "uint64_t" as well, in practice. Shorten the original assignment by using compound assignment, as well. Note that this patch is untested. In "make check", "tests/test-floppy.sh" passes. However, that test does not seem to pass the "size=..." option to the plugin, and the code being modified in create_virtual_floppy() is related to "size=..." -- at least it originates from commit c6d6113fa1f3 ("floppy: support FAT FSes with unused space.", 2021-06-16). Cc: Nolan Leake <no...@sigbus.net> Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2172516 Signed-off-by: Laszlo Ersek <ler...@redhat.com> --- Notes: context:-W plugins/floppy/directory-lfn.c | 13 +++++++++---- plugins/floppy/virtual-floppy.c | 19 +++++++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/plugins/floppy/directory-lfn.c b/plugins/floppy/directory-lfn.c index aa9b38bac755..afa5e4cfef36 100644 --- a/plugins/floppy/directory-lfn.c +++ b/plugins/floppy/directory-lfn.c @@ -63,15 +63,20 @@ struct lfn { size_t lfn_size; /* Size *in bytes* of lfn. */ }; -static int add_volume_label (const char *label, size_t di, struct virtual_floppy *floppy); +static int add_volume_label (const char *label, size_t di, + struct virtual_floppy *floppy); static int add_dot_entries (size_t di, struct virtual_floppy *floppy); -static int add_directory_entry (const struct lfn *lfn, uint8_t attributes, uint32_t file_size, struct stat *statbuf, size_t di, struct virtual_floppy *floppy); +static int add_directory_entry (const struct lfn *lfn, uint8_t attributes, + uint32_t file_size, struct stat *statbuf, + size_t di, struct virtual_floppy *floppy); static uint8_t lfn_checksum (const struct lfn *lfn); static void set_times (const struct stat *statbuf, struct dir_entry *entry); static int convert_long_file_names (struct lfn *lfns, size_t n); -static int convert_to_utf16le (const char *name, char **out, size_t *output_len); +static int convert_to_utf16le (const char *name, char **out, + size_t *output_len); static void free_lfns (struct lfn *lfns, size_t n); -static ssize_t append_dir_table (size_t di, const struct dir_entry *entry, struct virtual_floppy *floppy); +static ssize_t append_dir_table (size_t di, const struct dir_entry *entry, + struct virtual_floppy *floppy); /* Create the on disk directory table for dirs[di]. */ int diff --git a/plugins/floppy/virtual-floppy.c b/plugins/floppy/virtual-floppy.c index 2a6871bce6be..6d2f49adb2f0 100644 --- a/plugins/floppy/virtual-floppy.c +++ b/plugins/floppy/virtual-floppy.c @@ -61,14 +61,20 @@ #define OEM_NAME "MSWIN4.1" static ssize_t visit (const char *dir, struct virtual_floppy *floppy); -static int visit_subdirectory (const char *dir, const char *name, const struct stat *statbuf, size_t di, struct virtual_floppy *floppy); -static int visit_file (const char *dir, const char *name, const struct stat *statbuf, size_t di, struct virtual_floppy *floppy); +static int visit_subdirectory (const char *dir, const char *name, + const struct stat *statbuf, size_t di, + struct virtual_floppy *floppy); +static int visit_file (const char *dir, const char *name, + const struct stat *statbuf, size_t di, + struct virtual_floppy *floppy); static int create_mbr (struct virtual_floppy *floppy); static void chs_too_large (uint8_t *out); -static int create_partition_boot_sector (const char *label, struct virtual_floppy *floppy); +static int create_partition_boot_sector (const char *label, + struct virtual_floppy *floppy); static int create_fsinfo (struct virtual_floppy *floppy); static int create_fat (struct virtual_floppy *floppy); -static void write_fat_file (uint32_t first_cluster, uint32_t nr_clusters, struct virtual_floppy *floppy); +static void write_fat_file (uint32_t first_cluster, uint32_t nr_clusters, + struct virtual_floppy *floppy); static int create_regions (struct virtual_floppy *floppy); void @@ -88,135 +94,136 @@ int create_virtual_floppy (const char *dir, const char *label, uint64_t size, struct virtual_floppy *floppy) { size_t i; uint64_t nr_bytes, nr_clusters; uint64_t data_used_size; uint32_t cluster; if (visit (dir, floppy) == -1) return -1; nbdkit_debug ("floppy: %zu directories and %zu files", floppy->dirs.len, floppy->files.len); /* Create the on disk directory tables. */ for (i = 0; i < floppy->dirs.len; ++i) { if (create_directory (i, label, floppy) == -1) return -1; } /* We now have a complete list of directories and files, and * directories have been converted to on disk directory tables. So * we can assign them to clusters and also precisely calculate the * size of the data region and hence the size of the FAT. * * The first cluster number is always 2 (0 and 1 are reserved), and * (in this implementation) always contains the root directory. */ data_used_size = 0; cluster = 2; for (i = 0; i < floppy->dirs.len; ++i) { floppy->dirs.ptr[i].first_cluster = cluster; nr_bytes = ROUND_UP (floppy->dirs.ptr[i].table.len * sizeof (struct dir_entry), CLUSTER_SIZE); data_used_size += nr_bytes; nr_clusters = nr_bytes / CLUSTER_SIZE; if (cluster + nr_clusters > UINT32_MAX) goto too_big; floppy->dirs.ptr[i].nr_clusters = nr_clusters; cluster += nr_clusters; } for (i = 0; i < floppy->files.len; ++i) { floppy->files.ptr[i].first_cluster = cluster; nr_bytes = ROUND_UP (floppy->files.ptr[i].statbuf.st_size, CLUSTER_SIZE); data_used_size += nr_bytes; nr_clusters = nr_bytes / CLUSTER_SIZE; if (cluster + nr_clusters > UINT32_MAX) goto too_big; floppy->files.ptr[i].nr_clusters = nr_clusters; cluster += nr_clusters; } if (size > 0) { uint64_t data_size = size - (2080 * SECTOR_SIZE); - data_size = data_size - 2 * DIV_ROUND_UP ((data_size / CLUSTER_SIZE + 2) * 4, - CLUSTER_SIZE) * CLUSTER_SIZE; + uint64_t fat_clusters = DIV_ROUND_UP ((data_size / CLUSTER_SIZE + 2) * 4, + CLUSTER_SIZE); + data_size -= 2 * fat_clusters * CLUSTER_SIZE; if (data_used_size > data_size) { nbdkit_error ("filesystem is larger than \"size\" bytes"); return -1; } floppy->data_size = data_size; } else { floppy->data_size = data_used_size; } floppy->data_clusters = floppy->data_size / CLUSTER_SIZE; floppy->data_used_clusters = data_used_size / CLUSTER_SIZE; /* Despite its name, FAT32 only allows 28 bit cluster numbers, so * give an error if we go beyond this. */ if (floppy->data_clusters >= 0x10000000) { too_big: nbdkit_error ("disk image is too large for the FAT32 disk format"); return -1; } nbdkit_debug ("floppy: %" PRIu64 " data clusters, " "largest cluster number %" PRIu32 ", " "%" PRIu64 " bytes", floppy->data_clusters, cluster-1, floppy->data_size); floppy->fat_entries = floppy->data_clusters + 2; floppy->fat_clusters = DIV_ROUND_UP (floppy->fat_entries * 4, CLUSTER_SIZE); nbdkit_debug ("floppy: %" PRIu64 " FAT entries", floppy->fat_entries); /* We can now decide where we will place the FATs and data region on disk. */ floppy->fat2_start_sector = 2080 + floppy->fat_clusters * SECTORS_PER_CLUSTER; floppy->data_start_sector = floppy->fat2_start_sector + floppy->fat_clusters * SECTORS_PER_CLUSTER; floppy->data_last_sector = floppy->data_start_sector + floppy->data_clusters * SECTORS_PER_CLUSTER - 1; /* We now have to go back and update the cluster numbers in the * directory entries (which we didn't have available during * create_directory above). */ for (i = 0; i < floppy->dirs.len; ++i) { if (update_directory_first_cluster (i, floppy) == -1) return -1; } /* Create MBR. */ if (create_mbr (floppy) == -1) return -1; /* Create partition first sector. */ if (create_partition_boot_sector (label, floppy) == -1) return -1; /* Create filesystem information sector. */ if (create_fsinfo (floppy) == -1) return -1; /* Allocate and populate FAT. */ if (create_fat (floppy) == -1) return -1; /* Now we know how large everything is we can create the virtual * disk regions. */ if (create_regions (floppy) == -1) return -1; /* Check that if a size was specified, we ended up with it. */ if (size > 0) assert (virtual_size (&floppy->regions) == size); return 0; } _______________________________________________ Libguestfs mailing list Libguestfs@redhat.com https://listman.redhat.com/mailman/listinfo/libguestfs