SMREmulator 1.0
ShingledMagneticRecordingEmulator

writetoband.c

Go to the documentation of this file.
00001 
00009 #include <fcntl.h>
00010 #include <stdio.h>
00011 #include <errno.h>
00012 #include <stdlib.h>
00013 #include <unistd.h>
00014 
00015 #include "../include/emulator.h"
00016 
00017 
00018 
00019 #define BUFSIZE 65536
00020 
00021 
00022 
00023 
00024 int main(int argc, char *argv[]) {
00025         int emufd, filfd;
00026         int blk_size, band_count;
00027         int i;
00028 
00029         int bytes_cur, bytes_total, rba, blk_count;//, blk_total;
00030         char buf[BUFSIZE];
00031 
00032 
00033         if (argc < 2) {
00034                 printf("Usage: writetoband <EDI file> <list of files to write>\n");
00035                 exit(-1);
00036         }
00037 
00038         /* Initialize */
00039         if (edi_init() < 0) {
00040                 printf("Cannot initialize.\n");
00041                 exit(-1);
00042         }
00043 
00044         /* Open */
00045         if ((emufd = edi_open(argv[1])) < 0) {
00046                 printf("Cannot open EDI file!\n");
00047                 exit(-1);
00048         }
00049 
00050         /* Query basics */
00051         if (edi_modesense(emufd, INQ_BLKSIZE, &blk_size) < 0) {
00052                 printf("Cannot get block size from EDI file.\n");
00053                 goto close_edi;
00054         }
00055 
00056         printf("Block size is %d bytes\n", blk_size);
00057 
00058         if (edi_modesense(emufd, INQ_BNDCOUNT, &band_count) < 0) {
00059                 printf("Cannot get band count from EDI file.\n");
00060                 goto close_edi;
00061         }
00062         printf("Band count is %d\n", band_count);
00063 
00064 
00065         for (i=0; i<band_count; i++) {
00066                 /* Read from source and write to EDI */
00067                 if ((filfd = open(argv[(i % (argc - 2)) + 2], O_RDONLY)) < 0) {
00068                         printf("Cannot open test file!\n");
00069                         goto close_edi;
00070                 }
00071 
00072                 rba = 0;
00073                 bytes_total = 0;
00074                 while((bytes_cur = read(filfd, buf, BUFSIZE)) > 0) {
00075                         printf("Read %d bytes from file\n", bytes_cur);
00076                         /* Calculate the number of blocks for the bytes read */
00077                         blk_count = bytes_cur/blk_size;
00078                         blk_count = (bytes_cur % blk_size) ? blk_count + 1 : blk_count;
00079 
00080                         /* Write the data to the EDI */
00081                         if (edi_write(emufd, i, rba, buf, blk_count) < 0) {
00082                                 printf("edi_write() failed! Error code: %d", errno);
00083                                 goto close_file;
00084                         }
00085 
00086                         /* Update the counts for the next iteration */
00087                         rba += blk_count;
00088                         bytes_total += bytes_cur;
00089                         printf("Wrote %d blocks (%d bytes of %d bytes)...\n",
00090                                         rba, bytes_cur, bytes_total);
00091                 }
00092 
00093                 /* Close the source file */
00094                 close(filfd);
00095         }
00096 
00097 
00098 
00099 //      /* Now read back the data from the EDI and write it out to a new file */
00100 //      if ((filfd = open("libc1.txt", O_RDWR | O_CREAT | O_TRUNC, 0644)) < 0) {
00101 //              printf("Cannot create/open new file for readback!\n");
00102 //              goto close_edi;
00103 //      }
00104 //
00105 //      /* Read back data from the EDI */
00106 //      blk_total = rba;
00107 //      rba = 0;
00108 //      blk_count = BUFSIZE / blk_size;
00109 //      blk_count = (blk_total > blk_count) ? blk_count : blk_total;
00110 //
00111 //      while(blk_count) {
00112 //              /* Read data from the EDI */
00113 //              if (edi_read(emufd, 0, rba, buf, blk_count) < 0) {
00114 //                      printf("Error reading EDI data from block %d to %d!\n", rba, blk_count);
00115 //                      goto close_file;
00116 //              }
00117 //
00118 //              /* Write the data to the new file */
00119 //              bytes_cur = blk_count * blk_size;
00120 //              bytes_cur = (bytes_total > bytes_cur) ? bytes_cur : bytes_total;
00121 //              if (write(filfd, buf, bytes_cur) != bytes_cur) {
00122 //                      printf("Error writing data read from EDI to new file!\n");
00123 //                      goto close_file;
00124 //              }
00125 //
00126 //              /* Adjust the counters */
00127 //              rba += blk_count;
00128 //              bytes_total -= bytes_cur;
00129 //
00130 //              printf("Wrote %d blocks (%d bytes). %d bytes remaining)...\n",
00131 //                              blk_count, bytes_cur, bytes_total);
00132 //
00133 //              blk_total -= blk_count;
00134 //              blk_count = (blk_total > blk_count) ? blk_count : blk_total;
00135 //      }
00136 //      printf("Data copied back from EDI file!\n");
00137 
00138 
00139 close_file:
00140         close(filfd);
00141 
00142 close_edi:
00143         edi_close(emufd);
00144         return 0;
00145 }