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;
168290587Sngie	int len, ret;
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
216290587Sngie	ret = cd9660_write_filedata(fd, sector, buffer_head,
217214921Scognet	    path_table_sectors);
218290587Sngie	free(buffer_head);
219290587Sngie	return ret;
220214921Scognet}
221214921Scognet
222214921Scognet
223214921Scognet/*
224214921Scognet * Write out the path tables to disk
225214921Scognet * Each file descriptor should be pointed to by the PVD, so we know which
226214921Scognet * sector to copy them to. One thing to watch out for: the only path tables
227214921Scognet * stored are in the endian mode that the application is compiled for. So,
228214921Scognet * the first thing to do is write out that path table, then to write the one
229214921Scognet * in the other endian mode requires to convert the endianness of each entry
230214921Scognet * in the table. The best way to do this would be to create a temporary
231214921Scognet * path_table_entry structure, then for each path table entry, copy it to
232214921Scognet * the temporary entry, translate, then copy that to disk.
233214921Scognet *
234214921Scognet * @param FILE* Valid file descriptor
235214921Scognet * @returns int 0 on failure, 1 on success
236214921Scognet */
237214921Scognetstatic int
238214921Scognetcd9660_write_path_tables(FILE *fd)
239214921Scognet{
240214921Scognet	if (cd9660_write_path_table(fd,
241214921Scognet	    diskStructure.primaryLittleEndianTableSector, LITTLE_ENDIAN) == 0)
242214921Scognet		return 0;
243214921Scognet
244214921Scognet	if (cd9660_write_path_table(fd,
245214921Scognet	    diskStructure.primaryBigEndianTableSector, BIG_ENDIAN) == 0)
246214921Scognet		return 0;
247214921Scognet
248214921Scognet	/* @TODO: handle remaining two path tables */
249214921Scognet	return 1;
250214921Scognet}
251214921Scognet
252214921Scognet/*
253214921Scognet * Write a file to disk
254214921Scognet * Writes a file, its directory record, and its data to disk
255214921Scognet * This file is designed to be called RECURSIVELY, so initially call it
256214921Scognet * with the root node. All of the records should store what sector the
257214921Scognet * file goes in, so no computation should be  necessary.
258214921Scognet *
259214921Scognet * @param int fd Valid file descriptor
260214921Scognet * @param struct cd9660node* writenode Pointer to the file to be written
261214921Scognet * @returns int 0 on failure, 1 on success
262214921Scognet */
263214921Scognetstatic int
264214921Scognetcd9660_write_file(FILE *fd, cd9660node *writenode)
265214921Scognet{
266214921Scognet	char *buf;
267214921Scognet	char *temp_file_name;
268214921Scognet	int ret;
269214921Scognet	off_t working_sector;
270214921Scognet	int cur_sector_offset;
271214921Scognet	int written;
272214921Scognet	iso_directory_record_cd9660 temp_record;
273214921Scognet	cd9660node *temp;
274214921Scognet	int rv = 0;
275214921Scognet
276214921Scognet	/* Todo : clean up variables */
277214921Scognet
278214921Scognet	temp_file_name = malloc(CD9660MAXPATH + 1);
279214921Scognet	if (temp_file_name == NULL)
280214921Scognet		err(EXIT_FAILURE, "%s: malloc", __func__);
281214921Scognet
282214921Scognet	memset(temp_file_name, 0, CD9660MAXPATH + 1);
283214921Scognet
284214921Scognet	buf = malloc(diskStructure.sectorSize);
285214921Scognet	if (buf == NULL)
286214921Scognet		err(EXIT_FAILURE, "%s: malloc", __func__);
287214921Scognet
288214921Scognet	if ((writenode->level != 0) &&
289214921Scognet	    !(writenode->node->type & S_IFDIR)) {
290214921Scognet		fsinode *inode = writenode->node->inode;
291214921Scognet		/* Only attempt to write unwritten files that have length. */
292214921Scognet		if ((inode->flags & FI_WRITTEN) != 0) {
293214921Scognet			INODE_WARNX(("%s: skipping written inode %d", __func__,
294214921Scognet			    (int)inode->st.st_ino));
295214921Scognet		} else if (writenode->fileDataLength > 0) {
296214921Scognet			INODE_WARNX(("%s: writing inode %d blocks at %" PRIu32,
297214921Scognet			    __func__, (int)inode->st.st_ino, inode->ino));
298214921Scognet			inode->flags |= FI_WRITTEN;
299223306Smarcel			if (writenode->node->contents == NULL)
300223306Smarcel				cd9660_compute_full_filename(writenode,
301230795Sjkim				    temp_file_name);
302214921Scognet			ret = cd9660_copy_file(fd, writenode->fileDataSector,
303223306Smarcel			    (writenode->node->contents != NULL) ?
304223306Smarcel			    writenode->node->contents : temp_file_name);
305214921Scognet			if (ret == 0)
306214921Scognet				goto out;
307214921Scognet		}
308214921Scognet	} else {
309214921Scognet		/*
310228990Suqs		 * Here is a new revelation that ECMA didn't explain
311214921Scognet		 * (at least not well).
312214921Scognet		 * ALL . and .. records store the name "\0" and "\1"
313228990Suqs		 * respectively. So, for each directory, we have to
314214921Scognet		 * make a new node.
315214921Scognet		 *
316214921Scognet		 * This is where it gets kinda messy, since we have to
317214921Scognet		 * be careful of sector boundaries
318214921Scognet		 */
319214921Scognet		cur_sector_offset = 0;
320214921Scognet		working_sector = writenode->fileDataSector;
321214921Scognet		if (fseeko(fd, working_sector * diskStructure.sectorSize,
322214921Scognet		    SEEK_SET) == -1)
323214921Scognet			err(1, "fseeko");
324214921Scognet
325214921Scognet		/*
326214921Scognet		 * Now loop over children, writing out their directory
327214921Scognet		 * records - beware of sector boundaries
328214921Scognet	 	 */
329214921Scognet		TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) {
330214921Scognet			/*
331214921Scognet			 * Copy the temporary record and adjust its size
332214921Scognet			 * if necessary
333214921Scognet			 */
334214921Scognet			memcpy(&temp_record, temp->isoDirRecord,
335214921Scognet			    sizeof(iso_directory_record_cd9660));
336214921Scognet
337214921Scognet			temp_record.length[0] =
338214921Scognet			    cd9660_compute_record_size(temp);
339214921Scognet
340214921Scognet			if (temp_record.length[0] + cur_sector_offset >=
341214921Scognet			    diskStructure.sectorSize) {
342214921Scognet				cur_sector_offset = 0;
343214921Scognet				working_sector++;
344214921Scognet
345214921Scognet				/* Seek to the next sector. */
346214921Scognet				if (fseeko(fd, working_sector *
347214921Scognet				    diskStructure.sectorSize, SEEK_SET) == -1)
348214921Scognet					err(1, "fseeko");
349214921Scognet			}
350214921Scognet			/* Write out the basic ISO directory record */
351214921Scognet			written = fwrite(&temp_record, 1,
352214921Scognet			    temp->isoDirRecord->length[0], fd);
353214921Scognet			if (diskStructure.rock_ridge_enabled) {
354214921Scognet				cd9660_write_rr(fd, temp,
355214921Scognet				    cur_sector_offset, working_sector);
356214921Scognet			}
357214921Scognet			if (fseeko(fd, working_sector *
358214921Scognet			    diskStructure.sectorSize + cur_sector_offset +
359214921Scognet			    temp_record.length[0] - temp->su_tail_size,
360214921Scognet			    SEEK_SET) == -1)
361214921Scognet				err(1, "fseeko");
362214921Scognet			if (temp->su_tail_size > 0)
363214921Scognet				fwrite(temp->su_tail_data, 1,
364214921Scognet				    temp->su_tail_size, fd);
365214921Scognet			if (ferror(fd)) {
366214921Scognet				warnx("%s: write error", __func__);
367214921Scognet				goto out;
368214921Scognet			}
369214921Scognet			cur_sector_offset += temp_record.length[0];
370214921Scognet
371214921Scognet		}
372214921Scognet
373214921Scognet		/*
374214921Scognet		 * Recurse on children.
375214921Scognet		 */
376214921Scognet		TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) {
377214921Scognet			if ((ret = cd9660_write_file(fd, temp)) == 0)
378214921Scognet				goto out;
379214921Scognet		}
380214921Scognet	}
381214921Scognet	rv = 1;
382214921Scognetout:
383214921Scognet	free(temp_file_name);
384214921Scognet	free(buf);
385214921Scognet	return rv;
386214921Scognet}
387214921Scognet
388214921Scognet/*
389214921Scognet * Wrapper function to write a buffer (one sector) to disk.
390214921Scognet * Seeks and writes the buffer.
391214921Scognet * NOTE: You dont NEED to use this function, but it might make your
392214921Scognet * life easier if you have to write things that align to a sector
393214921Scognet * (such as volume descriptors).
394214921Scognet *
395214921Scognet * @param int fd Valid file descriptor
396214921Scognet * @param int sector Sector number to write to
397214921Scognet * @param const unsigned char* Buffer to write. This should be the
398214921Scognet *                             size of a sector, and if only a portion
399214921Scognet *                             is written, the rest should be set to 0.
400214921Scognet */
401214921Scognetstatic int
402214921Scognetcd9660_write_filedata(FILE *fd, off_t sector, const unsigned char *buf,
403214921Scognet		      int numsecs)
404214921Scognet{
405214921Scognet	off_t curpos;
406214921Scognet	size_t success;
407214921Scognet
408214921Scognet	curpos = ftello(fd);
409214921Scognet
410214921Scognet	if (fseeko(fd, sector * diskStructure.sectorSize, SEEK_SET) == -1)
411214921Scognet		err(1, "fseeko");
412214921Scognet
413214921Scognet	success = fwrite(buf, diskStructure.sectorSize * numsecs, 1, fd);
414214921Scognet
415214921Scognet	if (fseeko(fd, curpos, SEEK_SET) == -1)
416214921Scognet		err(1, "fseeko");
417214921Scognet
418214921Scognet	if (success == 1)
419214921Scognet		success = diskStructure.sectorSize * numsecs;
420214921Scognet	return success;
421214921Scognet}
422214921Scognet
423214921Scognet#if 0
424214921Scognetstatic int
425214921Scognetcd9660_write_buffered(FILE *fd, off_t offset, int buff_len,
426214921Scognet		      const unsigned char* buffer)
427214921Scognet{
428214921Scognet	static int working_sector = -1;
429214921Scognet	static char buf[CD9660_SECTOR_SIZE];
430214921Scognet
431214921Scognet	return 0;
432214921Scognet}
433214921Scognet#endif
434214921Scognet
435214921Scognetint
436214921Scognetcd9660_copy_file(FILE *fd, off_t start_sector, const char *filename)
437214921Scognet{
438214921Scognet	FILE *rf;
439214921Scognet	int bytes_read;
440214921Scognet	off_t sector = start_sector;
441214921Scognet	int buf_size = diskStructure.sectorSize;
442214921Scognet	char *buf;
443214921Scognet
444214921Scognet	buf = malloc(buf_size);
445214921Scognet	if (buf == NULL)
446214921Scognet		err(EXIT_FAILURE, "%s: malloc", __func__);
447214921Scognet
448214921Scognet	if ((rf = fopen(filename, "rb")) == NULL) {
449214921Scognet		warn("%s: cannot open %s", __func__, filename);
450214921Scognet		free(buf);
451214921Scognet		return 0;
452214921Scognet	}
453214921Scognet
454214921Scognet	if (diskStructure.verbose_level > 1)
455214921Scognet		printf("Writing file: %s\n",filename);
456214921Scognet
457214921Scognet	if (fseeko(fd, start_sector * diskStructure.sectorSize, SEEK_SET) == -1)
458214921Scognet		err(1, "fseeko");
459214921Scognet
460214921Scognet	while (!feof(rf)) {
461214921Scognet		bytes_read = fread(buf,1,buf_size,rf);
462214921Scognet		if (ferror(rf)) {
463214921Scognet			warn("%s: fread", __func__);
464214921Scognet			free(buf);
465224762Smarius			(void)fclose(rf);
466214921Scognet			return 0;
467214921Scognet		}
468214921Scognet
469214921Scognet		fwrite(buf,1,bytes_read,fd);
470214921Scognet		if (ferror(fd)) {
471214921Scognet			warn("%s: fwrite", __func__);
472214921Scognet			free(buf);
473224762Smarius			(void)fclose(rf);
474214921Scognet			return 0;
475214921Scognet		}
476214921Scognet		sector++;
477214921Scognet	}
478214921Scognet
479214921Scognet	fclose(rf);
480214921Scognet	free(buf);
481214921Scognet	return 1;
482214921Scognet}
483214921Scognet
484214921Scognetstatic void
485214921Scognetcd9660_write_rr(FILE *fd, cd9660node *writenode, off_t offset, off_t sector)
486214921Scognet{
487214921Scognet	int in_ca = 0;
488214921Scognet	struct ISO_SUSP_ATTRIBUTES *myattr;
489214921Scognet
490214921Scognet	offset += writenode->isoDirRecord->length[0];
491214921Scognet	if (fseeko(fd, sector * diskStructure.sectorSize + offset, SEEK_SET) ==
492214921Scognet	    -1)
493214921Scognet		err(1, "fseeko");
494214921Scognet	/* Offset now points at the end of the record */
495214921Scognet	TAILQ_FOREACH(myattr, &writenode->head, rr_ll) {
496214921Scognet		fwrite(&(myattr->attr), CD9660_SUSP_ENTRY_SIZE(myattr), 1, fd);
497214921Scognet
498214921Scognet		if (!in_ca) {
499214921Scognet			offset += CD9660_SUSP_ENTRY_SIZE(myattr);
500214921Scognet			if (myattr->last_in_suf) {
501214921Scognet				/*
502214921Scognet				 * Point the offset to the start of this
503214921Scognet				 * record's CE area
504214921Scognet				 */
505214921Scognet				if (fseeko(fd, ((off_t)diskStructure.
506214921Scognet				    susp_continuation_area_start_sector *
507214921Scognet				    diskStructure.sectorSize)
508214921Scognet				    + writenode->susp_entry_ce_start,
509214921Scognet				    SEEK_SET) == -1)
510214921Scognet					err(1, "fseeko");
511214921Scognet				in_ca = 1;
512214921Scognet			}
513214921Scognet		}
514214921Scognet	}
515214921Scognet
516214921Scognet	/*
517214921Scognet	 * If we had to go to the continuation area, head back to
518214921Scognet	 * where we should be.
519214921Scognet	 */
520214921Scognet	if (in_ca)
521214921Scognet		if (fseeko(fd, sector * diskStructure.sectorSize + offset,
522214921Scognet		    SEEK_SET) == -1)
523214921Scognet			err(1, "fseeko");
524214921Scognet}
525