1 /* $NetBSD: cd9660_write.c,v 1.14 2011/01/04 09:48:21 wiz Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
6 * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
7 * Perez-Rathke and Ram Vedam. All rights reserved.
9 * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
10 * Alan Perez-Rathke and Ram Vedam.
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
23 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
27 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
38 #include "iso9660_rrip.h"
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
45 static int cd9660_write_volume_descriptors(iso9660_disk *, FILE *);
46 static int cd9660_write_path_table(iso9660_disk *, FILE *, off_t, int);
47 static int cd9660_write_path_tables(iso9660_disk *, FILE *);
48 static int cd9660_write_file(iso9660_disk *, FILE *, cd9660node *);
49 static int cd9660_write_filedata(iso9660_disk *, FILE *, off_t,
50 const unsigned char *, int);
52 static int cd9660_write_buffered(FILE *, off_t, int, const unsigned char *);
54 static void cd9660_write_rr(iso9660_disk *, FILE *, cd9660node *, off_t, off_t);
58 * Writes the entire image
59 * @param const char* The filename for the image
60 * @returns int 1 on success, 0 on failure
63 cd9660_write_image(iso9660_disk *diskStructure, const char* image)
67 char buf[CD9660_SECTOR_SIZE];
69 if ((fd = fopen(image, "w+")) == NULL) {
70 err(EXIT_FAILURE, "%s: Can't open `%s' for writing", __func__,
74 if (diskStructure->verbose_level > 0)
75 printf("Writing image\n");
77 if (diskStructure->has_generic_bootimage) {
78 status = cd9660_copy_file(diskStructure, fd, 0,
79 diskStructure->generic_bootimage);
81 warnx("%s: Error writing generic boot image",
83 goto cleanup_bad_image;
87 /* Write the volume descriptors */
88 status = cd9660_write_volume_descriptors(diskStructure, fd);
90 warnx("%s: Error writing volume descriptors to image",
92 goto cleanup_bad_image;
95 if (diskStructure->verbose_level > 0)
96 printf("Volume descriptors written\n");
99 * Write the path tables: there are actually four, but right
100 * now we are only concearned with two.
102 status = cd9660_write_path_tables(diskStructure, fd);
104 warnx("%s: Error writing path tables to image", __func__);
105 goto cleanup_bad_image;
108 if (diskStructure->verbose_level > 0)
109 printf("Path tables written\n");
111 /* Write the directories and files */
112 status = cd9660_write_file(diskStructure, fd, diskStructure->rootNode);
114 warnx("%s: Error writing files to image", __func__);
115 goto cleanup_bad_image;
118 if (diskStructure->is_bootable) {
119 cd9660_write_boot(diskStructure, fd);
122 /* Write padding bits. This is temporary */
123 memset(buf, 0, CD9660_SECTOR_SIZE);
124 cd9660_write_filedata(diskStructure, fd,
125 diskStructure->totalSectors - 1, buf, 1);
127 if (diskStructure->verbose_level > 0)
128 printf("Files written\n");
131 if (diskStructure->verbose_level > 0)
132 printf("Image closed\n");
137 if (!diskStructure->keep_bad_images)
139 if (diskStructure->verbose_level > 0)
140 printf("Bad image cleaned up\n");
145 cd9660_write_volume_descriptors(iso9660_disk *diskStructure, FILE *fd)
147 volume_descriptor *vd_temp = diskStructure->firstVolumeDescriptor;
149 while (vd_temp != NULL) {
150 cd9660_write_filedata(diskStructure, fd, vd_temp->sector,
151 vd_temp->volumeDescriptorData, 1);
152 vd_temp = vd_temp->next;
158 * Write out an individual path table
159 * Used just to keep redundant code to a minimum
160 * @param FILE *fd Valid file pointer
161 * @param int Sector to start writing path table to
162 * @param int Endian mode : BIG_ENDIAN or LITTLE_ENDIAN
163 * @returns int 1 on success, 0 on failure
166 cd9660_write_path_table(iso9660_disk *diskStructure, FILE *fd, off_t sector,
169 int path_table_sectors = CD9660_BLOCKS(diskStructure->sectorSize,
170 diskStructure->pathTableLength);
171 unsigned char *buffer;
172 unsigned char *buffer_head;
174 path_table_entry temp_entry;
177 buffer = ecalloc(path_table_sectors, diskStructure->sectorSize);
178 buffer_head = buffer;
180 ptcur = diskStructure->rootNode;
182 while (ptcur != NULL) {
183 memset(&temp_entry, 0, sizeof(path_table_entry));
184 temp_entry.length[0] = ptcur->isoDirRecord->name_len[0];
185 temp_entry.extended_attribute_length[0] =
186 ptcur->isoDirRecord->ext_attr_length[0];
187 memcpy(temp_entry.name, ptcur->isoDirRecord->name,
188 temp_entry.length[0] + 1);
191 len = temp_entry.length[0] + 8 + (temp_entry.length[0] & 0x01);
193 /* todo: function pointers instead */
194 if (mode == LITTLE_ENDIAN) {
195 cd9660_731(ptcur->fileDataSector,
196 temp_entry.first_sector);
197 cd9660_721((ptcur->parent == NULL ?
198 1 : ptcur->parent->ptnumber),
199 temp_entry.parent_number);
201 cd9660_732(ptcur->fileDataSector,
202 temp_entry.first_sector);
203 cd9660_722((ptcur->parent == NULL ?
204 1 : ptcur->parent->ptnumber),
205 temp_entry.parent_number);
209 memcpy(buffer, &temp_entry, len);
212 ptcur = ptcur->ptnext;
215 ret = cd9660_write_filedata(diskStructure, fd, sector, buffer_head,
223 * Write out the path tables to disk
224 * Each file descriptor should be pointed to by the PVD, so we know which
225 * sector to copy them to. One thing to watch out for: the only path tables
226 * stored are in the endian mode that the application is compiled for. So,
227 * the first thing to do is write out that path table, then to write the one
228 * in the other endian mode requires to convert the endianness of each entry
229 * in the table. The best way to do this would be to create a temporary
230 * path_table_entry structure, then for each path table entry, copy it to
231 * the temporary entry, translate, then copy that to disk.
233 * @param FILE* Valid file descriptor
234 * @returns int 0 on failure, 1 on success
237 cd9660_write_path_tables(iso9660_disk *diskStructure, FILE *fd)
239 if (cd9660_write_path_table(diskStructure, fd,
240 diskStructure->primaryLittleEndianTableSector, LITTLE_ENDIAN) == 0)
243 if (cd9660_write_path_table(diskStructure, fd,
244 diskStructure->primaryBigEndianTableSector, BIG_ENDIAN) == 0)
247 /* @TODO: handle remaining two path tables */
252 * Write a file to disk
253 * Writes a file, its directory record, and its data to disk
254 * This file is designed to be called RECURSIVELY, so initially call it
255 * with the root node. All of the records should store what sector the
256 * file goes in, so no computation should be necessary.
258 * @param int fd Valid file descriptor
259 * @param struct cd9660node* writenode Pointer to the file to be written
260 * @returns int 0 on failure, 1 on success
263 cd9660_write_file(iso9660_disk *diskStructure, FILE *fd, cd9660node *writenode)
266 char *temp_file_name;
268 off_t working_sector;
269 int cur_sector_offset;
270 iso_directory_record_cd9660 temp_record;
274 /* Todo : clean up variables */
276 temp_file_name = ecalloc(CD9660MAXPATH + 1, 1);
277 buf = emalloc(diskStructure->sectorSize);
278 if ((writenode->level != 0) &&
279 !(writenode->node->type & S_IFDIR)) {
280 fsinode *inode = writenode->node->inode;
281 /* Only attempt to write unwritten files that have length. */
282 if ((inode->flags & FI_WRITTEN) != 0) {
283 INODE_WARNX(("%s: skipping written inode %d", __func__,
284 (int)inode->st.st_ino));
285 } else if (writenode->fileDataLength > 0) {
286 INODE_WARNX(("%s: writing inode %d blocks at %" PRIu32,
287 __func__, (int)inode->st.st_ino, inode->ino));
288 inode->flags |= FI_WRITTEN;
289 if (writenode->node->contents == NULL)
290 cd9660_compute_full_filename(writenode,
292 ret = cd9660_copy_file(diskStructure, fd,
293 writenode->fileDataSector,
294 (writenode->node->contents != NULL) ?
295 writenode->node->contents : temp_file_name);
301 * Here is a new revelation that ECMA didn't explain
302 * (at least not well).
303 * ALL . and .. records store the name "\0" and "\1"
304 * respectively. So, for each directory, we have to
307 * This is where it gets kinda messy, since we have to
308 * be careful of sector boundaries
310 cur_sector_offset = 0;
311 working_sector = writenode->fileDataSector;
312 if (fseeko(fd, working_sector * diskStructure->sectorSize,
317 * Now loop over children, writing out their directory
318 * records - beware of sector boundaries
320 TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) {
322 * Copy the temporary record and adjust its size
325 memcpy(&temp_record, temp->isoDirRecord,
326 sizeof(iso_directory_record_cd9660));
328 temp_record.length[0] =
329 cd9660_compute_record_size(diskStructure, temp);
331 if (temp_record.length[0] + cur_sector_offset >=
332 diskStructure->sectorSize) {
333 cur_sector_offset = 0;
336 /* Seek to the next sector. */
337 if (fseeko(fd, working_sector *
338 diskStructure->sectorSize, SEEK_SET) == -1)
341 /* Write out the basic ISO directory record */
342 (void)fwrite(&temp_record, 1,
343 temp->isoDirRecord->length[0], fd);
344 if (diskStructure->rock_ridge_enabled) {
345 cd9660_write_rr(diskStructure, fd, temp,
346 cur_sector_offset, working_sector);
348 if (fseeko(fd, working_sector *
349 diskStructure->sectorSize + cur_sector_offset +
350 temp_record.length[0] - temp->su_tail_size,
353 if (temp->su_tail_size > 0)
354 fwrite(temp->su_tail_data, 1,
355 temp->su_tail_size, fd);
357 warnx("%s: write error", __func__);
360 cur_sector_offset += temp_record.length[0];
365 * Recurse on children.
367 TAILQ_FOREACH(temp, &writenode->cn_children, cn_next_child) {
368 if ((ret = cd9660_write_file(diskStructure, fd, temp)) == 0)
374 free(temp_file_name);
380 * Wrapper function to write a buffer (one sector) to disk.
381 * Seeks and writes the buffer.
382 * NOTE: You dont NEED to use this function, but it might make your
383 * life easier if you have to write things that align to a sector
384 * (such as volume descriptors).
386 * @param int fd Valid file descriptor
387 * @param int sector Sector number to write to
388 * @param const unsigned char* Buffer to write. This should be the
389 * size of a sector, and if only a portion
390 * is written, the rest should be set to 0.
393 cd9660_write_filedata(iso9660_disk *diskStructure, FILE *fd, off_t sector,
394 const unsigned char *buf, int numsecs)
401 if (fseeko(fd, sector * diskStructure->sectorSize, SEEK_SET) == -1)
404 success = fwrite(buf, diskStructure->sectorSize * numsecs, 1, fd);
406 if (fseeko(fd, curpos, SEEK_SET) == -1)
410 success = diskStructure->sectorSize * numsecs;
416 cd9660_write_buffered(FILE *fd, off_t offset, int buff_len,
417 const unsigned char* buffer)
419 static int working_sector = -1;
420 static char buf[CD9660_SECTOR_SIZE];
427 cd9660_copy_file(iso9660_disk *diskStructure, FILE *fd, off_t start_sector,
428 const char *filename)
432 off_t sector = start_sector;
433 int buf_size = diskStructure->sectorSize;
436 buf = emalloc(buf_size);
437 if ((rf = fopen(filename, "rb")) == NULL) {
438 warn("%s: cannot open %s", __func__, filename);
443 if (diskStructure->verbose_level > 1)
444 printf("Writing file: %s\n",filename);
446 if (fseeko(fd, start_sector * diskStructure->sectorSize, SEEK_SET) == -1)
450 bytes_read = fread(buf,1,buf_size,rf);
452 warn("%s: fread", __func__);
458 fwrite(buf,1,bytes_read,fd);
460 warn("%s: fwrite", __func__);
474 cd9660_write_rr(iso9660_disk *diskStructure, FILE *fd, cd9660node *writenode,
475 off_t offset, off_t sector)
478 struct ISO_SUSP_ATTRIBUTES *myattr;
480 offset += writenode->isoDirRecord->length[0];
481 if (fseeko(fd, sector * diskStructure->sectorSize + offset, SEEK_SET) ==
484 /* Offset now points at the end of the record */
485 TAILQ_FOREACH(myattr, &writenode->head, rr_ll) {
486 fwrite(&(myattr->attr), CD9660_SUSP_ENTRY_SIZE(myattr), 1, fd);
489 offset += CD9660_SUSP_ENTRY_SIZE(myattr);
490 if (myattr->last_in_suf) {
492 * Point the offset to the start of this
495 if (fseeko(fd, ((off_t)diskStructure->
496 susp_continuation_area_start_sector *
497 diskStructure->sectorSize)
498 + writenode->susp_entry_ce_start,
507 * If we had to go to the continuation area, head back to
508 * where we should be.
511 if (fseeko(fd, sector * diskStructure->sectorSize + offset,