Home | History | Annotate | Line # | Download | only in makefs
udf.c revision 1.10
      1  1.10  reinoud /* $NetBSD: udf.c,v 1.10 2013/08/06 13:15:30 reinoud Exp $ */
      2   1.1  reinoud 
      3   1.1  reinoud /*
      4   1.1  reinoud  * Copyright (c) 2006, 2008, 2013 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.10  reinoud __RCSID("$NetBSD: udf.c,v 1.10 2013/08/06 13:15:30 reinoud 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.1  reinoud #include <sys/types.h>
     45   1.1  reinoud #include <sys/param.h>
     46   1.4  reinoud #include <sys/stat.h>
     47   1.4  reinoud #include <util.h>
     48   1.4  reinoud 
     49   1.3    joerg #if !HAVE_NBTOOL_CONFIG_H
     50   1.4  reinoud #define _EXPOSE_MMC
     51   1.1  reinoud #include <sys/cdio.h>
     52   1.4  reinoud #else
     53   1.4  reinoud #include "udf/cdio_mmc_structs.h"
     54   1.3    joerg #endif
     55   1.1  reinoud 
     56   1.1  reinoud #include "makefs.h"
     57   1.1  reinoud #include "udf_create.h"
     58   1.1  reinoud #include "udf_write.h"
     59   1.1  reinoud #include "newfs_udf.h"
     60   1.1  reinoud 
     61  1.10  reinoud #undef APP_NAME
     62  1.10  reinoud #define APP_NAME "*NetBSD makefs"
     63   1.1  reinoud 
     64   1.1  reinoud /*
     65   1.1  reinoud  * Note: due to the setup of the newfs code, the current state of the program
     66   1.1  reinoud  * and its options are helt in a few global variables. The FS specific parts
     67   1.1  reinoud  * are in a global `context' structure.
     68   1.1  reinoud  */
     69   1.1  reinoud 
     70   1.1  reinoud /* global variables describing disc and format requests */
     71   1.1  reinoud int	 fd;				/* device: file descriptor */
     72   1.1  reinoud char	*dev;				/* device: name		   */
     73   1.1  reinoud struct mmc_discinfo mmc_discinfo;	/* device: disc info	   */
     74   1.1  reinoud 
     75   1.1  reinoud char	*format_str;			/* format: string representation */
     76   1.1  reinoud int	 format_flags;			/* format: attribute flags	 */
     77   1.1  reinoud int	 media_accesstype;		/* derived from current mmc cap  */
     78   1.1  reinoud int	 check_surface;			/* for rewritables               */
     79   1.1  reinoud int	 imagefile_secsize;		/* for files			 */
     80   1.1  reinoud 
     81   1.1  reinoud int	 display_progressbar;
     82   1.1  reinoud 
     83   1.1  reinoud int	 wrtrack_skew;
     84   1.1  reinoud float	 meta_fract = (float) UDF_META_PERC / 100.0;
     85   1.1  reinoud 
     86   1.1  reinoud int	 mmc_profile;			/* emulated profile */
     87   1.1  reinoud int	 req_enable, req_disable;
     88   1.1  reinoud 
     89   1.1  reinoud 
     90   1.1  reinoud /* --------------------------------------------------------------------- */
     91   1.1  reinoud 
     92   1.1  reinoud int
     93   1.1  reinoud udf_write_sector(void *sector, uint32_t location)
     94   1.1  reinoud {
     95   1.1  reinoud 	uint64_t wpos;
     96   1.1  reinoud 	ssize_t ret;
     97   1.1  reinoud 
     98   1.1  reinoud 	wpos = (uint64_t) location * context.sector_size;
     99   1.1  reinoud 	ret = pwrite(fd, sector, context.sector_size, wpos);
    100   1.1  reinoud 	if (ret == -1)
    101   1.1  reinoud 		return errno;
    102   1.9  reinoud 	if (ret < (int) context.sector_size)
    103   1.1  reinoud 		return EIO;
    104   1.1  reinoud 	return 0;
    105   1.1  reinoud }
    106   1.1  reinoud 
    107   1.1  reinoud 
    108   1.1  reinoud /* not implemented for files */
    109   1.1  reinoud int
    110   1.1  reinoud udf_surface_check(void)
    111   1.1  reinoud {
    112   1.1  reinoud 	return 0;
    113   1.1  reinoud }
    114   1.1  reinoud 
    115   1.1  reinoud 
    116   1.1  reinoud /*
    117   1.1  reinoud  * mmc_discinfo and mmc_trackinfo readers modified from origional in udf main
    118   1.1  reinoud  * code in sys/fs/udf/
    119   1.1  reinoud  */
    120   1.1  reinoud 
    121   1.1  reinoud #ifdef DEBUG
    122   1.1  reinoud static void
    123   1.1  reinoud udf_dump_discinfo(struct mmc_discinfo *di)
    124   1.1  reinoud {
    125   1.1  reinoud 	char bits[128];
    126   1.1  reinoud 
    127   1.1  reinoud 	printf("Device/media info  :\n");
    128   1.1  reinoud 	printf("\tMMC profile        0x%02x\n", di->mmc_profile);
    129   1.1  reinoud 	printf("\tderived class      %d\n", di->mmc_class);
    130   1.1  reinoud 	printf("\tsector size        %d\n", di->sector_size);
    131   1.1  reinoud 	printf("\tdisc state         %d\n", di->disc_state);
    132   1.1  reinoud 	printf("\tlast ses state     %d\n", di->last_session_state);
    133   1.1  reinoud 	printf("\tbg format state    %d\n", di->bg_format_state);
    134   1.1  reinoud 	printf("\tfrst track         %d\n", di->first_track);
    135   1.1  reinoud 	printf("\tfst on last ses    %d\n", di->first_track_last_session);
    136   1.1  reinoud 	printf("\tlst on last ses    %d\n", di->last_track_last_session);
    137   1.1  reinoud 	printf("\tlink block penalty %d\n", di->link_block_penalty);
    138   1.7  reinoud 	snprintb(bits, sizeof(bits), MMC_DFLAGS_FLAGBITS,
    139   1.7  reinoud 			(uint64_t) di->disc_flags);
    140   1.1  reinoud 	printf("\tdisc flags         %s\n", bits);
    141   1.1  reinoud 	printf("\tdisc id            %x\n", di->disc_id);
    142   1.1  reinoud 	printf("\tdisc barcode       %"PRIx64"\n", di->disc_barcode);
    143   1.1  reinoud 
    144   1.1  reinoud 	printf("\tnum sessions       %d\n", di->num_sessions);
    145   1.1  reinoud 	printf("\tnum tracks         %d\n", di->num_tracks);
    146   1.1  reinoud 
    147   1.1  reinoud 	snprintb(bits, sizeof(bits), MMC_CAP_FLAGBITS, di->mmc_cur);
    148   1.1  reinoud 	printf("\tcapabilities cur   %s\n", bits);
    149   1.1  reinoud 	snprintb(bits, sizeof(bits), MMC_CAP_FLAGBITS, di->mmc_cap);
    150   1.1  reinoud 	printf("\tcapabilities cap   %s\n", bits);
    151   1.1  reinoud 	printf("\n");
    152   1.1  reinoud 	printf("\tlast_possible_lba  %d\n", di->last_possible_lba);
    153   1.1  reinoud 	printf("\n");
    154   1.1  reinoud }
    155   1.1  reinoud #else
    156   1.1  reinoud #define udf_dump_discinfo(a);
    157   1.1  reinoud #endif
    158   1.1  reinoud 
    159   1.1  reinoud /* --------------------------------------------------------------------- */
    160   1.1  reinoud 
    161   1.1  reinoud static int
    162   1.1  reinoud udf_emulate_discinfo(fsinfo_t *fsopts, struct mmc_discinfo *di,
    163   1.1  reinoud 		int mmc_emuprofile)
    164   1.1  reinoud {
    165   1.1  reinoud 	off_t sectors;
    166   1.1  reinoud 
    167   1.2  reinoud 	memset(di, 0, sizeof(*di));
    168   1.1  reinoud 
    169   1.1  reinoud 	/* file support */
    170   1.1  reinoud 	if ((mmc_emuprofile != 0x01) && (fsopts->sectorsize != 2048))
    171   1.1  reinoud 		warnx("cd/dvd/bd sectorsize is not set to default 2048");
    172   1.1  reinoud 
    173   1.1  reinoud 	sectors = fsopts->size / fsopts->sectorsize;
    174   1.1  reinoud 
    175   1.1  reinoud 	/* commons */
    176   1.1  reinoud 	di->mmc_profile		= mmc_emuprofile;
    177   1.1  reinoud 	di->disc_state		= MMC_STATE_CLOSED;
    178   1.1  reinoud 	di->last_session_state	= MMC_STATE_CLOSED;
    179   1.1  reinoud 	di->bg_format_state	= MMC_BGFSTATE_COMPLETED;
    180   1.1  reinoud 	di->link_block_penalty	= 0;
    181   1.1  reinoud 
    182   1.1  reinoud 	di->disc_flags = MMC_DFLAGS_UNRESTRICTED;
    183   1.1  reinoud 
    184   1.1  reinoud 	di->last_possible_lba = sectors - 1;
    185   1.1  reinoud 	di->sector_size       = fsopts->sectorsize;
    186   1.1  reinoud 
    187   1.1  reinoud 	di->num_sessions = 1;
    188   1.1  reinoud 	di->num_tracks   = 1;
    189   1.1  reinoud 
    190   1.1  reinoud 	di->first_track  = 1;
    191   1.1  reinoud 	di->first_track_last_session = di->last_track_last_session = 1;
    192   1.1  reinoud 
    193   1.1  reinoud 	di->mmc_cur = MMC_CAP_RECORDABLE | MMC_CAP_ZEROLINKBLK;
    194   1.1  reinoud 	switch (mmc_emuprofile) {
    195   1.1  reinoud 	case 0x00:	/* unknown, treat as CDROM */
    196   1.1  reinoud 	case 0x08:	/* CDROM */
    197   1.7  reinoud 	case 0x10:	/* DVDROM */
    198   1.7  reinoud 	case 0x40:	/* BDROM */
    199   1.1  reinoud 		req_enable |= FORMAT_READONLY;
    200   1.1  reinoud 		/* FALLTROUGH */
    201   1.1  reinoud 	case 0x01:	/* disc */
    202   1.1  reinoud 		/* set up a disc info profile for partitions/files */
    203   1.1  reinoud 		di->mmc_class	= MMC_CLASS_DISC;
    204   1.1  reinoud 		di->mmc_cur    |= MMC_CAP_REWRITABLE | MMC_CAP_HW_DEFECTFREE;
    205   1.1  reinoud 		break;
    206   1.1  reinoud 	case 0x09:	/* CD-R */
    207   1.1  reinoud 		di->mmc_class	= MMC_CLASS_CD;
    208   1.1  reinoud 		di->mmc_cur    |= MMC_CAP_SEQUENTIAL;
    209   1.1  reinoud 		di->disc_state  = MMC_STATE_EMPTY;
    210   1.1  reinoud 		break;
    211   1.1  reinoud 	case 0x0a:	/* CD-RW + CD-MRW (regretably) */
    212   1.1  reinoud 		di->mmc_class	= MMC_CLASS_CD;
    213   1.1  reinoud 		di->mmc_cur    |= MMC_CAP_REWRITABLE;
    214   1.1  reinoud 		break;
    215   1.1  reinoud 	case 0x13:	/* DVD-RW */
    216   1.1  reinoud 		di->mmc_class	= MMC_CLASS_DVD;
    217   1.1  reinoud 		di->mmc_cur    |= MMC_CAP_REWRITABLE;
    218   1.1  reinoud 		break;
    219   1.1  reinoud 	case 0x11:	/* DVD-R */
    220   1.1  reinoud 	case 0x14:	/* DVD-RW sequential */
    221   1.1  reinoud 	case 0x1b:	/* DVD+R */
    222   1.1  reinoud 	case 0x2b:	/* DVD+R DL */
    223   1.1  reinoud 	case 0x51:	/* HD DVD-R */
    224   1.1  reinoud 		di->mmc_class	= MMC_CLASS_DVD;
    225   1.1  reinoud 		di->mmc_cur    |= MMC_CAP_SEQUENTIAL;
    226   1.1  reinoud 		di->disc_state  = MMC_STATE_EMPTY;
    227   1.1  reinoud 		break;
    228   1.1  reinoud 	case 0x41:	/* BD-R */
    229   1.1  reinoud 		di->mmc_class   = MMC_CLASS_BD;
    230   1.1  reinoud 		di->mmc_cur    |= MMC_CAP_SEQUENTIAL | MMC_CAP_HW_DEFECTFREE;
    231   1.1  reinoud 		di->disc_state  = MMC_STATE_EMPTY;
    232   1.1  reinoud 		break;
    233   1.1  reinoud 	case 0x43:	/* BD-RE */
    234   1.1  reinoud 		di->mmc_class   = MMC_CLASS_BD;
    235   1.1  reinoud 		di->mmc_cur    |= MMC_CAP_REWRITABLE | MMC_CAP_HW_DEFECTFREE;
    236   1.1  reinoud 		break;
    237   1.1  reinoud 	default:
    238   1.1  reinoud 		err(EINVAL, "makefs_udf: unknown or unimplemented device type");
    239   1.1  reinoud 		return EINVAL;
    240   1.1  reinoud 	}
    241   1.1  reinoud 	di->mmc_cap    = di->mmc_cur;
    242   1.1  reinoud 
    243   1.1  reinoud 	udf_dump_discinfo(di);
    244   1.1  reinoud 	return 0;
    245   1.1  reinoud }
    246   1.1  reinoud 
    247   1.1  reinoud 
    248   1.1  reinoud int
    249   1.1  reinoud udf_update_trackinfo(struct mmc_discinfo *di, struct mmc_trackinfo *ti)
    250   1.1  reinoud {
    251   1.1  reinoud 	/* discs partition support */
    252   1.1  reinoud 	if (ti->tracknr != 1)
    253   1.1  reinoud 		return EIO;
    254   1.1  reinoud 
    255   1.1  reinoud 	/* create fake ti */
    256   1.1  reinoud 	ti->sessionnr  = 1;
    257   1.1  reinoud 
    258   1.1  reinoud 	ti->track_mode = 0;	/* XXX */
    259   1.1  reinoud 	ti->data_mode  = 0;	/* XXX */
    260   1.1  reinoud 	ti->flags = MMC_TRACKINFO_LRA_VALID | MMC_TRACKINFO_NWA_VALID;
    261   1.1  reinoud 
    262   1.1  reinoud 	ti->track_start    = 0;
    263   1.1  reinoud 	ti->packet_size    = 32;	/* take sensible default */
    264   1.1  reinoud 
    265   1.1  reinoud 	ti->track_size    = di->last_possible_lba;
    266   1.1  reinoud 	ti->next_writable = di->last_possible_lba;
    267   1.1  reinoud 	ti->last_recorded = ti->next_writable;
    268   1.1  reinoud 	ti->free_blocks   = 0;
    269   1.1  reinoud 
    270   1.1  reinoud 	return 0;
    271   1.1  reinoud }
    272   1.1  reinoud 
    273   1.1  reinoud #define OPT_STR(letter, name, desc)  \
    274   1.1  reinoud 	{ letter, name, NULL, OPT_STRBUF, 0, 0, desc }
    275   1.1  reinoud 
    276   1.1  reinoud #define OPT_NUM(letter, name, field, min, max, desc) \
    277   1.9  reinoud 	{ letter, name, &context.field, \
    278   1.9  reinoud 	  sizeof(context.field) == 8 ? OPT_INT64 : \
    279   1.9  reinoud 	  (sizeof(context.field) == 4 ? OPT_INT32 : \
    280   1.9  reinoud 	  (sizeof(context.field) == 2 ? OPT_INT16 : OPT_INT8)), \
    281   1.1  reinoud 	  min, max, desc }
    282   1.1  reinoud 
    283   1.1  reinoud #define OPT_BOOL(letter, name, field, desc) \
    284   1.1  reinoud 	OPT_NUM(letter, name, field, 0, 1, desc)
    285   1.1  reinoud 
    286   1.1  reinoud void
    287   1.1  reinoud udf_prep_opts(fsinfo_t *fsopts)
    288   1.1  reinoud {
    289   1.1  reinoud 	struct tm *tm;
    290   1.1  reinoud 	time_t now;
    291   1.1  reinoud 
    292   1.1  reinoud 	const option_t udf_options[] = {
    293   1.8  reinoud 		OPT_STR('T', "disctype", "disc type (cdrom,dvdrom,bdrom,"
    294   1.8  reinoud 			"dvdram,bdre,disk,cdr,dvdr,bdr,cdrw,dvdrw)"),
    295   1.8  reinoud 		OPT_STR('L', "loglabel", "\"logical volume name\""),
    296   1.9  reinoud 		OPT_STR('P', "discid",   "\"[volset name ':']"
    297   1.9  reinoud 			"physical volume name\""),
    298   1.9  reinoud 		OPT_NUM('t', "tz", gmtoff, -24, 24, "timezone"),
    299   1.9  reinoud 		OPT_STR('v', "minver", "minimum UDF version in either "
    300   1.9  reinoud 			"``0x201'' or ``2.01'' format"),
    301   1.9  reinoud #if notyet
    302   1.9  reinoud 		OPT_STR('V', "maxver", "maximum UDF version in either "
    303   1.9  reinoud 			"``0x201'' or ``2.01'' format"),
    304   1.9  reinoud #endif
    305   1.1  reinoud 		{ .name = NULL }
    306   1.1  reinoud 	};
    307   1.1  reinoud 
    308   1.1  reinoud 	/* initialise */
    309   1.1  reinoud 	format_str    = strdup("");
    310   1.1  reinoud 	req_enable    = req_disable = 0;
    311   1.1  reinoud 	format_flags  = FORMAT_INVALID;
    312   1.1  reinoud 	fsopts->sectorsize = 512;	/* minimum allowed sector size */
    313   1.1  reinoud 
    314   1.1  reinoud 	srandom((unsigned long) time(NULL));
    315   1.1  reinoud 
    316   1.1  reinoud 	mmc_profile = 0x01;		/* 'disc'/file */
    317   1.1  reinoud 
    318   1.1  reinoud 	udf_init_create_context();
    319  1.10  reinoud 	context.app_name         = "*NetBSD makefs";
    320   1.1  reinoud 	context.app_version_main = APP_VERSION_MAIN;
    321   1.1  reinoud 	context.app_version_sub  = APP_VERSION_SUB;
    322  1.10  reinoud 	context.impl_name        = IMPL_NAME;
    323   1.1  reinoud 
    324   1.1  reinoud 	/* minimum and maximum UDF versions we advise */
    325   1.1  reinoud 	context.min_udf = 0x102;
    326   1.9  reinoud 	context.max_udf = 0x201;	/* 0x250 and 0x260 are not ready */
    327   1.1  reinoud 
    328   1.1  reinoud 	/* use user's time zone as default */
    329   1.1  reinoud 	(void)time(&now);
    330   1.1  reinoud 	tm = localtime(&now);
    331   1.1  reinoud 	context.gmtoff = tm->tm_gmtoff;
    332   1.1  reinoud 
    333   1.1  reinoud 	/* return info */
    334   1.1  reinoud 	fsopts->fs_specific = NULL;
    335   1.1  reinoud 	fsopts->fs_options = copy_opts(udf_options);
    336   1.1  reinoud }
    337   1.1  reinoud 
    338   1.1  reinoud 
    339   1.1  reinoud void
    340   1.1  reinoud udf_cleanup_opts(fsinfo_t *fsopts)
    341   1.1  reinoud {
    342   1.1  reinoud 	free(fsopts->fs_options);
    343   1.1  reinoud }
    344   1.1  reinoud 
    345   1.1  reinoud 
    346   1.9  reinoud /* ----- included from newfs_udf.c ------ */
    347   1.9  reinoud /* ----- */
    348   1.9  reinoud 
    349   1.9  reinoud 
    350   1.7  reinoud #define CDRSIZE    ((uint64_t)   700*1024*1024)	/* small approx */
    351   1.7  reinoud #define CDRWSIZE   ((uint64_t)   576*1024*1024)	/* small approx */
    352   1.7  reinoud #define DVDRSIZE   ((uint64_t)  4488*1024*1024)	/* small approx */
    353   1.7  reinoud #define DVDRAMSIZE ((uint64_t)  4330*1024*1024)	/* small approx with spare */
    354   1.7  reinoud #define DVDRWSIZE  ((uint64_t)  4482*1024*1024)	/* small approx */
    355   1.7  reinoud #define BDRSIZE    ((uint64_t) 23866*1024*1024)	/* small approx */
    356   1.7  reinoud #define BDRESIZE   ((uint64_t) 23098*1024*1024)	/* small approx */
    357   1.1  reinoud int
    358   1.1  reinoud udf_parse_opts(const char *option, fsinfo_t *fsopts)
    359   1.1  reinoud {
    360   1.1  reinoud 	option_t *udf_options = fsopts->fs_options;
    361   1.7  reinoud 	uint64_t stdsize;
    362   1.7  reinoud 	uint32_t set_sectorsize;
    363   1.1  reinoud 	const char *name, *desc;
    364   1.8  reinoud 	char buffer[1024], *buf, *colon;
    365   1.1  reinoud 	int i;
    366   1.1  reinoud 
    367   1.1  reinoud 	assert(option != NULL);
    368   1.1  reinoud 
    369   1.1  reinoud 	if (debug & DEBUG_FS_PARSE_OPTS)
    370   1.1  reinoud 		printf("udf_parse_opts: got `%s'\n", option);
    371   1.1  reinoud 
    372   1.8  reinoud 	i = set_option(udf_options, option, buffer, sizeof(buffer));
    373   1.1  reinoud 	if (i == -1)
    374   1.1  reinoud 		return 0;
    375   1.1  reinoud 
    376   1.1  reinoud 	if (udf_options[i].name == NULL)
    377   1.1  reinoud 		abort();
    378   1.1  reinoud 
    379   1.7  reinoud 	set_sectorsize = 0;
    380   1.7  reinoud 	stdsize = 0;
    381   1.7  reinoud 
    382   1.8  reinoud 	buf = buffer;
    383   1.1  reinoud 	name = udf_options[i].name;
    384   1.1  reinoud 	desc = udf_options[i].desc;
    385   1.1  reinoud 	switch (udf_options[i].letter) {
    386   1.1  reinoud 	case 'T':
    387   1.1  reinoud 		if (strcmp(buf, "cdrom") == 0) {
    388   1.1  reinoud 			mmc_profile = 0x00;
    389   1.1  reinoud 		} else if (strcmp(buf, "dvdrom") == 0) {
    390   1.1  reinoud 			mmc_profile = 0x10;
    391   1.7  reinoud 		} else if (strcmp(buf, "bdrom") == 0) {
    392   1.7  reinoud 			mmc_profile = 0x40;
    393   1.1  reinoud 		} else if (strcmp(buf, "dvdram") == 0) {
    394   1.1  reinoud 			mmc_profile = 0x12;
    395   1.7  reinoud 			stdsize = DVDRAMSIZE;
    396   1.1  reinoud 		} else if (strcmp(buf, "bdre") == 0) {
    397   1.1  reinoud 			mmc_profile = 0x43;
    398   1.7  reinoud 			stdsize = BDRESIZE;
    399   1.1  reinoud 		} else if (strcmp(buf, "disk") == 0) {
    400   1.1  reinoud 			mmc_profile = 0x01;
    401   1.1  reinoud 		} else if (strcmp(buf, "cdr") == 0) {
    402   1.1  reinoud 			mmc_profile = 0x09;
    403   1.7  reinoud 			stdsize = CDRSIZE;
    404   1.1  reinoud 		} else if (strcmp(buf, "dvdr") == 0) {
    405   1.1  reinoud 			mmc_profile = 0x1b;
    406   1.7  reinoud 			stdsize = DVDRSIZE;
    407   1.7  reinoud 		} else if (strcmp(buf, "bdr") == 0) {
    408   1.7  reinoud 			mmc_profile = 0x41;
    409   1.7  reinoud 			stdsize = BDRSIZE;
    410   1.1  reinoud 		} else if (strcmp(buf, "cdrw") == 0) {
    411   1.1  reinoud 			mmc_profile = 0x0a;
    412   1.7  reinoud 			stdsize = CDRWSIZE;
    413   1.1  reinoud 		} else if (strcmp(buf, "dvdrw") == 0) {
    414   1.1  reinoud 			mmc_profile = 0x13;
    415   1.7  reinoud 			stdsize = DVDRWSIZE;
    416   1.1  reinoud 		} else {
    417   1.1  reinoud 			errx(EINVAL, "Unknown or unimplemented disc format");
    418   1.1  reinoud 			return 0;
    419   1.1  reinoud 		}
    420   1.7  reinoud 		if (mmc_profile != 0x01)
    421   1.7  reinoud 			set_sectorsize = 2048;
    422   1.8  reinoud 		break;
    423   1.8  reinoud 	case 'L':
    424   1.8  reinoud 		if (context.logvol_name) free(context.logvol_name);
    425   1.8  reinoud 		context.logvol_name = strdup(buf);
    426   1.8  reinoud 		break;
    427   1.8  reinoud 	case 'P':
    428   1.8  reinoud 		if ((colon = strstr(buf, ":"))) {
    429   1.8  reinoud 			if (context.volset_name)
    430   1.8  reinoud 				free(context.volset_name);
    431   1.8  reinoud 			*colon = 0;
    432   1.8  reinoud 			context.volset_name = strdup(buf);
    433   1.8  reinoud 			buf = colon+1;
    434   1.8  reinoud 		}
    435   1.8  reinoud 		if (context.primary_name)
    436   1.8  reinoud 			free(context.primary_name);
    437   1.8  reinoud 		if ((strstr(buf, ":"))) {
    438   1.9  reinoud 			errx(EINVAL, "primary name can't have ':' in its name");
    439   1.8  reinoud 			return 0;
    440   1.8  reinoud 		}
    441   1.8  reinoud 		context.primary_name = strdup(buf);
    442   1.8  reinoud 		break;
    443   1.9  reinoud 	case 'v':
    444   1.9  reinoud 		context.min_udf = a_udf_version(buf, "min_udf");
    445   1.9  reinoud 		if (context.min_udf > 0x201) {
    446   1.9  reinoud 			errx(EINVAL, "maximum supported version is UDF 2.01");
    447   1.9  reinoud 			return 0;
    448   1.9  reinoud 		}
    449   1.9  reinoud 		if (context.min_udf > context.max_udf)
    450   1.9  reinoud 			context.max_udf = context.min_udf;
    451   1.9  reinoud 		break;
    452   1.1  reinoud 	}
    453   1.7  reinoud 	if (set_sectorsize)
    454   1.7  reinoud 		fsopts->sectorsize = set_sectorsize;
    455   1.7  reinoud 	if (stdsize)
    456   1.7  reinoud 		fsopts->size = stdsize;
    457   1.1  reinoud 	return 1;
    458   1.1  reinoud }
    459   1.1  reinoud 
    460   1.1  reinoud /* --------------------------------------------------------------------- */
    461   1.1  reinoud 
    462   1.1  reinoud struct udf_stats {
    463   1.1  reinoud 	uint32_t nfiles;
    464   1.1  reinoud 	uint32_t ndirs;
    465   1.1  reinoud 	uint32_t ndescr;
    466   1.1  reinoud 	uint32_t nmetadatablocks;
    467   1.1  reinoud 	uint32_t ndatablocks;
    468   1.1  reinoud };
    469   1.1  reinoud 
    470   1.1  reinoud 
    471   1.1  reinoud /* node reference administration */
    472   1.1  reinoud static void
    473   1.1  reinoud udf_inc_link(union dscrptr *dscr)
    474   1.1  reinoud {
    475   1.1  reinoud 	struct file_entry *fe;
    476   1.1  reinoud 	struct extfile_entry *efe;
    477   1.1  reinoud 
    478   1.1  reinoud 	if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
    479   1.1  reinoud 		fe        = &dscr->fe;
    480   1.1  reinoud 		fe->link_cnt = udf_rw16(udf_rw16(fe->link_cnt) + 1);
    481   1.1  reinoud 	} else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
    482   1.1  reinoud 		efe       = &dscr->efe;
    483   1.1  reinoud 		efe->link_cnt = udf_rw16(udf_rw16(efe->link_cnt) + 1);
    484   1.1  reinoud 	} else {
    485   1.1  reinoud 		errx(1, "Bad tag passed to udf_inc_link");
    486   1.1  reinoud 	}
    487   1.1  reinoud }
    488   1.1  reinoud 
    489   1.1  reinoud 
    490   1.1  reinoud static void
    491   1.1  reinoud udf_set_link_cnt(union dscrptr *dscr, int num)
    492   1.1  reinoud {
    493   1.1  reinoud 	struct file_entry *fe;
    494   1.1  reinoud 	struct extfile_entry *efe;
    495   1.1  reinoud 
    496   1.1  reinoud 	if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
    497   1.1  reinoud 		fe        = &dscr->fe;
    498   1.1  reinoud 		fe->link_cnt = udf_rw16(num);
    499   1.1  reinoud 	} else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
    500   1.1  reinoud 		efe       = &dscr->efe;
    501   1.1  reinoud 		efe->link_cnt = udf_rw16(num);
    502   1.1  reinoud 	} else {
    503   1.1  reinoud 		errx(1, "Bad tag passed to udf_set_link_cnt");
    504   1.1  reinoud 	}
    505   1.1  reinoud }
    506   1.1  reinoud 
    507   1.1  reinoud 
    508   1.1  reinoud static uint32_t
    509   1.1  reinoud udf_datablocks(off_t sz)
    510   1.1  reinoud {
    511   1.1  reinoud 	/* predictor if it can be written inside the node */
    512   1.1  reinoud 	if (sz < context.sector_size - UDF_EXTFENTRY_SIZE - 16)
    513   1.1  reinoud 		return 0;
    514   1.1  reinoud 
    515   1.1  reinoud 	return UDF_ROUNDUP(sz, context.sector_size) / context.sector_size;
    516   1.1  reinoud }
    517   1.1  reinoud 
    518   1.1  reinoud 
    519   1.1  reinoud static void
    520   1.1  reinoud udf_prepare_fids(struct long_ad *dir_icb, struct long_ad *dirdata_icb,
    521   1.1  reinoud 		uint8_t *dirdata, uint32_t dirdata_size)
    522   1.1  reinoud {
    523   1.1  reinoud 	struct fileid_desc *fid;
    524   1.1  reinoud 	struct long_ad     *icb;
    525   1.1  reinoud 	uint32_t fidsize, offset;
    526   1.1  reinoud 	uint32_t location;
    527   1.1  reinoud 
    528   1.1  reinoud 	if (udf_datablocks(dirdata_size) == 0) {
    529   1.1  reinoud 		/* going internal */
    530   1.1  reinoud 		icb = dir_icb;
    531   1.1  reinoud 	} else {
    532   1.1  reinoud 		/* external blocks to write to */
    533   1.1  reinoud 		icb = dirdata_icb;
    534   1.1  reinoud 	}
    535   1.1  reinoud 
    536   1.1  reinoud 	for (offset = 0; offset < dirdata_size; offset += fidsize) {
    537   1.1  reinoud 		/* for each FID: */
    538   1.1  reinoud 		fid = (struct fileid_desc *) (dirdata + offset);
    539   1.1  reinoud 		assert(udf_rw16(fid->tag.id) == TAGID_FID);
    540   1.1  reinoud 
    541   1.1  reinoud 		location  = udf_rw32(icb->loc.lb_num);
    542   1.1  reinoud 		location += offset / context.sector_size;
    543   1.1  reinoud 
    544   1.1  reinoud 		fid->tag.tag_loc = udf_rw32(location);
    545   1.1  reinoud 		udf_validate_tag_and_crc_sums((union dscrptr *) fid);
    546   1.1  reinoud 
    547   1.1  reinoud 		fidsize = udf_fidsize(fid);
    548   1.1  reinoud 	}
    549   1.1  reinoud }
    550   1.1  reinoud 
    551   1.1  reinoud static int
    552   1.1  reinoud udf_file_inject_blob(union dscrptr *dscr,  uint8_t *blob, size_t size)
    553   1.1  reinoud {
    554   1.1  reinoud 	struct icb_tag *icb;
    555   1.1  reinoud 	struct file_entry *fe;
    556   1.1  reinoud 	struct extfile_entry *efe;
    557   1.1  reinoud 	uint64_t inf_len, obj_size;
    558   1.1  reinoud 	uint32_t l_ea, l_ad;
    559   1.1  reinoud 	uint32_t free_space, desc_size;
    560   1.1  reinoud 	uint16_t crclen;
    561   1.1  reinoud 	uint8_t *data, *pos;
    562   1.1  reinoud 
    563   1.1  reinoud 	fe = NULL;
    564   1.1  reinoud 	efe = NULL;
    565   1.1  reinoud 	if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
    566   1.1  reinoud 		fe        = &dscr->fe;
    567   1.1  reinoud 		data      = fe->data;
    568   1.1  reinoud 		l_ea      = udf_rw32(fe->l_ea);
    569   1.1  reinoud 		l_ad      = udf_rw32(fe->l_ad);
    570   1.1  reinoud 		icb       = &fe->icbtag;
    571   1.1  reinoud 		inf_len   = udf_rw64(fe->inf_len);
    572   1.1  reinoud 		obj_size  = 0;
    573   1.1  reinoud 		desc_size = sizeof(struct file_entry);
    574   1.1  reinoud 	} else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
    575   1.1  reinoud 		efe       = &dscr->efe;
    576   1.1  reinoud 		data      = efe->data;
    577   1.1  reinoud 		l_ea      = udf_rw32(efe->l_ea);
    578   1.1  reinoud 		l_ad      = udf_rw32(efe->l_ad);
    579   1.1  reinoud 		icb       = &efe->icbtag;
    580   1.1  reinoud 		inf_len   = udf_rw64(efe->inf_len);
    581   1.1  reinoud 		obj_size  = udf_rw64(efe->obj_size);
    582   1.1  reinoud 		desc_size = sizeof(struct extfile_entry);
    583   1.1  reinoud 	} else {
    584   1.1  reinoud 		errx(1, "Bad tag passed to udf_file_inject_blob");
    585   1.1  reinoud 	}
    586   1.1  reinoud 	crclen = udf_rw16(dscr->tag.desc_crc_len);
    587   1.1  reinoud 
    588   1.1  reinoud 	/* calculate free space */
    589   1.1  reinoud 	free_space = context.sector_size - (l_ea + l_ad) - desc_size;
    590   1.1  reinoud 	if (udf_datablocks(size)) {
    591   1.1  reinoud 		assert(free_space < size);
    592   1.1  reinoud 		return 1;
    593   1.1  reinoud 	}
    594   1.1  reinoud 
    595   1.1  reinoud 	/* going internal */
    596   1.1  reinoud 	assert(l_ad == 0);
    597   1.6  reinoud 	assert((udf_rw16(icb->flags) & UDF_ICB_TAG_FLAGS_ALLOC_MASK) ==
    598   1.6  reinoud 			UDF_ICB_INTERN_ALLOC);
    599   1.1  reinoud 
    600   1.1  reinoud 	// assert(free_space >= size);
    601   1.1  reinoud 	pos = data + l_ea + l_ad;
    602   1.1  reinoud 	memcpy(pos, blob, size);
    603   1.1  reinoud 	l_ad   += size;
    604   1.1  reinoud 	crclen += size;
    605   1.1  reinoud 
    606   1.1  reinoud 	inf_len  += size;
    607   1.1  reinoud 	obj_size += size;
    608   1.1  reinoud 
    609   1.1  reinoud 	if (fe) {
    610   1.1  reinoud 		fe->l_ad = udf_rw32(l_ad);
    611   1.1  reinoud 		fe->inf_len = udf_rw64(inf_len);
    612   1.1  reinoud 	} else if (efe) {
    613   1.1  reinoud 		efe->l_ad = udf_rw32(l_ad);
    614   1.1  reinoud 		efe->inf_len  = udf_rw64(inf_len);
    615   1.1  reinoud 		efe->obj_size = udf_rw64(inf_len);
    616   1.1  reinoud 	}
    617   1.1  reinoud 
    618   1.1  reinoud 	/* make sure the header sums stays correct */
    619   1.1  reinoud 	dscr->tag.desc_crc_len = udf_rw16(crclen);
    620   1.1  reinoud 	udf_validate_tag_and_crc_sums(dscr);
    621   1.1  reinoud 
    622   1.1  reinoud 	return 0;
    623   1.1  reinoud }
    624   1.1  reinoud 
    625   1.1  reinoud 
    626   1.1  reinoud /* XXX no sparse file support */
    627   1.1  reinoud static void
    628   1.1  reinoud udf_append_file_mapping(union dscrptr *dscr, struct long_ad *piece)
    629   1.1  reinoud {
    630   1.1  reinoud 	struct icb_tag *icb;
    631   1.1  reinoud 	struct file_entry *fe;
    632   1.1  reinoud 	struct extfile_entry *efe;
    633   1.1  reinoud 	struct long_ad *last_long, last_piece;
    634   1.1  reinoud 	struct short_ad *last_short, new_short;
    635   1.1  reinoud 	uint64_t inf_len, obj_size, logblks_rec;
    636   1.1  reinoud 	uint32_t l_ea, l_ad, size;
    637   1.1  reinoud 	uint32_t last_lb_num, piece_lb_num;
    638   1.1  reinoud 	uint32_t last_len, piece_len, last_flags;
    639   1.1  reinoud 	uint32_t rest_len, merge_len, last_end;
    640   1.1  reinoud 	uint16_t last_part_num, piece_part_num;
    641   1.1  reinoud 	uint16_t crclen, cur_alloc;
    642   1.1  reinoud 	uint8_t *data, *pos;
    643   1.1  reinoud 	const int short_len = sizeof(struct short_ad);
    644   1.1  reinoud 	const int long_len  = sizeof(struct long_ad);
    645   1.1  reinoud 	const int sector_size = context.sector_size;
    646   1.1  reinoud 	const int use_shorts = (context.data_part == context.metadata_part);
    647   1.1  reinoud 	uint64_t max_len = UDF_ROUNDDOWN(UDF_EXT_MAXLEN, sector_size);
    648   1.1  reinoud 
    649   1.1  reinoud 	if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
    650   1.1  reinoud 		fe          = &dscr->fe;
    651   1.1  reinoud 		data        =    fe->data;
    652   1.1  reinoud 		l_ea        = fe->l_ea;
    653   1.1  reinoud 		l_ad        = udf_rw32(fe->l_ad);
    654   1.1  reinoud 		icb         = &fe->icbtag;
    655   1.1  reinoud 		inf_len     = udf_rw64(fe->inf_len);
    656   1.1  reinoud 		logblks_rec = udf_rw64(fe->logblks_rec);
    657   1.1  reinoud 		obj_size = 0;
    658   1.1  reinoud 	} else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
    659   1.1  reinoud 		efe         = &dscr->efe;
    660   1.1  reinoud 		data        = efe->data;
    661   1.1  reinoud 		l_ea        = efe->l_ea;
    662   1.1  reinoud 		l_ad        = udf_rw32(efe->l_ad);
    663   1.1  reinoud 		icb         = &efe->icbtag;
    664   1.1  reinoud 		inf_len     = udf_rw64(efe->inf_len);
    665   1.1  reinoud 		obj_size    = udf_rw64(efe->obj_size);
    666   1.1  reinoud 		logblks_rec = udf_rw64(efe->logblks_rec);
    667   1.1  reinoud 	} else {
    668   1.1  reinoud 		errx(1, "Bad tag passed to udf_file_append_blob");
    669   1.1  reinoud 	}
    670   1.1  reinoud 	crclen = udf_rw16(dscr->tag.desc_crc_len);
    671   1.1  reinoud 
    672   1.1  reinoud 	pos = data + l_ea;
    673   1.1  reinoud 	cur_alloc = udf_rw16(icb->flags);
    674   1.1  reinoud 	size = UDF_EXT_LEN(udf_rw32(piece->len));
    675   1.1  reinoud 
    676   1.1  reinoud 	/* extract last entry as a long_ad */
    677   1.2  reinoud 	memset(&last_piece, 0, sizeof(last_piece));
    678   1.2  reinoud 	last_len      = 0;
    679   1.2  reinoud 	last_lb_num   = 0;
    680   1.2  reinoud 	last_part_num = 0;
    681   1.1  reinoud 	if (l_ad != 0) {
    682   1.1  reinoud 		if (use_shorts) {
    683   1.1  reinoud 			assert(cur_alloc == UDF_ICB_SHORT_ALLOC);
    684   1.1  reinoud 			pos += l_ad - short_len;
    685   1.1  reinoud 			last_short   = (struct short_ad *) pos;
    686   1.1  reinoud 			last_lb_num  = udf_rw32(last_short->lb_num);
    687   1.1  reinoud 			last_part_num = udf_rw16(piece->loc.part_num);
    688   1.1  reinoud 			last_len     = UDF_EXT_LEN(udf_rw32(last_short->len));
    689   1.1  reinoud 			last_flags   = UDF_EXT_FLAGS(udf_rw32(last_short->len));
    690   1.1  reinoud 		} else {
    691   1.1  reinoud 			assert(cur_alloc == UDF_ICB_LONG_ALLOC);
    692   1.1  reinoud 			pos += l_ad - long_len;
    693   1.1  reinoud 			last_long    = (struct long_ad *) pos;
    694   1.1  reinoud 			last_lb_num  = udf_rw32(last_long->loc.lb_num);
    695   1.1  reinoud 			last_part_num = udf_rw16(last_long->loc.part_num);
    696   1.1  reinoud 			last_len     = UDF_EXT_LEN(udf_rw32(last_long->len));
    697   1.1  reinoud 			last_flags   = UDF_EXT_FLAGS(udf_rw32(last_long->len));
    698   1.1  reinoud 		}
    699   1.1  reinoud 	}
    700   1.1  reinoud 
    701   1.1  reinoud 	piece_len      = UDF_EXT_LEN(udf_rw32(piece->len));
    702   1.1  reinoud 	piece_lb_num   = udf_rw32(piece->loc.lb_num);
    703   1.1  reinoud 	piece_part_num = udf_rw16(piece->loc.part_num);
    704   1.1  reinoud 
    705   1.1  reinoud 	/* try merging */
    706   1.1  reinoud 	rest_len  = max_len - last_len;
    707   1.1  reinoud 
    708   1.1  reinoud 	merge_len = MIN(udf_rw32(piece->len), rest_len);
    709   1.1  reinoud 	last_end  = last_lb_num + (last_len / sector_size);
    710   1.1  reinoud 
    711   1.1  reinoud 	if ((piece_lb_num == last_end) && (last_part_num == piece_part_num)) {
    712   1.1  reinoud 		/* we can merge */
    713   1.1  reinoud 		last_len  += merge_len;
    714   1.1  reinoud 		piece_len -= merge_len;
    715   1.1  reinoud 
    716   1.1  reinoud 		/* write back merge result */
    717   1.1  reinoud 		if (use_shorts) {
    718   1.1  reinoud 			last_short->len = udf_rw32(last_len | last_flags);
    719   1.1  reinoud 		} else {
    720   1.1  reinoud 			last_long->len  = udf_rw32(last_len | last_flags);
    721   1.1  reinoud 		}
    722   1.1  reinoud 		piece_lb_num += merge_len / sector_size;
    723   1.1  reinoud 	}
    724   1.1  reinoud 
    725   1.1  reinoud 	if (piece_len) {
    726   1.1  reinoud 		/* append new entry */
    727   1.1  reinoud 		pos = data + l_ea + l_ad;
    728   1.1  reinoud 		if (use_shorts) {
    729   1.1  reinoud 			icb->flags = udf_rw16(UDF_ICB_SHORT_ALLOC);
    730   1.1  reinoud 			memset(&new_short, 0, short_len);
    731   1.1  reinoud 			new_short.len    = udf_rw32(piece_len);
    732   1.1  reinoud 			new_short.lb_num = udf_rw32(piece_lb_num);
    733   1.1  reinoud 			memcpy(pos, &new_short, short_len);
    734   1.1  reinoud 			l_ad += short_len;
    735   1.1  reinoud 			crclen += short_len;
    736   1.1  reinoud 		} else {
    737   1.1  reinoud 			icb->flags = udf_rw16(UDF_ICB_LONG_ALLOC);
    738   1.1  reinoud 			piece->len        = udf_rw32(piece_len);
    739   1.1  reinoud 			piece->loc.lb_num = udf_rw32(piece_lb_num);
    740   1.1  reinoud 			memcpy(pos, piece, long_len);
    741   1.1  reinoud 			l_ad += long_len;
    742   1.1  reinoud 			crclen += long_len;
    743   1.1  reinoud 		}
    744   1.1  reinoud 	}
    745   1.1  reinoud 	piece->len = udf_rw32(0);
    746   1.1  reinoud 
    747   1.1  reinoud 	inf_len  += size;
    748   1.1  reinoud 	obj_size += size;
    749   1.1  reinoud 	logblks_rec += UDF_ROUNDUP(size, sector_size) / sector_size;
    750   1.1  reinoud 
    751   1.1  reinoud 	dscr->tag.desc_crc_len = udf_rw16(crclen);
    752   1.1  reinoud 	if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
    753   1.1  reinoud 		fe->l_ad = udf_rw32(l_ad);
    754   1.1  reinoud 		fe->inf_len = udf_rw64(inf_len);
    755   1.1  reinoud 		fe->logblks_rec = udf_rw64(logblks_rec);
    756   1.1  reinoud 	} else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
    757   1.1  reinoud 		efe->l_ad = udf_rw32(l_ad);
    758   1.1  reinoud 		efe->inf_len  = udf_rw64(inf_len);
    759   1.1  reinoud 		efe->obj_size = udf_rw64(inf_len);
    760   1.1  reinoud 		efe->logblks_rec = udf_rw64(logblks_rec);
    761   1.1  reinoud 	}
    762   1.1  reinoud }
    763   1.1  reinoud 
    764   1.1  reinoud 
    765   1.1  reinoud static int
    766   1.1  reinoud udf_append_file_contents(union dscrptr *dscr, struct long_ad *data_icb,
    767   1.1  reinoud 		uint8_t *fdata, size_t flen)
    768   1.1  reinoud {
    769   1.1  reinoud 	struct long_ad icb;
    770   1.1  reinoud 	uint32_t location;
    771   1.1  reinoud 	uint32_t phys;
    772   1.1  reinoud 	uint16_t vpart;
    773   1.1  reinoud 	uint8_t *bpos;
    774   1.1  reinoud 	int cnt, sects;
    775   1.1  reinoud 	int error;
    776   1.1  reinoud 
    777   1.1  reinoud 	if (udf_file_inject_blob(dscr, fdata, flen) == 0)
    778   1.1  reinoud 		return 0;
    779   1.1  reinoud 
    780   1.1  reinoud 	/* has to be appended in mappings */
    781   1.1  reinoud 	icb = *data_icb;
    782   1.1  reinoud 	icb.len = udf_rw32(flen);
    783   1.1  reinoud 	while (udf_rw32(icb.len) > 0)
    784   1.1  reinoud 		udf_append_file_mapping(dscr, &icb);
    785   1.1  reinoud 	udf_validate_tag_and_crc_sums(dscr);
    786   1.1  reinoud 
    787   1.1  reinoud 	/* write out data piece */
    788   1.1  reinoud 	vpart    = udf_rw16(data_icb->loc.part_num);
    789   1.1  reinoud 	location = udf_rw32(data_icb->loc.lb_num);
    790   1.1  reinoud 	sects    = udf_datablocks(flen);
    791   1.1  reinoud 	for (cnt = 0; cnt < sects; cnt++) {
    792   1.1  reinoud 		bpos = fdata + cnt*context.sector_size;
    793   1.1  reinoud 		phys = context.vtop_offset[vpart] + location + cnt;
    794   1.1  reinoud 		error = udf_write_sector(bpos, phys);
    795   1.1  reinoud 		if (error)
    796   1.1  reinoud 			return error;
    797   1.1  reinoud 	}
    798   1.1  reinoud 	return 0;
    799   1.1  reinoud }
    800   1.1  reinoud 
    801   1.1  reinoud 
    802   1.1  reinoud static int
    803   1.1  reinoud udf_create_new_file(struct stat *st, union dscrptr **dscr,
    804   1.1  reinoud 	int filetype, struct long_ad *icb)
    805   1.1  reinoud {
    806   1.1  reinoud 	struct file_entry *fe;
    807   1.1  reinoud 	struct extfile_entry *efe;
    808   1.1  reinoud 	int error;
    809   1.1  reinoud 
    810   1.1  reinoud 	fe = NULL;
    811   1.1  reinoud 	efe = NULL;
    812   1.1  reinoud 	if (context.dscrver == 2) {
    813   1.1  reinoud 		error = udf_create_new_fe(&fe, filetype, st);
    814   1.1  reinoud 		if (error)
    815   1.1  reinoud 			errx(error, "can't create fe");
    816   1.1  reinoud 		*dscr = (union dscrptr *) fe;
    817   1.1  reinoud 		icb->longad_uniqueid = fe->unique_id;
    818   1.1  reinoud 	} else {
    819   1.1  reinoud 		error = udf_create_new_efe(&efe, filetype, st);
    820   1.1  reinoud 		if (error)
    821   1.1  reinoud 			errx(error, "can't create fe");
    822   1.1  reinoud 		*dscr = (union dscrptr *) efe;
    823   1.1  reinoud 		icb->longad_uniqueid = efe->unique_id;
    824   1.1  reinoud 	}
    825   1.1  reinoud 
    826   1.1  reinoud 	return 0;
    827   1.1  reinoud }
    828   1.1  reinoud 
    829   1.1  reinoud 
    830   1.1  reinoud static void
    831   1.1  reinoud udf_estimate_walk(fsinfo_t *fsopts,
    832   1.1  reinoud 		fsnode *root, char *dir, struct udf_stats *stats)
    833   1.1  reinoud {
    834   1.1  reinoud 	struct fileid_desc *fid;
    835   1.1  reinoud 	struct long_ad dummy_ref;
    836   1.1  reinoud 	fsnode *cur;
    837   1.1  reinoud 	fsinode *fnode;
    838   1.1  reinoud 	size_t pathlen = strlen(dir);
    839   1.1  reinoud 	char *mydir = dir + pathlen;
    840   1.1  reinoud 	off_t sz;
    841   1.1  reinoud 	uint32_t nblk, ddoff;
    842   1.1  reinoud 	uint32_t softlink_len;
    843   1.1  reinoud 	uint8_t *softlink_buf;
    844   1.1  reinoud 	int nentries;
    845   1.1  reinoud 	int error;
    846   1.1  reinoud 
    847   1.1  reinoud 	stats->ndirs++;
    848   1.1  reinoud 
    849   1.1  reinoud 	/*
    850   1.1  reinoud 	 * Count number of directory entries and count directory size; needed
    851   1.1  reinoud 	 * for the reservation of enough space for the directory. Pity we
    852   1.1  reinoud 	 * don't keep the FIDs we created. If it turns out to be a issue we
    853   1.1  reinoud 	 * can cache it later.
    854   1.1  reinoud 	 */
    855   1.1  reinoud 	fid = (struct fileid_desc *) malloc(context.sector_size);
    856   1.1  reinoud 	assert(fid);
    857   1.1  reinoud 
    858   1.1  reinoud 	ddoff = 40;	/* '..' entry */
    859   1.1  reinoud 	for (cur = root, nentries = 0; cur != NULL; cur = cur->next) {
    860   1.1  reinoud 		switch (cur->type & S_IFMT) {
    861   1.1  reinoud 		default:
    862   1.1  reinoud 			/* what kind of nodes? */
    863   1.1  reinoud 			break;
    864   1.1  reinoud 		case S_IFCHR:
    865   1.1  reinoud 		case S_IFBLK:
    866   1.1  reinoud 			/* not supported yet */
    867   1.1  reinoud 			continue;
    868   1.1  reinoud 		case S_IFDIR:
    869   1.1  reinoud 			if (strcmp(cur->name, ".") == 0)
    870   1.1  reinoud 				continue;
    871   1.1  reinoud 		case S_IFLNK:
    872   1.1  reinoud 		case S_IFREG:
    873   1.1  reinoud 			/* create dummy FID to see how long name will become */
    874   1.1  reinoud 			udf_create_fid(ddoff, fid, cur->name, 0, &dummy_ref);
    875   1.1  reinoud 
    876   1.1  reinoud 			nentries++;
    877   1.1  reinoud 			ddoff += udf_fidsize(fid);
    878   1.1  reinoud 		}
    879   1.1  reinoud 	}
    880   1.1  reinoud 	sz = ddoff;
    881   1.1  reinoud 
    882   1.1  reinoud 	root->inode->st.st_size = sz;	/* max now */
    883   1.1  reinoud 	root->inode->flags |= FI_SIZED;
    884   1.1  reinoud 
    885   1.1  reinoud 	nblk = udf_datablocks(sz);
    886   1.1  reinoud 	stats->nmetadatablocks += nblk;
    887   1.1  reinoud 
    888   1.1  reinoud 	/* for each entry in the directory, there needs to be a (E)FE */
    889   1.1  reinoud 	stats->nmetadatablocks += nentries + 1;
    890   1.1  reinoud 
    891   1.1  reinoud 	/* recurse */
    892   1.1  reinoud 	for (cur = root; cur != NULL; cur = cur->next) {
    893   1.1  reinoud 		switch (cur->type & S_IFMT) {
    894   1.1  reinoud 		default:
    895   1.1  reinoud 			/* what kind of nodes? */
    896   1.1  reinoud 			break;
    897   1.1  reinoud 		case S_IFCHR:
    898   1.1  reinoud 		case S_IFBLK:
    899   1.1  reinoud 			/* not supported yet */
    900   1.1  reinoud 			// stats->nfiles++;
    901   1.1  reinoud 			break;
    902   1.1  reinoud 		case S_IFDIR:
    903   1.1  reinoud 			if (strcmp(cur->name, ".") == 0)
    904   1.1  reinoud 				continue;
    905   1.1  reinoud 			/* empty dir? */
    906   1.1  reinoud 			if (!cur->child)
    907   1.1  reinoud 				break;
    908   1.1  reinoud 			mydir[0] = '/';
    909   1.1  reinoud 			strncpy(&mydir[1], cur->name, MAXPATHLEN - pathlen);
    910   1.1  reinoud 			udf_estimate_walk(fsopts, cur->child, dir, stats);
    911   1.1  reinoud 			mydir[0] = '\0';
    912   1.1  reinoud 			break;
    913   1.1  reinoud 		case S_IFREG:
    914   1.1  reinoud 			fnode = cur->inode;
    915   1.1  reinoud 			/* don't double-count hard-links */
    916   1.1  reinoud 			if (!(fnode->flags & FI_SIZED)) {
    917   1.1  reinoud 				sz = fnode->st.st_size;
    918   1.1  reinoud 				nblk = udf_datablocks(sz);
    919   1.1  reinoud 				stats->ndatablocks += nblk;
    920   1.1  reinoud 				/* ... */
    921   1.1  reinoud 				fnode->flags |= FI_SIZED;
    922   1.1  reinoud 			}
    923   1.1  reinoud 			stats->nfiles++;
    924   1.1  reinoud 			break;
    925   1.1  reinoud 		case S_IFLNK:
    926   1.1  reinoud 			/* softlink */
    927   1.1  reinoud 			fnode = cur->inode;
    928   1.1  reinoud 			/* don't double-count hard-links */
    929   1.1  reinoud 			if (!(fnode->flags & FI_SIZED)) {
    930   1.1  reinoud 				error = udf_encode_symlink(&softlink_buf,
    931   1.1  reinoud 						&softlink_len, cur->symlink);
    932   1.1  reinoud 				if (error) {
    933   1.1  reinoud 					printf("SOFTLINK error %d\n", error);
    934   1.1  reinoud 					break;
    935   1.1  reinoud 				}
    936   1.1  reinoud 				nblk = udf_datablocks(softlink_len);
    937   1.1  reinoud 				stats->ndatablocks += nblk;
    938   1.1  reinoud 				fnode->flags |= FI_SIZED;
    939   1.1  reinoud 
    940   1.1  reinoud 				free(softlink_buf);
    941   1.1  reinoud 			}
    942   1.1  reinoud 			stats->nfiles++;
    943   1.1  reinoud 			break;
    944   1.1  reinoud 		}
    945   1.1  reinoud 	}
    946   1.1  reinoud }
    947   1.1  reinoud 
    948   1.1  reinoud 
    949   1.1  reinoud #define UDF_MAX_CHUNK_SIZE (4*1024*1024)
    950   1.1  reinoud static int
    951   1.1  reinoud udf_copy_file(struct stat *st, char *path, fsnode *cur, struct fileid_desc *fid,
    952   1.1  reinoud 	struct long_ad *icb)
    953   1.1  reinoud {
    954   1.1  reinoud 	union dscrptr *dscr;
    955   1.1  reinoud 	struct long_ad data_icb;
    956   1.1  reinoud 	fsinode *fnode;
    957   1.1  reinoud 	size_t sz, chunk, rd;
    958   1.1  reinoud 	uint8_t *data;
    959   1.1  reinoud 	int nblk;
    960   1.1  reinoud 	int i, f;
    961   1.5  reinoud 	int error;
    962   1.1  reinoud 
    963   1.1  reinoud 	fnode = cur->inode;
    964   1.1  reinoud 
    965   1.3    joerg 	f = open(path, O_RDONLY);
    966   1.1  reinoud 	if (f < 0) {
    967   1.1  reinoud 		warn("Can't open file %s for reading", cur->name);
    968   1.1  reinoud 		return errno;
    969   1.1  reinoud 	}
    970   1.1  reinoud 
    971   1.1  reinoud 	/* claim disc space for the (e)fe descriptor for this file */
    972   1.1  reinoud 	udf_metadata_alloc(1, icb);
    973   1.1  reinoud 	udf_create_new_file(st, &dscr, UDF_ICB_FILETYPE_RANDOMACCESS, icb);
    974   1.1  reinoud 
    975   1.1  reinoud 	sz = fnode->st.st_size;
    976   1.1  reinoud 
    977   1.1  reinoud 	chunk = MIN(sz, UDF_MAX_CHUNK_SIZE);
    978   1.1  reinoud 	data = malloc(MAX(chunk, context.sector_size));
    979   1.1  reinoud 	assert(data);
    980   1.1  reinoud 
    981   1.1  reinoud 	printf("  ");
    982   1.1  reinoud 	i = 0;
    983   1.5  reinoud 	error = 0;
    984   1.1  reinoud 	while (chunk) {
    985   1.1  reinoud 		rd = read(f, data, chunk);
    986   1.1  reinoud 		if (rd != chunk) {
    987   1.1  reinoud 			warn("Short read of file %s\n", cur->name);
    988   1.5  reinoud 			error = errno;
    989   1.5  reinoud 			break;
    990   1.1  reinoud 		}
    991   1.1  reinoud 		printf("\b%c", "\\|/-"[i++ % 4]); fflush(stdout);fflush(stderr);
    992   1.1  reinoud 
    993   1.1  reinoud 		nblk = udf_datablocks(chunk);
    994   1.1  reinoud 		if (nblk > 0)
    995   1.1  reinoud 			udf_data_alloc(nblk, &data_icb);
    996   1.1  reinoud 		udf_append_file_contents(dscr, &data_icb, data, chunk);
    997   1.1  reinoud 
    998   1.1  reinoud 		sz -= chunk;
    999   1.1  reinoud 		chunk = MIN(sz, UDF_MAX_CHUNK_SIZE);
   1000   1.1  reinoud 	}
   1001   1.1  reinoud 	printf("\b \n");
   1002   1.1  reinoud 	close(f);
   1003   1.1  reinoud 	free(data);
   1004   1.1  reinoud 
   1005   1.1  reinoud 	/* write out dscr (e)fe */
   1006   1.1  reinoud 	udf_set_link_cnt(dscr, fnode->nlink);
   1007   1.1  reinoud 	udf_write_dscr_virt(dscr, udf_rw32(icb->loc.lb_num),
   1008   1.1  reinoud 		udf_rw16(icb->loc.part_num), 1);
   1009   1.5  reinoud 	free(dscr);
   1010   1.1  reinoud 
   1011   1.1  reinoud 	/* remember our location for hardlinks */
   1012   1.1  reinoud 	cur->inode->fsuse = malloc(sizeof(struct long_ad));
   1013   1.1  reinoud 	memcpy(cur->inode->fsuse, icb, sizeof(struct long_ad));
   1014   1.1  reinoud 
   1015   1.5  reinoud 	return error;
   1016   1.1  reinoud }
   1017   1.1  reinoud 
   1018   1.1  reinoud 
   1019   1.1  reinoud static int
   1020   1.1  reinoud udf_populate_walk(fsinfo_t *fsopts, fsnode *root, char *dir,
   1021   1.1  reinoud 		struct long_ad *parent_icb, struct long_ad *dir_icb)
   1022   1.1  reinoud {
   1023   1.1  reinoud 	union dscrptr *dir_dscr, *dscr;
   1024   1.1  reinoud 	struct fileid_desc *fid;
   1025   1.1  reinoud 	struct long_ad icb, data_icb, dirdata_icb;
   1026   1.1  reinoud 	fsnode *cur;
   1027   1.1  reinoud 	fsinode *fnode;
   1028   1.1  reinoud 	size_t pathlen = strlen(dir);
   1029   1.1  reinoud 	size_t dirlen;
   1030   1.1  reinoud 	char *mydir = dir + pathlen;
   1031   1.1  reinoud 	uint32_t nblk, ddoff;
   1032   1.1  reinoud 	uint32_t softlink_len;
   1033   1.1  reinoud 	uint8_t *softlink_buf;
   1034   1.1  reinoud 	uint8_t *dirdata;
   1035   1.1  reinoud 	int error, ret, retval;
   1036   1.1  reinoud 
   1037   1.1  reinoud 	/* claim disc space for the (e)fe descriptor for this dir */
   1038   1.1  reinoud 	udf_metadata_alloc(1, dir_icb);
   1039   1.1  reinoud 
   1040   1.1  reinoud 	/* create new e(fe) */
   1041   1.1  reinoud 	udf_create_new_file(&root->inode->st, &dir_dscr,
   1042   1.1  reinoud 		UDF_ICB_FILETYPE_DIRECTORY, dir_icb);
   1043   1.1  reinoud 
   1044   1.1  reinoud 	/* claim space for the directory contents */
   1045   1.1  reinoud 	dirlen = root->inode->st.st_size;
   1046   1.1  reinoud 	nblk = udf_datablocks(dirlen);
   1047   1.1  reinoud 	if (nblk > 0) {
   1048   1.1  reinoud 		/* claim disc space for the dir contents */
   1049   1.1  reinoud 		udf_data_alloc(nblk, &dirdata_icb);
   1050   1.1  reinoud 	}
   1051   1.1  reinoud 
   1052   1.1  reinoud 	/* allocate memory for the directory contents */
   1053   1.1  reinoud 	nblk++;
   1054   1.1  reinoud 	dirdata = malloc(nblk * context.sector_size);
   1055   1.1  reinoud 	assert(dirdata);
   1056   1.1  reinoud 	memset(dirdata, 0, nblk * context.sector_size);
   1057   1.1  reinoud 
   1058   1.1  reinoud 	/* create and append '..' */
   1059   1.1  reinoud 	fid = (struct fileid_desc *) dirdata;
   1060   1.1  reinoud 	ddoff = udf_create_parentfid(fid, parent_icb);
   1061   1.1  reinoud 
   1062   1.1  reinoud 	/* for '..' */
   1063   1.1  reinoud 	udf_inc_link(dir_dscr);
   1064   1.1  reinoud 
   1065   1.1  reinoud 	/* recurse */
   1066   1.1  reinoud 	retval = 0;
   1067   1.1  reinoud 	for (cur = root; cur != NULL; cur = cur->next) {
   1068   1.1  reinoud 		mydir[0] = '/';
   1069   1.1  reinoud 		strncpy(&mydir[1], cur->name, MAXPATHLEN - pathlen);
   1070   1.1  reinoud 
   1071   1.1  reinoud 		fid = (struct fileid_desc *) (dirdata + ddoff);
   1072   1.1  reinoud 		switch (cur->type & S_IFMT) {
   1073   1.1  reinoud 		default:
   1074   1.1  reinoud 			/* what kind of nodes? */
   1075   1.1  reinoud 			retval = 2;
   1076   1.1  reinoud 			break;
   1077   1.1  reinoud 		case S_IFCHR:
   1078   1.1  reinoud 		case S_IFBLK:
   1079   1.1  reinoud 			/* not supported */
   1080   1.1  reinoud 			retval = 2;
   1081   1.1  reinoud 			warnx("device node %s not supported", dir);
   1082   1.1  reinoud 			break;
   1083   1.1  reinoud 		case S_IFDIR:
   1084   1.1  reinoud 			/* not an empty dir? */
   1085   1.1  reinoud 			if (strcmp(cur->name, ".") == 0)
   1086   1.1  reinoud 				break;
   1087   1.1  reinoud 			assert(cur->child);
   1088   1.1  reinoud 			if (cur->child) {
   1089   1.1  reinoud 				ret = udf_populate_walk(fsopts, cur->child,
   1090   1.1  reinoud 					dir, dir_icb, &icb);
   1091   1.1  reinoud 				if (ret)
   1092   1.1  reinoud 					retval = 2;
   1093   1.1  reinoud 			}
   1094   1.1  reinoud 			udf_create_fid(ddoff, fid, cur->name,
   1095   1.1  reinoud 					UDF_FILE_CHAR_DIR, &icb);
   1096   1.1  reinoud 			udf_inc_link(dir_dscr);
   1097   1.1  reinoud 			ddoff += udf_fidsize(fid);
   1098   1.1  reinoud 			break;
   1099   1.1  reinoud 		case S_IFREG:
   1100   1.1  reinoud 			fnode = cur->inode;
   1101   1.1  reinoud 			/* don't re-copy hard-links */
   1102   1.1  reinoud 			if (!(fnode->flags & FI_WRITTEN)) {
   1103   1.1  reinoud 				printf("%s", dir);
   1104   1.1  reinoud 				error = udf_copy_file(&fnode->st, dir, cur,
   1105   1.1  reinoud 					fid, &icb);
   1106   1.1  reinoud 				if (!error) {
   1107   1.1  reinoud 					fnode->flags |= FI_WRITTEN;
   1108   1.1  reinoud 					udf_create_fid(ddoff, fid, cur->name,
   1109   1.1  reinoud 						0, &icb);
   1110   1.1  reinoud 					ddoff += udf_fidsize(fid);
   1111   1.1  reinoud 				} else {
   1112   1.1  reinoud 					retval = 2;
   1113   1.1  reinoud 				}
   1114   1.1  reinoud 			} else {
   1115   1.1  reinoud 				/* hardlink! */
   1116   1.1  reinoud 				printf("%s (hardlink)\n", dir);
   1117   1.1  reinoud 				udf_create_fid(ddoff, fid, cur->name,
   1118   1.1  reinoud 					0, (struct long_ad *) (fnode->fsuse));
   1119   1.1  reinoud 				ddoff += udf_fidsize(fid);
   1120   1.1  reinoud 			}
   1121   1.1  reinoud 			fnode->nlink--;
   1122   1.1  reinoud 			if (fnode->nlink == 0)
   1123   1.1  reinoud 				free(fnode->fsuse);
   1124   1.1  reinoud 			break;
   1125   1.1  reinoud 		case S_IFLNK:
   1126   1.1  reinoud 			/* softlink */
   1127   1.1  reinoud 			fnode = cur->inode;
   1128   1.1  reinoud 			printf("%s -> %s\n", dir, cur->symlink);
   1129   1.1  reinoud 			error = udf_encode_symlink(&softlink_buf,
   1130   1.1  reinoud 					&softlink_len, cur->symlink);
   1131   1.1  reinoud 			if (error) {
   1132   1.1  reinoud 				printf("SOFTLINK error %d\n", error);
   1133   1.1  reinoud 				retval = 2;
   1134   1.1  reinoud 				break;
   1135   1.1  reinoud 			}
   1136   1.1  reinoud 
   1137   1.1  reinoud 			udf_metadata_alloc(1, &icb);
   1138   1.1  reinoud 			udf_create_new_file(&fnode->st, &dscr,
   1139   1.1  reinoud 				UDF_ICB_FILETYPE_SYMLINK, &icb);
   1140   1.1  reinoud 
   1141   1.1  reinoud 			nblk = udf_datablocks(softlink_len);
   1142   1.1  reinoud 			if (nblk > 0)
   1143   1.1  reinoud 				udf_data_alloc(nblk, &data_icb);
   1144   1.1  reinoud 			udf_append_file_contents(dscr, &data_icb,
   1145   1.1  reinoud 					softlink_buf, softlink_len);
   1146   1.1  reinoud 
   1147   1.1  reinoud 			/* write out dscr (e)fe */
   1148   1.1  reinoud 			udf_inc_link(dscr);
   1149   1.1  reinoud 			udf_write_dscr_virt(dscr, udf_rw32(icb.loc.lb_num),
   1150   1.1  reinoud 				udf_rw16(icb.loc.part_num), 1);
   1151   1.1  reinoud 
   1152   1.5  reinoud 			free(dscr);
   1153   1.1  reinoud 			free(softlink_buf);
   1154   1.1  reinoud 
   1155   1.1  reinoud 			udf_create_fid(ddoff, fid, cur->name, 0, &icb);
   1156   1.1  reinoud 			ddoff += udf_fidsize(fid);
   1157   1.1  reinoud 			break;
   1158   1.1  reinoud 		}
   1159   1.1  reinoud 		mydir[0] = '\0';
   1160   1.1  reinoud 	}
   1161   1.1  reinoud 
   1162   1.1  reinoud 	/* writeout directory contents */
   1163   1.1  reinoud 	dirlen = ddoff;	/* XXX might bite back */
   1164   1.1  reinoud 
   1165   1.1  reinoud 	udf_prepare_fids(dir_icb, &dirdata_icb, dirdata, dirlen);
   1166   1.1  reinoud 	udf_append_file_contents(dir_dscr, &dirdata_icb, dirdata, dirlen);
   1167   1.1  reinoud 
   1168   1.1  reinoud 	/* write out dir_dscr (e)fe */
   1169   1.1  reinoud 	udf_write_dscr_virt(dir_dscr, udf_rw32(dir_icb->loc.lb_num),
   1170   1.1  reinoud 			udf_rw16(dir_icb->loc.part_num), 1);
   1171   1.1  reinoud 
   1172   1.5  reinoud 	free(dirdata);
   1173   1.5  reinoud 	free(dir_dscr);
   1174   1.1  reinoud 	return retval;
   1175   1.1  reinoud }
   1176   1.1  reinoud 
   1177   1.1  reinoud 
   1178   1.1  reinoud static int
   1179   1.1  reinoud udf_populate(const char *dir, fsnode *root, fsinfo_t *fsopts,
   1180   1.1  reinoud 		struct udf_stats *stats)
   1181   1.1  reinoud {
   1182   1.1  reinoud 	struct long_ad rooticb;
   1183   1.1  reinoud 	static char path[MAXPATHLEN+1];
   1184   1.1  reinoud 	int error;
   1185   1.1  reinoud 
   1186   1.1  reinoud 	/* make sure the root gets the rootdir entry */
   1187   1.1  reinoud 	context.metadata_alloc_pos = layout.rootdir;
   1188   1.1  reinoud 	context.data_alloc_pos = layout.rootdir;
   1189   1.1  reinoud 
   1190   1.1  reinoud 	strncpy(path, dir, sizeof(path));
   1191   1.1  reinoud 	error = udf_populate_walk(fsopts, root, path, &rooticb, &rooticb);
   1192   1.1  reinoud 
   1193   1.1  reinoud 	return error;
   1194   1.1  reinoud }
   1195   1.1  reinoud 
   1196   1.1  reinoud 
   1197   1.1  reinoud static void
   1198   1.1  reinoud udf_enumerate_and_estimate(const char *dir, fsnode *root, fsinfo_t *fsopts,
   1199   1.1  reinoud 		struct udf_stats *stats)
   1200   1.1  reinoud {
   1201   1.1  reinoud 	char path[MAXPATHLEN + 1];
   1202   1.7  reinoud 	off_t proposed_size;
   1203   1.1  reinoud 	uint32_t n, nblk;
   1204   1.1  reinoud 
   1205   1.1  reinoud 	strncpy(path, dir, sizeof(path));
   1206   1.1  reinoud 
   1207   1.1  reinoud 	/* calculate strict minimal size */
   1208   1.1  reinoud 	udf_estimate_walk(fsopts, root, path, stats);
   1209   1.5  reinoud 	printf("ndirs            %d\n", stats->ndirs);
   1210   1.5  reinoud 	printf("nfiles           %d\n", stats->nfiles);
   1211   1.5  reinoud 	printf("ndata_blocks     %d\n", stats->ndatablocks);
   1212   1.5  reinoud 	printf("nmetadata_blocks %d\n", stats->nmetadatablocks);
   1213   1.5  reinoud 	printf("\n");
   1214   1.1  reinoud 
   1215   1.1  reinoud 	/* adjust for options : free file nodes */
   1216   1.1  reinoud 	if (fsopts->freefiles) {
   1217   1.1  reinoud 		/* be mercifull and reserve more for the FID */
   1218   1.1  reinoud 		stats->nmetadatablocks += fsopts->freefiles * 1.5;
   1219   1.1  reinoud 	} else if ((n = fsopts->freefilepc)) {
   1220   1.1  reinoud 		stats->nmetadatablocks += (stats->nmetadatablocks*n) / (100-n);
   1221   1.1  reinoud 	}
   1222   1.1  reinoud 
   1223   1.1  reinoud 	/* adjust for options : free data blocks */
   1224   1.1  reinoud 	if (fsopts->freeblocks) {
   1225   1.1  reinoud 		stats->ndatablocks += fsopts->freeblocks;
   1226   1.1  reinoud 	} else if ((n = fsopts->freeblockpc)) {
   1227   1.1  reinoud 		stats->ndatablocks += (stats->ndatablocks * n) / (100-n);
   1228   1.1  reinoud 	}
   1229   1.1  reinoud 
   1230   1.1  reinoud 	/* rough predictor of minimum disc size */
   1231   1.1  reinoud 	nblk  = stats->ndatablocks + stats->nmetadatablocks;
   1232   1.1  reinoud 	nblk = (double) nblk * (1.0 + 1.0/8.0);		/* free space map    */
   1233   1.1  reinoud 	nblk += 256;					/* pre-volume space  */
   1234   1.1  reinoud 	nblk += 256;					/* post-volume space */
   1235   1.1  reinoud 	nblk += 64;					/* safeguard	     */
   1236   1.1  reinoud 
   1237   1.1  reinoud 	/* try to honour minimum size */
   1238   1.1  reinoud 	n = fsopts->minsize / fsopts->sectorsize;
   1239   1.1  reinoud 	if (nblk < n) {
   1240   1.1  reinoud 		stats->ndatablocks += (n - nblk);
   1241   1.1  reinoud 		nblk += n - nblk;
   1242   1.1  reinoud 	}
   1243   1.7  reinoud 	proposed_size = (off_t) nblk * fsopts->sectorsize;
   1244   1.7  reinoud 	/* sanity size */
   1245   1.7  reinoud 	if (proposed_size < 512*1024)
   1246   1.7  reinoud 		proposed_size = 512*1024;
   1247   1.1  reinoud 
   1248   1.7  reinoud 	if (fsopts->size) {
   1249   1.7  reinoud 		if (fsopts->size < proposed_size)
   1250   1.7  reinoud 			err(EINVAL, "makefs_udf: won't fit on disc!");
   1251   1.7  reinoud 	} else {
   1252   1.7  reinoud 		fsopts->size = proposed_size;
   1253   1.7  reinoud 	}
   1254   1.1  reinoud 
   1255   1.1  reinoud 	fsopts->inodes = stats->nfiles + stats->ndirs;
   1256   1.1  reinoud }
   1257   1.1  reinoud 
   1258   1.1  reinoud 
   1259   1.1  reinoud void
   1260   1.1  reinoud udf_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
   1261   1.1  reinoud {
   1262   1.1  reinoud 	struct udf_stats stats;
   1263   1.1  reinoud 	uint64_t truncate_len;
   1264   1.9  reinoud 	char scrap[255];
   1265   1.1  reinoud 	int error;
   1266   1.1  reinoud 
   1267   1.1  reinoud 	/* determine format */
   1268   1.1  reinoud 	udf_emulate_discinfo(fsopts, &mmc_discinfo, mmc_profile);
   1269   1.1  reinoud 	printf("req_enable %d, req_disable %d\n", req_enable, req_disable);
   1270   1.1  reinoud 
   1271   1.1  reinoud 	context.sector_size = fsopts->sectorsize;
   1272   1.1  reinoud 	error = udf_derive_format(req_enable, req_disable, false);
   1273   1.1  reinoud 	if (error)
   1274   1.1  reinoud 		err(EINVAL, "makefs_udf: can't determine format");
   1275   1.1  reinoud 
   1276   1.1  reinoud 	/* names */
   1277   1.1  reinoud 	error = udf_proces_names();
   1278   1.1  reinoud 	if (error)
   1279   1.1  reinoud 		err(EINVAL, "makefs_udf: bad names given");
   1280   1.1  reinoud 
   1281   1.1  reinoud 	/* set return value to 1 indicating error */
   1282   1.1  reinoud 	error = 1;
   1283   1.1  reinoud 
   1284   1.1  reinoud 	/* estimate the amount of space needed */
   1285   1.2  reinoud 	memset(&stats, 0, sizeof(stats));
   1286   1.1  reinoud 	udf_enumerate_and_estimate(dir, root, fsopts, &stats);
   1287   1.1  reinoud 
   1288   1.1  reinoud 	printf("Calculated size of `%s': %lld bytes, %ld inodes\n",
   1289   1.1  reinoud 	    image, (long long)fsopts->size, (long)fsopts->inodes);
   1290   1.1  reinoud 
   1291   1.1  reinoud 	/* create file image */
   1292   1.1  reinoud 	if ((fd = open(image, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) {
   1293   1.1  reinoud 		err(EXIT_FAILURE, "%s", image);
   1294   1.1  reinoud 	}
   1295   1.1  reinoud 	if (lseek(fd, fsopts->size - 1, SEEK_SET) == -1) {
   1296   1.1  reinoud 		goto err_exit;
   1297   1.1  reinoud 	}
   1298   1.1  reinoud 	if (write(fd, &fd, 1) != 1) {
   1299   1.1  reinoud 		goto err_exit;
   1300   1.1  reinoud 	}
   1301   1.1  reinoud 	if (lseek(fd, 0, SEEK_SET) == -1) {
   1302   1.1  reinoud 		goto err_exit;
   1303   1.1  reinoud 	}
   1304   1.1  reinoud 	fsopts->fd = fd;
   1305   1.1  reinoud 
   1306   1.1  reinoud 	/* calculate metadata percentage */
   1307   1.1  reinoud 	meta_fract = fsopts->size / (stats.nmetadatablocks*fsopts->sectorsize);
   1308   1.1  reinoud 	meta_fract = ((int) ((meta_fract + 0.005)*100.0)) / 100;
   1309   1.1  reinoud 
   1310   1.1  reinoud 	/* update mmc info but now with correct size */
   1311   1.1  reinoud 	udf_emulate_discinfo(fsopts, &mmc_discinfo, mmc_profile);
   1312   1.1  reinoud 
   1313   1.9  reinoud 	printf("Building disc compatible with UDF version %x to %x\n\n",
   1314   1.9  reinoud 		context.min_udf, context.max_udf);
   1315   1.9  reinoud 	(void)snprintb(scrap, sizeof(scrap), FORMAT_FLAGBITS,
   1316   1.9  reinoud 	    (uint64_t) format_flags);
   1317   1.9  reinoud 	printf("UDF properties       %s\n", scrap);
   1318   1.9  reinoud 	printf("Volume set          `%s'\n", context.volset_name);
   1319   1.9  reinoud 	printf("Primary volume      `%s`\n", context.primary_name);
   1320   1.9  reinoud 	printf("Logical volume      `%s`\n", context.logvol_name);
   1321   1.9  reinoud 	if (format_flags & FORMAT_META)
   1322   1.9  reinoud 		printf("Metadata percentage  %d %%\n",
   1323   1.9  reinoud 			(int) (100.0*stats.ndatablocks/stats.nmetadatablocks));
   1324   1.9  reinoud 	printf("\n");
   1325   1.1  reinoud 	udf_do_newfs_prefix();
   1326   1.1  reinoud 
   1327   1.1  reinoud 	/* update context */
   1328   1.1  reinoud 	context.unique_id = 0;
   1329   1.1  reinoud 
   1330   1.1  reinoud 	/* XXX are the next two needed? or should be re-count them? */
   1331   1.1  reinoud 	context.num_files = stats.nfiles;
   1332   1.1  reinoud 	context.num_directories = stats.ndirs;
   1333   1.1  reinoud 
   1334   1.1  reinoud 	error = udf_populate(dir, root, fsopts, &stats);
   1335   1.1  reinoud 
   1336   1.1  reinoud 	udf_do_newfs_postfix();
   1337   1.1  reinoud 
   1338   1.1  reinoud 	if (format_flags & FORMAT_VAT) {
   1339   1.1  reinoud 		truncate_len = context.vtop_offset[context.data_part] +
   1340   1.1  reinoud 			context.data_alloc_pos;
   1341   1.1  reinoud 		truncate_len *= context.sector_size;
   1342   1.1  reinoud 
   1343   1.1  reinoud 		printf("\nTruncing the disc-image to allow for VAT\n");
   1344   1.7  reinoud 		printf("Free space left on this volume approx. "
   1345   1.7  reinoud 			"%"PRIu64" KiB, %"PRIu64" MiB\n",
   1346   1.7  reinoud 			(fsopts->size - truncate_len)/1024,
   1347   1.7  reinoud 			(fsopts->size - truncate_len)/1024/1024);
   1348   1.1  reinoud 		ftruncate(fd, truncate_len);
   1349   1.1  reinoud 	}
   1350   1.1  reinoud 
   1351   1.1  reinoud 	if (error) {
   1352   1.1  reinoud 		error = 2;	/* some files couldn't be added */
   1353   1.1  reinoud 		goto err_exit;
   1354   1.1  reinoud 	}
   1355   1.1  reinoud 
   1356   1.1  reinoud 	close(fd);
   1357   1.1  reinoud 	return;
   1358   1.1  reinoud 
   1359   1.1  reinoud err_exit:
   1360   1.1  reinoud 	close(fd);
   1361   1.1  reinoud 	if (error == 2) {
   1362   1.1  reinoud 		errx(error, "Not all files could be added");
   1363   1.1  reinoud 	} else {
   1364   1.1  reinoud 		errx(error, "creation of %s failed", image);
   1365   1.1  reinoud 	}
   1366   1.1  reinoud }
   1367   1.1  reinoud 
   1368