summaryrefslogtreecommitdiffstats
path: root/lib/libalpm/add.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libalpm/add.c')
-rw-r--r--lib/libalpm/add.c126
1 files changed, 60 insertions, 66 deletions
diff --git a/lib/libalpm/add.c b/lib/libalpm/add.c
index 6c2f0cb6..014990d2 100644
--- a/lib/libalpm/add.c
+++ b/lib/libalpm/add.c
@@ -18,11 +18,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "config.h"
-
#include <stdlib.h>
#include <errno.h>
-#include <time.h>
#include <string.h>
#include <limits.h>
#include <fcntl.h>
@@ -132,6 +129,18 @@ static int perform_extraction(alpm_handle_t *handle, struct archive *archive,
return 0;
}
+static int try_rename(alpm_handle_t *handle, const char *src, const char *dest)
+{
+ if(rename(src, dest)) {
+ _alpm_log(handle, ALPM_LOG_ERROR, _("could not rename %s to %s (%s)\n"),
+ src, dest, strerror(errno));
+ alpm_logaction(handle, "error: could not rename %s to %s (%s)\n",
+ src, dest, strerror(errno));
+ return 1;
+ }
+ return 0;
+}
+
static int extract_single_file(alpm_handle_t *handle, struct archive *archive,
struct archive_entry *entry, alpm_pkg_t *newpkg, alpm_pkg_t *oldpkg)
{
@@ -146,8 +155,6 @@ static int extract_single_file(alpm_handle_t *handle, struct archive *archive,
entryname = archive_entry_pathname(entry);
entrymode = archive_entry_mode(entry);
- memset(filename, 0, PATH_MAX); /* just to be sure */
-
if(strcmp(entryname, ".INSTALL") == 0) {
/* the install script goes inside the db */
snprintf(filename, PATH_MAX, "%s%s-%s/install",
@@ -170,7 +177,7 @@ static int extract_single_file(alpm_handle_t *handle, struct archive *archive,
}
/* if a file is in NoExtract then we never extract it */
- if(alpm_list_find_str(handle->noextract, entryname)) {
+ if(alpm_list_find(handle->noextract, entryname, _alpm_fnmatch)) {
_alpm_log(handle, ALPM_LOG_DEBUG, "%s is in NoExtract, skipping extraction\n",
entryname);
alpm_logaction(handle, "note: %s is in NoExtract, skipping extraction\n",
@@ -250,7 +257,7 @@ static int extract_single_file(alpm_handle_t *handle, struct archive *archive,
} else if(S_ISREG(entrymode)) {
/* case 4,7: */
/* if file is in NoUpgrade, don't touch it */
- if(alpm_list_find_str(handle->noupgrade, entryname)) {
+ if(alpm_list_find(handle->noupgrade, entryname, _alpm_fnmatch)) {
notouch = 1;
} else {
alpm_backup_t *backup;
@@ -282,17 +289,18 @@ static int extract_single_file(alpm_handle_t *handle, struct archive *archive,
STRDUP(entryname_orig, entryname, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
if(needbackup) {
- char checkfile[PATH_MAX];
+ char *checkfile;
char *hash_local = NULL, *hash_pkg = NULL;
- int ret;
+ size_t len;
- snprintf(checkfile, PATH_MAX, "%s.paccheck", filename);
+ len = strlen(filename) + 10;
+ MALLOC(checkfile, len,
+ errors++; handle->pm_errno = ALPM_ERR_MEMORY; goto needbackup_cleanup);
+ snprintf(checkfile, len, "%s.paccheck", filename);
- ret = perform_extraction(handle, archive, entry, checkfile, entryname_orig);
- if(ret == 1) {
- /* error */
- FREE(entryname_orig);
- return 1;
+ if(perform_extraction(handle, archive, entry, checkfile, entryname_orig)) {
+ errors++;
+ goto needbackup_cleanup;
}
hash_local = alpm_compute_md5sum(filename);
@@ -320,29 +328,26 @@ static int extract_single_file(alpm_handle_t *handle, struct archive *archive,
if(hash_local && hash_pkg && strcmp(hash_local, hash_pkg) != 0) {
/* looks like we have a local file that has a different hash as the
* file in the package, move it to a .pacorig */
- char newpath[PATH_MAX];
- snprintf(newpath, PATH_MAX, "%s.pacorig", filename);
+ char *newpath;
+ size_t newlen = strlen(filename) + 9;
+ MALLOC(newpath, newlen,
+ errors++; handle->pm_errno = ALPM_ERR_MEMORY; goto needbackup_cleanup);
+ snprintf(newpath, newlen, "%s.pacorig", filename);
/* move the existing file to the "pacorig" */
- if(rename(filename, newpath)) {
- _alpm_log(handle, ALPM_LOG_ERROR, _("could not rename %s to %s (%s)\n"),
- filename, newpath, strerror(errno));
- alpm_logaction(handle, "error: could not rename %s to %s (%s)\n",
- filename, newpath, strerror(errno));
+ if(try_rename(handle, filename, newpath)) {
+ errors++;
errors++;
} else {
/* rename the file we extracted to the real name */
- if(rename(checkfile, filename)) {
- _alpm_log(handle, ALPM_LOG_ERROR, _("could not rename %s to %s (%s)\n"),
- checkfile, filename, strerror(errno));
- alpm_logaction(handle, "error: could not rename %s to %s (%s)\n",
- checkfile, filename, strerror(errno));
+ if(try_rename(handle, checkfile, filename)) {
errors++;
} else {
_alpm_log(handle, ALPM_LOG_WARNING, _("%s saved as %s\n"), filename, newpath);
alpm_logaction(handle, "warning: %s saved as %s\n", filename, newpath);
}
}
+ free(newpath);
} else {
/* local file is identical to pkg one, so just remove pkg one */
unlink(checkfile);
@@ -356,11 +361,7 @@ static int extract_single_file(alpm_handle_t *handle, struct archive *archive,
_alpm_log(handle, ALPM_LOG_DEBUG, "action: installing new file: %s\n",
entryname_orig);
- if(rename(checkfile, filename)) {
- _alpm_log(handle, ALPM_LOG_ERROR, _("could not rename %s to %s (%s)\n"),
- checkfile, filename, strerror(errno));
- alpm_logaction(handle, "error: could not rename %s to %s (%s)\n",
- checkfile, filename, strerror(errno));
+ if(try_rename(handle, checkfile, filename)) {
errors++;
}
} else {
@@ -382,29 +383,30 @@ static int extract_single_file(alpm_handle_t *handle, struct archive *archive,
_alpm_log(handle, ALPM_LOG_DEBUG, "action: leaving existing file in place\n");
unlink(checkfile);
} else {
- char newpath[PATH_MAX];
+ char *newpath;
+ size_t newlen = strlen(filename) + 8;
_alpm_log(handle, ALPM_LOG_DEBUG, "action: keeping current file and installing"
" new one with .pacnew ending\n");
- snprintf(newpath, PATH_MAX, "%s.pacnew", filename);
- if(rename(checkfile, newpath)) {
- _alpm_log(handle, ALPM_LOG_ERROR, _("could not install %s as %s (%s)\n"),
- filename, newpath, strerror(errno));
- alpm_logaction(handle, "error: could not install %s as %s (%s)\n",
- filename, newpath, strerror(errno));
+ MALLOC(newpath, newlen,
+ errors++; handle->pm_errno = ALPM_ERR_MEMORY; goto needbackup_cleanup);
+ snprintf(newpath, newlen, "%s.pacnew", filename);
+ if(try_rename(handle, checkfile, newpath)) {
+ errors++;
} else {
_alpm_log(handle, ALPM_LOG_WARNING, _("%s installed as %s\n"),
filename, newpath);
alpm_logaction(handle, "warning: %s installed as %s\n",
filename, newpath);
}
+ free(newpath);
}
}
- FREE(hash_local);
- FREE(hash_pkg);
+needbackup_cleanup:
+ free(checkfile);
+ free(hash_local);
+ free(hash_pkg);
} else {
- int ret;
-
/* we didn't need a backup */
if(notouch) {
/* change the path to a .pacnew extension */
@@ -423,11 +425,11 @@ static int extract_single_file(alpm_handle_t *handle, struct archive *archive,
unlink(filename);
}
- ret = perform_extraction(handle, archive, entry, filename, entryname_orig);
- if(ret == 1) {
+ if(perform_extraction(handle, archive, entry, filename, entryname_orig)) {
/* error */
- FREE(entryname_orig);
- return 1;
+ free(entryname_orig);
+ errors++;
+ return errors;
}
/* calculate an hash if this is in newpkg's backup */
@@ -444,7 +446,7 @@ static int extract_single_file(alpm_handle_t *handle, struct archive *archive,
backup->hash = newhash;
}
}
- FREE(entryname_orig);
+ free(entryname_orig);
return errors;
}
@@ -521,31 +523,20 @@ static int commit_single_pkg(alpm_handle_t *handle, alpm_pkg_t *newpkg,
if(!(trans->flags & ALPM_TRANS_FLAG_DBONLY)) {
struct archive *archive;
struct archive_entry *entry;
- int cwdfd;
+ struct stat buf;
+ int fd, cwdfd;
_alpm_log(handle, ALPM_LOG_DEBUG, "extracting files\n");
- if((archive = archive_read_new()) == NULL) {
- handle->pm_errno = ALPM_ERR_LIBARCHIVE;
- ret = -1;
- goto cleanup;
- }
-
- archive_read_support_compression_all(archive);
- archive_read_support_format_all(archive);
-
- _alpm_log(handle, ALPM_LOG_DEBUG, "archive: %s\n", pkgfile);
- if(archive_read_open_filename(archive, pkgfile,
- ALPM_BUFFER_SIZE) != ARCHIVE_OK) {
- handle->pm_errno = ALPM_ERR_PKG_OPEN;
+ fd = _alpm_open_archive(db->handle, pkgfile, &buf,
+ &archive, ALPM_ERR_PKG_OPEN);
+ if(fd < 0) {
ret = -1;
goto cleanup;
}
/* save the cwd so we can restore it later */
- do {
- cwdfd = open(".", O_RDONLY);
- } while(cwdfd == -1 && errno == EINTR);
+ OPEN(cwdfd, ".", O_RDONLY);
if(cwdfd < 0) {
_alpm_log(handle, ALPM_LOG_ERROR, _("could not get current working directory\n"));
}
@@ -554,6 +545,8 @@ static int commit_single_pkg(alpm_handle_t *handle, alpm_pkg_t *newpkg,
if(chdir(handle->root) != 0) {
_alpm_log(handle, ALPM_LOG_ERROR, _("could not change directory to %s (%s)\n"),
handle->root, strerror(errno));
+ archive_read_finish(archive);
+ CLOSE(fd);
ret = -1;
goto cleanup;
}
@@ -595,6 +588,7 @@ static int commit_single_pkg(alpm_handle_t *handle, alpm_pkg_t *newpkg,
errors += extract_single_file(handle, archive, entry, newpkg, oldpkg);
}
archive_read_finish(archive);
+ CLOSE(fd);
/* restore the old cwd if we have it */
if(cwdfd >= 0) {
@@ -602,7 +596,7 @@ static int commit_single_pkg(alpm_handle_t *handle, alpm_pkg_t *newpkg,
_alpm_log(handle, ALPM_LOG_ERROR,
_("could not restore working directory (%s)\n"), strerror(errno));
}
- close(cwdfd);
+ CLOSE(cwdfd);
}
if(errors) {