Home | History | Annotate | Line # | Download | only in makefs
udf.c revision 1.24
      1  1.24       wiz /* $NetBSD: udf.c,v 1.24 2022/04/06 13:44:25 wiz Exp $ */
      2   1.1   reinoud 
      3   1.1   reinoud /*
      4  1.23   reinoud  * Copyright (c) 2006, 2008, 2013, 2021, 2022 Reinoud Zandijk
      5   1.1   reinoud  * All rights reserved.
      6   1.1   reinoud  *
      7   1.1   reinoud  * Redistribution and use in source and binary forms, with or without
      8   1.1   reinoud  * modification, are permitted provided that the following conditions
      9   1.1   reinoud  * are met:
     10   1.1   reinoud  * 1. Redistributions of source code must retain the above copyright
     11   1.1   reinoud  *    notice, this list of conditions and the following disclaimer.
     12   1.1   reinoud  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1   reinoud  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   reinoud  *    documentation and/or other materials provided with the distribution.
     15   1.1   reinoud  *
     16   1.1   reinoud  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1   reinoud  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1   reinoud  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1   reinoud  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1   reinoud  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21   1.1   reinoud  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22   1.1   reinoud  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23   1.1   reinoud  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24   1.1   reinoud  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25   1.1   reinoud  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26   1.1   reinoud  *
     27   1.1   reinoud  */
     28   1.3     joerg #if HAVE_NBTOOL_CONFIG_H
     29   1.3     joerg #include "nbtool_config.h"
     30   1.3     joerg #endif
     31   1.1   reinoud 
     32   1.1   reinoud #include <sys/cdefs.h>
     33  1.24       wiz __RCSID("$NetBSD: udf.c,v 1.24 2022/04/06 13:44:25 wiz Exp $");
     34   1.1   reinoud 
     35   1.1   reinoud #include <stdio.h>
     36   1.1   reinoud #include <stdlib.h>
     37   1.1   reinoud #include <string.h>
     38   1.1   reinoud #include <errno.h>
     39   1.1   reinoud #include <time.h>
     40   1.1   reinoud #include <assert.h>
     41   1.1   reinoud #include <err.h>
     42   1.1   reinoud #include <unistd.h>
     43   1.1   reinoud #include <fcntl.h>
     44  1.23   reinoud #include <math.h>
     45   1.1   reinoud #include <sys/types.h>
     46   1.1   reinoud #include <sys/param.h>
     47   1.4   reinoud #include <sys/stat.h>
     48   1.4   reinoud #include <util.h>
     49   1.4   reinoud 
     50   1.3     joerg #if !HAVE_NBTOOL_CONFIG_H
     51   1.4   reinoud #define _EXPOSE_MMC
     52   1.1   reinoud #include <sys/cdio.h>
     53   1.4   reinoud #else
     54   1.4   reinoud #include "udf/cdio_mmc_structs.h"
     55   1.3     joerg #endif
     56   1.1   reinoud 
     57  1.13  jmcneill #if !HAVE_NBTOOL_CONFIG_H
     58  1.13  jmcneill #define HAVE_STRUCT_TM_TM_GMTOFF
     59  1.13  jmcneill #endif
     60  1.13  jmcneill 
     61   1.1   reinoud #include "makefs.h"
     62  1.23   reinoud #include "udf_core.h"
     63   1.1   reinoud #include "newfs_udf.h"
     64   1.1   reinoud 
     65  1.23   reinoud /* identification */
     66  1.23   reinoud #define IMPL_NAME		"*NetBSD makefs 10.0"
     67  1.23   reinoud #define APP_VERSION_MAIN	0
     68  1.23   reinoud #define APP_VERSION_SUB		5
     69   1.1   reinoud 
     70   1.1   reinoud /*
     71   1.1   reinoud  * Note: due to the setup of the newfs code, the current state of the program
     72  1.22    andvar  * and its options are held in a few global variables. The FS specific parts
     73  1.23   reinoud  * are in global `context' and 'layout' structures.
     74   1.1   reinoud  */
     75   1.1   reinoud 
     76   1.1   reinoud /* global variables describing disc and format requests */
     77   1.1   reinoud int	 req_enable, req_disable;
     78   1.1   reinoud 
     79   1.1   reinoud 
     80   1.1   reinoud /* --------------------------------------------------------------------- */
     81   1.1   reinoud 
     82   1.1   reinoud static int
     83  1.23   reinoud udf_readonly_format(void)
     84   1.1   reinoud {
     85  1.23   reinoud 	/*
     86  1.23   reinoud 	 * we choose the emulated profile to determine this since the media
     87  1.23   reinoud 	 * might be different from the format we create. Say creating a CDROM
     88  1.23   reinoud 	 * on a CD-R media.
     89  1.23   reinoud 	 */
     90  1.23   reinoud 	switch (emul_mmc_profile) {
     91   1.1   reinoud 	case 0x00:	/* unknown, treat as CDROM */
     92   1.1   reinoud 	case 0x08:	/* CDROM */
     93   1.7   reinoud 	case 0x10:	/* DVDROM */
     94   1.7   reinoud 	case 0x40:	/* BDROM */
     95  1.23   reinoud 		return true;
     96   1.1   reinoud 	}
     97  1.23   reinoud 	return false;
     98   1.1   reinoud }
     99   1.1   reinoud 
    100   1.1   reinoud 
    101   1.1   reinoud #define OPT_STR(letter, name, desc)  \
    102   1.1   reinoud 	{ letter, name, NULL, OPT_STRBUF, 0, 0, desc }
    103   1.1   reinoud 
    104   1.1   reinoud #define OPT_NUM(letter, name, field, min, max, desc) \
    105   1.9   reinoud 	{ letter, name, &context.field, \
    106   1.9   reinoud 	  sizeof(context.field) == 8 ? OPT_INT64 : \
    107   1.9   reinoud 	  (sizeof(context.field) == 4 ? OPT_INT32 : \
    108   1.9   reinoud 	  (sizeof(context.field) == 2 ? OPT_INT16 : OPT_INT8)), \
    109   1.1   reinoud 	  min, max, desc }
    110   1.1   reinoud 
    111   1.1   reinoud #define OPT_BOOL(letter, name, field, desc) \
    112   1.1   reinoud 	OPT_NUM(letter, name, field, 0, 1, desc)
    113   1.1   reinoud 
    114   1.1   reinoud void
    115   1.1   reinoud udf_prep_opts(fsinfo_t *fsopts)
    116   1.1   reinoud {
    117   1.1   reinoud 	const option_t udf_options[] = {
    118   1.8   reinoud 		OPT_STR('T', "disctype", "disc type (cdrom,dvdrom,bdrom,"
    119   1.8   reinoud 			"dvdram,bdre,disk,cdr,dvdr,bdr,cdrw,dvdrw)"),
    120   1.8   reinoud 		OPT_STR('L', "loglabel", "\"logical volume name\""),
    121   1.9   reinoud 		OPT_STR('P', "discid",   "\"[volset name ':']"
    122   1.9   reinoud 			"physical volume name\""),
    123   1.9   reinoud 		OPT_NUM('t', "tz", gmtoff, -24, 24, "timezone"),
    124   1.9   reinoud 		OPT_STR('v', "minver", "minimum UDF version in either "
    125   1.9   reinoud 			"``0x201'' or ``2.01'' format"),
    126   1.9   reinoud 		OPT_STR('V', "maxver", "maximum UDF version in either "
    127   1.9   reinoud 			"``0x201'' or ``2.01'' format"),
    128  1.23   reinoud 		OPT_NUM('p', "metaperc", meta_perc, 1, 99,
    129  1.23   reinoud 			"minimum free metadata percentage"),
    130  1.23   reinoud 		OPT_BOOL('c', "checksurface", check_surface,
    131  1.23   reinoud 			"perform crude surface check on rewritable media"),
    132  1.23   reinoud 		OPT_BOOL('F', "forceformat", create_new_session,
    133  1.24       wiz 			"force file system construction on non-empty recordable media"),
    134   1.1   reinoud 		{ .name = NULL }
    135   1.1   reinoud 	};
    136   1.1   reinoud 
    137   1.1   reinoud 	/* initialise */
    138  1.23   reinoud 	req_enable = req_disable = 0;
    139   1.1   reinoud 	fsopts->sectorsize = 512;	/* minimum allowed sector size */
    140   1.1   reinoud 
    141   1.1   reinoud 	srandom((unsigned long) time(NULL));
    142   1.1   reinoud 
    143   1.1   reinoud 	udf_init_create_context();
    144  1.23   reinoud 	context.app_name         = "*NetBSD UDF";
    145   1.1   reinoud 	context.app_version_main = APP_VERSION_MAIN;
    146   1.1   reinoud 	context.app_version_sub  = APP_VERSION_SUB;
    147  1.10   reinoud 	context.impl_name        = IMPL_NAME;
    148   1.1   reinoud 
    149   1.1   reinoud 	/* minimum and maximum UDF versions we advise */
    150   1.1   reinoud 	context.min_udf = 0x102;
    151  1.23   reinoud 	context.max_udf = 0x250;	/* 0x260 is not ready */
    152  1.23   reinoud 
    153  1.23   reinoud 	/* defaults for disc/files */
    154  1.23   reinoud 	emul_mmc_profile  =  -1;	/* invalid->no emulation	*/
    155  1.23   reinoud 	emul_packetsize   =   1;	/* reasonable default		*/
    156  1.23   reinoud 	emul_sectorsize   = 512;	/* minimum allowed sector size	*/
    157  1.23   reinoud 	emul_size	  =   0;	/* empty			*/
    158   1.1   reinoud 
    159   1.1   reinoud 	/* use user's time zone as default */
    160  1.13  jmcneill #ifdef HAVE_STRUCT_TM_TM_GMTOFF
    161  1.18  christos 	if (!stampst.st_ino)  {
    162  1.18  christos 		struct tm tm;
    163  1.18  christos 		time_t now;
    164  1.18  christos 		(void)time(&now);
    165  1.18  christos 		(void)localtime_r(&now, &tm);
    166  1.18  christos 		context.gmtoff = tm.tm_gmtoff;
    167  1.18  christos 	} else
    168  1.13  jmcneill #endif
    169  1.18  christos 		context.gmtoff = 0;
    170   1.1   reinoud 
    171   1.1   reinoud 	/* return info */
    172   1.1   reinoud 	fsopts->fs_specific = NULL;
    173   1.1   reinoud 	fsopts->fs_options = copy_opts(udf_options);
    174   1.1   reinoud }
    175   1.1   reinoud 
    176   1.1   reinoud 
    177   1.1   reinoud void
    178   1.1   reinoud udf_cleanup_opts(fsinfo_t *fsopts)
    179   1.1   reinoud {
    180   1.1   reinoud 	free(fsopts->fs_options);
    181   1.1   reinoud }
    182   1.1   reinoud 
    183   1.1   reinoud 
    184   1.9   reinoud /* ----- included from newfs_udf.c ------ */
    185   1.9   reinoud 
    186   1.7   reinoud #define CDRSIZE    ((uint64_t)   700*1024*1024)	/* small approx */
    187   1.7   reinoud #define CDRWSIZE   ((uint64_t)   576*1024*1024)	/* small approx */
    188   1.7   reinoud #define DVDRSIZE   ((uint64_t)  4488*1024*1024)	/* small approx */
    189   1.7   reinoud #define DVDRAMSIZE ((uint64_t)  4330*1024*1024)	/* small approx with spare */
    190   1.7   reinoud #define DVDRWSIZE  ((uint64_t)  4482*1024*1024)	/* small approx */
    191   1.7   reinoud #define BDRSIZE    ((uint64_t) 23866*1024*1024)	/* small approx */
    192   1.7   reinoud #define BDRESIZE   ((uint64_t) 23098*1024*1024)	/* small approx */
    193   1.1   reinoud int
    194   1.1   reinoud udf_parse_opts(const char *option, fsinfo_t *fsopts)
    195   1.1   reinoud {
    196   1.1   reinoud 	option_t *udf_options = fsopts->fs_options;
    197  1.23   reinoud 	uint64_t stdsize, maxsize;
    198   1.7   reinoud 	uint32_t set_sectorsize;
    199   1.8   reinoud 	char buffer[1024], *buf, *colon;
    200   1.1   reinoud 	int i;
    201   1.1   reinoud 
    202   1.1   reinoud 	assert(option != NULL);
    203   1.1   reinoud 
    204   1.1   reinoud 	if (debug & DEBUG_FS_PARSE_OPTS)
    205   1.1   reinoud 		printf("udf_parse_opts: got `%s'\n", option);
    206   1.1   reinoud 
    207   1.8   reinoud 	i = set_option(udf_options, option, buffer, sizeof(buffer));
    208   1.1   reinoud 	if (i == -1)
    209   1.1   reinoud 		return 0;
    210   1.1   reinoud 
    211   1.1   reinoud 	if (udf_options[i].name == NULL)
    212   1.1   reinoud 		abort();
    213   1.1   reinoud 
    214   1.7   reinoud 	set_sectorsize = 0;
    215   1.7   reinoud 	stdsize = 0;
    216  1.23   reinoud 	maxsize = 0;
    217   1.7   reinoud 
    218   1.8   reinoud 	buf = buffer;
    219   1.1   reinoud 	switch (udf_options[i].letter) {
    220   1.1   reinoud 	case 'T':
    221   1.1   reinoud 		if (strcmp(buf, "cdrom") == 0) {
    222  1.23   reinoud 			emul_mmc_profile = 0x00;
    223  1.23   reinoud 			maxsize = CDRSIZE;
    224   1.1   reinoud 		} else if (strcmp(buf, "dvdrom") == 0) {
    225  1.23   reinoud 			emul_mmc_profile = 0x10;
    226  1.23   reinoud 			maxsize = DVDRSIZE;
    227   1.7   reinoud 		} else if (strcmp(buf, "bdrom") == 0) {
    228  1.23   reinoud 			emul_mmc_profile = 0x40;
    229  1.23   reinoud 			maxsize = BDRSIZE;
    230   1.1   reinoud 		} else if (strcmp(buf, "dvdram") == 0) {
    231  1.23   reinoud 			emul_mmc_profile = 0x12;
    232   1.7   reinoud 			stdsize = DVDRAMSIZE;
    233   1.1   reinoud 		} else if (strcmp(buf, "bdre") == 0) {
    234  1.23   reinoud 			emul_mmc_profile = 0x43;
    235   1.7   reinoud 			stdsize = BDRESIZE;
    236   1.1   reinoud 		} else if (strcmp(buf, "disk") == 0) {
    237  1.23   reinoud 			emul_mmc_profile = 0x01;
    238   1.1   reinoud 		} else if (strcmp(buf, "cdr") == 0) {
    239  1.23   reinoud 			emul_mmc_profile = 0x09;
    240   1.7   reinoud 			stdsize = CDRSIZE;
    241   1.1   reinoud 		} else if (strcmp(buf, "dvdr") == 0) {
    242  1.23   reinoud 			emul_mmc_profile = 0x1b;
    243   1.7   reinoud 			stdsize = DVDRSIZE;
    244   1.7   reinoud 		} else if (strcmp(buf, "bdr") == 0) {
    245  1.23   reinoud 			emul_mmc_profile = 0x41;
    246   1.7   reinoud 			stdsize = BDRSIZE;
    247   1.1   reinoud 		} else if (strcmp(buf, "cdrw") == 0) {
    248  1.23   reinoud 			emul_mmc_profile = 0x0a;
    249   1.7   reinoud 			stdsize = CDRWSIZE;
    250   1.1   reinoud 		} else if (strcmp(buf, "dvdrw") == 0) {
    251  1.23   reinoud 			emul_mmc_profile = 0x1a;
    252   1.7   reinoud 			stdsize = DVDRWSIZE;
    253   1.1   reinoud 		} else {
    254  1.23   reinoud 			errx(1, "unknown or unimplemented disc format");
    255   1.1   reinoud 		}
    256  1.23   reinoud 		if (emul_mmc_profile != 0x01)
    257   1.7   reinoud 			set_sectorsize = 2048;
    258   1.8   reinoud 		break;
    259   1.8   reinoud 	case 'L':
    260   1.8   reinoud 		if (context.logvol_name) free(context.logvol_name);
    261   1.8   reinoud 		context.logvol_name = strdup(buf);
    262   1.8   reinoud 		break;
    263   1.8   reinoud 	case 'P':
    264   1.8   reinoud 		if ((colon = strstr(buf, ":"))) {
    265   1.8   reinoud 			if (context.volset_name)
    266   1.8   reinoud 				free(context.volset_name);
    267   1.8   reinoud 			*colon = 0;
    268   1.8   reinoud 			context.volset_name = strdup(buf);
    269   1.8   reinoud 			buf = colon+1;
    270   1.8   reinoud 		}
    271   1.8   reinoud 		if (context.primary_name)
    272   1.8   reinoud 			free(context.primary_name);
    273  1.23   reinoud 		if ((strstr(buf, ":")))
    274  1.23   reinoud 			errx(1, "primary name can't have ':' in its name");
    275   1.8   reinoud 		context.primary_name = strdup(buf);
    276   1.8   reinoud 		break;
    277   1.9   reinoud 	case 'v':
    278   1.9   reinoud 		context.min_udf = a_udf_version(buf, "min_udf");
    279  1.23   reinoud 		if (context.min_udf > 0x250)
    280  1.23   reinoud 			errx(1, "maximum supported version is UDF 2.50");
    281   1.9   reinoud 		if (context.min_udf > context.max_udf)
    282   1.9   reinoud 			context.max_udf = context.min_udf;
    283   1.9   reinoud 		break;
    284  1.23   reinoud 	case 'V':
    285  1.23   reinoud 		context.max_udf = a_udf_version(buf, "min_udf");
    286  1.23   reinoud 		if (context.max_udf > 0x250)
    287  1.23   reinoud 			errx(1, "maximum supported version is UDF 2.50");
    288  1.23   reinoud 		if (context.min_udf > context.max_udf)
    289  1.23   reinoud 			context.min_udf = context.max_udf;
    290  1.23   reinoud 		break;
    291   1.1   reinoud 	}
    292   1.7   reinoud 	if (set_sectorsize)
    293   1.7   reinoud 		fsopts->sectorsize = set_sectorsize;
    294  1.23   reinoud 	if (stdsize) {
    295  1.23   reinoud 		if (fsopts->maxsize > 0)
    296  1.23   reinoud 			stdsize = MIN(stdsize, (uint64_t) fsopts->maxsize);
    297  1.23   reinoud 		if (fsopts->minsize > 0)
    298  1.23   reinoud 			stdsize = MAX(stdsize, (uint64_t) fsopts->minsize);
    299  1.23   reinoud 		fsopts->size = fsopts->minsize = fsopts->maxsize = stdsize;
    300  1.23   reinoud 	}
    301  1.23   reinoud 	if (maxsize) {
    302  1.23   reinoud 		if (fsopts->maxsize > 0)
    303  1.23   reinoud 			maxsize = MIN(maxsize, (uint64_t) fsopts->maxsize);
    304  1.23   reinoud 		if (fsopts->minsize > 0)
    305  1.23   reinoud 			maxsize = MAX(maxsize, (uint64_t) fsopts->minsize);
    306  1.23   reinoud 		fsopts->maxsize = maxsize;
    307  1.23   reinoud 	}
    308   1.1   reinoud 	return 1;
    309   1.1   reinoud }
    310   1.1   reinoud 
    311  1.23   reinoud /* -
    312  1.23   reinoud  * -------------------------------------------------------------------- */
    313   1.1   reinoud 
    314   1.1   reinoud struct udf_stats {
    315   1.1   reinoud 	uint32_t nfiles;
    316   1.1   reinoud 	uint32_t ndirs;
    317   1.1   reinoud 	uint32_t ndescr;
    318   1.1   reinoud 	uint32_t nmetadatablocks;
    319   1.1   reinoud 	uint32_t ndatablocks;
    320   1.1   reinoud };
    321   1.1   reinoud 
    322   1.1   reinoud 
    323   1.1   reinoud /* node reference administration */
    324   1.1   reinoud static void
    325   1.1   reinoud udf_inc_link(union dscrptr *dscr)
    326   1.1   reinoud {
    327   1.1   reinoud 	struct file_entry *fe;
    328   1.1   reinoud 	struct extfile_entry *efe;
    329   1.1   reinoud 
    330   1.1   reinoud 	if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
    331   1.1   reinoud 		fe        = &dscr->fe;
    332   1.1   reinoud 		fe->link_cnt = udf_rw16(udf_rw16(fe->link_cnt) + 1);
    333   1.1   reinoud 	} else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
    334   1.1   reinoud 		efe       = &dscr->efe;
    335   1.1   reinoud 		efe->link_cnt = udf_rw16(udf_rw16(efe->link_cnt) + 1);
    336   1.1   reinoud 	} else {
    337  1.23   reinoud 		errx(1, "bad tag passed to udf_inc_link");
    338   1.1   reinoud 	}
    339   1.1   reinoud }
    340   1.1   reinoud 
    341   1.1   reinoud 
    342   1.1   reinoud static void
    343   1.1   reinoud udf_set_link_cnt(union dscrptr *dscr, int num)
    344   1.1   reinoud {
    345   1.1   reinoud 	struct file_entry *fe;
    346   1.1   reinoud 	struct extfile_entry *efe;
    347   1.1   reinoud 
    348   1.1   reinoud 	if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
    349   1.1   reinoud 		fe        = &dscr->fe;
    350   1.1   reinoud 		fe->link_cnt = udf_rw16(num);
    351   1.1   reinoud 	} else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
    352   1.1   reinoud 		efe       = &dscr->efe;
    353   1.1   reinoud 		efe->link_cnt = udf_rw16(num);
    354   1.1   reinoud 	} else {
    355  1.23   reinoud 		errx(1, "bad tag passed to udf_set_link_cnt");
    356   1.1   reinoud 	}
    357   1.1   reinoud }
    358   1.1   reinoud 
    359   1.1   reinoud 
    360   1.1   reinoud static uint32_t
    361   1.1   reinoud udf_datablocks(off_t sz)
    362   1.1   reinoud {
    363   1.1   reinoud 	/* predictor if it can be written inside the node */
    364  1.20   reinoud 	/* XXX the predictor assumes NO extended attributes in the node */
    365   1.1   reinoud 	if (sz < context.sector_size - UDF_EXTFENTRY_SIZE - 16)
    366   1.1   reinoud 		return 0;
    367   1.1   reinoud 
    368   1.1   reinoud 	return UDF_ROUNDUP(sz, context.sector_size) / context.sector_size;
    369   1.1   reinoud }
    370   1.1   reinoud 
    371   1.1   reinoud 
    372   1.1   reinoud static void
    373   1.1   reinoud udf_prepare_fids(struct long_ad *dir_icb, struct long_ad *dirdata_icb,
    374   1.1   reinoud 		uint8_t *dirdata, uint32_t dirdata_size)
    375   1.1   reinoud {
    376   1.1   reinoud 	struct fileid_desc *fid;
    377   1.1   reinoud 	struct long_ad     *icb;
    378   1.1   reinoud 	uint32_t fidsize, offset;
    379   1.1   reinoud 	uint32_t location;
    380   1.1   reinoud 
    381   1.1   reinoud 	if (udf_datablocks(dirdata_size) == 0) {
    382   1.1   reinoud 		/* going internal */
    383   1.1   reinoud 		icb = dir_icb;
    384   1.1   reinoud 	} else {
    385   1.1   reinoud 		/* external blocks to write to */
    386   1.1   reinoud 		icb = dirdata_icb;
    387   1.1   reinoud 	}
    388   1.1   reinoud 
    389   1.1   reinoud 	for (offset = 0; offset < dirdata_size; offset += fidsize) {
    390   1.1   reinoud 		/* for each FID: */
    391   1.1   reinoud 		fid = (struct fileid_desc *) (dirdata + offset);
    392   1.1   reinoud 		assert(udf_rw16(fid->tag.id) == TAGID_FID);
    393   1.1   reinoud 
    394   1.1   reinoud 		location  = udf_rw32(icb->loc.lb_num);
    395   1.1   reinoud 		location += offset / context.sector_size;
    396   1.1   reinoud 
    397   1.1   reinoud 		fid->tag.tag_loc = udf_rw32(location);
    398   1.1   reinoud 		udf_validate_tag_and_crc_sums((union dscrptr *) fid);
    399   1.1   reinoud 
    400   1.1   reinoud 		fidsize = udf_fidsize(fid);
    401   1.1   reinoud 	}
    402   1.1   reinoud }
    403   1.1   reinoud 
    404  1.23   reinoud 
    405   1.1   reinoud static int
    406  1.11   reinoud udf_file_inject_blob(union dscrptr *dscr,  uint8_t *blob, off_t size)
    407   1.1   reinoud {
    408   1.1   reinoud 	struct icb_tag *icb;
    409   1.1   reinoud 	struct file_entry *fe;
    410   1.1   reinoud 	struct extfile_entry *efe;
    411   1.1   reinoud 	uint64_t inf_len, obj_size;
    412   1.1   reinoud 	uint32_t l_ea, l_ad;
    413   1.1   reinoud 	uint16_t crclen;
    414   1.1   reinoud 	uint8_t *data, *pos;
    415   1.1   reinoud 
    416   1.1   reinoud 	fe = NULL;
    417   1.1   reinoud 	efe = NULL;
    418   1.1   reinoud 	if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
    419   1.1   reinoud 		fe        = &dscr->fe;
    420   1.1   reinoud 		data      = fe->data;
    421   1.1   reinoud 		l_ea      = udf_rw32(fe->l_ea);
    422   1.1   reinoud 		l_ad      = udf_rw32(fe->l_ad);
    423   1.1   reinoud 		icb       = &fe->icbtag;
    424   1.1   reinoud 		inf_len   = udf_rw64(fe->inf_len);
    425   1.1   reinoud 		obj_size  = 0;
    426   1.1   reinoud 	} else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
    427   1.1   reinoud 		efe       = &dscr->efe;
    428   1.1   reinoud 		data      = efe->data;
    429   1.1   reinoud 		l_ea      = udf_rw32(efe->l_ea);
    430   1.1   reinoud 		l_ad      = udf_rw32(efe->l_ad);
    431   1.1   reinoud 		icb       = &efe->icbtag;
    432   1.1   reinoud 		inf_len   = udf_rw64(efe->inf_len);
    433   1.1   reinoud 		obj_size  = udf_rw64(efe->obj_size);
    434   1.1   reinoud 	} else {
    435  1.23   reinoud 		errx(1, "bad tag passed to udf_file_inject_blob");
    436   1.1   reinoud 	}
    437   1.1   reinoud 	crclen = udf_rw16(dscr->tag.desc_crc_len);
    438   1.1   reinoud 
    439  1.23   reinoud 	/* check if we can go internal */
    440  1.23   reinoud 	if ((udf_rw16(icb->flags) & UDF_ICB_TAG_FLAGS_ALLOC_MASK) !=
    441  1.23   reinoud 			UDF_ICB_INTERN_ALLOC)
    442  1.23   reinoud 		return 1;
    443  1.23   reinoud 
    444  1.20   reinoud 	/* check if it will fit internally */
    445   1.1   reinoud 	if (udf_datablocks(size)) {
    446  1.20   reinoud 		/* the predictor tells it won't fit internally */
    447   1.1   reinoud 		return 1;
    448   1.1   reinoud 	}
    449   1.1   reinoud 
    450   1.1   reinoud 	/* going internal */
    451   1.6   reinoud 	assert((udf_rw16(icb->flags) & UDF_ICB_TAG_FLAGS_ALLOC_MASK) ==
    452   1.6   reinoud 			UDF_ICB_INTERN_ALLOC);
    453  1.23   reinoud 	assert(l_ad == 0);
    454   1.1   reinoud 
    455   1.1   reinoud 	pos = data + l_ea + l_ad;
    456   1.1   reinoud 	memcpy(pos, blob, size);
    457   1.1   reinoud 	l_ad   += size;
    458   1.1   reinoud 	crclen += size;
    459   1.1   reinoud 
    460   1.1   reinoud 	inf_len  += size;
    461   1.1   reinoud 	obj_size += size;
    462   1.1   reinoud 
    463   1.1   reinoud 	if (fe) {
    464   1.1   reinoud 		fe->l_ad = udf_rw32(l_ad);
    465   1.1   reinoud 		fe->inf_len = udf_rw64(inf_len);
    466   1.1   reinoud 	} else if (efe) {
    467   1.1   reinoud 		efe->l_ad = udf_rw32(l_ad);
    468   1.1   reinoud 		efe->inf_len  = udf_rw64(inf_len);
    469   1.1   reinoud 		efe->obj_size = udf_rw64(inf_len);
    470   1.1   reinoud 	}
    471   1.1   reinoud 
    472   1.1   reinoud 	/* make sure the header sums stays correct */
    473   1.1   reinoud 	dscr->tag.desc_crc_len = udf_rw16(crclen);
    474   1.1   reinoud 	udf_validate_tag_and_crc_sums(dscr);
    475   1.1   reinoud 
    476   1.1   reinoud 	return 0;
    477   1.1   reinoud }
    478   1.1   reinoud 
    479   1.1   reinoud 
    480   1.1   reinoud /* XXX no sparse file support */
    481   1.1   reinoud static void
    482   1.1   reinoud udf_append_file_mapping(union dscrptr *dscr, struct long_ad *piece)
    483   1.1   reinoud {
    484   1.1   reinoud 	struct icb_tag *icb;
    485   1.1   reinoud 	struct file_entry *fe;
    486   1.1   reinoud 	struct extfile_entry *efe;
    487   1.1   reinoud 	struct long_ad *last_long, last_piece;
    488   1.1   reinoud 	struct short_ad *last_short, new_short;
    489   1.1   reinoud 	uint64_t inf_len, obj_size, logblks_rec;
    490   1.1   reinoud 	uint32_t l_ea, l_ad, size;
    491   1.1   reinoud 	uint32_t last_lb_num, piece_lb_num;
    492  1.12   reinoud 	uint64_t last_len, piece_len, last_flags;
    493  1.12   reinoud 	uint64_t rest_len, merge_len, last_end;
    494   1.1   reinoud 	uint16_t last_part_num, piece_part_num;
    495   1.1   reinoud 	uint16_t crclen, cur_alloc;
    496   1.1   reinoud 	uint8_t *data, *pos;
    497   1.1   reinoud 	const int short_len = sizeof(struct short_ad);
    498   1.1   reinoud 	const int long_len  = sizeof(struct long_ad);
    499   1.1   reinoud 	const int sector_size = context.sector_size;
    500   1.1   reinoud 	uint64_t max_len = UDF_ROUNDDOWN(UDF_EXT_MAXLEN, sector_size);
    501  1.23   reinoud 	int use_shorts;
    502   1.1   reinoud 
    503  1.11   reinoud 	fe  = NULL;
    504  1.11   reinoud 	efe = NULL;
    505   1.1   reinoud 	if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
    506   1.1   reinoud 		fe          = &dscr->fe;
    507  1.11   reinoud 		data        = fe->data;
    508   1.1   reinoud 		l_ea        = fe->l_ea;
    509   1.1   reinoud 		l_ad        = udf_rw32(fe->l_ad);
    510   1.1   reinoud 		icb         = &fe->icbtag;
    511   1.1   reinoud 		inf_len     = udf_rw64(fe->inf_len);
    512   1.1   reinoud 		logblks_rec = udf_rw64(fe->logblks_rec);
    513   1.1   reinoud 		obj_size = 0;
    514   1.1   reinoud 	} else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
    515   1.1   reinoud 		efe         = &dscr->efe;
    516   1.1   reinoud 		data        = efe->data;
    517   1.1   reinoud 		l_ea        = efe->l_ea;
    518   1.1   reinoud 		l_ad        = udf_rw32(efe->l_ad);
    519   1.1   reinoud 		icb         = &efe->icbtag;
    520   1.1   reinoud 		inf_len     = udf_rw64(efe->inf_len);
    521   1.1   reinoud 		obj_size    = udf_rw64(efe->obj_size);
    522   1.1   reinoud 		logblks_rec = udf_rw64(efe->logblks_rec);
    523   1.1   reinoud 	} else {
    524  1.23   reinoud 		errx(1, "bad tag passed to udf_file_append_blob");
    525   1.1   reinoud 	}
    526   1.1   reinoud 	crclen = udf_rw16(dscr->tag.desc_crc_len);
    527   1.1   reinoud 
    528  1.23   reinoud 	/* we use shorts if referring inside the metadata partition */
    529  1.23   reinoud 	use_shorts = (udf_rw16(piece->loc.part_num) == context.metadata_part);
    530  1.23   reinoud 
    531   1.1   reinoud 	pos = data + l_ea;
    532   1.1   reinoud 	cur_alloc = udf_rw16(icb->flags);
    533   1.1   reinoud 	size = UDF_EXT_LEN(udf_rw32(piece->len));
    534   1.1   reinoud 
    535   1.1   reinoud 	/* extract last entry as a long_ad */
    536   1.2   reinoud 	memset(&last_piece, 0, sizeof(last_piece));
    537   1.2   reinoud 	last_len      = 0;
    538   1.2   reinoud 	last_lb_num   = 0;
    539   1.2   reinoud 	last_part_num = 0;
    540  1.11   reinoud 	last_flags    = 0;
    541  1.11   reinoud 	last_short    = NULL;
    542  1.11   reinoud 	last_long     = NULL;
    543   1.1   reinoud 	if (l_ad != 0) {
    544   1.1   reinoud 		if (use_shorts) {
    545   1.1   reinoud 			assert(cur_alloc == UDF_ICB_SHORT_ALLOC);
    546   1.1   reinoud 			pos += l_ad - short_len;
    547   1.1   reinoud 			last_short   = (struct short_ad *) pos;
    548   1.1   reinoud 			last_lb_num  = udf_rw32(last_short->lb_num);
    549   1.1   reinoud 			last_part_num = udf_rw16(piece->loc.part_num);
    550   1.1   reinoud 			last_len     = UDF_EXT_LEN(udf_rw32(last_short->len));
    551   1.1   reinoud 			last_flags   = UDF_EXT_FLAGS(udf_rw32(last_short->len));
    552   1.1   reinoud 		} else {
    553   1.1   reinoud 			assert(cur_alloc == UDF_ICB_LONG_ALLOC);
    554   1.1   reinoud 			pos += l_ad - long_len;
    555   1.1   reinoud 			last_long    = (struct long_ad *) pos;
    556   1.1   reinoud 			last_lb_num  = udf_rw32(last_long->loc.lb_num);
    557   1.1   reinoud 			last_part_num = udf_rw16(last_long->loc.part_num);
    558   1.1   reinoud 			last_len     = UDF_EXT_LEN(udf_rw32(last_long->len));
    559   1.1   reinoud 			last_flags   = UDF_EXT_FLAGS(udf_rw32(last_long->len));
    560   1.1   reinoud 		}
    561   1.1   reinoud 	}
    562   1.1   reinoud 
    563   1.1   reinoud 	piece_len      = UDF_EXT_LEN(udf_rw32(piece->len));
    564   1.1   reinoud 	piece_lb_num   = udf_rw32(piece->loc.lb_num);
    565   1.1   reinoud 	piece_part_num = udf_rw16(piece->loc.part_num);
    566   1.1   reinoud 
    567   1.1   reinoud 	/* try merging */
    568   1.1   reinoud 	rest_len  = max_len - last_len;
    569   1.1   reinoud 
    570  1.12   reinoud 	merge_len = MIN(piece_len, rest_len);
    571   1.1   reinoud 	last_end  = last_lb_num + (last_len / sector_size);
    572   1.1   reinoud 	if ((piece_lb_num == last_end) && (last_part_num == piece_part_num)) {
    573   1.1   reinoud 		/* we can merge */
    574   1.1   reinoud 		last_len  += merge_len;
    575   1.1   reinoud 		piece_len -= merge_len;
    576   1.1   reinoud 
    577   1.1   reinoud 		/* write back merge result */
    578   1.1   reinoud 		if (use_shorts) {
    579   1.1   reinoud 			last_short->len = udf_rw32(last_len | last_flags);
    580   1.1   reinoud 		} else {
    581   1.1   reinoud 			last_long->len  = udf_rw32(last_len | last_flags);
    582   1.1   reinoud 		}
    583   1.1   reinoud 		piece_lb_num += merge_len / sector_size;
    584   1.1   reinoud 	}
    585   1.1   reinoud 
    586   1.1   reinoud 	if (piece_len) {
    587   1.1   reinoud 		/* append new entry */
    588   1.1   reinoud 		pos = data + l_ea + l_ad;
    589   1.1   reinoud 		if (use_shorts) {
    590   1.1   reinoud 			icb->flags = udf_rw16(UDF_ICB_SHORT_ALLOC);
    591   1.1   reinoud 			memset(&new_short, 0, short_len);
    592   1.1   reinoud 			new_short.len    = udf_rw32(piece_len);
    593   1.1   reinoud 			new_short.lb_num = udf_rw32(piece_lb_num);
    594   1.1   reinoud 			memcpy(pos, &new_short, short_len);
    595   1.1   reinoud 			l_ad += short_len;
    596   1.1   reinoud 			crclen += short_len;
    597   1.1   reinoud 		} else {
    598   1.1   reinoud 			icb->flags = udf_rw16(UDF_ICB_LONG_ALLOC);
    599   1.1   reinoud 			piece->len        = udf_rw32(piece_len);
    600   1.1   reinoud 			piece->loc.lb_num = udf_rw32(piece_lb_num);
    601   1.1   reinoud 			memcpy(pos, piece, long_len);
    602   1.1   reinoud 			l_ad += long_len;
    603   1.1   reinoud 			crclen += long_len;
    604   1.1   reinoud 		}
    605   1.1   reinoud 	}
    606   1.1   reinoud 	piece->len = udf_rw32(0);
    607   1.1   reinoud 
    608   1.1   reinoud 	inf_len  += size;
    609   1.1   reinoud 	obj_size += size;
    610   1.1   reinoud 	logblks_rec += UDF_ROUNDUP(size, sector_size) / sector_size;
    611   1.1   reinoud 
    612   1.1   reinoud 	dscr->tag.desc_crc_len = udf_rw16(crclen);
    613   1.1   reinoud 	if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
    614   1.1   reinoud 		fe->l_ad = udf_rw32(l_ad);
    615   1.1   reinoud 		fe->inf_len = udf_rw64(inf_len);
    616   1.1   reinoud 		fe->logblks_rec = udf_rw64(logblks_rec);
    617   1.1   reinoud 	} else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
    618   1.1   reinoud 		efe->l_ad = udf_rw32(l_ad);
    619   1.1   reinoud 		efe->inf_len  = udf_rw64(inf_len);
    620   1.1   reinoud 		efe->obj_size = udf_rw64(inf_len);
    621   1.1   reinoud 		efe->logblks_rec = udf_rw64(logblks_rec);
    622   1.1   reinoud 	}
    623   1.1   reinoud }
    624   1.1   reinoud 
    625   1.1   reinoud 
    626   1.1   reinoud static int
    627   1.1   reinoud udf_append_file_contents(union dscrptr *dscr, struct long_ad *data_icb,
    628  1.11   reinoud 		uint8_t *fdata, off_t flen)
    629   1.1   reinoud {
    630   1.1   reinoud 	struct long_ad icb;
    631   1.1   reinoud 	uint32_t location;
    632   1.1   reinoud 	uint16_t vpart;
    633  1.23   reinoud 	int sectors;
    634   1.1   reinoud 
    635   1.1   reinoud 	if (udf_file_inject_blob(dscr, fdata, flen) == 0)
    636   1.1   reinoud 		return 0;
    637   1.1   reinoud 
    638   1.1   reinoud 	/* has to be appended in mappings */
    639   1.1   reinoud 	icb = *data_icb;
    640   1.1   reinoud 	icb.len = udf_rw32(flen);
    641   1.1   reinoud 	while (udf_rw32(icb.len) > 0)
    642   1.1   reinoud 		udf_append_file_mapping(dscr, &icb);
    643   1.1   reinoud 	udf_validate_tag_and_crc_sums(dscr);
    644   1.1   reinoud 
    645   1.1   reinoud 	/* write out data piece */
    646   1.1   reinoud 	vpart    = udf_rw16(data_icb->loc.part_num);
    647   1.1   reinoud 	location = udf_rw32(data_icb->loc.lb_num);
    648  1.23   reinoud 	sectors  = udf_datablocks(flen);
    649  1.23   reinoud 
    650  1.23   reinoud 	return udf_write_virt(fdata, location, vpart, sectors);
    651   1.1   reinoud }
    652   1.1   reinoud 
    653   1.1   reinoud 
    654   1.1   reinoud static int
    655   1.1   reinoud udf_create_new_file(struct stat *st, union dscrptr **dscr,
    656   1.1   reinoud 	int filetype, struct long_ad *icb)
    657   1.1   reinoud {
    658   1.1   reinoud 	struct file_entry *fe;
    659   1.1   reinoud 	struct extfile_entry *efe;
    660   1.1   reinoud 	int error;
    661   1.1   reinoud 
    662   1.1   reinoud 	fe = NULL;
    663   1.1   reinoud 	efe = NULL;
    664   1.1   reinoud 	if (context.dscrver == 2) {
    665   1.1   reinoud 		error = udf_create_new_fe(&fe, filetype, st);
    666   1.1   reinoud 		if (error)
    667   1.1   reinoud 			errx(error, "can't create fe");
    668   1.1   reinoud 		*dscr = (union dscrptr *) fe;
    669   1.1   reinoud 		icb->longad_uniqueid = fe->unique_id;
    670   1.1   reinoud 	} else {
    671   1.1   reinoud 		error = udf_create_new_efe(&efe, filetype, st);
    672   1.1   reinoud 		if (error)
    673   1.1   reinoud 			errx(error, "can't create fe");
    674   1.1   reinoud 		*dscr = (union dscrptr *) efe;
    675   1.1   reinoud 		icb->longad_uniqueid = efe->unique_id;
    676   1.1   reinoud 	}
    677   1.1   reinoud 
    678   1.1   reinoud 	return 0;
    679   1.1   reinoud }
    680   1.1   reinoud 
    681   1.1   reinoud 
    682   1.1   reinoud static void
    683   1.1   reinoud udf_estimate_walk(fsinfo_t *fsopts,
    684   1.1   reinoud 		fsnode *root, char *dir, struct udf_stats *stats)
    685   1.1   reinoud {
    686   1.1   reinoud 	struct fileid_desc *fid;
    687  1.16     joerg 	struct long_ad dummy_ref;
    688   1.1   reinoud 	fsnode *cur;
    689   1.1   reinoud 	fsinode *fnode;
    690   1.1   reinoud 	size_t pathlen = strlen(dir);
    691   1.1   reinoud 	char *mydir = dir + pathlen;
    692   1.1   reinoud 	off_t sz;
    693   1.1   reinoud 	uint32_t nblk, ddoff;
    694   1.1   reinoud 	uint32_t softlink_len;
    695   1.1   reinoud 	uint8_t *softlink_buf;
    696   1.1   reinoud 	int nentries;
    697   1.1   reinoud 	int error;
    698   1.1   reinoud 
    699   1.1   reinoud 	stats->ndirs++;
    700   1.1   reinoud 
    701   1.1   reinoud 	/*
    702   1.1   reinoud 	 * Count number of directory entries and count directory size; needed
    703   1.1   reinoud 	 * for the reservation of enough space for the directory. Pity we
    704   1.1   reinoud 	 * don't keep the FIDs we created. If it turns out to be a issue we
    705   1.1   reinoud 	 * can cache it later.
    706   1.1   reinoud 	 */
    707   1.1   reinoud 	fid = (struct fileid_desc *) malloc(context.sector_size);
    708   1.1   reinoud 	assert(fid);
    709   1.1   reinoud 
    710   1.1   reinoud 	ddoff = 40;	/* '..' entry */
    711   1.1   reinoud 	for (cur = root, nentries = 0; cur != NULL; cur = cur->next) {
    712   1.1   reinoud 		switch (cur->type & S_IFMT) {
    713   1.1   reinoud 		default:
    714   1.1   reinoud 			/* what kind of nodes? */
    715   1.1   reinoud 			break;
    716   1.1   reinoud 		case S_IFCHR:
    717   1.1   reinoud 		case S_IFBLK:
    718   1.1   reinoud 			/* not supported yet */
    719  1.23   reinoud 			break;
    720   1.1   reinoud 		case S_IFDIR:
    721   1.1   reinoud 			if (strcmp(cur->name, ".") == 0)
    722   1.1   reinoud 				continue;
    723  1.19       mrg 			/* FALLTHROUGH */
    724   1.1   reinoud 		case S_IFLNK:
    725   1.1   reinoud 		case S_IFREG:
    726   1.1   reinoud 			/* create dummy FID to see how long name will become */
    727  1.16     joerg 			memset(&dummy_ref, 0, sizeof(dummy_ref));
    728   1.1   reinoud 			udf_create_fid(ddoff, fid, cur->name, 0, &dummy_ref);
    729   1.1   reinoud 			nentries++;
    730   1.1   reinoud 			ddoff += udf_fidsize(fid);
    731  1.23   reinoud 			break;
    732   1.1   reinoud 		}
    733   1.1   reinoud 	}
    734   1.1   reinoud 	sz = ddoff;
    735   1.1   reinoud 
    736   1.1   reinoud 	root->inode->st.st_size = sz;	/* max now */
    737   1.1   reinoud 	root->inode->flags |= FI_SIZED;
    738   1.1   reinoud 
    739   1.1   reinoud 	nblk = udf_datablocks(sz);
    740   1.1   reinoud 	stats->nmetadatablocks += nblk;
    741   1.1   reinoud 
    742   1.1   reinoud 	/* for each entry in the directory, there needs to be a (E)FE */
    743   1.1   reinoud 	stats->nmetadatablocks += nentries + 1;
    744   1.1   reinoud 
    745   1.1   reinoud 	/* recurse */
    746   1.1   reinoud 	for (cur = root; cur != NULL; cur = cur->next) {
    747   1.1   reinoud 		switch (cur->type & S_IFMT) {
    748   1.1   reinoud 		default:
    749   1.1   reinoud 			/* what kind of nodes? */
    750   1.1   reinoud 			break;
    751   1.1   reinoud 		case S_IFDIR:
    752   1.1   reinoud 			if (strcmp(cur->name, ".") == 0)
    753   1.1   reinoud 				continue;
    754   1.1   reinoud 			/* empty dir? */
    755   1.1   reinoud 			if (!cur->child)
    756   1.1   reinoud 				break;
    757   1.1   reinoud 			mydir[0] = '/';
    758   1.1   reinoud 			strncpy(&mydir[1], cur->name, MAXPATHLEN - pathlen);
    759   1.1   reinoud 			udf_estimate_walk(fsopts, cur->child, dir, stats);
    760   1.1   reinoud 			mydir[0] = '\0';
    761   1.1   reinoud 			break;
    762  1.23   reinoud 		case S_IFCHR:
    763  1.23   reinoud 		case S_IFBLK:
    764  1.23   reinoud 			/* not supported yet */
    765  1.23   reinoud 			// stats->nfiles++;
    766  1.23   reinoud 			break;
    767   1.1   reinoud 		case S_IFREG:
    768   1.1   reinoud 			fnode = cur->inode;
    769   1.1   reinoud 			/* don't double-count hard-links */
    770   1.1   reinoud 			if (!(fnode->flags & FI_SIZED)) {
    771   1.1   reinoud 				sz = fnode->st.st_size;
    772   1.1   reinoud 				nblk = udf_datablocks(sz);
    773   1.1   reinoud 				stats->ndatablocks += nblk;
    774   1.1   reinoud 				/* ... */
    775   1.1   reinoud 				fnode->flags |= FI_SIZED;
    776   1.1   reinoud 			}
    777   1.1   reinoud 			stats->nfiles++;
    778   1.1   reinoud 			break;
    779   1.1   reinoud 		case S_IFLNK:
    780   1.1   reinoud 			/* softlink */
    781   1.1   reinoud 			fnode = cur->inode;
    782   1.1   reinoud 			/* don't double-count hard-links */
    783   1.1   reinoud 			if (!(fnode->flags & FI_SIZED)) {
    784   1.1   reinoud 				error = udf_encode_symlink(&softlink_buf,
    785   1.1   reinoud 						&softlink_len, cur->symlink);
    786   1.1   reinoud 				if (error) {
    787   1.1   reinoud 					printf("SOFTLINK error %d\n", error);
    788   1.1   reinoud 					break;
    789   1.1   reinoud 				}
    790   1.1   reinoud 				nblk = udf_datablocks(softlink_len);
    791   1.1   reinoud 				stats->ndatablocks += nblk;
    792   1.1   reinoud 				fnode->flags |= FI_SIZED;
    793   1.1   reinoud 
    794   1.1   reinoud 				free(softlink_buf);
    795   1.1   reinoud 			}
    796   1.1   reinoud 			stats->nfiles++;
    797   1.1   reinoud 			break;
    798   1.1   reinoud 		}
    799   1.1   reinoud 	}
    800   1.1   reinoud }
    801   1.1   reinoud 
    802   1.1   reinoud 
    803   1.1   reinoud #define UDF_MAX_CHUNK_SIZE (4*1024*1024)
    804   1.1   reinoud static int
    805   1.1   reinoud udf_copy_file(struct stat *st, char *path, fsnode *cur, struct fileid_desc *fid,
    806   1.1   reinoud 	struct long_ad *icb)
    807   1.1   reinoud {
    808   1.1   reinoud 	union dscrptr *dscr;
    809   1.1   reinoud 	struct long_ad data_icb;
    810   1.1   reinoud 	fsinode *fnode;
    811  1.11   reinoud 	off_t sz, chunk, rd;
    812   1.1   reinoud 	uint8_t *data;
    813  1.23   reinoud 	bool intern;
    814   1.1   reinoud 	int nblk;
    815  1.23   reinoud 	int f;
    816   1.5   reinoud 	int error;
    817   1.1   reinoud 
    818   1.1   reinoud 	fnode = cur->inode;
    819   1.1   reinoud 
    820   1.3     joerg 	f = open(path, O_RDONLY);
    821   1.1   reinoud 	if (f < 0) {
    822   1.1   reinoud 		warn("Can't open file %s for reading", cur->name);
    823   1.1   reinoud 		return errno;
    824   1.1   reinoud 	}
    825   1.1   reinoud 
    826   1.1   reinoud 	/* claim disc space for the (e)fe descriptor for this file */
    827   1.1   reinoud 	udf_metadata_alloc(1, icb);
    828   1.1   reinoud 	udf_create_new_file(st, &dscr, UDF_ICB_FILETYPE_RANDOMACCESS, icb);
    829   1.1   reinoud 
    830   1.1   reinoud 	sz = fnode->st.st_size;
    831   1.1   reinoud 
    832   1.1   reinoud 	chunk = MIN(sz, UDF_MAX_CHUNK_SIZE);
    833   1.1   reinoud 	data = malloc(MAX(chunk, context.sector_size));
    834   1.1   reinoud 	assert(data);
    835   1.1   reinoud 
    836  1.23   reinoud 	intern = (udf_datablocks(chunk) == 0);
    837   1.5   reinoud 	error = 0;
    838   1.1   reinoud 	while (chunk) {
    839   1.1   reinoud 		rd = read(f, data, chunk);
    840   1.1   reinoud 		if (rd != chunk) {
    841  1.17  christos 			warn("Short read of file %s", cur->name);
    842   1.5   reinoud 			error = errno;
    843   1.5   reinoud 			break;
    844   1.1   reinoud 		}
    845   1.1   reinoud 
    846  1.23   reinoud 		nblk = UDF_ROUNDUP(chunk, context.sector_size) / context.sector_size;
    847  1.23   reinoud 		if (chunk && !intern)
    848   1.1   reinoud 			udf_data_alloc(nblk, &data_icb);
    849   1.1   reinoud 		udf_append_file_contents(dscr, &data_icb, data, chunk);
    850   1.1   reinoud 
    851   1.1   reinoud 		sz -= chunk;
    852   1.1   reinoud 		chunk = MIN(sz, UDF_MAX_CHUNK_SIZE);
    853   1.1   reinoud 	}
    854   1.1   reinoud 	close(f);
    855   1.1   reinoud 	free(data);
    856   1.1   reinoud 
    857   1.1   reinoud 	/* write out dscr (e)fe */
    858   1.1   reinoud 	udf_set_link_cnt(dscr, fnode->nlink);
    859   1.1   reinoud 	udf_write_dscr_virt(dscr, udf_rw32(icb->loc.lb_num),
    860   1.1   reinoud 		udf_rw16(icb->loc.part_num), 1);
    861   1.5   reinoud 	free(dscr);
    862   1.1   reinoud 
    863   1.1   reinoud 	/* remember our location for hardlinks */
    864   1.1   reinoud 	cur->inode->fsuse = malloc(sizeof(struct long_ad));
    865   1.1   reinoud 	memcpy(cur->inode->fsuse, icb, sizeof(struct long_ad));
    866   1.1   reinoud 
    867   1.5   reinoud 	return error;
    868   1.1   reinoud }
    869   1.1   reinoud 
    870   1.1   reinoud 
    871   1.1   reinoud static int
    872   1.1   reinoud udf_populate_walk(fsinfo_t *fsopts, fsnode *root, char *dir,
    873   1.1   reinoud 		struct long_ad *parent_icb, struct long_ad *dir_icb)
    874   1.1   reinoud {
    875   1.1   reinoud 	union dscrptr *dir_dscr, *dscr;
    876   1.1   reinoud 	struct fileid_desc *fid;
    877   1.1   reinoud 	struct long_ad icb, data_icb, dirdata_icb;
    878   1.1   reinoud 	fsnode *cur;
    879   1.1   reinoud 	fsinode *fnode;
    880   1.1   reinoud 	size_t pathlen = strlen(dir);
    881   1.1   reinoud 	size_t dirlen;
    882   1.1   reinoud 	char *mydir = dir + pathlen;
    883   1.1   reinoud 	uint32_t nblk, ddoff;
    884   1.1   reinoud 	uint32_t softlink_len;
    885   1.1   reinoud 	uint8_t *softlink_buf;
    886   1.1   reinoud 	uint8_t *dirdata;
    887   1.1   reinoud 	int error, ret, retval;
    888   1.1   reinoud 
    889   1.1   reinoud 	/* claim disc space for the (e)fe descriptor for this dir */
    890   1.1   reinoud 	udf_metadata_alloc(1, dir_icb);
    891   1.1   reinoud 
    892   1.1   reinoud 	/* create new e(fe) */
    893   1.1   reinoud 	udf_create_new_file(&root->inode->st, &dir_dscr,
    894   1.1   reinoud 		UDF_ICB_FILETYPE_DIRECTORY, dir_icb);
    895   1.1   reinoud 
    896  1.23   reinoud 	/* allocate memory for the directory contents */
    897   1.1   reinoud 	dirlen = root->inode->st.st_size;
    898  1.23   reinoud 	nblk = UDF_ROUNDUP(dirlen, context.sector_size) / context.sector_size;
    899   1.1   reinoud 	dirdata = malloc(nblk * context.sector_size);
    900   1.1   reinoud 	assert(dirdata);
    901   1.1   reinoud 	memset(dirdata, 0, nblk * context.sector_size);
    902   1.1   reinoud 
    903   1.1   reinoud 	/* create and append '..' */
    904   1.1   reinoud 	fid = (struct fileid_desc *) dirdata;
    905   1.1   reinoud 	ddoff = udf_create_parentfid(fid, parent_icb);
    906  1.23   reinoud 	assert(ddoff == 40);
    907   1.1   reinoud 
    908   1.1   reinoud 	/* for '..' */
    909   1.1   reinoud 	udf_inc_link(dir_dscr);
    910   1.1   reinoud 
    911   1.1   reinoud 	/* recurse */
    912   1.1   reinoud 	retval = 0;
    913   1.1   reinoud 	for (cur = root; cur != NULL; cur = cur->next) {
    914   1.1   reinoud 		mydir[0] = '/';
    915   1.1   reinoud 		strncpy(&mydir[1], cur->name, MAXPATHLEN - pathlen);
    916   1.1   reinoud 
    917   1.1   reinoud 		fid = (struct fileid_desc *) (dirdata + ddoff);
    918   1.1   reinoud 		switch (cur->type & S_IFMT) {
    919   1.1   reinoud 		default:
    920   1.1   reinoud 			/* what kind of nodes? */
    921   1.1   reinoud 			retval = 2;
    922   1.1   reinoud 			break;
    923   1.1   reinoud 		case S_IFCHR:
    924   1.1   reinoud 		case S_IFBLK:
    925   1.1   reinoud 			/* not supported */
    926   1.1   reinoud 			retval = 2;
    927   1.1   reinoud 			warnx("device node %s not supported", dir);
    928   1.1   reinoud 			break;
    929   1.1   reinoud 		case S_IFDIR:
    930   1.1   reinoud 			/* not an empty dir? */
    931   1.1   reinoud 			if (strcmp(cur->name, ".") == 0)
    932   1.1   reinoud 				break;
    933   1.1   reinoud 			assert(cur->child);
    934   1.1   reinoud 			if (cur->child) {
    935   1.1   reinoud 				ret = udf_populate_walk(fsopts, cur->child,
    936   1.1   reinoud 					dir, dir_icb, &icb);
    937   1.1   reinoud 				if (ret)
    938   1.1   reinoud 					retval = 2;
    939   1.1   reinoud 			}
    940   1.1   reinoud 			udf_create_fid(ddoff, fid, cur->name,
    941   1.1   reinoud 					UDF_FILE_CHAR_DIR, &icb);
    942   1.1   reinoud 			udf_inc_link(dir_dscr);
    943   1.1   reinoud 			ddoff += udf_fidsize(fid);
    944   1.1   reinoud 			break;
    945   1.1   reinoud 		case S_IFREG:
    946   1.1   reinoud 			fnode = cur->inode;
    947   1.1   reinoud 			/* don't re-copy hard-links */
    948   1.1   reinoud 			if (!(fnode->flags & FI_WRITTEN)) {
    949  1.23   reinoud 				printf("%s\n", dir);
    950   1.1   reinoud 				error = udf_copy_file(&fnode->st, dir, cur,
    951   1.1   reinoud 					fid, &icb);
    952   1.1   reinoud 				if (!error) {
    953   1.1   reinoud 					fnode->flags |= FI_WRITTEN;
    954   1.1   reinoud 					udf_create_fid(ddoff, fid, cur->name,
    955   1.1   reinoud 						0, &icb);
    956   1.1   reinoud 					ddoff += udf_fidsize(fid);
    957   1.1   reinoud 				} else {
    958   1.1   reinoud 					retval = 2;
    959   1.1   reinoud 				}
    960   1.1   reinoud 			} else {
    961   1.1   reinoud 				/* hardlink! */
    962   1.1   reinoud 				printf("%s (hardlink)\n", dir);
    963   1.1   reinoud 				udf_create_fid(ddoff, fid, cur->name,
    964   1.1   reinoud 					0, (struct long_ad *) (fnode->fsuse));
    965   1.1   reinoud 				ddoff += udf_fidsize(fid);
    966   1.1   reinoud 			}
    967   1.1   reinoud 			fnode->nlink--;
    968   1.1   reinoud 			if (fnode->nlink == 0)
    969   1.1   reinoud 				free(fnode->fsuse);
    970   1.1   reinoud 			break;
    971   1.1   reinoud 		case S_IFLNK:
    972   1.1   reinoud 			/* softlink */
    973   1.1   reinoud 			fnode = cur->inode;
    974   1.1   reinoud 			printf("%s -> %s\n", dir, cur->symlink);
    975   1.1   reinoud 			error = udf_encode_symlink(&softlink_buf,
    976   1.1   reinoud 					&softlink_len, cur->symlink);
    977   1.1   reinoud 			if (error) {
    978   1.1   reinoud 				printf("SOFTLINK error %d\n", error);
    979   1.1   reinoud 				retval = 2;
    980   1.1   reinoud 				break;
    981   1.1   reinoud 			}
    982   1.1   reinoud 
    983   1.1   reinoud 			udf_metadata_alloc(1, &icb);
    984   1.1   reinoud 			udf_create_new_file(&fnode->st, &dscr,
    985   1.1   reinoud 				UDF_ICB_FILETYPE_SYMLINK, &icb);
    986   1.1   reinoud 
    987   1.1   reinoud 			nblk = udf_datablocks(softlink_len);
    988   1.1   reinoud 			if (nblk > 0)
    989   1.1   reinoud 				udf_data_alloc(nblk, &data_icb);
    990   1.1   reinoud 			udf_append_file_contents(dscr, &data_icb,
    991   1.1   reinoud 					softlink_buf, softlink_len);
    992   1.1   reinoud 
    993   1.1   reinoud 			/* write out dscr (e)fe */
    994   1.1   reinoud 			udf_inc_link(dscr);
    995   1.1   reinoud 			udf_write_dscr_virt(dscr, udf_rw32(icb.loc.lb_num),
    996   1.1   reinoud 				udf_rw16(icb.loc.part_num), 1);
    997   1.1   reinoud 
    998   1.5   reinoud 			free(dscr);
    999   1.1   reinoud 			free(softlink_buf);
   1000   1.1   reinoud 
   1001   1.1   reinoud 			udf_create_fid(ddoff, fid, cur->name, 0, &icb);
   1002   1.1   reinoud 			ddoff += udf_fidsize(fid);
   1003   1.1   reinoud 			break;
   1004   1.1   reinoud 		}
   1005   1.1   reinoud 		mydir[0] = '\0';
   1006   1.1   reinoud 	}
   1007  1.23   reinoud 	assert(dirlen == ddoff);
   1008  1.23   reinoud 
   1009  1.23   reinoud 	/* pre allocate space for the directory contents */
   1010  1.23   reinoud 	memset(&dirdata_icb, 0, sizeof(dirdata_icb));
   1011  1.23   reinoud 	nblk = udf_datablocks(dirlen);
   1012   1.1   reinoud 
   1013  1.23   reinoud 	/* claim disc space for the dir contents if needed */
   1014  1.23   reinoud 	if (nblk > 0)
   1015  1.23   reinoud 		udf_fids_alloc(nblk, &dirdata_icb);
   1016   1.1   reinoud 
   1017   1.1   reinoud 	udf_prepare_fids(dir_icb, &dirdata_icb, dirdata, dirlen);
   1018   1.1   reinoud 	udf_append_file_contents(dir_dscr, &dirdata_icb, dirdata, dirlen);
   1019   1.1   reinoud 
   1020   1.1   reinoud 	/* write out dir_dscr (e)fe */
   1021   1.1   reinoud 	udf_write_dscr_virt(dir_dscr, udf_rw32(dir_icb->loc.lb_num),
   1022   1.1   reinoud 			udf_rw16(dir_icb->loc.part_num), 1);
   1023   1.1   reinoud 
   1024   1.5   reinoud 	free(dirdata);
   1025   1.5   reinoud 	free(dir_dscr);
   1026   1.1   reinoud 	return retval;
   1027   1.1   reinoud }
   1028   1.1   reinoud 
   1029   1.1   reinoud 
   1030   1.1   reinoud static int
   1031   1.1   reinoud udf_populate(const char *dir, fsnode *root, fsinfo_t *fsopts,
   1032   1.1   reinoud 		struct udf_stats *stats)
   1033   1.1   reinoud {
   1034   1.1   reinoud 	struct long_ad rooticb;
   1035   1.1   reinoud 	static char path[MAXPATHLEN+1];
   1036   1.1   reinoud 	int error;
   1037   1.1   reinoud 
   1038   1.1   reinoud 	strncpy(path, dir, sizeof(path));
   1039   1.1   reinoud 	error = udf_populate_walk(fsopts, root, path, &rooticb, &rooticb);
   1040   1.1   reinoud 
   1041   1.1   reinoud 	return error;
   1042   1.1   reinoud }
   1043   1.1   reinoud 
   1044   1.1   reinoud 
   1045   1.1   reinoud static void
   1046   1.1   reinoud udf_enumerate_and_estimate(const char *dir, fsnode *root, fsinfo_t *fsopts,
   1047   1.1   reinoud 		struct udf_stats *stats)
   1048   1.1   reinoud {
   1049   1.1   reinoud 	char path[MAXPATHLEN + 1];
   1050   1.7   reinoud 	off_t proposed_size;
   1051  1.23   reinoud 	uint32_t n, nblk, nmetablk, nbytes;
   1052  1.23   reinoud 	uint32_t spareable_blocks, spareable_blockingnr;
   1053   1.1   reinoud 
   1054   1.1   reinoud 	strncpy(path, dir, sizeof(path));
   1055   1.1   reinoud 
   1056   1.1   reinoud 	/* calculate strict minimal size */
   1057   1.1   reinoud 	udf_estimate_walk(fsopts, root, path, stats);
   1058  1.23   reinoud #if 0
   1059   1.5   reinoud 	printf("ndirs            %d\n", stats->ndirs);
   1060   1.5   reinoud 	printf("nfiles           %d\n", stats->nfiles);
   1061   1.5   reinoud 	printf("ndata_blocks     %d\n", stats->ndatablocks);
   1062   1.5   reinoud 	printf("nmetadata_blocks %d\n", stats->nmetadatablocks);
   1063   1.5   reinoud 	printf("\n");
   1064  1.23   reinoud #endif
   1065   1.1   reinoud 
   1066   1.1   reinoud 	/* adjust for options : free file nodes */
   1067   1.1   reinoud 	if (fsopts->freefiles) {
   1068   1.1   reinoud 		/* be mercifull and reserve more for the FID */
   1069   1.1   reinoud 		stats->nmetadatablocks += fsopts->freefiles * 1.5;
   1070   1.1   reinoud 	} else if ((n = fsopts->freefilepc)) {
   1071   1.1   reinoud 		stats->nmetadatablocks += (stats->nmetadatablocks*n) / (100-n);
   1072   1.1   reinoud 	}
   1073   1.1   reinoud 
   1074   1.1   reinoud 	/* adjust for options : free data blocks */
   1075   1.1   reinoud 	if (fsopts->freeblocks) {
   1076   1.1   reinoud 		stats->ndatablocks += fsopts->freeblocks;
   1077   1.1   reinoud 	} else if ((n = fsopts->freeblockpc)) {
   1078   1.1   reinoud 		stats->ndatablocks += (stats->ndatablocks * n) / (100-n);
   1079   1.1   reinoud 	}
   1080   1.1   reinoud 
   1081   1.1   reinoud 	/* rough predictor of minimum disc size */
   1082   1.1   reinoud 	nblk  = stats->ndatablocks + stats->nmetadatablocks;
   1083  1.23   reinoud 	if (context.format_flags & FORMAT_META) {
   1084  1.23   reinoud 		float meta_p;
   1085  1.23   reinoud 		double factor;
   1086  1.23   reinoud 
   1087  1.23   reinoud 		meta_p = (float) context.meta_perc/100.0;
   1088  1.23   reinoud 		factor = meta_p / (1.0 - meta_p);
   1089  1.23   reinoud 
   1090  1.23   reinoud 		/* add space for metadata partition including some slack */
   1091  1.23   reinoud 		nmetablk = factor * nblk + 32;
   1092  1.23   reinoud 		nblk =  stats->ndatablocks + nmetablk;
   1093  1.23   reinoud 
   1094  1.23   reinoud 		/* free space maps */
   1095  1.23   reinoud 		nbytes = ceil((double) nblk * (1.0/8.0));
   1096  1.23   reinoud 		nblk += 1 + (nbytes + context.sector_size-1)/context.sector_size;
   1097  1.23   reinoud 		if (!(context.format_flags & FORMAT_READONLY)) {
   1098  1.23   reinoud 			nbytes = ceil((double) nmetablk * (1.0/8.0));
   1099  1.23   reinoud 			nblk += 1 + (nbytes + context.sector_size-1)/context.sector_size;
   1100  1.23   reinoud 		}
   1101  1.23   reinoud 	} else if (context.format_flags & FORMAT_SEQUENTIAL) {
   1102  1.23   reinoud 		/* nothing */
   1103  1.23   reinoud 	} else {
   1104  1.23   reinoud 		if (!(context.format_flags & FORMAT_READONLY)) {
   1105  1.23   reinoud 			nbytes = ceil((double) nblk * (1.0/8.0));
   1106  1.23   reinoud 			nblk += 1 + (nbytes + context.sector_size-1)/
   1107  1.23   reinoud 				context.sector_size;
   1108  1.23   reinoud 		}
   1109  1.23   reinoud 	}
   1110  1.23   reinoud 
   1111  1.23   reinoud 	/*
   1112  1.23   reinoud 	 * Make extra room for spareable table if requested
   1113  1.23   reinoud 	 */
   1114  1.23   reinoud 	if (context.format_flags & FORMAT_SPAREABLE) {
   1115  1.23   reinoud 		spareable_blockingnr = udf_spareable_blockingnr();
   1116  1.23   reinoud 		spareable_blocks     = udf_spareable_blocks();
   1117  1.23   reinoud 
   1118  1.23   reinoud 		nblk += spareable_blocks * spareable_blockingnr;
   1119  1.23   reinoud 		nblk += spareable_blockingnr;		/* slack */
   1120  1.23   reinoud 	}
   1121  1.23   reinoud 
   1122   1.1   reinoud 	nblk += 256;					/* pre-volume space  */
   1123   1.1   reinoud 	nblk += 256;					/* post-volume space */
   1124  1.23   reinoud 	nblk += 1024;					/* safeguard	     */
   1125   1.1   reinoud 
   1126   1.1   reinoud 	/* try to honour minimum size */
   1127   1.1   reinoud 	n = fsopts->minsize / fsopts->sectorsize;
   1128   1.1   reinoud 	if (nblk < n) {
   1129   1.1   reinoud 		stats->ndatablocks += (n - nblk);
   1130   1.1   reinoud 		nblk += n - nblk;
   1131   1.1   reinoud 	}
   1132   1.7   reinoud 	proposed_size = (off_t) nblk * fsopts->sectorsize;
   1133   1.7   reinoud 	/* sanity size */
   1134   1.7   reinoud 	if (proposed_size < 512*1024)
   1135   1.7   reinoud 		proposed_size = 512*1024;
   1136   1.1   reinoud 
   1137   1.7   reinoud 	if (fsopts->size) {
   1138  1.23   reinoud 		if (fsopts->size < proposed_size)
   1139  1.23   reinoud 			errx(1, "makefs_udf: won't fit on disc!");
   1140   1.7   reinoud 	} else {
   1141   1.7   reinoud 		fsopts->size = proposed_size;
   1142   1.7   reinoud 	}
   1143   1.1   reinoud 
   1144   1.1   reinoud 	fsopts->inodes = stats->nfiles + stats->ndirs;
   1145   1.1   reinoud }
   1146   1.1   reinoud 
   1147   1.1   reinoud 
   1148   1.1   reinoud void
   1149   1.1   reinoud udf_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
   1150   1.1   reinoud {
   1151   1.1   reinoud 	struct udf_stats stats;
   1152   1.1   reinoud 	uint64_t truncate_len;
   1153  1.23   reinoud 	uint32_t last_sector, ext;
   1154   1.9   reinoud 	char scrap[255];
   1155   1.1   reinoud 	int error;
   1156   1.1   reinoud 
   1157  1.23   reinoud 	/* setup */
   1158  1.23   reinoud 	emul_sectorsize = fsopts->sectorsize;
   1159  1.23   reinoud 	emul_size = 0;
   1160   1.1   reinoud 	context.sector_size = fsopts->sectorsize;
   1161   1.1   reinoud 
   1162   1.1   reinoud 	/* names */
   1163   1.1   reinoud 	error = udf_proces_names();
   1164   1.1   reinoud 	if (error)
   1165  1.23   reinoud 		errx(1, "bad names given");
   1166  1.23   reinoud 
   1167  1.23   reinoud 	/* open disc device or emulated file */
   1168  1.23   reinoud 	if (udf_opendisc(image, O_CREAT)) {
   1169  1.23   reinoud 		udf_closedisc();
   1170  1.23   reinoud 		errx(1, "can't open %s", image);
   1171  1.23   reinoud 	}
   1172  1.23   reinoud 	fsopts->fd = dev_fd;
   1173   1.1   reinoud 
   1174  1.23   reinoud 	/* determine format */
   1175  1.23   reinoud 	if (udf_readonly_format())
   1176  1.23   reinoud 		req_enable |= FORMAT_READONLY;
   1177  1.23   reinoud 	// printf("req_enable %d, req_disable %d\n", req_enable, req_disable);
   1178  1.23   reinoud 	error = udf_derive_format(req_enable, req_disable);
   1179  1.23   reinoud 	if (error) {
   1180  1.23   reinoud 		udf_closedisc();
   1181  1.23   reinoud 		errx(1, "can't derive format from media/settings");
   1182  1.23   reinoud 	}
   1183   1.1   reinoud 
   1184   1.1   reinoud 	/* estimate the amount of space needed */
   1185   1.2   reinoud 	memset(&stats, 0, sizeof(stats));
   1186   1.1   reinoud 	udf_enumerate_and_estimate(dir, root, fsopts, &stats);
   1187   1.1   reinoud 
   1188  1.23   reinoud 	printf("Calculated size of `%s' is "
   1189  1.23   reinoud 		"%"PRIu64" KiB, %"PRIu64" MiB, %"PRIu64" GiB with %ld inodes\n",
   1190  1.23   reinoud 		image,
   1191  1.23   reinoud 		(uint64_t) fsopts->size/1024,
   1192  1.23   reinoud 		(uint64_t) fsopts->size/1024/1024,
   1193  1.23   reinoud 		(uint64_t) fsopts->size/1024/1024/1024,
   1194  1.23   reinoud 		(long)fsopts->inodes);
   1195  1.23   reinoud 	emul_size = MAX(emul_size, fsopts->size);
   1196  1.23   reinoud 	if ((fsopts->maxsize > 0) && (emul_size > fsopts->maxsize))
   1197  1.23   reinoud 		errx(1, "won't fit due to set maximum disk size");
   1198   1.1   reinoud 
   1199  1.23   reinoud 	/* prepare disc if necessary (recordables mainly) */
   1200  1.23   reinoud 	error = udf_prepare_disc();
   1201  1.23   reinoud 	if (error) {
   1202  1.23   reinoud 		udf_closedisc();
   1203  1.23   reinoud 		errx(1, "preparing disc failed");
   1204   1.1   reinoud 	}
   1205   1.1   reinoud 
   1206   1.1   reinoud 	/* update mmc info but now with correct size */
   1207  1.23   reinoud 	udf_update_discinfo();
   1208  1.23   reinoud 	udf_dump_discinfo(&mmc_discinfo);
   1209   1.1   reinoud 
   1210   1.9   reinoud 	printf("Building disc compatible with UDF version %x to %x\n\n",
   1211   1.9   reinoud 		context.min_udf, context.max_udf);
   1212   1.9   reinoud 	(void)snprintb(scrap, sizeof(scrap), FORMAT_FLAGBITS,
   1213  1.23   reinoud 	    (uint64_t) context.format_flags);
   1214   1.9   reinoud 	printf("UDF properties       %s\n", scrap);
   1215   1.9   reinoud 	printf("Volume set          `%s'\n", context.volset_name);
   1216   1.9   reinoud 	printf("Primary volume      `%s`\n", context.primary_name);
   1217   1.9   reinoud 	printf("Logical volume      `%s`\n", context.logvol_name);
   1218  1.23   reinoud 	if (context.format_flags & FORMAT_META)
   1219  1.23   reinoud 		printf("Metadata percentage  %d%% (%d%% used)\n",
   1220  1.23   reinoud 			context.meta_perc,
   1221  1.23   reinoud 			(int) ceilf(100.0*stats.nmetadatablocks/stats.ndatablocks));
   1222   1.9   reinoud 	printf("\n");
   1223  1.23   reinoud 
   1224  1.23   reinoud 	/* prefix */
   1225  1.23   reinoud 	udf_allow_writing();
   1226  1.23   reinoud 	if (udf_do_newfs_prefix()) {
   1227  1.23   reinoud 		udf_closedisc();
   1228  1.23   reinoud 		errx(1, "basic setup failed");
   1229  1.23   reinoud 	}
   1230   1.1   reinoud 
   1231   1.1   reinoud 	/* update context */
   1232   1.1   reinoud 	context.unique_id = 0;
   1233   1.1   reinoud 
   1234  1.23   reinoud 	/* add all directories */
   1235   1.1   reinoud 	error = udf_populate(dir, root, fsopts, &stats);
   1236   1.1   reinoud 
   1237  1.23   reinoud 	if (!error) {
   1238  1.24       wiz 		/* update values for integrity sequence */
   1239  1.23   reinoud 		context.num_files = stats.nfiles;
   1240  1.23   reinoud 		context.num_directories = stats.ndirs;
   1241  1.23   reinoud 
   1242  1.23   reinoud 		udf_do_newfs_postfix();
   1243  1.23   reinoud 
   1244  1.23   reinoud 		if (S_ISREG(dev_fd_stat.st_mode) &&
   1245  1.23   reinoud 				(context.format_flags & FORMAT_VAT)) {
   1246  1.23   reinoud 			udf_translate_vtop(context.alloc_pos[context.data_part],
   1247  1.23   reinoud 				context.data_part,
   1248  1.23   reinoud 				&last_sector, &ext);
   1249  1.23   reinoud 			truncate_len = (uint64_t) last_sector * context.sector_size;
   1250  1.23   reinoud 
   1251  1.23   reinoud 			printf("\nTruncing the disc-image to allow for VAT\n");
   1252  1.23   reinoud 			printf("Free space left on this volume approx. "
   1253  1.23   reinoud 				"%"PRIu64" KiB, %"PRIu64" MiB\n",
   1254  1.23   reinoud 				(fsopts->size - truncate_len)/1024,
   1255  1.23   reinoud 				(fsopts->size - truncate_len)/1024/1024);
   1256  1.23   reinoud 			ftruncate(dev_fd, truncate_len);
   1257  1.23   reinoud 		}
   1258   1.1   reinoud 	}
   1259  1.23   reinoud 	udf_closedisc();
   1260   1.1   reinoud 
   1261  1.23   reinoud 	if (error == 2)
   1262  1.23   reinoud 		errx(error, "not all files could be added");
   1263  1.23   reinoud 	if (error == 1)
   1264  1.23   reinoud 		errx(error, "creation of %s failed", image);
   1265   1.1   reinoud 	return;
   1266   1.1   reinoud }
   1267   1.1   reinoud 
   1268