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