Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
separate the compression handling to be able to fallback to other compression when not supported
Baptiste Daroussin committed 15 years ago
commit 22687cf959c31a735478a9e5d8c33c1417c28b59
parent 7576856
1 file changed +41 -37
modified libpkg/pkg_create.c
@@ -17,6 +17,7 @@
#define METADATA_GLOB "+{DEINSTALL,INSTALL,MTREE_DIRS}"

static int pkg_create_from_dir(char *, const char *, struct archive *);
+
static const char * pkg_create_set_format(struct archive *, pkg_formats);

static int
pkg_create_from_dir(char *path, const char *root, struct archive *pkg_archive)
@@ -122,25 +123,6 @@ pkg_create(char *pkgname, pkg_formats format, const char *outdir, const char *ro
	char archive_path[MAXPATHLEN];
	const char *ext;

-
	switch (format) {
-
		case TAR:
-
			ext = "tar";
-
			break;
-
		case TGZ:
-
			ext = "tgz";
-
			break;
-
		case TBZ:
-
			ext = "tbz";
-
			break;
-
		case TXZ:
-
			ext = "txz";
-
			break;
-
		default:
-
			warnx("Unsupport format");
-
			return (-1);
-
			break; /* NOT REACHED */
-
	}
-

	pkg_dbdir = pkgdb_get_dir();

	if (pkgdb_init(&db, pkgname, MATCH_EXACT) == -1) {
@@ -156,36 +138,58 @@ pkg_create(char *pkgname, pkg_formats format, const char *outdir, const char *ro
		return (-1);
	}

+
	pkg_archive = archive_write_new();
+

+
	if ((ext = pkg_create_set_format(pkg_archive, format)) == NULL) {
+
		archive_write_finish(pkg_archive);
+
		warnx("Unsupport format");
+
		return (-1);
+
	}
+

	snprintf(namever, sizeof(namever), "%s-%s", pkg_name(&pkg), pkg_version(&pkg));
	printf("Creating package %s/%s.%s\n", outdir, namever, ext);
	snprintf(pkgpath, sizeof(pkgpath), "%s/%s/", pkg_dbdir, namever);
	snprintf(archive_path, sizeof(archive_path), "%s/%s.%s", outdir, namever, ext);

-
	pkg_archive = archive_write_new();
+
	archive_write_set_format_pax_restricted(pkg_archive);
+
	archive_write_open_filename(pkg_archive, archive_path);
+
	pkg_create_from_dir(pkgpath, rootdir, pkg_archive);
+
	archive_write_close(pkg_archive);
+
	archive_write_finish(pkg_archive);

+
	pkgdb_free(&db);
+
	return (0);
+
}
+

+

+
static const char *
+
pkg_create_set_format(struct archive *pkg_archive, pkg_formats format)
+
{
	switch (format) {
		case TAR:
			archive_write_set_compression_none(pkg_archive);
-
			break;
+
			return ("tar");
		case TGZ:
-
			archive_write_set_compression_gzip(pkg_archive);
-
			break;
+
			if (archive_write_set_compression_gzip(pkg_archive) != ARCHIVE_OK) {
+
				warnx("gzip compression is not supported trying plain tar");
+
				return (pkg_create_set_format(pkg_archive, TAR));
+
			} else {
+
				return ("tgz");
+
			}
		case TBZ:
-
			archive_write_set_compression_bzip2(pkg_archive);
-
			break;
+
			if (archive_write_set_compression_bzip2(pkg_archive) != ARCHIVE_OK) {
+
				warnx("bzip2 compression is not supported trying gzip");
+
				return (pkg_create_set_format(pkg_archive, TGZ));
+
			} else {
+
				return ("tbz");
+
			}
		case TXZ:
-
			if (archive_write_set_compression_lzma(pkg_archive) != ARCHIVE_OK) {
-
				warnx("%s", archive_error_string(pkg_archive));
+
			if (archive_write_set_compression_xz(pkg_archive) != ARCHIVE_OK) {
+
				warnx("xs compression is not supported trying bzip2");
+
				return (pkg_create_set_format(pkg_archive, TBZ));
+
			} else {
+
				return ("txz");
			}
-
			break;
	}
-

-
	archive_write_set_format_pax_restricted(pkg_archive);
-
	archive_write_open_filename(pkg_archive, archive_path);
-
	pkg_create_from_dir(pkgpath, rootdir, pkg_archive);
-
	archive_write_close(pkg_archive);
-
	archive_write_finish(pkg_archive);
-

-
	pkgdb_free(&db);
-
	return (0);
+
	return (NULL);
}