cd9660_write.c revision 1.16 1 1.16 christos /* $NetBSD: cd9660_write.c,v 1.16 2013/01/28 21:03:28 christos Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*
4 1.1 fvdl * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
5 1.1 fvdl * Perez-Rathke and Ram Vedam. All rights reserved.
6 1.1 fvdl *
7 1.1 fvdl * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
8 1.1 fvdl * Alan Perez-Rathke and Ram Vedam.
9 1.1 fvdl *
10 1.1 fvdl * Redistribution and use in source and binary forms, with or
11 1.1 fvdl * without modification, are permitted provided that the following
12 1.1 fvdl * conditions are met:
13 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
14 1.1 fvdl * notice, this list of conditions and the following disclaimer.
15 1.1 fvdl * 2. Redistributions in binary form must reproduce the above
16 1.1 fvdl * copyright notice, this list of conditions and the following
17 1.1 fvdl * disclaimer in the documentation and/or other materials provided
18 1.1 fvdl * with the distribution.
19 1.1 fvdl *
20 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
21 1.1 fvdl * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
22 1.1 fvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.3 dyoung * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 1.1 fvdl * DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
25 1.1 fvdl * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 fvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 fvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 1.1 fvdl * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 1.1 fvdl * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 1.1 fvdl * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 fvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 1.1 fvdl * OF SUCH DAMAGE.
33 1.1 fvdl */
34 1.1 fvdl
35 1.1 fvdl #include "cd9660.h"
36 1.1 fvdl #include "iso9660_rrip.h"
37 1.1 fvdl
38 1.1 fvdl #include <sys/cdefs.h>
39 1.1 fvdl #if defined(__RCSID) && !defined(__lint)
40 1.16 christos __RCSID("$NetBSD: cd9660_write.c,v 1.16 2013/01/28 21:03:28 christos Exp $");
41 1.1 fvdl #endif /* !__lint */
42 1.1 fvdl
43 1.16 christos #include <util.h>
44 1.16 christos
45 1.16 christos static int cd9660_write_volume_descriptors(iso9660_disk *, FILE *);
46 1.16 christos static int cd9660_write_path_table(iso9660_disk *, FILE *, off_t, int);
47 1.16 christos static int cd9660_write_path_tables(iso9660_disk *, FILE *);
48 1.16 christos static int cd9660_write_file(iso9660_disk *, FILE *, cd9660node *);
49 1.16 christos static int cd9660_write_filedata(iso9660_disk *, FILE *, off_t,
50 1.16 christos const unsigned char *, int);
51 1.1 fvdl #if 0
52 1.13 christos static int cd9660_write_buffered(FILE *, off_t, int, const unsigned char *);
53 1.1 fvdl #endif
54 1.16 christos static void cd9660_write_rr(iso9660_disk *, FILE *, cd9660node *, off_t, off_t);
55 1.1 fvdl
56 1.1 fvdl /*
57 1.1 fvdl * Write the image
58 1.1 fvdl * Writes the entire image
59 1.1 fvdl * @param const char* The filename for the image
60 1.1 fvdl * @returns int 1 on success, 0 on failure
61 1.1 fvdl */
62 1.3 dyoung int
63 1.16 christos cd9660_write_image(iso9660_disk *diskStructure, const char* image)
64 1.1 fvdl {
65 1.1 fvdl FILE *fd;
66 1.1 fvdl int status;
67 1.13 christos char buf[CD9660_SECTOR_SIZE];
68 1.3 dyoung
69 1.4 dyoung if ((fd = fopen(image, "w+")) == NULL) {
70 1.4 dyoung err(EXIT_FAILURE, "%s: Can't open `%s' for writing", __func__,
71 1.4 dyoung image);
72 1.4 dyoung }
73 1.1 fvdl
74 1.16 christos if (diskStructure->verbose_level > 0)
75 1.1 fvdl printf("Writing image\n");
76 1.1 fvdl
77 1.16 christos if (diskStructure->has_generic_bootimage) {
78 1.16 christos status = cd9660_copy_file(diskStructure, fd, 0,
79 1.16 christos diskStructure->generic_bootimage);
80 1.9 skrll if (status == 0) {
81 1.9 skrll warnx("%s: Error writing generic boot image",
82 1.9 skrll __func__);
83 1.9 skrll goto cleanup_bad_image;
84 1.9 skrll }
85 1.9 skrll }
86 1.9 skrll
87 1.1 fvdl /* Write the volume descriptors */
88 1.16 christos status = cd9660_write_volume_descriptors(diskStructure, fd);
89 1.1 fvdl if (status == 0) {
90 1.4 dyoung warnx("%s: Error writing volume descriptors to image",
91 1.4 dyoung __func__);
92 1.1 fvdl goto cleanup_bad_image;
93 1.1 fvdl }
94 1.3 dyoung
95 1.16 christos if (diskStructure->verbose_level > 0)
96 1.1 fvdl printf("Volume descriptors written\n");
97 1.3 dyoung
98 1.1 fvdl /*
99 1.1 fvdl * Write the path tables: there are actually four, but right
100 1.1 fvdl * now we are only concearned with two.
101 1.1 fvdl */
102 1.16 christos status = cd9660_write_path_tables(diskStructure, fd);
103 1.1 fvdl if (status == 0) {
104 1.4 dyoung warnx("%s: Error writing path tables to image", __func__);
105 1.1 fvdl goto cleanup_bad_image;
106 1.1 fvdl }
107 1.3 dyoung
108 1.16 christos if (diskStructure->verbose_level > 0)
109 1.1 fvdl printf("Path tables written\n");
110 1.3 dyoung
111 1.1 fvdl /* Write the directories and files */
112 1.16 christos status = cd9660_write_file(diskStructure, fd, diskStructure->rootNode);
113 1.1 fvdl if (status == 0) {
114 1.4 dyoung warnx("%s: Error writing files to image", __func__);
115 1.1 fvdl goto cleanup_bad_image;
116 1.1 fvdl }
117 1.1 fvdl
118 1.16 christos if (diskStructure->is_bootable) {
119 1.16 christos cd9660_write_boot(diskStructure, fd);
120 1.1 fvdl }
121 1.1 fvdl
122 1.1 fvdl /* Write padding bits. This is temporary */
123 1.13 christos memset(buf, 0, CD9660_SECTOR_SIZE);
124 1.16 christos cd9660_write_filedata(diskStructure, fd,
125 1.16 christos diskStructure->totalSectors - 1, buf, 1);
126 1.3 dyoung
127 1.16 christos if (diskStructure->verbose_level > 0)
128 1.1 fvdl printf("Files written\n");
129 1.1 fvdl fclose(fd);
130 1.3 dyoung
131 1.16 christos if (diskStructure->verbose_level > 0)
132 1.1 fvdl printf("Image closed\n");
133 1.1 fvdl return 1;
134 1.1 fvdl
135 1.1 fvdl cleanup_bad_image:
136 1.1 fvdl fclose(fd);
137 1.16 christos if (!diskStructure->keep_bad_images)
138 1.1 fvdl unlink(image);
139 1.16 christos if (diskStructure->verbose_level > 0)
140 1.1 fvdl printf("Bad image cleaned up\n");
141 1.1 fvdl return 0;
142 1.1 fvdl }
143 1.1 fvdl
144 1.1 fvdl static int
145 1.16 christos cd9660_write_volume_descriptors(iso9660_disk *diskStructure, FILE *fd)
146 1.1 fvdl {
147 1.16 christos volume_descriptor *vd_temp = diskStructure->firstVolumeDescriptor;
148 1.1 fvdl int pos;
149 1.1 fvdl
150 1.1 fvdl while (vd_temp != NULL) {
151 1.16 christos pos = vd_temp->sector * diskStructure->sectorSize;
152 1.16 christos cd9660_write_filedata(diskStructure, fd, vd_temp->sector,
153 1.1 fvdl vd_temp->volumeDescriptorData, 1);
154 1.1 fvdl vd_temp = vd_temp->next;
155 1.1 fvdl }
156 1.1 fvdl return 1;
157 1.1 fvdl }
158 1.1 fvdl
159 1.1 fvdl /*
160 1.1 fvdl * Write out an individual path table
161 1.1 fvdl * Used just to keep redundant code to a minimum
162 1.1 fvdl * @param FILE *fd Valid file pointer
163 1.1 fvdl * @param int Sector to start writing path table to
164 1.1 fvdl * @param int Endian mode : BIG_ENDIAN or LITTLE_ENDIAN
165 1.1 fvdl * @returns int 1 on success, 0 on failure
166 1.1 fvdl */
167 1.1 fvdl static int
168 1.16 christos cd9660_write_path_table(iso9660_disk *diskStructure, FILE *fd, off_t sector,
169 1.16 christos int mode)
170 1.1 fvdl {
171 1.16 christos int path_table_sectors = CD9660_BLOCKS(diskStructure->sectorSize,
172 1.16 christos diskStructure->pathTableLength);
173 1.1 fvdl unsigned char *buffer;
174 1.1 fvdl unsigned char *buffer_head;
175 1.1 fvdl int len;
176 1.1 fvdl path_table_entry temp_entry;
177 1.1 fvdl cd9660node *ptcur;
178 1.1 fvdl
179 1.16 christos buffer = ecalloc(path_table_sectors, diskStructure->sectorSize);
180 1.1 fvdl buffer_head = buffer;
181 1.1 fvdl
182 1.16 christos ptcur = diskStructure->rootNode;
183 1.3 dyoung
184 1.1 fvdl while (ptcur != NULL) {
185 1.1 fvdl memset(&temp_entry, 0, sizeof(path_table_entry));
186 1.1 fvdl temp_entry.length[0] = ptcur->isoDirRecord->name_len[0];
187 1.1 fvdl temp_entry.extended_attribute_length[0] =
188 1.1 fvdl ptcur->isoDirRecord->ext_attr_length[0];
189 1.1 fvdl memcpy(temp_entry.name, ptcur->isoDirRecord->name,
190 1.1 fvdl temp_entry.length[0] + 1);
191 1.1 fvdl
192 1.1 fvdl /* round up */
193 1.1 fvdl len = temp_entry.length[0] + 8 + (temp_entry.length[0] & 0x01);
194 1.1 fvdl
195 1.1 fvdl /* todo: function pointers instead */
196 1.1 fvdl if (mode == LITTLE_ENDIAN) {
197 1.1 fvdl cd9660_731(ptcur->fileDataSector,
198 1.1 fvdl temp_entry.first_sector);
199 1.1 fvdl cd9660_721((ptcur->parent == NULL ?
200 1.1 fvdl 1 : ptcur->parent->ptnumber),
201 1.1 fvdl temp_entry.parent_number);
202 1.1 fvdl } else {
203 1.1 fvdl cd9660_732(ptcur->fileDataSector,
204 1.1 fvdl temp_entry.first_sector);
205 1.1 fvdl cd9660_722((ptcur->parent == NULL ?
206 1.1 fvdl 1 : ptcur->parent->ptnumber),
207 1.1 fvdl temp_entry.parent_number);
208 1.1 fvdl }
209 1.3 dyoung
210 1.1 fvdl
211 1.1 fvdl memcpy(buffer, &temp_entry, len);
212 1.1 fvdl buffer += len;
213 1.3 dyoung
214 1.1 fvdl ptcur = ptcur->ptnext;
215 1.1 fvdl }
216 1.3 dyoung
217 1.16 christos return cd9660_write_filedata(diskStructure, fd, sector, buffer_head,
218 1.1 fvdl path_table_sectors);
219 1.1 fvdl }
220 1.1 fvdl
221 1.1 fvdl
222 1.1 fvdl /*
223 1.1 fvdl * Write out the path tables to disk
224 1.1 fvdl * Each file descriptor should be pointed to by the PVD, so we know which
225 1.1 fvdl * sector to copy them to. One thing to watch out for: the only path tables
226 1.1 fvdl * stored are in the endian mode that the application is compiled for. So,
227 1.1 fvdl * the first thing to do is write out that path table, then to write the one
228 1.1 fvdl * in the other endian mode requires to convert the endianness of each entry
229 1.1 fvdl * in the table. The best way to do this would be to create a temporary
230 1.1 fvdl * path_table_entry structure, then for each path table entry, copy it to
231 1.1 fvdl * the temporary entry, translate, then copy that to disk.
232 1.1 fvdl *
233 1.1 fvdl * @param FILE* Valid file descriptor
234 1.1 fvdl * @returns int 0 on failure, 1 on success
235 1.1 fvdl */
236 1.1 fvdl static int
237 1.16 christos cd9660_write_path_tables(iso9660_disk *diskStructure, FILE *fd)
238 1.1 fvdl {
239 1.16 christos if (cd9660_write_path_table(diskStructure, fd,
240 1.16 christos diskStructure->primaryLittleEndianTableSector, LITTLE_ENDIAN) == 0)
241 1.1 fvdl return 0;
242 1.1 fvdl
243 1.16 christos if (cd9660_write_path_table(diskStructure, fd,
244 1.16 christos diskStructure->primaryBigEndianTableSector, BIG_ENDIAN) == 0)
245 1.1 fvdl return 0;
246 1.1 fvdl
247 1.1 fvdl /* @TODO: handle remaining two path tables */
248 1.1 fvdl return 1;
249 1.1 fvdl }
250 1.1 fvdl
251 1.1 fvdl /*
252 1.1 fvdl * Write a file to disk
253 1.1 fvdl * Writes a file, its directory record, and its data to disk
254 1.1 fvdl * This file is designed to be called RECURSIVELY, so initially call it
255 1.1 fvdl * with the root node. All of the records should store what sector the
256 1.1 fvdl * file goes in, so no computation should be necessary.
257 1.1 fvdl *
258 1.1 fvdl * @param int fd Valid file descriptor
259 1.1 fvdl * @param struct cd9660node* writenode Pointer to the file to be written
260 1.1 fvdl * @returns int 0 on failure, 1 on success
261 1.1 fvdl */
262 1.1 fvdl static int
263 1.16 christos cd9660_write_file(iso9660_disk *diskStructure, FILE *fd, cd9660node *writenode)
264 1.1 fvdl {
265 1.1 fvdl char *buf;
266 1.1 fvdl char *temp_file_name;
267 1.1 fvdl int ret;
268 1.13 christos off_t working_sector;
269 1.1 fvdl int cur_sector_offset;
270 1.1 fvdl int written;
271 1.1 fvdl iso_directory_record_cd9660 temp_record;
272 1.1 fvdl cd9660node *temp;
273 1.10 bjh21 int rv = 0;
274 1.1 fvdl
275 1.1 fvdl /* Todo : clean up variables */
276 1.3 dyoung
277 1.16 christos temp_file_name = ecalloc(CD9660MAXPATH + 1, 1);
278 1.16 christos buf = emalloc(diskStructure->sectorSize);
279 1.1 fvdl if ((writenode->level != 0) &&
280 1.1 fvdl !(writenode->node->type & S_IFDIR)) {
281 1.5 dyoung fsinode *inode = writenode->node->inode;
282 1.5 dyoung /* Only attempt to write unwritten files that have length. */
283 1.5 dyoung if ((inode->flags & FI_WRITTEN) != 0) {
284 1.5 dyoung INODE_WARNX(("%s: skipping written inode %d", __func__,
285 1.5 dyoung (int)inode->st.st_ino));
286 1.5 dyoung } else if (writenode->fileDataLength > 0) {
287 1.6 dyoung INODE_WARNX(("%s: writing inode %d blocks at %" PRIu32,
288 1.6 dyoung __func__, (int)inode->st.st_ino, inode->ino));
289 1.5 dyoung inode->flags |= FI_WRITTEN;
290 1.1 fvdl cd9660_compute_full_filename(writenode,
291 1.15 christos temp_file_name);
292 1.16 christos ret = cd9660_copy_file(diskStructure, fd,
293 1.16 christos writenode->fileDataSector, temp_file_name);
294 1.8 christos if (ret == 0)
295 1.8 christos goto out;
296 1.1 fvdl }
297 1.1 fvdl } else {
298 1.1 fvdl /*
299 1.1 fvdl * Here is a new revelation that ECMA didnt explain
300 1.1 fvdl * (at least not well).
301 1.1 fvdl * ALL . and .. records store the name "\0" and "\1"
302 1.1 fvdl * resepctively. So, for each directory, we have to
303 1.1 fvdl * make a new node.
304 1.1 fvdl *
305 1.1 fvdl * This is where it gets kinda messy, since we have to
306 1.1 fvdl * be careful of sector boundaries
307 1.1 fvdl */
308 1.1 fvdl cur_sector_offset = 0;
309 1.1 fvdl working_sector = writenode->fileDataSector;
310 1.16 christos if (fseeko(fd, working_sector * diskStructure->sectorSize,
311 1.13 christos SEEK_SET) == -1)
312 1.13 christos err(1, "fseeko");
313 1.3 dyoung
314 1.1 fvdl /*
315 1.1 fvdl * Now loop over children, writing out their directory
316 1.1 fvdl * records - beware of sector boundaries
317 1.1 fvdl */
318 1.2 dyoung TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) {
319 1.1 fvdl /*
320 1.1 fvdl * Copy the temporary record and adjust its size
321 1.1 fvdl * if necessary
322 1.1 fvdl */
323 1.1 fvdl memcpy(&temp_record, temp->isoDirRecord,
324 1.1 fvdl sizeof(iso_directory_record_cd9660));
325 1.1 fvdl
326 1.1 fvdl temp_record.length[0] =
327 1.16 christos cd9660_compute_record_size(diskStructure, temp);
328 1.3 dyoung
329 1.1 fvdl if (temp_record.length[0] + cur_sector_offset >=
330 1.16 christos diskStructure->sectorSize) {
331 1.1 fvdl cur_sector_offset = 0;
332 1.1 fvdl working_sector++;
333 1.1 fvdl
334 1.1 fvdl /* Seek to the next sector. */
335 1.13 christos if (fseeko(fd, working_sector *
336 1.16 christos diskStructure->sectorSize, SEEK_SET) == -1)
337 1.13 christos err(1, "fseeko");
338 1.1 fvdl }
339 1.10 bjh21 /* Write out the basic ISO directory record */
340 1.10 bjh21 written = fwrite(&temp_record, 1,
341 1.10 bjh21 temp->isoDirRecord->length[0], fd);
342 1.16 christos if (diskStructure->rock_ridge_enabled) {
343 1.16 christos cd9660_write_rr(diskStructure, fd, temp,
344 1.1 fvdl cur_sector_offset, working_sector);
345 1.1 fvdl }
346 1.13 christos if (fseeko(fd, working_sector *
347 1.16 christos diskStructure->sectorSize + cur_sector_offset +
348 1.13 christos temp_record.length[0] - temp->su_tail_size,
349 1.13 christos SEEK_SET) == -1)
350 1.13 christos err(1, "fseeko");
351 1.10 bjh21 if (temp->su_tail_size > 0)
352 1.10 bjh21 fwrite(temp->su_tail_data, 1,
353 1.10 bjh21 temp->su_tail_size, fd);
354 1.1 fvdl if (ferror(fd)) {
355 1.4 dyoung warnx("%s: write error", __func__);
356 1.8 christos goto out;
357 1.1 fvdl }
358 1.1 fvdl cur_sector_offset += temp_record.length[0];
359 1.1 fvdl
360 1.1 fvdl }
361 1.3 dyoung
362 1.1 fvdl /*
363 1.2 dyoung * Recurse on children.
364 1.1 fvdl */
365 1.2 dyoung TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) {
366 1.16 christos if ((ret = cd9660_write_file(diskStructure, fd, temp))
367 1.16 christos == 0)
368 1.8 christos goto out;
369 1.1 fvdl }
370 1.1 fvdl }
371 1.8 christos rv = 1;
372 1.8 christos out:
373 1.1 fvdl free(temp_file_name);
374 1.8 christos free(buf);
375 1.8 christos return rv;
376 1.1 fvdl }
377 1.1 fvdl
378 1.1 fvdl /*
379 1.1 fvdl * Wrapper function to write a buffer (one sector) to disk.
380 1.1 fvdl * Seeks and writes the buffer.
381 1.1 fvdl * NOTE: You dont NEED to use this function, but it might make your
382 1.1 fvdl * life easier if you have to write things that align to a sector
383 1.1 fvdl * (such as volume descriptors).
384 1.1 fvdl *
385 1.1 fvdl * @param int fd Valid file descriptor
386 1.1 fvdl * @param int sector Sector number to write to
387 1.1 fvdl * @param const unsigned char* Buffer to write. This should be the
388 1.1 fvdl * size of a sector, and if only a portion
389 1.1 fvdl * is written, the rest should be set to 0.
390 1.1 fvdl */
391 1.1 fvdl static int
392 1.16 christos cd9660_write_filedata(iso9660_disk *diskStructure, FILE *fd, off_t sector,
393 1.16 christos const unsigned char *buf, int numsecs)
394 1.1 fvdl {
395 1.1 fvdl off_t curpos;
396 1.1 fvdl size_t success;
397 1.1 fvdl
398 1.1 fvdl curpos = ftello(fd);
399 1.3 dyoung
400 1.16 christos if (fseeko(fd, sector * diskStructure->sectorSize, SEEK_SET) == -1)
401 1.13 christos err(1, "fseeko");
402 1.1 fvdl
403 1.16 christos success = fwrite(buf, diskStructure->sectorSize * numsecs, 1, fd);
404 1.1 fvdl
405 1.13 christos if (fseeko(fd, curpos, SEEK_SET) == -1)
406 1.13 christos err(1, "fseeko");
407 1.1 fvdl
408 1.1 fvdl if (success == 1)
409 1.16 christos success = diskStructure->sectorSize * numsecs;
410 1.1 fvdl return success;
411 1.1 fvdl }
412 1.1 fvdl
413 1.1 fvdl #if 0
414 1.1 fvdl static int
415 1.13 christos cd9660_write_buffered(FILE *fd, off_t offset, int buff_len,
416 1.1 fvdl const unsigned char* buffer)
417 1.1 fvdl {
418 1.1 fvdl static int working_sector = -1;
419 1.13 christos static char buf[CD9660_SECTOR_SIZE];
420 1.3 dyoung
421 1.1 fvdl return 0;
422 1.1 fvdl }
423 1.1 fvdl #endif
424 1.1 fvdl
425 1.1 fvdl int
426 1.16 christos cd9660_copy_file(iso9660_disk *diskStructure, FILE *fd, off_t start_sector,
427 1.16 christos const char *filename)
428 1.1 fvdl {
429 1.1 fvdl FILE *rf;
430 1.1 fvdl int bytes_read;
431 1.13 christos off_t sector = start_sector;
432 1.16 christos int buf_size = diskStructure->sectorSize;
433 1.1 fvdl char *buf;
434 1.1 fvdl
435 1.16 christos buf = emalloc(buf_size);
436 1.1 fvdl if ((rf = fopen(filename, "rb")) == NULL) {
437 1.4 dyoung warn("%s: cannot open %s", __func__, filename);
438 1.7 riz free(buf);
439 1.3 dyoung return 0;
440 1.1 fvdl }
441 1.1 fvdl
442 1.16 christos if (diskStructure->verbose_level > 1)
443 1.1 fvdl printf("Writing file: %s\n",filename);
444 1.3 dyoung
445 1.16 christos if (fseeko(fd, start_sector * diskStructure->sectorSize, SEEK_SET) == -1)
446 1.13 christos err(1, "fseeko");
447 1.3 dyoung
448 1.1 fvdl while (!feof(rf)) {
449 1.1 fvdl bytes_read = fread(buf,1,buf_size,rf);
450 1.1 fvdl if (ferror(rf)) {
451 1.4 dyoung warn("%s: fread", __func__);
452 1.7 riz free(buf);
453 1.14 wiz (void)fclose(rf);
454 1.3 dyoung return 0;
455 1.1 fvdl }
456 1.3 dyoung
457 1.1 fvdl fwrite(buf,1,bytes_read,fd);
458 1.1 fvdl if (ferror(fd)) {
459 1.4 dyoung warn("%s: fwrite", __func__);
460 1.7 riz free(buf);
461 1.14 wiz (void)fclose(rf);
462 1.3 dyoung return 0;
463 1.1 fvdl }
464 1.1 fvdl sector++;
465 1.1 fvdl }
466 1.3 dyoung
467 1.1 fvdl fclose(rf);
468 1.1 fvdl free(buf);
469 1.1 fvdl return 1;
470 1.1 fvdl }
471 1.1 fvdl
472 1.10 bjh21 static void
473 1.16 christos cd9660_write_rr(iso9660_disk *diskStructure, FILE *fd, cd9660node *writenode,
474 1.16 christos off_t offset, off_t sector)
475 1.1 fvdl {
476 1.1 fvdl int in_ca = 0;
477 1.1 fvdl struct ISO_SUSP_ATTRIBUTES *myattr;
478 1.1 fvdl
479 1.1 fvdl offset += writenode->isoDirRecord->length[0];
480 1.16 christos if (fseeko(fd, sector * diskStructure->sectorSize + offset, SEEK_SET) ==
481 1.13 christos -1)
482 1.13 christos err(1, "fseeko");
483 1.1 fvdl /* Offset now points at the end of the record */
484 1.2 dyoung TAILQ_FOREACH(myattr, &writenode->head, rr_ll) {
485 1.1 fvdl fwrite(&(myattr->attr), CD9660_SUSP_ENTRY_SIZE(myattr), 1, fd);
486 1.1 fvdl
487 1.1 fvdl if (!in_ca) {
488 1.10 bjh21 offset += CD9660_SUSP_ENTRY_SIZE(myattr);
489 1.10 bjh21 if (myattr->last_in_suf) {
490 1.1 fvdl /*
491 1.1 fvdl * Point the offset to the start of this
492 1.1 fvdl * record's CE area
493 1.1 fvdl */
494 1.16 christos if (fseeko(fd, ((off_t)diskStructure->
495 1.13 christos susp_continuation_area_start_sector *
496 1.16 christos diskStructure->sectorSize)
497 1.10 bjh21 + writenode->susp_entry_ce_start,
498 1.13 christos SEEK_SET) == -1)
499 1.13 christos err(1, "fseeko");
500 1.1 fvdl in_ca = 1;
501 1.1 fvdl }
502 1.1 fvdl }
503 1.1 fvdl }
504 1.1 fvdl
505 1.10 bjh21 /*
506 1.12 mbalmer * If we had to go to the continuation area, head back to
507 1.10 bjh21 * where we should be.
508 1.10 bjh21 */
509 1.10 bjh21 if (in_ca)
510 1.16 christos if (fseeko(fd, sector * diskStructure->sectorSize + offset,
511 1.13 christos SEEK_SET) == -1)
512 1.13 christos err(1, "fseeko");
513 1.1 fvdl }
514