Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Yet more deduplication: now all code reads metadata with new function
Baptiste Daroussin committed 9 years ago
commit 1fdfd6003907b35fb314c9a784b41ae027c03bfc
parent ac0ad90
3 files changed +23 -77
modified libpkg/pkg.h.in
@@ -1134,7 +1134,7 @@ int pkg_create_from_manifest(const char *, pkg_formats, const char *,
 * Create package from stage install with a metadata directory
 */
int pkg_create_staged(const char *, pkg_formats, const char *, const char *, char *);
-
int pkg_load_metadata(struct pkg *, const char *, const char *, const char *);
+
int pkg_load_metadata(struct pkg *, const char *, const char *, const char *, const char *, bool);

/**
 * Download the latest repo db file and checks its signature if any
modified libpkg/pkg_create.c
@@ -123,8 +123,6 @@ pkg_create_from_dir(struct pkg *pkg, const char *root,
	UT_string *b;
	utstring_new(b);

-
	pkg_analyse_files(NULL, pkg, root);
-

	pkg_emit_manifest_buf(pkg, b, PKG_MANIFEST_EMIT_COMPACT, NULL);
	packing_append_buffer(pkg_archive, utstring_body(b), "+COMPACT_MANIFEST", utstring_len(b));
	utstring_clear(b);
@@ -228,9 +226,7 @@ pkg_create_from_manifest(const char *outdir, pkg_formats format,
{
	struct pkg	*pkg = NULL;
	struct packing	*pkg_archive = NULL;
-
	char		 arch[BUFSIZ];
	int		 ret = ENOMEM;
-
	struct pkg_manifest_key *keys = NULL;

	pkg_debug(1, "Creating package from stage directory: '%s'", rootdir);

@@ -239,20 +235,8 @@ pkg_create_from_manifest(const char *outdir, pkg_formats format,
		goto cleanup;
	}

-
	pkg_manifest_keys_new(&keys);
-
	if ((ret = pkg_parse_manifest_file(pkg, manifest, keys)) != EPKG_OK) {
-
		ret = EPKG_FATAL;
-
		goto cleanup;
-
	}
-

-
	/* if no arch autodetermine it */
-
	if (pkg->abi == NULL) {
-
		pkg_get_myarch(arch, BUFSIZ);
-
		pkg->abi = strdup(arch);
-
	}
-

-
	if (plist != NULL &&
-
	    ports_parse_plist(pkg, plist, rootdir) != EPKG_OK) {
+
	if ((ret = pkg_load_metadata(pkg, manifest, NULL, plist, rootdir, false))
+
	    != EPKG_OK) {
		ret = EPKG_FATAL;
		goto cleanup;
	}
@@ -269,9 +253,7 @@ pkg_create_from_manifest(const char *outdir, pkg_formats format,

cleanup:
	free(pkg);
-
	pkg_manifest_keys_free(keys);
	packing_finish(pkg_archive);
-

	return (ret);
}

@@ -320,37 +302,44 @@ pkg_load_message_from_file(int fd, struct pkg *pkg, const char *path)
}

int
-
pkg_load_metadata(struct pkg *pkg, const char *md_dir, const char *plist,
-
    const char *rootdir)
+
pkg_load_metadata(struct pkg *pkg, const char *mfile, const char *md_dir,
+
    const char *plist, const char *rootdir, bool testing)
{
	struct pkg_manifest_key *keys = NULL;
	char			 arch[BUFSIZ];
	regex_t			 preg;
	regmatch_t		 pmatch[2];
-
	int			 i, mfd, ret = EPKG_OK;
+
	int			 i, ret = EPKG_OK;
+
	int			 mfd = -1;
	size_t			 size;
	bool			 defaultarch = false;

-
	if ((mfd = open(md_dir, O_DIRECTORY|O_CLOEXEC)) == -1) {
+
	if (md_dir != NULL &&
+
	    (mfd = open(md_dir, O_DIRECTORY|O_CLOEXEC)) == -1) {
		pkg_emit_errno("open", md_dir);
		goto cleanup;
	}

	pkg_manifest_keys_new(&keys);

+
	if (mfile != NULL) {
+
		ret = pkg_parse_manifest_file(pkg, mfile, keys);
+
	}
+

	/* Load the manifest from the metadata directory */
-
	if ((ret = pkg_parse_manifest_fileat(mfd, pkg, "+MANIFEST", keys))
+
	if (mfile == NULL && mfd != -1 &&
+
	    (ret = pkg_parse_manifest_fileat(mfd, pkg, "+MANIFEST", keys))
	    != EPKG_OK) {
		ret = EPKG_FATAL;
		goto cleanup;
	}

	/* if no descriptions provided then try to get it from a file */
-
	if (pkg->desc == NULL)
+
	if (mfd != -1 && pkg->desc == NULL)
		pkg_load_from_file(mfd, pkg, PKG_DESC, "+DESC");

	/* if no message try to get it from a file */
-
	if (pkg->message == NULL) {
+
	if (mfd != -1 && pkg->message == NULL) {
		/* Try ucl version first */
		pkg_load_message_from_file(mfd, pkg, "+DISPLAY");
	}
@@ -390,6 +379,9 @@ pkg_load_metadata(struct pkg *pkg, const char *md_dir, const char *plist,
		regfree(&preg);
	}

+
	if (!testing)
+
		pkg_analyse_files(NULL, pkg, rootdir);
+

	if (developer_mode)
		pkg_suggest_arch(pkg, pkg->abi, defaultarch);

@@ -415,7 +407,8 @@ pkg_create_staged(const char *outdir, pkg_formats format, const char *rootdir,
	if ((ret = pkg_new(&pkg, PKG_FILE)) != EPKG_OK)
		goto cleanup;

-
	if ((ret = pkg_load_metadata(pkg, md_dir, plist, rootdir)) != EPKG_OK)
+
	if ((ret = pkg_load_metadata(pkg, NULL, md_dir, plist, rootdir, false))
+
	    != EPKG_OK)
		goto cleanup;

	/* Create the archive */
modified src/register.c
@@ -56,18 +56,12 @@ exec_register(int argc, char **argv)
	struct pkg	*pkg = NULL;
	struct pkgdb	*db  = NULL;

-
	struct pkg_manifest_key *keys = NULL;
-

-
	char		*arch = NULL;
-
	char		 myarch[BUFSIZ];
-

	const char	*plist      = NULL;
	const char	*mdir       = NULL;
	const char	*mfile      = NULL;
	const char	*input_path = NULL;
	const char	*location   = NULL;

-
	bool		 developer;
	bool		 legacy        = false;
	bool		 testing_mode  = false;

@@ -89,8 +83,6 @@ exec_register(int argc, char **argv)
		{ NULL,		0,			NULL,	0},
	};

-
	developer = pkg_object_bool(pkg_config_get("DEVELOPER_MODE"));
-

	if (pkg_new(&pkg, PKG_INSTALLED) != EPKG_OK)
		err(EX_OSERR, "malloc");

@@ -175,13 +167,6 @@ exec_register(int argc, char **argv)
		return (EX_USAGE);
	}

-
	if (mfile != NULL && plist != NULL) {
-
		warnx("-M incompatible with -f option");
-
		usage_register();
-
		pkg_free(pkg);
-
		return (EX_USAGE);
-
	}
-

	if (testing_mode && input_path != NULL) {
		warnx("-i incompatible with -t option");
		usage_register();
@@ -189,14 +174,7 @@ exec_register(int argc, char **argv)
		return (EX_USAGE);
	}

-
	if (mfile != NULL) {
-
		pkg_manifest_keys_new(&keys);
-
		ret = pkg_parse_manifest_file(pkg, mfile, keys);
-
		pkg_manifest_keys_free(keys);
-
	} else {
-
		ret = pkg_load_metadata(pkg, mdir, plist, input_path);
-
	}
-

+
	ret = pkg_load_metadata(pkg, mfile, mdir, plist, input_path, testing_mode);
	if (ret != EPKG_OK) {
		pkg_free(pkg);
		return (EX_IOERR);
@@ -215,31 +193,6 @@ exec_register(int argc, char **argv)
		return (EX_TEMPFAIL);
	}

-
	/*
-
	 * testing_mode allows updating the local package database
-
	 * without any check that the files etc. listed in the meta
-
	 * data actually exist on the system.  Inappropriate use of
-
	 * testing_mode can really screw things up.
-
	 */
-

-
	if (!testing_mode)
-
		pkg_analyse_files(db, pkg, input_path);
-

-
	pkg_get(pkg, PKG_ABI, &arch);
-
	if (arch == NULL) {
-
		/*
-
		 * do not take the one from configuration on purpose
-
		 * but the real abi of the package.
-
		 */
-
		pkg_get_myarch(myarch, BUFSIZ);
-
		if (developer)
-
			pkg_suggest_arch(pkg, myarch, true);
-
		pkg_set(pkg, PKG_ABI, myarch);
-
	} else {
-
		if (developer)
-
			pkg_suggest_arch(pkg, arch, false);
-
	}
-

	retcode = pkg_add_port(db, pkg, input_path, location, testing_mode);

	if (!legacy && retcode == EPKG_OK && messages != NULL) {