|
|
| version 1.2, 2014/02/05 15:44:06 | version 1.3, 2014/02/05 22:00:29 |
|---|---|
| Line 57 Usage() | Line 57 Usage() |
| printf( "IMGUPD is tool for management of images\n" | printf( "IMGUPD is tool for management of images\n" |
| "=== %s === %s@%s ===\n\n" | "=== %s === %s@%s ===\n\n" |
| " Syntax: imgupd [options] <image_file>\n\n" | " Syntax: imgupd [options] [image_file]\n\n" |
| "\t-v\t\tVerbose ...\n" | "\t-v\t\tVerbose ...\n" |
| "\t-g\t\tGet image from Storage\n" | |
| "\t-t\t\tTruncate Storage file name\n" | "\t-t\t\tTruncate Storage file name\n" |
| "\t-s <size>\tStorage size (required for stdin)\n" | "\t-s <size>\tStorage size (required for stdin)\n" |
| "\t-f <devfile>\tStorage file name\n" | "\t-f <devfile>\tStorage file name\n" |
| Line 133 FillStore(int img, int fd) | Line 134 FillStore(int img, int fd) |
| int | int |
| main(int argc, char **argv) | main(int argc, char **argv) |
| { | { |
| char ch; | char ch, m = 0; |
| int fd, img, tr = 0; | int fd, img, tr = 0; |
| while ((ch = getopt(argc, argv, "hvts:f:")) != -1) | while ((ch = getopt(argc, argv, "hvgts:f:")) != -1) |
| switch (ch) { | switch (ch) { |
| case 'f': | case 'f': |
| strlcpy(imgName, optarg, sizeof imgName); | strlcpy(imgName, optarg, sizeof imgName); |
| Line 151 main(int argc, char **argv) | Line 152 main(int argc, char **argv) |
| case 't': | case 't': |
| tr = O_TRUNC; | tr = O_TRUNC; |
| break; | break; |
| case 'g': | |
| m = 1; | |
| break; | |
| case 'v': | case 'v': |
| Verbose++; | Verbose++; |
| break; | break; |
| Line 162 main(int argc, char **argv) | Line 166 main(int argc, char **argv) |
| argc -= optind; | argc -= optind; |
| argv += optind; | argv += optind; |
| if (argc) { | if (!m) { |
| strlcpy(imgFile, *argv, sizeof imgFile); | if (argc) { |
| /* open image file */ | strlcpy(imgFile, *argv, sizeof imgFile); |
| fd = open(imgFile, O_RDONLY); | /* open image file */ |
| if (fd == -1) { | fd = open(imgFile, O_RDONLY); |
| if (fd == -1) { | |
| ESYSERR(0); | |
| return 2; | |
| } else | |
| iSize = lseek(fd, 0, SEEK_END); | |
| if (!imgSize) | |
| imgSize = E_ALIGN(iSize, IMGBUF_SIZE); | |
| if (iSize == -1 || iSize > imgSize) { | |
| close(fd); | |
| EERROR(ENOSPC, "Error:: file size %llu is " | |
| "greater from storage size %llu\n", | |
| iSize, imgSize); | |
| return 2; | |
| } else | |
| lseek(fd, 0, SEEK_SET); | |
| } else if (!imgSize) { | |
| Usage(); | |
| return 1; | |
| } else | |
| fd = STDIN_FILENO; | |
| } else { /* GET */ | |
| if (argc) { | |
| strlcpy(imgFile, *argv, sizeof imgFile); | |
| /* open image file */ | |
| fd = open(imgFile, O_WRONLY | O_TRUNC | O_CREAT, 0644); | |
| if (fd == -1) { | |
| ESYSERR(0); | |
| return 2; | |
| } | |
| } else if (!imgSize) { | |
| Usage(); | |
| return 1; | |
| } else | |
| fd = STDOUT_FILENO; | |
| } | |
| VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n", | |
| imgSize, imgName, argc ? imgFile : "<stdin>"); | |
| if (!m) { | |
| /* open storage device */ | |
| img = open(imgName, O_RDWR | O_CREAT | tr, 0644); | |
| if (img == -1) { | |
| ESYSERR(0); | ESYSERR(0); |
| return 2; | if (fd > 2) |
| close(fd); | |
| return 3; | |
| } | |
| } else { /* GET */ | |
| /* open storage device */ | |
| img = open(imgName, O_RDONLY); | |
| if (img == -1) { | |
| ESYSERR(0); | |
| if (fd > 2) | |
| close(fd); | |
| return 3; | |
| } else | } else |
| iSize = lseek(fd, 0, SEEK_END); | iSize = lseek(img, 0, SEEK_END); |
| if (!imgSize) | if (!imgSize) |
| imgSize = E_ALIGN(iSize, IMGBUF_SIZE); | imgSize = E_ALIGN(iSize, IMGBUF_SIZE); |
| if (iSize == -1 || iSize > imgSize) { | if (iSize == -1 || iSize > imgSize) { |
| close(fd); | if (fd > 2) |
| EERROR(ENOSPC, "Error:: file size %llu is greater from storage size %llu\n", | close(fd); |
| close(img); | |
| EERROR(ENOSPC, "Error:: storage size %llu is " | |
| "greater from file size %llu\n", | |
| iSize, imgSize); | iSize, imgSize); |
| return 2; | return 3; |
| } else | } else |
| lseek(fd, 0, SEEK_SET); | lseek(img, 0, SEEK_SET); |
| } else if (!imgSize) { | |
| Usage(); | |
| return 1; | |
| } else | |
| fd = STDIN_FILENO; | |
| VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n", | |
| imgSize, imgName, argc ? imgFile : "<stdin>"); | |
| /* open storage device */ | |
| img = open(imgName, O_RDWR | O_CREAT | tr, 0644); | |
| if (img == -1) { | |
| ESYSERR(0); | |
| if (fd > 2) | |
| close(fd); | |
| return 3; | |
| } | } |
| if (EmptyStore(img) == -1) { | |
| if (fd > 2) | |
| close(fd); | |
| close(img); | |
| unlink(imgName); | |
| return 3; | |
| } | |
| if (FillStore(img, fd) == -1) { | if (!m) { |
| if (fd > 2) | if (EmptyStore(img) == -1) { |
| close(fd); | if (fd > 2) |
| close(img); | close(fd); |
| unlink(imgName); | close(img); |
| return 4; | return 3; |
| } | |
| if (FillStore(img, fd) == -1) { | |
| if (fd > 2) | |
| close(fd); | |
| close(img); | |
| return 4; | |
| } | |
| } else { /* GET */ | |
| if (EmptyStore(fd) == -1) { | |
| if (fd > 2) | |
| close(fd); | |
| close(img); | |
| return 3; | |
| } | |
| if (FillStore(fd, img) == -1) { | |
| if (fd > 2) | |
| close(fd); | |
| close(img); | |
| return 4; | |
| } | |
| } | } |
| close(img); | close(img); |