xref: /titanic_53/usr/src/cmd/cdrw/copycd.c (revision 0c83a891bb3029b854c3ebe4d0467b839a68fa49)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*0c83a891Snakanon  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <limits.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <libintl.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "main.h"
387c478bd9Sstevel@tonic-gate #include "util.h"
397c478bd9Sstevel@tonic-gate #include "misc_scsi.h"
407c478bd9Sstevel@tonic-gate #include "mmc.h"
417c478bd9Sstevel@tonic-gate #include "bstream.h"
427c478bd9Sstevel@tonic-gate #include "device.h"
437c478bd9Sstevel@tonic-gate #include "msgs.h"
447c478bd9Sstevel@tonic-gate #include "transport.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate struct t_data {
477c478bd9Sstevel@tonic-gate 	bstreamhandle h;
487c478bd9Sstevel@tonic-gate 	struct track_info ti;
497c478bd9Sstevel@tonic-gate };
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate int read_audio_track(cd_device *dev, struct track_info *ti, bstreamhandle h);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	READ_BURST	24	/* < 64K in all cases */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * This reads the data off of a cd while updating the progress indicator.
577c478bd9Sstevel@tonic-gate  * We want to do this in smaller chunks since some CD drives have
587c478bd9Sstevel@tonic-gate  * problems with larger reads.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate static int
617c478bd9Sstevel@tonic-gate read_data_track(cd_device *dev, struct track_info *ti, bstreamhandle h)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	int blksize;
647c478bd9Sstevel@tonic-gate 	uint32_t blks_read, cblk, read_chunk, read_size;
657c478bd9Sstevel@tonic-gate 	uchar_t *buf;
667c478bd9Sstevel@tonic-gate 	int ret, sav;
677c478bd9Sstevel@tonic-gate 	int link_blks_count;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	buf = NULL;
707c478bd9Sstevel@tonic-gate 	ret = 0;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	/*
737c478bd9Sstevel@tonic-gate 	 * the last link_blks_count blocks may not exist or be completely
747c478bd9Sstevel@tonic-gate 	 * filled. We need to record the amount to avoid bailing out if
757c478bd9Sstevel@tonic-gate 	 * they cannot be read.
767c478bd9Sstevel@tonic-gate 	 */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	if (dev->d_blksize == 512) {
797c478bd9Sstevel@tonic-gate 		blksize = 512;
807c478bd9Sstevel@tonic-gate 		link_blks_count = 8;
817c478bd9Sstevel@tonic-gate 	} else {
827c478bd9Sstevel@tonic-gate 		blksize = 2048;
837c478bd9Sstevel@tonic-gate 		link_blks_count = 2;
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	buf = (uchar_t *)my_zalloc(READ_BURST * blksize);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	print_n_flush(gettext("Reading track %d..."), ti->ti_track_no);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if (verbose)
917c478bd9Sstevel@tonic-gate 		print_n_flush("Track size is %u...", ti->ti_track_size);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	init_progress();
947c478bd9Sstevel@tonic-gate 	cblk = ti->ti_start_address;
957c478bd9Sstevel@tonic-gate 	blks_read = 0;
967c478bd9Sstevel@tonic-gate 	while (blks_read < ti->ti_track_size) {
977c478bd9Sstevel@tonic-gate 		/* Last few are special */
987c478bd9Sstevel@tonic-gate 		read_chunk = ti->ti_track_size - blks_read - link_blks_count;
997c478bd9Sstevel@tonic-gate 		read_chunk = (read_chunk > READ_BURST) ? READ_BURST :
1007c478bd9Sstevel@tonic-gate 		    read_chunk;
1017c478bd9Sstevel@tonic-gate 		if (read_chunk == 0) {
1027c478bd9Sstevel@tonic-gate 			/* Time for last link blocks */
1037c478bd9Sstevel@tonic-gate 			read_chunk = link_blks_count;
1047c478bd9Sstevel@tonic-gate 		}
1057c478bd9Sstevel@tonic-gate 		read_size = read_chunk * blksize;
1067c478bd9Sstevel@tonic-gate 		if (read10(dev->d_fd, cblk, read_chunk, buf, read_size)) {
1077c478bd9Sstevel@tonic-gate 			if (h->bstr_write(h, buf, read_size) != read_size) {
1087c478bd9Sstevel@tonic-gate 				goto read_data_track_failed;
1097c478bd9Sstevel@tonic-gate 			}
1107c478bd9Sstevel@tonic-gate 		} else {
1117c478bd9Sstevel@tonic-gate 			if (blks_read !=
1127c478bd9Sstevel@tonic-gate 			    (ti->ti_track_size - link_blks_count)) {
1137c478bd9Sstevel@tonic-gate 				goto read_data_track_failed;
1147c478bd9Sstevel@tonic-gate 			} else {
1157c478bd9Sstevel@tonic-gate 			/* Read can fail for last link sectors */
1167c478bd9Sstevel@tonic-gate 				errno = 0;
1177c478bd9Sstevel@tonic-gate 			}
1187c478bd9Sstevel@tonic-gate 		}
1197c478bd9Sstevel@tonic-gate 		blks_read += read_chunk;
1207c478bd9Sstevel@tonic-gate 		cblk += read_chunk;
121*0c83a891Snakanon 		(void) progress((ti->ti_track_size), blks_read);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : 'done' as in "Reading track 1...done"  */
1247c478bd9Sstevel@tonic-gate 	(void) str_print(gettext("done.\n"), progress_pos);
1257c478bd9Sstevel@tonic-gate 	ret = 1;
1267c478bd9Sstevel@tonic-gate read_data_track_failed:
1277c478bd9Sstevel@tonic-gate 	sav = errno;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	free(buf);
1307c478bd9Sstevel@tonic-gate 	errno = sav;
1317c478bd9Sstevel@tonic-gate 	return (ret);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate static void
1357c478bd9Sstevel@tonic-gate ensure_media_space(uint32_t total_nblks, uchar_t end_tno)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	off_t nblks_avail;
1387c478bd9Sstevel@tonic-gate 	uint_t bsize;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	get_media_type(target->d_fd);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	if (use_media_stated_capacity) {
1437c478bd9Sstevel@tonic-gate 		nblks_avail = get_last_possible_lba(target);
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 		if (nblks_avail <= 0) {
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 			/* most newer drives use READ FORMAT CAPACITY */
1487c478bd9Sstevel@tonic-gate 			nblks_avail = read_format_capacity(target->d_fd,
1497c478bd9Sstevel@tonic-gate 			    &bsize);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 			/* if both methods fail no choice but to bail out */
1527c478bd9Sstevel@tonic-gate 			if (nblks_avail <= 0) {
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 				err_msg(gettext(
1557c478bd9Sstevel@tonic-gate 				    "Cannot find out media capacity.\n"));
1567c478bd9Sstevel@tonic-gate 				exit(1);
1577c478bd9Sstevel@tonic-gate 			}
1587c478bd9Sstevel@tonic-gate 		}
1597c478bd9Sstevel@tonic-gate 	} else {
1607c478bd9Sstevel@tonic-gate 		if (device_type == CD_RW) {
1617c478bd9Sstevel@tonic-gate 			nblks_avail = MAX_CD_BLKS;
1627c478bd9Sstevel@tonic-gate 		} else {
1637c478bd9Sstevel@tonic-gate 			/*
1647c478bd9Sstevel@tonic-gate 			 * For DVD drives use read_format_capacity as default
1657c478bd9Sstevel@tonic-gate 			 * retrieve the media size, it can be 3.6, 3.9, 4.2,
1667c478bd9Sstevel@tonic-gate 			 * 4.7, or 9.2 GB
1677c478bd9Sstevel@tonic-gate 			 */
1687c478bd9Sstevel@tonic-gate 			nblks_avail =
1697c478bd9Sstevel@tonic-gate 			    read_format_capacity(target->d_fd, &bsize);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 			/* sanity check. if not reasonable default to 4.7 GB */
1727c478bd9Sstevel@tonic-gate 			if (nblks_avail < MAX_CD_BLKS) {
1737c478bd9Sstevel@tonic-gate 				nblks_avail = MAX_DVD_BLKS;
1747c478bd9Sstevel@tonic-gate 			}
1757c478bd9Sstevel@tonic-gate 		}
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if ((total_nblks + end_tno*300) > nblks_avail) {
1797c478bd9Sstevel@tonic-gate 		err_msg(gettext("Not enough space on the media.\n"));
1807c478bd9Sstevel@tonic-gate 		if (debug) {
1817c478bd9Sstevel@tonic-gate 			(void) printf("Need %u  only found %u \n",
1827c478bd9Sstevel@tonic-gate 			    (total_nblks + end_tno*300),
1837c478bd9Sstevel@tonic-gate 			    (uint32_t)nblks_avail);
1847c478bd9Sstevel@tonic-gate 		}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 		exit(1);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * This copies both audio and data CDs. It first reads the TOC of the source CD
1927c478bd9Sstevel@tonic-gate  * and creates a temp file with the CD image. After this is completed it creates
1937c478bd9Sstevel@tonic-gate  * the target CD using TAO mode.
1947c478bd9Sstevel@tonic-gate  */
1957c478bd9Sstevel@tonic-gate void
1967c478bd9Sstevel@tonic-gate copy_cd(void)
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate 	cd_device *src;
1997c478bd9Sstevel@tonic-gate 	char *p;
2007c478bd9Sstevel@tonic-gate 	uchar_t *toc, end_tno;
2017c478bd9Sstevel@tonic-gate 	int blksize, i;
2027c478bd9Sstevel@tonic-gate 	int audio_cd, data_cd;
2037c478bd9Sstevel@tonic-gate 	uint32_t total_nblks;
2047c478bd9Sstevel@tonic-gate 	int ret;
2057c478bd9Sstevel@tonic-gate 	struct t_data *tlist;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	print_n_flush(gettext("Analyzing source CD..."));
2087c478bd9Sstevel@tonic-gate 	(void) check_device(target,
2097c478bd9Sstevel@tonic-gate 	    CHECK_DEVICE_NOT_WRITABLE|EXIT_IF_CHECK_FAILED);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	/* if source drive is specified on the command line */
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if (copy_src) {
2147c478bd9Sstevel@tonic-gate 		p = my_zalloc(PATH_MAX);
2157c478bd9Sstevel@tonic-gate 		if (lookup_device(copy_src, p) == 0) {
2167c478bd9Sstevel@tonic-gate 			err_msg(gettext("Cannot find device %s"), copy_src);
2177c478bd9Sstevel@tonic-gate 			err_msg(gettext(" or no media in the drive\n"));
2187c478bd9Sstevel@tonic-gate 			exit(1);
2197c478bd9Sstevel@tonic-gate 		}
2207c478bd9Sstevel@tonic-gate 		src = get_device(copy_src, p);
2217c478bd9Sstevel@tonic-gate 		if (src == NULL) {
2227c478bd9Sstevel@tonic-gate 			err_msg(gettext("Unable to open %s\n"), copy_src);
2237c478bd9Sstevel@tonic-gate 			exit(1);
2247c478bd9Sstevel@tonic-gate 		}
2257c478bd9Sstevel@tonic-gate 		free(p);
2267c478bd9Sstevel@tonic-gate 	} else {
2277c478bd9Sstevel@tonic-gate 		/* source is same as target drive */
2287c478bd9Sstevel@tonic-gate 		src = target;
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	(void) check_device(src, CHECK_TYPE_NOT_CDROM | CHECK_NO_MEDIA |
2327c478bd9Sstevel@tonic-gate 	    CHECK_DEVICE_NOT_READY | EXIT_IF_CHECK_FAILED);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	toc = (uchar_t *)my_zalloc(4);
2357c478bd9Sstevel@tonic-gate 	if (!read_toc(src->d_fd, 0, 0, 4, toc)) {
2367c478bd9Sstevel@tonic-gate 		err_msg(gettext("Cannot read table of contents\n"));
2377c478bd9Sstevel@tonic-gate 		exit(1);
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 	end_tno = toc[3];
2407c478bd9Sstevel@tonic-gate 	free(toc);
2417c478bd9Sstevel@tonic-gate 	tlist = (struct t_data *)my_zalloc(end_tno * sizeof (struct t_data));
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	audio_cd = data_cd = 0;
2447c478bd9Sstevel@tonic-gate 	total_nblks = 0;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	/* build track information so we can copy it over */
2477c478bd9Sstevel@tonic-gate 	for (i = 1; i <= end_tno; i++) {
2487c478bd9Sstevel@tonic-gate 		struct track_info *ti;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 		ti = &tlist[i - 1].ti;
2517c478bd9Sstevel@tonic-gate 		if (!build_track_info(src, i, ti)) {
2527c478bd9Sstevel@tonic-gate 			err_msg(gettext(
2537c478bd9Sstevel@tonic-gate 			    "Cannot get information for track %d\n"), i);
2547c478bd9Sstevel@tonic-gate 			exit(1);
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate 		total_nblks += ti->ti_track_size;
2577c478bd9Sstevel@tonic-gate 		if (ti->ti_track_mode & 4)
2587c478bd9Sstevel@tonic-gate 			data_cd = 1;
2597c478bd9Sstevel@tonic-gate 		else
2607c478bd9Sstevel@tonic-gate 			audio_cd = 1;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 		/* Now some sanity checks on the track information */
2637c478bd9Sstevel@tonic-gate 		if ((ti->ti_flags & TI_SESSION_NO_VALID) &&
2647c478bd9Sstevel@tonic-gate 		    (ti->ti_session_no != 1)) {
2657c478bd9Sstevel@tonic-gate 			err_msg(
2667c478bd9Sstevel@tonic-gate 			gettext("Copying multisession CD is not supported\n"));
2677c478bd9Sstevel@tonic-gate 			exit(1);
2687c478bd9Sstevel@tonic-gate 		}
2697c478bd9Sstevel@tonic-gate 		if ((ti->ti_flags & TI_PACKET_MODE) ||
2707c478bd9Sstevel@tonic-gate 		    (ti->ti_flags & TI_BLANK_TRACK) ||
2717c478bd9Sstevel@tonic-gate 		    (ti->ti_flags & TI_DAMAGED_TRACK) ||
2727c478bd9Sstevel@tonic-gate 		    (data_cd && audio_cd) || (ti->ti_data_mode == 2)) {
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		    err_msg(gettext("CD format is not supported\n"));
2757c478bd9Sstevel@tonic-gate 			exit(1);
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 		if ((ti->ti_flags & TI_NWA_VALID) &&
2787c478bd9Sstevel@tonic-gate 		    (ti->ti_nwa != 0xffffffff)) {
2797c478bd9Sstevel@tonic-gate 			err_msg(gettext("Cannot copy incomplete discs\n"));
2807c478bd9Sstevel@tonic-gate 			exit(1);
2817c478bd9Sstevel@tonic-gate 		}
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : 'done' as in "Analyzing source CD...done"  */
2847c478bd9Sstevel@tonic-gate 	(void) printf(gettext("done.\n"));
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	if (data_cd) {
2877c478bd9Sstevel@tonic-gate 		blksize = 2048;
2887c478bd9Sstevel@tonic-gate 	} else {
2897c478bd9Sstevel@tonic-gate 		/* audio cd */
2907c478bd9Sstevel@tonic-gate 		blksize = 2352;
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	/* In case of audio CDs, build_track_info() returns 2352 sized nblks */
2947c478bd9Sstevel@tonic-gate 	if (src->d_blksize == 512 && data_cd) {
2957c478bd9Sstevel@tonic-gate 		total_nblks /= 4;
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\nCopying %d %s track%s : %ld kbytes\n\n"),
2987c478bd9Sstevel@tonic-gate 	    end_tno, (audio_cd == 1) ? gettext("audio") : gettext("data"),
2997c478bd9Sstevel@tonic-gate 	    (end_tno > 1) ? "s" : "", (long)((total_nblks*blksize)/1024));
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if (!check_avail_temp_space(total_nblks*blksize)) {
3027c478bd9Sstevel@tonic-gate 		err_msg(gettext("Not enough space in temporary directory\n"));
3037c478bd9Sstevel@tonic-gate 		err_msg(
3047c478bd9Sstevel@tonic-gate 		gettext("Use -m to specify alternate temporary directory\n"));
3057c478bd9Sstevel@tonic-gate 		exit(1);
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	/*
3097c478bd9Sstevel@tonic-gate 	 * If we can check available space on the target media at this
3107c478bd9Sstevel@tonic-gate 	 * Stage, then it is always better. We cannot check DVD+R(W)
3117c478bd9Sstevel@tonic-gate 	 * as this media may be formatted and not blank.
3127c478bd9Sstevel@tonic-gate 	 */
3137c478bd9Sstevel@tonic-gate 	if (target && (src != target) && (device_type != DVD_PLUS) &&
3147c478bd9Sstevel@tonic-gate 	    (device_type != DVD_PLUS_W) && (!check_device(target,
3157c478bd9Sstevel@tonic-gate 	    CHECK_NO_MEDIA|CHECK_MEDIA_IS_NOT_BLANK))) {
3167c478bd9Sstevel@tonic-gate 		ensure_media_space(total_nblks, end_tno);
3177c478bd9Sstevel@tonic-gate 	}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/* for each track */
3207c478bd9Sstevel@tonic-gate 	for (i = 1; i <= end_tno; i++) {
3217c478bd9Sstevel@tonic-gate 		tlist[i - 1].h = open_temp_file_stream();
3227c478bd9Sstevel@tonic-gate 		if (tlist[i - 1].h == NULL) {
3237c478bd9Sstevel@tonic-gate 			err_msg(gettext("Cannot create temporary file : %s\n"),
3247c478bd9Sstevel@tonic-gate 			    get_err_str());
3257c478bd9Sstevel@tonic-gate 			exit(1);
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 		if (audio_cd)
3297c478bd9Sstevel@tonic-gate 			ret = read_audio_track(src, &tlist[i - 1].ti,
3307c478bd9Sstevel@tonic-gate 			    tlist[i - 1].h);
3317c478bd9Sstevel@tonic-gate 		else
3327c478bd9Sstevel@tonic-gate 			ret = read_data_track(src, &tlist[i - 1].ti,
3337c478bd9Sstevel@tonic-gate 			    tlist[i - 1].h);
3347c478bd9Sstevel@tonic-gate 		if (ret == 0) {
3357c478bd9Sstevel@tonic-gate 			err_msg(gettext("Error reading track %d : %s\n"), i,
3367c478bd9Sstevel@tonic-gate 			    get_err_str());
3377c478bd9Sstevel@tonic-gate 			if (debug)
3387c478bd9Sstevel@tonic-gate 				(void) printf("%x %x %x %x\n", uscsi_status,
3397c478bd9Sstevel@tonic-gate 				    SENSE_KEY(rqbuf), ASC(rqbuf), ASCQ(rqbuf));
3407c478bd9Sstevel@tonic-gate 			exit(1);
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	/*
3457c478bd9Sstevel@tonic-gate 	 * We've finished copying the CD. If source and destination are the same
3467c478bd9Sstevel@tonic-gate 	 * or they where not specified then eject the disk and wait for a new
3477c478bd9Sstevel@tonic-gate 	 * disk to be inserted.
3487c478bd9Sstevel@tonic-gate 	 */
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	while ((target == NULL) ||
3517c478bd9Sstevel@tonic-gate 	    check_device(target, CHECK_NO_MEDIA|CHECK_MEDIA_IS_NOT_BLANK)) {
3527c478bd9Sstevel@tonic-gate 		if (target != NULL) {
3537c478bd9Sstevel@tonic-gate 			(void) eject_media(target);
3547c478bd9Sstevel@tonic-gate 		}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 		(void) printf("\n");
3577c478bd9Sstevel@tonic-gate 		print_n_flush(
3587c478bd9Sstevel@tonic-gate 		gettext("Insert a blank media in the drive and press Enter."));
3597c478bd9Sstevel@tonic-gate 		(void) fflush(stdin);
3607c478bd9Sstevel@tonic-gate 		if (target) {
3617c478bd9Sstevel@tonic-gate 			fini_device(target);
3627c478bd9Sstevel@tonic-gate 			target = NULL;
3637c478bd9Sstevel@tonic-gate 		}
3647c478bd9Sstevel@tonic-gate 		(void) getchar();
3657c478bd9Sstevel@tonic-gate 		(void) sleep(4);
3667c478bd9Sstevel@tonic-gate 		(void) setup_target(SCAN_WRITERS);
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 	(void) printf("\n");
3697c478bd9Sstevel@tonic-gate 	(void) setreuid(ruid, 0);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	if ((device_type != DVD_PLUS) && (device_type != DVD_PLUS_W)) {
3727c478bd9Sstevel@tonic-gate 		ensure_media_space(total_nblks, end_tno);
3737c478bd9Sstevel@tonic-gate 		write_init(audio_cd ? TRACK_MODE_AUDIO : TRACK_MODE_DATA);
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/* for each track */
3777c478bd9Sstevel@tonic-gate 	for (i = 0; i < end_tno; i++) {
3787c478bd9Sstevel@tonic-gate 		/*
3797c478bd9Sstevel@tonic-gate 		 * DVD's dont contain tracks and need to be written in DAO
3807c478bd9Sstevel@tonic-gate 		 * mode.
3817c478bd9Sstevel@tonic-gate 		 */
3827c478bd9Sstevel@tonic-gate 		if (device_type != CD_RW) {
3837c478bd9Sstevel@tonic-gate 			if (end_tno > 1) {
3847c478bd9Sstevel@tonic-gate 				err_msg(gettext(
3857c478bd9Sstevel@tonic-gate 				    "Media state is not suitable for this"
3867c478bd9Sstevel@tonic-gate 				    " write mode.\n"));
3877c478bd9Sstevel@tonic-gate 			}
3887c478bd9Sstevel@tonic-gate 			write_mode = DAO_MODE;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 			/*
3917c478bd9Sstevel@tonic-gate 			 * DVD-R(W) and DVD+R needs to have space reserved
3927c478bd9Sstevel@tonic-gate 			 * prior to writing.
3937c478bd9Sstevel@tonic-gate 			 */
3947c478bd9Sstevel@tonic-gate 			if ((device_type == DVD_MINUS) ||
3957c478bd9Sstevel@tonic-gate 			    (device_type == DVD_PLUS)) {
3967c478bd9Sstevel@tonic-gate 				if (!set_reservation(target->d_fd,
3977c478bd9Sstevel@tonic-gate 				    total_nblks + 1)) {
3987c478bd9Sstevel@tonic-gate 					(void) printf(gettext(
3997c478bd9Sstevel@tonic-gate 					    "Setting reservation failed\n"));
4007c478bd9Sstevel@tonic-gate 					exit(1);
4017c478bd9Sstevel@tonic-gate 				}
4027c478bd9Sstevel@tonic-gate 			}
4037c478bd9Sstevel@tonic-gate 		}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 		write_next_track(audio_cd ? TRACK_MODE_AUDIO : TRACK_MODE_DATA,
4067c478bd9Sstevel@tonic-gate 		    tlist[i].h);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 		/*
4097c478bd9Sstevel@tonic-gate 		 * Running in simulation mode and writing several tracks is
4107c478bd9Sstevel@tonic-gate 		 * useless so bail after the first track is done.
4117c478bd9Sstevel@tonic-gate 		 */
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 		if (simulation && (end_tno != 1)) {
4147c478bd9Sstevel@tonic-gate 			(void) printf(gettext(
4157c478bd9Sstevel@tonic-gate 			"Simulation mode : skipping remaining tracks\n"));
4167c478bd9Sstevel@tonic-gate 			break;
4177c478bd9Sstevel@tonic-gate 		}
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	write_fini();
4217c478bd9Sstevel@tonic-gate 	/* close the temp file handles */
4227c478bd9Sstevel@tonic-gate 	for (i = 0; i < end_tno; i++)
4237c478bd9Sstevel@tonic-gate 		(tlist[i].h)->bstr_close(tlist[i].h);
4247c478bd9Sstevel@tonic-gate 	free(tlist);
4257c478bd9Sstevel@tonic-gate 	fini_device(target);
4267c478bd9Sstevel@tonic-gate 	exit(0);
4277c478bd9Sstevel@tonic-gate }
428