1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
/*
vrfytool.c
23.09.99 tn
tool that does read a data-stream from stdin and does compare
it with a file on the harddrive.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <glib.h>
#include "xcdroast.h"
#define WAVHEADER 44
void usage() {
g_print("Usage: vrfytool [-d|-a] <file1> <file2> (Version: %s)\n", XCDROAST_VERSION);
g_print("\twhen file2 is a dash (-) then compare from stdin\n");
g_print("\t -d: compare data-files (blocksize: 2048)\n");
g_print("\t -a: compare audio-files (blocksize: 2352)\n");
}
gint main(gint argc, gchar **argv) {
gchar file1[MAXLINE];
gchar file2[MAXLINE];
guchar *buf1;
guchar *buf2;
struct stat buf;
gint size;
gint fd1, fd2;
gint nread1, nread2;
gint bytesread;
gint percent, oldpercent;
gint bs;
if (argc != 4) {
usage();
exit(1);
}
bs = 0;
if (strcmp(argv[1],"-a") == 0) {
bs = CDDAFRAME;
}
if (strcmp(argv[1],"-d") == 0) {
bs = DATASECTORSIZE;
}
if (bs == 0) {
usage();
exit(1);
}
strncpy(file1,argv[2],MAXLINE);
strncpy(file2,argv[3],MAXLINE);
/* get filesize */
if (stat(file1,&buf) != 0) {
g_print("Error stating %s: %s\n",file1,strerror(errno));
exit(1);
}
/* calculate how many bytes to handle */
size = buf.st_size;
buf1 = (guchar *)g_new(guchar, bs);
buf2 = (guchar *)g_new(guchar, bs);
/* open first file */
fd1 = open(file1, O_RDONLY, 0);
if (fd1 < 0) {
g_print("Error opening %s: %s\n",file1,strerror(errno));
exit(1);
}
/* open second file..or do read from stdin */
if (strcmp(file2, "-") == 0) {
fd2 = STDIN_FILENO;
} else {
fd2 = open(file2, O_RDONLY, 0);
if (fd2 < 0) {
g_print("Error opening %s: %s\n",file2,strerror(errno));
exit(1);
}
}
bytesread = 0;
oldpercent = 0;
g_print("Verifying %s (%d bytes)\n",file1,size);
fflush(stdout);
/* start comparing */
while (1) {
/* first sector for audio-files is only the wavheader */
/* so read first 44 bytes and then always 2352 bytes */
if (bs == WAVHEADER) {
bs = CDDAFRAME;
}
if (bs == CDDAFRAME && bytesread == 0) {
bs = WAVHEADER;
}
nread1 = read(fd1, buf1, bs);
if (nread1 < 0) {
g_print("Error reading %s: %s\n",file1,strerror(errno));
exit(1);
}
/* EOF */
if (nread1 == 0)
break;
/* read exactly as many bytes from stdin as from file */
nread2 = read(fd2, buf2, nread1);
if (nread2 < 0) {
g_print("Error reading %s: %s\n",file2,strerror(errno));
exit(1);
}
if (nread1 != nread2 || nread1 < bs || nread2 < bs) {
g_print("Warning: Short read on position %d\n", bytesread);
exit(1);
}
if (memcmp(buf1,buf2,bs) != 0) {
g_print("Warning: Files differ on position %d\n", bytesread);
exit(1);
}
bytesread+=nread1;
/* output percent meter */
percent = ((bytesread/1024) * 100) / (size/1024);
if (percent != oldpercent) {
g_print(" %d%%\n", percent);
fflush(stdout);
oldpercent = percent;
}
}
if (bytesread != size) {
g_print("Warning: different file sizes.\n");
exit(1);
}
/* continue comparing the rest of stdin with optional padding zeros */
while (1) {
nread2 = read(fd2, buf2, bs);
if (nread2 < 0) {
g_print("Error reading %s: %s\n",file2,strerror(errno));
exit(1);
}
/* EOF */
if (nread2 == 0)
break;
if (nread2 < bs) {
g_print("Warning: Short read on position %d\n", bytesread);
exit(1);
}
memset (buf1, 0, bs);
if (memcmp(buf1,buf2,bs) != 0) {
g_print("Warning: Files differ on position %d\n", bytesread);
exit(1);
}
}
g_free(buf1);
g_free(buf2);
close(fd1);
close(fd2);
/* padding is usually 15 sectors by cdrecord */
if (bytesread < size || bytesread > size+15*bs) {
g_print("Warning: different file sizes.\n");
exit(1);
}
/* all ok - compare perfect */
g_print("Compare of %s successful.\n",file1);
fflush(stdout);
return 0;
}
|