--- embedtools/src/Attic/upd.c 2009/11/13 23:19:23 1.1.2.3 +++ embedtools/src/Attic/upd.c 2009/11/14 00:37:20 1.1.2.4 @@ -42,22 +42,156 @@ int Activate(const char *csImg) return 0; } -int Install() +int Install(const char *csImg, const char *psDir) { - VERB(3) printf("Install procedure\n"); + int src, dst, len; + u_char buf[BUFSIZ]; + char szDir[MAXPATHLEN], szFile[MAXPATHLEN]; + struct stat ss, ds; - return 0; + strlcpy(szFile, psDir, MAXPATHLEN); + strlcat(szFile, "/", MAXPATHLEN); + strlcat(szFile, csImg, MAXPATHLEN); + if (access(szFile, R_OK) == -1) { + printf("Error:: Unable to find new image %s #%d - %s\n", + csImg, errno, strerror(errno)); + return -1; + } else { + memset(&ss, 0, sizeof ss); + if (stat(szFile, &ss) == -1) { + printf("Error:: Unable to find new image %s #%d - %s\n", + csImg, errno, strerror(errno)); + return -1; + } + } + + getcwd(szDir, MAXPATHLEN); + VERB(3) printf("Install procedure from %s to %s\n", szFile, szDir); + strlcat(szDir, "/", MAXPATHLEN); + strlcat(szDir, csImg, MAXPATHLEN); + memset(&ds, 0, sizeof ds); + if (stat(szDir, &ds) == -1 && ENOENT != errno) { + printf("Error:: Unable to stat target #%d - %s\n", + errno, strerror(errno)); + return -1; + } + + if (ss.st_dev == ds.st_dev && ss.st_ino == ds.st_ino) { + printf("Error:: Unable to install into self ...\n"); + return -1; + } + + dst = open(szDir, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (dst == -1) { + printf("Error:: in create image %s #%d - %s\n", + szDir, errno, strerror(errno)); + return -1; + } + src = open(szFile, O_RDONLY); + if (src == -1) { + printf("Error:: in open image %s #%d - %s\n", + szFile, errno, strerror(errno)); + close(dst); + unlink(szDir); + return -1; + } + + while ((len = read(src, buf, BUFSIZ)) > 0) + if (write(dst, buf, len) == -1) { + printf("Error:: in write image #%d - %s\n", + errno, strerror(errno)); + close(src); + close(dst); + unlink(szDir); + + len = -1; + break; + } + + close(src); + close(dst); + + if (!len) { + syslog(LOG_NOTICE, "Install image %s to %s", csImg, szDir); + VERB(1) printf("Install image %s to %s\n", csImg, szDir); + } + return len; } int Rollback(const char *csImg) { - char szDir[MAXPATHLEN]; + int src, dst, len; + u_char buf[BUFSIZ]; + char szDir[MAXPATHLEN], szFile[MAXPATHLEN]; + struct stat ss, ds; - if (ChkImg(csImg, szDir) == -1) + getcwd(szFile, MAXPATHLEN); + strlcat(szFile, "/", MAXPATHLEN); + strlcat(szFile, FIRMWARE_BAK, MAXPATHLEN); + if (access(szFile, R_OK) == -1) { + printf("Error:: Unable to find backup image #%d - %s\n", + errno, strerror(errno)); return -1; + } else { + memset(&ss, 0, sizeof ss); + if (stat(szFile, &ss) == -1) { + printf("Error:: Unable to find backup image #%d - %s\n", + errno, strerror(errno)); + return -1; + } + } - VERB(3) printf("Rollback procedure for %s\n", szDir); + getcwd(szDir, MAXPATHLEN); + strlcat(szDir, "/", MAXPATHLEN); + strlcat(szDir, csImg, MAXPATHLEN); + VERB(3) printf("Rollback procedure for image %s from backup!\n", csImg); + memset(&ds, 0, sizeof ds); + if (stat(szDir, &ds) == -1 && ENOENT != errno) { + printf("Error:: Unable to stat target #%d - %s\n", + errno, strerror(errno)); + return -1; + } + if (ss.st_dev == ds.st_dev && ss.st_ino == ds.st_ino) { + printf("Error:: Unable to rollback into self ...\n"); + return -1; + } + + src = open(szFile, O_RDONLY); + if (src == -1) { + printf("Error:: in open backup %s #%d - %s\n", + szFile, errno, strerror(errno)); + return -1; + } + dst = open(szDir, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (dst == -1) { + printf("Error:: in create image %s #%d - %s\n", + szDir, errno, strerror(errno)); + close(src); + return -1; + } + + while ((len = read(src, buf, BUFSIZ)) > 0) + if (write(dst, buf, len) == -1) { + printf("Error:: in write image #%d - %s\n", + errno, strerror(errno)); + close(dst); + close(src); + unlink(szDir); + + len = -1; + break; + } + + close(dst); + close(src); + + if (!len) { + syslog(LOG_NOTICE, "Rollback image %s to %s", csImg, szDir); + VERB(1) printf("Rollback image %s to %s\n", csImg, szDir); + } + return len; + return 0; } @@ -121,16 +255,19 @@ int Backup(const char *csImg, const char *psDir) close(src); close(dst); unlink(szFile); + + len = -1; + break; } close(src); close(dst); - if (!psDir) { + if (!psDir && !len) { syslog(LOG_NOTICE, "Backup image %s", csImg); VERB(1) printf("Backup image %s\n", csImg); } - return 0; + return len; } int Clean(const char *csImg)