?? cdrecord.c
字號:
/* @(#)cdrecord.c 1.66 98/10/18 Copyright 1995 J. Schilling */#ifndef lintstatic char sccsid[] = "@(#)cdrecord.c 1.66 98/10/18 Copyright 1995 J. Schilling";#endif/* * Record data on a CD/CVD-Recorder * * Copyright (c) 1995 J. Schilling *//* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */#include <mconfig.h>#include <stdio.h>#include <standard.h>#include <stdlib.h>#include <fcntl.h>#include <errno.h>#include <sys/types.h>#include <sys/time.h>#include <sys/resource.h> /* for rlimit */#include <sys/stat.h>#include <unixstd.h>#include <sys/mman.h>#include <string.h>#include <utypes.h>#include <scgio.h> /* XXX wegen inquiry */#include "scsireg.h" /* XXX wegen SC_NOT_READY */#include "cdrecord.h"#include "auheader.h"#include "scsitransp.h"#include "scsi_scan.h"#ifndef O_BINARY /* Only present on DOS or similar */#define O_BINARY 0#endifchar cdr_version[] = "1.6.1";/* * Map toc/track types into names. */char *toc2name[] = { "CD-DA", "CD-ROM", "CD-ROM XA mode 1", "CD-ROM XA mode 2", "CD-I", "Illegal toc type 5", "Illegal toc type 6", "Illegal toc type 7",};/* * Map sector types into names. */char *st2name[] = { "Illegal sector type 0", "CD-ROM mode 1", "CD-ROM mode 2", "Illegal sector type 3", "CD-DA without preemphasis", "CD-DA with preemphasis", "Illegal sector type 6", "Illegal sector type 7",};/* * Map data block types into names. */char *db2name[] = { "Raw (audio)", "Raw (audio) with P/Q sub channel", "Raw (audio) with P/W sub channel", "Raw (audio) with P/W raw sub channel", "Reserved mode 4", "Reserved mode 5", "Reserved mode 6", "Vendor unique mode 7", "CD-ROM mode 1", "CD-ROM mode 2", "CD-ROM XA mode 1", "CD-ROM XA mode 2 form 1", "CD-ROM XA mode 2 form 2", "CD-ROM XA mode 2 form 1/2/mix", "Reserved mode 14", "Vendor unique mode 15",};int debug; /* print debug messages */int verbose; /* SCSI verbose flag */int lverbose; /* local verbose flag *//* * NOTICE: You should not make BUF_SIZE more than * the buffer size of the CD-Recorder. * * Do not set BUF_SIZE to be more than 126 KBytes * if you are running cdrecord on a sun4c machine. * * WARNING: Philips CDD 521 dies if BUF_SIZE is to big. *//*#define BUF_SIZE (126*1024)*//*#define BUF_SIZE (100*1024)*/#define BUF_SIZE (63*1024)/*#define BUF_SIZE (56*1024)*/char *buf; /* The transfer buffer */long bufsize; /* The size of the transfer buffer */int data_secs_per_tr; /* # of data secs per transfer */int audio_secs_per_tr; /* # of audio secs per transfer */BOOL isgui;struct timeval starttime;struct timeval stoptime;struct timeval fixtime;extern int silent;static long fs = -1L; /* fifo (ring buffer) size */EXPORT int main __PR((int ac, char **av));LOCAL void usage __PR((int));LOCAL void blusage __PR((int));EXPORT int read_buf __PR((int f, char *bp, int size));EXPORT int get_buf __PR((int f, char **bpp, int size));LOCAL int write_track_data __PR((cdr_t *, int , track_t *));EXPORT int pad_track __PR((cdr_t *dp, int track, track_t *trackp, long startsec, long amt, BOOL dolast, long *bytesp));LOCAL void printdata __PR((int, track_t *));LOCAL void printaudio __PR((int, track_t *));LOCAL void checkfile __PR((int, track_t *));LOCAL int checkfiles __PR((int, track_t *));LOCAL void setpregaps __PR((int, track_t *));LOCAL long checktsize __PR((int, track_t *));LOCAL void checksize __PR((track_t *));LOCAL BOOL checkdsize __PR((cdr_t *dp, dstat_t *dsp, long tsize));LOCAL void raise_fdlim __PR((void));LOCAL void gargs __PR((int, char **, int *, track_t *, char **, int *, cdr_t **, int *, long *, int *, int *));LOCAL void set_trsizes __PR((cdr_t *, int, track_t *));EXPORT void load_media __PR((cdr_t *));EXPORT void unload_media __PR((cdr_t *, int));LOCAL void check_recovery __PR((cdr_t *, int)); void audioread __PR((cdr_t *, int));LOCAL void print_msinfo __PR((cdr_t *));LOCAL void print_toc __PR((cdr_t *));LOCAL void print_track __PR((int, long, struct msf *, int, int, int));LOCAL void prtimediff __PR((const char *fmt, struct timeval *start, struct timeval *stop));#if !defined(HAVE_SYS_PRIOCNTL_H)LOCAL int rt_raisepri __PR((int));#endifEXPORT void raisepri __PR((int));LOCAL void checkgui __PR((void));LOCAL char * astoll __PR((const char *s, Llong *ll));LOCAL Llong number __PR((char* arg, int* retp));EXPORT int getnum __PR((char* arg, long* valp));EXPORT int getllnum __PR((char *arg, Llong* lvalp));LOCAL int getbltype __PR((char* optstr, long *typep));EXPORT int main(ac, av) int ac; char *av[];{ char *dev = NULL; int timeout = 40; /* Set default timeout to 40s CW-7502 is slow*/ int speed = 1; long flags = 0L; int toctype = -1; int blanktype = 0; int i; int tracks = 0; int trackno; long tsize; track_t track[MAX_TRACK+1]; cdr_t *dp = (cdr_t *)0; dstat_t ds; long startsec = 0L; int old_secsize = -1; int errs = 0; save_args(ac, av); raise_fdlim(); gargs(ac, av, &tracks, track, &dev, &timeout, &dp, &speed, &flags, &toctype, &blanktype); if (toctype < 0) comerrno(EX_BAD, "Internal error: Bad TOC type.\n"); if ((flags & F_MSINFO) == 0 || lverbose || flags & F_VERSION) printf("Cdrecord release %s Copyright (C) 1995-1998 J鰎g Schilling\n", cdr_version); if (flags & F_VERSION) exit(0); checkgui(); if (debug || lverbose) { printf("TOC Type: %d = %s\n", toctype, toc2name[toctype & TOC_MASK]); } if ((flags & (F_MSINFO|F_TOC|F_FIX|F_VERSION|F_CHECKDRIVE|F_INQUIRY|F_SCANBUS|F_RESET)) == 0) { /* * Try to lock us im memory (will only work for root) * but you need access to root anyway to use /dev/scg? */#if defined(HAVE_MLOCKALL) || defined(_POSIX_MEMLOCK) if (mlockall(MCL_CURRENT|MCL_FUTURE) < 0) comerr("Cannot do mlockall(2).\n");#endif raisepri(0); /* max priority */ init_fifo(fs); } if (open_scsi(dev, timeout, (flags & F_MSINFO) == 0 || lverbose) <= 0) comerr("Cannot open SCSI driver.\n"); bufsize = scsi_bufsize(BUF_SIZE); if ((buf = scsi_getbuf(bufsize)) == NULL) comerr("Cannot get SCSI I/O buffer.\n"); if ((flags & F_SCANBUS) != 0) { select_target(); exit(0); } if ((flags & F_RESET) != 0) { scsireset(); exit(0); } /* * First try to check which type of SCSI device we * have. */ if (debug || lverbose) printf("atapi: %d\n", scsi_isatapi()); silent++; test_unit_ready(); /* eat up unit attention */ silent--; if (!do_inquiry((flags & F_MSINFO) == 0 || lverbose)) { errmsgno(EX_BAD, "Cannot do inquiry for CD/DVD-Recorder.\n"); if (unit_ready()) errmsgno(EX_BAD, "The unit seems to be hung and needs power cycling.\n"); exit(EX_BAD); } if ((flags & F_PRCAP) != 0) { print_capabilities(); exit(0); } if ((flags & F_INQUIRY) != 0) exit(0); if (dp == (cdr_t *)NULL) { /* No driver= option specified */ dp = get_cdrcmds(); } else if (!is_unknown_dev() && dp != get_cdrcmds()) { errmsgno(EX_BAD, "WARNING: Trying to use other driver on known device.\n"); } if (!is_cddrive()) comerrno(EX_BAD, "Sorry, no CD/DVD-Drive found on this target.\n"); if (dp == (cdr_t *)0) comerrno(EX_BAD, "Sorry, no supported CD/DVD-Recorder found on this target.\n"); if (((flags & (F_MSINFO|F_TOC|F_LOAD|F_EJECT)) == 0 || tracks > 0) && (dp->cdr_flags & CDR_ISREADER) != 0) { comerrno(EX_BAD, "Sorry, no CD/DVD-Recorder or unsupported CD/DVD-Recorder found on this target.\n"); } if ((*dp->cdr_attach)(dp) != 0) comerrno(EX_BAD, "Cannot attach driver for CD/DVD-Recorder.\n"); if ((flags & F_MSINFO) == 0 || lverbose) { printf("Using %s (%s).\n", dp->cdr_drtext, dp->cdr_drname); printf("Driver flags : "); if ((dp->cdr_flags & CDR_SWABAUDIO) != 0) printf("SWABAUDIO"); printf("\n"); } if ((flags & F_CHECKDRIVE) != 0) exit(0); if (tracks == 0 && (flags & F_FIX) == 0 && (flags & F_EJECT) != 0) { /* * Do not check if the unit is ready here to allow to open * an empty unit too. */ unload_media(dp, flags); exit(0); } flush(); if (tracks > 1) sleep(2); /* Let the user watch the inquiry messages */ set_trsizes(dp, tracks, track); setpregaps(tracks, track); checkfiles(tracks, track); tsize = checktsize(tracks, track); /* * Is this the right place to do this ? */ check_recovery(dp, flags); load_media(dp); if ((flags & F_LOAD) != 0) { scsi_prevent_removal(0); /* allow manual open */ exit(0); } old_secsize = sense_secsize(1); if (old_secsize < 0) old_secsize = sense_secsize(0); if (lverbose) printf("Current Secsize: %d\n", old_secsize);/*audioread(dp, flags);*//*unload_media(dp, flags);*//*return 0;*/ fillbytes(&ds, sizeof(ds), '\0'); if ((*dp->cdr_getdisktype)(dp, &ds) < 0) comerrno(EX_BAD, "Cannot get disk type.\n"); /* * The next actions should depend on the disk type. */ if (flags & F_MSINFO) { print_msinfo(dp); scsi_prevent_removal(0); exit(0); } if (flags & F_TOC) { print_toc(dp); scsi_prevent_removal(0); exit(0); }#ifdef XXX if ((*dp->cdr_check_session)() < 0) { unload_media(dp, flags); exit(EX_BAD); }#endif if (tsize == 0) { if (tracks > 0) { errmsgno(EX_BAD, "WARNING: Track size unknown. Data may not fit on disk.\n"); } } else if (!checkdsize(dp, &ds, tsize) && (flags & F_IGNSIZE) == 0) { unload_media(dp, flags); exit(EX_BAD); } if (tracks > 0 && fs > 0l) { /* * Start the extra process needed for improved buffering. */ if (!init_faio(tracks, track, bufsize)) fs = 0L; } if ((*dp->cdr_set_speed_dummy)(speed, flags & F_DUMMY) < 0) { errmsgno(EX_BAD, "Cannot set speed/dummy.\n"); unload_media(dp, flags); exit(EX_BAD); } /* * Last chance to quit! */ printf("Starting to write CD/DVD at speed %d in %s mode for %s session.\n", speed, (flags & F_DUMMY) ? "dummy" : "write", (flags & F_MULTI) ? "multi" : "single"); printf("Last chance to quit, starting %s write in 9 seconds.", (flags & F_DUMMY)?"dummy":"real"); flush(); for (i=9; --i > 0;) { sleep(1); printf("\b\b\b\b\b\b\b\b\b\b%d seconds.", i); flush(); } printf("\n"); if (tracks > 0 && fs > 0l) { /* * Wait for the read-buffer to become full. * This should be take no extra time if the input is a file. * If the input is a pipe (e.g. mkisofs) this can take a * while. If mkisofs dumps core before it starts writing, * we abort before the writing process started. */ if (!await_faio()) { errmsgno(EX_BAD, "Input buffer error, aborting.\n"); unload_media(dp, flags); exit(EX_BAD); } } if (gettimeofday(&starttime, (struct timezone *)0) < 0) errmsg("Cannot get start time\n"); /* * Blank the media if we were requested to do so */ if (flags & F_BLANK) { if ((*dp->cdr_blank)(0L, blanktype) < 0) { errmsgno(EX_BAD, "Cannot blank disk, aborting.\n"); unload_media(dp, flags); exit(EX_BAD); } if (gettimeofday(&fixtime, (struct timezone *)0) < 0) errmsg("Cannot get blank time\n"); if (lverbose) prtimediff("Blanking time: ", &starttime, &fixtime); if (!wait_unit_ready(60) || tracks == 0) { scsi_prevent_removal(0); exit(0); } } /* * Get the number of the next recordable track. */ silent++; if (read_tochdr(dp, NULL, &trackno) < 0) { trackno = 0; } silent--; for (i = 1; i <= tracks; i++) { track[i].trackno = i + trackno; } trackno++; track[0].trackno = trackno; /* XXX Hack for TEAC fixate */ /* * Now we actually start writing to the CD/DVD. * XXX Check total size of the tracks and remaining size of disk. */ if ((*dp->cdr_open_session)(tracks, track, toctype, flags & F_MULTI) < 0) { errmsgno(EX_BAD, "Cannot open new session.\n"); unload_media(dp, flags); exit(EX_BAD); } /* * As long as open_session() will do nothing but * set up parameters, we may leave fix_it here. * I case we have to add an open_session() for a drive * that wants to do something that modifies the disk * We have to think about a new solution. */ if (flags & F_FIX) goto fix_it; /* * Need to set trackno to the real value from * the current disk status. */ for (i = 1; i <= tracks; i++, trackno++) { startsec = 0L; /* * trackno is the "real" track number while 'i' is a counter * going from 1 to tracks. */ if ((*dp->cdr_open_track)(dp, trackno, &track[i]) < 0) { errs++;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -