Home | History | Annotate | Line # | Download | only in cd9660
cd9660_write.c revision 1.4
      1 /*	$NetBSD: cd9660_write.c,v 1.4 2005/10/30 07:40:45 dyoung 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 #if defined(__RCSID) && !defined(__lint)
     40 __RCSID("$NetBSD: cd9660_write.c,v 1.4 2005/10/30 07:40:45 dyoung Exp $");
     41 #endif  /* !__lint */
     42 
     43 static int cd9660_write_volume_descriptors(FILE *);
     44 static int cd9660_write_path_table(FILE *, int, int);
     45 static int cd9660_write_path_tables(FILE *);
     46 static int cd9660_write_file(FILE *, cd9660node *);
     47 static int cd9660_write_filedata(FILE *, int, const unsigned char *, int);
     48 #if 0
     49 static int cd9660_write_buffered(FILE *, int, int, const unsigned char*);
     50 #endif
     51 static int cd9660_write_rr(FILE *, cd9660node *, int, int);
     52 
     53 /*
     54  * Write the image
     55  * Writes the entire image
     56  * @param const char* The filename for the image
     57  * @returns int 1 on success, 0 on failure
     58  */
     59 int
     60 cd9660_write_image(const char* image)
     61 {
     62 	FILE *fd;
     63 	int status;
     64 	char buf[2048];
     65 
     66 	if ((fd = fopen(image, "w+")) == NULL) {
     67 		err(EXIT_FAILURE, "%s: Can't open `%s' for writing", __func__,
     68 		    image);
     69 	}
     70 
     71 	if (diskStructure.verbose_level > 0)
     72 		printf("Writing image\n");
     73 
     74 	/* Write the volume descriptors */
     75 	status = cd9660_write_volume_descriptors(fd);
     76 	if (status == 0) {
     77 		warnx("%s: Error writing volume descriptors to image",
     78 		    __func__);
     79 		goto cleanup_bad_image;
     80 	}
     81 
     82 	if (diskStructure.verbose_level > 0)
     83 		printf("Volume descriptors written\n");
     84 
     85 	/*
     86 	 * Write the path tables: there are actually four, but right
     87 	 * now we are only concearned with two.
     88 	 */
     89 	status = cd9660_write_path_tables(fd);
     90 	if (status == 0) {
     91 		warnx("%s: Error writing path tables to image", __func__);
     92 		goto cleanup_bad_image;
     93 	}
     94 
     95 	if (diskStructure.verbose_level > 0)
     96 		printf("Path tables written\n");
     97 
     98 	/* Write the directories and files */
     99 	status = cd9660_write_file(fd, diskStructure.rootNode);
    100 	if (status == 0) {
    101 		warnx("%s: Error writing files to image", __func__);
    102 		goto cleanup_bad_image;
    103 	}
    104 
    105 	if (diskStructure.is_bootable) {
    106 		cd9660_write_boot(fd);
    107 	}
    108 
    109 	/* Write padding bits. This is temporary */
    110 	memset(buf, 0, 2048);
    111 	cd9660_write_filedata(fd, diskStructure.totalSectors - 1, buf, 1);
    112 
    113 	if (diskStructure.verbose_level > 0)
    114 		printf("Files written\n");
    115 	fclose(fd);
    116 
    117 	if (diskStructure.verbose_level > 0)
    118 		printf("Image closed\n");
    119 	return 1;
    120 
    121 cleanup_bad_image:
    122 	fclose(fd);
    123 	if (!diskStructure.keep_bad_images)
    124 		unlink(image);
    125 	if (diskStructure.verbose_level > 0)
    126 		printf("Bad image cleaned up\n");
    127 	return 0;
    128 }
    129 
    130 static int
    131 cd9660_write_volume_descriptors(FILE *fd)
    132 {
    133 	volume_descriptor *vd_temp = diskStructure.firstVolumeDescriptor;
    134 	int pos;
    135 
    136 	while (vd_temp != NULL) {
    137 		pos = vd_temp->sector*diskStructure.sectorSize;
    138 		cd9660_write_filedata(fd, vd_temp->sector,
    139 		    vd_temp->volumeDescriptorData, 1);
    140 		vd_temp = vd_temp->next;
    141 	}
    142 	return 1;
    143 }
    144 
    145 /*
    146  * Write out an individual path table
    147  * Used just to keep redundant code to a minimum
    148  * @param FILE *fd Valid file pointer
    149  * @param int Sector to start writing path table to
    150  * @param int Endian mode : BIG_ENDIAN or LITTLE_ENDIAN
    151  * @returns int 1 on success, 0 on failure
    152  */
    153 static int
    154 cd9660_write_path_table(FILE *fd, int sector, int mode)
    155 {
    156 	int path_table_sectors = CD9660_BLOCKS(diskStructure.sectorSize,
    157 	    diskStructure.pathTableLength);
    158 	unsigned char *buffer;
    159 	unsigned char *buffer_head;
    160 	int len;
    161 	path_table_entry temp_entry;
    162 	cd9660node *ptcur;
    163 
    164 	buffer = malloc(diskStructure.sectorSize * path_table_sectors);
    165 	if (buffer == NULL) {
    166 		warnx("%s: Memory allocation error allocating buffer",
    167 		    __func__);
    168 		return 0;
    169 	}
    170 	buffer_head = buffer;
    171 	memset(buffer, 0, diskStructure.sectorSize * path_table_sectors);
    172 
    173 	ptcur = diskStructure.rootNode;
    174 
    175 	while (ptcur != NULL) {
    176 		memset(&temp_entry, 0, sizeof(path_table_entry));
    177 		temp_entry.length[0] = ptcur->isoDirRecord->name_len[0];
    178 		temp_entry.extended_attribute_length[0] =
    179 		    ptcur->isoDirRecord->ext_attr_length[0];
    180 		memcpy(temp_entry.name, ptcur->isoDirRecord->name,
    181 		    temp_entry.length[0] + 1);
    182 
    183 		/* round up */
    184 		len = temp_entry.length[0] + 8 + (temp_entry.length[0] & 0x01);
    185 
    186                 /* todo: function pointers instead */
    187 		if (mode == LITTLE_ENDIAN) {
    188 			cd9660_731(ptcur->fileDataSector,
    189 			    temp_entry.first_sector);
    190 			cd9660_721((ptcur->parent == NULL ?
    191 				1 : ptcur->parent->ptnumber),
    192 			    temp_entry.parent_number);
    193 		} else {
    194 			cd9660_732(ptcur->fileDataSector,
    195 			    temp_entry.first_sector);
    196 			cd9660_722((ptcur->parent == NULL ?
    197 				1 : ptcur->parent->ptnumber),
    198 			    temp_entry.parent_number);
    199 		}
    200 
    201 
    202 		memcpy(buffer, &temp_entry, len);
    203 		buffer += len;
    204 
    205 		ptcur = ptcur->ptnext;
    206 	}
    207 
    208 	return cd9660_write_filedata(fd, sector, buffer_head,
    209 	    path_table_sectors);
    210 }
    211 
    212 
    213 /*
    214  * Write out the path tables to disk
    215  * Each file descriptor should be pointed to by the PVD, so we know which
    216  * sector to copy them to. One thing to watch out for: the only path tables
    217  * stored are in the endian mode that the application is compiled for. So,
    218  * the first thing to do is write out that path table, then to write the one
    219  * in the other endian mode requires to convert the endianness of each entry
    220  * in the table. The best way to do this would be to create a temporary
    221  * path_table_entry structure, then for each path table entry, copy it to
    222  * the temporary entry, translate, then copy that to disk.
    223  *
    224  * @param FILE* Valid file descriptor
    225  * @returns int 0 on failure, 1 on success
    226  */
    227 static int
    228 cd9660_write_path_tables(FILE *fd)
    229 {
    230 	if (cd9660_write_path_table(fd,
    231 	    diskStructure.primaryLittleEndianTableSector, LITTLE_ENDIAN) == 0)
    232 		return 0;
    233 
    234 	if (cd9660_write_path_table(fd,
    235 	    diskStructure.primaryBigEndianTableSector, BIG_ENDIAN) == 0)
    236 		return 0;
    237 
    238 	/* @TODO: handle remaining two path tables */
    239 	return 1;
    240 }
    241 
    242 /*
    243  * Write a file to disk
    244  * Writes a file, its directory record, and its data to disk
    245  * This file is designed to be called RECURSIVELY, so initially call it
    246  * with the root node. All of the records should store what sector the
    247  * file goes in, so no computation should be  necessary.
    248  *
    249  * @param int fd Valid file descriptor
    250  * @param struct cd9660node* writenode Pointer to the file to be written
    251  * @returns int 0 on failure, 1 on success
    252  */
    253 static int
    254 cd9660_write_file(FILE *fd, cd9660node *writenode)
    255 {
    256 	char *buf;
    257 	char *temp_file_name;
    258 	int ret;
    259 	int working_sector;
    260 	int cur_sector_offset;
    261 	int written;
    262 	iso_directory_record_cd9660 temp_record;
    263 	cd9660node *temp;
    264 	int ca = 0;
    265 
    266 	/* Todo : clean up variables */
    267 
    268 	temp_file_name = malloc(CD9660MAXPATH + 1);
    269 	if (temp_file_name == NULL)
    270 		err(EXIT_FAILURE, "%s: malloc", __func__);
    271 
    272 	memset(temp_file_name, 0, CD9660MAXPATH + 1);
    273 
    274 	buf = malloc(diskStructure.sectorSize);
    275 	if (buf == NULL)
    276 		err(EXIT_FAILURE, "%s: malloc", __func__);
    277 
    278 	if ((writenode->level != 0) &&
    279 	    !(writenode->node->type & S_IFDIR)) {
    280 		/* Only attempt to write files that have length */
    281 		if (writenode->fileDataLength > 0) {
    282 			cd9660_compute_full_filename(writenode,
    283 			    temp_file_name, 0);
    284 			ret = cd9660_copy_file(fd, writenode->fileDataSector,
    285 			    temp_file_name);
    286 			if (ret == 0) {
    287 				free(temp_file_name);
    288 				return 0;
    289 			}
    290 		}
    291 	} else {
    292 		/*
    293 		 * Here is a new revelation that ECMA didnt explain
    294 		 * (at least not well).
    295 		 * ALL . and .. records store the name "\0" and "\1"
    296 		 * resepctively. So, for each directory, we have to
    297 		 * make a new node.
    298 		 *
    299 		 * This is where it gets kinda messy, since we have to
    300 		 * be careful of sector boundaries
    301 		 */
    302 		cur_sector_offset = 0;
    303 		working_sector = writenode->fileDataSector;
    304 		fseek(fd, working_sector * diskStructure.sectorSize, SEEK_SET);
    305 
    306 		/*
    307 		 * Now loop over children, writing out their directory
    308 		 * records - beware of sector boundaries
    309 	 	 */
    310 		TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) {
    311 			/*
    312 			 * Copy the temporary record and adjust its size
    313 			 * if necessary
    314 			 */
    315 			memcpy(&temp_record, temp->isoDirRecord,
    316 			    sizeof(iso_directory_record_cd9660));
    317 
    318 			temp_record.length[0] =
    319 			    cd9660_compute_record_size(temp);
    320 
    321 			if (temp_record.length[0] + cur_sector_offset >=
    322 			    diskStructure.sectorSize) {
    323 				cur_sector_offset = 0;
    324 				working_sector++;
    325 
    326 				/* Seek to the next sector. */
    327 				fseek(fd,
    328 				    working_sector * diskStructure.sectorSize,
    329 				    SEEK_SET);
    330 			}
    331 
    332 			written = fwrite(&temp_record, 1, temp_record.length[0],
    333 			    fd);
    334 			ca = 0;
    335 			if (diskStructure.rock_ridge_enabled) {
    336 				ca = cd9660_write_rr(fd, temp,
    337 				    cur_sector_offset, working_sector);
    338 			}
    339 
    340 			if (ferror(fd)) {
    341 				warnx("%s: write error", __func__);
    342 				free(temp_file_name);
    343 				return 0;
    344 			}
    345 			cur_sector_offset += temp_record.length[0];
    346 
    347 			/*
    348 			 * If we had to go the the continuation area,
    349 			 * head back to where we should be.
    350 			 */
    351 			if (ca) {
    352 				fseek(fd,
    353 				    working_sector * diskStructure.sectorSize +
    354 					cur_sector_offset,
    355 				    SEEK_SET);
    356 			}
    357 		}
    358 
    359 		/*
    360 		 * Recurse on children.
    361 		 */
    362 		TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) {
    363 			if ((ret = cd9660_write_file(fd, temp)) == 0) {
    364 				free(temp_file_name);
    365 				return 0;
    366 			}
    367 		}
    368 	}
    369 	free(temp_file_name);
    370 	return 1;
    371 }
    372 
    373 /*
    374  * Wrapper function to write a buffer (one sector) to disk.
    375  * Seeks and writes the buffer.
    376  * NOTE: You dont NEED to use this function, but it might make your
    377  * life easier if you have to write things that align to a sector
    378  * (such as volume descriptors).
    379  *
    380  * @param int fd Valid file descriptor
    381  * @param int sector Sector number to write to
    382  * @param const unsigned char* Buffer to write. This should be the
    383  *                             size of a sector, and if only a portion
    384  *                             is written, the rest should be set to 0.
    385  */
    386 static int
    387 cd9660_write_filedata(FILE *fd, int sector, const unsigned char *buf,
    388 		      int numsecs)
    389 {
    390 	off_t curpos;
    391 	size_t success;
    392 
    393 	curpos = ftello(fd);
    394 
    395 	fseek(fd, sector * diskStructure.sectorSize, SEEK_SET);
    396 
    397 	success = fwrite(buf, diskStructure.sectorSize * numsecs, 1, fd);
    398 
    399 	fseek(fd, curpos, SEEK_SET);
    400 
    401 	if (success == 1)
    402 		success = diskStructure.sectorSize * numsecs;
    403 	return success;
    404 }
    405 
    406 #if 0
    407 static int
    408 cd9660_write_buffered(FILE *fd, int offset, int buff_len,
    409 		      const unsigned char* buffer)
    410 {
    411 	static int working_sector = -1;
    412 	static char buf[2048];
    413 
    414 	return 0;
    415 }
    416 #endif
    417 
    418 int
    419 cd9660_copy_file(FILE *fd, int start_sector, const char *filename)
    420 {
    421 	FILE *rf;
    422 	int bytes_read;
    423 	int sector = start_sector;
    424 	int buf_size = diskStructure.sectorSize;
    425 	char *buf;
    426 
    427 	buf = malloc(buf_size);
    428 	if (buf == NULL)
    429 		err(EXIT_FAILURE, "%s: malloc", __func__);
    430 
    431 	if ((rf = fopen(filename, "rb")) == NULL) {
    432 		warn("%s: cannot open %s", __func__, filename);
    433 		return 0;
    434 	}
    435 
    436 	if (diskStructure.verbose_level > 1)
    437 		printf("Writing file: %s\n",filename);
    438 
    439 	fseek(fd, start_sector * diskStructure.sectorSize, SEEK_SET);
    440 
    441 	while (!feof(rf)) {
    442 		bytes_read = fread(buf,1,buf_size,rf);
    443 		if (ferror(rf)) {
    444 			warn("%s: fread", __func__);
    445 			return 0;
    446 		}
    447 
    448 		fwrite(buf,1,bytes_read,fd);
    449 		if (ferror(fd)) {
    450 			warn("%s: fwrite", __func__);
    451 			return 0;
    452 		}
    453 		sector++;
    454 	}
    455 
    456 	fclose(rf);
    457 	free(buf);
    458 	return 1;
    459 }
    460 
    461 static int
    462 cd9660_write_rr(FILE *fd, cd9660node *writenode, int offset, int sector)
    463 {
    464 	int in_ca = 0;
    465 	struct ISO_SUSP_ATTRIBUTES *myattr;
    466 
    467 	offset += writenode->isoDirRecord->length[0];
    468 
    469 	/* Offset now points at the end of the record */
    470 	TAILQ_FOREACH(myattr, &writenode->head, rr_ll) {
    471 		fseek(fd,
    472 		    in_ca ? offset : sector*diskStructure.sectorSize + offset,
    473 		    SEEK_SET);
    474 		fwrite(&(myattr->attr), CD9660_SUSP_ENTRY_SIZE(myattr), 1, fd);
    475 
    476 		offset += CD9660_SUSP_ENTRY_SIZE(myattr);
    477 		if (!in_ca) {
    478 			if ((myattr->susp_type == SUSP_TYPE_SUSP) &&
    479 			    (myattr->entry_type == SUSP_ENTRY_SUSP_CE)) {
    480 				/*
    481 				 * Point the offset to the start of this
    482 				 * record's CE area
    483 				 */
    484 				offset = (diskStructure.
    485 					  susp_continuation_area_start_sector *
    486 					    diskStructure.sectorSize)
    487 					+ writenode->susp_entry_ce_start;
    488 				in_ca = 1;
    489 			}
    490 		}
    491 	}
    492 
    493 	return in_ca;
    494 }
    495