1 /* $NetBSD: cd9660_write.c,v 1.14 2011/01/04 09:48:21 wiz Exp $ */ 2 3 /* 4 * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan 5 * Perez-Rathke and Ram Vedam. All rights reserved. 6 * 7 * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys, 8 * Alan Perez-Rathke and Ram Vedam. 9 * 10 * Redistribution and use in source and binary forms, with or 11 * without modification, are permitted provided that the following 12 * conditions are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer in the documentation and/or other materials provided 18 * with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN 21 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 * DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN 25 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 28 * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 32 * OF SUCH DAMAGE. 33 */ 34 35 #include "cd9660.h" 36 #include "iso9660_rrip.h" 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 static int cd9660_write_volume_descriptors(iso9660_disk *, FILE *); 42 static int cd9660_write_path_table(iso9660_disk *, FILE *, off_t, int); 43 static int cd9660_write_path_tables(iso9660_disk *, FILE *); 44 static int cd9660_write_file(iso9660_disk *, FILE *, cd9660node *); 45 static int cd9660_write_filedata(iso9660_disk *, FILE *, off_t, 46 const unsigned char *, int); 47 #if 0 48 static int cd9660_write_buffered(FILE *, off_t, int, const unsigned char *); 49 #endif 50 static void cd9660_write_rr(iso9660_disk *, FILE *, cd9660node *, off_t, off_t); 51 52 /* 53 * Write the image 54 * Writes the entire image 55 * @param const char* The filename for the image 56 * @returns int 1 on success, 0 on failure 57 */ 58 int 59 cd9660_write_image(iso9660_disk *diskStructure, const char* image) 60 { 61 FILE *fd; 62 int status; 63 char buf[CD9660_SECTOR_SIZE]; 64 65 if ((fd = fopen(image, "w+")) == NULL) { 66 err(EXIT_FAILURE, "%s: Can't open `%s' for writing", __func__, 67 image); 68 } 69 70 if (diskStructure->verbose_level > 0) 71 printf("Writing image\n"); 72 73 if (diskStructure->has_generic_bootimage) { 74 status = cd9660_copy_file(diskStructure, fd, 0, 75 diskStructure->generic_bootimage); 76 if (status == 0) { 77 warnx("%s: Error writing generic boot image", 78 __func__); 79 goto cleanup_bad_image; 80 } 81 } 82 83 /* Write the volume descriptors */ 84 status = cd9660_write_volume_descriptors(diskStructure, fd); 85 if (status == 0) { 86 warnx("%s: Error writing volume descriptors to image", 87 __func__); 88 goto cleanup_bad_image; 89 } 90 91 if (diskStructure->verbose_level > 0) 92 printf("Volume descriptors written\n"); 93 94 /* 95 * Write the path tables: there are actually four, but right 96 * now we are only concearned with two. 97 */ 98 status = cd9660_write_path_tables(diskStructure, fd); 99 if (status == 0) { 100 warnx("%s: Error writing path tables to image", __func__); 101 goto cleanup_bad_image; 102 } 103 104 if (diskStructure->verbose_level > 0) 105 printf("Path tables written\n"); 106 107 /* Write the directories and files */ 108 status = cd9660_write_file(diskStructure, fd, diskStructure->rootNode); 109 if (status == 0) { 110 warnx("%s: Error writing files to image", __func__); 111 goto cleanup_bad_image; 112 } 113 114 if (diskStructure->is_bootable) { 115 cd9660_write_boot(diskStructure, fd); 116 } 117 118 /* Write padding bits. This is temporary */ 119 memset(buf, 0, CD9660_SECTOR_SIZE); 120 cd9660_write_filedata(diskStructure, fd, 121 diskStructure->totalSectors - 1, buf, 1); 122 123 if (diskStructure->verbose_level > 0) 124 printf("Files written\n"); 125 fclose(fd); 126 127 if (diskStructure->verbose_level > 0) 128 printf("Image closed\n"); 129 return 1; 130 131 cleanup_bad_image: 132 fclose(fd); 133 if (!diskStructure->keep_bad_images) 134 unlink(image); 135 if (diskStructure->verbose_level > 0) 136 printf("Bad image cleaned up\n"); 137 return 0; 138 } 139 140 static int 141 cd9660_write_volume_descriptors(iso9660_disk *diskStructure, FILE *fd) 142 { 143 volume_descriptor *vd_temp = diskStructure->firstVolumeDescriptor; 144 int pos; 145 146 while (vd_temp != NULL) { 147 pos = vd_temp->sector * diskStructure->sectorSize; 148 cd9660_write_filedata(diskStructure, fd, vd_temp->sector, 149 vd_temp->volumeDescriptorData, 1); 150 vd_temp = vd_temp->next; 151 } 152 return 1; 153 } 154 155 /* 156 * Write out an individual path table 157 * Used just to keep redundant code to a minimum 158 * @param FILE *fd Valid file pointer 159 * @param int Sector to start writing path table to 160 * @param int Endian mode : BIG_ENDIAN or LITTLE_ENDIAN 161 * @returns int 1 on success, 0 on failure 162 */ 163 static int 164 cd9660_write_path_table(iso9660_disk *diskStructure, FILE *fd, off_t sector, 165 int mode) 166 { 167 int path_table_sectors = CD9660_BLOCKS(diskStructure->sectorSize, 168 diskStructure->pathTableLength); 169 unsigned char *buffer; 170 unsigned char *buffer_head; 171 int len, ret; 172 path_table_entry temp_entry; 173 cd9660node *ptcur; 174 175 buffer = malloc(diskStructure->sectorSize * path_table_sectors); 176 if (buffer == NULL) { 177 warnx("%s: Memory allocation error allocating buffer", 178 __func__); 179 return 0; 180 } 181 buffer_head = buffer; 182 memset(buffer, 0, diskStructure->sectorSize * path_table_sectors); 183 184 ptcur = diskStructure->rootNode; 185 186 while (ptcur != NULL) { 187 memset(&temp_entry, 0, sizeof(path_table_entry)); 188 temp_entry.length[0] = ptcur->isoDirRecord->name_len[0]; 189 temp_entry.extended_attribute_length[0] = 190 ptcur->isoDirRecord->ext_attr_length[0]; 191 memcpy(temp_entry.name, ptcur->isoDirRecord->name, 192 temp_entry.length[0] + 1); 193 194 /* round up */ 195 len = temp_entry.length[0] + 8 + (temp_entry.length[0] & 0x01); 196 197 /* todo: function pointers instead */ 198 if (mode == LITTLE_ENDIAN) { 199 cd9660_731(ptcur->fileDataSector, 200 temp_entry.first_sector); 201 cd9660_721((ptcur->parent == NULL ? 202 1 : ptcur->parent->ptnumber), 203 temp_entry.parent_number); 204 } else { 205 cd9660_732(ptcur->fileDataSector, 206 temp_entry.first_sector); 207 cd9660_722((ptcur->parent == NULL ? 208 1 : ptcur->parent->ptnumber), 209 temp_entry.parent_number); 210 } 211 212 213 memcpy(buffer, &temp_entry, len); 214 buffer += len; 215 216 ptcur = ptcur->ptnext; 217 } 218 219 ret = cd9660_write_filedata(diskStructure, fd, sector, buffer_head, 220 path_table_sectors); 221 free(buffer_head); 222 return ret; 223 } 224 225 226 /* 227 * Write out the path tables to disk 228 * Each file descriptor should be pointed to by the PVD, so we know which 229 * sector to copy them to. One thing to watch out for: the only path tables 230 * stored are in the endian mode that the application is compiled for. So, 231 * the first thing to do is write out that path table, then to write the one 232 * in the other endian mode requires to convert the endianness of each entry 233 * in the table. The best way to do this would be to create a temporary 234 * path_table_entry structure, then for each path table entry, copy it to 235 * the temporary entry, translate, then copy that to disk. 236 * 237 * @param FILE* Valid file descriptor 238 * @returns int 0 on failure, 1 on success 239 */ 240 static int 241 cd9660_write_path_tables(iso9660_disk *diskStructure, FILE *fd) 242 { 243 if (cd9660_write_path_table(diskStructure, fd, 244 diskStructure->primaryLittleEndianTableSector, LITTLE_ENDIAN) == 0) 245 return 0; 246 247 if (cd9660_write_path_table(diskStructure, fd, 248 diskStructure->primaryBigEndianTableSector, BIG_ENDIAN) == 0) 249 return 0; 250 251 /* @TODO: handle remaining two path tables */ 252 return 1; 253 } 254 255 /* 256 * Write a file to disk 257 * Writes a file, its directory record, and its data to disk 258 * This file is designed to be called RECURSIVELY, so initially call it 259 * with the root node. All of the records should store what sector the 260 * file goes in, so no computation should be necessary. 261 * 262 * @param int fd Valid file descriptor 263 * @param struct cd9660node* writenode Pointer to the file to be written 264 * @returns int 0 on failure, 1 on success 265 */ 266 static int 267 cd9660_write_file(iso9660_disk *diskStructure, FILE *fd, cd9660node *writenode) 268 { 269 char *buf; 270 char *temp_file_name; 271 int ret; 272 off_t working_sector; 273 int cur_sector_offset; 274 int written; 275 iso_directory_record_cd9660 temp_record; 276 cd9660node *temp; 277 int rv = 0; 278 279 /* Todo : clean up variables */ 280 281 temp_file_name = malloc(CD9660MAXPATH + 1); 282 if (temp_file_name == NULL) 283 err(EXIT_FAILURE, "%s: malloc", __func__); 284 285 memset(temp_file_name, 0, CD9660MAXPATH + 1); 286 287 buf = malloc(diskStructure->sectorSize); 288 if (buf == NULL) 289 err(EXIT_FAILURE, "%s: malloc", __func__); 290 291 if ((writenode->level != 0) && 292 !(writenode->node->type & S_IFDIR)) { 293 fsinode *inode = writenode->node->inode; 294 /* Only attempt to write unwritten files that have length. */ 295 if ((inode->flags & FI_WRITTEN) != 0) { 296 INODE_WARNX(("%s: skipping written inode %d", __func__, 297 (int)inode->st.st_ino)); 298 } else if (writenode->fileDataLength > 0) { 299 INODE_WARNX(("%s: writing inode %d blocks at %" PRIu32, 300 __func__, (int)inode->st.st_ino, inode->ino)); 301 inode->flags |= FI_WRITTEN; 302 if (writenode->node->contents == NULL) 303 cd9660_compute_full_filename(writenode, 304 temp_file_name); 305 ret = cd9660_copy_file(diskStructure, fd, 306 writenode->fileDataSector, 307 (writenode->node->contents != NULL) ? 308 writenode->node->contents : temp_file_name); 309 if (ret == 0) 310 goto out; 311 } 312 } else { 313 /* 314 * Here is a new revelation that ECMA didn't explain 315 * (at least not well). 316 * ALL . and .. records store the name "\0" and "\1" 317 * respectively. So, for each directory, we have to 318 * make a new node. 319 * 320 * This is where it gets kinda messy, since we have to 321 * be careful of sector boundaries 322 */ 323 cur_sector_offset = 0; 324 working_sector = writenode->fileDataSector; 325 if (fseeko(fd, working_sector * diskStructure->sectorSize, 326 SEEK_SET) == -1) 327 err(1, "fseeko"); 328 329 /* 330 * Now loop over children, writing out their directory 331 * records - beware of sector boundaries 332 */ 333 TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) { 334 /* 335 * Copy the temporary record and adjust its size 336 * if necessary 337 */ 338 memcpy(&temp_record, temp->isoDirRecord, 339 sizeof(iso_directory_record_cd9660)); 340 341 temp_record.length[0] = 342 cd9660_compute_record_size(diskStructure, temp); 343 344 if (temp_record.length[0] + cur_sector_offset >= 345 diskStructure->sectorSize) { 346 cur_sector_offset = 0; 347 working_sector++; 348 349 /* Seek to the next sector. */ 350 if (fseeko(fd, working_sector * 351 diskStructure->sectorSize, SEEK_SET) == -1) 352 err(1, "fseeko"); 353 } 354 /* Write out the basic ISO directory record */ 355 written = fwrite(&temp_record, 1, 356 temp->isoDirRecord->length[0], fd); 357 if (diskStructure->rock_ridge_enabled) { 358 cd9660_write_rr(diskStructure, fd, temp, 359 cur_sector_offset, working_sector); 360 } 361 if (fseeko(fd, working_sector * 362 diskStructure->sectorSize + cur_sector_offset + 363 temp_record.length[0] - temp->su_tail_size, 364 SEEK_SET) == -1) 365 err(1, "fseeko"); 366 if (temp->su_tail_size > 0) 367 fwrite(temp->su_tail_data, 1, 368 temp->su_tail_size, fd); 369 if (ferror(fd)) { 370 warnx("%s: write error", __func__); 371 goto out; 372 } 373 cur_sector_offset += temp_record.length[0]; 374 375 } 376 377 /* 378 * Recurse on children. 379 */ 380 TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) { 381 if ((ret = cd9660_write_file(diskStructure, fd, temp)) == 0) 382 goto out; 383 } 384 } 385 rv = 1; 386 out: 387 free(temp_file_name); 388 free(buf); 389 return rv; 390 } 391 392 /* 393 * Wrapper function to write a buffer (one sector) to disk. 394 * Seeks and writes the buffer. 395 * NOTE: You dont NEED to use this function, but it might make your 396 * life easier if you have to write things that align to a sector 397 * (such as volume descriptors). 398 * 399 * @param int fd Valid file descriptor 400 * @param int sector Sector number to write to 401 * @param const unsigned char* Buffer to write. This should be the 402 * size of a sector, and if only a portion 403 * is written, the rest should be set to 0. 404 */ 405 static int 406 cd9660_write_filedata(iso9660_disk *diskStructure, FILE *fd, off_t sector, 407 const unsigned char *buf, int numsecs) 408 { 409 off_t curpos; 410 size_t success; 411 412 curpos = ftello(fd); 413 414 if (fseeko(fd, sector * diskStructure->sectorSize, SEEK_SET) == -1) 415 err(1, "fseeko"); 416 417 success = fwrite(buf, diskStructure->sectorSize * numsecs, 1, fd); 418 419 if (fseeko(fd, curpos, SEEK_SET) == -1) 420 err(1, "fseeko"); 421 422 if (success == 1) 423 success = diskStructure->sectorSize * numsecs; 424 return success; 425 } 426 427 #if 0 428 static int 429 cd9660_write_buffered(FILE *fd, off_t offset, int buff_len, 430 const unsigned char* buffer) 431 { 432 static int working_sector = -1; 433 static char buf[CD9660_SECTOR_SIZE]; 434 435 return 0; 436 } 437 #endif 438 439 int 440 cd9660_copy_file(iso9660_disk *diskStructure, FILE *fd, off_t start_sector, 441 const char *filename) 442 { 443 FILE *rf; 444 int bytes_read; 445 off_t sector = start_sector; 446 int buf_size = diskStructure->sectorSize; 447 char *buf; 448 449 buf = malloc(buf_size); 450 if (buf == NULL) 451 err(EXIT_FAILURE, "%s: malloc", __func__); 452 453 if ((rf = fopen(filename, "rb")) == NULL) { 454 warn("%s: cannot open %s", __func__, filename); 455 free(buf); 456 return 0; 457 } 458 459 if (diskStructure->verbose_level > 1) 460 printf("Writing file: %s\n",filename); 461 462 if (fseeko(fd, start_sector * diskStructure->sectorSize, SEEK_SET) == -1) 463 err(1, "fseeko"); 464 465 while (!feof(rf)) { 466 bytes_read = fread(buf,1,buf_size,rf); 467 if (ferror(rf)) { 468 warn("%s: fread", __func__); 469 free(buf); 470 (void)fclose(rf); 471 return 0; 472 } 473 474 fwrite(buf,1,bytes_read,fd); 475 if (ferror(fd)) { 476 warn("%s: fwrite", __func__); 477 free(buf); 478 (void)fclose(rf); 479 return 0; 480 } 481 sector++; 482 } 483 484 fclose(rf); 485 free(buf); 486 return 1; 487 } 488 489 static void 490 cd9660_write_rr(iso9660_disk *diskStructure, FILE *fd, cd9660node *writenode, 491 off_t offset, off_t sector) 492 { 493 int in_ca = 0; 494 struct ISO_SUSP_ATTRIBUTES *myattr; 495 496 offset += writenode->isoDirRecord->length[0]; 497 if (fseeko(fd, sector * diskStructure->sectorSize + offset, SEEK_SET) == 498 -1) 499 err(1, "fseeko"); 500 /* Offset now points at the end of the record */ 501 TAILQ_FOREACH(myattr, &writenode->head, rr_ll) { 502 fwrite(&(myattr->attr), CD9660_SUSP_ENTRY_SIZE(myattr), 1, fd); 503 504 if (!in_ca) { 505 offset += CD9660_SUSP_ENTRY_SIZE(myattr); 506 if (myattr->last_in_suf) { 507 /* 508 * Point the offset to the start of this 509 * record's CE area 510 */ 511 if (fseeko(fd, ((off_t)diskStructure-> 512 susp_continuation_area_start_sector * 513 diskStructure->sectorSize) 514 + writenode->susp_entry_ce_start, 515 SEEK_SET) == -1) 516 err(1, "fseeko"); 517 in_ca = 1; 518 } 519 } 520 } 521 522 /* 523 * If we had to go to the continuation area, head back to 524 * where we should be. 525 */ 526 if (in_ca) 527 if (fseeko(fd, sector * diskStructure->sectorSize + offset, 528 SEEK_SET) == -1) 529 err(1, "fseeko"); 530 } 531