xref: /freebsd/usr.sbin/makefs/cd9660/cd9660_write.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1852f933dSMarius Strobl /*	$NetBSD: cd9660_write.c,v 1.14 2011/01/04 09:48:21 wiz Exp $	*/
201a0f853SOlivier Houchard 
31de7b4b8SPedro F. Giffuni /*-
4*b61a5730SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
51de7b4b8SPedro F. Giffuni  *
601a0f853SOlivier Houchard  * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
701a0f853SOlivier Houchard  * Perez-Rathke and Ram Vedam.  All rights reserved.
801a0f853SOlivier Houchard  *
901a0f853SOlivier Houchard  * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
1001a0f853SOlivier Houchard  * Alan Perez-Rathke and Ram Vedam.
1101a0f853SOlivier Houchard  *
1201a0f853SOlivier Houchard  * Redistribution and use in source and binary forms, with or
1301a0f853SOlivier Houchard  * without modification, are permitted provided that the following
1401a0f853SOlivier Houchard  * conditions are met:
1501a0f853SOlivier Houchard  * 1. Redistributions of source code must retain the above copyright
1601a0f853SOlivier Houchard  *    notice, this list of conditions and the following disclaimer.
1701a0f853SOlivier Houchard  * 2. Redistributions in binary form must reproduce the above
1801a0f853SOlivier Houchard  *    copyright notice, this list of conditions and the following
1901a0f853SOlivier Houchard  *    disclaimer in the documentation and/or other materials provided
2001a0f853SOlivier Houchard  *    with the distribution.
2101a0f853SOlivier Houchard  *
2201a0f853SOlivier Houchard  * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
2301a0f853SOlivier Houchard  * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
2401a0f853SOlivier Houchard  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2501a0f853SOlivier Houchard  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2601a0f853SOlivier Houchard  * DISCLAIMED.  IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
2701a0f853SOlivier Houchard  * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
2801a0f853SOlivier Houchard  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2901a0f853SOlivier Houchard  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
3001a0f853SOlivier Houchard  * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
3101a0f853SOlivier Houchard  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3201a0f853SOlivier Houchard  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3301a0f853SOlivier Houchard  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
3401a0f853SOlivier Houchard  * OF SUCH DAMAGE.
3501a0f853SOlivier Houchard  */
3601a0f853SOlivier Houchard 
3701a0f853SOlivier Houchard #include "cd9660.h"
3801a0f853SOlivier Houchard #include "iso9660_rrip.h"
3901a0f853SOlivier Houchard 
4001a0f853SOlivier Houchard #include <sys/cdefs.h>
415f5598b1SEd Maste #include <util.h>
425f5598b1SEd Maste 
431631d42aSEd Maste static int cd9660_write_volume_descriptors(iso9660_disk *, FILE *);
441631d42aSEd Maste static int cd9660_write_path_table(iso9660_disk *, FILE *, off_t, int);
451631d42aSEd Maste static int cd9660_write_path_tables(iso9660_disk *, FILE *);
461631d42aSEd Maste static int cd9660_write_file(iso9660_disk *, FILE *, cd9660node *);
471631d42aSEd Maste static int cd9660_write_filedata(iso9660_disk *, FILE *, off_t,
481631d42aSEd Maste     const unsigned char *, int);
4901a0f853SOlivier Houchard #if 0
5001a0f853SOlivier Houchard static int cd9660_write_buffered(FILE *, off_t, int, const unsigned char *);
5101a0f853SOlivier Houchard #endif
521631d42aSEd Maste static void cd9660_write_rr(iso9660_disk *, FILE *, cd9660node *, off_t, off_t);
5301a0f853SOlivier Houchard 
5401a0f853SOlivier Houchard /*
5501a0f853SOlivier Houchard  * Write the image
5601a0f853SOlivier Houchard  * Writes the entire image
5701a0f853SOlivier Houchard  * @param const char* The filename for the image
5801a0f853SOlivier Houchard  * @returns int 1 on success, 0 on failure
5901a0f853SOlivier Houchard  */
6001a0f853SOlivier Houchard int
cd9660_write_image(iso9660_disk * diskStructure,const char * image)611631d42aSEd Maste cd9660_write_image(iso9660_disk *diskStructure, const char* image)
6201a0f853SOlivier Houchard {
6301a0f853SOlivier Houchard 	FILE *fd;
6401a0f853SOlivier Houchard 	int status;
65fe41c64bSAlex Richardson 	unsigned char buf[CD9660_SECTOR_SIZE];
6601a0f853SOlivier Houchard 
6701a0f853SOlivier Houchard 	if ((fd = fopen(image, "w+")) == NULL) {
6801a0f853SOlivier Houchard 		err(EXIT_FAILURE, "%s: Can't open `%s' for writing", __func__,
6901a0f853SOlivier Houchard 		    image);
7001a0f853SOlivier Houchard 	}
7101a0f853SOlivier Houchard 
721631d42aSEd Maste 	if (diskStructure->verbose_level > 0)
7301a0f853SOlivier Houchard 		printf("Writing image\n");
7401a0f853SOlivier Houchard 
751631d42aSEd Maste 	if (diskStructure->has_generic_bootimage) {
761631d42aSEd Maste 		status = cd9660_copy_file(diskStructure, fd, 0,
771631d42aSEd Maste 		    diskStructure->generic_bootimage);
7801a0f853SOlivier Houchard 		if (status == 0) {
7901a0f853SOlivier Houchard 			warnx("%s: Error writing generic boot image",
8001a0f853SOlivier Houchard 			    __func__);
8101a0f853SOlivier Houchard 			goto cleanup_bad_image;
8201a0f853SOlivier Houchard 		}
8301a0f853SOlivier Houchard 	}
8401a0f853SOlivier Houchard 
8501a0f853SOlivier Houchard 	/* Write the volume descriptors */
861631d42aSEd Maste 	status = cd9660_write_volume_descriptors(diskStructure, fd);
8701a0f853SOlivier Houchard 	if (status == 0) {
8801a0f853SOlivier Houchard 		warnx("%s: Error writing volume descriptors to image",
8901a0f853SOlivier Houchard 		    __func__);
9001a0f853SOlivier Houchard 		goto cleanup_bad_image;
9101a0f853SOlivier Houchard 	}
9201a0f853SOlivier Houchard 
931631d42aSEd Maste 	if (diskStructure->verbose_level > 0)
9401a0f853SOlivier Houchard 		printf("Volume descriptors written\n");
9501a0f853SOlivier Houchard 
9601a0f853SOlivier Houchard 	/*
9701a0f853SOlivier Houchard 	 * Write the path tables: there are actually four, but right
98164fa411SGordon Bergling 	 * now we are only concerned with two.
9901a0f853SOlivier Houchard 	 */
1001631d42aSEd Maste 	status = cd9660_write_path_tables(diskStructure, fd);
10101a0f853SOlivier Houchard 	if (status == 0) {
10201a0f853SOlivier Houchard 		warnx("%s: Error writing path tables to image", __func__);
10301a0f853SOlivier Houchard 		goto cleanup_bad_image;
10401a0f853SOlivier Houchard 	}
10501a0f853SOlivier Houchard 
1061631d42aSEd Maste 	if (diskStructure->verbose_level > 0)
10701a0f853SOlivier Houchard 		printf("Path tables written\n");
10801a0f853SOlivier Houchard 
10901a0f853SOlivier Houchard 	/* Write the directories and files */
1101631d42aSEd Maste 	status = cd9660_write_file(diskStructure, fd, diskStructure->rootNode);
11101a0f853SOlivier Houchard 	if (status == 0) {
11201a0f853SOlivier Houchard 		warnx("%s: Error writing files to image", __func__);
11301a0f853SOlivier Houchard 		goto cleanup_bad_image;
11401a0f853SOlivier Houchard 	}
11501a0f853SOlivier Houchard 
1161631d42aSEd Maste 	if (diskStructure->is_bootable) {
1171631d42aSEd Maste 		cd9660_write_boot(diskStructure, fd);
11801a0f853SOlivier Houchard 	}
11901a0f853SOlivier Houchard 
12001a0f853SOlivier Houchard 	/* Write padding bits. This is temporary */
12101a0f853SOlivier Houchard 	memset(buf, 0, CD9660_SECTOR_SIZE);
1221631d42aSEd Maste 	cd9660_write_filedata(diskStructure, fd,
1231631d42aSEd Maste 	    diskStructure->totalSectors - 1, buf, 1);
12401a0f853SOlivier Houchard 
1251631d42aSEd Maste 	if (diskStructure->verbose_level > 0)
12601a0f853SOlivier Houchard 		printf("Files written\n");
12701a0f853SOlivier Houchard 	fclose(fd);
12801a0f853SOlivier Houchard 
1291631d42aSEd Maste 	if (diskStructure->verbose_level > 0)
13001a0f853SOlivier Houchard 		printf("Image closed\n");
13101a0f853SOlivier Houchard 	return 1;
13201a0f853SOlivier Houchard 
13301a0f853SOlivier Houchard cleanup_bad_image:
13401a0f853SOlivier Houchard 	fclose(fd);
1351631d42aSEd Maste 	if (!diskStructure->keep_bad_images)
13601a0f853SOlivier Houchard 		unlink(image);
1371631d42aSEd Maste 	if (diskStructure->verbose_level > 0)
13801a0f853SOlivier Houchard 		printf("Bad image cleaned up\n");
13901a0f853SOlivier Houchard 	return 0;
14001a0f853SOlivier Houchard }
14101a0f853SOlivier Houchard 
14201a0f853SOlivier Houchard static int
cd9660_write_volume_descriptors(iso9660_disk * diskStructure,FILE * fd)1431631d42aSEd Maste cd9660_write_volume_descriptors(iso9660_disk *diskStructure, FILE *fd)
14401a0f853SOlivier Houchard {
1451631d42aSEd Maste 	volume_descriptor *vd_temp = diskStructure->firstVolumeDescriptor;
14601a0f853SOlivier Houchard 
14701a0f853SOlivier Houchard 	while (vd_temp != NULL) {
1481631d42aSEd Maste 		cd9660_write_filedata(diskStructure, fd, vd_temp->sector,
14901a0f853SOlivier Houchard 		    vd_temp->volumeDescriptorData, 1);
15001a0f853SOlivier Houchard 		vd_temp = vd_temp->next;
15101a0f853SOlivier Houchard 	}
15201a0f853SOlivier Houchard 	return 1;
15301a0f853SOlivier Houchard }
15401a0f853SOlivier Houchard 
15501a0f853SOlivier Houchard /*
15601a0f853SOlivier Houchard  * Write out an individual path table
15701a0f853SOlivier Houchard  * Used just to keep redundant code to a minimum
15801a0f853SOlivier Houchard  * @param FILE *fd Valid file pointer
15901a0f853SOlivier Houchard  * @param int Sector to start writing path table to
16001a0f853SOlivier Houchard  * @param int Endian mode : BIG_ENDIAN or LITTLE_ENDIAN
16101a0f853SOlivier Houchard  * @returns int 1 on success, 0 on failure
16201a0f853SOlivier Houchard  */
16301a0f853SOlivier Houchard static int
cd9660_write_path_table(iso9660_disk * diskStructure,FILE * fd,off_t sector,int mode)1641631d42aSEd Maste cd9660_write_path_table(iso9660_disk *diskStructure, FILE *fd, off_t sector,
1651631d42aSEd Maste     int mode)
16601a0f853SOlivier Houchard {
1671631d42aSEd Maste 	int path_table_sectors = CD9660_BLOCKS(diskStructure->sectorSize,
1681631d42aSEd Maste 	    diskStructure->pathTableLength);
16901a0f853SOlivier Houchard 	unsigned char *buffer;
17001a0f853SOlivier Houchard 	unsigned char *buffer_head;
171b73321f0SEnji Cooper 	int len, ret;
17201a0f853SOlivier Houchard 	path_table_entry temp_entry;
17301a0f853SOlivier Houchard 	cd9660node *ptcur;
17401a0f853SOlivier Houchard 
1755f5598b1SEd Maste 	buffer = ecalloc(path_table_sectors, diskStructure->sectorSize);
17601a0f853SOlivier Houchard 	buffer_head = buffer;
17701a0f853SOlivier Houchard 
1781631d42aSEd Maste 	ptcur = diskStructure->rootNode;
17901a0f853SOlivier Houchard 
18001a0f853SOlivier Houchard 	while (ptcur != NULL) {
18101a0f853SOlivier Houchard 		memset(&temp_entry, 0, sizeof(path_table_entry));
18201a0f853SOlivier Houchard 		temp_entry.length[0] = ptcur->isoDirRecord->name_len[0];
18301a0f853SOlivier Houchard 		temp_entry.extended_attribute_length[0] =
18401a0f853SOlivier Houchard 		    ptcur->isoDirRecord->ext_attr_length[0];
18501a0f853SOlivier Houchard 		memcpy(temp_entry.name, ptcur->isoDirRecord->name,
18601a0f853SOlivier Houchard 		    temp_entry.length[0] + 1);
18701a0f853SOlivier Houchard 
18801a0f853SOlivier Houchard 		/* round up */
18901a0f853SOlivier Houchard 		len = temp_entry.length[0] + 8 + (temp_entry.length[0] & 0x01);
19001a0f853SOlivier Houchard 
19101a0f853SOlivier Houchard                 /* todo: function pointers instead */
19201a0f853SOlivier Houchard 		if (mode == LITTLE_ENDIAN) {
19301a0f853SOlivier Houchard 			cd9660_731(ptcur->fileDataSector,
19401a0f853SOlivier Houchard 			    temp_entry.first_sector);
19501a0f853SOlivier Houchard 			cd9660_721((ptcur->parent == NULL ?
19601a0f853SOlivier Houchard 				1 : ptcur->parent->ptnumber),
19701a0f853SOlivier Houchard 			    temp_entry.parent_number);
19801a0f853SOlivier Houchard 		} else {
19901a0f853SOlivier Houchard 			cd9660_732(ptcur->fileDataSector,
20001a0f853SOlivier Houchard 			    temp_entry.first_sector);
20101a0f853SOlivier Houchard 			cd9660_722((ptcur->parent == NULL ?
20201a0f853SOlivier Houchard 				1 : ptcur->parent->ptnumber),
20301a0f853SOlivier Houchard 			    temp_entry.parent_number);
20401a0f853SOlivier Houchard 		}
20501a0f853SOlivier Houchard 
20601a0f853SOlivier Houchard 
20701a0f853SOlivier Houchard 		memcpy(buffer, &temp_entry, len);
20801a0f853SOlivier Houchard 		buffer += len;
20901a0f853SOlivier Houchard 
21001a0f853SOlivier Houchard 		ptcur = ptcur->ptnext;
21101a0f853SOlivier Houchard 	}
21201a0f853SOlivier Houchard 
2131631d42aSEd Maste 	ret = cd9660_write_filedata(diskStructure, fd, sector, buffer_head,
21401a0f853SOlivier Houchard 	    path_table_sectors);
21542afd55dSEnji Cooper 	free(buffer_head);
216b73321f0SEnji Cooper 	return ret;
21701a0f853SOlivier Houchard }
21801a0f853SOlivier Houchard 
21901a0f853SOlivier Houchard 
22001a0f853SOlivier Houchard /*
22101a0f853SOlivier Houchard  * Write out the path tables to disk
22201a0f853SOlivier Houchard  * Each file descriptor should be pointed to by the PVD, so we know which
22301a0f853SOlivier Houchard  * sector to copy them to. One thing to watch out for: the only path tables
22401a0f853SOlivier Houchard  * stored are in the endian mode that the application is compiled for. So,
22501a0f853SOlivier Houchard  * the first thing to do is write out that path table, then to write the one
22601a0f853SOlivier Houchard  * in the other endian mode requires to convert the endianness of each entry
22701a0f853SOlivier Houchard  * in the table. The best way to do this would be to create a temporary
22801a0f853SOlivier Houchard  * path_table_entry structure, then for each path table entry, copy it to
22901a0f853SOlivier Houchard  * the temporary entry, translate, then copy that to disk.
23001a0f853SOlivier Houchard  *
23101a0f853SOlivier Houchard  * @param FILE* Valid file descriptor
23201a0f853SOlivier Houchard  * @returns int 0 on failure, 1 on success
23301a0f853SOlivier Houchard  */
23401a0f853SOlivier Houchard static int
cd9660_write_path_tables(iso9660_disk * diskStructure,FILE * fd)2351631d42aSEd Maste cd9660_write_path_tables(iso9660_disk *diskStructure, FILE *fd)
23601a0f853SOlivier Houchard {
2371631d42aSEd Maste 	if (cd9660_write_path_table(diskStructure, fd,
2381631d42aSEd Maste 	    diskStructure->primaryLittleEndianTableSector, LITTLE_ENDIAN) == 0)
23901a0f853SOlivier Houchard 		return 0;
24001a0f853SOlivier Houchard 
2411631d42aSEd Maste 	if (cd9660_write_path_table(diskStructure, fd,
2421631d42aSEd Maste 	    diskStructure->primaryBigEndianTableSector, BIG_ENDIAN) == 0)
24301a0f853SOlivier Houchard 		return 0;
24401a0f853SOlivier Houchard 
24501a0f853SOlivier Houchard 	/* @TODO: handle remaining two path tables */
24601a0f853SOlivier Houchard 	return 1;
24701a0f853SOlivier Houchard }
24801a0f853SOlivier Houchard 
24901a0f853SOlivier Houchard /*
25001a0f853SOlivier Houchard  * Write a file to disk
25101a0f853SOlivier Houchard  * Writes a file, its directory record, and its data to disk
25201a0f853SOlivier Houchard  * This file is designed to be called RECURSIVELY, so initially call it
25301a0f853SOlivier Houchard  * with the root node. All of the records should store what sector the
25401a0f853SOlivier Houchard  * file goes in, so no computation should be  necessary.
25501a0f853SOlivier Houchard  *
25601a0f853SOlivier Houchard  * @param int fd Valid file descriptor
25701a0f853SOlivier Houchard  * @param struct cd9660node* writenode Pointer to the file to be written
25801a0f853SOlivier Houchard  * @returns int 0 on failure, 1 on success
25901a0f853SOlivier Houchard  */
26001a0f853SOlivier Houchard static int
cd9660_write_file(iso9660_disk * diskStructure,FILE * fd,cd9660node * writenode)2611631d42aSEd Maste cd9660_write_file(iso9660_disk *diskStructure, FILE *fd, cd9660node *writenode)
26201a0f853SOlivier Houchard {
26301a0f853SOlivier Houchard 	char *buf;
26401a0f853SOlivier Houchard 	char *temp_file_name;
26501a0f853SOlivier Houchard 	int ret;
26601a0f853SOlivier Houchard 	off_t working_sector;
26701a0f853SOlivier Houchard 	int cur_sector_offset;
26801a0f853SOlivier Houchard 	iso_directory_record_cd9660 temp_record;
26901a0f853SOlivier Houchard 	cd9660node *temp;
27001a0f853SOlivier Houchard 	int rv = 0;
27101a0f853SOlivier Houchard 
27201a0f853SOlivier Houchard 	/* Todo : clean up variables */
27301a0f853SOlivier Houchard 
2742f11df63SEd Maste 	temp_file_name = ecalloc(PATH_MAX, 1);
2755f5598b1SEd Maste 	buf = emalloc(diskStructure->sectorSize);
27601a0f853SOlivier Houchard 	if ((writenode->level != 0) &&
27701a0f853SOlivier Houchard 	    !(writenode->node->type & S_IFDIR)) {
27801a0f853SOlivier Houchard 		fsinode *inode = writenode->node->inode;
27901a0f853SOlivier Houchard 		/* Only attempt to write unwritten files that have length. */
28001a0f853SOlivier Houchard 		if ((inode->flags & FI_WRITTEN) != 0) {
28101a0f853SOlivier Houchard 			INODE_WARNX(("%s: skipping written inode %d", __func__,
28201a0f853SOlivier Houchard 			    (int)inode->st.st_ino));
28301a0f853SOlivier Houchard 		} else if (writenode->fileDataLength > 0) {
28401a0f853SOlivier Houchard 			INODE_WARNX(("%s: writing inode %d blocks at %" PRIu32,
28501a0f853SOlivier Houchard 			    __func__, (int)inode->st.st_ino, inode->ino));
28601a0f853SOlivier Houchard 			inode->flags |= FI_WRITTEN;
287484b5c25SMarcel Moolenaar 			if (writenode->node->contents == NULL)
28801a0f853SOlivier Houchard 				cd9660_compute_full_filename(writenode,
289688aaa09SJung-uk Kim 				    temp_file_name);
2901631d42aSEd Maste 			ret = cd9660_copy_file(diskStructure, fd,
2911631d42aSEd Maste 			    writenode->fileDataSector,
292484b5c25SMarcel Moolenaar 			    (writenode->node->contents != NULL) ?
293484b5c25SMarcel Moolenaar 			    writenode->node->contents : temp_file_name);
29401a0f853SOlivier Houchard 			if (ret == 0)
29501a0f853SOlivier Houchard 				goto out;
29601a0f853SOlivier Houchard 		}
29701a0f853SOlivier Houchard 	} else {
29801a0f853SOlivier Houchard 		/*
2993df5ecacSUlrich Spörlein 		 * Here is a new revelation that ECMA didn't explain
30001a0f853SOlivier Houchard 		 * (at least not well).
30101a0f853SOlivier Houchard 		 * ALL . and .. records store the name "\0" and "\1"
3023df5ecacSUlrich Spörlein 		 * respectively. So, for each directory, we have to
30301a0f853SOlivier Houchard 		 * make a new node.
30401a0f853SOlivier Houchard 		 *
30501a0f853SOlivier Houchard 		 * This is where it gets kinda messy, since we have to
30601a0f853SOlivier Houchard 		 * be careful of sector boundaries
30701a0f853SOlivier Houchard 		 */
30801a0f853SOlivier Houchard 		cur_sector_offset = 0;
30901a0f853SOlivier Houchard 		working_sector = writenode->fileDataSector;
3101631d42aSEd Maste 		if (fseeko(fd, working_sector * diskStructure->sectorSize,
31101a0f853SOlivier Houchard 		    SEEK_SET) == -1)
31201a0f853SOlivier Houchard 			err(1, "fseeko");
31301a0f853SOlivier Houchard 
31401a0f853SOlivier Houchard 		/*
31501a0f853SOlivier Houchard 		 * Now loop over children, writing out their directory
31601a0f853SOlivier Houchard 		 * records - beware of sector boundaries
31701a0f853SOlivier Houchard 		 */
31801a0f853SOlivier Houchard 		TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) {
31901a0f853SOlivier Houchard 			/*
32001a0f853SOlivier Houchard 			 * Copy the temporary record and adjust its size
32101a0f853SOlivier Houchard 			 * if necessary
32201a0f853SOlivier Houchard 			 */
32301a0f853SOlivier Houchard 			memcpy(&temp_record, temp->isoDirRecord,
32401a0f853SOlivier Houchard 			    sizeof(iso_directory_record_cd9660));
32501a0f853SOlivier Houchard 
32601a0f853SOlivier Houchard 			temp_record.length[0] =
3271631d42aSEd Maste 			    cd9660_compute_record_size(diskStructure, temp);
32801a0f853SOlivier Houchard 
32901a0f853SOlivier Houchard 			if (temp_record.length[0] + cur_sector_offset >=
3301631d42aSEd Maste 			    diskStructure->sectorSize) {
33101a0f853SOlivier Houchard 				cur_sector_offset = 0;
33201a0f853SOlivier Houchard 				working_sector++;
33301a0f853SOlivier Houchard 
33401a0f853SOlivier Houchard 				/* Seek to the next sector. */
33501a0f853SOlivier Houchard 				if (fseeko(fd, working_sector *
3361631d42aSEd Maste 				    diskStructure->sectorSize, SEEK_SET) == -1)
33701a0f853SOlivier Houchard 					err(1, "fseeko");
33801a0f853SOlivier Houchard 			}
33901a0f853SOlivier Houchard 			/* Write out the basic ISO directory record */
34037ca9f42SEd Maste 			(void)fwrite(&temp_record, 1,
34101a0f853SOlivier Houchard 			    temp->isoDirRecord->length[0], fd);
3421631d42aSEd Maste 			if (diskStructure->rock_ridge_enabled) {
3431631d42aSEd Maste 				cd9660_write_rr(diskStructure, fd, temp,
34401a0f853SOlivier Houchard 				    cur_sector_offset, working_sector);
34501a0f853SOlivier Houchard 			}
34601a0f853SOlivier Houchard 			if (fseeko(fd, working_sector *
3471631d42aSEd Maste 			    diskStructure->sectorSize + cur_sector_offset +
34801a0f853SOlivier Houchard 			    temp_record.length[0] - temp->su_tail_size,
34901a0f853SOlivier Houchard 			    SEEK_SET) == -1)
35001a0f853SOlivier Houchard 				err(1, "fseeko");
35101a0f853SOlivier Houchard 			if (temp->su_tail_size > 0)
35201a0f853SOlivier Houchard 				fwrite(temp->su_tail_data, 1,
35301a0f853SOlivier Houchard 				    temp->su_tail_size, fd);
35401a0f853SOlivier Houchard 			if (ferror(fd)) {
35501a0f853SOlivier Houchard 				warnx("%s: write error", __func__);
35601a0f853SOlivier Houchard 				goto out;
35701a0f853SOlivier Houchard 			}
35801a0f853SOlivier Houchard 			cur_sector_offset += temp_record.length[0];
35901a0f853SOlivier Houchard 
36001a0f853SOlivier Houchard 		}
36101a0f853SOlivier Houchard 
36201a0f853SOlivier Houchard 		/*
36301a0f853SOlivier Houchard 		 * Recurse on children.
36401a0f853SOlivier Houchard 		 */
36501a0f853SOlivier Houchard 		TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) {
3661631d42aSEd Maste 			if ((ret = cd9660_write_file(diskStructure, fd, temp)) == 0)
36701a0f853SOlivier Houchard 				goto out;
36801a0f853SOlivier Houchard 		}
36901a0f853SOlivier Houchard 	}
37001a0f853SOlivier Houchard 	rv = 1;
37101a0f853SOlivier Houchard out:
37201a0f853SOlivier Houchard 	free(temp_file_name);
37301a0f853SOlivier Houchard 	free(buf);
37401a0f853SOlivier Houchard 	return rv;
37501a0f853SOlivier Houchard }
37601a0f853SOlivier Houchard 
37701a0f853SOlivier Houchard /*
37801a0f853SOlivier Houchard  * Wrapper function to write a buffer (one sector) to disk.
37901a0f853SOlivier Houchard  * Seeks and writes the buffer.
38001a0f853SOlivier Houchard  * NOTE: You dont NEED to use this function, but it might make your
38101a0f853SOlivier Houchard  * life easier if you have to write things that align to a sector
38201a0f853SOlivier Houchard  * (such as volume descriptors).
38301a0f853SOlivier Houchard  *
38401a0f853SOlivier Houchard  * @param int fd Valid file descriptor
38501a0f853SOlivier Houchard  * @param int sector Sector number to write to
38601a0f853SOlivier Houchard  * @param const unsigned char* Buffer to write. This should be the
38701a0f853SOlivier Houchard  *                             size of a sector, and if only a portion
38801a0f853SOlivier Houchard  *                             is written, the rest should be set to 0.
38901a0f853SOlivier Houchard  */
39001a0f853SOlivier Houchard static int
cd9660_write_filedata(iso9660_disk * diskStructure,FILE * fd,off_t sector,const unsigned char * buf,int numsecs)3911631d42aSEd Maste cd9660_write_filedata(iso9660_disk *diskStructure, FILE *fd, off_t sector,
3921631d42aSEd Maste     const unsigned char *buf, int numsecs)
39301a0f853SOlivier Houchard {
39401a0f853SOlivier Houchard 	off_t curpos;
39501a0f853SOlivier Houchard 	size_t success;
39601a0f853SOlivier Houchard 
39701a0f853SOlivier Houchard 	curpos = ftello(fd);
39801a0f853SOlivier Houchard 
3991631d42aSEd Maste 	if (fseeko(fd, sector * diskStructure->sectorSize, SEEK_SET) == -1)
40001a0f853SOlivier Houchard 		err(1, "fseeko");
40101a0f853SOlivier Houchard 
4021631d42aSEd Maste 	success = fwrite(buf, diskStructure->sectorSize * numsecs, 1, fd);
40301a0f853SOlivier Houchard 
40401a0f853SOlivier Houchard 	if (fseeko(fd, curpos, SEEK_SET) == -1)
40501a0f853SOlivier Houchard 		err(1, "fseeko");
40601a0f853SOlivier Houchard 
40701a0f853SOlivier Houchard 	if (success == 1)
4081631d42aSEd Maste 		success = diskStructure->sectorSize * numsecs;
40901a0f853SOlivier Houchard 	return success;
41001a0f853SOlivier Houchard }
41101a0f853SOlivier Houchard 
41201a0f853SOlivier Houchard #if 0
41301a0f853SOlivier Houchard static int
41401a0f853SOlivier Houchard cd9660_write_buffered(FILE *fd, off_t offset, int buff_len,
41501a0f853SOlivier Houchard 		      const unsigned char* buffer)
41601a0f853SOlivier Houchard {
41701a0f853SOlivier Houchard 	static int working_sector = -1;
41801a0f853SOlivier Houchard 	static char buf[CD9660_SECTOR_SIZE];
41901a0f853SOlivier Houchard 
42001a0f853SOlivier Houchard 	return 0;
42101a0f853SOlivier Houchard }
42201a0f853SOlivier Houchard #endif
42301a0f853SOlivier Houchard 
42401a0f853SOlivier Houchard int
cd9660_copy_file(iso9660_disk * diskStructure,FILE * fd,off_t start_sector,const char * filename)4251631d42aSEd Maste cd9660_copy_file(iso9660_disk *diskStructure, FILE *fd, off_t start_sector,
4261631d42aSEd Maste     const char *filename)
42701a0f853SOlivier Houchard {
42801a0f853SOlivier Houchard 	FILE *rf;
42901a0f853SOlivier Houchard 	int bytes_read;
4301631d42aSEd Maste 	int buf_size = diskStructure->sectorSize;
43101a0f853SOlivier Houchard 	char *buf;
43201a0f853SOlivier Houchard 
4335f5598b1SEd Maste 	buf = emalloc(buf_size);
43401a0f853SOlivier Houchard 	if ((rf = fopen(filename, "rb")) == NULL) {
43501a0f853SOlivier Houchard 		warn("%s: cannot open %s", __func__, filename);
43601a0f853SOlivier Houchard 		free(buf);
43701a0f853SOlivier Houchard 		return 0;
43801a0f853SOlivier Houchard 	}
43901a0f853SOlivier Houchard 
4401631d42aSEd Maste 	if (diskStructure->verbose_level > 1)
44101a0f853SOlivier Houchard 		printf("Writing file: %s\n",filename);
44201a0f853SOlivier Houchard 
4431631d42aSEd Maste 	if (fseeko(fd, start_sector * diskStructure->sectorSize, SEEK_SET) == -1)
44401a0f853SOlivier Houchard 		err(1, "fseeko");
44501a0f853SOlivier Houchard 
44601a0f853SOlivier Houchard 	while (!feof(rf)) {
44701a0f853SOlivier Houchard 		bytes_read = fread(buf,1,buf_size,rf);
44801a0f853SOlivier Houchard 		if (ferror(rf)) {
44901a0f853SOlivier Houchard 			warn("%s: fread", __func__);
45001a0f853SOlivier Houchard 			free(buf);
451852f933dSMarius Strobl 			(void)fclose(rf);
45201a0f853SOlivier Houchard 			return 0;
45301a0f853SOlivier Houchard 		}
45401a0f853SOlivier Houchard 
45501a0f853SOlivier Houchard 		fwrite(buf,1,bytes_read,fd);
45601a0f853SOlivier Houchard 		if (ferror(fd)) {
45701a0f853SOlivier Houchard 			warn("%s: fwrite", __func__);
45801a0f853SOlivier Houchard 			free(buf);
459852f933dSMarius Strobl 			(void)fclose(rf);
46001a0f853SOlivier Houchard 			return 0;
46101a0f853SOlivier Houchard 		}
46201a0f853SOlivier Houchard 	}
46301a0f853SOlivier Houchard 
46401a0f853SOlivier Houchard 	fclose(rf);
46501a0f853SOlivier Houchard 	free(buf);
46601a0f853SOlivier Houchard 	return 1;
46701a0f853SOlivier Houchard }
46801a0f853SOlivier Houchard 
46901a0f853SOlivier Houchard static void
cd9660_write_rr(iso9660_disk * diskStructure,FILE * fd,cd9660node * writenode,off_t offset,off_t sector)4701631d42aSEd Maste cd9660_write_rr(iso9660_disk *diskStructure, FILE *fd, cd9660node *writenode,
4711631d42aSEd Maste     off_t offset, off_t sector)
47201a0f853SOlivier Houchard {
47301a0f853SOlivier Houchard 	int in_ca = 0;
47401a0f853SOlivier Houchard 	struct ISO_SUSP_ATTRIBUTES *myattr;
47501a0f853SOlivier Houchard 
47601a0f853SOlivier Houchard 	offset += writenode->isoDirRecord->length[0];
4771631d42aSEd Maste 	if (fseeko(fd, sector * diskStructure->sectorSize + offset, SEEK_SET) ==
47801a0f853SOlivier Houchard 	    -1)
47901a0f853SOlivier Houchard 		err(1, "fseeko");
48001a0f853SOlivier Houchard 	/* Offset now points at the end of the record */
48101a0f853SOlivier Houchard 	TAILQ_FOREACH(myattr, &writenode->head, rr_ll) {
48201a0f853SOlivier Houchard 		fwrite(&(myattr->attr), CD9660_SUSP_ENTRY_SIZE(myattr), 1, fd);
48301a0f853SOlivier Houchard 
48401a0f853SOlivier Houchard 		if (!in_ca) {
48501a0f853SOlivier Houchard 			offset += CD9660_SUSP_ENTRY_SIZE(myattr);
48601a0f853SOlivier Houchard 			if (myattr->last_in_suf) {
48701a0f853SOlivier Houchard 				/*
48801a0f853SOlivier Houchard 				 * Point the offset to the start of this
48901a0f853SOlivier Houchard 				 * record's CE area
49001a0f853SOlivier Houchard 				 */
4911631d42aSEd Maste 				if (fseeko(fd, ((off_t)diskStructure->
49201a0f853SOlivier Houchard 				    susp_continuation_area_start_sector *
4931631d42aSEd Maste 				    diskStructure->sectorSize)
49401a0f853SOlivier Houchard 				    + writenode->susp_entry_ce_start,
49501a0f853SOlivier Houchard 				    SEEK_SET) == -1)
49601a0f853SOlivier Houchard 					err(1, "fseeko");
49701a0f853SOlivier Houchard 				in_ca = 1;
49801a0f853SOlivier Houchard 			}
49901a0f853SOlivier Houchard 		}
50001a0f853SOlivier Houchard 	}
50101a0f853SOlivier Houchard 
50201a0f853SOlivier Houchard 	/*
50301a0f853SOlivier Houchard 	 * If we had to go to the continuation area, head back to
50401a0f853SOlivier Houchard 	 * where we should be.
50501a0f853SOlivier Houchard 	 */
50601a0f853SOlivier Houchard 	if (in_ca)
5071631d42aSEd Maste 		if (fseeko(fd, sector * diskStructure->sectorSize + offset,
50801a0f853SOlivier Houchard 		    SEEK_SET) == -1)
50901a0f853SOlivier Houchard 			err(1, "fseeko");
51001a0f853SOlivier Houchard }
511