Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Split pkg_load_metadata into small internal function
Baptiste Daroussin committed 6 years ago
commit ee95aca2fd8f96090195b14f9739e71d5f11f271
parent c5b01d0
1 file changed +82 -67
modified libpkg/pkg_create.c
@@ -295,93 +295,75 @@ pkg_load_message_from_file(int fd, struct pkg *pkg, const char *path)
	return (ret);
}

-
static void
-
fixup_abi(struct pkg *pkg, const char *rootdir, bool testing)
+
/* TODO use file descriptor for rootdir */
+
static int
+
load_manifest(struct pkg *pkg, const char *metadata, const char *plist,
+
    struct pkg_manifest_key *keys, const char *rootdir)
{
-
	bool defaultarch = false;
-
	const char *arch;
-

-
	/* if no arch autodetermine it */
-
	if (pkg->abi == NULL) {
-
#ifdef __FreeBSD__
-
		char *osversion;
-
		xasprintf(&osversion, "%d", ctx.osversion);
-
		pkg_kv_add(&pkg->annotations, "FreeBSD_version", osversion, "annotation");
-
#endif
-
		arch = pkg_object_string(pkg_config_get("ABI"));
-
		pkg->abi = xstrdup(arch);
-
		defaultarch = true;
-
	}
+
	int ret;

-
	if (!testing)
-
		pkg_analyse_files(NULL, pkg, rootdir);
+
	ret = pkg_parse_manifest_file(pkg, metadata, keys);

-
	if (ctx.developer_mode)
-
		suggest_arch(pkg, defaultarch);
+
	if (ret == EPKG_OK && plist != NULL)
+
		ret = ports_parse_plist(pkg, plist, rootdir);
+
	return (ret);
}

-
int
-
pkg_load_metadata(struct pkg *pkg, const char *mfile, const char *md_dir,
-
    const char *plist, const char *rootdir, bool testing)
+
/* TODO use file descriptor for rootdir */
+
static int
+
load_metadata(struct pkg *pkg, const char *metadata, const char *plist,
+
    const char *rootdir)
{
	struct pkg_manifest_key *keys = NULL;
-
	regex_t			 preg;
-
	regmatch_t		 pmatch[2];
-
	int			 i, ret = EPKG_OK;
-
	int			 mfd = -1;
-
	size_t			 size;
-

-
	if (md_dir != NULL &&
-
	    (mfd = open(md_dir, O_DIRECTORY|O_CLOEXEC)) == -1) {
-
		pkg_emit_errno("open", md_dir);
-
		goto cleanup;
-
	}
+
	regex_t preg;
+
	regmatch_t pmatch[2];
+
	size_t size;
+
	int fd, i;

	pkg_manifest_keys_new(&keys);

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

-
	/* Load the manifest from the metadata directory */
-
	if (mfile == NULL && mfd != -1 &&
-
	    (ret = pkg_parse_manifest_fileat(mfd, pkg, "+MANIFEST", keys))
-
	    != EPKG_OK) {
-
		ret = EPKG_FATAL;
-
		goto cleanup;
+
	/* Let's see if we have a directory or a manifest */
+
	if ((fd = open(metadata, O_DIRECTORY|O_CLOEXEC)) == -1) {
+
		if (errno == ENOTDIR)
+
			return (load_manifest(pkg, metadata, plist, keys, rootdir));
+
		pkg_emit_errno("open", metadata);
+
		pkg_manifest_keys_free(keys);
+
		return (EPKG_FATAL);
	}

-
	/* if no descriptions provided then try to get it from a file */
-
	if (mfd != -1 && pkg->desc == NULL)
-
		pkg_set_from_fileat(mfd, pkg, PKG_DESC, "+DESC", false);
+
	/* First load the message before what ever in the manifest for backward compat */
+
	if (pkg->message == NULL)
+
		pkg_load_message_from_file(fd, pkg, "+DISPLAY");

-
	/* if no message try to get it from a file */
-
	if (mfd != -1 && pkg->message == NULL) {
-
		/* Try ucl version first */
-
		pkg_load_message_from_file(mfd, pkg, "+DISPLAY");
+
	if ((pkg_parse_manifest_fileat(fd, pkg, "+MANIFEST", keys)) != EPKG_OK) {
+
		pkg_manifest_keys_free(keys);
+
		close(fd);
+
		return (EPKG_FATAL);
+
	}
+
	pkg_manifest_keys_free(keys);
+
	if (plist != NULL && ports_parse_plist(pkg, plist, rootdir) != EPKG_OK) {
+
		return (EPKG_FATAL);
	}

+
	if (pkg->desc == NULL)
+
		pkg_set_from_fileat(fd, pkg, PKG_DESC, "+DESC", false);
+

	for (i = 0; scripts[i] != NULL; i++) {
-
		if (faccessat(mfd, scripts[i], F_OK, 0) == 0)
-
			pkg_addscript_fileat(mfd, pkg, scripts[i]);
+
		if (faccessat(fd, scripts[i], F_OK, 0) == 0)
+
			pkg_addscript_fileat(fd, pkg, scripts[i]);
	}

	for (i = 0; lua_scripts[i] != NULL; i++) {
-
		if (faccessat(mfd, lua_scripts[i], F_OK, 0) == 0)
-
			pkg_addluascript_fileat(mfd, pkg, lua_scripts[i]);
+
		if (faccessat(fd, lua_scripts[i], F_OK, 0) == 0)
+
			pkg_addluascript_fileat(fd, pkg, lua_scripts[i]);
	}

-
	if (plist != NULL &&
-
	    ports_parse_plist(pkg, plist, rootdir) != EPKG_OK) {
-
		ret = EPKG_FATAL;
-
		goto cleanup;
-
	}
+
	close(fd);

	if (pkg->www == NULL) {
		if (pkg->desc == NULL) {
			pkg_emit_error("No www or desc defined in manifest");
-
			ret = EPKG_FATAL;
-
			goto cleanup;
+
			return (EPKG_FATAL);
		}
		regcomp(&preg, "^WWW:[[:space:]]*(.*)$",
		    REG_EXTENDED|REG_ICASE|REG_NEWLINE);
@@ -394,12 +376,45 @@ pkg_load_metadata(struct pkg *pkg, const char *mfile, const char *md_dir,
		regfree(&preg);
	}

-
	fixup_abi(pkg, rootdir, testing);
+
	return (EPKG_OK);
+
}

-
cleanup:
-
	if (mfd != -1)
-
		close(mfd);
-
	pkg_manifest_keys_free(keys);
+
static void
+
fixup_abi(struct pkg *pkg, const char *rootdir, bool testing)
+
{
+
	bool defaultarch = false;
+
	const char *arch;
+

+
	/* if no arch autodetermine it */
+
	if (pkg->abi == NULL) {
+
#ifdef __FreeBSD__
+
		char *osversion;
+
		xasprintf(&osversion, "%d", ctx.osversion);
+
		pkg_kv_add(&pkg->annotations, "FreeBSD_version", osversion, "annotation");
+
#endif
+
		arch = pkg_object_string(pkg_config_get("ABI"));
+
		pkg->abi = xstrdup(arch);
+
		defaultarch = true;
+
	}
+

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

+
	if (ctx.developer_mode)
+
		suggest_arch(pkg, defaultarch);
+
}
+

+
int
+
pkg_load_metadata(struct pkg *pkg, const char *mfile, const char *md_dir,
+
    const char *plist, const char *rootdir, bool testing)
+
{
+
	int ret;
+

+
	ret = load_metadata(pkg, md_dir != NULL ? md_dir: mfile, plist, rootdir);
+
	if (ret != EPKG_OK)
+
		return (ret);
+

+
	fixup_abi(pkg, rootdir, testing);
	return (ret);
}