- (read(arfd, scbuf2, sizeof(scbuf2)) != phyblk) ||
- (bcmp(scbuf1, scbuf2, phyblk) != 0))
+ ((res = read(arfd, scbuf2, sizeof(scbuf2))) <= 0))
+ return(-1);
+
+ /*
+ * If we get a bigger size on the second read or the first read is not
+ * a multiple of the second read, we better not chance an append.
+ */
+ if ((res > phyblk) || (phyblk % res))
+ return(-1);
+
+ /*
+ * if both reads are the same size and the data is consistant we can
+ * go on with the append
+ */
+ if (res == phyblk) {
+ if (bcmp(scbuf1, scbuf2, phyblk) != 0)
+ return(-1);
+ return(0);
+ }
+
+ /*
+ * We got two different block sizes. We were not at the tape EOF.
+ * So we try one more time, if the result is not consistant we abort.
+ */
+ if ((ioctl(arfd, MTIOCTOP, &mb) < 0) ||
+ (read(arfd, scbuf1, sizeof(scbuf1)) != res) ||
+ (bcmp(scbuf1, scbuf2, res) != 0))
+ return(-1);
+
+ /*
+ * Ok, we assume the physical block size is in res. We need to adjust
+ * head position backwards based on the what we got on the first read.
+ * (must leave the head at the same place it was when we were called).
+ */
+ mb.mt_count = (phyblk - res)/res;
+ phyblk = res;
+ if ((mb.mt_count == 0) || (ioctl(arfd, MTIOCTOP, &mb) < 0))