1224762Smarius/*	$NetBSD: cd9660_write.c,v 1.14 2011/01/04 09:48:21 wiz Exp $	*/
2214921Scognet
3214921Scognet/*
4214921Scognet * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
5214921Scognet * Perez-Rathke and Ram Vedam.  All rights reserved.
6214921Scognet *
7214921Scognet * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
8214921Scognet * Alan Perez-Rathke and Ram Vedam.
9214921Scognet *
10214921Scognet * Redistribution and use in source and binary forms, with or
11214921Scognet * without modification, are permitted provided that the following
12214921Scognet * conditions are met:
13214921Scognet * 1. Redistributions of source code must retain the above copyright
14214921Scognet *    notice, this list of conditions and the following disclaimer.
15214921Scognet * 2. Redistributions in binary form must reproduce the above
16214921Scognet *    copyright notice, this list of conditions and the following
17214921Scognet *    disclaimer in the documentation and/or other materials provided
18214921Scognet *    with the distribution.
19214921Scognet *
20214921Scognet * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
21214921Scognet * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
22214921Scognet * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23214921Scognet * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24214921Scognet * DISCLAIMED.  IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
25214921Scognet * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
26214921Scognet * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27214921Scognet * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28214921Scognet * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29214921Scognet * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30214921Scognet * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31214921Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32214921Scognet * OF SUCH DAMAGE.
33214921Scognet */
34214921Scognet
35214921Scognet#include "cd9660.h"
36214921Scognet#include "iso9660_rrip.h"
37214921Scognet
38214921Scognet#include <sys/cdefs.h>
39214921Scognet__FBSDID("$FreeBSD$");
40214921Scognet
41214921Scognetstatic int cd9660_write_volume_descriptors(FILE *);
42214921Scognetstatic int cd9660_write_path_table(FILE *, off_t, int);
43214921Scognetstatic int cd9660_write_path_tables(FILE *);
44214921Scognetstatic int cd9660_write_file(FILE *, cd9660node *);
45214921Scognetstatic int cd9660_write_filedata(FILE *, off_t, const unsigned char *, int);
46214921Scognet#if 0
47214921Scognetstatic int cd9660_write_buffered(FILE *, off_t, int, const unsigned char *);
48214921Scognet#endif
49214921Scognetstatic void cd9660_write_rr(FILE *, cd9660node *, off_t, off_t);
50214921Scognet
51214921Scognet/*
52214921Scognet * Write the image
53214921Scognet * Writes the entire image
54214921Scognet * @param const char* The filename for the image
55214921Scognet * @returns int 1 on success, 0 on failure
56214921Scognet */
57214921Scognetint
58214921Scognetcd9660_write_image(const char* image)
59214921Scognet{
60214921Scognet	FILE *fd;
61214921Scognet	int status;
62214921Scognet	char buf[CD9660_SECTOR_SIZE];
63214921Scognet
64214921Scognet	if ((fd = fopen(image, "w+")) == NULL) {
65214921Scognet		err(EXIT_FAILURE, "%s: Can't open `%s' for writing", __func__,
66214921Scognet		    image);
67214921Scognet	}
68214921Scognet
69214921Scognet	if (diskStructure.verbose_level > 0)
70214921Scognet		printf("Writing image\n");
71214921Scognet
72214921Scognet	if (diskStructure.has_generic_bootimage) {
73214921Scognet		status = cd9660_copy_file(fd, 0,
74214921Scognet		    diskStructure.generic_bootimage);
75214921Scognet		if (status == 0) {
76214921Scognet			warnx("%s: Error writing generic boot image",
77214921Scognet			    __func__);
78214921Scognet			goto cleanup_bad_image;
79214921Scognet		}
80214921Scognet	}
81214921Scognet
82214921Scognet	/* Write the volume descriptors */
83214921Scognet	status = cd9660_write_volume_descriptors(fd);
84214921Scognet	if (status == 0) {
85214921Scognet		warnx("%s: Error writing volume descriptors to image",
86214921Scognet		    __func__);
87214921Scognet		goto cleanup_bad_image;
88214921Scognet	}
89214921Scognet
90214921Scognet	if (diskStructure.verbose_level > 0)
91214921Scognet		printf("Volume descriptors written\n");
92214921Scognet
93214921Scognet	/*
94214921Scognet	 * Write the path tables: there are actually four, but right
95214921Scognet	 * now we are only concearned with two.
96214921Scognet	 */
97214921Scognet	status = cd9660_write_path_tables(fd);
98214921Scognet	if (status == 0) {
99214921Scognet		warnx("%s: Error writing path tables to image", __func__);
100214921Scognet		goto cleanup_bad_image;
101214921Scognet	}
102214921Scognet
103214921Scognet	if (diskStructure.verbose_level > 0)
104214921Scognet		printf("Path tables written\n");
105214921Scognet
106214921Scognet	/* Write the directories and files */
107214921Scognet	status = cd9660_write_file(fd, diskStructure.rootNode);
108214921Scognet	if (status == 0) {
109214921Scognet		warnx("%s: Error writing files to image", __func__);
110214921Scognet		goto cleanup_bad_image;
111214921Scognet	}
112214921Scognet
113214921Scognet	if (diskStructure.is_bootable) {
114214921Scognet		cd9660_write_boot(fd);
115214921Scognet	}
116214921Scognet
117214921Scognet	/* Write padding bits. This is temporary */
118214921Scognet	memset(buf, 0, CD9660_SECTOR_SIZE);
119214921Scognet	cd9660_write_filedata(fd, diskStructure.totalSectors - 1, buf, 1);
120214921Scognet
121214921Scognet	if (diskStructure.verbose_level > 0)
122214921Scognet		printf("Files written\n");
123214921Scognet	fclose(fd);
124214921Scognet
125214921Scognet	if (diskStructure.verbose_level > 0)
126214921Scognet		printf("Image closed\n");
127214921Scognet	return 1;
128214921Scognet
129214921Scognetcleanup_bad_image:
130214921Scognet	fclose(fd);
131214921Scognet	if (!diskStructure.keep_bad_images)
132214921Scognet		unlink(image);
133214921Scognet	if (diskStructure.verbose_level > 0)
134214921Scognet		printf("Bad image cleaned up\n");
135214921Scognet	return 0;
136214921Scognet}
137214921Scognet
138214921Scognetstatic int
139214921Scognetcd9660_write_volume_descriptors(FILE *fd)
140214921Scognet{
141214921Scognet	volume_descriptor *vd_temp = diskStructure.firstVolumeDescriptor;
142214921Scognet	int pos;
143214921Scognet
144214921Scognet	while (vd_temp != NULL) {
145214921Scognet		pos = vd_temp->sector * diskStructure.sectorSize;
146214921Scognet		cd9660_write_filedata(fd, vd_temp->sector,
147214921Scognet		    vd_temp->volumeDescriptorData, 1);
148214921Scognet		vd_temp = vd_temp->next;
149214921Scognet	}
150214921Scognet	return 1;
151214921Scognet}
152214921Scognet
153214921Scognet/*
154214921Scognet * Write out an individual path table
155214921Scognet * Used just to keep redundant code to a minimum
156214921Scognet * @param FILE *fd Valid file pointer
157214921Scognet * @param int Sector to start writing path table to
158214921Scognet * @param int Endian mode : BIG_ENDIAN or LITTLE_ENDIAN
159214921Scognet * @returns int 1 on success, 0 on failure
160214921Scognet */
161214921Scognetstatic int
162214921Scognetcd9660_write_path_table(FILE *fd, off_t sector, int mode)
163214921Scognet{
164214921Scognet	int path_table_sectors = CD9660_BLOCKS(diskStructure.sectorSize,
165214921Scognet	    diskStructure.pathTableLength);
166214921Scognet	unsigned char *buffer;
167214921Scognet	unsigned char *buffer_head;
168214921Scognet	int len;
169214921Scognet	path_table_entry temp_entry;
170214921Scognet	cd9660node *ptcur;
171214921Scognet
172214921Scognet	buffer = malloc(diskStructure.sectorSize * path_table_sectors);
173214921Scognet	if (buffer == NULL) {
174214921Scognet		warnx("%s: Memory allocation error allocating buffer",
175214921Scognet		    __func__);
176214921Scognet		return 0;
177214921Scognet	}
178214921Scognet	buffer_head = buffer;
179214921Scognet	memset(buffer, 0, diskStructure.sectorSize * path_table_sectors);
180214921Scognet
181214921Scognet	ptcur = diskStructure.rootNode;
182214921Scognet
183214921Scognet	while (ptcur != NULL) {
184214921Scognet		memset(&temp_entry, 0, sizeof(path_table_entry));
185214921Scognet		temp_entry.length[0] = ptcur->isoDirRecord->name_len[0];
186214921Scognet		temp_entry.extended_attribute_length[0] =
187214921Scognet		    ptcur->isoDirRecord->ext_attr_length[0];
188214921Scognet		memcpy(temp_entry.name, ptcur->isoDirRecord->name,
189214921Scognet		    temp_entry.length[0] + 1);
190214921Scognet
191214921Scognet		/* round up */
192214921Scognet		len = temp_entry.length[0] + 8 + (temp_entry.length[0] & 0x01);
193214921Scognet
194214921Scognet                /* todo: function pointers instead */
195214921Scognet		if (mode == LITTLE_ENDIAN) {
196214921Scognet			cd9660_731(ptcur->fileDataSector,
197214921Scognet			    temp_entry.first_sector);
198214921Scognet			cd9660_721((ptcur->parent == NULL ?
199214921Scognet				1 : ptcur->parent->ptnumber),
200214921Scognet			    temp_entry.parent_number);
201214921Scognet		} else {
202214921Scognet			cd9660_732(ptcur->fileDataSector,
203214921Scognet			    temp_entry.first_sector);
204214921Scognet			cd9660_722((ptcur->parent == NULL ?
205214921Scognet				1 : ptcur->parent->ptnumber),
206214921Scognet			    temp_entry.parent_number);
207214921Scognet		}
208214921Scognet
209214921Scognet
210214921Scognet		memcpy(buffer, &temp_entry, len);
211214921Scognet		buffer += len;
212214921Scognet
213214921Scognet		ptcur = ptcur->ptnext;
214214921Scognet	}
215214921Scognet
216214921Scognet	return cd9660_write_filedata(fd, sector, buffer_head,
217214921Scognet	    path_table_sectors);
218214921Scognet}
219214921Scognet
220214921Scognet
221214921Scognet/*
222214921Scognet * Write out the path tables to disk
223214921Scognet * Each file descriptor should be pointed to by the PVD, so we know which
224214921Scognet * sector to copy them to. One thing to watch out for: the only path tables
225214921Scognet * stored are in the endian mode that the application is compiled for. So,
226214921Scognet * the first thing to do is write out that path table, then to write the one
227214921Scognet * in the other endian mode requires to convert the endianness of each entry
228214921Scognet * in the table. The best way to do this would be to create a temporary
229214921Scognet * path_table_entry structure, then for each path table entry, copy it to
230214921Scognet * the temporary entry, translate, then copy that to disk.
231214921Scognet *
232214921Scognet * @param FILE* Valid file descriptor
233214921Scognet * @returns int 0 on failure, 1 on success
234214921Scognet */
235214921Scognetstatic int
236214921Scognetcd9660_write_path_tables(FILE *fd)
237214921Scognet{
238214921Scognet	if (cd9660_write_path_table(fd,
239214921Scognet	    diskStructure.primaryLittleEndianTableSector, LITTLE_ENDIAN) == 0)
240214921Scognet		return 0;
241214921Scognet
242214921Scognet	if (cd9660_write_path_table(fd,
243214921Scognet	    diskStructure.primaryBigEndianTableSector, BIG_ENDIAN) == 0)
244214921Scognet		return 0;
245214921Scognet
246214921Scognet	/* @TODO: handle remaining two path tables */
247214921Scognet	return 1;
248214921Scognet}
249214921Scognet
250214921Scognet/*
251214921Scognet * Write a file to disk
252214921Scognet * Writes a file, its directory record, and its data to disk
253214921Scognet * This file is designed to be called RECURSIVELY, so initially call it
254214921Scognet * with the root node. All of the records should store what sector the
255214921Scognet * file goes in, so no computation should be  necessary.
256214921Scognet *
257214921Scognet * @param int fd Valid file descriptor
258214921Scognet * @param struct cd9660node* writenode Pointer to the file to be written
259214921Scognet * @returns int 0 on failure, 1 on success
260214921Scognet */
261214921Scognetstatic int
262214921Scognetcd9660_write_file(FILE *fd, cd9660node *writenode)
263214921Scognet{
264214921Scognet	char *buf;
265214921Scognet	char *temp_file_name;
266214921Scognet	int ret;
267214921Scognet	off_t working_sector;
268214921Scognet	int cur_sector_offset;
269214921Scognet	int written;
270214921Scognet	iso_directory_record_cd9660 temp_record;
271214921Scognet	cd9660node *temp;
272214921Scognet	int rv = 0;
273214921Scognet
274214921Scognet	/* Todo : clean up variables */
275214921Scognet
276214921Scognet	temp_file_name = malloc(CD9660MAXPATH + 1);
277214921Scognet	if (temp_file_name == NULL)
278214921Scognet		err(EXIT_FAILURE, "%s: malloc", __func__);
279214921Scognet
280214921Scognet	memset(temp_file_name, 0, CD9660MAXPATH + 1);
281214921Scognet
282214921Scognet	buf = malloc(diskStructure.sectorSize);
283214921Scognet	if (buf == NULL)
284214921Scognet		err(EXIT_FAILURE, "%s: malloc", __func__);
285214921Scognet
286214921Scognet	if ((writenode->level != 0) &&
287214921Scognet	    !(writenode->node->type & S_IFDIR)) {
288214921Scognet		fsinode *inode = writenode->node->inode;
289214921Scognet		/* Only attempt to write unwritten files that have length. */
290214921Scognet		if ((inode->flags & FI_WRITTEN) != 0) {
291214921Scognet			INODE_WARNX(("%s: skipping written inode %d", __func__,
292214921Scognet			    (int)inode->st.st_ino));
293214921Scognet		} else if (writenode->fileDataLength > 0) {
294214921Scognet			INODE_WARNX(("%s: writing inode %d blocks at %" PRIu32,
295214921Scognet			    __func__, (int)inode->st.st_ino, inode->ino));
296214921Scognet			inode->flags |= FI_WRITTEN;
297223306Smarcel			if (writenode->node->contents == NULL)
298223306Smarcel				cd9660_compute_full_filename(writenode,
299231942Sjkim				    temp_file_name);
300214921Scognet			ret = cd9660_copy_file(fd, writenode->fileDataSector,
301223306Smarcel			    (writenode->node->contents != NULL) ?
302223306Smarcel			    writenode->node->contents : temp_file_name);
303214921Scognet			if (ret == 0)
304214921Scognet				goto out;
305214921Scognet		}
306214921Scognet	} else {
307214921Scognet		/*
308248293Sbrooks		 * Here is a new revelation that ECMA didn't explain
309214921Scognet		 * (at least not well).
310214921Scognet		 * ALL . and .. records store the name "\0" and "\1"
311248293Sbrooks		 * respectively. So, for each directory, we have to
312214921Scognet		 * make a new node.
313214921Scognet		 *
314214921Scognet		 * This is where it gets kinda messy, since we have to
315214921Scognet		 * be careful of sector boundaries
316214921Scognet		 */
317214921Scognet		cur_sector_offset = 0;
318214921Scognet		working_sector = writenode->fileDataSector;
319214921Scognet		if (fseeko(fd, working_sector * diskStructure.sectorSize,
320214921Scognet		    SEEK_SET) == -1)
321214921Scognet			err(1, "fseeko");
322214921Scognet
323214921Scognet		/*
324214921Scognet		 * Now loop over children, writing out their directory
325214921Scognet		 * records - beware of sector boundaries
326214921Scognet	 	 */
327214921Scognet		TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) {
328214921Scognet			/*
329214921Scognet			 * Copy the temporary record and adjust its size
330214921Scognet			 * if necessary
331214921Scognet			 */
332214921Scognet			memcpy(&temp_record, temp->isoDirRecord,
333214921Scognet			    sizeof(iso_directory_record_cd9660));
334214921Scognet
335214921Scognet			temp_record.length[0] =
336214921Scognet			    cd9660_compute_record_size(temp);
337214921Scognet
338214921Scognet			if (temp_record.length[0] + cur_sector_offset >=
339214921Scognet			    diskStructure.sectorSize) {
340214921Scognet				cur_sector_offset = 0;
341214921Scognet				working_sector++;
342214921Scognet
343214921Scognet				/* Seek to the next sector. */
344214921Scognet				if (fseeko(fd, working_sector *
345214921Scognet				    diskStructure.sectorSize, SEEK_SET) == -1)
346214921Scognet					err(1, "fseeko");
347214921Scognet			}
348214921Scognet			/* Write out the basic ISO directory record */
349214921Scognet			written = fwrite(&temp_record, 1,
350214921Scognet			    temp->isoDirRecord->length[0], fd);
351214921Scognet			if (diskStructure.rock_ridge_enabled) {
352214921Scognet				cd9660_write_rr(fd, temp,
353214921Scognet				    cur_sector_offset, working_sector);
354214921Scognet			}
355214921Scognet			if (fseeko(fd, working_sector *
356214921Scognet			    diskStructure.sectorSize + cur_sector_offset +
357214921Scognet			    temp_record.length[0] - temp->su_tail_size,
358214921Scognet			    SEEK_SET) == -1)
359214921Scognet				err(1, "fseeko");
360214921Scognet			if (temp->su_tail_size > 0)
361214921Scognet				fwrite(temp->su_tail_data, 1,
362214921Scognet				    temp->su_tail_size, fd);
363214921Scognet			if (ferror(fd)) {
364214921Scognet				warnx("%s: write error", __func__);
365214921Scognet				goto out;
366214921Scognet			}
367214921Scognet			cur_sector_offset += temp_record.length[0];
368214921Scognet
369214921Scognet		}
370214921Scognet
371214921Scognet		/*
372214921Scognet		 * Recurse on children.
373214921Scognet		 */
374214921Scognet		TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) {
375214921Scognet			if ((ret = cd9660_write_file(fd, temp)) == 0)
376214921Scognet				goto out;
377214921Scognet		}
378214921Scognet	}
379214921Scognet	rv = 1;
380214921Scognetout:
381214921Scognet	free(temp_file_name);
382214921Scognet	free(buf);
383214921Scognet	return rv;
384214921Scognet}
385214921Scognet
386214921Scognet/*
387214921Scognet * Wrapper function to write a buffer (one sector) to disk.
388214921Scognet * Seeks and writes the buffer.
389214921Scognet * NOTE: You dont NEED to use this function, but it might make your
390214921Scognet * life easier if you have to write things that align to a sector
391214921Scognet * (such as volume descriptors).
392214921Scognet *
393214921Scognet * @param int fd Valid file descriptor
394214921Scognet * @param int sector Sector number to write to
395214921Scognet * @param const unsigned char* Buffer to write. This should be the
396214921Scognet *                             size of a sector, and if only a portion
397214921Scognet *                             is written, the rest should be set to 0.
398214921Scognet */
399214921Scognetstatic int
400214921Scognetcd9660_write_filedata(FILE *fd, off_t sector, const unsigned char *buf,
401214921Scognet		      int numsecs)
402214921Scognet{
403214921Scognet	off_t curpos;
404214921Scognet	size_t success;
405214921Scognet
406214921Scognet	curpos = ftello(fd);
407214921Scognet
408214921Scognet	if (fseeko(fd, sector * diskStructure.sectorSize, SEEK_SET) == -1)
409214921Scognet		err(1, "fseeko");
410214921Scognet
411214921Scognet	success = fwrite(buf, diskStructure.sectorSize * numsecs, 1, fd);
412214921Scognet
413214921Scognet	if (fseeko(fd, curpos, SEEK_SET) == -1)
414214921Scognet		err(1, "fseeko");
415214921Scognet
416214921Scognet	if (success == 1)
417214921Scognet		success = diskStructure.sectorSize * numsecs;
418214921Scognet	return success;
419214921Scognet}
420214921Scognet
421214921Scognet#if 0
422214921Scognetstatic int
423214921Scognetcd9660_write_buffered(FILE *fd, off_t offset, int buff_len,
424214921Scognet		      const unsigned char* buffer)
425214921Scognet{
426214921Scognet	static int working_sector = -1;
427214921Scognet	static char buf[CD9660_SECTOR_SIZE];
428214921Scognet
429214921Scognet	return 0;
430214921Scognet}
431214921Scognet#endif
432214921Scognet
433214921Scognetint
434214921Scognetcd9660_copy_file(FILE *fd, off_t start_sector, const char *filename)
435214921Scognet{
436214921Scognet	FILE *rf;
437214921Scognet	int bytes_read;
438214921Scognet	off_t sector = start_sector;
439214921Scognet	int buf_size = diskStructure.sectorSize;
440214921Scognet	char *buf;
441214921Scognet
442214921Scognet	buf = malloc(buf_size);
443214921Scognet	if (buf == NULL)
444214921Scognet		err(EXIT_FAILURE, "%s: malloc", __func__);
445214921Scognet
446214921Scognet	if ((rf = fopen(filename, "rb")) == NULL) {
447214921Scognet		warn("%s: cannot open %s", __func__, filename);
448214921Scognet		free(buf);
449214921Scognet		return 0;
450214921Scognet	}
451214921Scognet
452214921Scognet	if (diskStructure.verbose_level > 1)
453214921Scognet		printf("Writing file: %s\n",filename);
454214921Scognet
455214921Scognet	if (fseeko(fd, start_sector * diskStructure.sectorSize, SEEK_SET) == -1)
456214921Scognet		err(1, "fseeko");
457214921Scognet
458214921Scognet	while (!feof(rf)) {
459214921Scognet		bytes_read = fread(buf,1,buf_size,rf);
460214921Scognet		if (ferror(rf)) {
461214921Scognet			warn("%s: fread", __func__);
462214921Scognet			free(buf);
463224762Smarius			(void)fclose(rf);
464214921Scognet			return 0;
465214921Scognet		}
466214921Scognet
467214921Scognet		fwrite(buf,1,bytes_read,fd);
468214921Scognet		if (ferror(fd)) {
469214921Scognet			warn("%s: fwrite", __func__);
470214921Scognet			free(buf);
471224762Smarius			(void)fclose(rf);
472214921Scognet			return 0;
473214921Scognet		}
474214921Scognet		sector++;
475214921Scognet	}
476214921Scognet
477214921Scognet	fclose(rf);
478214921Scognet	free(buf);
479214921Scognet	return 1;
480214921Scognet}
481214921Scognet
482214921Scognetstatic void
483214921Scognetcd9660_write_rr(FILE *fd, cd9660node *writenode, off_t offset, off_t sector)
484214921Scognet{
485214921Scognet	int in_ca = 0;
486214921Scognet	struct ISO_SUSP_ATTRIBUTES *myattr;
487214921Scognet
488214921Scognet	offset += writenode->isoDirRecord->length[0];
489214921Scognet	if (fseeko(fd, sector * diskStructure.sectorSize + offset, SEEK_SET) ==
490214921Scognet	    -1)
491214921Scognet		err(1, "fseeko");
492214921Scognet	/* Offset now points at the end of the record */
493214921Scognet	TAILQ_FOREACH(myattr, &writenode->head, rr_ll) {
494214921Scognet		fwrite(&(myattr->attr), CD9660_SUSP_ENTRY_SIZE(myattr), 1, fd);
495214921Scognet
496214921Scognet		if (!in_ca) {
497214921Scognet			offset += CD9660_SUSP_ENTRY_SIZE(myattr);
498214921Scognet			if (myattr->last_in_suf) {
499214921Scognet				/*
500214921Scognet				 * Point the offset to the start of this
501214921Scognet				 * record's CE area
502214921Scognet				 */
503214921Scognet				if (fseeko(fd, ((off_t)diskStructure.
504214921Scognet				    susp_continuation_area_start_sector *
505214921Scognet				    diskStructure.sectorSize)
506214921Scognet				    + writenode->susp_entry_ce_start,
507214921Scognet				    SEEK_SET) == -1)
508214921Scognet					err(1, "fseeko");
509214921Scognet				in_ca = 1;
510214921Scognet			}
511214921Scognet		}
512214921Scognet	}
513214921Scognet
514214921Scognet	/*
515214921Scognet	 * If we had to go to the continuation area, head back to
516214921Scognet	 * where we should be.
517214921Scognet	 */
518214921Scognet	if (in_ca)
519214921Scognet		if (fseeko(fd, sector * diskStructure.sectorSize + offset,
520214921Scognet		    SEEK_SET) == -1)
521214921Scognet			err(1, "fseeko");
522214921Scognet}
523