xref: /freebsd/usr.sbin/makefs/cd9660/cd9660_write.c (revision b61a573019428cbdc38ed03be9ecd2482011ea66)
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>
4101a0f853SOlivier Houchard __FBSDID("$FreeBSD$");
4201a0f853SOlivier Houchard 
435f5598b1SEd Maste #include <util.h>
445f5598b1SEd Maste 
451631d42aSEd Maste static int cd9660_write_volume_descriptors(iso9660_disk *, FILE *);
461631d42aSEd Maste static int cd9660_write_path_table(iso9660_disk *, FILE *, off_t, int);
471631d42aSEd Maste static int cd9660_write_path_tables(iso9660_disk *, FILE *);
481631d42aSEd Maste static int cd9660_write_file(iso9660_disk *, FILE *, cd9660node *);
491631d42aSEd Maste static int cd9660_write_filedata(iso9660_disk *, FILE *, off_t,
501631d42aSEd Maste     const unsigned char *, int);
5101a0f853SOlivier Houchard #if 0
5201a0f853SOlivier Houchard static int cd9660_write_buffered(FILE *, off_t, int, const unsigned char *);
5301a0f853SOlivier Houchard #endif
541631d42aSEd Maste static void cd9660_write_rr(iso9660_disk *, FILE *, cd9660node *, off_t, off_t);
5501a0f853SOlivier Houchard 
5601a0f853SOlivier Houchard /*
5701a0f853SOlivier Houchard  * Write the image
5801a0f853SOlivier Houchard  * Writes the entire image
5901a0f853SOlivier Houchard  * @param const char* The filename for the image
6001a0f853SOlivier Houchard  * @returns int 1 on success, 0 on failure
6101a0f853SOlivier Houchard  */
6201a0f853SOlivier Houchard int
631631d42aSEd Maste cd9660_write_image(iso9660_disk *diskStructure, const char* image)
6401a0f853SOlivier Houchard {
6501a0f853SOlivier Houchard 	FILE *fd;
6601a0f853SOlivier Houchard 	int status;
67fe41c64bSAlex Richardson 	unsigned char buf[CD9660_SECTOR_SIZE];
6801a0f853SOlivier Houchard 
6901a0f853SOlivier Houchard 	if ((fd = fopen(image, "w+")) == NULL) {
7001a0f853SOlivier Houchard 		err(EXIT_FAILURE, "%s: Can't open `%s' for writing", __func__,
7101a0f853SOlivier Houchard 		    image);
7201a0f853SOlivier Houchard 	}
7301a0f853SOlivier Houchard 
741631d42aSEd Maste 	if (diskStructure->verbose_level > 0)
7501a0f853SOlivier Houchard 		printf("Writing image\n");
7601a0f853SOlivier Houchard 
771631d42aSEd Maste 	if (diskStructure->has_generic_bootimage) {
781631d42aSEd Maste 		status = cd9660_copy_file(diskStructure, fd, 0,
791631d42aSEd Maste 		    diskStructure->generic_bootimage);
8001a0f853SOlivier Houchard 		if (status == 0) {
8101a0f853SOlivier Houchard 			warnx("%s: Error writing generic boot image",
8201a0f853SOlivier Houchard 			    __func__);
8301a0f853SOlivier Houchard 			goto cleanup_bad_image;
8401a0f853SOlivier Houchard 		}
8501a0f853SOlivier Houchard 	}
8601a0f853SOlivier Houchard 
8701a0f853SOlivier Houchard 	/* Write the volume descriptors */
881631d42aSEd Maste 	status = cd9660_write_volume_descriptors(diskStructure, fd);
8901a0f853SOlivier Houchard 	if (status == 0) {
9001a0f853SOlivier Houchard 		warnx("%s: Error writing volume descriptors to image",
9101a0f853SOlivier Houchard 		    __func__);
9201a0f853SOlivier Houchard 		goto cleanup_bad_image;
9301a0f853SOlivier Houchard 	}
9401a0f853SOlivier Houchard 
951631d42aSEd Maste 	if (diskStructure->verbose_level > 0)
9601a0f853SOlivier Houchard 		printf("Volume descriptors written\n");
9701a0f853SOlivier Houchard 
9801a0f853SOlivier Houchard 	/*
9901a0f853SOlivier Houchard 	 * Write the path tables: there are actually four, but right
100164fa411SGordon Bergling 	 * now we are only concerned with two.
10101a0f853SOlivier Houchard 	 */
1021631d42aSEd Maste 	status = cd9660_write_path_tables(diskStructure, fd);
10301a0f853SOlivier Houchard 	if (status == 0) {
10401a0f853SOlivier Houchard 		warnx("%s: Error writing path tables to image", __func__);
10501a0f853SOlivier Houchard 		goto cleanup_bad_image;
10601a0f853SOlivier Houchard 	}
10701a0f853SOlivier Houchard 
1081631d42aSEd Maste 	if (diskStructure->verbose_level > 0)
10901a0f853SOlivier Houchard 		printf("Path tables written\n");
11001a0f853SOlivier Houchard 
11101a0f853SOlivier Houchard 	/* Write the directories and files */
1121631d42aSEd Maste 	status = cd9660_write_file(diskStructure, fd, diskStructure->rootNode);
11301a0f853SOlivier Houchard 	if (status == 0) {
11401a0f853SOlivier Houchard 		warnx("%s: Error writing files to image", __func__);
11501a0f853SOlivier Houchard 		goto cleanup_bad_image;
11601a0f853SOlivier Houchard 	}
11701a0f853SOlivier Houchard 
1181631d42aSEd Maste 	if (diskStructure->is_bootable) {
1191631d42aSEd Maste 		cd9660_write_boot(diskStructure, fd);
12001a0f853SOlivier Houchard 	}
12101a0f853SOlivier Houchard 
12201a0f853SOlivier Houchard 	/* Write padding bits. This is temporary */
12301a0f853SOlivier Houchard 	memset(buf, 0, CD9660_SECTOR_SIZE);
1241631d42aSEd Maste 	cd9660_write_filedata(diskStructure, fd,
1251631d42aSEd Maste 	    diskStructure->totalSectors - 1, buf, 1);
12601a0f853SOlivier Houchard 
1271631d42aSEd Maste 	if (diskStructure->verbose_level > 0)
12801a0f853SOlivier Houchard 		printf("Files written\n");
12901a0f853SOlivier Houchard 	fclose(fd);
13001a0f853SOlivier Houchard 
1311631d42aSEd Maste 	if (diskStructure->verbose_level > 0)
13201a0f853SOlivier Houchard 		printf("Image closed\n");
13301a0f853SOlivier Houchard 	return 1;
13401a0f853SOlivier Houchard 
13501a0f853SOlivier Houchard cleanup_bad_image:
13601a0f853SOlivier Houchard 	fclose(fd);
1371631d42aSEd Maste 	if (!diskStructure->keep_bad_images)
13801a0f853SOlivier Houchard 		unlink(image);
1391631d42aSEd Maste 	if (diskStructure->verbose_level > 0)
14001a0f853SOlivier Houchard 		printf("Bad image cleaned up\n");
14101a0f853SOlivier Houchard 	return 0;
14201a0f853SOlivier Houchard }
14301a0f853SOlivier Houchard 
14401a0f853SOlivier Houchard static int
1451631d42aSEd Maste cd9660_write_volume_descriptors(iso9660_disk *diskStructure, FILE *fd)
14601a0f853SOlivier Houchard {
1471631d42aSEd Maste 	volume_descriptor *vd_temp = diskStructure->firstVolumeDescriptor;
14801a0f853SOlivier Houchard 
14901a0f853SOlivier Houchard 	while (vd_temp != NULL) {
1501631d42aSEd Maste 		cd9660_write_filedata(diskStructure, fd, vd_temp->sector,
15101a0f853SOlivier Houchard 		    vd_temp->volumeDescriptorData, 1);
15201a0f853SOlivier Houchard 		vd_temp = vd_temp->next;
15301a0f853SOlivier Houchard 	}
15401a0f853SOlivier Houchard 	return 1;
15501a0f853SOlivier Houchard }
15601a0f853SOlivier Houchard 
15701a0f853SOlivier Houchard /*
15801a0f853SOlivier Houchard  * Write out an individual path table
15901a0f853SOlivier Houchard  * Used just to keep redundant code to a minimum
16001a0f853SOlivier Houchard  * @param FILE *fd Valid file pointer
16101a0f853SOlivier Houchard  * @param int Sector to start writing path table to
16201a0f853SOlivier Houchard  * @param int Endian mode : BIG_ENDIAN or LITTLE_ENDIAN
16301a0f853SOlivier Houchard  * @returns int 1 on success, 0 on failure
16401a0f853SOlivier Houchard  */
16501a0f853SOlivier Houchard static int
1661631d42aSEd Maste cd9660_write_path_table(iso9660_disk *diskStructure, FILE *fd, off_t sector,
1671631d42aSEd Maste     int mode)
16801a0f853SOlivier Houchard {
1691631d42aSEd Maste 	int path_table_sectors = CD9660_BLOCKS(diskStructure->sectorSize,
1701631d42aSEd Maste 	    diskStructure->pathTableLength);
17101a0f853SOlivier Houchard 	unsigned char *buffer;
17201a0f853SOlivier Houchard 	unsigned char *buffer_head;
173b73321f0SEnji Cooper 	int len, ret;
17401a0f853SOlivier Houchard 	path_table_entry temp_entry;
17501a0f853SOlivier Houchard 	cd9660node *ptcur;
17601a0f853SOlivier Houchard 
1775f5598b1SEd Maste 	buffer = ecalloc(path_table_sectors, diskStructure->sectorSize);
17801a0f853SOlivier Houchard 	buffer_head = buffer;
17901a0f853SOlivier Houchard 
1801631d42aSEd Maste 	ptcur = diskStructure->rootNode;
18101a0f853SOlivier Houchard 
18201a0f853SOlivier Houchard 	while (ptcur != NULL) {
18301a0f853SOlivier Houchard 		memset(&temp_entry, 0, sizeof(path_table_entry));
18401a0f853SOlivier Houchard 		temp_entry.length[0] = ptcur->isoDirRecord->name_len[0];
18501a0f853SOlivier Houchard 		temp_entry.extended_attribute_length[0] =
18601a0f853SOlivier Houchard 		    ptcur->isoDirRecord->ext_attr_length[0];
18701a0f853SOlivier Houchard 		memcpy(temp_entry.name, ptcur->isoDirRecord->name,
18801a0f853SOlivier Houchard 		    temp_entry.length[0] + 1);
18901a0f853SOlivier Houchard 
19001a0f853SOlivier Houchard 		/* round up */
19101a0f853SOlivier Houchard 		len = temp_entry.length[0] + 8 + (temp_entry.length[0] & 0x01);
19201a0f853SOlivier Houchard 
19301a0f853SOlivier Houchard                 /* todo: function pointers instead */
19401a0f853SOlivier Houchard 		if (mode == LITTLE_ENDIAN) {
19501a0f853SOlivier Houchard 			cd9660_731(ptcur->fileDataSector,
19601a0f853SOlivier Houchard 			    temp_entry.first_sector);
19701a0f853SOlivier Houchard 			cd9660_721((ptcur->parent == NULL ?
19801a0f853SOlivier Houchard 				1 : ptcur->parent->ptnumber),
19901a0f853SOlivier Houchard 			    temp_entry.parent_number);
20001a0f853SOlivier Houchard 		} else {
20101a0f853SOlivier Houchard 			cd9660_732(ptcur->fileDataSector,
20201a0f853SOlivier Houchard 			    temp_entry.first_sector);
20301a0f853SOlivier Houchard 			cd9660_722((ptcur->parent == NULL ?
20401a0f853SOlivier Houchard 				1 : ptcur->parent->ptnumber),
20501a0f853SOlivier Houchard 			    temp_entry.parent_number);
20601a0f853SOlivier Houchard 		}
20701a0f853SOlivier Houchard 
20801a0f853SOlivier Houchard 
20901a0f853SOlivier Houchard 		memcpy(buffer, &temp_entry, len);
21001a0f853SOlivier Houchard 		buffer += len;
21101a0f853SOlivier Houchard 
21201a0f853SOlivier Houchard 		ptcur = ptcur->ptnext;
21301a0f853SOlivier Houchard 	}
21401a0f853SOlivier Houchard 
2151631d42aSEd Maste 	ret = cd9660_write_filedata(diskStructure, fd, sector, buffer_head,
21601a0f853SOlivier Houchard 	    path_table_sectors);
21742afd55dSEnji Cooper 	free(buffer_head);
218b73321f0SEnji Cooper 	return ret;
21901a0f853SOlivier Houchard }
22001a0f853SOlivier Houchard 
22101a0f853SOlivier Houchard 
22201a0f853SOlivier Houchard /*
22301a0f853SOlivier Houchard  * Write out the path tables to disk
22401a0f853SOlivier Houchard  * Each file descriptor should be pointed to by the PVD, so we know which
22501a0f853SOlivier Houchard  * sector to copy them to. One thing to watch out for: the only path tables
22601a0f853SOlivier Houchard  * stored are in the endian mode that the application is compiled for. So,
22701a0f853SOlivier Houchard  * the first thing to do is write out that path table, then to write the one
22801a0f853SOlivier Houchard  * in the other endian mode requires to convert the endianness of each entry
22901a0f853SOlivier Houchard  * in the table. The best way to do this would be to create a temporary
23001a0f853SOlivier Houchard  * path_table_entry structure, then for each path table entry, copy it to
23101a0f853SOlivier Houchard  * the temporary entry, translate, then copy that to disk.
23201a0f853SOlivier Houchard  *
23301a0f853SOlivier Houchard  * @param FILE* Valid file descriptor
23401a0f853SOlivier Houchard  * @returns int 0 on failure, 1 on success
23501a0f853SOlivier Houchard  */
23601a0f853SOlivier Houchard static int
2371631d42aSEd Maste cd9660_write_path_tables(iso9660_disk *diskStructure, FILE *fd)
23801a0f853SOlivier Houchard {
2391631d42aSEd Maste 	if (cd9660_write_path_table(diskStructure, fd,
2401631d42aSEd Maste 	    diskStructure->primaryLittleEndianTableSector, LITTLE_ENDIAN) == 0)
24101a0f853SOlivier Houchard 		return 0;
24201a0f853SOlivier Houchard 
2431631d42aSEd Maste 	if (cd9660_write_path_table(diskStructure, fd,
2441631d42aSEd Maste 	    diskStructure->primaryBigEndianTableSector, BIG_ENDIAN) == 0)
24501a0f853SOlivier Houchard 		return 0;
24601a0f853SOlivier Houchard 
24701a0f853SOlivier Houchard 	/* @TODO: handle remaining two path tables */
24801a0f853SOlivier Houchard 	return 1;
24901a0f853SOlivier Houchard }
25001a0f853SOlivier Houchard 
25101a0f853SOlivier Houchard /*
25201a0f853SOlivier Houchard  * Write a file to disk
25301a0f853SOlivier Houchard  * Writes a file, its directory record, and its data to disk
25401a0f853SOlivier Houchard  * This file is designed to be called RECURSIVELY, so initially call it
25501a0f853SOlivier Houchard  * with the root node. All of the records should store what sector the
25601a0f853SOlivier Houchard  * file goes in, so no computation should be  necessary.
25701a0f853SOlivier Houchard  *
25801a0f853SOlivier Houchard  * @param int fd Valid file descriptor
25901a0f853SOlivier Houchard  * @param struct cd9660node* writenode Pointer to the file to be written
26001a0f853SOlivier Houchard  * @returns int 0 on failure, 1 on success
26101a0f853SOlivier Houchard  */
26201a0f853SOlivier Houchard static int
2631631d42aSEd Maste cd9660_write_file(iso9660_disk *diskStructure, FILE *fd, cd9660node *writenode)
26401a0f853SOlivier Houchard {
26501a0f853SOlivier Houchard 	char *buf;
26601a0f853SOlivier Houchard 	char *temp_file_name;
26701a0f853SOlivier Houchard 	int ret;
26801a0f853SOlivier Houchard 	off_t working_sector;
26901a0f853SOlivier Houchard 	int cur_sector_offset;
27001a0f853SOlivier Houchard 	iso_directory_record_cd9660 temp_record;
27101a0f853SOlivier Houchard 	cd9660node *temp;
27201a0f853SOlivier Houchard 	int rv = 0;
27301a0f853SOlivier Houchard 
27401a0f853SOlivier Houchard 	/* Todo : clean up variables */
27501a0f853SOlivier Houchard 
2762f11df63SEd Maste 	temp_file_name = ecalloc(PATH_MAX, 1);
2775f5598b1SEd Maste 	buf = emalloc(diskStructure->sectorSize);
27801a0f853SOlivier Houchard 	if ((writenode->level != 0) &&
27901a0f853SOlivier Houchard 	    !(writenode->node->type & S_IFDIR)) {
28001a0f853SOlivier Houchard 		fsinode *inode = writenode->node->inode;
28101a0f853SOlivier Houchard 		/* Only attempt to write unwritten files that have length. */
28201a0f853SOlivier Houchard 		if ((inode->flags & FI_WRITTEN) != 0) {
28301a0f853SOlivier Houchard 			INODE_WARNX(("%s: skipping written inode %d", __func__,
28401a0f853SOlivier Houchard 			    (int)inode->st.st_ino));
28501a0f853SOlivier Houchard 		} else if (writenode->fileDataLength > 0) {
28601a0f853SOlivier Houchard 			INODE_WARNX(("%s: writing inode %d blocks at %" PRIu32,
28701a0f853SOlivier Houchard 			    __func__, (int)inode->st.st_ino, inode->ino));
28801a0f853SOlivier Houchard 			inode->flags |= FI_WRITTEN;
289484b5c25SMarcel Moolenaar 			if (writenode->node->contents == NULL)
29001a0f853SOlivier Houchard 				cd9660_compute_full_filename(writenode,
291688aaa09SJung-uk Kim 				    temp_file_name);
2921631d42aSEd Maste 			ret = cd9660_copy_file(diskStructure, fd,
2931631d42aSEd Maste 			    writenode->fileDataSector,
294484b5c25SMarcel Moolenaar 			    (writenode->node->contents != NULL) ?
295484b5c25SMarcel Moolenaar 			    writenode->node->contents : temp_file_name);
29601a0f853SOlivier Houchard 			if (ret == 0)
29701a0f853SOlivier Houchard 				goto out;
29801a0f853SOlivier Houchard 		}
29901a0f853SOlivier Houchard 	} else {
30001a0f853SOlivier Houchard 		/*
3013df5ecacSUlrich Spörlein 		 * Here is a new revelation that ECMA didn't explain
30201a0f853SOlivier Houchard 		 * (at least not well).
30301a0f853SOlivier Houchard 		 * ALL . and .. records store the name "\0" and "\1"
3043df5ecacSUlrich Spörlein 		 * respectively. So, for each directory, we have to
30501a0f853SOlivier Houchard 		 * make a new node.
30601a0f853SOlivier Houchard 		 *
30701a0f853SOlivier Houchard 		 * This is where it gets kinda messy, since we have to
30801a0f853SOlivier Houchard 		 * be careful of sector boundaries
30901a0f853SOlivier Houchard 		 */
31001a0f853SOlivier Houchard 		cur_sector_offset = 0;
31101a0f853SOlivier Houchard 		working_sector = writenode->fileDataSector;
3121631d42aSEd Maste 		if (fseeko(fd, working_sector * diskStructure->sectorSize,
31301a0f853SOlivier Houchard 		    SEEK_SET) == -1)
31401a0f853SOlivier Houchard 			err(1, "fseeko");
31501a0f853SOlivier Houchard 
31601a0f853SOlivier Houchard 		/*
31701a0f853SOlivier Houchard 		 * Now loop over children, writing out their directory
31801a0f853SOlivier Houchard 		 * records - beware of sector boundaries
31901a0f853SOlivier Houchard 		 */
32001a0f853SOlivier Houchard 		TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) {
32101a0f853SOlivier Houchard 			/*
32201a0f853SOlivier Houchard 			 * Copy the temporary record and adjust its size
32301a0f853SOlivier Houchard 			 * if necessary
32401a0f853SOlivier Houchard 			 */
32501a0f853SOlivier Houchard 			memcpy(&temp_record, temp->isoDirRecord,
32601a0f853SOlivier Houchard 			    sizeof(iso_directory_record_cd9660));
32701a0f853SOlivier Houchard 
32801a0f853SOlivier Houchard 			temp_record.length[0] =
3291631d42aSEd Maste 			    cd9660_compute_record_size(diskStructure, temp);
33001a0f853SOlivier Houchard 
33101a0f853SOlivier Houchard 			if (temp_record.length[0] + cur_sector_offset >=
3321631d42aSEd Maste 			    diskStructure->sectorSize) {
33301a0f853SOlivier Houchard 				cur_sector_offset = 0;
33401a0f853SOlivier Houchard 				working_sector++;
33501a0f853SOlivier Houchard 
33601a0f853SOlivier Houchard 				/* Seek to the next sector. */
33701a0f853SOlivier Houchard 				if (fseeko(fd, working_sector *
3381631d42aSEd Maste 				    diskStructure->sectorSize, SEEK_SET) == -1)
33901a0f853SOlivier Houchard 					err(1, "fseeko");
34001a0f853SOlivier Houchard 			}
34101a0f853SOlivier Houchard 			/* Write out the basic ISO directory record */
34237ca9f42SEd Maste 			(void)fwrite(&temp_record, 1,
34301a0f853SOlivier Houchard 			    temp->isoDirRecord->length[0], fd);
3441631d42aSEd Maste 			if (diskStructure->rock_ridge_enabled) {
3451631d42aSEd Maste 				cd9660_write_rr(diskStructure, fd, temp,
34601a0f853SOlivier Houchard 				    cur_sector_offset, working_sector);
34701a0f853SOlivier Houchard 			}
34801a0f853SOlivier Houchard 			if (fseeko(fd, working_sector *
3491631d42aSEd Maste 			    diskStructure->sectorSize + cur_sector_offset +
35001a0f853SOlivier Houchard 			    temp_record.length[0] - temp->su_tail_size,
35101a0f853SOlivier Houchard 			    SEEK_SET) == -1)
35201a0f853SOlivier Houchard 				err(1, "fseeko");
35301a0f853SOlivier Houchard 			if (temp->su_tail_size > 0)
35401a0f853SOlivier Houchard 				fwrite(temp->su_tail_data, 1,
35501a0f853SOlivier Houchard 				    temp->su_tail_size, fd);
35601a0f853SOlivier Houchard 			if (ferror(fd)) {
35701a0f853SOlivier Houchard 				warnx("%s: write error", __func__);
35801a0f853SOlivier Houchard 				goto out;
35901a0f853SOlivier Houchard 			}
36001a0f853SOlivier Houchard 			cur_sector_offset += temp_record.length[0];
36101a0f853SOlivier Houchard 
36201a0f853SOlivier Houchard 		}
36301a0f853SOlivier Houchard 
36401a0f853SOlivier Houchard 		/*
36501a0f853SOlivier Houchard 		 * Recurse on children.
36601a0f853SOlivier Houchard 		 */
36701a0f853SOlivier Houchard 		TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) {
3681631d42aSEd Maste 			if ((ret = cd9660_write_file(diskStructure, fd, temp)) == 0)
36901a0f853SOlivier Houchard 				goto out;
37001a0f853SOlivier Houchard 		}
37101a0f853SOlivier Houchard 	}
37201a0f853SOlivier Houchard 	rv = 1;
37301a0f853SOlivier Houchard out:
37401a0f853SOlivier Houchard 	free(temp_file_name);
37501a0f853SOlivier Houchard 	free(buf);
37601a0f853SOlivier Houchard 	return rv;
37701a0f853SOlivier Houchard }
37801a0f853SOlivier Houchard 
37901a0f853SOlivier Houchard /*
38001a0f853SOlivier Houchard  * Wrapper function to write a buffer (one sector) to disk.
38101a0f853SOlivier Houchard  * Seeks and writes the buffer.
38201a0f853SOlivier Houchard  * NOTE: You dont NEED to use this function, but it might make your
38301a0f853SOlivier Houchard  * life easier if you have to write things that align to a sector
38401a0f853SOlivier Houchard  * (such as volume descriptors).
38501a0f853SOlivier Houchard  *
38601a0f853SOlivier Houchard  * @param int fd Valid file descriptor
38701a0f853SOlivier Houchard  * @param int sector Sector number to write to
38801a0f853SOlivier Houchard  * @param const unsigned char* Buffer to write. This should be the
38901a0f853SOlivier Houchard  *                             size of a sector, and if only a portion
39001a0f853SOlivier Houchard  *                             is written, the rest should be set to 0.
39101a0f853SOlivier Houchard  */
39201a0f853SOlivier Houchard static int
3931631d42aSEd Maste cd9660_write_filedata(iso9660_disk *diskStructure, FILE *fd, off_t sector,
3941631d42aSEd Maste     const unsigned char *buf, int numsecs)
39501a0f853SOlivier Houchard {
39601a0f853SOlivier Houchard 	off_t curpos;
39701a0f853SOlivier Houchard 	size_t success;
39801a0f853SOlivier Houchard 
39901a0f853SOlivier Houchard 	curpos = ftello(fd);
40001a0f853SOlivier Houchard 
4011631d42aSEd Maste 	if (fseeko(fd, sector * diskStructure->sectorSize, SEEK_SET) == -1)
40201a0f853SOlivier Houchard 		err(1, "fseeko");
40301a0f853SOlivier Houchard 
4041631d42aSEd Maste 	success = fwrite(buf, diskStructure->sectorSize * numsecs, 1, fd);
40501a0f853SOlivier Houchard 
40601a0f853SOlivier Houchard 	if (fseeko(fd, curpos, SEEK_SET) == -1)
40701a0f853SOlivier Houchard 		err(1, "fseeko");
40801a0f853SOlivier Houchard 
40901a0f853SOlivier Houchard 	if (success == 1)
4101631d42aSEd Maste 		success = diskStructure->sectorSize * numsecs;
41101a0f853SOlivier Houchard 	return success;
41201a0f853SOlivier Houchard }
41301a0f853SOlivier Houchard 
41401a0f853SOlivier Houchard #if 0
41501a0f853SOlivier Houchard static int
41601a0f853SOlivier Houchard cd9660_write_buffered(FILE *fd, off_t offset, int buff_len,
41701a0f853SOlivier Houchard 		      const unsigned char* buffer)
41801a0f853SOlivier Houchard {
41901a0f853SOlivier Houchard 	static int working_sector = -1;
42001a0f853SOlivier Houchard 	static char buf[CD9660_SECTOR_SIZE];
42101a0f853SOlivier Houchard 
42201a0f853SOlivier Houchard 	return 0;
42301a0f853SOlivier Houchard }
42401a0f853SOlivier Houchard #endif
42501a0f853SOlivier Houchard 
42601a0f853SOlivier Houchard int
4271631d42aSEd Maste cd9660_copy_file(iso9660_disk *diskStructure, FILE *fd, off_t start_sector,
4281631d42aSEd Maste     const char *filename)
42901a0f853SOlivier Houchard {
43001a0f853SOlivier Houchard 	FILE *rf;
43101a0f853SOlivier Houchard 	int bytes_read;
4321631d42aSEd Maste 	int buf_size = diskStructure->sectorSize;
43301a0f853SOlivier Houchard 	char *buf;
43401a0f853SOlivier Houchard 
4355f5598b1SEd Maste 	buf = emalloc(buf_size);
43601a0f853SOlivier Houchard 	if ((rf = fopen(filename, "rb")) == NULL) {
43701a0f853SOlivier Houchard 		warn("%s: cannot open %s", __func__, filename);
43801a0f853SOlivier Houchard 		free(buf);
43901a0f853SOlivier Houchard 		return 0;
44001a0f853SOlivier Houchard 	}
44101a0f853SOlivier Houchard 
4421631d42aSEd Maste 	if (diskStructure->verbose_level > 1)
44301a0f853SOlivier Houchard 		printf("Writing file: %s\n",filename);
44401a0f853SOlivier Houchard 
4451631d42aSEd Maste 	if (fseeko(fd, start_sector * diskStructure->sectorSize, SEEK_SET) == -1)
44601a0f853SOlivier Houchard 		err(1, "fseeko");
44701a0f853SOlivier Houchard 
44801a0f853SOlivier Houchard 	while (!feof(rf)) {
44901a0f853SOlivier Houchard 		bytes_read = fread(buf,1,buf_size,rf);
45001a0f853SOlivier Houchard 		if (ferror(rf)) {
45101a0f853SOlivier Houchard 			warn("%s: fread", __func__);
45201a0f853SOlivier Houchard 			free(buf);
453852f933dSMarius Strobl 			(void)fclose(rf);
45401a0f853SOlivier Houchard 			return 0;
45501a0f853SOlivier Houchard 		}
45601a0f853SOlivier Houchard 
45701a0f853SOlivier Houchard 		fwrite(buf,1,bytes_read,fd);
45801a0f853SOlivier Houchard 		if (ferror(fd)) {
45901a0f853SOlivier Houchard 			warn("%s: fwrite", __func__);
46001a0f853SOlivier Houchard 			free(buf);
461852f933dSMarius Strobl 			(void)fclose(rf);
46201a0f853SOlivier Houchard 			return 0;
46301a0f853SOlivier Houchard 		}
46401a0f853SOlivier Houchard 	}
46501a0f853SOlivier Houchard 
46601a0f853SOlivier Houchard 	fclose(rf);
46701a0f853SOlivier Houchard 	free(buf);
46801a0f853SOlivier Houchard 	return 1;
46901a0f853SOlivier Houchard }
47001a0f853SOlivier Houchard 
47101a0f853SOlivier Houchard static void
4721631d42aSEd Maste cd9660_write_rr(iso9660_disk *diskStructure, FILE *fd, cd9660node *writenode,
4731631d42aSEd Maste     off_t offset, off_t sector)
47401a0f853SOlivier Houchard {
47501a0f853SOlivier Houchard 	int in_ca = 0;
47601a0f853SOlivier Houchard 	struct ISO_SUSP_ATTRIBUTES *myattr;
47701a0f853SOlivier Houchard 
47801a0f853SOlivier Houchard 	offset += writenode->isoDirRecord->length[0];
4791631d42aSEd Maste 	if (fseeko(fd, sector * diskStructure->sectorSize + offset, SEEK_SET) ==
48001a0f853SOlivier Houchard 	    -1)
48101a0f853SOlivier Houchard 		err(1, "fseeko");
48201a0f853SOlivier Houchard 	/* Offset now points at the end of the record */
48301a0f853SOlivier Houchard 	TAILQ_FOREACH(myattr, &writenode->head, rr_ll) {
48401a0f853SOlivier Houchard 		fwrite(&(myattr->attr), CD9660_SUSP_ENTRY_SIZE(myattr), 1, fd);
48501a0f853SOlivier Houchard 
48601a0f853SOlivier Houchard 		if (!in_ca) {
48701a0f853SOlivier Houchard 			offset += CD9660_SUSP_ENTRY_SIZE(myattr);
48801a0f853SOlivier Houchard 			if (myattr->last_in_suf) {
48901a0f853SOlivier Houchard 				/*
49001a0f853SOlivier Houchard 				 * Point the offset to the start of this
49101a0f853SOlivier Houchard 				 * record's CE area
49201a0f853SOlivier Houchard 				 */
4931631d42aSEd Maste 				if (fseeko(fd, ((off_t)diskStructure->
49401a0f853SOlivier Houchard 				    susp_continuation_area_start_sector *
4951631d42aSEd Maste 				    diskStructure->sectorSize)
49601a0f853SOlivier Houchard 				    + writenode->susp_entry_ce_start,
49701a0f853SOlivier Houchard 				    SEEK_SET) == -1)
49801a0f853SOlivier Houchard 					err(1, "fseeko");
49901a0f853SOlivier Houchard 				in_ca = 1;
50001a0f853SOlivier Houchard 			}
50101a0f853SOlivier Houchard 		}
50201a0f853SOlivier Houchard 	}
50301a0f853SOlivier Houchard 
50401a0f853SOlivier Houchard 	/*
50501a0f853SOlivier Houchard 	 * If we had to go to the continuation area, head back to
50601a0f853SOlivier Houchard 	 * where we should be.
50701a0f853SOlivier Houchard 	 */
50801a0f853SOlivier Houchard 	if (in_ca)
5091631d42aSEd Maste 		if (fseeko(fd, sector * diskStructure->sectorSize + offset,
51001a0f853SOlivier Houchard 		    SEEK_SET) == -1)
51101a0f853SOlivier Houchard 			err(1, "fseeko");
51201a0f853SOlivier Houchard }
513