SMREmulator 1.0
ShingledMagneticRecordingEmulator

edicreate.c

Go to the documentation of this file.
00001 
00009 #include <stdio.h>
00010 #include <stdint.h>
00011 #include <limits.h>
00012 #include <unistd.h>
00013 #include <string.h>
00014 #include <stdlib.h>
00015 
00016 #include <emudiskimg.h>
00017 #include <emudebug.h>
00018 #include <emulator.h>
00019 #include <ediversion.h>
00020 
00021 
00022 
00023 /* -- Macro Definitions -- */
00024 #define ARGS_MIN 6
00025 #define ARGS_MAX 11
00026 
00027 
00028 
00029 /* -- Type Definitions -- */
00030 typedef struct {
00031         char edi_path[PATH_MAX];
00032         uint32_t version;
00033 
00034         uint32_t blk_size;
00035         band_t band_count;
00036         rba_t band_size;
00037 } cmd_args_t;
00038 
00039 
00040 
00041 /* -- Constants -- */
00042 const char* usage = "edicreate -b <band size> -c <band count> -s <block size> "
00043                                         "-v <EDI version> -f <EDI filename>";
00044 
00045 
00046 
00047 /* -- Static Variables -- */
00048 static cmd_args_t args;
00049 
00050 
00051 
00052 /* -- Function Implementations -- */
00053 
00054 static void print_usage() {
00055         /* Display the usage string */
00056         printf("\n%s\n\n", usage);
00057 }
00058 
00059 
00060 
00061 static void parse_args(int argc, char** argv) {
00062         int option, badargs = 0;
00063 
00064 
00065         /* Check if arguments were passed */
00066         if (argc < ARGS_MIN) {
00067                 print_usage();
00068                 exit(EXIT_FAILURE);
00069         }
00070 
00071 
00072         /* Loop over the arguments and parse them */
00073         while ((option = getopt(argc, argv, "b:c:s:f:v:")) != -1) {
00074                 switch(option) {
00075                 case 'b':                       /* Band Size */
00076                         args.band_size = atoi(optarg);
00077 
00078                         /* Ensure that the band size is greater than 0 */
00079                         if (args.band_size <= 0) {
00080                                 printf("Band size should be greater than 0.\n");
00081                                 badargs = 1;
00082                         }
00083 
00084                         break;
00085 
00086 
00087                 case 'c':                       /* Band Count */
00088                         args.band_count = atoi(optarg);
00089 
00090                         /* Ensure that the band count is greater than 0 */
00091                         if (args.band_count <= 0) {
00092                                 printf("Band count should be greater than 0.\n");
00093                                 badargs = 1;
00094                         }
00095 
00096                         break;
00097 
00098 
00099                 case 's':                       /* Block size */
00100                         args.blk_size = atoi(optarg);
00101 
00102                         /* Ensure that the block size is a power of 2 */
00103                         if ((args.blk_size & (args.blk_size - 1)) != 0) {
00104                                 printf("Block size should be a power of 2.\n");
00105                                 badargs = 1;
00106                         }
00107 
00108                         break;
00109 
00110 
00111                 case 'v':                       /* EDI Version */
00112                         args.version = atoi(optarg);
00113 
00114                         /* Validate the version */
00115                         if (!ediv_is_valid(args.version)) {
00116                                 printf("Version %d is invalid", args.version);
00117                                 badargs = 1;
00118                         }
00119 
00120                         break;
00121 
00122 
00123                 case 'f':                       /* EDI Filename */
00124                         strcpy(args.edi_path, optarg);
00125 
00126                         break;
00127 
00128 
00129                 default:                        /* Unsupported option */
00130                         printf("Invalid option: -%c", option);
00131                         break;
00132                 }
00133         }
00134 
00135 
00136         /* Ensure that all arguments were parsed successfully */
00137         if (badargs) {
00138                 print_usage();
00139                 exit(EXIT_FAILURE);
00140         }
00141 }
00142 
00143 
00144 
00145 int main(int argc, char** argv) {
00146         int fd;
00147 
00148 
00149         /* Parse the arguments passed */
00150         parse_args(argc, argv);
00151         dbg("Parsed arguments:\n\tFile: %s\n\tVersion: %d\n\tBlock Size: %d\n\t"
00152                         "Band Count: %d\n\tBand Size: %d\n", args.edi_path, args.version,
00153                         args.blk_size, args.band_count, args.band_size);
00154 
00155 
00156         /* Initialize the EDI Versioning module */
00157         if (ediv_init() < 0) {
00158                 printf("Failed to initialize Verioning Module!\n");
00159                 exit(EXIT_FAILURE);
00160         }
00161 
00162 
00163         /* Check the version number */
00164         switch(args.version) {
00165         case 0:         /* EDI v0 */
00166                 fd = edi_create(args.edi_path, args.version,
00167                                 args.blk_size, args.band_count, args.band_size);
00168                 break;
00169 
00170 
00171         default:        /* Invalid version. Should not be here if the code is correct */
00172                 printf("Unsupported EDI version %d.\n", args.version);
00173                 print_usage();
00174                 break;
00175         }
00176 
00177 
00178         /* Check if the file was successfully created */
00179         if (fd < 0) {
00180                 printf("Error creating EDI file!\n");
00181                 exit(EXIT_FAILURE);
00182         }
00183         dbg("Created v0 EDI file! Open with file descriptor %d\n", fd);
00184 
00185 
00186         /* Close the open EDI file */
00187         if (close(fd) < 0) {
00188                 printf("EDI file created successfully, but unable to close it!\n");
00189                 exit(EXIT_FAILURE);
00190         }
00191 
00192         printf("EDI file %s successfully created!\n", args.edi_path);
00193         return 0;
00194 }