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