Index: stable/11/usr.sbin/makefs/cd9660/cd9660_archimedes.c =================================================================== --- stable/11/usr.sbin/makefs/cd9660/cd9660_archimedes.c (revision 332980) +++ stable/11/usr.sbin/makefs/cd9660/cd9660_archimedes.c (revision 332981) @@ -1,130 +1,126 @@ /* $NetBSD: cd9660_archimedes.c,v 1.1 2009/01/10 22:06:29 bjh21 Exp $ */ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1998, 2009 Ben Harris * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* * cd9660_archimedes.c - support for RISC OS "ARCHIMEDES" extension * * RISC OS CDFS looks for a special block at the end of the System Use * Field for each file. If present, this contains the RISC OS load * and exec address (used to hold the file timestamp and type), the * file attributes, and a flag indicating whether the first character * of the filename should be replaced with '!' (since many special * RISC OS filenames do). */ #include __FBSDID("$FreeBSD$"); -#include #include #include #include -#include #include +#include #include "makefs.h" #include "cd9660.h" #include "cd9660_archimedes.h" /* * Convert a Unix time_t (non-leap seconds since 1970-01-01) to a RISC * OS time (non-leap(?) centiseconds since 1900-01-01(?)). */ static u_int64_t riscos_date(time_t unixtime) { u_int64_t base; base = 31536000ULL * 70 + 86400 * 17; return (((u_int64_t)unixtime) + base)*100; } /* * Add "ARCHIMEDES" metadata to a node if that seems appropriate. * * We touch regular files with names matching /,[0-9a-f]{3}$/ and * directories matching /^!/. */ static void archimedes_convert_node(cd9660node *node) { struct ISO_ARCHIMEDES *arc; size_t len; int type = -1; uint64_t stamp; if (node->su_tail_data != NULL) /* Something else already has the tail. */ return; len = strlen(node->node->name); if (len < 1) return; if (len >= 4 && node->node->name[len-4] == ',') /* XXX should support ,xxx and ,lxa */ type = strtoul(node->node->name + len - 3, NULL, 16); if (type == -1 && node->node->name[0] != '!') return; if (type == -1) type = 0; - assert(sizeof(struct ISO_ARCHIMEDES) == 32); - if ((arc = calloc(1, sizeof(struct ISO_ARCHIMEDES))) == NULL) { - CD9660_MEM_ALLOC_ERROR("archimedes_convert_node"); - exit(1); - } + assert(sizeof(*arc) == 32); + arc = ecalloc(1, sizeof(*arc)); stamp = riscos_date(node->node->inode->st.st_mtime); memcpy(arc->magic, "ARCHIMEDES", 10); cd9660_731(0xfff00000 | (type << 8) | (stamp >> 32), arc->loadaddr); cd9660_731(stamp & 0x00ffffffffULL, arc->execaddr); arc->ro_attr = RO_ACCESS_UR | RO_ACCESS_OR; arc->cdfs_attr = node->node->name[0] == '!' ? CDFS_PLING : 0; node->su_tail_data = (void *)arc; node->su_tail_size = sizeof(*arc); } /* * Add "ARCHIMEDES" metadata to an entire tree recursively. */ void archimedes_convert_tree(cd9660node *node) { cd9660node *cn; assert(node != NULL); archimedes_convert_node(node); /* Recurse on children. */ TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) archimedes_convert_tree(cn); } Index: stable/11/usr.sbin/makefs/cd9660/cd9660_eltorito.c =================================================================== --- stable/11/usr.sbin/makefs/cd9660/cd9660_eltorito.c (revision 332980) +++ stable/11/usr.sbin/makefs/cd9660/cd9660_eltorito.c (revision 332981) @@ -1,715 +1,693 @@ /* $NetBSD: cd9660_eltorito.c,v 1.17 2011/06/23 02:35:56 enami Exp $ */ /*- * SPDX-License-Identifier: BSD-2-Clause-NetBSD * * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan * Perez-Rathke and Ram Vedam. All rights reserved. * * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys, * Alan Perez-Rathke and Ram Vedam. * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. */ #include "cd9660.h" #include "cd9660_eltorito.h" +#include #include __FBSDID("$FreeBSD$"); #ifdef DEBUG #define ELTORITO_DPRINTF(__x) printf __x #else #define ELTORITO_DPRINTF(__x) #endif static struct boot_catalog_entry *cd9660_init_boot_catalog_entry(void); static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char); static struct boot_catalog_entry *cd9660_boot_setup_default_entry( struct cd9660_boot_image *); static struct boot_catalog_entry *cd9660_boot_setup_section_head(char); static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char); #if 0 static u_char cd9660_boot_get_system_type(struct cd9660_boot_image *); #endif int cd9660_add_boot_disk(iso9660_disk *diskStructure, const char *boot_info) { struct stat stbuf; const char *mode_msg; char *temp; char *sysname; char *filename; struct cd9660_boot_image *new_image, *tmp_image; assert(boot_info != NULL); if (*boot_info == '\0') { warnx("Error: Boot disk information must be in the " "format 'system;filename'"); return 0; } /* First decode the boot information */ - if ((temp = strdup(boot_info)) == NULL) { - warn("%s: strdup", __func__); - return 0; - } + temp = estrdup(boot_info); sysname = temp; filename = strchr(sysname, ';'); if (filename == NULL) { warnx("supply boot disk information in the format " "'system;filename'"); free(temp); return 0; } *filename++ = '\0'; if (diskStructure->verbose_level > 0) { printf("Found bootdisk with system %s, and filename %s\n", sysname, filename); } - if ((new_image = malloc(sizeof(*new_image))) == NULL) { - warn("%s: malloc", __func__); - free(temp); - return 0; - } - (void)memset(new_image, 0, sizeof(*new_image)); + new_image = ecalloc(1, sizeof(*new_image)); new_image->loadSegment = 0; /* default for now */ /* Decode System */ if (strcmp(sysname, "i386") == 0) new_image->system = ET_SYS_X86; else if (strcmp(sysname, "powerpc") == 0) new_image->system = ET_SYS_PPC; else if (strcmp(sysname, "macppc") == 0 || strcmp(sysname, "mac68k") == 0) new_image->system = ET_SYS_MAC; else { warnx("boot disk system must be " "i386, powerpc, macppc, or mac68k"); free(temp); free(new_image); return 0; } - if ((new_image->filename = strdup(filename)) == NULL) { - warn("%s: strdup", __func__); - free(temp); - free(new_image); - return 0; - } + new_image->filename = estrdup(filename); free(temp); /* Get information about the file */ if (lstat(new_image->filename, &stbuf) == -1) err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__, new_image->filename); switch (stbuf.st_size) { case 1440 * 1024: new_image->targetMode = ET_MEDIA_144FDD; mode_msg = "Assigned boot image to 1.44 emulation mode"; break; case 1200 * 1024: new_image->targetMode = ET_MEDIA_12FDD; mode_msg = "Assigned boot image to 1.2 emulation mode"; break; case 2880 * 1024: new_image->targetMode = ET_MEDIA_288FDD; mode_msg = "Assigned boot image to 2.88 emulation mode"; break; default: new_image->targetMode = ET_MEDIA_NOEM; mode_msg = "Assigned boot image to no emulation mode"; break; } if (diskStructure->verbose_level > 0) printf("%s\n", mode_msg); new_image->size = stbuf.st_size; new_image->num_sectors = howmany(new_image->size, diskStructure->sectorSize) * howmany(diskStructure->sectorSize, 512); if (diskStructure->verbose_level > 0) { printf("New image has size %d, uses %d 512-byte sectors\n", new_image->size, new_image->num_sectors); } new_image->sector = -1; /* Bootable by default */ new_image->bootable = ET_BOOTABLE; /* Add boot disk */ /* Group images for the same platform together. */ TAILQ_FOREACH(tmp_image, &diskStructure->boot_images, image_list) { if (tmp_image->system != new_image->system) break; } if (tmp_image == NULL) { TAILQ_INSERT_HEAD(&diskStructure->boot_images, new_image, image_list); } else TAILQ_INSERT_BEFORE(tmp_image, new_image, image_list); new_image->serialno = diskStructure->image_serialno++; /* TODO : Need to do anything about the boot image in the tree? */ diskStructure->is_bootable = 1; return 1; } int cd9660_eltorito_add_boot_option(iso9660_disk *diskStructure, const char *option_string, const char *value) { char *eptr; struct cd9660_boot_image *image; assert(option_string != NULL); /* Find the last image added */ TAILQ_FOREACH(image, &diskStructure->boot_images, image_list) { if (image->serialno + 1 == diskStructure->image_serialno) break; } if (image == NULL) errx(EXIT_FAILURE, "Attempted to add boot option, " "but no boot images have been specified"); if (strcmp(option_string, "no-emul-boot") == 0) { image->targetMode = ET_MEDIA_NOEM; } else if (strcmp(option_string, "no-boot") == 0) { image->bootable = ET_NOT_BOOTABLE; } else if (strcmp(option_string, "hard-disk-boot") == 0) { image->targetMode = ET_MEDIA_HDD; } else if (strcmp(option_string, "boot-load-segment") == 0) { image->loadSegment = strtoul(value, &eptr, 16); if (eptr == value || *eptr != '\0' || errno != ERANGE) { warn("%s: strtoul", __func__); return 0; } } else { return 0; } return 1; } static struct boot_catalog_entry * cd9660_init_boot_catalog_entry(void) { - struct boot_catalog_entry *temp; - - if ((temp = malloc(sizeof(*temp))) == NULL) - return NULL; - - return memset(temp, 0, sizeof(*temp)); + return ecalloc(1, sizeof(struct boot_catalog_entry)); } static struct boot_catalog_entry * cd9660_boot_setup_validation_entry(char sys) { struct boot_catalog_entry *entry; boot_catalog_validation_entry *ve; int16_t checksum; unsigned char *csptr; int i; entry = cd9660_init_boot_catalog_entry(); - if (entry == NULL) { - warnx("Error: memory allocation failed in " - "cd9660_boot_setup_validation_entry"); - return 0; - } ve = &entry->entry_data.VE; ve->header_id[0] = 1; ve->platform_id[0] = sys; ve->key[0] = 0x55; ve->key[1] = 0xAA; /* Calculate checksum */ checksum = 0; cd9660_721(0, ve->checksum); csptr = (unsigned char*)ve; for (i = 0; i < sizeof(*ve); i += 2) { checksum += (int16_t)csptr[i]; checksum += 256 * (int16_t)csptr[i + 1]; } checksum = -checksum; cd9660_721(checksum, ve->checksum); ELTORITO_DPRINTF(("%s: header_id %d, platform_id %d, key[0] %d, key[1] %d, " "checksum %04x\n", __func__, ve->header_id[0], ve->platform_id[0], ve->key[0], ve->key[1], checksum)); return entry; } static struct boot_catalog_entry * cd9660_boot_setup_default_entry(struct cd9660_boot_image *disk) { struct boot_catalog_entry *default_entry; boot_catalog_initial_entry *ie; default_entry = cd9660_init_boot_catalog_entry(); if (default_entry == NULL) return NULL; ie = &default_entry->entry_data.IE; ie->boot_indicator[0] = disk->bootable; ie->media_type[0] = disk->targetMode; cd9660_721(disk->loadSegment, ie->load_segment); ie->system_type[0] = disk->system; cd9660_721(disk->num_sectors, ie->sector_count); cd9660_731(disk->sector, ie->load_rba); ELTORITO_DPRINTF(("%s: boot indicator %d, media type %d, " "load segment %04x, system type %d, sector count %d, " "load rba %d\n", __func__, ie->boot_indicator[0], ie->media_type[0], disk->loadSegment, ie->system_type[0], disk->num_sectors, disk->sector)); return default_entry; } static struct boot_catalog_entry * cd9660_boot_setup_section_head(char platform) { struct boot_catalog_entry *entry; boot_catalog_section_header *sh; entry = cd9660_init_boot_catalog_entry(); if (entry == NULL) return NULL; sh = &entry->entry_data.SH; /* More by default. The last one will manually be set to 0x91 */ sh->header_indicator[0] = ET_SECTION_HEADER_MORE; sh->platform_id[0] = platform; sh->num_section_entries[0] = 0; return entry; } static struct boot_catalog_entry * cd9660_boot_setup_section_entry(struct cd9660_boot_image *disk) { struct boot_catalog_entry *entry; boot_catalog_section_entry *se; if ((entry = cd9660_init_boot_catalog_entry()) == NULL) return NULL; se = &entry->entry_data.SE; se->boot_indicator[0] = ET_BOOTABLE; se->media_type[0] = disk->targetMode; cd9660_721(disk->loadSegment, se->load_segment); cd9660_721(disk->num_sectors, se->sector_count); cd9660_731(disk->sector, se->load_rba); return entry; } #if 0 static u_char cd9660_boot_get_system_type(struct cd9660_boot_image *disk) { /* For hard drive booting, we need to examine the MBR to figure out what the partition type is */ return 0; } #endif /* * Set up the BVD, Boot catalog, and the boot entries, but do no writing */ int cd9660_setup_boot(iso9660_disk *diskStructure, int first_sector) { int sector; int used_sectors; int num_entries = 0; int catalog_sectors; struct boot_catalog_entry *x86_head, *mac_head, *ppc_head, *valid_entry, *default_entry, *temp, *head, **headp, *next; struct cd9660_boot_image *tmp_disk; headp = NULL; x86_head = mac_head = ppc_head = NULL; /* If there are no boot disks, don't bother building boot information */ if (TAILQ_EMPTY(&diskStructure->boot_images)) return 0; /* Point to catalog: For now assume it consumes one sector */ ELTORITO_DPRINTF(("Boot catalog will go in sector %d\n", first_sector)); diskStructure->boot_catalog_sector = first_sector; cd9660_bothendian_dword(first_sector, diskStructure->boot_descriptor->boot_catalog_pointer); /* Step 1: Generate boot catalog */ /* Step 1a: Validation entry */ valid_entry = cd9660_boot_setup_validation_entry(ET_SYS_X86); if (valid_entry == NULL) return -1; /* * Count how many boot images there are, * and how many sectors they consume. */ num_entries = 1; used_sectors = 0; TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) { used_sectors += tmp_disk->num_sectors; /* One default entry per image */ num_entries++; } catalog_sectors = howmany(num_entries * 0x20, diskStructure->sectorSize); used_sectors += catalog_sectors; if (diskStructure->verbose_level > 0) { printf("%s: there will be %i entries consuming %i sectors. " "Catalog is %i sectors\n", __func__, num_entries, used_sectors, catalog_sectors); } /* Populate sector numbers */ sector = first_sector + catalog_sectors; TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) { tmp_disk->sector = sector; sector += tmp_disk->num_sectors; } LIST_INSERT_HEAD(&diskStructure->boot_entries, valid_entry, ll_struct); /* Step 1b: Initial/default entry */ /* TODO : PARAM */ tmp_disk = TAILQ_FIRST(&diskStructure->boot_images); default_entry = cd9660_boot_setup_default_entry(tmp_disk); if (default_entry == NULL) { warnx("Error: memory allocation failed in cd9660_setup_boot"); return -1; } LIST_INSERT_AFTER(valid_entry, default_entry, ll_struct); /* Todo: multiple default entries? */ tmp_disk = TAILQ_NEXT(tmp_disk, image_list); temp = default_entry; /* If multiple boot images are given : */ while (tmp_disk != NULL) { /* Step 2: Section header */ switch (tmp_disk->system) { case ET_SYS_X86: headp = &x86_head; break; case ET_SYS_PPC: headp = &ppc_head; break; case ET_SYS_MAC: headp = &mac_head; break; default: warnx("%s: internal error: unknown system type", __func__); return -1; } if (*headp == NULL) { head = cd9660_boot_setup_section_head(tmp_disk->system); if (head == NULL) { warnx("Error: memory allocation failed in " "cd9660_setup_boot"); return -1; } LIST_INSERT_AFTER(default_entry, head, ll_struct); *headp = head; } else head = *headp; head->entry_data.SH.num_section_entries[0]++; /* Step 2a: Section entry and extensions */ temp = cd9660_boot_setup_section_entry(tmp_disk); if (temp == NULL) { warn("%s: cd9660_boot_setup_section_entry", __func__); return -1; } while ((next = LIST_NEXT(head, ll_struct)) != NULL && next->entry_type == ET_ENTRY_SE) head = next; LIST_INSERT_AFTER(head, temp, ll_struct); tmp_disk = TAILQ_NEXT(tmp_disk, image_list); } /* TODO: Remaining boot disks when implemented */ return first_sector + used_sectors; } int cd9660_setup_boot_volume_descriptor(iso9660_disk *diskStructure, volume_descriptor *bvd) { boot_volume_descriptor *bvdData = (boot_volume_descriptor*)bvd->volumeDescriptorData; bvdData->boot_record_indicator[0] = ISO_VOLUME_DESCRIPTOR_BOOT; memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5); bvdData->version[0] = 1; memcpy(bvdData->boot_system_identifier, ET_ID, 23); memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5); diskStructure->boot_descriptor = (boot_volume_descriptor*) bvd->volumeDescriptorData; return 1; } static int cd9660_write_mbr_partition_entry(FILE *fd, int idx, off_t sector_start, off_t nsectors, int type) { uint8_t val; uint32_t lba; if (fseeko(fd, (off_t)(idx) * 16 + 0x1be, SEEK_SET) == -1) err(1, "fseeko"); val = 0x80; /* Bootable */ fwrite(&val, sizeof(val), 1, fd); val = 0xff; /* CHS begin */ fwrite(&val, sizeof(val), 1, fd); fwrite(&val, sizeof(val), 1, fd); fwrite(&val, sizeof(val), 1, fd); val = type; /* Part type */ fwrite(&val, sizeof(val), 1, fd); val = 0xff; /* CHS end */ fwrite(&val, sizeof(val), 1, fd); fwrite(&val, sizeof(val), 1, fd); fwrite(&val, sizeof(val), 1, fd); /* LBA extent */ lba = htole32(sector_start); fwrite(&lba, sizeof(lba), 1, fd); lba = htole32(nsectors); fwrite(&lba, sizeof(lba), 1, fd); return 0; } static int cd9660_write_apm_partition_entry(FILE *fd, int idx, int total_partitions, off_t sector_start, off_t nsectors, off_t sector_size, const char *part_name, const char *part_type) { uint32_t apm32, part_status; uint16_t apm16; /* See Apple Tech Note 1189 for the details about the pmPartStatus * flags. * Below the flags which are default: * - IsValid 0x01 * - IsAllocated 0x02 * - IsReadable 0x10 * - IsWritable 0x20 */ part_status = 0x01 | 0x02 | 0x10 | 0x20; if (fseeko(fd, (off_t)(idx + 1) * sector_size, SEEK_SET) == -1) err(1, "fseeko"); /* Signature */ apm16 = htobe16(0x504d); fwrite(&apm16, sizeof(apm16), 1, fd); apm16 = 0; fwrite(&apm16, sizeof(apm16), 1, fd); /* Total number of partitions */ apm32 = htobe32(total_partitions); fwrite(&apm32, sizeof(apm32), 1, fd); /* Bounds */ apm32 = htobe32(sector_start); fwrite(&apm32, sizeof(apm32), 1, fd); apm32 = htobe32(nsectors); fwrite(&apm32, sizeof(apm32), 1, fd); fwrite(part_name, strlen(part_name) + 1, 1, fd); fseek(fd, 32 - strlen(part_name) - 1, SEEK_CUR); fwrite(part_type, strlen(part_type) + 1, 1, fd); fseek(fd, 32 - strlen(part_type) - 1, SEEK_CUR); apm32 = 0; /* pmLgDataStart */ fwrite(&apm32, sizeof(apm32), 1, fd); /* pmDataCnt */ apm32 = htobe32(nsectors); fwrite(&apm32, sizeof(apm32), 1, fd); /* pmPartStatus */ apm32 = htobe32(part_status); fwrite(&apm32, sizeof(apm32), 1, fd); return 0; } int cd9660_write_boot(iso9660_disk *diskStructure, FILE *fd) { struct boot_catalog_entry *e; struct cd9660_boot_image *t; int apm_partitions = 0; int mbr_partitions = 0; /* write boot catalog */ if (fseeko(fd, (off_t)diskStructure->boot_catalog_sector * diskStructure->sectorSize, SEEK_SET) == -1) err(1, "fseeko"); if (diskStructure->verbose_level > 0) { printf("Writing boot catalog to sector %" PRId64 "\n", diskStructure->boot_catalog_sector); } LIST_FOREACH(e, &diskStructure->boot_entries, ll_struct) { if (diskStructure->verbose_level > 0) { printf("Writing catalog entry of type %d\n", e->entry_type); } /* * It doesn't matter which one gets written * since they are the same size */ fwrite(&(e->entry_data.VE), 1, 32, fd); } if (diskStructure->verbose_level > 0) printf("Finished writing boot catalog\n"); /* copy boot images */ TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) { if (diskStructure->verbose_level > 0) { printf("Writing boot image from %s to sectors %d\n", t->filename, t->sector); } cd9660_copy_file(diskStructure, fd, t->sector, t->filename); if (t->system == ET_SYS_MAC) apm_partitions++; if (t->system == ET_SYS_PPC) mbr_partitions++; } /* some systems need partition tables as well */ if (mbr_partitions > 0 || diskStructure->chrp_boot) { uint16_t sig; fseek(fd, 0x1fe, SEEK_SET); sig = htole16(0xaa55); fwrite(&sig, sizeof(sig), 1, fd); mbr_partitions = 0; /* Write ISO9660 descriptor, enclosing the whole disk */ if (diskStructure->chrp_boot) cd9660_write_mbr_partition_entry(fd, mbr_partitions++, 0, diskStructure->totalSectors * (diskStructure->sectorSize / 512), 0x96); /* Write all partition entries */ TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) { if (t->system != ET_SYS_PPC) continue; cd9660_write_mbr_partition_entry(fd, mbr_partitions++, t->sector * (diskStructure->sectorSize / 512), t->num_sectors * (diskStructure->sectorSize / 512), 0x41 /* PReP Boot */); } } if (apm_partitions > 0) { /* Write DDR and global APM info */ uint32_t apm32; uint16_t apm16; int total_parts; fseek(fd, 0, SEEK_SET); apm16 = htobe16(0x4552); fwrite(&apm16, sizeof(apm16), 1, fd); /* Device block size */ apm16 = htobe16(512); fwrite(&apm16, sizeof(apm16), 1, fd); /* Device block count */ apm32 = htobe32(diskStructure->totalSectors * (diskStructure->sectorSize / 512)); fwrite(&apm32, sizeof(apm32), 1, fd); /* Device type/id */ apm16 = htobe16(1); fwrite(&apm16, sizeof(apm16), 1, fd); fwrite(&apm16, sizeof(apm16), 1, fd); /* Count total needed entries */ total_parts = 2 + apm_partitions; /* Self + ISO9660 */ /* Write self-descriptor */ cd9660_write_apm_partition_entry(fd, 0, total_parts, 1, total_parts, 512, "Apple", "Apple_partition_map"); /* Write all partition entries */ apm_partitions = 0; TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) { if (t->system != ET_SYS_MAC) continue; cd9660_write_apm_partition_entry(fd, 1 + apm_partitions++, total_parts, t->sector * (diskStructure->sectorSize / 512), t->num_sectors * (diskStructure->sectorSize / 512), 512, "CD Boot", "Apple_Bootstrap"); } /* Write ISO9660 descriptor, enclosing the whole disk */ cd9660_write_apm_partition_entry(fd, 2 + apm_partitions, total_parts, 0, diskStructure->totalSectors * (diskStructure->sectorSize / 512), 512, "ISO9660", "CD_ROM_Mode_1"); } return 0; } Index: stable/11/usr.sbin/makefs/cd9660/cd9660_write.c =================================================================== --- stable/11/usr.sbin/makefs/cd9660/cd9660_write.c (revision 332980) +++ stable/11/usr.sbin/makefs/cd9660/cd9660_write.c (revision 332981) @@ -1,532 +1,517 @@ /* $NetBSD: cd9660_write.c,v 1.14 2011/01/04 09:48:21 wiz Exp $ */ /*- * SPDX-License-Identifier: BSD-2-Clause-NetBSD * * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan * Perez-Rathke and Ram Vedam. All rights reserved. * * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys, * Alan Perez-Rathke and Ram Vedam. * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. */ #include "cd9660.h" #include "iso9660_rrip.h" #include __FBSDID("$FreeBSD$"); +#include + static int cd9660_write_volume_descriptors(iso9660_disk *, FILE *); static int cd9660_write_path_table(iso9660_disk *, FILE *, off_t, int); static int cd9660_write_path_tables(iso9660_disk *, FILE *); static int cd9660_write_file(iso9660_disk *, FILE *, cd9660node *); static int cd9660_write_filedata(iso9660_disk *, FILE *, off_t, const unsigned char *, int); #if 0 static int cd9660_write_buffered(FILE *, off_t, int, const unsigned char *); #endif static void cd9660_write_rr(iso9660_disk *, FILE *, cd9660node *, off_t, off_t); /* * Write the image * Writes the entire image * @param const char* The filename for the image * @returns int 1 on success, 0 on failure */ int cd9660_write_image(iso9660_disk *diskStructure, const char* image) { FILE *fd; int status; char buf[CD9660_SECTOR_SIZE]; if ((fd = fopen(image, "w+")) == NULL) { err(EXIT_FAILURE, "%s: Can't open `%s' for writing", __func__, image); } if (diskStructure->verbose_level > 0) printf("Writing image\n"); if (diskStructure->has_generic_bootimage) { status = cd9660_copy_file(diskStructure, fd, 0, diskStructure->generic_bootimage); if (status == 0) { warnx("%s: Error writing generic boot image", __func__); goto cleanup_bad_image; } } /* Write the volume descriptors */ status = cd9660_write_volume_descriptors(diskStructure, fd); if (status == 0) { warnx("%s: Error writing volume descriptors to image", __func__); goto cleanup_bad_image; } if (diskStructure->verbose_level > 0) printf("Volume descriptors written\n"); /* * Write the path tables: there are actually four, but right * now we are only concearned with two. */ status = cd9660_write_path_tables(diskStructure, fd); if (status == 0) { warnx("%s: Error writing path tables to image", __func__); goto cleanup_bad_image; } if (diskStructure->verbose_level > 0) printf("Path tables written\n"); /* Write the directories and files */ status = cd9660_write_file(diskStructure, fd, diskStructure->rootNode); if (status == 0) { warnx("%s: Error writing files to image", __func__); goto cleanup_bad_image; } if (diskStructure->is_bootable) { cd9660_write_boot(diskStructure, fd); } /* Write padding bits. This is temporary */ memset(buf, 0, CD9660_SECTOR_SIZE); cd9660_write_filedata(diskStructure, fd, diskStructure->totalSectors - 1, buf, 1); if (diskStructure->verbose_level > 0) printf("Files written\n"); fclose(fd); if (diskStructure->verbose_level > 0) printf("Image closed\n"); return 1; cleanup_bad_image: fclose(fd); if (!diskStructure->keep_bad_images) unlink(image); if (diskStructure->verbose_level > 0) printf("Bad image cleaned up\n"); return 0; } static int cd9660_write_volume_descriptors(iso9660_disk *diskStructure, FILE *fd) { volume_descriptor *vd_temp = diskStructure->firstVolumeDescriptor; int pos; while (vd_temp != NULL) { pos = vd_temp->sector * diskStructure->sectorSize; cd9660_write_filedata(diskStructure, fd, vd_temp->sector, vd_temp->volumeDescriptorData, 1); vd_temp = vd_temp->next; } return 1; } /* * Write out an individual path table * Used just to keep redundant code to a minimum * @param FILE *fd Valid file pointer * @param int Sector to start writing path table to * @param int Endian mode : BIG_ENDIAN or LITTLE_ENDIAN * @returns int 1 on success, 0 on failure */ static int cd9660_write_path_table(iso9660_disk *diskStructure, FILE *fd, off_t sector, int mode) { int path_table_sectors = CD9660_BLOCKS(diskStructure->sectorSize, diskStructure->pathTableLength); unsigned char *buffer; unsigned char *buffer_head; int len, ret; path_table_entry temp_entry; cd9660node *ptcur; - buffer = malloc(diskStructure->sectorSize * path_table_sectors); - if (buffer == NULL) { - warnx("%s: Memory allocation error allocating buffer", - __func__); - return 0; - } + buffer = ecalloc(path_table_sectors, diskStructure->sectorSize); buffer_head = buffer; - memset(buffer, 0, diskStructure->sectorSize * path_table_sectors); ptcur = diskStructure->rootNode; while (ptcur != NULL) { memset(&temp_entry, 0, sizeof(path_table_entry)); temp_entry.length[0] = ptcur->isoDirRecord->name_len[0]; temp_entry.extended_attribute_length[0] = ptcur->isoDirRecord->ext_attr_length[0]; memcpy(temp_entry.name, ptcur->isoDirRecord->name, temp_entry.length[0] + 1); /* round up */ len = temp_entry.length[0] + 8 + (temp_entry.length[0] & 0x01); /* todo: function pointers instead */ if (mode == LITTLE_ENDIAN) { cd9660_731(ptcur->fileDataSector, temp_entry.first_sector); cd9660_721((ptcur->parent == NULL ? 1 : ptcur->parent->ptnumber), temp_entry.parent_number); } else { cd9660_732(ptcur->fileDataSector, temp_entry.first_sector); cd9660_722((ptcur->parent == NULL ? 1 : ptcur->parent->ptnumber), temp_entry.parent_number); } memcpy(buffer, &temp_entry, len); buffer += len; ptcur = ptcur->ptnext; } ret = cd9660_write_filedata(diskStructure, fd, sector, buffer_head, path_table_sectors); free(buffer_head); return ret; } /* * Write out the path tables to disk * Each file descriptor should be pointed to by the PVD, so we know which * sector to copy them to. One thing to watch out for: the only path tables * stored are in the endian mode that the application is compiled for. So, * the first thing to do is write out that path table, then to write the one * in the other endian mode requires to convert the endianness of each entry * in the table. The best way to do this would be to create a temporary * path_table_entry structure, then for each path table entry, copy it to * the temporary entry, translate, then copy that to disk. * * @param FILE* Valid file descriptor * @returns int 0 on failure, 1 on success */ static int cd9660_write_path_tables(iso9660_disk *diskStructure, FILE *fd) { if (cd9660_write_path_table(diskStructure, fd, diskStructure->primaryLittleEndianTableSector, LITTLE_ENDIAN) == 0) return 0; if (cd9660_write_path_table(diskStructure, fd, diskStructure->primaryBigEndianTableSector, BIG_ENDIAN) == 0) return 0; /* @TODO: handle remaining two path tables */ return 1; } /* * Write a file to disk * Writes a file, its directory record, and its data to disk * This file is designed to be called RECURSIVELY, so initially call it * with the root node. All of the records should store what sector the * file goes in, so no computation should be necessary. * * @param int fd Valid file descriptor * @param struct cd9660node* writenode Pointer to the file to be written * @returns int 0 on failure, 1 on success */ static int cd9660_write_file(iso9660_disk *diskStructure, FILE *fd, cd9660node *writenode) { char *buf; char *temp_file_name; int ret; off_t working_sector; int cur_sector_offset; int written; iso_directory_record_cd9660 temp_record; cd9660node *temp; int rv = 0; /* Todo : clean up variables */ - temp_file_name = malloc(CD9660MAXPATH + 1); - if (temp_file_name == NULL) - err(EXIT_FAILURE, "%s: malloc", __func__); - - memset(temp_file_name, 0, CD9660MAXPATH + 1); - - buf = malloc(diskStructure->sectorSize); - if (buf == NULL) - err(EXIT_FAILURE, "%s: malloc", __func__); - + temp_file_name = ecalloc(CD9660MAXPATH + 1, 1); + buf = emalloc(diskStructure->sectorSize); if ((writenode->level != 0) && !(writenode->node->type & S_IFDIR)) { fsinode *inode = writenode->node->inode; /* Only attempt to write unwritten files that have length. */ if ((inode->flags & FI_WRITTEN) != 0) { INODE_WARNX(("%s: skipping written inode %d", __func__, (int)inode->st.st_ino)); } else if (writenode->fileDataLength > 0) { INODE_WARNX(("%s: writing inode %d blocks at %" PRIu32, __func__, (int)inode->st.st_ino, inode->ino)); inode->flags |= FI_WRITTEN; if (writenode->node->contents == NULL) cd9660_compute_full_filename(writenode, temp_file_name); ret = cd9660_copy_file(diskStructure, fd, writenode->fileDataSector, (writenode->node->contents != NULL) ? writenode->node->contents : temp_file_name); if (ret == 0) goto out; } } else { /* * Here is a new revelation that ECMA didn't explain * (at least not well). * ALL . and .. records store the name "\0" and "\1" * respectively. So, for each directory, we have to * make a new node. * * This is where it gets kinda messy, since we have to * be careful of sector boundaries */ cur_sector_offset = 0; working_sector = writenode->fileDataSector; if (fseeko(fd, working_sector * diskStructure->sectorSize, SEEK_SET) == -1) err(1, "fseeko"); /* * Now loop over children, writing out their directory * records - beware of sector boundaries */ TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) { /* * Copy the temporary record and adjust its size * if necessary */ memcpy(&temp_record, temp->isoDirRecord, sizeof(iso_directory_record_cd9660)); temp_record.length[0] = cd9660_compute_record_size(diskStructure, temp); if (temp_record.length[0] + cur_sector_offset >= diskStructure->sectorSize) { cur_sector_offset = 0; working_sector++; /* Seek to the next sector. */ if (fseeko(fd, working_sector * diskStructure->sectorSize, SEEK_SET) == -1) err(1, "fseeko"); } /* Write out the basic ISO directory record */ written = fwrite(&temp_record, 1, temp->isoDirRecord->length[0], fd); if (diskStructure->rock_ridge_enabled) { cd9660_write_rr(diskStructure, fd, temp, cur_sector_offset, working_sector); } if (fseeko(fd, working_sector * diskStructure->sectorSize + cur_sector_offset + temp_record.length[0] - temp->su_tail_size, SEEK_SET) == -1) err(1, "fseeko"); if (temp->su_tail_size > 0) fwrite(temp->su_tail_data, 1, temp->su_tail_size, fd); if (ferror(fd)) { warnx("%s: write error", __func__); goto out; } cur_sector_offset += temp_record.length[0]; } /* * Recurse on children. */ TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) { if ((ret = cd9660_write_file(diskStructure, fd, temp)) == 0) goto out; } } rv = 1; out: free(temp_file_name); free(buf); return rv; } /* * Wrapper function to write a buffer (one sector) to disk. * Seeks and writes the buffer. * NOTE: You dont NEED to use this function, but it might make your * life easier if you have to write things that align to a sector * (such as volume descriptors). * * @param int fd Valid file descriptor * @param int sector Sector number to write to * @param const unsigned char* Buffer to write. This should be the * size of a sector, and if only a portion * is written, the rest should be set to 0. */ static int cd9660_write_filedata(iso9660_disk *diskStructure, FILE *fd, off_t sector, const unsigned char *buf, int numsecs) { off_t curpos; size_t success; curpos = ftello(fd); if (fseeko(fd, sector * diskStructure->sectorSize, SEEK_SET) == -1) err(1, "fseeko"); success = fwrite(buf, diskStructure->sectorSize * numsecs, 1, fd); if (fseeko(fd, curpos, SEEK_SET) == -1) err(1, "fseeko"); if (success == 1) success = diskStructure->sectorSize * numsecs; return success; } #if 0 static int cd9660_write_buffered(FILE *fd, off_t offset, int buff_len, const unsigned char* buffer) { static int working_sector = -1; static char buf[CD9660_SECTOR_SIZE]; return 0; } #endif int cd9660_copy_file(iso9660_disk *diskStructure, FILE *fd, off_t start_sector, const char *filename) { FILE *rf; int bytes_read; off_t sector = start_sector; int buf_size = diskStructure->sectorSize; char *buf; - buf = malloc(buf_size); - if (buf == NULL) - err(EXIT_FAILURE, "%s: malloc", __func__); - + buf = emalloc(buf_size); if ((rf = fopen(filename, "rb")) == NULL) { warn("%s: cannot open %s", __func__, filename); free(buf); return 0; } if (diskStructure->verbose_level > 1) printf("Writing file: %s\n",filename); if (fseeko(fd, start_sector * diskStructure->sectorSize, SEEK_SET) == -1) err(1, "fseeko"); while (!feof(rf)) { bytes_read = fread(buf,1,buf_size,rf); if (ferror(rf)) { warn("%s: fread", __func__); free(buf); (void)fclose(rf); return 0; } fwrite(buf,1,bytes_read,fd); if (ferror(fd)) { warn("%s: fwrite", __func__); free(buf); (void)fclose(rf); return 0; } sector++; } fclose(rf); free(buf); return 1; } static void cd9660_write_rr(iso9660_disk *diskStructure, FILE *fd, cd9660node *writenode, off_t offset, off_t sector) { int in_ca = 0; struct ISO_SUSP_ATTRIBUTES *myattr; offset += writenode->isoDirRecord->length[0]; if (fseeko(fd, sector * diskStructure->sectorSize + offset, SEEK_SET) == -1) err(1, "fseeko"); /* Offset now points at the end of the record */ TAILQ_FOREACH(myattr, &writenode->head, rr_ll) { fwrite(&(myattr->attr), CD9660_SUSP_ENTRY_SIZE(myattr), 1, fd); if (!in_ca) { offset += CD9660_SUSP_ENTRY_SIZE(myattr); if (myattr->last_in_suf) { /* * Point the offset to the start of this * record's CE area */ if (fseeko(fd, ((off_t)diskStructure-> susp_continuation_area_start_sector * diskStructure->sectorSize) + writenode->susp_entry_ce_start, SEEK_SET) == -1) err(1, "fseeko"); in_ca = 1; } } } /* * If we had to go to the continuation area, head back to * where we should be. */ if (in_ca) if (fseeko(fd, sector * diskStructure->sectorSize + offset, SEEK_SET) == -1) err(1, "fseeko"); } Index: stable/11/usr.sbin/makefs/cd9660/iso9660_rrip.c =================================================================== --- stable/11/usr.sbin/makefs/cd9660/iso9660_rrip.c (revision 332980) +++ stable/11/usr.sbin/makefs/cd9660/iso9660_rrip.c (revision 332981) @@ -1,844 +1,841 @@ /* $NetBSD: iso9660_rrip.c,v 1.14 2014/05/30 13:14:47 martin Exp $ */ /*- * SPDX-License-Identifier: BSD-2-Clause-NetBSD * * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan * Perez-Rathke and Ram Vedam. All rights reserved. * * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys, * Alan Perez-Rathke and Ram Vedam. * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. */ /* This will hold all the function definitions * defined in iso9660_rrip.h */ #include __FBSDID("$FreeBSD$"); #include #include #include #include "makefs.h" #include "cd9660.h" #include "iso9660_rrip.h" +#include static void cd9660_rrip_initialize_inode(cd9660node *); static int cd9660_susp_handle_continuation(iso9660_disk *, cd9660node *); static int cd9660_susp_handle_continuation_common(iso9660_disk *, cd9660node *, int); int cd9660_susp_initialize(iso9660_disk *diskStructure, cd9660node *node, cd9660node *parent, cd9660node *grandparent) { cd9660node *cn; int r; /* Make sure the node is not NULL. If it is, there are major problems */ assert(node != NULL); if (!(node->type & CD9660_TYPE_DOT) && !(node->type & CD9660_TYPE_DOTDOT)) TAILQ_INIT(&(node->head)); if (node->dot_record != 0) TAILQ_INIT(&(node->dot_record->head)); if (node->dot_dot_record != 0) TAILQ_INIT(&(node->dot_dot_record->head)); /* SUSP specific entries here */ if ((r = cd9660_susp_initialize_node(diskStructure, node)) < 0) return r; /* currently called cd9660node_rrip_init_links */ r = cd9660_rrip_initialize_node(diskStructure, node, parent, grandparent); if (r < 0) return r; /* * See if we need a CE record, and set all of the * associated counters. * * This should be called after all extensions. After * this is called, no new records should be added. */ if ((r = cd9660_susp_handle_continuation(diskStructure, node)) < 0) return r; /* Recurse on children. */ TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) { if ((r = cd9660_susp_initialize(diskStructure, cn, node, parent)) < 0) return 0; } return 1; } int cd9660_susp_finalize(iso9660_disk *diskStructure, cd9660node *node) { cd9660node *temp; int r; assert(node != NULL); if (node == diskStructure->rootNode) diskStructure->susp_continuation_area_current_free = 0; if ((r = cd9660_susp_finalize_node(diskStructure, node)) < 0) return r; if ((r = cd9660_rrip_finalize_node(node)) < 0) return r; TAILQ_FOREACH(temp, &node->cn_children, cn_next_child) { if ((r = cd9660_susp_finalize(diskStructure, temp)) < 0) return r; } return 1; } /* * If we really wanted to speed things up, we could have some sort of * lookup table on the SUSP entry type that calls a functor. Or, we could * combine the functions. These functions are kept separate to allow * easier addition of other extensions. * For the sake of simplicity and clarity, we won't be doing that for now. */ /* * SUSP needs to update the following types: * CE (continuation area) */ int cd9660_susp_finalize_node(iso9660_disk *diskStructure, cd9660node *node) { struct ISO_SUSP_ATTRIBUTES *t; /* Handle CE counters */ if (node->susp_entry_ce_length > 0) { node->susp_entry_ce_start = diskStructure->susp_continuation_area_current_free; diskStructure->susp_continuation_area_current_free += node->susp_entry_ce_length; } TAILQ_FOREACH(t, &node->head, rr_ll) { if (t->susp_type != SUSP_TYPE_SUSP || t->entry_type != SUSP_ENTRY_SUSP_CE) continue; cd9660_bothendian_dword( diskStructure-> susp_continuation_area_start_sector, t->attr.su_entry.CE.ca_sector); cd9660_bothendian_dword( diskStructure-> susp_continuation_area_start_sector, t->attr.su_entry.CE.ca_sector); cd9660_bothendian_dword(node->susp_entry_ce_start, t->attr.su_entry.CE.offset); cd9660_bothendian_dword(node->susp_entry_ce_length, t->attr.su_entry.CE.length); } return 0; } int cd9660_rrip_finalize_node(cd9660node *node) { struct ISO_SUSP_ATTRIBUTES *t; TAILQ_FOREACH(t, &node->head, rr_ll) { if (t->susp_type != SUSP_TYPE_RRIP) continue; switch (t->entry_type) { case SUSP_ENTRY_RRIP_CL: /* Look at rr_relocated*/ if (node->rr_relocated == NULL) return -1; cd9660_bothendian_dword( node->rr_relocated->fileDataSector, (unsigned char *) t->attr.rr_entry.CL.dir_loc); break; case SUSP_ENTRY_RRIP_PL: /* Look at rr_real_parent */ if (node->parent == NULL || node->parent->rr_real_parent == NULL) return -1; cd9660_bothendian_dword( node->parent->rr_real_parent->fileDataSector, (unsigned char *) t->attr.rr_entry.PL.dir_loc); break; } } return 0; } static int cd9660_susp_handle_continuation_common(iso9660_disk *diskStructure, cd9660node *node, int space) { int ca_used, susp_used, susp_used_pre_ce, working; struct ISO_SUSP_ATTRIBUTES *temp, *pre_ce, *last, *CE, *ST; pre_ce = last = NULL; working = 254 - space; if (node->su_tail_size > 0) /* Allow 4 bytes for "ST" record. */ working -= node->su_tail_size + 4; /* printf("There are %i bytes to work with\n",working); */ susp_used_pre_ce = susp_used = 0; ca_used = 0; TAILQ_FOREACH(temp, &node->head, rr_ll) { if (working < 0) break; /* * printf("SUSP Entry found, length is %i\n", * CD9660_SUSP_ENTRY_SIZE(temp)); */ working -= CD9660_SUSP_ENTRY_SIZE(temp); if (working >= 0) { last = temp; susp_used += CD9660_SUSP_ENTRY_SIZE(temp); } if (working >= 28) { /* * Remember the last entry after which we * could insert a "CE" entry. */ pre_ce = last; susp_used_pre_ce = susp_used; } } /* A CE entry is needed */ if (working <= 0) { CE = cd9660node_susp_create_node(SUSP_TYPE_SUSP, SUSP_ENTRY_SUSP_CE, "CE", SUSP_LOC_ENTRY); cd9660_susp_ce(CE, node); /* This will automatically insert at the appropriate location */ if (pre_ce != NULL) TAILQ_INSERT_AFTER(&node->head, pre_ce, CE, rr_ll); else TAILQ_INSERT_HEAD(&node->head, CE, rr_ll); last = CE; susp_used = susp_used_pre_ce + 28; /* Count how much CA data is necessary */ for (temp = TAILQ_NEXT(last, rr_ll); temp != NULL; temp = TAILQ_NEXT(temp, rr_ll)) { ca_used += CD9660_SUSP_ENTRY_SIZE(temp); } } /* An ST entry is needed */ if (node->su_tail_size > 0) { ST = cd9660node_susp_create_node(SUSP_TYPE_SUSP, SUSP_ENTRY_SUSP_ST, "ST", SUSP_LOC_ENTRY); cd9660_susp_st(ST, node); if (last != NULL) TAILQ_INSERT_AFTER(&node->head, last, ST, rr_ll); else TAILQ_INSERT_HEAD(&node->head, ST, rr_ll); last = ST; susp_used += 4; } if (last != NULL) last->last_in_suf = 1; node->susp_entry_size = susp_used; node->susp_entry_ce_length = ca_used; diskStructure->susp_continuation_area_size += ca_used; return 1; } /* See if a continuation entry is needed for each of the different types */ static int cd9660_susp_handle_continuation(iso9660_disk *diskStructure, cd9660node *node) { assert (node != NULL); /* Entry */ if (cd9660_susp_handle_continuation_common(diskStructure, node,(int)(node->isoDirRecord->length[0])) < 0) return 0; return 1; } int cd9660_susp_initialize_node(iso9660_disk *diskStructure, cd9660node *node) { struct ISO_SUSP_ATTRIBUTES *temp; /* * Requirements/notes: * CE: is added for us where needed * ST: not sure if it is even required, but if so, should be * handled by the CE code * PD: isn't needed (though might be added for testing) * SP: is stored ONLY on the . record of the root directory * ES: not sure */ /* Check for root directory, add SP and ER if needed. */ if (node->type & CD9660_TYPE_DOT) { if (node->parent == diskStructure->rootNode) { temp = cd9660node_susp_create_node(SUSP_TYPE_SUSP, SUSP_ENTRY_SUSP_SP, "SP", SUSP_LOC_DOT); cd9660_susp_sp(temp, node); /* Should be first entry. */ TAILQ_INSERT_HEAD(&node->head, temp, rr_ll); } } return 1; } static void cd9660_rrip_initialize_inode(cd9660node *node) { struct ISO_SUSP_ATTRIBUTES *attr; /* * Inode dependent values - this may change, * but for now virtual files and directories do * not have an inode structure */ if ((node->node != NULL) && (node->node->inode != NULL)) { /* PX - POSIX attributes */ attr = cd9660node_susp_create_node(SUSP_TYPE_RRIP, SUSP_ENTRY_RRIP_PX, "PX", SUSP_LOC_ENTRY); cd9660node_rrip_px(attr, node->node); TAILQ_INSERT_TAIL(&node->head, attr, rr_ll); /* TF - timestamp */ attr = cd9660node_susp_create_node(SUSP_TYPE_RRIP, SUSP_ENTRY_RRIP_TF, "TF", SUSP_LOC_ENTRY); cd9660node_rrip_tf(attr, node->node); TAILQ_INSERT_TAIL(&node->head, attr, rr_ll); /* SL - Symbolic link */ /* ?????????? Dan - why is this here? */ if (TAILQ_EMPTY(&node->cn_children) && node->node->inode != NULL && S_ISLNK(node->node->inode->st.st_mode)) cd9660_createSL(node); /* PN - device number */ if (node->node->inode != NULL && ((S_ISCHR(node->node->inode->st.st_mode) || S_ISBLK(node->node->inode->st.st_mode)))) { attr = cd9660node_susp_create_node(SUSP_TYPE_RRIP, SUSP_ENTRY_RRIP_PN, "PN", SUSP_LOC_ENTRY); cd9660node_rrip_pn(attr, node->node); TAILQ_INSERT_TAIL(&node->head, attr, rr_ll); } } } int cd9660_rrip_initialize_node(iso9660_disk *diskStructure, cd9660node *node, cd9660node *parent, cd9660node *grandparent) { struct ISO_SUSP_ATTRIBUTES *current = NULL; assert(node != NULL); if (node->type & CD9660_TYPE_DOT) { /* * Handle ER - should be the only entry to appear on * a "." record */ if (node->parent == diskStructure->rootNode) { cd9660_susp_ER(node, 1, SUSP_RRIP_ER_EXT_ID, SUSP_RRIP_ER_EXT_DES, SUSP_RRIP_ER_EXT_SRC); } if (parent != NULL && parent->node != NULL && parent->node->inode != NULL) { /* PX - POSIX attributes */ current = cd9660node_susp_create_node(SUSP_TYPE_RRIP, SUSP_ENTRY_RRIP_PX, "PX", SUSP_LOC_ENTRY); cd9660node_rrip_px(current, parent->node); TAILQ_INSERT_TAIL(&node->head, current, rr_ll); } } else if (node->type & CD9660_TYPE_DOTDOT) { if (grandparent != NULL && grandparent->node != NULL && grandparent->node->inode != NULL) { /* PX - POSIX attributes */ current = cd9660node_susp_create_node(SUSP_TYPE_RRIP, SUSP_ENTRY_RRIP_PX, "PX", SUSP_LOC_ENTRY); cd9660node_rrip_px(current, grandparent->node); TAILQ_INSERT_TAIL(&node->head, current, rr_ll); } /* Handle PL */ if (parent != NULL && parent->rr_real_parent != NULL) { current = cd9660node_susp_create_node(SUSP_TYPE_RRIP, SUSP_ENTRY_RRIP_PL, "PL", SUSP_LOC_DOTDOT); cd9660_rrip_PL(current,node); TAILQ_INSERT_TAIL(&node->head, current, rr_ll); } } else { cd9660_rrip_initialize_inode(node); /* * Not every node needs a NM set - only if the name is * actually different. IE: If a file is TEST -> TEST, * no NM. test -> TEST, need a NM * * The rr_moved_dir needs to be assigned a NM record as well. */ if (node == diskStructure->rr_moved_dir) { cd9660_rrip_add_NM(node, RRIP_DEFAULT_MOVE_DIR_NAME); } else if ((node->node != NULL) && ((strlen(node->node->name) != (uint8_t)node->isoDirRecord->name_len[0]) || (memcmp(node->node->name,node->isoDirRecord->name, (uint8_t)node->isoDirRecord->name_len[0]) != 0))) { cd9660_rrip_NM(node); } /* Rock ridge directory relocation code here. */ /* First handle the CL for the placeholder file. */ if (node->rr_relocated != NULL) { current = cd9660node_susp_create_node(SUSP_TYPE_RRIP, SUSP_ENTRY_RRIP_CL, "CL", SUSP_LOC_ENTRY); cd9660_rrip_CL(current, node); TAILQ_INSERT_TAIL(&node->head, current, rr_ll); } /* Handle RE*/ if (node->rr_real_parent != NULL) { current = cd9660node_susp_create_node(SUSP_TYPE_RRIP, SUSP_ENTRY_RRIP_RE, "RE", SUSP_LOC_ENTRY); cd9660_rrip_RE(current,node); TAILQ_INSERT_TAIL(&node->head, current, rr_ll); } } return 1; } struct ISO_SUSP_ATTRIBUTES* cd9660node_susp_create_node(int susp_type, int entry_type, const char *type_id, int write_loc) { struct ISO_SUSP_ATTRIBUTES* temp; - if ((temp = malloc(sizeof(struct ISO_SUSP_ATTRIBUTES))) == NULL) { - CD9660_MEM_ALLOC_ERROR("cd9660node_susp_create_node"); - exit(1); - } - + temp = emalloc(sizeof(*temp)); temp->susp_type = susp_type; temp->entry_type = entry_type; temp->last_in_suf = 0; /* Phase this out */ temp->type_of[0] = type_id[0]; temp->type_of[1] = type_id[1]; temp->write_location = write_loc; /* * Since the first four bytes is common, lets go ahead and * set the type identifier, since we are passing that to this * function anyhow. */ temp->attr.su_entry.SP.h.type[0] = type_id[0]; temp->attr.su_entry.SP.h.type[1] = type_id[1]; return temp; } int cd9660_rrip_PL(struct ISO_SUSP_ATTRIBUTES* p, cd9660node *node __unused) { p->attr.rr_entry.PL.h.length[0] = 12; p->attr.rr_entry.PL.h.version[0] = 1; return 1; } int cd9660_rrip_CL(struct ISO_SUSP_ATTRIBUTES *p, cd9660node *node __unused) { p->attr.rr_entry.CL.h.length[0] = 12; p->attr.rr_entry.CL.h.version[0] = 1; return 1; } int cd9660_rrip_RE(struct ISO_SUSP_ATTRIBUTES *p, cd9660node *node __unused) { p->attr.rr_entry.RE.h.length[0] = 4; p->attr.rr_entry.RE.h.version[0] = 1; return 1; } void cd9660_createSL(cd9660node *node) { struct ISO_SUSP_ATTRIBUTES* current; int path_count, dir_count, done, i, j, dir_copied; char temp_cr[255]; char temp_sl[255]; /* used in copying continuation entry*/ char* sl_ptr; sl_ptr = node->node->symlink; done = 0; path_count = 0; dir_count = 0; dir_copied = 0; current = cd9660node_susp_create_node(SUSP_TYPE_RRIP, SUSP_ENTRY_RRIP_SL, "SL", SUSP_LOC_ENTRY); current->attr.rr_entry.SL.h.version[0] = 1; current->attr.rr_entry.SL.flags[0] = SL_FLAGS_NONE; if (*sl_ptr == '/') { temp_cr[0] = SL_FLAGS_ROOT; temp_cr[1] = 0; memcpy(current->attr.rr_entry.SL.component + path_count, temp_cr, 2); path_count += 2; sl_ptr++; } for (i = 0; i < (dir_count + 2); i++) temp_cr[i] = '\0'; while (!done) { while ((*sl_ptr != '/') && (*sl_ptr != '\0')) { dir_copied = 1; if (*sl_ptr == '.') { if ((*(sl_ptr + 1) == '/') || (*(sl_ptr + 1) == '\0')) { temp_cr[0] = SL_FLAGS_CURRENT; sl_ptr++; } else if(*(sl_ptr + 1) == '.') { if ((*(sl_ptr + 2) == '/') || (*(sl_ptr + 2) == '\0')) { temp_cr[0] = SL_FLAGS_PARENT; sl_ptr += 2; } } else { temp_cr[dir_count+2] = *sl_ptr; sl_ptr++; dir_count++; } } else { temp_cr[dir_count + 2] = *sl_ptr; sl_ptr++; dir_count++; } } if ((path_count + dir_count) >= 249) { current->attr.rr_entry.SL.flags[0] |= SL_FLAGS_CONTINUE; j = 0; if (path_count <= 249) { while(j != (249 - path_count)) { temp_sl[j] = temp_cr[j]; j++; } temp_sl[0] = SL_FLAGS_CONTINUE; temp_sl[1] = j - 2; memcpy( current->attr.rr_entry.SL.component + path_count, temp_sl, j); } path_count += j; current->attr.rr_entry.SL.h.length[0] = path_count + 5; TAILQ_INSERT_TAIL(&node->head, current, rr_ll); current= cd9660node_susp_create_node(SUSP_TYPE_RRIP, SUSP_ENTRY_RRIP_SL, "SL", SUSP_LOC_ENTRY); current->attr.rr_entry.SL.h.version[0] = 1; current->attr.rr_entry.SL.flags[0] = SL_FLAGS_NONE; path_count = 0; if (dir_count > 2) { while (j != dir_count + 2) { current->attr.rr_entry.SL.component[ path_count + 2] = temp_cr[j]; j++; path_count++; } current->attr.rr_entry.SL.component[1] = path_count; path_count+= 2; } else { while(j != dir_count) { current->attr.rr_entry.SL.component[ path_count+2] = temp_cr[j]; j++; path_count++; } } } else { if (dir_copied == 1) { temp_cr[1] = dir_count; memcpy(current->attr.rr_entry.SL.component + path_count, temp_cr, dir_count + 2); path_count += dir_count + 2; } } if (*sl_ptr == '\0') { done = 1; current->attr.rr_entry.SL.h.length[0] = path_count + 5; TAILQ_INSERT_TAIL(&node->head, current, rr_ll); } else { sl_ptr++; dir_count = 0; dir_copied = 0; for(i = 0; i < 255; i++) { temp_cr[i] = '\0'; } } } } int cd9660node_rrip_px(struct ISO_SUSP_ATTRIBUTES *v, fsnode *pxinfo) { v->attr.rr_entry.PX.h.length[0] = 44; v->attr.rr_entry.PX.h.version[0] = 1; cd9660_bothendian_dword(pxinfo->inode->st.st_mode, v->attr.rr_entry.PX.mode); cd9660_bothendian_dword(pxinfo->inode->st.st_nlink, v->attr.rr_entry.PX.links); cd9660_bothendian_dword(pxinfo->inode->st.st_uid, v->attr.rr_entry.PX.uid); cd9660_bothendian_dword(pxinfo->inode->st.st_gid, v->attr.rr_entry.PX.gid); cd9660_bothendian_dword(pxinfo->inode->st.st_ino, v->attr.rr_entry.PX.serial); return 1; } int cd9660node_rrip_pn(struct ISO_SUSP_ATTRIBUTES *pn_field, fsnode *fnode) { pn_field->attr.rr_entry.PN.h.length[0] = 20; pn_field->attr.rr_entry.PN.h.version[0] = 1; if (sizeof (fnode->inode->st.st_rdev) > 4) cd9660_bothendian_dword( (uint64_t)fnode->inode->st.st_rdev >> 32, pn_field->attr.rr_entry.PN.high); else cd9660_bothendian_dword(0, pn_field->attr.rr_entry.PN.high); cd9660_bothendian_dword(fnode->inode->st.st_rdev & 0xffffffff, pn_field->attr.rr_entry.PN.low); return 1; } #if 0 int cd9660node_rrip_nm(struct ISO_SUSP_ATTRIBUTES *p, cd9660node *file_node) { int nm_length = strlen(file_node->isoDirRecord->name) + 5; p->attr.rr_entry.NM.h.type[0] = 'N'; p->attr.rr_entry.NM.h.type[1] = 'M'; sprintf(p->attr.rr_entry.NM.altname, "%s", file_node->isoDirRecord->name); p->attr.rr_entry.NM.h.length[0] = (unsigned char)nm_length; p->attr.rr_entry.NM.h.version[0] = (unsigned char)1; p->attr.rr_entry.NM.flags[0] = (unsigned char) NM_PARENT; return 1; } #endif int cd9660node_rrip_tf(struct ISO_SUSP_ATTRIBUTES *p, fsnode *_node) { p->attr.rr_entry.TF.flags[0] = TF_MODIFY | TF_ACCESS | TF_ATTRIBUTES; p->attr.rr_entry.TF.h.length[0] = 5; p->attr.rr_entry.TF.h.version[0] = 1; /* * Need to add creation time, backup time, * expiration time, and effective time. */ cd9660_time_915(p->attr.rr_entry.TF.timestamp, _node->inode->st.st_atime); p->attr.rr_entry.TF.h.length[0] += 7; cd9660_time_915(p->attr.rr_entry.TF.timestamp + 7, _node->inode->st.st_mtime); p->attr.rr_entry.TF.h.length[0] += 7; cd9660_time_915(p->attr.rr_entry.TF.timestamp + 14, _node->inode->st.st_ctime); p->attr.rr_entry.TF.h.length[0] += 7; return 1; } int cd9660_susp_sp(struct ISO_SUSP_ATTRIBUTES *p, cd9660node *spinfo __unused) { p->attr.su_entry.SP.h.length[0] = 7; p->attr.su_entry.SP.h.version[0] = 1; p->attr.su_entry.SP.check[0] = 0xBE; p->attr.su_entry.SP.check[1] = 0xEF; p->attr.su_entry.SP.len_skp[0] = 0; return 1; } int cd9660_susp_st(struct ISO_SUSP_ATTRIBUTES *p, cd9660node *stinfo __unused) { p->attr.su_entry.ST.h.type[0] = 'S'; p->attr.su_entry.ST.h.type[1] = 'T'; p->attr.su_entry.ST.h.length[0] = 4; p->attr.su_entry.ST.h.version[0] = 1; return 1; } int cd9660_susp_ce(struct ISO_SUSP_ATTRIBUTES *p, cd9660node *spinfo __unused) { p->attr.su_entry.CE.h.length[0] = 28; p->attr.su_entry.CE.h.version[0] = 1; /* Other attributes dont matter right now, will be updated later */ return 1; } int cd9660_susp_pd(struct ISO_SUSP_ATTRIBUTES *p __unused, int length __unused) { return 1; } void cd9660_rrip_add_NM(cd9660node *node, const char *name) { int working,len; const char *p; struct ISO_SUSP_ATTRIBUTES *r; /* * Each NM record has 254 byes to work with. This means that * the name data itself only has 249 bytes to work with. So, a * name with 251 characters would require two nm records. */ p = name; working = 1; while (working) { r = cd9660node_susp_create_node(SUSP_TYPE_RRIP, SUSP_ENTRY_RRIP_NM, "NM", SUSP_LOC_ENTRY); r->attr.rr_entry.NM.h.version[0] = 1; r->attr.rr_entry.NM.flags[0] = RRIP_NM_FLAGS_NONE; len = strlen(p); if (len > 249) { len = 249; r->attr.rr_entry.NM.flags[0] = RRIP_NM_FLAGS_CONTINUE; } else { working = 0; } memcpy(r->attr.rr_entry.NM.altname, p, len); r->attr.rr_entry.NM.h.length[0] = 5 + len; TAILQ_INSERT_TAIL(&node->head, r, rr_ll); p += len; } } void cd9660_rrip_NM(cd9660node *node) { cd9660_rrip_add_NM(node, node->node->name); } struct ISO_SUSP_ATTRIBUTES* cd9660_susp_ER(cd9660node *node, u_char ext_version, const char* ext_id, const char* ext_des, const char* ext_src) { int l; struct ISO_SUSP_ATTRIBUTES *r; r = cd9660node_susp_create_node(SUSP_TYPE_SUSP, SUSP_ENTRY_SUSP_ER, "ER", SUSP_LOC_DOT); /* Fixed data is 8 bytes */ r->attr.su_entry.ER.h.length[0] = 8; r->attr.su_entry.ER.h.version[0] = 1; r->attr.su_entry.ER.len_id[0] = (u_char)strlen(ext_id); r->attr.su_entry.ER.len_des[0] = (u_char)strlen(ext_des); r->attr.su_entry.ER.len_src[0] = (u_char)strlen(ext_src); l = r->attr.su_entry.ER.len_id[0] + r->attr.su_entry.ER.len_src[0] + r->attr.su_entry.ER.len_des[0]; /* Everything must fit. */ assert(l + r->attr.su_entry.ER.h.length[0] <= 254); r->attr.su_entry.ER.h.length[0] += (u_char)l; r->attr.su_entry.ER.ext_ver[0] = ext_version; memcpy(r->attr.su_entry.ER.ext_data, ext_id, (int)r->attr.su_entry.ER.len_id[0]); l = (int) r->attr.su_entry.ER.len_id[0]; memcpy(r->attr.su_entry.ER.ext_data + l,ext_des, (int)r->attr.su_entry.ER.len_des[0]); l += (int)r->attr.su_entry.ER.len_des[0]; memcpy(r->attr.su_entry.ER.ext_data + l,ext_src, (int)r->attr.su_entry.ER.len_src[0]); TAILQ_INSERT_TAIL(&node->head, r, rr_ll); return r; } struct ISO_SUSP_ATTRIBUTES* cd9660_susp_ES(struct ISO_SUSP_ATTRIBUTES *last __unused, cd9660node *node __unused) { return NULL; } Index: stable/11/usr.sbin/makefs/cd9660.c =================================================================== --- stable/11/usr.sbin/makefs/cd9660.c (revision 332980) +++ stable/11/usr.sbin/makefs/cd9660.c (revision 332981) @@ -1,2198 +1,2134 @@ /* $NetBSD: cd9660.c,v 1.32 2011/08/23 17:09:11 christos Exp $ */ /*- * SPDX-License-Identifier: BSD-2-Clause-NetBSD AND BSD-4-Clause * * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan * Perez-Rathke and Ram Vedam. All rights reserved. * * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys, * Alan Perez-Rathke and Ram Vedam. * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. */ /* * Copyright (c) 2001 Wasabi Systems, Inc. * All rights reserved. * * Written by Luke Mewburn for Wasabi Systems, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed for the NetBSD Project by * Wasabi Systems, Inc. * 4. The name of Wasabi Systems, Inc. may not be used to endorse * or promote products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /* * Copyright (c) 1982, 1986, 1989, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include +#include #include "makefs.h" #include "cd9660.h" #include "cd9660/iso9660_rrip.h" #include "cd9660/cd9660_archimedes.h" static void cd9660_finalize_PVD(iso9660_disk *); static cd9660node *cd9660_allocate_cd9660node(void); static void cd9660_set_defaults(iso9660_disk *); static int cd9660_arguments_set_string(const char *, const char *, int, char, char *); static void cd9660_populate_iso_dir_record( struct _iso_directory_record_cd9660 *, u_char, u_char, u_char, const char *); static void cd9660_setup_root_node(iso9660_disk *); static int cd9660_setup_volume_descriptors(iso9660_disk *); #if 0 static int cd9660_fill_extended_attribute_record(cd9660node *); #endif static void cd9660_sort_nodes(cd9660node *); static int cd9660_translate_node_common(iso9660_disk *, cd9660node *); static int cd9660_translate_node(iso9660_disk *, fsnode *, cd9660node *); static int cd9660_compare_filename(const char *, const char *); static void cd9660_sorted_child_insert(cd9660node *, cd9660node *); static int cd9660_handle_collisions(iso9660_disk *, cd9660node *, int); static cd9660node *cd9660_rename_filename(iso9660_disk *, cd9660node *, int, int); static void cd9660_copy_filenames(iso9660_disk *, cd9660node *); static void cd9660_sorting_nodes(cd9660node *); static int cd9660_count_collisions(cd9660node *); static cd9660node *cd9660_rrip_move_directory(iso9660_disk *, cd9660node *); static int cd9660_add_dot_records(iso9660_disk *, cd9660node *); static void cd9660_convert_structure(iso9660_disk *, fsnode *, cd9660node *, int, int *, int *); static void cd9660_free_structure(cd9660node *); static int cd9660_generate_path_table(iso9660_disk *); static int cd9660_level1_convert_filename(iso9660_disk *, const char *, char *, int); static int cd9660_level2_convert_filename(iso9660_disk *, const char *, char *, int); #if 0 static int cd9660_joliet_convert_filename(iso9660_disk *, const char *, char *, int); #endif static int cd9660_convert_filename(iso9660_disk *, const char *, char *, int); static void cd9660_populate_dot_records(iso9660_disk *, cd9660node *); static int64_t cd9660_compute_offsets(iso9660_disk *, cd9660node *, int64_t); #if 0 static int cd9660_copy_stat_info(cd9660node *, cd9660node *, int); #endif static cd9660node *cd9660_create_virtual_entry(iso9660_disk *, const char *, cd9660node *, int, int); static cd9660node *cd9660_create_file(iso9660_disk *, const char *, cd9660node *, cd9660node *); static cd9660node *cd9660_create_directory(iso9660_disk *, const char *, cd9660node *, cd9660node *); static cd9660node *cd9660_create_special_directory(iso9660_disk *, u_char, cd9660node *); /* * Allocate and initialize a cd9660node * @returns struct cd9660node * Pointer to new node, or NULL on error */ static cd9660node * cd9660_allocate_cd9660node(void) { - cd9660node *temp; + cd9660node *temp = ecalloc(1, sizeof(*temp)); - if ((temp = calloc(1, sizeof(cd9660node))) == NULL) - err(EXIT_FAILURE, "%s: calloc", __func__); TAILQ_INIT(&temp->cn_children); temp->parent = temp->dot_record = temp->dot_dot_record = NULL; temp->ptnext = temp->ptprev = temp->ptlast = NULL; temp->node = NULL; temp->isoDirRecord = NULL; temp->isoExtAttributes = NULL; temp->rr_real_parent = temp->rr_relocated = NULL; temp->su_tail_data = NULL; return temp; } int cd9660_defaults_set = 0; /** * Set default values for cd9660 extension to makefs */ static void cd9660_set_defaults(iso9660_disk *diskStructure) { /*Fix the sector size for now, though the spec allows for other sizes*/ diskStructure->sectorSize = 2048; /* Set up defaults in our own structure */ diskStructure->verbose_level = 0; diskStructure->keep_bad_images = 0; diskStructure->follow_sym_links = 0; diskStructure->isoLevel = 2; diskStructure->rock_ridge_enabled = 0; diskStructure->rock_ridge_renamed_dir_name = 0; diskStructure->rock_ridge_move_count = 0; diskStructure->rr_moved_dir = 0; diskStructure->archimedes_enabled = 0; diskStructure->chrp_boot = 0; diskStructure->include_padding_areas = 1; /* Spec breaking functionality */ diskStructure->allow_deep_trees = diskStructure->allow_start_dot = diskStructure->allow_max_name = diskStructure->allow_illegal_chars = diskStructure->allow_lowercase = diskStructure->allow_multidot = diskStructure->omit_trailing_period = 0; /* Make sure the PVD is clear */ memset(&diskStructure->primaryDescriptor, 0, 2048); memset(diskStructure->primaryDescriptor.publisher_id, 0x20,128); memset(diskStructure->primaryDescriptor.preparer_id, 0x20,128); memset(diskStructure->primaryDescriptor.application_id, 0x20,128); memset(diskStructure->primaryDescriptor.copyright_file_id, 0x20,37); memset(diskStructure->primaryDescriptor.abstract_file_id, 0x20,37); memset(diskStructure->primaryDescriptor.bibliographic_file_id, 0x20,37); strcpy(diskStructure->primaryDescriptor.system_id, "FreeBSD"); cd9660_defaults_set = 1; /* Boot support: Initially disabled */ diskStructure->has_generic_bootimage = 0; diskStructure->generic_bootimage = NULL; diskStructure->boot_image_directory = 0; /*memset(diskStructure->boot_descriptor, 0, 2048);*/ diskStructure->is_bootable = 0; TAILQ_INIT(&diskStructure->boot_images); LIST_INIT(&diskStructure->boot_entries); } void cd9660_prep_opts(fsinfo_t *fsopts) { - iso9660_disk *diskStructure; + iso9660_disk *diskStructure = ecalloc(1, sizeof(*diskStructure)); - if ((diskStructure = calloc(1, sizeof(*diskStructure))) == NULL) - err(EXIT_FAILURE, "%s: calloc", __func__); - #define OPT_STR(letter, name, desc) \ { letter, name, NULL, OPT_STRBUF, 0, 0, desc } #define OPT_NUM(letter, name, field, min, max, desc) \ { letter, name, &diskStructure->field, \ sizeof(diskStructure->field) == 8 ? OPT_INT64 : \ (sizeof(diskStructure->field) == 4 ? OPT_INT32 : \ (sizeof(diskStructure->field) == 2 ? OPT_INT16 : OPT_INT8)), \ min, max, desc } #define OPT_BOOL(letter, name, field, desc) \ OPT_NUM(letter, name, field, 0, 1, desc) const option_t cd9660_options[] = { OPT_NUM('l', "isolevel", isoLevel, 1, 2, "ISO Level"), OPT_NUM('v', "verbose", verbose_level, 0, 2, "Turns on verbose output"), OPT_BOOL('h', "help", displayHelp, "Show help message"), OPT_BOOL('S', "follow-symlinks", follow_sym_links, "Resolve symlinks in pathnames"), OPT_BOOL('R', "rockridge", rock_ridge_enabled, "Enable Rock-Ridge extensions"), OPT_BOOL('C', "chrp-boot", chrp_boot, "Enable CHRP boot"), OPT_BOOL('K', "keep-bad-images", keep_bad_images, "Keep bad images"), OPT_BOOL('D', "allow-deep-trees", allow_deep_trees, "Allow trees more than 8 levels"), OPT_BOOL('a', "allow-max-name", allow_max_name, "Allow 37 char filenames (unimplemented)"), OPT_BOOL('i', "allow-illegal-chars", allow_illegal_chars, "Allow illegal characters in filenames"), OPT_BOOL('d', "allow-multidot", allow_multidot, "Allow multiple periods in filenames"), OPT_BOOL('o', "omit-trailing-period", omit_trailing_period, "Omit trailing periods in filenames"), OPT_BOOL('\0', "allow-lowercase", allow_lowercase, "Allow lowercase characters in filenames"), OPT_BOOL('\0', "archimedes", archimedes_enabled, "Enable Archimedes structure"), OPT_BOOL('\0', "no-trailing-padding", include_padding_areas, "Include padding areas"), OPT_STR('A', "applicationid", "Application Identifier"), OPT_STR('P', "publisher", "Publisher Identifier"), OPT_STR('p', "preparer", "Preparer Identifier"), OPT_STR('L', "label", "Disk Label"), OPT_STR('V', "volumeid", "Volume Set Identifier"), OPT_STR('B', "bootimage", "Boot image parameter"), OPT_STR('G', "generic-bootimage", "Generic boot image param"), OPT_STR('\0', "bootimagedir", "Boot image directory"), OPT_STR('\0', "no-emul-boot", "No boot emulation"), OPT_STR('\0', "no-boot", "No boot support"), OPT_STR('\0', "hard-disk-boot", "Boot from hard disk"), OPT_STR('\0', "boot-load-segment", "Boot load segment"), { .name = NULL } }; fsopts->fs_specific = diskStructure; fsopts->fs_options = copy_opts(cd9660_options); cd9660_set_defaults(diskStructure); } void cd9660_cleanup_opts(fsinfo_t *fsopts) { free(fsopts->fs_specific); free(fsopts->fs_options); } static int cd9660_arguments_set_string(const char *val, const char *fieldtitle, int length, char testmode, char * dest) { int len, test; if (val == NULL) warnx("error: The %s requires a string argument", fieldtitle); else if ((len = strlen(val)) <= length) { if (testmode == 'd') test = cd9660_valid_d_chars(val); else test = cd9660_valid_a_chars(val); if (test) { memcpy(dest, val, len); if (test == 2) cd9660_uppercase_characters(dest, len); return 1; } else warnx("error: The %s must be composed of " "%c-characters", fieldtitle, testmode); } else warnx("error: The %s must be at most 32 characters long", fieldtitle); return 0; } /* * Command-line parsing function */ int cd9660_parse_opts(const char *option, fsinfo_t *fsopts) { int rv, i; iso9660_disk *diskStructure = fsopts->fs_specific; option_t *cd9660_options = fsopts->fs_options; char buf[1024]; const char *name, *desc; assert(option != NULL); if (debug & DEBUG_FS_PARSE_OPTS) printf("cd9660_parse_opts: got `%s'\n", option); i = set_option(cd9660_options, option, buf, sizeof(buf)); if (i == -1) return 0; if (cd9660_options[i].name == NULL) abort(); name = cd9660_options[i].name; desc = cd9660_options[i].desc; switch (cd9660_options[i].letter) { case 'h': case 'S': rv = 0; /* this is not handled yet */ break; case 'L': rv = cd9660_arguments_set_string(buf, desc, 32, 'd', diskStructure->primaryDescriptor.volume_id); break; case 'A': rv = cd9660_arguments_set_string(buf, desc, 128, 'a', diskStructure->primaryDescriptor.application_id); break; case 'P': rv = cd9660_arguments_set_string(buf, desc, 128, 'a', diskStructure->primaryDescriptor.publisher_id); break; case 'p': rv = cd9660_arguments_set_string(buf, desc, 128, 'a', diskStructure->primaryDescriptor.preparer_id); break; case 'V': rv = cd9660_arguments_set_string(buf, desc, 128, 'a', diskStructure->primaryDescriptor.volume_set_id); break; /* Boot options */ case 'B': if (buf[0] == '\0') { warnx("The Boot Image parameter requires a valid boot" "information string"); rv = 0; } else rv = cd9660_add_boot_disk(diskStructure, buf); break; case 'G': if (buf[0] == '\0') { warnx("The Generic Boot Image parameter requires a" " valid boot information string"); rv = 0; } else rv = cd9660_add_generic_bootimage(diskStructure, buf); break; default: if (strcmp(name, "bootimagedir") == 0) { /* * XXXfvdl this is unused. */ if (buf[0] == '\0') { warnx("The Boot Image Directory parameter" " requires a directory name\n"); rv = 0; } else { diskStructure->boot_image_directory = - malloc(strlen(buf) + 1); - if (diskStructure->boot_image_directory == NULL) - err(1, "malloc"); + emalloc(strlen(buf) + 1); /* BIG TODO: Add the max length function here */ rv = cd9660_arguments_set_string(buf, desc, 12, 'd', diskStructure->boot_image_directory); } } else if (strcmp(name, "no-emul-boot") == 0 || strcmp(name, "no-boot") == 0 || strcmp(name, "hard-disk-boot") == 0) { /* RRIP */ cd9660_eltorito_add_boot_option(diskStructure, name, 0); rv = 1; } else if (strcmp(name, "boot-load-segment") == 0) { if (buf[0] == '\0') { warnx("Option `%s' doesn't contain a value", name); rv = 0; } else { cd9660_eltorito_add_boot_option(diskStructure, name, buf); rv = 1; } } else rv = 1; } return rv; } /* * Main function for cd9660_makefs * Builds the ISO image file * @param const char *image The image filename to create * @param const char *dir The directory that is being read * @param struct fsnode *root The root node of the filesystem tree * @param struct fsinfo_t *fsopts Any options */ void cd9660_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts) { int64_t startoffset; int numDirectories; uint64_t pathTableSectors; int64_t firstAvailableSector; int64_t totalSpace; int error; cd9660node *real_root; iso9660_disk *diskStructure = fsopts->fs_specific; if (diskStructure->verbose_level > 0) printf("cd9660_makefs: ISO level is %i\n", diskStructure->isoLevel); if (diskStructure->isoLevel < 2 && diskStructure->allow_multidot) errx(1, "allow-multidot requires iso level of 2\n"); assert(image != NULL); assert(dir != NULL); assert(root != NULL); if (diskStructure->displayHelp) { /* * Display help here - probably want to put it in * a separate function */ return; } if (diskStructure->verbose_level > 0) printf("cd9660_makefs: image %s directory %s root %p\n", image, dir, root); /* Set up some constants. Later, these will be defined with options */ /* Counter needed for path tables */ numDirectories = 0; /* Convert tree to our own format */ /* Actually, we now need to add the REAL root node, at level 0 */ real_root = cd9660_allocate_cd9660node(); - if ((real_root->isoDirRecord = - malloc( sizeof(iso_directory_record_cd9660) )) == NULL) { - CD9660_MEM_ALLOC_ERROR("cd9660_makefs"); - exit(1); - } - + real_root->isoDirRecord = emalloc(sizeof(*real_root->isoDirRecord)); /* Leave filename blank for root */ memset(real_root->isoDirRecord->name, 0, ISO_FILENAME_MAXLENGTH_WITH_PADDING); real_root->level = 0; diskStructure->rootNode = real_root; real_root->type = CD9660_TYPE_DIR; error = 0; real_root->node = root; cd9660_convert_structure(diskStructure, root, real_root, 1, &numDirectories, &error); if (TAILQ_EMPTY(&real_root->cn_children)) { errx(1, "cd9660_makefs: converted directory is empty. " "Tree conversion failed\n"); } else if (error != 0) { errx(1, "cd9660_makefs: tree conversion failed\n"); } else { if (diskStructure->verbose_level > 0) printf("cd9660_makefs: tree converted\n"); } /* Add the dot and dot dot records */ cd9660_add_dot_records(diskStructure, real_root); cd9660_setup_root_node(diskStructure); if (diskStructure->verbose_level > 0) printf("cd9660_makefs: done converting tree\n"); /* non-SUSP extensions */ if (diskStructure->archimedes_enabled) archimedes_convert_tree(diskStructure->rootNode); /* Rock ridge / SUSP init pass */ if (diskStructure->rock_ridge_enabled) { cd9660_susp_initialize(diskStructure, diskStructure->rootNode, diskStructure->rootNode, NULL); } /* Build path table structure */ diskStructure->pathTableLength = cd9660_generate_path_table( diskStructure); pathTableSectors = CD9660_BLOCKS(diskStructure->sectorSize, diskStructure->pathTableLength); firstAvailableSector = cd9660_setup_volume_descriptors(diskStructure); if (diskStructure->is_bootable) { firstAvailableSector = cd9660_setup_boot(diskStructure, firstAvailableSector); if (firstAvailableSector < 0) errx(1, "setup_boot failed"); } /* LE first, then BE */ diskStructure->primaryLittleEndianTableSector = firstAvailableSector; diskStructure->primaryBigEndianTableSector = diskStructure->primaryLittleEndianTableSector + pathTableSectors; /* Set the secondary ones to -1, not going to use them for now */ diskStructure->secondaryBigEndianTableSector = -1; diskStructure->secondaryLittleEndianTableSector = -1; diskStructure->dataFirstSector = diskStructure->primaryBigEndianTableSector + pathTableSectors; if (diskStructure->verbose_level > 0) printf("cd9660_makefs: Path table conversion complete. " "Each table is %i bytes, or %" PRIu64 " sectors.\n", diskStructure->pathTableLength, pathTableSectors); startoffset = diskStructure->sectorSize*diskStructure->dataFirstSector; totalSpace = cd9660_compute_offsets(diskStructure, real_root, startoffset); diskStructure->totalSectors = diskStructure->dataFirstSector + CD9660_BLOCKS(diskStructure->sectorSize, totalSpace); /* Disabled until pass 1 is done */ if (diskStructure->rock_ridge_enabled) { diskStructure->susp_continuation_area_start_sector = diskStructure->totalSectors; diskStructure->totalSectors += CD9660_BLOCKS(diskStructure->sectorSize, diskStructure->susp_continuation_area_size); cd9660_susp_finalize(diskStructure, diskStructure->rootNode); } cd9660_finalize_PVD(diskStructure); /* Add padding sectors, just for testing purposes right now */ /* diskStructure->totalSectors+=150; */ /* Debugging output */ if (diskStructure->verbose_level > 0) { printf("cd9660_makefs: Sectors 0-15 reserved\n"); printf("cd9660_makefs: Primary path tables starts in sector %" PRId64 "\n", diskStructure->primaryLittleEndianTableSector); printf("cd9660_makefs: File data starts in sector %" PRId64 "\n", diskStructure->dataFirstSector); printf("cd9660_makefs: Total sectors: %" PRId64 "\n", diskStructure->totalSectors); } /* * Add padding sectors at the end * TODO: Clean this up and separate padding */ if (diskStructure->include_padding_areas) diskStructure->totalSectors += 150; cd9660_write_image(diskStructure, image); if (diskStructure->verbose_level > 1) { debug_print_volume_descriptor_information(diskStructure); debug_print_tree(diskStructure, real_root, 0); debug_print_path_tree(real_root); } /* Clean up data structures */ cd9660_free_structure(real_root); if (diskStructure->verbose_level > 0) printf("cd9660_makefs: done\n"); } /* Generic function pointer - implement later */ typedef int (*cd9660node_func)(cd9660node *); static void cd9660_finalize_PVD(iso9660_disk *diskStructure) { time_t tstamp = stampst.st_ino ? stampst.st_mtime : time(NULL); /* root should be a fixed size of 34 bytes since it has no name */ memcpy(diskStructure->primaryDescriptor.root_directory_record, diskStructure->rootNode->dot_record->isoDirRecord, 34); /* In RRIP, this might be longer than 34 */ diskStructure->primaryDescriptor.root_directory_record[0] = 34; /* Set up all the important numbers in the PVD */ cd9660_bothendian_dword(diskStructure->totalSectors, (unsigned char *)diskStructure->primaryDescriptor.volume_space_size); cd9660_bothendian_word(1, (unsigned char *)diskStructure->primaryDescriptor.volume_set_size); cd9660_bothendian_word(1, (unsigned char *) diskStructure->primaryDescriptor.volume_sequence_number); cd9660_bothendian_word(diskStructure->sectorSize, (unsigned char *) diskStructure->primaryDescriptor.logical_block_size); cd9660_bothendian_dword(diskStructure->pathTableLength, (unsigned char *)diskStructure->primaryDescriptor.path_table_size); cd9660_731(diskStructure->primaryLittleEndianTableSector, (u_char *)diskStructure->primaryDescriptor.type_l_path_table); cd9660_732(diskStructure->primaryBigEndianTableSector, (u_char *)diskStructure->primaryDescriptor.type_m_path_table); diskStructure->primaryDescriptor.file_structure_version[0] = 1; /* Pad all strings with spaces instead of nulls */ cd9660_pad_string_spaces(diskStructure->primaryDescriptor.volume_id, 32); cd9660_pad_string_spaces(diskStructure->primaryDescriptor.system_id, 32); cd9660_pad_string_spaces(diskStructure->primaryDescriptor.volume_set_id, 128); cd9660_pad_string_spaces(diskStructure->primaryDescriptor.publisher_id, 128); cd9660_pad_string_spaces(diskStructure->primaryDescriptor.preparer_id, 128); cd9660_pad_string_spaces(diskStructure->primaryDescriptor.application_id, 128); cd9660_pad_string_spaces( diskStructure->primaryDescriptor.copyright_file_id, 37); cd9660_pad_string_spaces( diskStructure->primaryDescriptor.abstract_file_id, 37); cd9660_pad_string_spaces( diskStructure->primaryDescriptor.bibliographic_file_id, 37); /* Setup dates */ cd9660_time_8426( (unsigned char *)diskStructure->primaryDescriptor.creation_date, tstamp); cd9660_time_8426( (unsigned char *)diskStructure->primaryDescriptor.modification_date, tstamp); #if 0 cd9660_set_date(diskStructure->primaryDescriptor.expiration_date, tstamp); #endif memset(diskStructure->primaryDescriptor.expiration_date, '0', 16); diskStructure->primaryDescriptor.expiration_date[16] = 0; cd9660_time_8426( (unsigned char *)diskStructure->primaryDescriptor.effective_date, tstamp); /* make this sane */ cd9660_time_915(diskStructure->rootNode->dot_record->isoDirRecord->date, tstamp); } static void cd9660_populate_iso_dir_record(struct _iso_directory_record_cd9660 *record, u_char ext_attr_length, u_char flags, u_char name_len, const char * name) { record->ext_attr_length[0] = ext_attr_length; record->flags[0] = ISO_FLAG_CLEAR | flags; record->file_unit_size[0] = 0; record->interleave[0] = 0; cd9660_bothendian_word(1, record->volume_sequence_number); record->name_len[0] = name_len; memset(record->name, '\0', sizeof (record->name)); memcpy(record->name, name, name_len); record->length[0] = 33 + name_len; /* Todo : better rounding */ record->length[0] += (record->length[0] & 1) ? 1 : 0; } static void cd9660_setup_root_node(iso9660_disk *diskStructure) { cd9660_populate_iso_dir_record(diskStructure->rootNode->isoDirRecord, 0, ISO_FLAG_DIRECTORY, 1, "\0"); } /*********** SUPPORT FUNCTIONS ***********/ static int cd9660_setup_volume_descriptors(iso9660_disk *diskStructure) { /* Boot volume descriptor should come second */ int sector = 16; /* For now, a fixed 2 : PVD and terminator */ volume_descriptor *temp, *t; /* Set up the PVD */ - if ((temp = malloc(sizeof(volume_descriptor))) == NULL) { - CD9660_MEM_ALLOC_ERROR("cd9660_setup_volume_descriptors"); - exit(1); - } - + temp = emalloc(sizeof(*temp)); temp->volumeDescriptorData = (unsigned char *)&diskStructure->primaryDescriptor; temp->volumeDescriptorData[0] = ISO_VOLUME_DESCRIPTOR_PVD; temp->volumeDescriptorData[6] = 1; temp->sector = sector; memcpy(temp->volumeDescriptorData + 1, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5); diskStructure->firstVolumeDescriptor = temp; sector++; /* Set up boot support if enabled. BVD must reside in sector 17 */ if (diskStructure->is_bootable) { - if ((t = malloc(sizeof(volume_descriptor))) == NULL) { - CD9660_MEM_ALLOC_ERROR( - "cd9660_setup_volume_descriptors"); - exit(1); - } - if ((t->volumeDescriptorData = malloc(2048)) == NULL) { - CD9660_MEM_ALLOC_ERROR( - "cd9660_setup_volume_descriptors"); - exit(1); - } + t = emalloc(sizeof(*t)); + t->volumeDescriptorData = ecalloc(1, 2048); temp->next = t; temp = t; - memset(t->volumeDescriptorData, 0, 2048); t->sector = 17; if (diskStructure->verbose_level > 0) printf("Setting up boot volume descriptor\n"); cd9660_setup_boot_volume_descriptor(diskStructure, t); sector++; } /* Set up the terminator */ - if ((t = malloc(sizeof(volume_descriptor))) == NULL) { - CD9660_MEM_ALLOC_ERROR("cd9660_setup_volume_descriptors"); - exit(1); - } - if ((t->volumeDescriptorData = malloc(2048)) == NULL) { - CD9660_MEM_ALLOC_ERROR("cd9660_setup_volume_descriptors"); - exit(1); - } - + t = emalloc(sizeof(*t)); + t->volumeDescriptorData = ecalloc(1, 2048); temp->next = t; - memset(t->volumeDescriptorData, 0, 2048); t->volumeDescriptorData[0] = ISO_VOLUME_DESCRIPTOR_TERMINATOR; t->next = NULL; t->volumeDescriptorData[6] = 1; t->sector = sector; memcpy(t->volumeDescriptorData + 1, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5); sector++; return sector; } #if 0 /* * Populate EAR at some point. Not required, but is used by NetBSD's * cd9660 support */ static int cd9660_fill_extended_attribute_record(cd9660node *node) { - if ((node->isoExtAttributes = - malloc(sizeof(struct iso_extended_attributes))) == NULL) { - CD9660_MEM_ALLOC_ERROR("cd9660_fill_extended_attribute_record"); - exit(1); - }; - + node->isoExtAttributes = emalloc(sizeof(*node->isoExtAttributes)); return 1; } #endif static int cd9660_translate_node_common(iso9660_disk *diskStructure, cd9660node *newnode) { time_t tstamp = stampst.st_ino ? stampst.st_mtime : time(NULL); int test; u_char flag; char temp[ISO_FILENAME_MAXLENGTH_WITH_PADDING]; /* Now populate the isoDirRecord structure */ memset(temp, 0, ISO_FILENAME_MAXLENGTH_WITH_PADDING); test = cd9660_convert_filename(diskStructure, newnode->node->name, temp, !(S_ISDIR(newnode->node->type))); flag = ISO_FLAG_CLEAR; if (S_ISDIR(newnode->node->type)) flag |= ISO_FLAG_DIRECTORY; cd9660_populate_iso_dir_record(newnode->isoDirRecord, 0, flag, strlen(temp), temp); /* Set the various dates */ /* If we want to use the current date and time */ cd9660_time_915(newnode->isoDirRecord->date, tstamp); cd9660_bothendian_dword(newnode->fileDataLength, newnode->isoDirRecord->size); /* If the file is a link, we want to set the size to 0 */ if (S_ISLNK(newnode->node->type)) newnode->fileDataLength = 0; return 1; } /* * Translate fsnode to cd9660node * Translate filenames and other metadata, including dates, sizes, * permissions, etc * @param struct fsnode * The node generated by makefs * @param struct cd9660node * The intermediate node to be written to * @returns int 0 on failure, 1 on success */ static int cd9660_translate_node(iso9660_disk *diskStructure, fsnode *node, cd9660node *newnode) { if (node == NULL) { if (diskStructure->verbose_level > 0) printf("cd9660_translate_node: NULL node passed, " "returning\n"); return 0; } - if ((newnode->isoDirRecord = - malloc(sizeof(iso_directory_record_cd9660))) == NULL) { - CD9660_MEM_ALLOC_ERROR("cd9660_translate_node"); - return 0; - } - + newnode->isoDirRecord = emalloc(sizeof(*newnode->isoDirRecord)); /* Set the node pointer */ newnode->node = node; /* Set the size */ if (!(S_ISDIR(node->type))) newnode->fileDataLength = node->inode->st.st_size; if (cd9660_translate_node_common(diskStructure, newnode) == 0) return 0; /* Finally, overwrite some of the values that are set by default */ cd9660_time_915(newnode->isoDirRecord->date, stampst.st_ino ? stampst.st_mtime : node->inode->st.st_mtime); return 1; } /* * Compares two ISO filenames * @param const char * The first file name * @param const char * The second file name * @returns : -1 if first is less than second, 0 if they are the same, 1 if * the second is greater than the first */ static int cd9660_compare_filename(const char *first, const char *second) { /* * This can be made more optimal once it has been tested * (the extra character, for example, is for testing) */ int p1 = 0; int p2 = 0; char c1, c2; /* First, on the filename */ while (p1 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION-1 && p2 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION-1) { c1 = first[p1]; c2 = second[p2]; if (c1 == '.' && c2 =='.') break; else if (c1 == '.') { p2++; c1 = ' '; } else if (c2 == '.') { p1++; c2 = ' '; } else { p1++; p2++; } if (c1 < c2) return -1; else if (c1 > c2) { return 1; } } if (first[p1] == '.' && second[p2] == '.') { p1++; p2++; while (p1 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION - 1 && p2 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION - 1) { c1 = first[p1]; c2 = second[p2]; if (c1 == ';' && c2 == ';') break; else if (c1 == ';') { p2++; c1 = ' '; } else if (c2 == ';') { p1++; c2 = ' '; } else { p1++; p2++; } if (c1 < c2) return -1; else if (c1 > c2) return 1; } } return 0; } /* * Insert a node into list with ISO sorting rules * @param cd9660node * The head node of the list * @param cd9660node * The node to be inserted */ static void cd9660_sorted_child_insert(cd9660node *parent, cd9660node *cn_new) { int compare; cd9660node *cn; struct cd9660_children_head *head = &parent->cn_children; /* TODO: Optimize? */ cn_new->parent = parent; /* * first will either be 0, the . or the .. * if . or .., this means no other entry may be written before first * if 0, the new node may be inserted at the head */ TAILQ_FOREACH(cn, head, cn_next_child) { /* * Dont insert a node twice - * that would cause an infinite loop */ if (cn_new == cn) return; compare = cd9660_compare_filename(cn_new->isoDirRecord->name, cn->isoDirRecord->name); if (compare == 0) compare = cd9660_compare_filename(cn_new->node->name, cn->node->name); if (compare < 0) break; } if (cn == NULL) TAILQ_INSERT_TAIL(head, cn_new, cn_next_child); else TAILQ_INSERT_BEFORE(cn, cn_new, cn_next_child); } /* * Called After cd9660_sorted_child_insert * handles file collisions by suffixing each filname with ~n * where n represents the files respective place in the ordering */ static int cd9660_handle_collisions(iso9660_disk *diskStructure, cd9660node *colliding, int past) { cd9660node *iter, *next, *prev; int skip; int delete_chars = 0; int temp_past = past; int temp_skip; int flag = 0; cd9660node *end_of_range; for (iter = TAILQ_FIRST(&colliding->cn_children); iter != NULL && (next = TAILQ_NEXT(iter, cn_next_child)) != NULL;) { if (strcmp(iter->isoDirRecord->name, next->isoDirRecord->name) != 0) { iter = TAILQ_NEXT(iter, cn_next_child); continue; } flag = 1; temp_skip = skip = cd9660_count_collisions(iter); end_of_range = iter; while (temp_skip > 0) { temp_skip--; end_of_range = TAILQ_NEXT(end_of_range, cn_next_child); } temp_past = past; while (temp_past > 0) { if ((next = TAILQ_NEXT(end_of_range, cn_next_child)) != NULL) end_of_range = next; else if ((prev = TAILQ_PREV(iter, cd9660_children_head, cn_next_child)) != NULL) iter = prev; else delete_chars++; temp_past--; } skip += past; iter = cd9660_rename_filename(diskStructure, iter, skip, delete_chars); } return flag; } static cd9660node * cd9660_rename_filename(iso9660_disk *diskStructure, cd9660node *iter, int num, int delete_chars) { int i = 0; int numbts, digit, digits, temp, powers, count; char *naming; int maxlength; char *tmp; if (diskStructure->verbose_level > 0) printf("Rename_filename called\n"); assert(1 <= diskStructure->isoLevel && diskStructure->isoLevel <= 2); /* TODO : A LOT of chanes regarding 8.3 filenames */ if (diskStructure->isoLevel == 1) maxlength = 8; else if (diskStructure->isoLevel == 2) maxlength = 31; else maxlength = ISO_FILENAME_MAXLENGTH_BEFORE_VERSION; - tmp = malloc(ISO_FILENAME_MAXLENGTH_WITH_PADDING); + tmp = emalloc(ISO_FILENAME_MAXLENGTH_WITH_PADDING); while (i < num && iter) { powers = 1; count = 0; digits = 1; while (((int)(i / powers) ) >= 10) { digits++; powers = powers * 10; } naming = iter->o_name; /* while ((*naming != '.') && (*naming != ';')) { naming++; count++; } */ while (count < maxlength) { if (*naming == ';') break; naming++; count++; } if ((count + digits) < maxlength) numbts = count; else numbts = maxlength - (digits); numbts -= delete_chars; /* 8.3 rules - keep the extension, add before the dot */ /* * This code makes a bunch of assumptions. * See if you can spot them all :) */ /* if (diskStructure->isoLevel == 1) { numbts = 8 - digits - delete_chars; if (dot < 0) { } else { if (dot < 8) { memmove(&tmp[numbts],&tmp[dot],4); } } } */ /* (copying just the filename before the '.' */ memcpy(tmp, (iter->o_name), numbts); /* adding the appropriate number following the name */ temp = i; while (digits > 0) { digit = (int)(temp / powers); temp = temp - digit * powers; sprintf(&tmp[numbts] , "%d", digit); digits--; numbts++; powers = powers / 10; } while ((*naming != ';') && (numbts < maxlength)) { tmp[numbts] = (*naming); naming++; numbts++; } tmp[numbts] = ';'; tmp[numbts+1] = '1'; tmp[numbts+2] = '\0'; /* * now tmp has exactly the identifier * we want so we'll copy it back to record */ memcpy((iter->isoDirRecord->name), tmp, numbts + 3); iter = TAILQ_NEXT(iter, cn_next_child); i++; } free(tmp); return iter; } /* Todo: Figure out why these functions are nec. */ static void cd9660_copy_filenames(iso9660_disk *diskStructure, cd9660node *node) { cd9660node *cn; if (TAILQ_EMPTY(&node->cn_children)) return; if (TAILQ_FIRST(&node->cn_children)->isoDirRecord == NULL) { debug_print_tree(diskStructure, diskStructure->rootNode, 0); exit(1); } TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) { cd9660_copy_filenames(diskStructure, cn); memcpy(cn->o_name, cn->isoDirRecord->name, ISO_FILENAME_MAXLENGTH_WITH_PADDING); } } static void cd9660_sorting_nodes(cd9660node *node) { cd9660node *cn; TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) cd9660_sorting_nodes(cn); cd9660_sort_nodes(node); } /* XXX Bubble sort. */ static void cd9660_sort_nodes(cd9660node *node) { cd9660node *cn, *next; do { TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) { if ((next = TAILQ_NEXT(cn, cn_next_child)) == NULL) return; else if (strcmp(next->isoDirRecord->name, cn->isoDirRecord->name) >= 0) continue; TAILQ_REMOVE(&node->cn_children, next, cn_next_child); TAILQ_INSERT_BEFORE(cn, next, cn_next_child); break; } } while (cn != NULL); } static int cd9660_count_collisions(cd9660node *copy) { int count = 0; cd9660node *iter, *next; for (iter = copy; (next = TAILQ_NEXT(iter, cn_next_child)) != NULL; iter = next) { if (cd9660_compare_filename(iter->isoDirRecord->name, next->isoDirRecord->name) == 0) count++; else return count; } #if 0 if ((next = TAILQ_NEXT(iter, cn_next_child)) != NULL) { printf("cd9660_recurse_on_collision: count is %i \n", count); compare = cd9660_compare_filename(iter->isoDirRecord->name, next->isoDirRecord->name); if (compare == 0) { count++; return cd9660_recurse_on_collision(next, count); } else return count; } #endif return count; } static cd9660node * cd9660_rrip_move_directory(iso9660_disk *diskStructure, cd9660node *dir) { char newname[9]; cd9660node *tfile; /* * This function needs to: * 1) Create an empty virtual file in place of the old directory * 2) Point the virtual file to the new directory * 3) Point the relocated directory to its old parent * 4) Move the directory specified by dir into rr_moved_dir, * and rename it to "diskStructure->rock_ridge_move_count" (as a string) */ /* First see if the moved directory even exists */ if (diskStructure->rr_moved_dir == NULL) { diskStructure->rr_moved_dir = cd9660_create_directory( diskStructure, ISO_RRIP_DEFAULT_MOVE_DIR_NAME, diskStructure->rootNode, dir); if (diskStructure->rr_moved_dir == NULL) return 0; cd9660_time_915(diskStructure->rr_moved_dir->isoDirRecord->date, stampst.st_ino ? stampst.st_mtime : start_time.tv_sec); } /* Create a file with the same ORIGINAL name */ tfile = cd9660_create_file(diskStructure, dir->node->name, dir->parent, dir); if (tfile == NULL) return NULL; diskStructure->rock_ridge_move_count++; snprintf(newname, sizeof(newname), "%08i", diskStructure->rock_ridge_move_count); /* Point to old parent */ dir->rr_real_parent = dir->parent; /* Place the placeholder file */ if (TAILQ_EMPTY(&dir->rr_real_parent->cn_children)) { TAILQ_INSERT_HEAD(&dir->rr_real_parent->cn_children, tfile, cn_next_child); } else { cd9660_sorted_child_insert(dir->rr_real_parent, tfile); } /* Point to new parent */ dir->parent = diskStructure->rr_moved_dir; /* Point the file to the moved directory */ tfile->rr_relocated = dir; /* Actually move the directory */ cd9660_sorted_child_insert(diskStructure->rr_moved_dir, dir); /* TODO: Inherit permissions / ownership (basically the entire inode) */ /* Set the new name */ memset(dir->isoDirRecord->name, 0, ISO_FILENAME_MAXLENGTH_WITH_PADDING); strncpy(dir->isoDirRecord->name, newname, 8); dir->isoDirRecord->length[0] = 34 + 8; dir->isoDirRecord->name_len[0] = 8; return dir; } static int cd9660_add_dot_records(iso9660_disk *diskStructure, cd9660node *root) { struct cd9660_children_head *head = &root->cn_children; cd9660node *cn; TAILQ_FOREACH(cn, head, cn_next_child) { if ((cn->type & CD9660_TYPE_DIR) == 0) continue; /* Recursion first */ cd9660_add_dot_records(diskStructure, cn); } cd9660_create_special_directory(diskStructure, CD9660_TYPE_DOT, root); cd9660_create_special_directory(diskStructure, CD9660_TYPE_DOTDOT, root); return 1; } /* * Convert node to cd9660 structure * This function is designed to be called recursively on the root node of * the filesystem * Lots of recursion going on here, want to make sure it is efficient * @param struct fsnode * The root node to be converted * @param struct cd9660* The parent node (should not be NULL) * @param int Current directory depth * @param int* Running count of the number of directories that are being created */ static void cd9660_convert_structure(iso9660_disk *diskStructure, fsnode *root, cd9660node *parent_node, int level, int *numDirectories, int *error) { fsnode *iterator = root; cd9660node *this_node; int working_level; int add; int flag = 0; int counter = 0; /* * Newer, more efficient method, reduces recursion depth */ if (root == NULL) { warnx("%s: root is null\n", __func__); return; } /* Test for an empty directory - makefs still gives us the . record */ if ((S_ISDIR(root->type)) && (root->name[0] == '.') && (root->name[1] == '\0')) { root = root->next; if (root == NULL) return; } if ((this_node = cd9660_allocate_cd9660node()) == NULL) { CD9660_MEM_ALLOC_ERROR(__func__); } /* * To reduce the number of recursive calls, we will iterate over * the next pointers to the right. */ while (iterator != NULL) { add = 1; /* * Increment the directory count if this is a directory * Ignore "." entries. We will generate them later */ if (!S_ISDIR(iterator->type) || strcmp(iterator->name, ".") != 0) { /* Translate the node, including its filename */ this_node->parent = parent_node; cd9660_translate_node(diskStructure, iterator, this_node); this_node->level = level; if (S_ISDIR(iterator->type)) { (*numDirectories)++; this_node->type = CD9660_TYPE_DIR; working_level = level + 1; /* * If at level 8, directory would be at 8 * and have children at 9 which is not * allowed as per ISO spec */ if (level == 8) { if ((!diskStructure->allow_deep_trees) && (!diskStructure->rock_ridge_enabled)) { warnx("error: found entry " "with depth greater " "than 8."); (*error) = 1; return; } else if (diskStructure-> rock_ridge_enabled) { working_level = 3; /* * Moved directory is actually * at level 2. */ this_node->level = working_level - 1; if (cd9660_rrip_move_directory( diskStructure, this_node) == NULL) { warnx("Failure in " "cd9660_rrip_" "move_directory" ); (*error) = 1; return; } add = 0; } } /* Do the recursive call on the children */ if (iterator->child != NULL) { cd9660_convert_structure(diskStructure, iterator->child, this_node, working_level, numDirectories, error); if ((*error) == 1) { warnx("%s: Error on recursive " "call", __func__); return; } } } else { /* Only directories should have children */ assert(iterator->child == NULL); this_node->type = CD9660_TYPE_FILE; } /* * Finally, do a sorted insert */ if (add) { cd9660_sorted_child_insert( parent_node, this_node); } /*Allocate new temp_node */ if (iterator->next != NULL) { this_node = cd9660_allocate_cd9660node(); if (this_node == NULL) CD9660_MEM_ALLOC_ERROR(__func__); } } iterator = iterator->next; } /* cd9660_handle_collisions(first_node); */ /* TODO: need cleanup */ cd9660_copy_filenames(diskStructure, parent_node); do { flag = cd9660_handle_collisions(diskStructure, parent_node, counter); counter++; cd9660_sorting_nodes(parent_node); } while ((flag == 1) && (counter < 100)); } /* * Clean up the cd9660node tree * This is designed to be called recursively on the root node * @param struct cd9660node *root The node to free * @returns void */ static void cd9660_free_structure(cd9660node *root) { cd9660node *cn; while ((cn = TAILQ_FIRST(&root->cn_children)) != NULL) { TAILQ_REMOVE(&root->cn_children, cn, cn_next_child); cd9660_free_structure(cn); } free(root); } /* * Be a little more memory conservative: * instead of having the TAILQ_ENTRY as part of the cd9660node, * just create a temporary structure */ struct ptq_entry { TAILQ_ENTRY(ptq_entry) ptq; cd9660node *node; } *n; #define PTQUEUE_NEW(n,s,r,t){\ - n = malloc(sizeof(struct s)); \ - if (n == NULL) \ - return r; \ + n = emalloc(sizeof(struct s)); \ n->node = t;\ } /* * Generate the path tables * The specific implementation of this function is left as an exercise to the * programmer. It could be done recursively. Make sure you read how the path * table has to be laid out, it has levels. * @param struct iso9660_disk *disk The disk image * @returns int The number of built path tables (between 1 and 4), 0 on failure */ static int cd9660_generate_path_table(iso9660_disk *diskStructure) { cd9660node *cn, *dirNode = diskStructure->rootNode; cd9660node *last = dirNode; int pathTableSize = 0; /* computed as we go */ int counter = 1; /* root gets a count of 0 */ TAILQ_HEAD(cd9660_pt_head, ptq_entry) pt_head; TAILQ_INIT(&pt_head); PTQUEUE_NEW(n, ptq_entry, -1, diskStructure->rootNode); /* Push the root node */ TAILQ_INSERT_HEAD(&pt_head, n, ptq); /* Breadth-first traversal of file structure */ while (pt_head.tqh_first != 0) { n = pt_head.tqh_first; dirNode = n->node; TAILQ_REMOVE(&pt_head, pt_head.tqh_first, ptq); free(n); /* Update the size */ pathTableSize += ISO_PATHTABLE_ENTRY_BASESIZE + dirNode->isoDirRecord->name_len[0]+ (dirNode->isoDirRecord->name_len[0] % 2 == 0 ? 0 : 1); /* includes the padding bit */ dirNode->ptnumber=counter; if (dirNode != last) { last->ptnext = dirNode; dirNode->ptprev = last; } last = dirNode; /* Push children onto queue */ TAILQ_FOREACH(cn, &dirNode->cn_children, cn_next_child) { /* * Dont add the DOT and DOTDOT types to the path * table. */ if ((cn->type != CD9660_TYPE_DOT) && (cn->type != CD9660_TYPE_DOTDOT)) { if (S_ISDIR(cn->node->type)) { PTQUEUE_NEW(n, ptq_entry, -1, cn); TAILQ_INSERT_TAIL(&pt_head, n, ptq); } } } counter++; } return pathTableSize; } void cd9660_compute_full_filename(cd9660node *node, char *buf) { int len; len = CD9660MAXPATH + 1; len = snprintf(buf, len, "%s/%s/%s", node->node->root, node->node->path, node->node->name); if (len > CD9660MAXPATH) errx(1, "Pathname too long."); } /* NEW filename conversion method */ typedef int(*cd9660_filename_conversion_functor)(iso9660_disk *, const char *, char *, int); /* * TODO: These two functions are almost identical. * Some code cleanup is possible here * * XXX bounds checking! */ static int cd9660_level1_convert_filename(iso9660_disk *diskStructure, const char *oldname, char *newname, int is_file) { /* * ISO 9660 : 10.1 * File Name shall not contain more than 8 d or d1 characters * File Name Extension shall not contain more than 3 d or d1 characters * Directory Identifier shall not contain more than 8 d or d1 characters */ int namelen = 0; int extlen = 0; int found_ext = 0; while (*oldname != '\0' && extlen < 3) { /* Handle period first, as it is special */ if (*oldname == '.') { if (found_ext) { *newname++ = '_'; extlen ++; } else { *newname++ = '.'; found_ext = 1; } } else { /* cut RISC OS file type off ISO name */ if (diskStructure->archimedes_enabled && *oldname == ',' && strlen(oldname) == 4) break; /* Enforce 12.3 / 8 */ if (namelen == 8 && !found_ext) break; if (islower((unsigned char)*oldname)) *newname++ = toupper((unsigned char)*oldname); else if (isupper((unsigned char)*oldname) || isdigit((unsigned char)*oldname)) *newname++ = *oldname; else *newname++ = '_'; if (found_ext) extlen++; else namelen++; } oldname++; } if (is_file) { if (!found_ext && !diskStructure->omit_trailing_period) *newname++ = '.'; /* Add version */ sprintf(newname, ";%i", 1); } return namelen + extlen + found_ext; } /* XXX bounds checking! */ static int cd9660_level2_convert_filename(iso9660_disk *diskStructure, const char *oldname, char *newname, int is_file) { /* * ISO 9660 : 7.5.1 * File name : 0+ d or d1 characters * separator 1 (.) * File name extension : 0+ d or d1 characters * separator 2 (;) * File version number (5 characters, 1-32767) * 1 <= Sum of File name and File name extension <= 30 */ int namelen = 0; int extlen = 0; int found_ext = 0; while (*oldname != '\0' && namelen + extlen < 30) { /* Handle period first, as it is special */ if (*oldname == '.') { if (found_ext) { if (diskStructure->allow_multidot) { *newname++ = '.'; } else { *newname++ = '_'; } extlen ++; } else { *newname++ = '.'; found_ext = 1; } } else { /* cut RISC OS file type off ISO name */ if (diskStructure->archimedes_enabled && *oldname == ',' && strlen(oldname) == 4) break; if (islower((unsigned char)*oldname)) *newname++ = toupper((unsigned char)*oldname); else if (isupper((unsigned char)*oldname) || isdigit((unsigned char)*oldname)) *newname++ = *oldname; else if (diskStructure->allow_multidot && *oldname == '.') { *newname++ = '.'; } else { *newname++ = '_'; } if (found_ext) extlen++; else namelen++; } oldname ++; } if (is_file) { if (!found_ext && !diskStructure->omit_trailing_period) *newname++ = '.'; /* Add version */ sprintf(newname, ";%i", 1); } return namelen + extlen + found_ext; } #if 0 static int cd9660_joliet_convert_filename(iso9660_disk *diskStructure, const char *oldname, char *newname, int is_file) { /* TODO: implement later, move to cd9660_joliet.c ?? */ } #endif /* * Convert a file name to ISO compliant file name * @param char * oldname The original filename * @param char ** newname The new file name, in the appropriate character * set and of appropriate length * @param int 1 if file, 0 if directory * @returns int The length of the new string */ static int cd9660_convert_filename(iso9660_disk *diskStructure, const char *oldname, char *newname, int is_file) { assert(1 <= diskStructure->isoLevel && diskStructure->isoLevel <= 2); /* NEW */ cd9660_filename_conversion_functor conversion_function = NULL; if (diskStructure->isoLevel == 1) conversion_function = &cd9660_level1_convert_filename; else if (diskStructure->isoLevel == 2) conversion_function = &cd9660_level2_convert_filename; return (*conversion_function)(diskStructure, oldname, newname, is_file); } int cd9660_compute_record_size(iso9660_disk *diskStructure, cd9660node *node) { int size = node->isoDirRecord->length[0]; if (diskStructure->rock_ridge_enabled) size += node->susp_entry_size; size += node->su_tail_size; size += size & 1; /* Ensure length of record is even. */ assert(size <= 254); return size; } static void cd9660_populate_dot_records(iso9660_disk *diskStructure, cd9660node *node) { node->dot_record->fileDataSector = node->fileDataSector; memcpy(node->dot_record->isoDirRecord,node->isoDirRecord, 34); node->dot_record->isoDirRecord->name_len[0] = 1; node->dot_record->isoDirRecord->name[0] = 0; node->dot_record->isoDirRecord->name[1] = 0; node->dot_record->isoDirRecord->length[0] = 34; node->dot_record->fileRecordSize = cd9660_compute_record_size(diskStructure, node->dot_record); if (node == diskStructure->rootNode) { node->dot_dot_record->fileDataSector = node->fileDataSector; memcpy(node->dot_dot_record->isoDirRecord,node->isoDirRecord, 34); } else { node->dot_dot_record->fileDataSector = node->parent->fileDataSector; memcpy(node->dot_dot_record->isoDirRecord, node->parent->isoDirRecord,34); } node->dot_dot_record->isoDirRecord->name_len[0] = 1; node->dot_dot_record->isoDirRecord->name[0] = 1; node->dot_dot_record->isoDirRecord->name[1] = 0; node->dot_dot_record->isoDirRecord->length[0] = 34; node->dot_dot_record->fileRecordSize = cd9660_compute_record_size(diskStructure, node->dot_dot_record); } /* * @param struct cd9660node *node The node * @param int The offset (in bytes) - SHOULD align to the beginning of a sector * @returns int The total size of files and directory entries (should be * a multiple of sector size) */ static int64_t cd9660_compute_offsets(iso9660_disk *diskStructure, cd9660node *node, int64_t startOffset) { /* * This function needs to compute the size of directory records and * runs, file lengths, and set the appropriate variables both in * cd9660node and isoDirEntry */ int64_t used_bytes = 0; int64_t current_sector_usage = 0; cd9660node *child; fsinode *inode; int64_t r; assert(node != NULL); /* * NOTE : There needs to be some special case detection for * the "real root" node, since for it, node->node is undefined */ node->fileDataSector = -1; if (node->type & CD9660_TYPE_DIR) { node->fileRecordSize = cd9660_compute_record_size( diskStructure, node); /*Set what sector this directory starts in*/ node->fileDataSector = CD9660_BLOCKS(diskStructure->sectorSize,startOffset); cd9660_bothendian_dword(node->fileDataSector, node->isoDirRecord->extent); /* * First loop over children, need to know the size of * their directory records */ node->fileSectorsUsed = 1; TAILQ_FOREACH(child, &node->cn_children, cn_next_child) { node->fileDataLength += cd9660_compute_record_size(diskStructure, child); if ((cd9660_compute_record_size(diskStructure, child) + current_sector_usage) >= diskStructure->sectorSize) { current_sector_usage = 0; node->fileSectorsUsed++; } current_sector_usage += cd9660_compute_record_size(diskStructure, child); } cd9660_bothendian_dword(node->fileSectorsUsed * diskStructure->sectorSize,node->isoDirRecord->size); /* * This should point to the sector after the directory * record (or, the first byte in that sector) */ used_bytes += node->fileSectorsUsed * diskStructure->sectorSize; for (child = TAILQ_NEXT(node->dot_dot_record, cn_next_child); child != NULL; child = TAILQ_NEXT(child, cn_next_child)) { /* Directories need recursive call */ if (S_ISDIR(child->node->type)) { r = cd9660_compute_offsets(diskStructure, child, used_bytes + startOffset); if (r != -1) used_bytes += r; else return -1; } } /* Explicitly set the . and .. records */ cd9660_populate_dot_records(diskStructure, node); /* Finally, do another iteration to write the file data*/ for (child = TAILQ_NEXT(node->dot_dot_record, cn_next_child); child != NULL; child = TAILQ_NEXT(child, cn_next_child)) { /* Files need extent set */ if (S_ISDIR(child->node->type)) continue; child->fileRecordSize = cd9660_compute_record_size(diskStructure, child); child->fileSectorsUsed = CD9660_BLOCKS(diskStructure->sectorSize, child->fileDataLength); inode = child->node->inode; if ((inode->flags & FI_ALLOCATED) == 0) { inode->ino = CD9660_BLOCKS(diskStructure->sectorSize, used_bytes + startOffset); inode->flags |= FI_ALLOCATED; used_bytes += child->fileSectorsUsed * diskStructure->sectorSize; } else { INODE_WARNX(("%s: already allocated inode %d " "data sectors at %" PRIu32, __func__, (int)inode->st.st_ino, inode->ino)); } child->fileDataSector = inode->ino; cd9660_bothendian_dword(child->fileDataSector, child->isoDirRecord->extent); } } return used_bytes; } #if 0 /* Might get rid of this func */ static int cd9660_copy_stat_info(cd9660node *from, cd9660node *to, int file) { to->node->inode->st.st_dev = 0; to->node->inode->st.st_ino = 0; to->node->inode->st.st_size = 0; to->node->inode->st.st_blksize = from->node->inode->st.st_blksize; to->node->inode->st.st_atime = from->node->inode->st.st_atime; to->node->inode->st.st_mtime = from->node->inode->st.st_mtime; to->node->inode->st.st_ctime = from->node->inode->st.st_ctime; to->node->inode->st.st_uid = from->node->inode->st.st_uid; to->node->inode->st.st_gid = from->node->inode->st.st_gid; to->node->inode->st.st_mode = from->node->inode->st.st_mode; /* Clear out type */ to->node->inode->st.st_mode = to->node->inode->st.st_mode & ~(S_IFMT); if (file) to->node->inode->st.st_mode |= S_IFREG; else to->node->inode->st.st_mode |= S_IFDIR; return 1; } #endif static cd9660node * cd9660_create_virtual_entry(iso9660_disk *diskStructure, const char *name, cd9660node *parent, int file, int insert) { cd9660node *temp; fsnode * tfsnode; assert(parent != NULL); temp = cd9660_allocate_cd9660node(); if (temp == NULL) return NULL; - if ((tfsnode = malloc(sizeof(fsnode))) == NULL) { - CD9660_MEM_ALLOC_ERROR("cd9660_create_virtual_entry"); - return NULL; - } + tfsnode = emalloc(sizeof(*tfsnode)); + tfsnode->name = estrdup(name); + temp->isoDirRecord = emalloc(sizeof(*temp->isoDirRecord)); - /* Assume for now name is a valid length */ - if ((tfsnode->name = malloc(strlen(name) + 1)) == NULL) { - CD9660_MEM_ALLOC_ERROR("cd9660_create_virtual_entry"); - return NULL; - } - - if ((temp->isoDirRecord = - malloc(sizeof(iso_directory_record_cd9660))) == NULL) { - CD9660_MEM_ALLOC_ERROR("cd9660_create_virtual_entry"); - return NULL; - } - - strcpy(tfsnode->name, name); - cd9660_convert_filename(diskStructure, tfsnode->name, temp->isoDirRecord->name, file); temp->node = tfsnode; temp->parent = parent; if (insert) { if (temp->parent != NULL) { temp->level = temp->parent->level + 1; if (!TAILQ_EMPTY(&temp->parent->cn_children)) cd9660_sorted_child_insert(temp->parent, temp); else TAILQ_INSERT_HEAD(&temp->parent->cn_children, temp, cn_next_child); } } if (parent->node != NULL) { tfsnode->type = parent->node->type; } /* Clear out file type bits */ tfsnode->type &= ~(S_IFMT); if (file) tfsnode->type |= S_IFREG; else tfsnode->type |= S_IFDIR; /* Indicate that there is no spec entry (inode) */ tfsnode->flags &= ~(FSNODE_F_HASSPEC); #if 0 cd9660_copy_stat_info(parent, temp, file); #endif return temp; } static cd9660node * cd9660_create_file(iso9660_disk *diskStructure, const char *name, cd9660node *parent, cd9660node *me) { cd9660node *temp; temp = cd9660_create_virtual_entry(diskStructure, name, parent, 1, 1); if (temp == NULL) return NULL; temp->fileDataLength = 0; temp->type = CD9660_TYPE_FILE | CD9660_TYPE_VIRTUAL; - if ((temp->node->inode = calloc(1, sizeof(fsinode))) == NULL) - return NULL; + temp->node->inode = ecalloc(1, sizeof(*temp->node->inode)); *temp->node->inode = *me->node->inode; if (cd9660_translate_node_common(diskStructure, temp) == 0) return NULL; return temp; } /* * Create a new directory which does not exist on disk * @param const char * name The name to assign to the directory * @param const char * parent Pointer to the parent directory * @returns cd9660node * Pointer to the new directory */ static cd9660node * cd9660_create_directory(iso9660_disk *diskStructure, const char *name, cd9660node *parent, cd9660node *me) { cd9660node *temp; temp = cd9660_create_virtual_entry(diskStructure, name, parent, 0, 1); if (temp == NULL) return NULL; temp->node->type |= S_IFDIR; temp->type = CD9660_TYPE_DIR | CD9660_TYPE_VIRTUAL; - if ((temp->node->inode = calloc(1, sizeof(fsinode))) == NULL) - return NULL; + temp->node->inode = ecalloc(1, sizeof(*temp->node->inode)); *temp->node->inode = *me->node->inode; if (cd9660_translate_node_common(diskStructure, temp) == 0) return NULL; return temp; } static cd9660node * cd9660_create_special_directory(iso9660_disk *diskStructure, u_char type, cd9660node *parent) { cd9660node *temp, *first; char na[2]; assert(parent != NULL); if (type == CD9660_TYPE_DOT) na[0] = 0; else if (type == CD9660_TYPE_DOTDOT) na[0] = 1; else return 0; na[1] = 0; if ((temp = cd9660_create_virtual_entry(diskStructure, na, parent, 0, 0)) == NULL) return NULL; temp->parent = parent; temp->type = type; temp->isoDirRecord->length[0] = 34; /* Dot record is always first */ if (type == CD9660_TYPE_DOT) { parent->dot_record = temp; TAILQ_INSERT_HEAD(&parent->cn_children, temp, cn_next_child); /* DotDot should be second */ } else if (type == CD9660_TYPE_DOTDOT) { parent->dot_dot_record = temp; /* * If the first child is the dot record, insert * this second. Otherwise, insert it at the head. */ if ((first = TAILQ_FIRST(&parent->cn_children)) == NULL || (first->type & CD9660_TYPE_DOT) == 0) { TAILQ_INSERT_HEAD(&parent->cn_children, temp, cn_next_child); } else { TAILQ_INSERT_AFTER(&parent->cn_children, first, temp, cn_next_child); } } return temp; } int cd9660_add_generic_bootimage(iso9660_disk *diskStructure, const char *bootimage) { struct stat stbuf; assert(bootimage != NULL); if (*bootimage == '\0') { warnx("Error: Boot image must be a filename"); return 0; } - if ((diskStructure->generic_bootimage = strdup(bootimage)) == NULL) { - warn("%s: strdup", __func__); - return 0; - } + diskStructure->generic_bootimage = estrdup(bootimage); /* Get information about the file */ if (lstat(diskStructure->generic_bootimage, &stbuf) == -1) err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__, diskStructure->generic_bootimage); if (stbuf.st_size > 32768) { warnx("Error: Boot image must be no greater than 32768 bytes"); return 0; } if (diskStructure->verbose_level > 0) { printf("Generic boot image image has size %lld\n", (long long)stbuf.st_size); } diskStructure->has_generic_bootimage = 1; return 1; } Index: stable/11/usr.sbin/makefs/ffs/buf.c =================================================================== --- stable/11/usr.sbin/makefs/ffs/buf.c (revision 332980) +++ stable/11/usr.sbin/makefs/ffs/buf.c (revision 332981) @@ -1,230 +1,227 @@ /* $NetBSD: buf.c,v 1.13 2004/06/20 22:20:18 jmc Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause * * Copyright (c) 2001 Wasabi Systems, Inc. * All rights reserved. * * Written by Luke Mewburn for Wasabi Systems, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed for the NetBSD Project by * Wasabi Systems, Inc. * 4. The name of Wasabi Systems, Inc. may not be used to endorse * or promote products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include +#include #include "makefs.h" #include #include #include "ffs/buf.h" #include "ffs/ufs_inode.h" extern int sectorsize; /* XXX: from ffs.c & mkfs.c */ TAILQ_HEAD(buftailhead,buf) buftail; int bread(struct vnode *vp, daddr_t blkno, int size, struct ucred *u1 __unused, struct buf **bpp) { off_t offset; ssize_t rv; struct fs *fs = vp->fs; assert (fs != NULL); assert (bpp != NULL); if (debug & DEBUG_BUF_BREAD) printf("bread: fs %p blkno %lld size %d\n", fs, (long long)blkno, size); *bpp = getblk(vp, blkno, size, 0, 0, 0); offset = (*bpp)->b_blkno * sectorsize; /* XXX */ if (debug & DEBUG_BUF_BREAD) printf("bread: bp %p blkno %lld offset %lld bcount %ld\n", (*bpp), (long long)(*bpp)->b_blkno, (long long) offset, (*bpp)->b_bcount); if (lseek((*bpp)->b_fd, offset, SEEK_SET) == -1) err(1, "bread: lseek %lld (%lld)", (long long)(*bpp)->b_blkno, (long long)offset); rv = read((*bpp)->b_fd, (*bpp)->b_data, (*bpp)->b_bcount); if (debug & DEBUG_BUF_BREAD) printf("bread: read %ld (%lld) returned %d\n", (*bpp)->b_bcount, (long long)offset, (int)rv); if (rv == -1) /* read error */ err(1, "bread: read %ld (%lld) returned %d", (*bpp)->b_bcount, (long long)offset, (int)rv); else if (rv != (*bpp)->b_bcount) /* short read */ err(1, "bread: read %ld (%lld) returned %d", (*bpp)->b_bcount, (long long)offset, (int)rv); else return (0); } void brelse(struct buf *bp, int u1 __unused) { assert (bp != NULL); assert (bp->b_data != NULL); if (bp->b_lblkno < 0) { /* * XXX don't remove any buffers with negative logical block * numbers (lblkno), so that we retain the mapping * of negative lblkno -> real blkno that ffs_balloc() * sets up. * * if we instead released these buffers, and implemented * ufs_strategy() (and ufs_bmaparray()) and called those * from bread() and bwrite() to convert the lblkno to * a real blkno, we'd add a lot more code & complexity * and reading off disk, for little gain, because this * simple hack works for our purpose. */ bp->b_bcount = 0; return; } TAILQ_REMOVE(&buftail, bp, b_tailq); free(bp->b_data); free(bp); } int bwrite(struct buf *bp) { off_t offset; ssize_t rv; assert (bp != NULL); offset = bp->b_blkno * sectorsize; /* XXX */ if (debug & DEBUG_BUF_BWRITE) printf("bwrite: bp %p blkno %lld offset %lld bcount %ld\n", bp, (long long)bp->b_blkno, (long long) offset, bp->b_bcount); if (lseek(bp->b_fd, offset, SEEK_SET) == -1) return (errno); rv = write(bp->b_fd, bp->b_data, bp->b_bcount); if (debug & DEBUG_BUF_BWRITE) printf("bwrite: write %ld (offset %lld) returned %lld\n", bp->b_bcount, (long long)offset, (long long)rv); if (rv == bp->b_bcount) return (0); else if (rv == -1) /* write error */ return (errno); else /* short write ? */ return (EAGAIN); } void bcleanup(void) { struct buf *bp; /* * XXX this really shouldn't be necessary, but i'm curious to * know why there's still some buffers lying around that * aren't brelse()d */ if (TAILQ_EMPTY(&buftail)) return; printf("bcleanup: unflushed buffers:\n"); TAILQ_FOREACH(bp, &buftail, b_tailq) { printf("\tlblkno %10lld blkno %10lld count %6ld bufsize %6ld\n", (long long)bp->b_lblkno, (long long)bp->b_blkno, bp->b_bcount, bp->b_bufsize); } printf("bcleanup: done\n"); } struct buf * getblk(struct vnode *vp, daddr_t blkno, int size, int u1 __unused, int u2 __unused, int u3 __unused) { static int buftailinitted; struct buf *bp; void *n; int fd = vp->fd; struct fs *fs = vp->fs; blkno += vp->offset; assert (fs != NULL); if (debug & DEBUG_BUF_GETBLK) printf("getblk: fs %p blkno %lld size %d\n", fs, (long long)blkno, size); bp = NULL; if (!buftailinitted) { if (debug & DEBUG_BUF_GETBLK) printf("getblk: initialising tailq\n"); TAILQ_INIT(&buftail); buftailinitted = 1; } else { TAILQ_FOREACH(bp, &buftail, b_tailq) { if (bp->b_lblkno != blkno) continue; break; } } if (bp == NULL) { - if ((bp = calloc(1, sizeof(struct buf))) == NULL) - err(1, "getblk: calloc"); - + bp = ecalloc(1, sizeof(*bp)); bp->b_bufsize = 0; bp->b_blkno = bp->b_lblkno = blkno; bp->b_fd = fd; bp->b_fs = fs; bp->b_data = NULL; TAILQ_INSERT_HEAD(&buftail, bp, b_tailq); } bp->b_bcount = size; if (bp->b_data == NULL || bp->b_bcount > bp->b_bufsize) { - n = realloc(bp->b_data, size); - if (n == NULL) - err(1, "getblk: realloc b_data %ld", bp->b_bcount); + n = erealloc(bp->b_data, size); bp->b_data = n; bp->b_bufsize = size; } return (bp); } Index: stable/11/usr.sbin/makefs/ffs/mkfs.c =================================================================== --- stable/11/usr.sbin/makefs/ffs/mkfs.c (revision 332980) +++ stable/11/usr.sbin/makefs/ffs/mkfs.c (revision 332981) @@ -1,838 +1,833 @@ /* $NetBSD: mkfs.c,v 1.22 2011/10/09 22:30:13 christos Exp $ */ /* * Copyright (c) 2002 Networks Associates Technology, Inc. * All rights reserved. * * This software was developed for the FreeBSD Project by Marshall * Kirk McKusick and Network Associates Laboratories, the Security * Research Division of Network Associates, Inc. under DARPA/SPAWAR * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS * research program * * Copyright (c) 1980, 1989, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include +#include #include "makefs.h" #include "ffs.h" #include #include #include "ffs/ufs_bswap.h" #include "ffs/ufs_inode.h" #include "ffs/ffs_extern.h" #include "ffs/newfs_extern.h" #ifndef BBSIZE #define BBSIZE 8192 /* size of boot area, with label */ #endif static void initcg(int, time_t, const fsinfo_t *); static int ilog2(int); static int count_digits(int); /* * make file system for cylinder-group style file systems */ #define UMASK 0755 #define POWEROF2(num) (((num) & ((num) - 1)) == 0) union { struct fs fs; char pad[SBLOCKSIZE]; } fsun; #define sblock fsun.fs struct csum *fscs; union { struct cg cg; char pad[FFS_MAXBSIZE]; } cgun; #define acg cgun.cg char *iobuf; int iobufsize; char writebuf[FFS_MAXBSIZE]; static int Oflag; /* format as an 4.3BSD file system */ static int64_t fssize; /* file system size */ static int sectorsize; /* bytes/sector */ static int fsize; /* fragment size */ static int bsize; /* block size */ static int maxbsize; /* maximum clustering */ static int maxblkspercg; static int minfree; /* free space threshold */ static int opt; /* optimization preference (space or time) */ static int density; /* number of bytes per inode */ static int maxcontig; /* max contiguous blocks to allocate */ static int maxbpg; /* maximum blocks per file in a cyl group */ static int bbsize; /* boot block size */ static int sbsize; /* superblock size */ static int avgfilesize; /* expected average file size */ static int avgfpdir; /* expected number of files per directory */ struct fs * ffs_mkfs(const char *fsys, const fsinfo_t *fsopts, time_t tstamp) { int fragsperinode, optimalfpg, origdensity, minfpg, lastminfpg; int32_t cylno, i, csfrags; long long sizepb; void *space; int size, blks; int nprintcols, printcolwidth; ffs_opt_t *ffs_opts = fsopts->fs_specific; Oflag = ffs_opts->version; fssize = fsopts->size / fsopts->sectorsize; sectorsize = fsopts->sectorsize; fsize = ffs_opts->fsize; bsize = ffs_opts->bsize; maxbsize = ffs_opts->maxbsize; maxblkspercg = ffs_opts->maxblkspercg; minfree = ffs_opts->minfree; opt = ffs_opts->optimization; density = ffs_opts->density; maxcontig = ffs_opts->maxcontig; maxbpg = ffs_opts->maxbpg; avgfilesize = ffs_opts->avgfilesize; avgfpdir = ffs_opts->avgfpdir; bbsize = BBSIZE; sbsize = SBLOCKSIZE; strlcpy(sblock.fs_volname, ffs_opts->label, sizeof(sblock.fs_volname)); if (Oflag == 0) { sblock.fs_old_inodefmt = FS_42INODEFMT; sblock.fs_maxsymlinklen = 0; sblock.fs_old_flags = 0; } else { sblock.fs_old_inodefmt = FS_44INODEFMT; sblock.fs_maxsymlinklen = (Oflag == 1 ? MAXSYMLINKLEN_UFS1 : MAXSYMLINKLEN_UFS2); sblock.fs_old_flags = FS_FLAGS_UPDATED; sblock.fs_flags = 0; } /* * Validate the given file system size. * Verify that its last block can actually be accessed. * Convert to file system fragment sized units. */ if (fssize <= 0) { printf("preposterous size %lld\n", (long long)fssize); exit(13); } ffs_wtfs(fssize - 1, sectorsize, (char *)&sblock, fsopts); /* * collect and verify the filesystem density info */ sblock.fs_avgfilesize = avgfilesize; sblock.fs_avgfpdir = avgfpdir; if (sblock.fs_avgfilesize <= 0) printf("illegal expected average file size %d\n", sblock.fs_avgfilesize), exit(14); if (sblock.fs_avgfpdir <= 0) printf("illegal expected number of files per directory %d\n", sblock.fs_avgfpdir), exit(15); /* * collect and verify the block and fragment sizes */ sblock.fs_bsize = bsize; sblock.fs_fsize = fsize; if (!POWEROF2(sblock.fs_bsize)) { printf("block size must be a power of 2, not %d\n", sblock.fs_bsize); exit(16); } if (!POWEROF2(sblock.fs_fsize)) { printf("fragment size must be a power of 2, not %d\n", sblock.fs_fsize); exit(17); } if (sblock.fs_fsize < sectorsize) { printf("fragment size %d is too small, minimum is %d\n", sblock.fs_fsize, sectorsize); exit(18); } if (sblock.fs_bsize < MINBSIZE) { printf("block size %d is too small, minimum is %d\n", sblock.fs_bsize, MINBSIZE); exit(19); } if (sblock.fs_bsize > FFS_MAXBSIZE) { printf("block size %d is too large, maximum is %d\n", sblock.fs_bsize, FFS_MAXBSIZE); exit(19); } if (sblock.fs_bsize < sblock.fs_fsize) { printf("block size (%d) cannot be smaller than fragment size (%d)\n", sblock.fs_bsize, sblock.fs_fsize); exit(20); } if (maxbsize < bsize || !POWEROF2(maxbsize)) { sblock.fs_maxbsize = sblock.fs_bsize; printf("Extent size set to %d\n", sblock.fs_maxbsize); } else if (sblock.fs_maxbsize > FS_MAXCONTIG * sblock.fs_bsize) { sblock.fs_maxbsize = FS_MAXCONTIG * sblock.fs_bsize; printf("Extent size reduced to %d\n", sblock.fs_maxbsize); } else { sblock.fs_maxbsize = maxbsize; } sblock.fs_maxcontig = maxcontig; if (sblock.fs_maxcontig < sblock.fs_maxbsize / sblock.fs_bsize) { sblock.fs_maxcontig = sblock.fs_maxbsize / sblock.fs_bsize; printf("Maxcontig raised to %d\n", sblock.fs_maxbsize); } if (sblock.fs_maxcontig > 1) sblock.fs_contigsumsize = MIN(sblock.fs_maxcontig,FS_MAXCONTIG); sblock.fs_bmask = ~(sblock.fs_bsize - 1); sblock.fs_fmask = ~(sblock.fs_fsize - 1); sblock.fs_qbmask = ~sblock.fs_bmask; sblock.fs_qfmask = ~sblock.fs_fmask; for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1) sblock.fs_bshift++; for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1) sblock.fs_fshift++; sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize); for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1) sblock.fs_fragshift++; if (sblock.fs_frag > MAXFRAG) { printf("fragment size %d is too small, " "minimum with block size %d is %d\n", sblock.fs_fsize, sblock.fs_bsize, sblock.fs_bsize / MAXFRAG); exit(21); } sblock.fs_fsbtodb = ilog2(sblock.fs_fsize / sectorsize); sblock.fs_size = sblock.fs_providersize = fssize = dbtofsb(&sblock, fssize); if (Oflag <= 1) { sblock.fs_magic = FS_UFS1_MAGIC; sblock.fs_sblockloc = SBLOCK_UFS1; sblock.fs_nindir = sblock.fs_bsize / sizeof(ufs1_daddr_t); sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode); sblock.fs_maxsymlinklen = ((NDADDR + NIADDR) * sizeof (ufs1_daddr_t)); sblock.fs_old_inodefmt = FS_44INODEFMT; sblock.fs_old_cgoffset = 0; sblock.fs_old_cgmask = 0xffffffff; sblock.fs_old_size = sblock.fs_size; sblock.fs_old_rotdelay = 0; sblock.fs_old_rps = 60; sblock.fs_old_nspf = sblock.fs_fsize / sectorsize; sblock.fs_old_cpg = 1; sblock.fs_old_interleave = 1; sblock.fs_old_trackskew = 0; sblock.fs_old_cpc = 0; sblock.fs_old_postblformat = 1; sblock.fs_old_nrpos = 1; } else { sblock.fs_magic = FS_UFS2_MAGIC; sblock.fs_sblockloc = SBLOCK_UFS2; sblock.fs_nindir = sblock.fs_bsize / sizeof(ufs2_daddr_t); sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs2_dinode); sblock.fs_maxsymlinklen = ((NDADDR + NIADDR) * sizeof (ufs2_daddr_t)); } sblock.fs_sblkno = roundup(howmany(sblock.fs_sblockloc + SBLOCKSIZE, sblock.fs_fsize), sblock.fs_frag); sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno + roundup(howmany(SBLOCKSIZE, sblock.fs_fsize), sblock.fs_frag)); sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag; sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1; for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) { sizepb *= NINDIR(&sblock); sblock.fs_maxfilesize += sizepb; } /* * Calculate the number of blocks to put into each cylinder group. * * This algorithm selects the number of blocks per cylinder * group. The first goal is to have at least enough data blocks * in each cylinder group to meet the density requirement. Once * this goal is achieved we try to expand to have at least * 1 cylinder group. Once this goal is achieved, we pack as * many blocks into each cylinder group map as will fit. * * We start by calculating the smallest number of blocks that we * can put into each cylinder group. If this is too big, we reduce * the density until it fits. */ origdensity = density; for (;;) { fragsperinode = MAX(numfrags(&sblock, density), 1); minfpg = fragsperinode * INOPB(&sblock); if (minfpg > sblock.fs_size) minfpg = sblock.fs_size; sblock.fs_ipg = INOPB(&sblock); sblock.fs_fpg = roundup(sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag); if (sblock.fs_fpg < minfpg) sblock.fs_fpg = minfpg; sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), INOPB(&sblock)); sblock.fs_fpg = roundup(sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag); if (sblock.fs_fpg < minfpg) sblock.fs_fpg = minfpg; sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), INOPB(&sblock)); if (CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize) break; density -= sblock.fs_fsize; } if (density != origdensity) printf("density reduced from %d to %d\n", origdensity, density); if (maxblkspercg <= 0 || maxblkspercg >= fssize) maxblkspercg = fssize - 1; /* * Start packing more blocks into the cylinder group until * it cannot grow any larger, the number of cylinder groups * drops below 1, or we reach the size requested. */ for ( ; sblock.fs_fpg < maxblkspercg; sblock.fs_fpg += sblock.fs_frag) { sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), INOPB(&sblock)); if (sblock.fs_size / sblock.fs_fpg < 1) break; if (CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize) continue; if (CGSIZE(&sblock) == (unsigned long)sblock.fs_bsize) break; sblock.fs_fpg -= sblock.fs_frag; sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), INOPB(&sblock)); break; } /* * Check to be sure that the last cylinder group has enough blocks * to be viable. If it is too small, reduce the number of blocks * per cylinder group which will have the effect of moving more * blocks into the last cylinder group. */ optimalfpg = sblock.fs_fpg; for (;;) { sblock.fs_ncg = howmany(sblock.fs_size, sblock.fs_fpg); lastminfpg = roundup(sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag); if (sblock.fs_size < lastminfpg) { printf("Filesystem size %lld < minimum size of %d\n", (long long)sblock.fs_size, lastminfpg); exit(28); } if (sblock.fs_size % sblock.fs_fpg >= lastminfpg || sblock.fs_size % sblock.fs_fpg == 0) break; sblock.fs_fpg -= sblock.fs_frag; sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), INOPB(&sblock)); } if (optimalfpg != sblock.fs_fpg) printf("Reduced frags per cylinder group from %d to %d %s\n", optimalfpg, sblock.fs_fpg, "to enlarge last cyl group"); sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock)); sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock); if (Oflag <= 1) { sblock.fs_old_spc = sblock.fs_fpg * sblock.fs_old_nspf; sblock.fs_old_nsect = sblock.fs_old_spc; sblock.fs_old_npsect = sblock.fs_old_spc; sblock.fs_old_ncyl = sblock.fs_ncg; } /* * fill in remaining fields of the super block */ sblock.fs_csaddr = cgdmin(&sblock, 0); sblock.fs_cssize = fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum)); /* * Setup memory for temporary in-core cylgroup summaries. * Cribbed from ffs_mountfs(). */ size = sblock.fs_cssize; blks = howmany(size, sblock.fs_fsize); if (sblock.fs_contigsumsize > 0) size += sblock.fs_ncg * sizeof(int32_t); - if ((space = (char *)calloc(1, size)) == NULL) - err(1, "memory allocation error for cg summaries"); + space = ecalloc(1, size); sblock.fs_csp = space; space = (char *)space + sblock.fs_cssize; if (sblock.fs_contigsumsize > 0) { int32_t *lp; sblock.fs_maxcluster = lp = space; for (i = 0; i < sblock.fs_ncg; i++) *lp++ = sblock.fs_contigsumsize; } sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs)); if (sblock.fs_sbsize > SBLOCKSIZE) sblock.fs_sbsize = SBLOCKSIZE; sblock.fs_minfree = minfree; sblock.fs_maxcontig = maxcontig; sblock.fs_maxbpg = maxbpg; sblock.fs_optim = opt; sblock.fs_cgrotor = 0; sblock.fs_pendingblocks = 0; sblock.fs_pendinginodes = 0; sblock.fs_cstotal.cs_ndir = 0; sblock.fs_cstotal.cs_nbfree = 0; sblock.fs_cstotal.cs_nifree = 0; sblock.fs_cstotal.cs_nffree = 0; sblock.fs_fmod = 0; sblock.fs_ronly = 0; sblock.fs_state = 0; sblock.fs_clean = FS_ISCLEAN; sblock.fs_ronly = 0; sblock.fs_id[0] = tstamp; sblock.fs_id[1] = random(); sblock.fs_fsmnt[0] = '\0'; csfrags = howmany(sblock.fs_cssize, sblock.fs_fsize); sblock.fs_dsize = sblock.fs_size - sblock.fs_sblkno - sblock.fs_ncg * (sblock.fs_dblkno - sblock.fs_sblkno); sblock.fs_cstotal.cs_nbfree = fragstoblks(&sblock, sblock.fs_dsize) - howmany(csfrags, sblock.fs_frag); sblock.fs_cstotal.cs_nffree = fragnum(&sblock, sblock.fs_size) + (fragnum(&sblock, csfrags) > 0 ? sblock.fs_frag - fragnum(&sblock, csfrags) : 0); sblock.fs_cstotal.cs_nifree = sblock.fs_ncg * sblock.fs_ipg - ROOTINO; sblock.fs_cstotal.cs_ndir = 0; sblock.fs_dsize -= csfrags; sblock.fs_time = tstamp; if (Oflag <= 1) { sblock.fs_old_time = tstamp; sblock.fs_old_dsize = sblock.fs_dsize; sblock.fs_old_csaddr = sblock.fs_csaddr; sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir; sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree; sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree; sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree; } /* * Dump out summary information about file system. */ #define B2MBFACTOR (1 / (1024.0 * 1024.0)) printf("%s: %.1fMB (%lld sectors) block size %d, " "fragment size %d\n", fsys, (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR, (long long)fsbtodb(&sblock, sblock.fs_size), sblock.fs_bsize, sblock.fs_fsize); printf("\tusing %d cylinder groups of %.2fMB, %d blks, " "%d inodes.\n", sblock.fs_ncg, (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR, sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg); #undef B2MBFACTOR /* * Now determine how wide each column will be, and calculate how * many columns will fit in a 76 char line. 76 is the width of the * subwindows in sysinst. */ printcolwidth = count_digits( fsbtodb(&sblock, cgsblock(&sblock, sblock.fs_ncg -1))); nprintcols = 76 / (printcolwidth + 2); /* * allocate space for superblock, cylinder group map, and * two sets of inode blocks. */ if (sblock.fs_bsize < SBLOCKSIZE) iobufsize = SBLOCKSIZE + 3 * sblock.fs_bsize; else iobufsize = 4 * sblock.fs_bsize; - if ((iobuf = malloc(iobufsize)) == NULL) { - printf("Cannot allocate I/O buffer\n"); - exit(38); - } - memset(iobuf, 0, iobufsize); + iobuf = ecalloc(1, iobufsize); /* * Make a copy of the superblock into the buffer that we will be * writing out in each cylinder group. */ memcpy(writebuf, &sblock, sbsize); if (fsopts->needswap) ffs_sb_swap(&sblock, (struct fs*)writebuf); memcpy(iobuf, writebuf, SBLOCKSIZE); printf("super-block backups (for fsck -b #) at:"); for (cylno = 0; cylno < sblock.fs_ncg; cylno++) { initcg(cylno, tstamp, fsopts); if (cylno % nprintcols == 0) printf("\n"); printf(" %*lld,", printcolwidth, (long long)fsbtodb(&sblock, cgsblock(&sblock, cylno))); fflush(stdout); } printf("\n"); /* * Now construct the initial file system, * then write out the super-block. */ sblock.fs_time = tstamp; if (Oflag <= 1) { sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir; sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree; sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree; sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree; } if (fsopts->needswap) sblock.fs_flags |= FS_SWAPPED; ffs_write_superblock(&sblock, fsopts); return (&sblock); } /* * Write out the superblock and its duplicates, * and the cylinder group summaries */ void ffs_write_superblock(struct fs *fs, const fsinfo_t *fsopts) { int cylno, size, blks, i, saveflag; void *space; char *wrbuf; saveflag = fs->fs_flags & FS_INTERNAL; fs->fs_flags &= ~FS_INTERNAL; memcpy(writebuf, &sblock, sbsize); if (fsopts->needswap) ffs_sb_swap(fs, (struct fs*)writebuf); ffs_wtfs(fs->fs_sblockloc / sectorsize, sbsize, writebuf, fsopts); /* Write out the duplicate super blocks */ for (cylno = 0; cylno < fs->fs_ncg; cylno++) ffs_wtfs(fsbtodb(fs, cgsblock(fs, cylno)), sbsize, writebuf, fsopts); /* Write out the cylinder group summaries */ size = fs->fs_cssize; blks = howmany(size, fs->fs_fsize); space = (void *)fs->fs_csp; - if ((wrbuf = malloc(size)) == NULL) - err(1, "ffs_write_superblock: malloc %d", size); + wrbuf = emalloc(size); for (i = 0; i < blks; i+= fs->fs_frag) { size = fs->fs_bsize; if (i + fs->fs_frag > blks) size = (blks - i) * fs->fs_fsize; if (fsopts->needswap) ffs_csum_swap((struct csum *)space, (struct csum *)wrbuf, size); else memcpy(wrbuf, space, (u_int)size); ffs_wtfs(fsbtodb(fs, fs->fs_csaddr + i), size, wrbuf, fsopts); space = (char *)space + size; } free(wrbuf); fs->fs_flags |= saveflag; } /* * Initialize a cylinder group. */ static void initcg(int cylno, time_t utime, const fsinfo_t *fsopts) { daddr_t cbase, dmax; int32_t i, j, d, dlower, dupper, blkno; struct ufs1_dinode *dp1; struct ufs2_dinode *dp2; int start; /* * Determine block bounds for cylinder group. * Allow space for super block summary information in first * cylinder group. */ cbase = cgbase(&sblock, cylno); dmax = cbase + sblock.fs_fpg; if (dmax > sblock.fs_size) dmax = sblock.fs_size; dlower = cgsblock(&sblock, cylno) - cbase; dupper = cgdmin(&sblock, cylno) - cbase; if (cylno == 0) dupper += howmany(sblock.fs_cssize, sblock.fs_fsize); memset(&acg, 0, sblock.fs_cgsize); acg.cg_time = utime; acg.cg_magic = CG_MAGIC; acg.cg_cgx = cylno; acg.cg_niblk = sblock.fs_ipg; acg.cg_initediblk = MIN(sblock.fs_ipg, 2 * INOPB(&sblock)); acg.cg_ndblk = dmax - cbase; if (sblock.fs_contigsumsize > 0) acg.cg_nclusterblks = acg.cg_ndblk >> sblock.fs_fragshift; start = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield); if (Oflag == 2) { acg.cg_iusedoff = start; } else { if (cylno == sblock.fs_ncg - 1) acg.cg_old_ncyl = howmany(acg.cg_ndblk, sblock.fs_fpg / sblock.fs_old_cpg); else acg.cg_old_ncyl = sblock.fs_old_cpg; acg.cg_old_time = acg.cg_time; acg.cg_time = 0; acg.cg_old_niblk = acg.cg_niblk; acg.cg_niblk = 0; acg.cg_initediblk = 0; acg.cg_old_btotoff = start; acg.cg_old_boff = acg.cg_old_btotoff + sblock.fs_old_cpg * sizeof(int32_t); acg.cg_iusedoff = acg.cg_old_boff + sblock.fs_old_cpg * sizeof(u_int16_t); } acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT); if (sblock.fs_contigsumsize <= 0) { acg.cg_nextfreeoff = acg.cg_freeoff + howmany(sblock.fs_fpg, CHAR_BIT); } else { acg.cg_clustersumoff = acg.cg_freeoff + howmany(sblock.fs_fpg, CHAR_BIT) - sizeof(int32_t); acg.cg_clustersumoff = roundup(acg.cg_clustersumoff, sizeof(int32_t)); acg.cg_clusteroff = acg.cg_clustersumoff + (sblock.fs_contigsumsize + 1) * sizeof(int32_t); acg.cg_nextfreeoff = acg.cg_clusteroff + howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT); } if (acg.cg_nextfreeoff > sblock.fs_cgsize) { printf("Panic: cylinder group too big\n"); exit(37); } acg.cg_cs.cs_nifree += sblock.fs_ipg; if (cylno == 0) for (i = 0; i < ROOTINO; i++) { setbit(cg_inosused_swap(&acg, 0), i); acg.cg_cs.cs_nifree--; } if (cylno > 0) { /* * In cylno 0, beginning space is reserved * for boot and super blocks. */ for (d = 0, blkno = 0; d < dlower;) { ffs_setblock(&sblock, cg_blksfree_swap(&acg, 0), blkno); if (sblock.fs_contigsumsize > 0) setbit(cg_clustersfree_swap(&acg, 0), blkno); acg.cg_cs.cs_nbfree++; d += sblock.fs_frag; blkno++; } } if ((i = (dupper & (sblock.fs_frag - 1))) != 0) { acg.cg_frsum[sblock.fs_frag - i]++; for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) { setbit(cg_blksfree_swap(&acg, 0), dupper); acg.cg_cs.cs_nffree++; } } for (d = dupper, blkno = dupper >> sblock.fs_fragshift; d + sblock.fs_frag <= acg.cg_ndblk; ) { ffs_setblock(&sblock, cg_blksfree_swap(&acg, 0), blkno); if (sblock.fs_contigsumsize > 0) setbit(cg_clustersfree_swap(&acg, 0), blkno); acg.cg_cs.cs_nbfree++; d += sblock.fs_frag; blkno++; } if (d < acg.cg_ndblk) { acg.cg_frsum[acg.cg_ndblk - d]++; for (; d < acg.cg_ndblk; d++) { setbit(cg_blksfree_swap(&acg, 0), d); acg.cg_cs.cs_nffree++; } } if (sblock.fs_contigsumsize > 0) { int32_t *sump = cg_clustersum_swap(&acg, 0); u_char *mapp = cg_clustersfree_swap(&acg, 0); int map = *mapp++; int bit = 1; int run = 0; for (i = 0; i < acg.cg_nclusterblks; i++) { if ((map & bit) != 0) { run++; } else if (run != 0) { if (run > sblock.fs_contigsumsize) run = sblock.fs_contigsumsize; sump[run]++; run = 0; } if ((i & (CHAR_BIT - 1)) != (CHAR_BIT - 1)) { bit <<= 1; } else { map = *mapp++; bit = 1; } } if (run != 0) { if (run > sblock.fs_contigsumsize) run = sblock.fs_contigsumsize; sump[run]++; } } sblock.fs_cs(&sblock, cylno) = acg.cg_cs; /* * Write out the duplicate super block, the cylinder group map * and two blocks worth of inodes in a single write. */ start = MAX(sblock.fs_bsize, SBLOCKSIZE); memcpy(&iobuf[start], &acg, sblock.fs_cgsize); if (fsopts->needswap) ffs_cg_swap(&acg, (struct cg*)&iobuf[start], &sblock); start += sblock.fs_bsize; dp1 = (struct ufs1_dinode *)(&iobuf[start]); dp2 = (struct ufs2_dinode *)(&iobuf[start]); for (i = 0; i < acg.cg_initediblk; i++) { if (sblock.fs_magic == FS_UFS1_MAGIC) { /* No need to swap, it'll stay random */ dp1->di_gen = random(); dp1++; } else { dp2->di_gen = random(); dp2++; } } ffs_wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)), iobufsize, iobuf, fsopts); /* * For the old file system, we have to initialize all the inodes. */ if (Oflag <= 1) { for (i = 2 * sblock.fs_frag; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag) { dp1 = (struct ufs1_dinode *)(&iobuf[start]); for (j = 0; j < INOPB(&sblock); j++) { dp1->di_gen = random(); dp1++; } ffs_wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i), sblock.fs_bsize, &iobuf[start], fsopts); } } } /* * read a block from the file system */ void ffs_rdfs(daddr_t bno, int size, void *bf, const fsinfo_t *fsopts) { int n; off_t offset; offset = bno; offset *= fsopts->sectorsize; if (lseek(fsopts->fd, offset, SEEK_SET) < 0) err(1, "ffs_rdfs: seek error for sector %lld: %s\n", (long long)bno, strerror(errno)); n = read(fsopts->fd, bf, size); if (n == -1) { abort(); err(1, "ffs_rdfs: read error bno %lld size %d", (long long)bno, size); } else if (n != size) errx(1, "ffs_rdfs: read error for sector %lld: %s\n", (long long)bno, strerror(errno)); } /* * write a block to the file system */ void ffs_wtfs(daddr_t bno, int size, void *bf, const fsinfo_t *fsopts) { int n; off_t offset; offset = bno; offset *= fsopts->sectorsize; if (lseek(fsopts->fd, offset, SEEK_SET) < 0) err(1, "wtfs: seek error for sector %lld: %s\n", (long long)bno, strerror(errno)); n = write(fsopts->fd, bf, size); if (n == -1) err(1, "wtfs: write error for sector %lld: %s\n", (long long)bno, strerror(errno)); else if (n != size) errx(1, "wtfs: write error for sector %lld: %s\n", (long long)bno, strerror(errno)); } /* Determine how many digits are needed to print a given integer */ static int count_digits(int num) { int ndig; for(ndig = 1; num > 9; num /=10, ndig++); return (ndig); } static int ilog2(int val) { u_int n; for (n = 0; n < sizeof(n) * CHAR_BIT; n++) if (1 << n == val) return (n); errx(1, "ilog2: %d is not a power of 2\n", val); } Index: stable/11/usr.sbin/makefs/ffs.c =================================================================== --- stable/11/usr.sbin/makefs/ffs.c (revision 332980) +++ stable/11/usr.sbin/makefs/ffs.c (revision 332981) @@ -1,1182 +1,1172 @@ /* $NetBSD: ffs.c,v 1.45 2011/10/09 22:49:26 christos Exp $ */ /* * Copyright (c) 2001 Wasabi Systems, Inc. * All rights reserved. * * Written by Luke Mewburn for Wasabi Systems, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed for the NetBSD Project by * Wasabi Systems, Inc. * 4. The name of Wasabi Systems, Inc. may not be used to endorse * or promote products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /* * Copyright (c) 1982, 1986, 1989, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95 */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include +#include #include "makefs.h" #include "ffs.h" #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS #include #endif #include #include #include #include "ffs/ufs_bswap.h" #include "ffs/ufs_inode.h" #include "ffs/newfs_extern.h" #include "ffs/ffs_extern.h" #undef DIP #define DIP(dp, field) \ ((ffs_opts->version == 1) ? \ (dp)->ffs1_din.di_##field : (dp)->ffs2_din.di_##field) /* * Various file system defaults (cribbed from newfs(8)). */ #define DFL_FRAGSIZE 4096 /* fragment size */ #define DFL_BLKSIZE 32768 /* block size */ #define DFL_SECSIZE 512 /* sector size */ #define DFL_CYLSPERGROUP 65536 /* cylinders per group */ #define DFL_FRAGSPERINODE 4 /* fragments per inode */ #define DFL_ROTDELAY 0 /* rotational delay */ #define DFL_NRPOS 1 /* rotational positions */ #define DFL_RPM 3600 /* rpm of disk */ #define DFL_NSECTORS 64 /* # of sectors */ #define DFL_NTRACKS 16 /* # of tracks */ typedef struct { u_char *buf; /* buf for directory */ doff_t size; /* full size of buf */ doff_t cur; /* offset of current entry */ } dirbuf_t; static int ffs_create_image(const char *, fsinfo_t *); static void ffs_dump_fsinfo(fsinfo_t *); static void ffs_dump_dirbuf(dirbuf_t *, const char *, int); static void ffs_make_dirbuf(dirbuf_t *, const char *, fsnode *, int); static int ffs_populate_dir(const char *, fsnode *, fsinfo_t *); static void ffs_size_dir(fsnode *, fsinfo_t *); static void ffs_validate(const char *, fsnode *, fsinfo_t *); static void ffs_write_file(union dinode *, uint32_t, void *, fsinfo_t *); static void ffs_write_inode(union dinode *, uint32_t, const fsinfo_t *); static void *ffs_build_dinode1(struct ufs1_dinode *, dirbuf_t *, fsnode *, fsnode *, fsinfo_t *); static void *ffs_build_dinode2(struct ufs2_dinode *, dirbuf_t *, fsnode *, fsnode *, fsinfo_t *); int sectorsize; /* XXX: for buf.c::getblk() */ /* publicly visible functions */ void ffs_prep_opts(fsinfo_t *fsopts) { - ffs_opt_t *ffs_opts; + ffs_opt_t *ffs_opts = ecalloc(1, sizeof(*ffs_opts)); - if ((ffs_opts = calloc(1, sizeof(ffs_opt_t))) == NULL) - err(1, "Allocating memory for ffs_options"); - const option_t ffs_options[] = { { 'b', "bsize", &ffs_opts->bsize, OPT_INT32, 1, INT_MAX, "block size" }, { 'f', "fsize", &ffs_opts->fsize, OPT_INT32, 1, INT_MAX, "fragment size" }, { 'd', "density", &ffs_opts->density, OPT_INT32, 1, INT_MAX, "bytes per inode" }, { 'm', "minfree", &ffs_opts->minfree, OPT_INT32, 0, 99, "minfree" }, { 'M', "maxbpg", &ffs_opts->maxbpg, OPT_INT32, 1, INT_MAX, "max blocks per file in a cg" }, { 'a', "avgfilesize", &ffs_opts->avgfilesize, OPT_INT32, 1, INT_MAX, "expected average file size" }, { 'n', "avgfpdir", &ffs_opts->avgfpdir, OPT_INT32, 1, INT_MAX, "expected # of files per directory" }, { 'x', "extent", &ffs_opts->maxbsize, OPT_INT32, 1, INT_MAX, "maximum # extent size" }, { 'g', "maxbpcg", &ffs_opts->maxblkspercg, OPT_INT32, 1, INT_MAX, "max # of blocks per group" }, { 'v', "version", &ffs_opts->version, OPT_INT32, 1, 2, "UFS version" }, { 'o', "optimization", NULL, OPT_STRBUF, 0, 0, "Optimization (time|space)" }, { 'l', "label", ffs_opts->label, OPT_STRARRAY, 1, sizeof(ffs_opts->label), "UFS label" }, { .name = NULL } }; ffs_opts->bsize= -1; ffs_opts->fsize= -1; ffs_opts->cpg= -1; ffs_opts->density= -1; ffs_opts->minfree= -1; ffs_opts->optimization= -1; ffs_opts->maxcontig= -1; ffs_opts->maxbpg= -1; ffs_opts->avgfilesize= -1; ffs_opts->avgfpdir= -1; ffs_opts->version = 1; fsopts->fs_specific = ffs_opts; fsopts->fs_options = copy_opts(ffs_options); } void ffs_cleanup_opts(fsinfo_t *fsopts) { free(fsopts->fs_specific); free(fsopts->fs_options); } int ffs_parse_opts(const char *option, fsinfo_t *fsopts) { ffs_opt_t *ffs_opts = fsopts->fs_specific; option_t *ffs_options = fsopts->fs_options; char buf[1024]; int rv; assert(option != NULL); assert(fsopts != NULL); assert(ffs_opts != NULL); if (debug & DEBUG_FS_PARSE_OPTS) printf("ffs_parse_opts: got `%s'\n", option); rv = set_option(ffs_options, option, buf, sizeof(buf)); if (rv == -1) return 0; if (ffs_options[rv].name == NULL) abort(); switch (ffs_options[rv].letter) { case 'o': if (strcmp(buf, "time") == 0) { ffs_opts->optimization = FS_OPTTIME; } else if (strcmp(buf, "space") == 0) { ffs_opts->optimization = FS_OPTSPACE; } else { warnx("Invalid optimization `%s'", buf); return 0; } break; default: break; } return 1; } void ffs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts) { struct fs *superblock; struct timeval start; assert(image != NULL); assert(dir != NULL); assert(root != NULL); assert(fsopts != NULL); if (debug & DEBUG_FS_MAKEFS) printf("ffs_makefs: image %s directory %s root %p\n", image, dir, root); /* validate tree and options */ TIMER_START(start); ffs_validate(dir, root, fsopts); TIMER_RESULTS(start, "ffs_validate"); printf("Calculated size of `%s': %lld bytes, %lld inodes\n", image, (long long)fsopts->size, (long long)fsopts->inodes); /* create image */ TIMER_START(start); if (ffs_create_image(image, fsopts) == -1) errx(1, "Image file `%s' not created.", image); TIMER_RESULTS(start, "ffs_create_image"); fsopts->curinode = ROOTINO; if (debug & DEBUG_FS_MAKEFS) putchar('\n'); /* populate image */ printf("Populating `%s'\n", image); TIMER_START(start); if (! ffs_populate_dir(dir, root, fsopts)) errx(1, "Image file `%s' not populated.", image); TIMER_RESULTS(start, "ffs_populate_dir"); /* ensure no outstanding buffers remain */ if (debug & DEBUG_FS_MAKEFS) bcleanup(); /* update various superblock parameters */ superblock = fsopts->superblock; superblock->fs_fmod = 0; superblock->fs_old_cstotal.cs_ndir = superblock->fs_cstotal.cs_ndir; superblock->fs_old_cstotal.cs_nbfree = superblock->fs_cstotal.cs_nbfree; superblock->fs_old_cstotal.cs_nifree = superblock->fs_cstotal.cs_nifree; superblock->fs_old_cstotal.cs_nffree = superblock->fs_cstotal.cs_nffree; /* write out superblock; image is now complete */ ffs_write_superblock(fsopts->superblock, fsopts); if (close(fsopts->fd) == -1) err(1, "Closing `%s'", image); fsopts->fd = -1; printf("Image `%s' complete\n", image); } /* end of public functions */ static void ffs_validate(const char *dir, fsnode *root, fsinfo_t *fsopts) { int32_t ncg = 1; #if notyet int32_t spc, nspf, ncyl, fssize; #endif ffs_opt_t *ffs_opts = fsopts->fs_specific; assert(dir != NULL); assert(root != NULL); assert(fsopts != NULL); assert(ffs_opts != NULL); if (debug & DEBUG_FS_VALIDATE) { printf("ffs_validate: before defaults set:\n"); ffs_dump_fsinfo(fsopts); } /* set FFS defaults */ if (fsopts->sectorsize == -1) fsopts->sectorsize = DFL_SECSIZE; if (ffs_opts->fsize == -1) ffs_opts->fsize = MAX(DFL_FRAGSIZE, fsopts->sectorsize); if (ffs_opts->bsize == -1) ffs_opts->bsize = MIN(DFL_BLKSIZE, 8 * ffs_opts->fsize); if (ffs_opts->cpg == -1) ffs_opts->cpg = DFL_CYLSPERGROUP; else ffs_opts->cpgflg = 1; /* fsopts->density is set below */ if (ffs_opts->nsectors == -1) ffs_opts->nsectors = DFL_NSECTORS; if (ffs_opts->minfree == -1) ffs_opts->minfree = MINFREE; if (ffs_opts->optimization == -1) ffs_opts->optimization = DEFAULTOPT; if (ffs_opts->maxcontig == -1) ffs_opts->maxcontig = MAX(1, MIN(MAXPHYS, FFS_MAXBSIZE) / ffs_opts->bsize); /* XXX ondisk32 */ if (ffs_opts->maxbpg == -1) ffs_opts->maxbpg = ffs_opts->bsize / sizeof(int32_t); if (ffs_opts->avgfilesize == -1) ffs_opts->avgfilesize = AVFILESIZ; if (ffs_opts->avgfpdir == -1) ffs_opts->avgfpdir = AFPDIR; if (fsopts->maxsize > 0 && roundup(fsopts->minsize, ffs_opts->bsize) > fsopts->maxsize) errx(1, "`%s' minsize of %lld rounded up to ffs bsize of %d " "exceeds maxsize %lld. Lower bsize, or round the minimum " "and maximum sizes to bsize.", dir, (long long)fsopts->minsize, ffs_opts->bsize, (long long)fsopts->maxsize); /* calculate size of tree */ ffs_size_dir(root, fsopts); fsopts->inodes += ROOTINO; /* include first two inodes */ if (debug & DEBUG_FS_VALIDATE) printf("ffs_validate: size of tree: %lld bytes, %lld inodes\n", (long long)fsopts->size, (long long)fsopts->inodes); /* add requested slop */ fsopts->size += fsopts->freeblocks; fsopts->inodes += fsopts->freefiles; if (fsopts->freefilepc > 0) fsopts->inodes = fsopts->inodes * (100 + fsopts->freefilepc) / 100; if (fsopts->freeblockpc > 0) fsopts->size = fsopts->size * (100 + fsopts->freeblockpc) / 100; /* add space needed for superblocks */ /* * The old SBOFF (SBLOCK_UFS1) is used here because makefs is * typically used for small filesystems where space matters. * XXX make this an option. */ fsopts->size += (SBLOCK_UFS1 + SBLOCKSIZE) * ncg; /* add space needed to store inodes, x3 for blockmaps, etc */ if (ffs_opts->version == 1) fsopts->size += ncg * DINODE1_SIZE * roundup(fsopts->inodes / ncg, ffs_opts->bsize / DINODE1_SIZE); else fsopts->size += ncg * DINODE2_SIZE * roundup(fsopts->inodes / ncg, ffs_opts->bsize / DINODE2_SIZE); /* add minfree */ if (ffs_opts->minfree > 0) fsopts->size = fsopts->size * (100 + ffs_opts->minfree) / 100; /* * XXX any other fs slop to add, such as csum's, bitmaps, etc ?? */ if (fsopts->size < fsopts->minsize) /* ensure meets minimum size */ fsopts->size = fsopts->minsize; /* round up to the next block */ fsopts->size = roundup(fsopts->size, ffs_opts->bsize); /* round up to requested block size, if any */ if (fsopts->roundup > 0) fsopts->size = roundup(fsopts->size, fsopts->roundup); /* calculate density if necessary */ if (ffs_opts->density == -1) ffs_opts->density = fsopts->size / fsopts->inodes + 1; if (debug & DEBUG_FS_VALIDATE) { printf("ffs_validate: after defaults set:\n"); ffs_dump_fsinfo(fsopts); printf("ffs_validate: dir %s; %lld bytes, %lld inodes\n", dir, (long long)fsopts->size, (long long)fsopts->inodes); } sectorsize = fsopts->sectorsize; /* XXX - see earlier */ /* now check calculated sizes vs requested sizes */ if (fsopts->maxsize > 0 && fsopts->size > fsopts->maxsize) { errx(1, "`%s' size of %lld is larger than the maxsize of %lld.", dir, (long long)fsopts->size, (long long)fsopts->maxsize); } } static void ffs_dump_fsinfo(fsinfo_t *f) { ffs_opt_t *fs = f->fs_specific; printf("fsopts at %p\n", f); printf("\tsize %lld, inodes %lld, curinode %u\n", (long long)f->size, (long long)f->inodes, f->curinode); printf("\tminsize %lld, maxsize %lld\n", (long long)f->minsize, (long long)f->maxsize); printf("\tfree files %lld, freefile %% %d\n", (long long)f->freefiles, f->freefilepc); printf("\tfree blocks %lld, freeblock %% %d\n", (long long)f->freeblocks, f->freeblockpc); printf("\tneedswap %d, sectorsize %d\n", f->needswap, f->sectorsize); printf("\tbsize %d, fsize %d, cpg %d, density %d\n", fs->bsize, fs->fsize, fs->cpg, fs->density); printf("\tnsectors %d, rpm %d, minfree %d\n", fs->nsectors, fs->rpm, fs->minfree); printf("\tmaxcontig %d, maxbpg %d\n", fs->maxcontig, fs->maxbpg); printf("\toptimization %s\n", fs->optimization == FS_OPTSPACE ? "space" : "time"); } static int ffs_create_image(const char *image, fsinfo_t *fsopts) { #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS struct statvfs sfs; #endif struct fs *fs; char *buf; int i, bufsize; off_t bufrem; time_t tstamp; assert (image != NULL); assert (fsopts != NULL); /* create image */ if ((fsopts->fd = open(image, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) { warn("Can't open `%s' for writing", image); return (-1); } /* zero image */ #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS if (fstatvfs(fsopts->fd, &sfs) == -1) { #endif bufsize = 8192; #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS warn("can't fstatvfs `%s', using default %d byte chunk", image, bufsize); } else bufsize = sfs.f_iosize; #endif bufrem = fsopts->size; if (fsopts->sparse) { if (ftruncate(fsopts->fd, bufrem) == -1) { warn("sparse option disabled.\n"); fsopts->sparse = 0; } } if (fsopts->sparse) { /* File truncated at bufrem. Remaining is 0 */ bufrem = 0; buf = NULL; } else { if (debug & DEBUG_FS_CREATE_IMAGE) printf("zero-ing image `%s', %lld sectors, " "using %d byte chunks\n", image, (long long)bufrem, bufsize); - if ((buf = calloc(1, bufsize)) == NULL) { - warn("Can't create buffer for sector"); - return (-1); - } + buf = ecalloc(1, bufsize); } while (bufrem > 0) { i = write(fsopts->fd, buf, MIN(bufsize, bufrem)); if (i == -1) { warn("zeroing image, %lld bytes to go", (long long)bufrem); free(buf); return (-1); } bufrem -= i; } if (buf) free(buf); /* make the file system */ if (debug & DEBUG_FS_CREATE_IMAGE) printf("calling mkfs(\"%s\", ...)\n", image); if (stampst.st_ino != 0) tstamp = stampst.st_ctime; else tstamp = start_time.tv_sec; srandom(tstamp); fs = ffs_mkfs(image, fsopts, tstamp); fsopts->superblock = (void *)fs; if (debug & DEBUG_FS_CREATE_IMAGE) { time_t t; t = (time_t)((struct fs *)fsopts->superblock)->fs_time; printf("mkfs returned %p; fs_time %s", fsopts->superblock, ctime(&t)); printf("fs totals: nbfree %lld, nffree %lld, nifree %lld, ndir %lld\n", (long long)fs->fs_cstotal.cs_nbfree, (long long)fs->fs_cstotal.cs_nffree, (long long)fs->fs_cstotal.cs_nifree, (long long)fs->fs_cstotal.cs_ndir); } if (fs->fs_cstotal.cs_nifree + ROOTINO < fsopts->inodes) { warnx( "Image file `%s' has %lld free inodes; %lld are required.", image, (long long)(fs->fs_cstotal.cs_nifree + ROOTINO), (long long)fsopts->inodes); return (-1); } return (fsopts->fd); } static void ffs_size_dir(fsnode *root, fsinfo_t *fsopts) { struct direct tmpdir; fsnode * node; int curdirsize, this; ffs_opt_t *ffs_opts = fsopts->fs_specific; /* node may be NULL (empty directory) */ assert(fsopts != NULL); assert(ffs_opts != NULL); if (debug & DEBUG_FS_SIZE_DIR) printf("ffs_size_dir: entry: bytes %lld inodes %lld\n", (long long)fsopts->size, (long long)fsopts->inodes); #define ADDDIRENT(e) do { \ tmpdir.d_namlen = strlen((e)); \ this = DIRSIZ_SWAP(0, &tmpdir, 0); \ if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT) \ printf("ADDDIRENT: was: %s (%d) this %d cur %d\n", \ e, tmpdir.d_namlen, this, curdirsize); \ if (this + curdirsize > roundup(curdirsize, DIRBLKSIZ)) \ curdirsize = roundup(curdirsize, DIRBLKSIZ); \ curdirsize += this; \ if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT) \ printf("ADDDIRENT: now: %s (%d) this %d cur %d\n", \ e, tmpdir.d_namlen, this, curdirsize); \ } while (0); /* * XXX this needs to take into account extra space consumed * by indirect blocks, etc. */ #define ADDSIZE(x) do { \ fsopts->size += roundup((x), ffs_opts->fsize); \ } while (0); curdirsize = 0; for (node = root; node != NULL; node = node->next) { ADDDIRENT(node->name); if (node == root) { /* we're at "." */ assert(strcmp(node->name, ".") == 0); ADDDIRENT(".."); } else if ((node->inode->flags & FI_SIZED) == 0) { /* don't count duplicate names */ node->inode->flags |= FI_SIZED; if (debug & DEBUG_FS_SIZE_DIR_NODE) printf("ffs_size_dir: `%s' size %lld\n", node->name, (long long)node->inode->st.st_size); fsopts->inodes++; if (node->type == S_IFREG) ADDSIZE(node->inode->st.st_size); if (node->type == S_IFLNK) { int slen; slen = strlen(node->symlink) + 1; if (slen >= (ffs_opts->version == 1 ? MAXSYMLINKLEN_UFS1 : MAXSYMLINKLEN_UFS2)) ADDSIZE(slen); } } if (node->type == S_IFDIR) ffs_size_dir(node->child, fsopts); } ADDSIZE(curdirsize); if (debug & DEBUG_FS_SIZE_DIR) printf("ffs_size_dir: exit: size %lld inodes %lld\n", (long long)fsopts->size, (long long)fsopts->inodes); } static void * ffs_build_dinode1(struct ufs1_dinode *dinp, dirbuf_t *dbufp, fsnode *cur, fsnode *root, fsinfo_t *fsopts) { int slen; void *membuf; struct stat *st = stampst.st_ino != 0 ? &stampst : &cur->inode->st; memset(dinp, 0, sizeof(*dinp)); dinp->di_mode = cur->inode->st.st_mode; dinp->di_nlink = cur->inode->nlink; dinp->di_size = cur->inode->st.st_size; #if HAVE_STRUCT_STAT_ST_FLAGS dinp->di_flags = cur->inode->st.st_flags; #endif #if HAVE_STRUCT_STAT_ST_GEN dinp->di_gen = cur->inode->st.st_gen; #endif dinp->di_uid = cur->inode->st.st_uid; dinp->di_gid = cur->inode->st.st_gid; dinp->di_atime = st->st_atime; dinp->di_mtime = st->st_mtime; dinp->di_ctime = st->st_ctime; #if HAVE_STRUCT_STAT_ST_MTIMENSEC dinp->di_atimensec = st->st_atimensec; dinp->di_mtimensec = st->st_mtimensec; dinp->di_ctimensec = st->st_ctimensec; #endif /* not set: di_db, di_ib, di_blocks, di_spare */ membuf = NULL; if (cur == root) { /* "."; write dirbuf */ membuf = dbufp->buf; dinp->di_size = dbufp->size; } else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) { dinp->di_size = 0; /* a device */ dinp->di_rdev = ufs_rw32(cur->inode->st.st_rdev, fsopts->needswap); } else if (S_ISLNK(cur->type)) { /* symlink */ slen = strlen(cur->symlink); if (slen < MAXSYMLINKLEN_UFS1) { /* short link */ memcpy(dinp->di_db, cur->symlink, slen); } else membuf = cur->symlink; dinp->di_size = slen; } return membuf; } static void * ffs_build_dinode2(struct ufs2_dinode *dinp, dirbuf_t *dbufp, fsnode *cur, fsnode *root, fsinfo_t *fsopts) { int slen; void *membuf; struct stat *st = stampst.st_ino != 0 ? &stampst : &cur->inode->st; memset(dinp, 0, sizeof(*dinp)); dinp->di_mode = cur->inode->st.st_mode; dinp->di_nlink = cur->inode->nlink; dinp->di_size = cur->inode->st.st_size; #if HAVE_STRUCT_STAT_ST_FLAGS dinp->di_flags = cur->inode->st.st_flags; #endif #if HAVE_STRUCT_STAT_ST_GEN dinp->di_gen = cur->inode->st.st_gen; #endif dinp->di_uid = cur->inode->st.st_uid; dinp->di_gid = cur->inode->st.st_gid; dinp->di_atime = st->st_atime; dinp->di_mtime = st->st_mtime; dinp->di_ctime = st->st_ctime; #if HAVE_STRUCT_STAT_ST_MTIMENSEC dinp->di_atimensec = st->st_atimensec; dinp->di_mtimensec = st->st_mtimensec; dinp->di_ctimensec = st->st_ctimensec; #endif #if HAVE_STRUCT_STAT_BIRTHTIME dinp->di_birthtime = st->st_birthtime; dinp->di_birthnsec = st->st_birthtimensec; #endif /* not set: di_db, di_ib, di_blocks, di_spare */ membuf = NULL; if (cur == root) { /* "."; write dirbuf */ membuf = dbufp->buf; dinp->di_size = dbufp->size; } else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) { dinp->di_size = 0; /* a device */ dinp->di_rdev = ufs_rw64(cur->inode->st.st_rdev, fsopts->needswap); } else if (S_ISLNK(cur->type)) { /* symlink */ slen = strlen(cur->symlink); if (slen < MAXSYMLINKLEN_UFS2) { /* short link */ memcpy(dinp->di_db, cur->symlink, slen); } else membuf = cur->symlink; dinp->di_size = slen; } return membuf; } static int ffs_populate_dir(const char *dir, fsnode *root, fsinfo_t *fsopts) { fsnode *cur; dirbuf_t dirbuf; union dinode din; void *membuf; char path[MAXPATHLEN + 1]; ffs_opt_t *ffs_opts = fsopts->fs_specific; assert(dir != NULL); assert(root != NULL); assert(fsopts != NULL); assert(ffs_opts != NULL); (void)memset(&dirbuf, 0, sizeof(dirbuf)); if (debug & DEBUG_FS_POPULATE) printf("ffs_populate_dir: PASS 1 dir %s node %p\n", dir, root); /* * pass 1: allocate inode numbers, build directory `file' */ for (cur = root; cur != NULL; cur = cur->next) { if ((cur->inode->flags & FI_ALLOCATED) == 0) { cur->inode->flags |= FI_ALLOCATED; if (cur == root && cur->parent != NULL) cur->inode->ino = cur->parent->inode->ino; else { cur->inode->ino = fsopts->curinode; fsopts->curinode++; } } ffs_make_dirbuf(&dirbuf, cur->name, cur, fsopts->needswap); if (cur == root) { /* we're at "."; add ".." */ ffs_make_dirbuf(&dirbuf, "..", cur->parent == NULL ? cur : cur->parent->first, fsopts->needswap); root->inode->nlink++; /* count my parent's link */ } else if (cur->child != NULL) root->inode->nlink++; /* count my child's link */ /* * XXX possibly write file and long symlinks here, * ensuring that blocks get written before inodes? * otoh, this isn't a real filesystem, so who * cares about ordering? :-) */ } if (debug & DEBUG_FS_POPULATE_DIRBUF) ffs_dump_dirbuf(&dirbuf, dir, fsopts->needswap); /* * pass 2: write out dirbuf, then non-directories at this level */ if (debug & DEBUG_FS_POPULATE) printf("ffs_populate_dir: PASS 2 dir %s\n", dir); for (cur = root; cur != NULL; cur = cur->next) { if (cur->inode->flags & FI_WRITTEN) continue; /* skip hard-linked entries */ cur->inode->flags |= FI_WRITTEN; if (cur->contents == NULL) { if (snprintf(path, sizeof(path), "%s/%s/%s", cur->root, cur->path, cur->name) >= (int)sizeof(path)) errx(1, "Pathname too long."); } if (cur->child != NULL) continue; /* child creates own inode */ /* build on-disk inode */ if (ffs_opts->version == 1) membuf = ffs_build_dinode1(&din.ffs1_din, &dirbuf, cur, root, fsopts); else membuf = ffs_build_dinode2(&din.ffs2_din, &dirbuf, cur, root, fsopts); if (debug & DEBUG_FS_POPULATE_NODE) { printf("ffs_populate_dir: writing ino %d, %s", cur->inode->ino, inode_type(cur->type)); if (cur->inode->nlink > 1) printf(", nlink %d", cur->inode->nlink); putchar('\n'); } if (membuf != NULL) { ffs_write_file(&din, cur->inode->ino, membuf, fsopts); } else if (S_ISREG(cur->type)) { ffs_write_file(&din, cur->inode->ino, (cur->contents) ? cur->contents : path, fsopts); } else { assert (! S_ISDIR(cur->type)); ffs_write_inode(&din, cur->inode->ino, fsopts); } } /* * pass 3: write out sub-directories */ if (debug & DEBUG_FS_POPULATE) printf("ffs_populate_dir: PASS 3 dir %s\n", dir); for (cur = root; cur != NULL; cur = cur->next) { if (cur->child == NULL) continue; if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name) >= sizeof(path)) errx(1, "Pathname too long."); if (! ffs_populate_dir(path, cur->child, fsopts)) return (0); } if (debug & DEBUG_FS_POPULATE) printf("ffs_populate_dir: DONE dir %s\n", dir); /* cleanup */ if (dirbuf.buf != NULL) free(dirbuf.buf); return (1); } static void ffs_write_file(union dinode *din, uint32_t ino, void *buf, fsinfo_t *fsopts) { int isfile, ffd; char *fbuf, *p; off_t bufleft, chunk, offset; ssize_t nread; struct inode in; struct buf * bp; ffs_opt_t *ffs_opts = fsopts->fs_specific; assert (din != NULL); assert (buf != NULL); assert (fsopts != NULL); assert (ffs_opts != NULL); isfile = S_ISREG(DIP(din, mode)); fbuf = NULL; ffd = -1; p = NULL; in.i_fs = (struct fs *)fsopts->superblock; if (debug & DEBUG_FS_WRITE_FILE) { printf( "ffs_write_file: ino %u, din %p, isfile %d, %s, size %lld", ino, din, isfile, inode_type(DIP(din, mode) & S_IFMT), (long long)DIP(din, size)); if (isfile) printf(", file '%s'\n", (char *)buf); else printf(", buffer %p\n", buf); } in.i_number = ino; in.i_size = DIP(din, size); if (ffs_opts->version == 1) memcpy(&in.i_din.ffs1_din, &din->ffs1_din, sizeof(in.i_din.ffs1_din)); else memcpy(&in.i_din.ffs2_din, &din->ffs2_din, sizeof(in.i_din.ffs2_din)); in.i_fd = fsopts->fd; if (DIP(din, size) == 0) goto write_inode_and_leave; /* mmm, cheating */ if (isfile) { - if ((fbuf = malloc(ffs_opts->bsize)) == NULL) - err(1, "Allocating memory for write buffer"); + fbuf = emalloc(ffs_opts->bsize); if ((ffd = open((char *)buf, O_RDONLY, 0444)) == -1) { warn("Can't open `%s' for reading", (char *)buf); goto leave_ffs_write_file; } } else { p = buf; } chunk = 0; for (bufleft = DIP(din, size); bufleft > 0; bufleft -= chunk) { chunk = MIN(bufleft, ffs_opts->bsize); if (!isfile) ; else if ((nread = read(ffd, fbuf, chunk)) == -1) err(EXIT_FAILURE, "Reading `%s', %lld bytes to go", (char *)buf, (long long)bufleft); else if (nread != chunk) errx(EXIT_FAILURE, "Reading `%s', %lld bytes to go, " "read %zd bytes, expected %ju bytes, does " "metalog size= attribute mismatch source size?", (char *)buf, (long long)bufleft, nread, (uintmax_t)chunk); else p = fbuf; offset = DIP(din, size) - bufleft; if (debug & DEBUG_FS_WRITE_FILE_BLOCK) printf( "ffs_write_file: write %p offset %lld size %lld left %lld\n", p, (long long)offset, (long long)chunk, (long long)bufleft); /* * XXX if holey support is desired, do the check here * * XXX might need to write out last bit in fragroundup * sized chunk. however, ffs_balloc() handles this for us */ errno = ffs_balloc(&in, offset, chunk, &bp); bad_ffs_write_file: if (errno != 0) err(1, "Writing inode %d (%s), bytes %lld + %lld", ino, isfile ? (char *)buf : inode_type(DIP(din, mode) & S_IFMT), (long long)offset, (long long)chunk); memcpy(bp->b_data, p, chunk); errno = bwrite(bp); if (errno != 0) goto bad_ffs_write_file; brelse(bp, 0); if (!isfile) p += chunk; } write_inode_and_leave: ffs_write_inode(&in.i_din, in.i_number, fsopts); leave_ffs_write_file: if (fbuf) free(fbuf); if (ffd != -1) close(ffd); } static void ffs_dump_dirbuf(dirbuf_t *dbuf, const char *dir, int needswap) { doff_t i; struct direct *de; uint16_t reclen; assert (dbuf != NULL); assert (dir != NULL); printf("ffs_dump_dirbuf: dir %s size %d cur %d\n", dir, dbuf->size, dbuf->cur); for (i = 0; i < dbuf->size; ) { de = (struct direct *)(dbuf->buf + i); reclen = ufs_rw16(de->d_reclen, needswap); printf( " inode %4d %7s offset %4d reclen %3d namlen %3d name %s\n", ufs_rw32(de->d_ino, needswap), inode_type(DTTOIF(de->d_type)), i, reclen, de->d_namlen, de->d_name); i += reclen; assert(reclen > 0); } } static void ffs_make_dirbuf(dirbuf_t *dbuf, const char *name, fsnode *node, int needswap) { struct direct de, *dp; uint16_t llen, reclen; u_char *newbuf; assert (dbuf != NULL); assert (name != NULL); assert (node != NULL); /* create direct entry */ (void)memset(&de, 0, sizeof(de)); de.d_ino = ufs_rw32(node->inode->ino, needswap); de.d_type = IFTODT(node->type); de.d_namlen = (uint8_t)strlen(name); strcpy(de.d_name, name); reclen = DIRSIZ_SWAP(0, &de, needswap); de.d_reclen = ufs_rw16(reclen, needswap); dp = (struct direct *)(dbuf->buf + dbuf->cur); llen = 0; if (dp != NULL) llen = DIRSIZ_SWAP(0, dp, needswap); if (debug & DEBUG_FS_MAKE_DIRBUF) printf( "ffs_make_dirbuf: dbuf siz %d cur %d lastlen %d\n" " ino %d type %d reclen %d namlen %d name %.30s\n", dbuf->size, dbuf->cur, llen, ufs_rw32(de.d_ino, needswap), de.d_type, reclen, de.d_namlen, de.d_name); if (reclen + dbuf->cur + llen > roundup(dbuf->size, DIRBLKSIZ)) { if (debug & DEBUG_FS_MAKE_DIRBUF) printf("ffs_make_dirbuf: growing buf to %d\n", dbuf->size + DIRBLKSIZ); - if ((newbuf = realloc(dbuf->buf, dbuf->size + DIRBLKSIZ)) == NULL) - err(1, "Allocating memory for directory buffer"); + newbuf = erealloc(dbuf->buf, dbuf->size + DIRBLKSIZ); dbuf->buf = newbuf; dbuf->size += DIRBLKSIZ; memset(dbuf->buf + dbuf->size - DIRBLKSIZ, 0, DIRBLKSIZ); dbuf->cur = dbuf->size - DIRBLKSIZ; } else if (dp) { /* shrink end of previous */ dp->d_reclen = ufs_rw16(llen,needswap); dbuf->cur += llen; } dp = (struct direct *)(dbuf->buf + dbuf->cur); memcpy(dp, &de, reclen); dp->d_reclen = ufs_rw16(dbuf->size - dbuf->cur, needswap); } /* * cribbed from sys/ufs/ffs/ffs_alloc.c */ static void ffs_write_inode(union dinode *dp, uint32_t ino, const fsinfo_t *fsopts) { char *buf; struct ufs1_dinode *dp1; struct ufs2_dinode *dp2, *dip; struct cg *cgp; struct fs *fs; int cg, cgino, i; daddr_t d; char sbbuf[FFS_MAXBSIZE]; int32_t initediblk; ffs_opt_t *ffs_opts = fsopts->fs_specific; assert (dp != NULL); assert (ino > 0); assert (fsopts != NULL); assert (ffs_opts != NULL); fs = (struct fs *)fsopts->superblock; cg = ino_to_cg(fs, ino); cgino = ino % fs->fs_ipg; if (debug & DEBUG_FS_WRITE_INODE) printf("ffs_write_inode: din %p ino %u cg %d cgino %d\n", dp, ino, cg, cgino); ffs_rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf, fsopts); cgp = (struct cg *)sbbuf; if (!cg_chkmagic_swap(cgp, fsopts->needswap)) errx(1, "ffs_write_inode: cg %d: bad magic number", cg); assert (isclr(cg_inosused_swap(cgp, fsopts->needswap), cgino)); - buf = malloc(fs->fs_bsize); - if (buf == NULL) - errx(1, "ffs_write_inode: cg %d: can't alloc inode block", cg); - + buf = emalloc(fs->fs_bsize); dp1 = (struct ufs1_dinode *)buf; dp2 = (struct ufs2_dinode *)buf; if (fs->fs_cstotal.cs_nifree == 0) errx(1, "ffs_write_inode: fs out of inodes for ino %u", ino); if (fs->fs_cs(fs, cg).cs_nifree == 0) errx(1, "ffs_write_inode: cg %d out of inodes for ino %u", cg, ino); setbit(cg_inosused_swap(cgp, fsopts->needswap), cgino); ufs_add32(cgp->cg_cs.cs_nifree, -1, fsopts->needswap); fs->fs_cstotal.cs_nifree--; fs->fs_cs(fs, cg).cs_nifree--; if (S_ISDIR(DIP(dp, mode))) { ufs_add32(cgp->cg_cs.cs_ndir, 1, fsopts->needswap); fs->fs_cstotal.cs_ndir++; fs->fs_cs(fs, cg).cs_ndir++; } /* * Initialize inode blocks on the fly for UFS2. */ initediblk = ufs_rw32(cgp->cg_initediblk, fsopts->needswap); while (ffs_opts->version == 2 && cgino + INOPB(fs) > initediblk && initediblk < ufs_rw32(cgp->cg_niblk, fsopts->needswap)) { memset(buf, 0, fs->fs_bsize); dip = (struct ufs2_dinode *)buf; for (i = 0; i < INOPB(fs); i++) { dip->di_gen = random(); dip++; } ffs_wtfs(fsbtodb(fs, ino_to_fsba(fs, cg * fs->fs_ipg + initediblk)), fs->fs_bsize, buf, fsopts); initediblk += INOPB(fs); cgp->cg_initediblk = ufs_rw32(initediblk, fsopts->needswap); } ffs_wtfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf, fsopts); /* now write inode */ d = fsbtodb(fs, ino_to_fsba(fs, ino)); ffs_rdfs(d, fs->fs_bsize, buf, fsopts); if (fsopts->needswap) { if (ffs_opts->version == 1) ffs_dinode1_swap(&dp->ffs1_din, &dp1[ino_to_fsbo(fs, ino)]); else ffs_dinode2_swap(&dp->ffs2_din, &dp2[ino_to_fsbo(fs, ino)]); } else { if (ffs_opts->version == 1) dp1[ino_to_fsbo(fs, ino)] = dp->ffs1_din; else dp2[ino_to_fsbo(fs, ino)] = dp->ffs2_din; } ffs_wtfs(d, fs->fs_bsize, buf, fsopts); free(buf); } void panic(const char *fmt, ...) { va_list ap; va_start(ap, fmt); vwarnx(fmt, ap); va_end(ap); exit(1); } Index: stable/11/usr.sbin/makefs/makefs.c =================================================================== --- stable/11/usr.sbin/makefs/makefs.c (revision 332980) +++ stable/11/usr.sbin/makefs/makefs.c (revision 332981) @@ -1,505 +1,499 @@ /* $NetBSD: makefs.c,v 1.26 2006/10/22 21:11:56 christos Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause * * Copyright (c) 2001-2003 Wasabi Systems, Inc. * All rights reserved. * * Written by Luke Mewburn for Wasabi Systems, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed for the NetBSD Project by * Wasabi Systems, Inc. * 4. The name of Wasabi Systems, Inc. may not be used to endorse * or promote products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include +#include #include "makefs.h" #include "mtree.h" /* * list of supported file systems and dispatch functions */ typedef struct { const char *type; void (*prepare_options)(fsinfo_t *); int (*parse_options)(const char *, fsinfo_t *); void (*cleanup_options)(fsinfo_t *); void (*make_fs)(const char *, const char *, fsnode *, fsinfo_t *); } fstype_t; static fstype_t fstypes[] = { #define ENTRY(name) { \ # name, name ## _prep_opts, name ## _parse_opts, \ name ## _cleanup_opts, name ## _makefs \ } ENTRY(ffs), ENTRY(cd9660), { .type = NULL }, }; u_int debug; int dupsok; struct timespec start_time; struct stat stampst; static fstype_t *get_fstype(const char *); static int get_tstamp(const char *, struct stat *); static void usage(fstype_t *, fsinfo_t *); int main(int, char *[]); int main(int argc, char *argv[]) { struct stat sb; struct timeval start; fstype_t *fstype; fsinfo_t fsoptions; fsnode *root; int ch, i, len; char *subtree; char *specfile; setprogname(argv[0]); debug = 0; if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL) errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE); /* set default fsoptions */ (void)memset(&fsoptions, 0, sizeof(fsoptions)); fsoptions.fd = -1; fsoptions.sectorsize = -1; if (fstype->prepare_options) fstype->prepare_options(&fsoptions); specfile = NULL; ch = gettimeofday(&start, NULL); start_time.tv_sec = start.tv_sec; start_time.tv_nsec = start.tv_usec * 1000; if (ch == -1) err(1, "Unable to get system time"); while ((ch = getopt(argc, argv, "B:b:Dd:f:F:M:m:N:o:pR:s:S:t:T:xZ")) != -1) { switch (ch) { case 'B': if (strcmp(optarg, "be") == 0 || strcmp(optarg, "4321") == 0 || strcmp(optarg, "big") == 0) { #if BYTE_ORDER == LITTLE_ENDIAN fsoptions.needswap = 1; #endif } else if (strcmp(optarg, "le") == 0 || strcmp(optarg, "1234") == 0 || strcmp(optarg, "little") == 0) { #if BYTE_ORDER == BIG_ENDIAN fsoptions.needswap = 1; #endif } else { warnx("Invalid endian `%s'.", optarg); usage(fstype, &fsoptions); } break; case 'b': len = strlen(optarg) - 1; if (optarg[len] == '%') { optarg[len] = '\0'; fsoptions.freeblockpc = strsuftoll("free block percentage", optarg, 0, 99); } else { fsoptions.freeblocks = strsuftoll("free blocks", optarg, 0, LLONG_MAX); } break; case 'D': dupsok = 1; break; case 'd': debug = strtoll(optarg, NULL, 0); break; case 'f': len = strlen(optarg) - 1; if (optarg[len] == '%') { optarg[len] = '\0'; fsoptions.freefilepc = strsuftoll("free file percentage", optarg, 0, 99); } else { fsoptions.freefiles = strsuftoll("free files", optarg, 0, LLONG_MAX); } break; case 'F': specfile = optarg; break; case 'M': fsoptions.minsize = strsuftoll("minimum size", optarg, 1LL, LLONG_MAX); break; case 'N': if (! setup_getid(optarg)) errx(1, "Unable to use user and group databases in `%s'", optarg); break; case 'm': fsoptions.maxsize = strsuftoll("maximum size", optarg, 1LL, LLONG_MAX); break; case 'o': { char *p; while ((p = strsep(&optarg, ",")) != NULL) { if (*p == '\0') errx(1, "Empty option"); if (! fstype->parse_options(p, &fsoptions)) usage(fstype, &fsoptions); } break; } case 'p': /* Deprecated in favor of 'Z' */ fsoptions.sparse = 1; break; case 'R': /* Round image size up to specified block size */ fsoptions.roundup = strsuftoll("roundup-size", optarg, 0, LLONG_MAX); break; case 's': fsoptions.minsize = fsoptions.maxsize = strsuftoll("size", optarg, 1LL, LLONG_MAX); break; case 'S': fsoptions.sectorsize = (int)strsuftoll("sector size", optarg, 1LL, INT_MAX); break; case 't': /* Check current one and cleanup if necessary. */ if (fstype->cleanup_options) fstype->cleanup_options(&fsoptions); fsoptions.fs_specific = NULL; if ((fstype = get_fstype(optarg)) == NULL) errx(1, "Unknown fs type `%s'.", optarg); fstype->prepare_options(&fsoptions); break; case 'T': if (get_tstamp(optarg, &stampst) == -1) errx(1, "Cannot get timestamp from `%s'", optarg); break; case 'x': fsoptions.onlyspec = 1; break; case 'Z': /* Superscedes 'p' for compatibility with NetBSD makefs(8) */ fsoptions.sparse = 1; break; case '?': default: usage(fstype, &fsoptions); /* NOTREACHED */ } } if (debug) { printf("debug mask: 0x%08x\n", debug); printf("start time: %ld.%ld, %s", (long)start_time.tv_sec, (long)start_time.tv_nsec, ctime(&start_time.tv_sec)); } argc -= optind; argv += optind; if (argc < 2) usage(fstype, &fsoptions); /* -x must be accompanied by -F */ if (fsoptions.onlyspec != 0 && specfile == NULL) errx(1, "-x requires -F mtree-specfile."); /* Accept '-' as meaning "read from standard input". */ if (strcmp(argv[1], "-") == 0) sb.st_mode = S_IFREG; else { if (stat(argv[1], &sb) == -1) err(1, "Can't stat `%s'", argv[1]); } switch (sb.st_mode & S_IFMT) { case S_IFDIR: /* walk the tree */ subtree = argv[1]; TIMER_START(start); root = walk_dir(subtree, ".", NULL, NULL); TIMER_RESULTS(start, "walk_dir"); break; case S_IFREG: /* read the manifest file */ subtree = "."; TIMER_START(start); root = read_mtree(argv[1], NULL); TIMER_RESULTS(start, "manifest"); break; default: errx(1, "%s: not a file or directory", argv[1]); /* NOTREACHED */ } /* append extra directory */ for (i = 2; i < argc; i++) { if (stat(argv[i], &sb) == -1) err(1, "Can't stat `%s'", argv[i]); if (!S_ISDIR(sb.st_mode)) errx(1, "%s: not a directory", argv[i]); TIMER_START(start); root = walk_dir(argv[i], ".", NULL, root); TIMER_RESULTS(start, "walk_dir2"); } if (specfile) { /* apply a specfile */ TIMER_START(start); apply_specfile(specfile, subtree, root, fsoptions.onlyspec); TIMER_RESULTS(start, "apply_specfile"); } if (debug & DEBUG_DUMP_FSNODES) { printf("\nparent: %s\n", subtree); dump_fsnodes(root); putchar('\n'); } /* build the file system */ TIMER_START(start); fstype->make_fs(argv[0], subtree, root, &fsoptions); TIMER_RESULTS(start, "make_fs"); free_fsnodes(root); exit(0); /* NOTREACHED */ } int set_option(const option_t *options, const char *option, char *buf, size_t len) { char *var, *val; int retval; assert(option != NULL); - if ((var = strdup(option)) == NULL) { - err(EXIT_FAILURE, "Allocating memory for copy of option string"); - } - + var = estrdup(option); for (val = var; *val; val++) if (*val == '=') { *val++ = '\0'; break; } retval = set_option_var(options, var, val, buf, len); free(var); return retval; } int set_option_var(const option_t *options, const char *var, const char *val, char *buf, size_t len) { char *s; size_t i; #define NUM(type) \ if (!*val) { \ *(type *)options[i].value = 1; \ break; \ } \ *(type *)options[i].value = (type)strsuftoll(options[i].desc, val, \ options[i].minimum, options[i].maximum); break for (i = 0; options[i].name != NULL; i++) { if (var[1] == '\0') { if (options[i].letter != var[0]) continue; } else if (strcmp(options[i].name, var) != 0) continue; switch (options[i].type) { case OPT_BOOL: *(bool *)options[i].value = 1; break; case OPT_STRARRAY: strlcpy((void *)options[i].value, val, (size_t) options[i].maximum); break; case OPT_STRPTR: - if ((s = strdup(val)) == NULL) - err(1, NULL); + s = estrdup(val); *(char **)options[i].value = s; break; case OPT_STRBUF: if (buf == NULL) abort(); strlcpy(buf, val, len); break; case OPT_INT64: NUM(uint64_t); case OPT_INT32: NUM(uint32_t); case OPT_INT16: NUM(uint16_t); case OPT_INT8: NUM(uint8_t); default: warnx("Unknown type %d in option %s", options[i].type, val); return 0; } return i; } warnx("Unknown option `%s'", var); return -1; } static fstype_t * get_fstype(const char *type) { int i; for (i = 0; fstypes[i].type != NULL; i++) if (strcmp(fstypes[i].type, type) == 0) return (&fstypes[i]); return (NULL); } option_t * copy_opts(const option_t *o) { size_t i; - void *rv; for (i = 0; o[i].name; i++) continue; i++; - if ((rv = calloc(i, sizeof(*o))) == NULL) - err(1, "calloc"); - return memcpy(rv, o, i * sizeof(*o)); + return memcpy(ecalloc(i, sizeof(*o)), o, i * sizeof(*o)); } static int get_tstamp(const char *b, struct stat *st) { time_t when; char *eb; long long l; if (stat(b, st) != -1) return 0; { errno = 0; l = strtoll(b, &eb, 0); if (b == eb || *eb || errno) return -1; when = (time_t)l; } st->st_ino = 1; #ifdef HAVE_STRUCT_STAT_BIRTHTIME st->st_birthtime = #endif st->st_mtime = st->st_ctime = st->st_atime = when; return 0; } static void usage(fstype_t *fstype, fsinfo_t *fsoptions) { const char *prog; prog = getprogname(); fprintf(stderr, "Usage: %s [-xZ] [-B endian] [-b free-blocks] [-d debug-mask]\n" "\t[-F mtree-specfile] [-f free-files] [-M minimum-size] [-m maximum-size]\n" "\t[-N userdb-dir] [-o fs-options] [-R roundup-size] [-S sector-size]\n" "\t[-s image-size] [-T ] [-t fs-type]\n" "\timage-file directory | manifest [extra-directory ...]\n", prog); if (fstype) { size_t i; option_t *o = fsoptions->fs_options; fprintf(stderr, "\n%s specific options:\n", fstype->type); for (i = 0; o[i].name != NULL; i++) fprintf(stderr, "\t%c%c%20.20s\t%s\n", o[i].letter ? o[i].letter : ' ', o[i].letter ? ',' : ' ', o[i].name, o[i].desc); } exit(1); } Index: stable/11/usr.sbin/makefs/mtree.c =================================================================== --- stable/11/usr.sbin/makefs/mtree.c (revision 332980) +++ stable/11/usr.sbin/makefs/mtree.c (revision 332981) @@ -1,1127 +1,1105 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2011 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include +#include #include #include "makefs.h" #ifndef ENOATTR #define ENOATTR ENOMSG #endif #define IS_DOT(nm) ((nm)[0] == '.' && (nm)[1] == '\0') #define IS_DOTDOT(nm) ((nm)[0] == '.' && (nm)[1] == '.' && (nm)[2] == '\0') struct mtree_fileinfo { SLIST_ENTRY(mtree_fileinfo) next; FILE *fp; const char *name; u_int line; }; /* Global state used while parsing. */ static SLIST_HEAD(, mtree_fileinfo) mtree_fileinfo = SLIST_HEAD_INITIALIZER(mtree_fileinfo); static fsnode *mtree_root; static fsnode *mtree_current; static fsnode mtree_global; static fsinode mtree_global_inode; static u_int errors, warnings; static void mtree_error(const char *, ...) __printflike(1, 2); static void mtree_warning(const char *, ...) __printflike(1, 2); static int mtree_file_push(const char *name, FILE *fp) { struct mtree_fileinfo *fi; - fi = malloc(sizeof(*fi)); - if (fi == NULL) - return (ENOMEM); - + fi = emalloc(sizeof(*fi)); if (strcmp(name, "-") == 0) - fi->name = strdup("(stdin)"); + fi->name = estrdup("(stdin)"); else - fi->name = strdup(name); + fi->name = estrdup(name); if (fi->name == NULL) { free(fi); return (ENOMEM); } fi->fp = fp; fi->line = 0; SLIST_INSERT_HEAD(&mtree_fileinfo, fi, next); return (0); } static void mtree_print(const char *msgtype, const char *fmt, va_list ap) { struct mtree_fileinfo *fi; if (msgtype != NULL) { fi = SLIST_FIRST(&mtree_fileinfo); if (fi != NULL) fprintf(stderr, "%s:%u: ", fi->name, fi->line); fprintf(stderr, "%s: ", msgtype); } vfprintf(stderr, fmt, ap); } static void mtree_error(const char *fmt, ...) { va_list ap; va_start(ap, fmt); mtree_print("error", fmt, ap); va_end(ap); errors++; fputc('\n', stderr); } static void mtree_warning(const char *fmt, ...) { va_list ap; va_start(ap, fmt); mtree_print("warning", fmt, ap); va_end(ap); warnings++; fputc('\n', stderr); } #ifndef MAKEFS_MAX_TREE_DEPTH # define MAKEFS_MAX_TREE_DEPTH (MAXPATHLEN/2) #endif /* construct path to node->name */ static char * mtree_file_path(fsnode *node) { fsnode *pnode; struct sbuf *sb; char *res, *rp[MAKEFS_MAX_TREE_DEPTH]; int depth; depth = 0; rp[depth] = node->name; for (pnode = node->parent; pnode && depth < MAKEFS_MAX_TREE_DEPTH - 1; pnode = pnode->parent) { if (strcmp(pnode->name, ".") == 0) break; rp[++depth] = pnode->name; } sb = sbuf_new_auto(); if (sb == NULL) { errno = ENOMEM; return (NULL); } while (depth > 0) { sbuf_cat(sb, rp[depth--]); sbuf_putc(sb, '/'); } sbuf_cat(sb, rp[depth]); sbuf_finish(sb); - res = strdup(sbuf_data(sb)); + res = estrdup(sbuf_data(sb)); sbuf_delete(sb); if (res == NULL) errno = ENOMEM; return res; } /* mtree_resolve() sets errno to indicate why NULL was returned. */ static char * mtree_resolve(const char *spec, int *istemp) { struct sbuf *sb; char *res, *var = NULL; const char *base, *p, *v; size_t len; int c, error, quoted, subst; len = strlen(spec); if (len == 0) { errno = EINVAL; return (NULL); } c = (len > 1) ? (spec[0] == spec[len - 1]) ? spec[0] : 0 : 0; *istemp = (c == '`') ? 1 : 0; subst = (c == '`' || c == '"') ? 1 : 0; quoted = (subst || c == '\'') ? 1 : 0; if (!subst) { - res = strdup(spec + quoted); - if (res != NULL && quoted) + res = estrdup(spec + quoted); + if (quoted) res[len - 2] = '\0'; return (res); } sb = sbuf_new_auto(); if (sb == NULL) { errno = ENOMEM; return (NULL); } base = spec + 1; len -= 2; error = 0; while (len > 0) { p = strchr(base, '$'); if (p == NULL) { sbuf_bcat(sb, base, len); base += len; len = 0; continue; } /* The following is safe. spec always starts with a quote. */ if (p[-1] == '\\') p--; if (base != p) { sbuf_bcat(sb, base, p - base); len -= p - base; base = p; } if (*p == '\\') { sbuf_putc(sb, '$'); base += 2; len -= 2; continue; } /* Skip the '$'. */ base++; len--; /* Handle ${X} vs $X. */ v = base; if (*base == '{') { p = strchr(v, '}'); if (p == NULL) p = v; } else p = v; len -= (p + 1) - base; base = p + 1; if (v == p) { sbuf_putc(sb, *v); continue; } error = ENOMEM; - var = calloc(p - v, 1); - if (var == NULL) - break; - + var = ecalloc(p - v, 1); memcpy(var, v + 1, p - v - 1); if (strcmp(var, ".CURDIR") == 0) { res = getcwd(NULL, 0); if (res == NULL) break; } else if (strcmp(var, ".PROG") == 0) { - res = strdup(getprogname()); - if (res == NULL) - break; + res = estrdup(getprogname()); } else { v = getenv(var); if (v != NULL) { - res = strdup(v); - if (res == NULL) - break; + res = estrdup(v); } else res = NULL; } error = 0; if (res != NULL) { sbuf_cat(sb, res); free(res); } free(var); var = NULL; } free(var); sbuf_finish(sb); res = (error == 0) ? strdup(sbuf_data(sb)) : NULL; sbuf_delete(sb); if (res == NULL) errno = ENOMEM; return (res); } static int skip_over(FILE *fp, const char *cs) { int c; c = getc(fp); while (c != EOF && strchr(cs, c) != NULL) c = getc(fp); if (c != EOF) { ungetc(c, fp); return (0); } return (ferror(fp) ? errno : -1); } static int skip_to(FILE *fp, const char *cs) { int c; c = getc(fp); while (c != EOF && strchr(cs, c) == NULL) c = getc(fp); if (c != EOF) { ungetc(c, fp); return (0); } return (ferror(fp) ? errno : -1); } static int read_word(FILE *fp, char *buf, size_t bufsz) { struct mtree_fileinfo *fi; size_t idx, qidx; int c, done, error, esc, qlvl; if (bufsz == 0) return (EINVAL); done = 0; esc = 0; idx = 0; qidx = -1; qlvl = 0; do { c = getc(fp); switch (c) { case EOF: buf[idx] = '\0'; error = ferror(fp) ? errno : -1; if (error == -1) mtree_error("unexpected end of file"); return (error); case '#': /* comment -- skip to end of line. */ if (!esc) { error = skip_to(fp, "\n"); if (!error) continue; } break; case '\\': esc++; break; case '`': case '\'': case '"': if (esc) break; if (qlvl == 0) { qlvl++; qidx = idx; } else if (c == buf[qidx]) { qlvl--; if (qlvl > 0) { do { qidx--; } while (buf[qidx] != '`' && buf[qidx] != '\'' && buf[qidx] != '"'); } else qidx = -1; } else { qlvl++; qidx = idx; } break; case ' ': case '\t': case '\n': if (!esc && qlvl == 0) { ungetc(c, fp); c = '\0'; done = 1; break; } if (c == '\n') { /* * We going to eat the newline ourselves. */ if (qlvl > 0) mtree_warning("quoted word straddles " "onto next line."); fi = SLIST_FIRST(&mtree_fileinfo); fi->line++; } break; default: if (esc) buf[idx++] = '\\'; break; } buf[idx++] = c; esc = 0; } while (idx < bufsz && !done); if (idx >= bufsz) { mtree_error("word too long to fit buffer (max %zu characters)", bufsz); skip_to(fp, " \t\n"); } return (0); } static fsnode * create_node(const char *name, u_int type, fsnode *parent, fsnode *global) { fsnode *n; - n = calloc(1, sizeof(*n)); - if (n == NULL) - return (NULL); - - n->name = strdup(name); - if (n->name == NULL) { - free(n); - return (NULL); - } - + n = ecalloc(1, sizeof(*n)); + n->name = estrdup(name); n->type = (type == 0) ? global->type : type; n->parent = parent; - n->inode = calloc(1, sizeof(*n->inode)); - if (n->inode == NULL) { - free(n->name); - free(n); - return (NULL); - } + n->inode = ecalloc(1, sizeof(*n->inode)); /* Assign global options/defaults. */ bcopy(global->inode, n->inode, sizeof(*n->inode)); n->inode->st.st_mode = (n->inode->st.st_mode & ~S_IFMT) | n->type; if (n->type == S_IFLNK) n->symlink = global->symlink; else if (n->type == S_IFREG) n->contents = global->contents; return (n); } static void destroy_node(fsnode *n) { assert(n != NULL); assert(n->name != NULL); assert(n->inode != NULL); free(n->inode); free(n->name); free(n); } static int read_number(const char *tok, u_int base, intmax_t *res, intmax_t min, intmax_t max) { char *end; intmax_t val; val = strtoimax(tok, &end, base); if (end == tok || end[0] != '\0') return (EINVAL); if (val < min || val > max) return (EDOM); *res = val; return (0); } static int read_mtree_keywords(FILE *fp, fsnode *node) { char keyword[PATH_MAX]; char *name, *p, *value; gid_t gid; uid_t uid; struct stat *st, sb; intmax_t num; u_long flset, flclr; int error, istemp, type; st = &node->inode->st; do { error = skip_over(fp, " \t"); if (error) break; error = read_word(fp, keyword, sizeof(keyword)); if (error) break; if (keyword[0] == '\0') break; value = strchr(keyword, '='); if (value != NULL) *value++ = '\0'; /* * We use EINVAL, ENOATTR, ENOSYS and ENXIO to signal * certain conditions: * EINVAL - Value provided for a keyword that does * not take a value. The value is ignored. * ENOATTR - Value missing for a keyword that needs * a value. The keyword is ignored. * ENOSYS - Unsupported keyword encountered. The * keyword is ignored. * ENXIO - Value provided for a keyword that does * not take a value. The value is ignored. */ switch (keyword[0]) { case 'c': if (strcmp(keyword, "contents") == 0) { if (value == NULL) { error = ENOATTR; break; } - node->contents = strdup(value); + node->contents = estrdup(value); } else error = ENOSYS; break; case 'f': if (strcmp(keyword, "flags") == 0) { if (value == NULL) { error = ENOATTR; break; } flset = flclr = 0; if (!strtofflags(&value, &flset, &flclr)) { st->st_flags &= ~flclr; st->st_flags |= flset; } else error = errno; } else error = ENOSYS; break; case 'g': if (strcmp(keyword, "gid") == 0) { if (value == NULL) { error = ENOATTR; break; } error = read_number(value, 10, &num, 0, UINT_MAX); if (!error) st->st_gid = num; } else if (strcmp(keyword, "gname") == 0) { if (value == NULL) { error = ENOATTR; break; } if (gid_from_group(value, &gid) == 0) st->st_gid = gid; else error = EINVAL; } else error = ENOSYS; break; case 'l': if (strcmp(keyword, "link") == 0) { if (value == NULL) { error = ENOATTR; break; } - node->symlink = malloc(strlen(value) + 1); + node->symlink = emalloc(strlen(value) + 1); if (node->symlink == NULL) { error = errno; break; } if (strunvis(node->symlink, value) < 0) { error = errno; break; } } else error = ENOSYS; break; case 'm': if (strcmp(keyword, "mode") == 0) { if (value == NULL) { error = ENOATTR; break; } if (value[0] >= '0' && value[0] <= '9') { error = read_number(value, 8, &num, 0, 07777); if (!error) { st->st_mode &= S_IFMT; st->st_mode |= num; } } else { /* Symbolic mode not supported. */ error = EINVAL; break; } } else error = ENOSYS; break; case 'o': if (strcmp(keyword, "optional") == 0) { if (value != NULL) error = ENXIO; node->flags |= FSNODE_F_OPTIONAL; } else error = ENOSYS; break; case 's': if (strcmp(keyword, "size") == 0) { if (value == NULL) { error = ENOATTR; break; } error = read_number(value, 10, &num, 0, INTMAX_MAX); if (!error) st->st_size = num; } else error = ENOSYS; break; case 't': if (strcmp(keyword, "time") == 0) { if (value == NULL) { error = ENOATTR; break; } p = strchr(value, '.'); if (p != NULL) *p++ = '\0'; error = read_number(value, 10, &num, 0, INTMAX_MAX); if (error) break; st->st_atime = num; st->st_ctime = num; st->st_mtime = num; if (p == NULL) break; error = read_number(p, 10, &num, 0, INTMAX_MAX); if (error) break; if (num != 0) error = EINVAL; } else if (strcmp(keyword, "type") == 0) { if (value == NULL) { error = ENOATTR; break; } if (strcmp(value, "dir") == 0) node->type = S_IFDIR; else if (strcmp(value, "file") == 0) node->type = S_IFREG; else if (strcmp(value, "link") == 0) node->type = S_IFLNK; else error = EINVAL; } else error = ENOSYS; break; case 'u': if (strcmp(keyword, "uid") == 0) { if (value == NULL) { error = ENOATTR; break; } error = read_number(value, 10, &num, 0, UINT_MAX); if (!error) st->st_uid = num; } else if (strcmp(keyword, "uname") == 0) { if (value == NULL) { error = ENOATTR; break; } if (uid_from_user(value, &uid) == 0) st->st_uid = uid; else error = EINVAL; } else error = ENOSYS; break; default: error = ENOSYS; break; } switch (error) { case EINVAL: mtree_error("%s: invalid value '%s'", keyword, value); break; case ENOATTR: mtree_error("%s: keyword needs a value", keyword); break; case ENOSYS: mtree_warning("%s: unsupported keyword", keyword); break; case ENXIO: mtree_error("%s: keyword does not take a value", keyword); break; } } while (1); if (error) return (error); st->st_mode = (st->st_mode & ~S_IFMT) | node->type; /* Nothing more to do for the global defaults. */ if (node->name == NULL) return (0); /* * Be intelligent about the file type. */ if (node->contents != NULL) { if (node->symlink != NULL) { mtree_error("%s: both link and contents keywords " "defined", node->name); return (0); } type = S_IFREG; } else if (node->type != 0) { type = node->type; if (type == S_IFREG) { /* the named path is the default contents */ node->contents = mtree_file_path(node); } } else type = (node->symlink != NULL) ? S_IFLNK : S_IFDIR; if (node->type == 0) node->type = type; if (node->type != type) { mtree_error("%s: file type and defined keywords to not match", node->name); return (0); } st->st_mode = (st->st_mode & ~S_IFMT) | node->type; if (node->contents == NULL) return (0); name = mtree_resolve(node->contents, &istemp); if (name == NULL) return (errno); if (stat(name, &sb) != 0) { mtree_error("%s: contents file '%s' not found", node->name, name); free(name); return (0); } /* * Check for hardlinks. If the contents key is used, then the check * will only trigger if the contents file is a link even if it is used * by more than one file */ if (sb.st_nlink > 1) { fsinode *curino; st->st_ino = sb.st_ino; st->st_dev = sb.st_dev; curino = link_check(node->inode); if (curino != NULL) { free(node->inode); node->inode = curino; node->inode->nlink++; } } free(node->contents); node->contents = name; st->st_size = sb.st_size; return (0); } static int read_mtree_command(FILE *fp) { char cmd[10]; int error; error = read_word(fp, cmd, sizeof(cmd)); if (error) goto out; error = read_mtree_keywords(fp, &mtree_global); out: skip_to(fp, "\n"); (void)getc(fp); return (error); } static int read_mtree_spec1(FILE *fp, bool def, const char *name) { fsnode *last, *node, *parent; u_int type; int error; assert(name[0] != '\0'); /* * Treat '..' specially, because it only changes our current * directory. We don't create a node for it. We simply ignore * any keywords that may appear on the line as well. * Going up a directory is a little non-obvious. A directory * node has a corresponding '.' child. The parent of '.' is * not the '.' node of the parent directory, but the directory * node within the parent to which the child relates. However, * going up a directory means we need to find the '.' node to * which the directoy node is linked. This we can do via the * first * pointer, because '.' is always the first entry in a * directory. */ if (IS_DOTDOT(name)) { /* This deals with NULL pointers as well. */ if (mtree_current == mtree_root) { mtree_warning("ignoring .. in root directory"); return (0); } node = mtree_current; assert(node != NULL); assert(IS_DOT(node->name)); assert(node->first == node); /* Get the corresponding directory node in the parent. */ node = mtree_current->parent; assert(node != NULL); assert(!IS_DOT(node->name)); node = node->first; assert(node != NULL); assert(IS_DOT(node->name)); assert(node->first == node); mtree_current = node; return (0); } /* * If we don't have a current directory and the first specification * (either implicit or defined) is not '.', then we need to create * a '.' node first (using a recursive call). */ if (!IS_DOT(name) && mtree_current == NULL) { error = read_mtree_spec1(fp, false, "."); if (error) return (error); } /* * Lookup the name in the current directory (if we have a current * directory) to make sure we do not create multiple nodes for the * same component. For non-definitions, if we find a node with the * same name, simply change the current directory. For definitions * more happens. */ last = NULL; node = mtree_current; while (node != NULL) { assert(node->first == mtree_current); if (strcmp(name, node->name) == 0) { if (def == true) { if (!dupsok) mtree_error( "duplicate definition of %s", name); else mtree_warning( "duplicate definition of %s", name); return (0); } if (node->type != S_IFDIR) { mtree_error("%s is not a directory", name); return (0); } assert(!IS_DOT(name)); node = node->child; assert(node != NULL); assert(IS_DOT(node->name)); mtree_current = node; return (0); } last = node; node = last->next; } parent = (mtree_current != NULL) ? mtree_current->parent : NULL; type = (def == false || IS_DOT(name)) ? S_IFDIR : 0; node = create_node(name, type, parent, &mtree_global); if (node == NULL) return (ENOMEM); if (def == true) { error = read_mtree_keywords(fp, node); if (error) { destroy_node(node); return (error); } } node->first = (mtree_current != NULL) ? mtree_current : node; if (last != NULL) last->next = node; if (node->type != S_IFDIR) return (0); if (!IS_DOT(node->name)) { parent = node; node = create_node(".", S_IFDIR, parent, parent); if (node == NULL) { last->next = NULL; destroy_node(parent); return (ENOMEM); } parent->child = node; node->first = node; } assert(node != NULL); assert(IS_DOT(node->name)); assert(node->first == node); mtree_current = node; if (mtree_root == NULL) mtree_root = node; return (0); } static int read_mtree_spec(FILE *fp) { char pathspec[PATH_MAX], pathtmp[4*PATH_MAX + 1]; char *cp; int error; error = read_word(fp, pathtmp, sizeof(pathtmp)); if (error) goto out; if (strnunvis(pathspec, PATH_MAX, pathtmp) == -1) { error = errno; goto out; } error = 0; cp = strchr(pathspec, '/'); if (cp != NULL) { /* Absolute pathname */ mtree_current = mtree_root; do { *cp++ = '\0'; /* Disallow '..' as a component. */ if (IS_DOTDOT(pathspec)) { mtree_error("absolute path cannot contain " ".. component"); goto out; } /* Ignore multiple adjacent slashes and '.'. */ if (pathspec[0] != '\0' && !IS_DOT(pathspec)) error = read_mtree_spec1(fp, false, pathspec); memmove(pathspec, cp, strlen(cp) + 1); cp = strchr(pathspec, '/'); } while (!error && cp != NULL); /* Disallow '.' and '..' as the last component. */ if (!error && (IS_DOT(pathspec) || IS_DOTDOT(pathspec))) { mtree_error("absolute path cannot contain . or .. " "components"); goto out; } } /* Ignore absolute specfications that end with a slash. */ if (!error && pathspec[0] != '\0') error = read_mtree_spec1(fp, true, pathspec); out: skip_to(fp, "\n"); (void)getc(fp); return (error); } fsnode * read_mtree(const char *fname, fsnode *node) { struct mtree_fileinfo *fi; FILE *fp; int c, error; /* We do not yet support nesting... */ assert(node == NULL); if (strcmp(fname, "-") == 0) fp = stdin; else { fp = fopen(fname, "r"); if (fp == NULL) err(1, "Can't open `%s'", fname); } error = mtree_file_push(fname, fp); if (error) goto out; bzero(&mtree_global, sizeof(mtree_global)); bzero(&mtree_global_inode, sizeof(mtree_global_inode)); mtree_global.inode = &mtree_global_inode; mtree_global_inode.nlink = 1; mtree_global_inode.st.st_nlink = 1; mtree_global_inode.st.st_atime = mtree_global_inode.st.st_ctime = mtree_global_inode.st.st_mtime = time(NULL); errors = warnings = 0; setgroupent(1); setpassent(1); mtree_root = node; mtree_current = node; do { /* Start of a new line... */ fi = SLIST_FIRST(&mtree_fileinfo); fi->line++; error = skip_over(fp, " \t"); if (error) break; c = getc(fp); if (c == EOF) { error = ferror(fp) ? errno : -1; break; } switch (c) { case '\n': /* empty line */ error = 0; break; case '#': /* comment -- skip to end of line. */ error = skip_to(fp, "\n"); if (!error) (void)getc(fp); break; case '/': /* special commands */ error = read_mtree_command(fp); break; default: /* specification */ ungetc(c, fp); error = read_mtree_spec(fp); break; } } while (!error); endpwent(); endgrent(); if (error <= 0 && (errors || warnings)) { warnx("%u error(s) and %u warning(s) in mtree manifest", errors, warnings); if (errors) exit(1); } out: if (error > 0) errc(1, error, "Error reading mtree file"); if (fp != stdin) fclose(fp); if (mtree_root != NULL) return (mtree_root); /* Handle empty specifications. */ node = create_node(".", S_IFDIR, NULL, &mtree_global); node->first = node; return (node); } Index: stable/11/usr.sbin/makefs/walk.c =================================================================== --- stable/11/usr.sbin/makefs/walk.c (revision 332980) +++ stable/11/usr.sbin/makefs/walk.c (revision 332981) @@ -1,704 +1,696 @@ /* $NetBSD: walk.c,v 1.24 2008/12/28 21:51:46 christos Exp $ */ /* * SPDX-License-Identifier: BSD-4-Clause * * Copyright (c) 2001 Wasabi Systems, Inc. * All rights reserved. * * Written by Luke Mewburn for Wasabi Systems, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed for the NetBSD Project by * Wasabi Systems, Inc. * 4. The name of Wasabi Systems, Inc. may not be used to endorse * or promote products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include "makefs.h" #include "mtree.h" #include "extern.h" static void apply_specdir(const char *, NODE *, fsnode *, int); static void apply_specentry(const char *, NODE *, fsnode *); static fsnode *create_fsnode(const char *, const char *, const char *, struct stat *); /* * walk_dir -- * build a tree of fsnodes from `root' and `dir', with a parent * fsnode of `parent' (which may be NULL for the root of the tree). * append the tree to a fsnode of `join' if it is not NULL. * each "level" is a directory, with the "." entry guaranteed to be * at the start of the list, and without ".." entries. */ fsnode * walk_dir(const char *root, const char *dir, fsnode *parent, fsnode *join) { fsnode *first, *cur, *prev, *last; DIR *dirp; struct dirent *dent; char path[MAXPATHLEN + 1]; struct stat stbuf; char *name, *rp; int dot, len; assert(root != NULL); assert(dir != NULL); len = snprintf(path, sizeof(path), "%s/%s", root, dir); if (len >= (int)sizeof(path)) errx(1, "Pathname too long."); if (debug & DEBUG_WALK_DIR) printf("walk_dir: %s %p\n", path, parent); if ((dirp = opendir(path)) == NULL) err(1, "Can't opendir `%s'", path); rp = path + strlen(root) + 1; if (join != NULL) { first = cur = join; while (cur->next != NULL) cur = cur->next; prev = cur; } else first = prev = NULL; last = prev; while ((dent = readdir(dirp)) != NULL) { name = dent->d_name; dot = 0; if (name[0] == '.') switch (name[1]) { case '\0': /* "." */ if (join != NULL) continue; dot = 1; break; case '.': /* ".." */ if (name[2] == '\0') continue; /* FALLTHROUGH */ default: dot = 0; } if (debug & DEBUG_WALK_DIR_NODE) printf("scanning %s/%s/%s\n", root, dir, name); if (snprintf(path + len, sizeof(path) - len, "/%s", name) >= (int)sizeof(path) - len) errx(1, "Pathname too long."); if (lstat(path, &stbuf) == -1) err(1, "Can't lstat `%s'", path); #ifdef S_ISSOCK if (S_ISSOCK(stbuf.st_mode & S_IFMT)) { if (debug & DEBUG_WALK_DIR_NODE) printf(" skipping socket %s\n", path); continue; } #endif if (join != NULL) { cur = join->next; for (;;) { if (cur == NULL || strcmp(cur->name, name) == 0) break; if (cur == last) { cur = NULL; break; } cur = cur->next; } if (cur != NULL) { if (S_ISDIR(cur->type) && S_ISDIR(stbuf.st_mode)) { if (debug & DEBUG_WALK_DIR_NODE) printf("merging %s with %p\n", path, cur->child); cur->child = walk_dir(root, rp, cur, cur->child); continue; } errx(1, "Can't merge %s `%s' with existing %s", inode_type(stbuf.st_mode), path, inode_type(cur->type)); } } cur = create_fsnode(root, dir, name, &stbuf); cur->parent = parent; if (dot) { /* ensure "." is at the start of the list */ cur->next = first; first = cur; if (! prev) prev = cur; cur->first = first; } else { /* not "." */ if (prev) prev->next = cur; prev = cur; if (!first) first = cur; cur->first = first; if (S_ISDIR(cur->type)) { cur->child = walk_dir(root, rp, cur, NULL); continue; } } if (stbuf.st_nlink > 1) { fsinode *curino; curino = link_check(cur->inode); if (curino != NULL) { free(cur->inode); cur->inode = curino; cur->inode->nlink++; if (debug & DEBUG_WALK_DIR_LINKCHECK) printf("link_check: found [%llu, %llu]\n", (unsigned long long)curino->st.st_dev, (unsigned long long)curino->st.st_ino); } } if (S_ISLNK(cur->type)) { char slink[PATH_MAX+1]; int llen; llen = readlink(path, slink, sizeof(slink) - 1); if (llen == -1) err(1, "Readlink `%s'", path); slink[llen] = '\0'; - if ((cur->symlink = strdup(slink)) == NULL) - err(1, "Memory allocation error"); + cur->symlink = estrdup(slink); } } assert(first != NULL); if (join == NULL) for (cur = first->next; cur != NULL; cur = cur->next) cur->first = first; if (closedir(dirp) == -1) err(1, "Can't closedir `%s/%s'", root, dir); return (first); } static fsnode * create_fsnode(const char *root, const char *path, const char *name, struct stat *stbuf) { fsnode *cur; - if ((cur = calloc(1, sizeof(fsnode))) == NULL || - (cur->path = strdup(path)) == NULL || - (cur->name = strdup(name)) == NULL || - (cur->inode = calloc(1, sizeof(fsinode))) == NULL) - err(1, "Memory allocation error"); + cur = ecalloc(1, sizeof(*cur)); + cur->path = estrdup(path); + cur->name = estrdup(name); + cur->inode = ecalloc(1, sizeof(*cur->inode)); cur->root = root; cur->type = stbuf->st_mode & S_IFMT; cur->inode->nlink = 1; cur->inode->st = *stbuf; if (stampst.st_ino) { cur->inode->st.st_atime = stampst.st_atime; cur->inode->st.st_mtime = stampst.st_mtime; cur->inode->st.st_ctime = stampst.st_ctime; #if HAVE_STRUCT_STAT_ST_MTIMENSEC cur->inode->st.st_atimensec = stampst.st_atimensec; cur->inode->st.st_mtimensec = stampst.st_mtimensec; cur->inode->st.st_ctimensec = stampst.st_ctimensec; #endif #if HAVE_STRUCT_STAT_BIRTHTIME cur->inode->st.st_birthtime = stampst.st_birthtime; cur->inode->st.st_birthtimensec = stampst.st_birthtimensec; #endif } return (cur); } /* * free_fsnodes -- * Removes node from tree and frees it and all of * its descendants. */ void free_fsnodes(fsnode *node) { fsnode *cur, *next; assert(node != NULL); /* for ".", start with actual parent node */ if (node->first == node) { assert(node->name[0] == '.' && node->name[1] == '\0'); if (node->parent) { assert(node->parent->child == node); node = node->parent; } } /* Find ourselves in our sibling list and unlink */ if (node->first != node) { for (cur = node->first; cur->next; cur = cur->next) { if (cur->next == node) { cur->next = node->next; node->next = NULL; break; } } } for (cur = node; cur != NULL; cur = next) { next = cur->next; if (cur->child) { cur->child->parent = NULL; free_fsnodes(cur->child); } if (cur->inode->nlink-- == 1) free(cur->inode); if (cur->symlink) free(cur->symlink); free(cur->path); free(cur->name); free(cur); } } /* * apply_specfile -- * read in the mtree(8) specfile, and apply it to the tree * at dir,parent. parameters in parent on equivalent types * will be changed to those found in specfile, and missing * entries will be added. */ void apply_specfile(const char *specfile, const char *dir, fsnode *parent, int speconly) { struct timeval start; FILE *fp; NODE *root; assert(specfile != NULL); assert(parent != NULL); if (debug & DEBUG_APPLY_SPECFILE) printf("apply_specfile: %s, %s %p\n", specfile, dir, parent); /* read in the specfile */ if ((fp = fopen(specfile, "r")) == NULL) err(1, "Can't open `%s'", specfile); TIMER_START(start); root = spec(fp); TIMER_RESULTS(start, "spec"); if (fclose(fp) == EOF) err(1, "Can't close `%s'", specfile); /* perform some sanity checks */ if (root == NULL) errx(1, "Specfile `%s' did not contain a tree", specfile); assert(strcmp(root->name, ".") == 0); assert(root->type == F_DIR); /* merge in the changes */ apply_specdir(dir, root, parent, speconly); } static void apply_specdir(const char *dir, NODE *specnode, fsnode *dirnode, int speconly) { char path[MAXPATHLEN + 1]; NODE *curnode; fsnode *curfsnode; assert(specnode != NULL); assert(dirnode != NULL); if (debug & DEBUG_APPLY_SPECFILE) printf("apply_specdir: %s %p %p\n", dir, specnode, dirnode); if (specnode->type != F_DIR) errx(1, "Specfile node `%s/%s' is not a directory", dir, specnode->name); if (dirnode->type != S_IFDIR) errx(1, "Directory node `%s/%s' is not a directory", dir, dirnode->name); apply_specentry(dir, specnode, dirnode); /* Remove any filesystem nodes not found in specfile */ /* XXX inefficient. This is O^2 in each dir and it would * have been better never to have walked this part of the tree * to begin with */ if (speconly) { fsnode *next; assert(dirnode->name[0] == '.' && dirnode->name[1] == '\0'); for (curfsnode = dirnode->next; curfsnode != NULL; curfsnode = next) { next = curfsnode->next; for (curnode = specnode->child; curnode != NULL; curnode = curnode->next) { if (strcmp(curnode->name, curfsnode->name) == 0) break; } if (curnode == NULL) { if (debug & DEBUG_APPLY_SPECONLY) { printf("apply_specdir: trimming %s/%s %p\n", dir, curfsnode->name, curfsnode); } free_fsnodes(curfsnode); } } } /* now walk specnode->child matching up with dirnode */ for (curnode = specnode->child; curnode != NULL; curnode = curnode->next) { if (debug & DEBUG_APPLY_SPECENTRY) printf("apply_specdir: spec %s\n", curnode->name); for (curfsnode = dirnode->next; curfsnode != NULL; curfsnode = curfsnode->next) { #if 0 /* too verbose for now */ if (debug & DEBUG_APPLY_SPECENTRY) printf("apply_specdir: dirent %s\n", curfsnode->name); #endif if (strcmp(curnode->name, curfsnode->name) == 0) break; } if (snprintf(path, sizeof(path), "%s/%s", dir, curnode->name) >= sizeof(path)) errx(1, "Pathname too long."); if (curfsnode == NULL) { /* need new entry */ struct stat stbuf; /* * don't add optional spec entries * that lack an existing fs entry */ if ((curnode->flags & F_OPT) && lstat(path, &stbuf) == -1) continue; /* check that enough info is provided */ #define NODETEST(t, m) \ if (!(t)) \ errx(1, "`%s': %s not provided", path, m) NODETEST(curnode->flags & F_TYPE, "type"); NODETEST(curnode->flags & F_MODE, "mode"); /* XXX: require F_TIME ? */ NODETEST(curnode->flags & F_GID || curnode->flags & F_GNAME, "group"); NODETEST(curnode->flags & F_UID || curnode->flags & F_UNAME, "user"); /* if (curnode->type == F_BLOCK || curnode->type == F_CHAR) NODETEST(curnode->flags & F_DEV, "device number");*/ #undef NODETEST if (debug & DEBUG_APPLY_SPECFILE) printf("apply_specdir: adding %s\n", curnode->name); /* build minimal fsnode */ memset(&stbuf, 0, sizeof(stbuf)); stbuf.st_mode = nodetoino(curnode->type); stbuf.st_nlink = 1; stbuf.st_mtime = stbuf.st_atime = stbuf.st_ctime = start_time.tv_sec; #if HAVE_STRUCT_STAT_ST_MTIMENSEC stbuf.st_mtimensec = stbuf.st_atimensec = stbuf.st_ctimensec = start_time.tv_nsec; #endif curfsnode = create_fsnode(".", ".", curnode->name, &stbuf); curfsnode->parent = dirnode->parent; curfsnode->first = dirnode; curfsnode->next = dirnode->next; dirnode->next = curfsnode; if (curfsnode->type == S_IFDIR) { /* for dirs, make "." entry as well */ curfsnode->child = create_fsnode(".", ".", ".", &stbuf); curfsnode->child->parent = curfsnode; curfsnode->child->first = curfsnode->child; } if (curfsnode->type == S_IFLNK) { assert(curnode->slink != NULL); /* for symlinks, copy the target */ - if ((curfsnode->symlink = - strdup(curnode->slink)) == NULL) - err(1, "Memory allocation error"); + curfsnode->symlink = estrdup(curnode->slink); } } apply_specentry(dir, curnode, curfsnode); if (curnode->type == F_DIR) { if (curfsnode->type != S_IFDIR) errx(1, "`%s' is not a directory", path); assert (curfsnode->child != NULL); apply_specdir(path, curnode, curfsnode->child, speconly); } } } static void apply_specentry(const char *dir, NODE *specnode, fsnode *dirnode) { assert(specnode != NULL); assert(dirnode != NULL); if (nodetoino(specnode->type) != dirnode->type) errx(1, "`%s/%s' type mismatch: specfile %s, tree %s", dir, specnode->name, inode_type(nodetoino(specnode->type)), inode_type(dirnode->type)); if (debug & DEBUG_APPLY_SPECENTRY) printf("apply_specentry: %s/%s\n", dir, dirnode->name); #define ASEPRINT(t, b, o, n) \ if (debug & DEBUG_APPLY_SPECENTRY) \ printf("\t\t\tchanging %s from " b " to " b "\n", \ t, o, n) if (specnode->flags & (F_GID | F_GNAME)) { ASEPRINT("gid", "%d", dirnode->inode->st.st_gid, specnode->st_gid); dirnode->inode->st.st_gid = specnode->st_gid; } if (specnode->flags & F_MODE) { ASEPRINT("mode", "%#o", dirnode->inode->st.st_mode & ALLPERMS, specnode->st_mode); dirnode->inode->st.st_mode &= ~ALLPERMS; dirnode->inode->st.st_mode |= (specnode->st_mode & ALLPERMS); } /* XXX: ignoring F_NLINK for now */ if (specnode->flags & F_SIZE) { ASEPRINT("size", "%lld", (long long)dirnode->inode->st.st_size, (long long)specnode->st_size); dirnode->inode->st.st_size = specnode->st_size; } if (specnode->flags & F_SLINK) { assert(dirnode->symlink != NULL); assert(specnode->slink != NULL); ASEPRINT("symlink", "%s", dirnode->symlink, specnode->slink); free(dirnode->symlink); - if ((dirnode->symlink = strdup(specnode->slink)) == NULL) - err(1, "Memory allocation error"); + dirnode->symlink = estrdup(specnode->slink); } if (specnode->flags & F_TIME) { ASEPRINT("time", "%ld", (long)dirnode->inode->st.st_mtime, (long)specnode->st_mtimespec.tv_sec); dirnode->inode->st.st_mtime = specnode->st_mtimespec.tv_sec; dirnode->inode->st.st_atime = specnode->st_mtimespec.tv_sec; dirnode->inode->st.st_ctime = start_time.tv_sec; #if HAVE_STRUCT_STAT_ST_MTIMENSEC dirnode->inode->st.st_mtimensec = specnode->st_mtimespec.tv_nsec; dirnode->inode->st.st_atimensec = specnode->st_mtimespec.tv_nsec; dirnode->inode->st.st_ctimensec = start_time.tv_nsec; #endif } if (specnode->flags & (F_UID | F_UNAME)) { ASEPRINT("uid", "%d", dirnode->inode->st.st_uid, specnode->st_uid); dirnode->inode->st.st_uid = specnode->st_uid; } #if HAVE_STRUCT_STAT_ST_FLAGS if (specnode->flags & F_FLAGS) { ASEPRINT("flags", "%#lX", (unsigned long)dirnode->inode->st.st_flags, (unsigned long)specnode->st_flags); dirnode->inode->st.st_flags = specnode->st_flags; } #endif /* if (specnode->flags & F_DEV) { ASEPRINT("rdev", "%#llx", (unsigned long long)dirnode->inode->st.st_rdev, (unsigned long long)specnode->st_rdev); dirnode->inode->st.st_rdev = specnode->st_rdev; }*/ #undef ASEPRINT dirnode->flags |= FSNODE_F_HASSPEC; } /* * dump_fsnodes -- * dump the fsnodes from `cur' */ void dump_fsnodes(fsnode *root) { fsnode *cur; char path[MAXPATHLEN + 1]; printf("dump_fsnodes: %s %p\n", root->path, root); for (cur = root; cur != NULL; cur = cur->next) { if (snprintf(path, sizeof(path), "%s/%s", cur->path, cur->name) >= (int)sizeof(path)) errx(1, "Pathname too long."); if (debug & DEBUG_DUMP_FSNODES_VERBOSE) printf("cur=%8p parent=%8p first=%8p ", cur, cur->parent, cur->first); printf("%7s: %s", inode_type(cur->type), path); if (S_ISLNK(cur->type)) { assert(cur->symlink != NULL); printf(" -> %s", cur->symlink); } else { assert (cur->symlink == NULL); } if (cur->inode->nlink > 1) printf(", nlinks=%d", cur->inode->nlink); putchar('\n'); if (cur->child) { assert (cur->type == S_IFDIR); dump_fsnodes(cur->child); } } printf("dump_fsnodes: finished %s/%s\n", root->path, root->name); } /* * inode_type -- * for a given inode type `mode', return a descriptive string. * for most cases, uses inotype() from mtree/misc.c */ const char * inode_type(mode_t mode) { if (S_ISREG(mode)) return ("file"); if (S_ISLNK(mode)) return ("symlink"); if (S_ISDIR(mode)) return ("dir"); if (S_ISLNK(mode)) return ("link"); if (S_ISFIFO(mode)) return ("fifo"); if (S_ISSOCK(mode)) return ("socket"); /* XXX should not happen but handle them */ if (S_ISCHR(mode)) return ("char"); if (S_ISBLK(mode)) return ("block"); return ("unknown"); } /* * link_check -- * return pointer to fsinode matching `entry's st_ino & st_dev if it exists, * otherwise add `entry' to table and return NULL */ /* This was borrowed from du.c and tweaked to keep an fsnode * pointer instead. -- dbj@netbsd.org */ fsinode * link_check(fsinode *entry) { static struct entry { fsinode *data; } *htable; static int htshift; /* log(allocated size) */ static int htmask; /* allocated size - 1 */ static int htused; /* 2*number of insertions */ int h, h2; uint64_t tmp; /* this constant is (1<<64)/((1+sqrt(5))/2) * aka (word size)/(golden ratio) */ const uint64_t HTCONST = 11400714819323198485ULL; const int HTBITS = 64; /* Never store zero in hashtable */ assert(entry); /* Extend hash table if necessary, keep load under 0.5 */ if (htused<<1 >= htmask) { struct entry *ohtable; if (!htable) htshift = 10; /* starting hashtable size */ else htshift++; /* exponential hashtable growth */ htmask = (1 << htshift) - 1; htused = 0; ohtable = htable; - htable = calloc(htmask+1, sizeof(*htable)); - if (!htable) - err(1, "Memory allocation error"); - + htable = ecalloc(htmask+1, sizeof(*htable)); /* populate newly allocated hashtable */ if (ohtable) { int i; for (i = 0; i <= htmask>>1; i++) if (ohtable[i].data) link_check(ohtable[i].data); free(ohtable); } } /* multiplicative hashing */ tmp = entry->st.st_dev; tmp <<= HTBITS>>1; tmp |= entry->st.st_ino; tmp *= HTCONST; h = tmp >> (HTBITS - htshift); h2 = 1 | ( tmp >> (HTBITS - (htshift<<1) - 1)); /* must be odd */ /* open address hashtable search with double hash probing */ while (htable[h].data) { if ((htable[h].data->st.st_ino == entry->st.st_ino) && (htable[h].data->st.st_dev == entry->st.st_dev)) { return htable[h].data; } h = (h + h2) & htmask; } /* Insert the current entry into hashtable */ htable[h].data = entry; htused++; return NULL; } Index: stable/11 =================================================================== --- stable/11 (revision 332980) +++ stable/11 (revision 332981) Property changes on: stable/11 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head:r316579