#include "global.h" #include "upd.h" static inline int ChkImg(const char *csImg, char *psDir) { int res = 0; getcwd(psDir, MAXPATHLEN); if (access(csImg, R_OK) == -1) { printf("Error:: Unable to find new image %s #%d - %s\n", csImg, errno, strerror(errno)); res = -1; } else { strlcat(psDir, "/", MAXPATHLEN); strlcat(psDir, csImg, MAXPATHLEN); } return res; } // ------------------------------- int Activate(const char *csImg) { char szDir[MAXPATHLEN]; if (ChkImg(csImg, szDir) == -1) return -1; VERB(3) printf("Activate procedure for %s\n", szDir); unlink(FIRMWARE_IMG); if (symlink(szDir, FIRMWARE_IMG) == -1) { printf("Error:: Unable to activate new image %s #%d - %s\n", csImg, errno, strerror(errno)); return -2; } syslog(LOG_NOTICE, "Activate new image %s", csImg); VERB(1) printf("Activate new image %s\n", csImg); return 0; } int Install() { VERB(3) printf("Install procedure\n"); return 0; } int Rollback(const char *csImg) { char szDir[MAXPATHLEN]; if (ChkImg(csImg, szDir) == -1) return -1; VERB(3) printf("Rollback procedure for %s\n", szDir); return 0; } int tFTP(const char *csImg, const char *psDir) { int res = 0; char szDir[MAXPATHLEN]; if (ChkImg(csImg, szDir) == -1) return -1; VERB(3) printf("tFTP procedure for %s to %s\n", szDir, psDir); res = Backup(csImg, psDir); if (!res) { syslog(LOG_NOTICE, "Export tFTP image %s to %s", csImg, psDir); VERB(1) printf("Export tFTP image %s to %s\n", csImg, psDir); } return res; } int Backup(const char *csImg, const char *psDir) { int src, dst, len; u_char buf[BUFSIZ]; char szDir[MAXPATHLEN], szFile[MAXPATHLEN]; if (ChkImg(csImg, szDir) == -1) return -1; if (!psDir) getcwd(szFile, MAXPATHLEN); else strlcpy(szFile, psDir, MAXPATHLEN); strlcat(szFile, "/", MAXPATHLEN); strlcat(szFile, FIRMWARE_BAK, MAXPATHLEN); if (!psDir) VERB(3) printf("Backup procedure for %s\n", szDir); dst = open(szFile, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (dst == -1) { printf("Error:: in create backup %s #%d - %s\n", szFile, errno, strerror(errno)); return -1; } src = open(szDir, O_RDONLY); if (src == -1) { printf("Error:: in open image %s #%d - %s\n", szDir, errno, strerror(errno)); close(dst); unlink(szFile); return -1; } while ((len = read(src, buf, BUFSIZ)) > 0) if (write(dst, buf, len) == -1) { printf("Error:: in write backup #%d - %s\n", errno, strerror(errno)); close(src); close(dst); unlink(szFile); } close(src); close(dst); if (!psDir) { syslog(LOG_NOTICE, "Backup image %s", csImg); VERB(1) printf("Backup image %s\n", csImg); } return 0; } int Clean(const char *csImg) { char szDir[MAXPATHLEN], szFile[MAXPATHLEN]; if (ChkImg(csImg, szDir) == -1) return -1; getcwd(szFile, MAXPATHLEN); strlcat(szFile, "/", MAXPATHLEN); strlcat(szFile, FIRMWARE_BAK, MAXPATHLEN); VERB(3) printf("Clean procedure for %s\n", szDir); if (unlink(szFile) == -1) { printf("Error:: in clean backup #%d - %s\n", errno, strerror(errno)); return -1; } syslog(LOG_NOTICE, "Clean backup for image %s", csImg); VERB(1) printf("Clean backup for image %s\n", csImg); return 0; }