cd9660_write.c revision 230795
129088Smarkm/* $NetBSD: cd9660_write.c,v 1.14 2011/01/04 09:48:21 wiz Exp $ */ 229088Smarkm 329088Smarkm/* 429088Smarkm * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan 529088Smarkm * Perez-Rathke and Ram Vedam. All rights reserved. 629088Smarkm * 729088Smarkm * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys, 829088Smarkm * Alan Perez-Rathke and Ram Vedam. 929088Smarkm * 1029088Smarkm * Redistribution and use in source and binary forms, with or 1129088Smarkm * without modification, are permitted provided that the following 1229088Smarkm * conditions are met: 1329088Smarkm * 1. Redistributions of source code must retain the above copyright 1429088Smarkm * notice, this list of conditions and the following disclaimer. 1529088Smarkm * 2. Redistributions in binary form must reproduce the above 1629088Smarkm * copyright notice, this list of conditions and the following 1729088Smarkm * disclaimer in the documentation and/or other materials provided 1829088Smarkm * with the distribution. 1929088Smarkm * 2029088Smarkm * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN 2129088Smarkm * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR 2229088Smarkm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2329088Smarkm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 2429088Smarkm * DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN 2529088Smarkm * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT, 2629088Smarkm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2729088Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 2829088Smarkm * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 2929088Smarkm * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 3029088Smarkm * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3129088Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 3229088Smarkm * OF SUCH DAMAGE. 3329088Smarkm */ 3463248Speter 3529088Smarkm#include "cd9660.h" 3629088Smarkm#include "iso9660_rrip.h" 3729181Smarkm 3829181Smarkm#include <sys/cdefs.h> 3929088Smarkm__FBSDID("$FreeBSD: head/usr.sbin/makefs/cd9660/cd9660_write.c 230795 2012-01-31 00:32:37Z jkim $"); 4029088Smarkm 4129088Smarkmstatic int cd9660_write_volume_descriptors(FILE *); 4229088Smarkmstatic int cd9660_write_path_table(FILE *, off_t, int); 4329088Smarkmstatic int cd9660_write_path_tables(FILE *); 4429088Smarkmstatic int cd9660_write_file(FILE *, cd9660node *); 4529088Smarkmstatic int cd9660_write_filedata(FILE *, off_t, const unsigned char *, int); 4629088Smarkm#if 0 4729088Smarkmstatic int cd9660_write_buffered(FILE *, off_t, int, const unsigned char *); 4829088Smarkm#endif 4929088Smarkmstatic void cd9660_write_rr(FILE *, cd9660node *, off_t, off_t); 5029088Smarkm 5129088Smarkm/* 5229088Smarkm * Write the image 5329088Smarkm * Writes the entire image 5429088Smarkm * @param const char* The filename for the image 5529088Smarkm * @returns int 1 on success, 0 on failure 5629088Smarkm */ 5729088Smarkmint 5829088Smarkmcd9660_write_image(const char* image) 5929088Smarkm{ 6029088Smarkm FILE *fd; 6129088Smarkm int status; 6229088Smarkm char buf[CD9660_SECTOR_SIZE]; 6329088Smarkm 6429088Smarkm if ((fd = fopen(image, "w+")) == NULL) { 6529088Smarkm err(EXIT_FAILURE, "%s: Can't open `%s' for writing", __func__, 6629088Smarkm image); 6787139Smarkm } 6829088Smarkm 6929088Smarkm if (diskStructure.verbose_level > 0) 7029181Smarkm printf("Writing image\n"); 7129181Smarkm 7229088Smarkm if (diskStructure.has_generic_bootimage) { 7329088Smarkm status = cd9660_copy_file(fd, 0, 7429088Smarkm diskStructure.generic_bootimage); 7529088Smarkm if (status == 0) { 7629088Smarkm warnx("%s: Error writing generic boot image", 7729088Smarkm __func__); 7829088Smarkm goto cleanup_bad_image; 7929088Smarkm } 8029088Smarkm } 8129088Smarkm 8229088Smarkm /* Write the volume descriptors */ 8329088Smarkm status = cd9660_write_volume_descriptors(fd); 8429088Smarkm if (status == 0) { 8529088Smarkm warnx("%s: Error writing volume descriptors to image", 8629088Smarkm __func__); 8729088Smarkm goto cleanup_bad_image; 8829088Smarkm } 8929088Smarkm 9029088Smarkm if (diskStructure.verbose_level > 0) 9129088Smarkm printf("Volume descriptors written\n"); 9229088Smarkm 9329088Smarkm /* 9429088Smarkm * Write the path tables: there are actually four, but right 9529088Smarkm * now we are only concearned with two. 9629088Smarkm */ 9729088Smarkm status = cd9660_write_path_tables(fd); 9829088Smarkm if (status == 0) { 9929088Smarkm warnx("%s: Error writing path tables to image", __func__); 10029088Smarkm goto cleanup_bad_image; 10129088Smarkm } 10229088Smarkm 10329088Smarkm if (diskStructure.verbose_level > 0) 10429088Smarkm printf("Path tables written\n"); 10529088Smarkm 10629088Smarkm /* Write the directories and files */ 10729088Smarkm status = cd9660_write_file(fd, diskStructure.rootNode); 10829088Smarkm if (status == 0) { 10929088Smarkm warnx("%s: Error writing files to image", __func__); 11029088Smarkm goto cleanup_bad_image; 11129088Smarkm } 11229088Smarkm 11329088Smarkm if (diskStructure.is_bootable) { 11429088Smarkm cd9660_write_boot(fd); 11529088Smarkm } 11629088Smarkm 11729088Smarkm /* Write padding bits. This is temporary */ 11829088Smarkm memset(buf, 0, CD9660_SECTOR_SIZE); 11929088Smarkm cd9660_write_filedata(fd, diskStructure.totalSectors - 1, buf, 1); 12029088Smarkm 12129088Smarkm if (diskStructure.verbose_level > 0) 12229088Smarkm printf("Files written\n"); 12329088Smarkm fclose(fd); 124109466Sbillf 125109466Sbillf if (diskStructure.verbose_level > 0) 126109466Sbillf printf("Image closed\n"); 127109466Sbillf return 1; 12829088Smarkm 12929088Smarkmcleanup_bad_image: 13029088Smarkm fclose(fd); 131109466Sbillf if (!diskStructure.keep_bad_images) 13229088Smarkm unlink(image); 13387139Smarkm if (diskStructure.verbose_level > 0) 13429088Smarkm printf("Bad image cleaned up\n"); 13529088Smarkm return 0; 13629088Smarkm} 13729088Smarkm 13829088Smarkmstatic int 13929088Smarkmcd9660_write_volume_descriptors(FILE *fd) 14029088Smarkm{ 14129088Smarkm volume_descriptor *vd_temp = diskStructure.firstVolumeDescriptor; 14229088Smarkm int pos; 143109466Sbillf 144109466Sbillf while (vd_temp != NULL) { 14529181Smarkm pos = vd_temp->sector * diskStructure.sectorSize; 14629088Smarkm cd9660_write_filedata(fd, vd_temp->sector, 14729088Smarkm vd_temp->volumeDescriptorData, 1); 148109466Sbillf vd_temp = vd_temp->next; 14929088Smarkm } 15029088Smarkm return 1; 15129088Smarkm} 15229088Smarkm 15329088Smarkm/* 15429088Smarkm * Write out an individual path table 15529088Smarkm * Used just to keep redundant code to a minimum 15629088Smarkm * @param FILE *fd Valid file pointer 15729088Smarkm * @param int Sector to start writing path table to 15829088Smarkm * @param int Endian mode : BIG_ENDIAN or LITTLE_ENDIAN 15929088Smarkm * @returns int 1 on success, 0 on failure 16029088Smarkm */ 16129088Smarkmstatic int 16229088Smarkmcd9660_write_path_table(FILE *fd, off_t sector, int mode) 16329088Smarkm{ 16429088Smarkm int path_table_sectors = CD9660_BLOCKS(diskStructure.sectorSize, 16529088Smarkm diskStructure.pathTableLength); 16629088Smarkm unsigned char *buffer; 16729088Smarkm unsigned char *buffer_head; 16829088Smarkm int len; 16929088Smarkm path_table_entry temp_entry; 17029088Smarkm cd9660node *ptcur; 17129088Smarkm 17229088Smarkm buffer = malloc(diskStructure.sectorSize * path_table_sectors); 17329088Smarkm if (buffer == NULL) { 17429088Smarkm warnx("%s: Memory allocation error allocating buffer", 17529088Smarkm __func__); 17629088Smarkm return 0; 17729088Smarkm } 17829088Smarkm buffer_head = buffer; 17929088Smarkm memset(buffer, 0, diskStructure.sectorSize * path_table_sectors); 18029088Smarkm 18129088Smarkm ptcur = diskStructure.rootNode; 18229088Smarkm 18329088Smarkm while (ptcur != NULL) { 18429088Smarkm memset(&temp_entry, 0, sizeof(path_table_entry)); 18529088Smarkm temp_entry.length[0] = ptcur->isoDirRecord->name_len[0]; 18629088Smarkm temp_entry.extended_attribute_length[0] = 18729088Smarkm ptcur->isoDirRecord->ext_attr_length[0]; 18829088Smarkm memcpy(temp_entry.name, ptcur->isoDirRecord->name, 18929088Smarkm temp_entry.length[0] + 1); 19029088Smarkm 19129088Smarkm /* round up */ 19229088Smarkm len = temp_entry.length[0] + 8 + (temp_entry.length[0] & 0x01); 19329088Smarkm 19429088Smarkm /* todo: function pointers instead */ 19529088Smarkm if (mode == LITTLE_ENDIAN) { 19629088Smarkm cd9660_731(ptcur->fileDataSector, 19729088Smarkm temp_entry.first_sector); 19829088Smarkm cd9660_721((ptcur->parent == NULL ? 19929088Smarkm 1 : ptcur->parent->ptnumber), 20029088Smarkm temp_entry.parent_number); 20129088Smarkm } else { 20229088Smarkm cd9660_732(ptcur->fileDataSector, 20329088Smarkm temp_entry.first_sector); 20429088Smarkm cd9660_722((ptcur->parent == NULL ? 20529088Smarkm 1 : ptcur->parent->ptnumber), 20629181Smarkm temp_entry.parent_number); 20729181Smarkm } 20829181Smarkm 20929181Smarkm 21029181Smarkm memcpy(buffer, &temp_entry, len); 21129181Smarkm buffer += len; 21229181Smarkm 21329181Smarkm ptcur = ptcur->ptnext; 21429181Smarkm } 21529181Smarkm 21629181Smarkm return cd9660_write_filedata(fd, sector, buffer_head, 21729181Smarkm path_table_sectors); 21829088Smarkm} 21929181Smarkm 22029088Smarkm 22129088Smarkm/* 22272093Sasmodai * Write out the path tables to disk 22329088Smarkm * Each file descriptor should be pointed to by the PVD, so we know which 22429088Smarkm * sector to copy them to. One thing to watch out for: the only path tables 22585600Smarkm * stored are in the endian mode that the application is compiled for. So, 22685600Smarkm * the first thing to do is write out that path table, then to write the one 22729181Smarkm * in the other endian mode requires to convert the endianness of each entry 22829181Smarkm * in the table. The best way to do this would be to create a temporary 22929181Smarkm * path_table_entry structure, then for each path table entry, copy it to 23029181Smarkm * the temporary entry, translate, then copy that to disk. 23129181Smarkm * 23229181Smarkm * @param FILE* Valid file descriptor 23329088Smarkm * @returns int 0 on failure, 1 on success 23487139Smarkm */ 23529088Smarkmstatic int 23629088Smarkmcd9660_write_path_tables(FILE *fd) 23729088Smarkm{ 23829088Smarkm if (cd9660_write_path_table(fd, 23929088Smarkm diskStructure.primaryLittleEndianTableSector, LITTLE_ENDIAN) == 0) 24029088Smarkm return 0; 24129088Smarkm 24229088Smarkm if (cd9660_write_path_table(fd, 24329088Smarkm diskStructure.primaryBigEndianTableSector, BIG_ENDIAN) == 0) 24429088Smarkm return 0; 24529088Smarkm 24629088Smarkm /* @TODO: handle remaining two path tables */ 24729088Smarkm return 1; 24829088Smarkm} 24929088Smarkm 25029088Smarkm/* 25129088Smarkm * Write a file to disk 25229088Smarkm * Writes a file, its directory record, and its data to disk 25329088Smarkm * This file is designed to be called RECURSIVELY, so initially call it 25429088Smarkm * with the root node. All of the records should store what sector the 25529088Smarkm * file goes in, so no computation should be necessary. 25629088Smarkm * 25729088Smarkm * @param int fd Valid file descriptor 25829088Smarkm * @param struct cd9660node* writenode Pointer to the file to be written 25929088Smarkm * @returns int 0 on failure, 1 on success 26029088Smarkm */ 26129088Smarkmstatic int 26229088Smarkmcd9660_write_file(FILE *fd, cd9660node *writenode) 26329088Smarkm{ 26429088Smarkm char *buf; 26529088Smarkm char *temp_file_name; 26629088Smarkm int ret; 26729088Smarkm off_t working_sector; 26829088Smarkm int cur_sector_offset; 26929088Smarkm int written; 27029088Smarkm iso_directory_record_cd9660 temp_record; 27129088Smarkm cd9660node *temp; 27229088Smarkm int rv = 0; 27329088Smarkm 27429088Smarkm /* Todo : clean up variables */ 27529088Smarkm 27629088Smarkm temp_file_name = malloc(CD9660MAXPATH + 1); 27729088Smarkm if (temp_file_name == NULL) 27829088Smarkm err(EXIT_FAILURE, "%s: malloc", __func__); 27929088Smarkm 28029088Smarkm memset(temp_file_name, 0, CD9660MAXPATH + 1); 28129088Smarkm 28229088Smarkm buf = malloc(diskStructure.sectorSize); 28329088Smarkm if (buf == NULL) 28429088Smarkm err(EXIT_FAILURE, "%s: malloc", __func__); 28529088Smarkm 28629088Smarkm if ((writenode->level != 0) && 28729088Smarkm !(writenode->node->type & S_IFDIR)) { 28829088Smarkm fsinode *inode = writenode->node->inode; 28985600Smarkm /* Only attempt to write unwritten files that have length. */ 29049887Snsayer if ((inode->flags & FI_WRITTEN) != 0) { 29129088Smarkm INODE_WARNX(("%s: skipping written inode %d", __func__, 29229088Smarkm (int)inode->st.st_ino)); 29329088Smarkm } else if (writenode->fileDataLength > 0) { 29429088Smarkm INODE_WARNX(("%s: writing inode %d blocks at %" PRIu32, 29587139Smarkm __func__, (int)inode->st.st_ino, inode->ino)); 29649887Snsayer inode->flags |= FI_WRITTEN; 29729181Smarkm if (writenode->node->contents == NULL) 29829088Smarkm cd9660_compute_full_filename(writenode, 29929088Smarkm temp_file_name); 30029088Smarkm ret = cd9660_copy_file(fd, writenode->fileDataSector, 30129088Smarkm (writenode->node->contents != NULL) ? 30229088Smarkm writenode->node->contents : temp_file_name); 30329088Smarkm if (ret == 0) 30429088Smarkm goto out; 30529088Smarkm } 30629088Smarkm } else { 30729088Smarkm /* 30829088Smarkm * Here is a new revelation that ECMA didn't explain 30929088Smarkm * (at least not well). 31029088Smarkm * ALL . and .. records store the name "\0" and "\1" 31129088Smarkm * respectively. So, for each directory, we have to 31229088Smarkm * make a new node. 31329088Smarkm * 31429088Smarkm * This is where it gets kinda messy, since we have to 31529181Smarkm * be careful of sector boundaries 31629088Smarkm */ 31729088Smarkm cur_sector_offset = 0; 31829088Smarkm working_sector = writenode->fileDataSector; 31929088Smarkm if (fseeko(fd, working_sector * diskStructure.sectorSize, 32029088Smarkm SEEK_SET) == -1) 32129088Smarkm err(1, "fseeko"); 32229088Smarkm 32329088Smarkm /* 32429088Smarkm * Now loop over children, writing out their directory 32529088Smarkm * records - beware of sector boundaries 32687139Smarkm */ 32729088Smarkm TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) { 32829088Smarkm /* 32929181Smarkm * Copy the temporary record and adjust its size 33029088Smarkm * if necessary 33187139Smarkm */ 33229181Smarkm memcpy(&temp_record, temp->isoDirRecord, 33329181Smarkm sizeof(iso_directory_record_cd9660)); 33429088Smarkm 33529088Smarkm temp_record.length[0] = 33629088Smarkm cd9660_compute_record_size(temp); 33729088Smarkm 33829088Smarkm if (temp_record.length[0] + cur_sector_offset >= 33929088Smarkm diskStructure.sectorSize) { 34029088Smarkm cur_sector_offset = 0; 34129088Smarkm working_sector++; 34229088Smarkm 34329088Smarkm /* Seek to the next sector. */ 34429088Smarkm if (fseeko(fd, working_sector * 34529088Smarkm diskStructure.sectorSize, SEEK_SET) == -1) 34629088Smarkm err(1, "fseeko"); 34729088Smarkm } 348 /* Write out the basic ISO directory record */ 349 written = fwrite(&temp_record, 1, 350 temp->isoDirRecord->length[0], fd); 351 if (diskStructure.rock_ridge_enabled) { 352 cd9660_write_rr(fd, temp, 353 cur_sector_offset, working_sector); 354 } 355 if (fseeko(fd, working_sector * 356 diskStructure.sectorSize + cur_sector_offset + 357 temp_record.length[0] - temp->su_tail_size, 358 SEEK_SET) == -1) 359 err(1, "fseeko"); 360 if (temp->su_tail_size > 0) 361 fwrite(temp->su_tail_data, 1, 362 temp->su_tail_size, fd); 363 if (ferror(fd)) { 364 warnx("%s: write error", __func__); 365 goto out; 366 } 367 cur_sector_offset += temp_record.length[0]; 368 369 } 370 371 /* 372 * Recurse on children. 373 */ 374 TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) { 375 if ((ret = cd9660_write_file(fd, temp)) == 0) 376 goto out; 377 } 378 } 379 rv = 1; 380out: 381 free(temp_file_name); 382 free(buf); 383 return rv; 384} 385 386/* 387 * Wrapper function to write a buffer (one sector) to disk. 388 * Seeks and writes the buffer. 389 * NOTE: You dont NEED to use this function, but it might make your 390 * life easier if you have to write things that align to a sector 391 * (such as volume descriptors). 392 * 393 * @param int fd Valid file descriptor 394 * @param int sector Sector number to write to 395 * @param const unsigned char* Buffer to write. This should be the 396 * size of a sector, and if only a portion 397 * is written, the rest should be set to 0. 398 */ 399static int 400cd9660_write_filedata(FILE *fd, off_t sector, const unsigned char *buf, 401 int numsecs) 402{ 403 off_t curpos; 404 size_t success; 405 406 curpos = ftello(fd); 407 408 if (fseeko(fd, sector * diskStructure.sectorSize, SEEK_SET) == -1) 409 err(1, "fseeko"); 410 411 success = fwrite(buf, diskStructure.sectorSize * numsecs, 1, fd); 412 413 if (fseeko(fd, curpos, SEEK_SET) == -1) 414 err(1, "fseeko"); 415 416 if (success == 1) 417 success = diskStructure.sectorSize * numsecs; 418 return success; 419} 420 421#if 0 422static int 423cd9660_write_buffered(FILE *fd, off_t offset, int buff_len, 424 const unsigned char* buffer) 425{ 426 static int working_sector = -1; 427 static char buf[CD9660_SECTOR_SIZE]; 428 429 return 0; 430} 431#endif 432 433int 434cd9660_copy_file(FILE *fd, off_t start_sector, const char *filename) 435{ 436 FILE *rf; 437 int bytes_read; 438 off_t sector = start_sector; 439 int buf_size = diskStructure.sectorSize; 440 char *buf; 441 442 buf = malloc(buf_size); 443 if (buf == NULL) 444 err(EXIT_FAILURE, "%s: malloc", __func__); 445 446 if ((rf = fopen(filename, "rb")) == NULL) { 447 warn("%s: cannot open %s", __func__, filename); 448 free(buf); 449 return 0; 450 } 451 452 if (diskStructure.verbose_level > 1) 453 printf("Writing file: %s\n",filename); 454 455 if (fseeko(fd, start_sector * diskStructure.sectorSize, SEEK_SET) == -1) 456 err(1, "fseeko"); 457 458 while (!feof(rf)) { 459 bytes_read = fread(buf,1,buf_size,rf); 460 if (ferror(rf)) { 461 warn("%s: fread", __func__); 462 free(buf); 463 (void)fclose(rf); 464 return 0; 465 } 466 467 fwrite(buf,1,bytes_read,fd); 468 if (ferror(fd)) { 469 warn("%s: fwrite", __func__); 470 free(buf); 471 (void)fclose(rf); 472 return 0; 473 } 474 sector++; 475 } 476 477 fclose(rf); 478 free(buf); 479 return 1; 480} 481 482static void 483cd9660_write_rr(FILE *fd, cd9660node *writenode, off_t offset, off_t sector) 484{ 485 int in_ca = 0; 486 struct ISO_SUSP_ATTRIBUTES *myattr; 487 488 offset += writenode->isoDirRecord->length[0]; 489 if (fseeko(fd, sector * diskStructure.sectorSize + offset, SEEK_SET) == 490 -1) 491 err(1, "fseeko"); 492 /* Offset now points at the end of the record */ 493 TAILQ_FOREACH(myattr, &writenode->head, rr_ll) { 494 fwrite(&(myattr->attr), CD9660_SUSP_ENTRY_SIZE(myattr), 1, fd); 495 496 if (!in_ca) { 497 offset += CD9660_SUSP_ENTRY_SIZE(myattr); 498 if (myattr->last_in_suf) { 499 /* 500 * Point the offset to the start of this 501 * record's CE area 502 */ 503 if (fseeko(fd, ((off_t)diskStructure. 504 susp_continuation_area_start_sector * 505 diskStructure.sectorSize) 506 + writenode->susp_entry_ce_start, 507 SEEK_SET) == -1) 508 err(1, "fseeko"); 509 in_ca = 1; 510 } 511 } 512 } 513 514 /* 515 * If we had to go to the continuation area, head back to 516 * where we should be. 517 */ 518 if (in_ca) 519 if (fseeko(fd, sector * diskStructure.sectorSize + offset, 520 SEEK_SET) == -1) 521 err(1, "fseeko"); 522} 523