[pacman-dev] [PATCH v2 2/2] Save backup files with extension .pacsave.n
Teach pacman to save backup files with extension .pacsave.n, where n is a positive integer. The current backup file shall be saved as <name>.pacsave, while existing .pacsave.n files will be renamed to <name>.pacsave.n+1 Example: 1. You have subversion installed in your local repo. /etc/conf.d/svnserve is a file to be backed up. It contains local modifications 2. You remove subversion from your repo. /etc/conf.d/svnserve is backed up as /etc/conf.d/svnserve.pacsave 2. You install subversion again 3. You edit /etc/conf.d/svnserve 4. You remove subversion. The existing /etc/conf.d/svnserve.pacsave is renamed to /etc/conf.d/svnserve.pacsave.1 and /etc/conf.d/svnserve is backed up as /etc/conf.d/svnserve.pacsave Signed-off-by: Pang Yan Han <pangyanhan@gmail.com> --- lib/libalpm/remove.c | 75 ++++++++++++++++++++++++++++++++++++++++ test/pacman/tests/remove012.py | 20 +++++++++++ 2 files changed, 95 insertions(+), 0 deletions(-) create mode 100644 test/pacman/tests/remove012.py diff --git a/lib/libalpm/remove.c b/lib/libalpm/remove.c index 83c437f..22c4e23 100644 --- a/lib/libalpm/remove.c +++ b/lib/libalpm/remove.c @@ -28,8 +28,11 @@ #include <errno.h> #include <string.h> #include <limits.h> +#include <dirent.h> +#include <regex.h> #include <unistd.h> #include <sys/stat.h> +#include <sys/types.h> /* libalpm */ #include "remove.h" @@ -218,6 +221,77 @@ static int can_remove_file(alpm_handle_t *handle, const alpm_file_t *file, return 1; } +static void shift_pacsave(alpm_handle_t *handle, const char *file) +{ + DIR *dir = NULL; + struct dirent *ent; + struct stat st; + regex_t reg; + + const char *basename; + char *dirname; + char oldfile[PATH_MAX]; + char newfile[PATH_MAX]; + char regstr[PATH_MAX]; + + unsigned long log_max = 0; + size_t basename_len; + + dirname = _alpm_mdirname(file); + if (!dirname) { + return; + } + + basename = _alpm_mbasename(file); + basename_len = strlen(basename); + + snprintf(regstr, PATH_MAX, "^%s\\.pacsave\\.([[:digit:]]+)$", basename); + if (regcomp(®, regstr, REG_EXTENDED | REG_NEWLINE) != 0) { + goto cleanup; + } + + dir = opendir(dirname); + if (dir == NULL) { + _alpm_log(handle, ALPM_LOG_ERROR, _("could not open directory: %s: %s\n"), + dirname, strerror(errno)); + goto cleanup; + } + + while ((ent = readdir(dir)) != NULL) { + if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { + continue; + } + + if (regexec(®, ent->d_name, 0, 0, 0) == 0) { + unsigned long cur_log; + cur_log = strtoul(ent->d_name + basename_len + strlen(".pacsave."), NULL, 10); + if (cur_log > log_max) { + log_max = cur_log; + } + } + } + + /* Shift pacsaves */ + unsigned long i; + for (i = log_max + 1; i > 1; i--) { + snprintf(oldfile, PATH_MAX, "%s.pacsave.%lu", file, i-1); + snprintf(newfile, PATH_MAX, "%s.pacsave.%lu", file, i); + rename(oldfile, newfile); + } + + snprintf(oldfile, PATH_MAX, "%s.pacsave", file); + if (stat(oldfile, &st) == 0) { + snprintf(newfile, PATH_MAX, "%s.1", oldfile); + rename(oldfile, newfile); + } + + regfree(®); + +cleanup: + free(dirname); + closedir(dir); +} + /* Helper function for iterating through a package's file and deleting them * Used by _alpm_remove_commit. */ static void unlink_file(alpm_handle_t *handle, alpm_pkg_t *info, @@ -298,6 +372,7 @@ static void unlink_file(alpm_handle_t *handle, alpm_pkg_t *info, FREE(filehash); if(cmp != 0) { char newpath[PATH_MAX]; + shift_pacsave(handle, file); snprintf(newpath, PATH_MAX, "%s.pacsave", file); rename(file, newpath); _alpm_log(handle, ALPM_LOG_WARNING, _("%s saved as %s\n"), file, newpath); diff --git a/test/pacman/tests/remove012.py b/test/pacman/tests/remove012.py new file mode 100644 index 0000000..4274487 --- /dev/null +++ b/test/pacman/tests/remove012.py @@ -0,0 +1,20 @@ +self.description = "Remove a package with a modified file marked for backup and has existing pacsaves" + +self.filesystem = ["etc/dummy.conf.pacsave", + "etc/dummy.conf.pacsave.1", + "etc/dummy.conf.pacsave.2"] + +p1 = pmpkg("dummy") +p1.files = ["etc/dummy.conf*"] +p1.backup = ["etc/dummy.conf"] +self.addpkg2db("local", p1) + +self.args = "-R %s" % p1.name + +self.addrule("PACMAN_RETCODE=0") +self.addrule("!PKG_EXIST=dummy") +self.addrule("!FILE_EXIST=etc/dummy.conf") +self.addrule("FILE_PACSAVE=etc/dummy.conf") +self.addrule("FILE_EXIST=etc/dummy.conf.pacsave.1") +self.addrule("FILE_EXIST=etc/dummy.conf.pacsave.2") +self.addrule("FILE_EXIST=etc/dummy.conf.pacsave.3") -- 1.7.6
participants (1)
-
Pang Yan Han