📄 write.c
字号:
/* write -- write tracks Copyright (C) 1996 Dieter Baron and Armin Obersteiner This file is part of cdwrite. The authors can be contacted at <dillo@giga.or.at> <armin.obersteiner@giga.or.at> 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 of the License, 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; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/ #include "cdwrite.h"#include <stdio.h>#include <stdlib.h>#include <limits.h>#include <fcntl.h>#include <unistd.h>#include <errno.h>#include <scsi/all.h>#include <scsi/da.h>#include <scsi/hp.h>#define BSIZE_MAX 32768struct { struct sc_mode_header6 header; struct sc_mode_block_descriptor bdesc;} bdesc;intwrite_track(int no){ int bsize, end, fd, blksize, blks, err, n, add, i; u_long bytes_total, bytes_read, size, last_mb; u_char *buf;#ifdef TODISK/* #define OUTFILE "/dev/sd2a" */#define OUTFILE "tmp1:iso-disk" int outfd; if ((outfd=open(OUTFILE, O_WRONLY, 0666)) < 0) { fprintf(stderr, "%s: can't open disk: %s\n", prg, strerror(errno)); return -1; }#endif blksize = tinfo[no].audio ? 2352 : 2048; blks = BSIZE_MAX / blksize; bsize = blks * blksize; if ((buf=sc_alloc(bsize)) == NULL) { fprintf(stderr, "%s: malloc failure\n", prg); return -1; } if (verbose > 0) { if (tinfo[no].size) printf("%02d: 0 / %d mb\r", no+1, (int)(tinfo[no].size>>20)); else printf("%02d: 0 mb\r"); fflush(stdout); } /* XXX: for ISRC, use mode page 0x21 (write track) */ /* XXX: for user data in pre-gap, use mode page 0x24 */ switch (mode) { case 0: case 1: case 2: case 3: if (sc_hp_write_track(scsi, 0, 0, tinfo[no].audio, (tinfo[no].audio ? tinfo[no].emp : 1), 0, 0) < 0) { sc_fstatus(scsi, stderr, prg, "write-track", 3); return -1; } break; case 4: bdesc.header.bdno = 1; sc_store_l((char *)&bdesc.bdesc.r4, (tinfo[no].audio ? 2352 : 2048)); if (sc_mode_select6(scsi, 1, 0, sizeof(bdesc), (u_char *)&bdesc) < 0) { sc_fstatus(scsi, stderr, prg, "mode-select-bdesc", 3); return -1; } if (sc_cdr102_write_track(scsi, 0, tinfo[no].audio, tinfo[no].audio, 1, tinfo[no].audio, (tinfo[no].audio ? tinfo[no].emp : 1), 0, 0, 0) < 0) { sc_fstatus(scsi, stderr, prg, "write-track-102", 3); return -1; } break; } written = 1; isdata |= (tinfo[no].audio ? 0 : 1); last_mb = bytes_total = 0; size = tinfo[no].size ? tinfo[no].size : ULONG_MAX; fd = tinfo[no].fd; err = end = 0; while (!end) { for (n=bytes_read=0; bytes_read<bsize && n>=0; bytes_read+=n) { if ((n=read(fd, buf+bytes_read, bsize-bytes_read)) == 0) break; else if (n < 0) { n = 0; if (errno != EINTR && errno != EAGAIN) { fprintf(stderr, "%s: read error: %s\n", prg, strerror(errno)); err = -2; break; } } } if (bytes_read == 0) break; if (bytes_read < bsize) { end = 1; if (bytes_read % blksize != 0) { if (tinfo[no].audio) { bytes_read -= bytes_read % 4; while (bytes_read % blksize) { memcpy(buf+bytes_read, buf+bytes_read-4, 4); bytes_read += 4; } } else { add = blksize - (bytes_read % blksize); memset(buf+bytes_read, 0, add); bytes_read += add; } } blks = bytes_read / blksize; } if (sc_da_write6(scsi, 0, blks, bytes_read, buf) < 0) { sc_fstatus(scsi, stderr, prg, "write6", 3); /* XXX: don't break if error can be ignored (sense key?) */ err = -2; break; }#ifdef TODISK if (write(outfd, buf, bytes_read) != bytes_read) { fprintf(stderr, "%s: write error to disk: %s\n", prg, strerror(errno)); err = -2; break; }#endif bytes_total += bytes_read; if (verbose > 0 && ((bytes_total^last_mb) & ~0xfffff)) { printf("%02d: %3d\r", no+1, (int)(bytes_total >> 20)); fflush(stdout); last_mb = bytes_total; } if (bytes_total+bsize > size) { bsize = (size - bytes_total); blks = bsize / blksize; } if (bytes_total >= size) end = 1; } if (sc_da_synchronize_cache(scsi, 0, 0, 0) < 0 && !(scsi->sense.data.skey == 5 && scsi->sense.data.asc == 0xb5)) { /* dummy blocks added */ sc_fstatus(scsi, stderr, prg, "synchronize-cache", 3); return -3; } if (verbose > 0) { printf("%02d: %ld bytes written\n", no+1, bytes_total); }#ifdef TODISK if (close(outfd) < 0) { fprintf(stderr, "%s: warning: can't close disk: %s\n", prg, strerror(errno)); }#endif TODISK sc_free(buf); return err;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -