Improved unpacking package files, fixing bugs in configuration file

handling.

--HG--
extra : convert_revision : xtraeme%40gmail.com-20091021161617-c9d2jyhxlwlid4e6
This commit is contained in:
Juan RP 2009-10-21 18:16:17 +02:00
parent 7bfb24dad6
commit f99a154d7f

View File

@ -117,13 +117,16 @@ out:
* are some files that were in the old package that should be removed. * are some files that were in the old package that should be removed.
*/ */
#define EXTRACT_FLAGS ARCHIVE_EXTRACT_SECURE_NODOTDOT | \ #define EXTRACT_FLAGS ARCHIVE_EXTRACT_SECURE_NODOTDOT | \
ARCHIVE_EXTRACT_SECURE_SYMLINKS ARCHIVE_EXTRACT_SECURE_SYMLINKS | \
ARCHIVE_EXTRACT_NO_OVERWRITE | \
ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER
#define FEXTRACT_FLAGS ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM | \ #define FEXTRACT_FLAGS ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM | \
ARCHIVE_EXTRACT_TIME | EXTRACT_FLAGS ARCHIVE_EXTRACT_TIME | EXTRACT_FLAGS
static void static void
set_extract_flags(int *flags) set_extract_flags(int *flags)
{ {
*flags = 0;
if (getuid() == 0) if (getuid() == 0)
*flags = FEXTRACT_FLAGS; *flags = FEXTRACT_FLAGS;
else else
@ -132,12 +135,12 @@ set_extract_flags(int *flags)
static int static int
install_config_file(prop_dictionary_t d, struct archive_entry *entry, install_config_file(prop_dictionary_t d, struct archive_entry *entry,
const char *pkgname, int *flags, bool skip) const char *pkgname, int *flags, bool *skip)
{ {
prop_dictionary_t forigd; prop_dictionary_t forigd;
prop_object_t obj, obj2; prop_object_t obj, obj2;
prop_object_iterator_t iter, iter2; prop_object_iterator_t iter, iter2;
const char *cffile, *sha256_new; const char *cffile, *sha256_new = NULL;
char *buf, *sha256_cur = NULL, *sha256_orig = NULL; char *buf, *sha256_cur = NULL, *sha256_orig = NULL;
int rv = 0; int rv = 0;
bool install_new = false; bool install_new = false;
@ -170,31 +173,54 @@ install_config_file(prop_dictionary_t d, struct archive_entry *entry,
while ((obj2 = prop_object_iterator_next(iter2))) { while ((obj2 = prop_object_iterator_next(iter2))) {
prop_dictionary_get_cstring_nocopy(obj2, prop_dictionary_get_cstring_nocopy(obj2,
"file", &cffile); "file", &cffile);
if (strstr(archive_entry_pathname(entry), cffile)) { buf = xbps_xasprintf(".%s", cffile);
if (buf == NULL) {
prop_object_iterator_release(iter2);
rv = errno;
goto out;
}
if (strcmp(archive_entry_pathname(entry), buf) == 0) {
prop_dictionary_get_cstring(obj2, "sha256", prop_dictionary_get_cstring(obj2, "sha256",
&sha256_orig); &sha256_orig);
free(buf);
break; break;
} }
free(buf);
buf = NULL;
} }
prop_object_iterator_release(iter2); prop_object_iterator_release(iter2);
} }
prop_object_release(forigd); prop_object_release(forigd);
/*
* First case: original hash not found, install new file.
*/
if (sha256_orig == NULL) {
install_new = true;
goto out;
}
/* /*
* Compare original, installed and new hash for current file. * Compare original, installed and new hash for current file.
*/ */
while ((obj = prop_object_iterator_next(iter))) { while ((obj = prop_object_iterator_next(iter))) {
prop_dictionary_get_cstring_nocopy(obj, "file", &cffile); prop_dictionary_get_cstring_nocopy(obj, "file", &cffile);
if (strstr(archive_entry_pathname(entry), cffile) == 0)
continue;
buf = xbps_xasprintf(".%s", cffile); buf = xbps_xasprintf(".%s", cffile);
if (buf == NULL) { if (buf == NULL) {
prop_object_iterator_release(iter); prop_object_iterator_release(iter);
return errno; return errno;
} }
prop_dictionary_get_cstring_nocopy(obj, "sha256", &sha256_new); if (strcmp(archive_entry_pathname(entry), buf)) {
free(buf);
buf = NULL;
continue;
}
sha256_cur = xbps_get_file_hash(buf); sha256_cur = xbps_get_file_hash(buf);
free(buf); free(buf);
prop_dictionary_get_cstring_nocopy(obj, "sha256", &sha256_new);
if (sha256_new == NULL) {
rv = EINVAL;
break;
}
if (sha256_cur == NULL) { if (sha256_cur == NULL) {
if (errno == ENOENT) { if (errno == ENOENT) {
/* /*
@ -214,8 +240,8 @@ install_config_file(prop_dictionary_t d, struct archive_entry *entry,
* Install new file. * Install new file.
*/ */
if ((strcmp(sha256_orig, sha256_cur) == 0) && if ((strcmp(sha256_orig, sha256_cur) == 0) &&
(strcmp(sha256_cur, sha256_new) == 0) && (strcmp(sha256_orig, sha256_new) == 0) &&
(strcmp(sha256_orig, sha256_new) == 0)) { (strcmp(sha256_cur, sha256_new) == 0)) {
install_new = true; install_new = true;
break; break;
/* /*
@ -224,8 +250,10 @@ install_config_file(prop_dictionary_t d, struct archive_entry *entry,
* Install new file. * Install new file.
*/ */
} else if ((strcmp(sha256_orig, sha256_cur) == 0) && } else if ((strcmp(sha256_orig, sha256_cur) == 0) &&
(strcmp(sha256_cur, sha256_new)) && (strcmp(sha256_orig, sha256_new)) &&
(strcmp(sha256_orig, sha256_new))) { (strcmp(sha256_cur, sha256_new))) {
printf("Updating %s file with new version.\n",
cffile);
install_new = true; install_new = true;
break; break;
/* /*
@ -233,19 +261,20 @@ install_config_file(prop_dictionary_t d, struct archive_entry *entry,
* *
* Keep current file as is. * Keep current file as is.
*/ */
} else if ((strcmp(sha256_orig, sha256_cur)) && } else if ((strcmp(sha256_orig, sha256_new) == 0) &&
(strcmp(sha256_orig, sha256_new) == 0) && (strcmp(sha256_cur, sha256_new)) &&
(strcmp(sha256_cur, sha256_new))) { (strcmp(sha256_orig, sha256_cur))) {
skip = true; printf("Keeping modified file %s.\n", cffile);
*skip = true;
break; break;
/* /*
* Orig = X, Curr = Y, New = Y * Orig = X, Curr = Y, New = Y
* *
* Install new file. * Install new file.
*/ */
} else if ((strcmp(sha256_orig, sha256_cur)) && } else if ((strcmp(sha256_cur, sha256_new) == 0) &&
(strcmp(sha256_cur, sha256_new) == 0) && (strcmp(sha256_orig, sha256_new)) &&
(strcmp(sha256_orig, sha256_new))) { (strcmp(sha256_orig, sha256_cur))) {
install_new = true; install_new = true;
break; break;
/* /*
@ -261,8 +290,8 @@ install_config_file(prop_dictionary_t d, struct archive_entry *entry,
rv = errno; rv = errno;
break; break;
} }
printf("Installing new configuration " printf("Keeping modified file %s.\n", cffile);
"file %s.new\n", cffile); printf("Installing new version as %s.new.\n", cffile);
install_new = true; install_new = true;
archive_entry_set_pathname(entry, buf); archive_entry_set_pathname(entry, buf);
free(buf); free(buf);
@ -271,8 +300,10 @@ install_config_file(prop_dictionary_t d, struct archive_entry *entry,
} }
out: out:
if (install_new) if (install_new) {
set_extract_flags(flags); *flags &= ~ARCHIVE_EXTRACT_NO_OVERWRITE;
*flags &= ~ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
}
if (sha256_orig) if (sha256_orig)
free(sha256_orig); free(sha256_orig);
if (sha256_cur) if (sha256_cur)
@ -293,7 +324,7 @@ unpack_archive_fini(struct archive *ar, prop_dictionary_t pkg,
{ {
prop_dictionary_t filesd; prop_dictionary_t filesd;
struct archive_entry *entry; struct archive_entry *entry;
const char *pkgname, *version, *rootdir; const char *pkgname, *version, *rootdir, *entry_str;
char *buf, *buf2; char *buf, *buf2;
int rv = 0, flags, lflags; int rv = 0, flags, lflags;
bool actgt = false, skip_entry = false; bool actgt = false, skip_entry = false;
@ -313,28 +344,24 @@ unpack_archive_fini(struct archive *ar, prop_dictionary_t pkg,
prop_dictionary_get_cstring_nocopy(pkg, "version", &version); prop_dictionary_get_cstring_nocopy(pkg, "version", &version);
while (archive_read_next_header(ar, &entry) == ARCHIVE_OK) { while (archive_read_next_header(ar, &entry) == ARCHIVE_OK) {
lflags = 0; entry_str = archive_entry_pathname(entry);
set_extract_flags(&lflags); set_extract_flags(&lflags);
/* if (((strcmp("./INSTALL", entry_str)) == 0) ||
* Always overwrite pkg metadata files. Other files ((strcmp("./REMOVE", entry_str)) == 0) ||
* in the archive aren't overwritten unless a package ((strcmp("./files.plist", entry_str))) == 0 ||
* defines the "essential" boolean obj, or the ((strcmp("./props.plist", entry_str)) == 0) || essential) {
* XBPS_FLAG_FORCE is specified. /*
*/ * Always overwrite files in essential packages,
if (strcmp("./INSTALL", archive_entry_pathname(entry)) && * and metadata files.
strcmp("./REMOVE", archive_entry_pathname(entry)) && */
strcmp("./files.plist", archive_entry_pathname(entry)) && lflags &= ~ARCHIVE_EXTRACT_NO_OVERWRITE;
strcmp("./props.plist", archive_entry_pathname(entry))) { lflags &= ~ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
if (((flags & XBPS_FLAG_FORCE) == 0) && !essential) {
lflags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
lflags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
}
} }
/* /*
* Run the pre INSTALL action if the file is there. * Run the pre INSTALL action if the file is there.
*/ */
if (strcmp("./INSTALL", archive_entry_pathname(entry)) == 0) { if (strcmp("./INSTALL", entry_str) == 0) {
buf = xbps_xasprintf(".%s/metadata/%s/INSTALL", buf = xbps_xasprintf(".%s/metadata/%s/INSTALL",
XBPS_META_PATH, pkgname); XBPS_META_PATH, pkgname);
if (buf == NULL) if (buf == NULL)
@ -364,8 +391,7 @@ unpack_archive_fini(struct archive *ar, prop_dictionary_t pkg,
/* /*
* Unpack metadata files in final directory. * Unpack metadata files in final directory.
*/ */
} else if (strcmp("./REMOVE", } else if (strcmp("./REMOVE", entry_str) == 0) {
archive_entry_pathname(entry)) == 0) {
buf2 = xbps_xasprintf(".%s/metadata/%s/REMOVE", buf2 = xbps_xasprintf(".%s/metadata/%s/REMOVE",
XBPS_META_PATH, pkgname); XBPS_META_PATH, pkgname);
if (buf2 == NULL) if (buf2 == NULL)
@ -373,9 +399,7 @@ unpack_archive_fini(struct archive *ar, prop_dictionary_t pkg,
archive_entry_set_pathname(entry, buf2); archive_entry_set_pathname(entry, buf2);
free(buf2); free(buf2);
buf2 = NULL; buf2 = NULL;
} else if (strcmp("./files.plist", entry_str) == 0) {
} else if (strcmp("./files.plist",
archive_entry_pathname(entry)) == 0) {
/* /*
* Now we have a dictionary from the entry * Now we have a dictionary from the entry
* in memory. Will be written to disk later, when * in memory. Will be written to disk later, when
@ -388,28 +412,29 @@ unpack_archive_fini(struct archive *ar, prop_dictionary_t pkg,
/* Pass to next entry */ /* Pass to next entry */
continue; continue;
} else if (strcmp("./props.plist", } else if (strcmp("./props.plist", entry_str) == 0) {
archive_entry_pathname(entry)) == 0) {
buf2 = xbps_xasprintf(".%s/metadata/%s/props.plist", buf2 = xbps_xasprintf(".%s/metadata/%s/props.plist",
XBPS_META_PATH, pkgname); XBPS_META_PATH, pkgname);
if (buf2 == NULL) if (buf2 == NULL)
return errno; return errno;
archive_entry_set_pathname(entry, buf2); archive_entry_set_pathname(entry, buf2);
free(buf2); free(buf2);
} else {
/*
* Handle configuration files.
*/
if ((rv = install_config_file(filesd, entry, pkgname,
&lflags, &skip_entry)) != 0) {
prop_object_release(filesd);
return rv;
}
if (skip_entry) {
archive_read_data_skip(ar);
skip_entry = false;
continue;
}
} }
/*
* Handle configuration files.
*/
if ((rv = install_config_file(filesd, entry, pkgname,
&lflags, skip_entry)) != 0) {
prop_object_release(filesd);
return rv;
}
if (skip_entry) {
archive_read_data_skip(ar);
skip_entry = false;
continue;
}
/* /*
* Extract entry from archive. * Extract entry from archive.
*/ */