--- embedtools/src/imgupd.c 2014/02/05 15:44:06 1.2 +++ embedtools/src/imgupd.c 2014/02/05 21:59:51 1.2.2.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ - * $Id: imgupd.c,v 1.2 2014/02/05 15:44:06 misho Exp $ + * $Id: imgupd.c,v 1.2.2.1 2014/02/05 21:59:51 misho Exp $ * ************************************************************************* The ELWIX and AITNET software is distributed under the following @@ -57,8 +57,9 @@ Usage() printf( "IMGUPD is tool for management of images\n" "=== %s === %s@%s ===\n\n" - " Syntax: imgupd [options] \n\n" + " Syntax: imgupd [options] [image_file]\n\n" "\t-v\t\tVerbose ...\n" + "\t-g\t\tGet image from Storage\n" "\t-t\t\tTruncate Storage file name\n" "\t-s \tStorage size (required for stdin)\n" "\t-f \tStorage file name\n" @@ -133,10 +134,10 @@ FillStore(int img, int fd) int main(int argc, char **argv) { - char ch; + char ch, m = 0; int fd, img, tr = 0; - while ((ch = getopt(argc, argv, "hvts:f:")) != -1) + while ((ch = getopt(argc, argv, "hvgts:f:")) != -1) switch (ch) { case 'f': strlcpy(imgName, optarg, sizeof imgName); @@ -151,6 +152,9 @@ main(int argc, char **argv) case 't': tr = O_TRUNC; break; + case 'g': + m = 1; + break; case 'v': Verbose++; break; @@ -162,55 +166,109 @@ main(int argc, char **argv) argc -= optind; argv += optind; - if (argc) { - strlcpy(imgFile, *argv, sizeof imgFile); - /* open image file */ - fd = open(imgFile, O_RDONLY); - if (fd == -1) { + if (!m) { + if (argc) { + strlcpy(imgFile, *argv, sizeof imgFile); + /* open image file */ + 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 : ""); + + if (!m) { + /* open storage device */ + img = open(imgName, O_RDWR | O_CREAT | tr, 0644); + if (img == -1) { 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 - iSize = lseek(fd, 0, SEEK_END); + iSize = lseek(img, 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", + if (fd > 2) + close(fd); + close(img); + EERROR(ENOSPC, "Error:: storage size %llu is " + "greater from file size %llu\n", iSize, imgSize); - return 2; + return 3; } else - lseek(fd, 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 : ""); - - /* open storage device */ - img = open(imgName, O_RDWR | O_CREAT | tr, 0644); - if (img == -1) { - ESYSERR(0); - if (fd > 2) - close(fd); - return 3; + lseek(img, 0, SEEK_SET); } - if (EmptyStore(img) == -1) { - if (fd > 2) - close(fd); - close(img); - unlink(imgName); - return 3; - } - if (FillStore(img, fd) == -1) { - if (fd > 2) - close(fd); - close(img); - unlink(imgName); - return 4; + if (!m) { + if (EmptyStore(img) == -1) { + if (fd > 2) + close(fd); + close(img); + 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);