Skip to content

Commit

Permalink
version: reduce memory manipulation by using file descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
bapt committed Oct 25, 2023
1 parent ea90167 commit 2064bee
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions src/version.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*-
* Copyright (c) 2011-2015 Baptiste Daroussin <[email protected]>
* Copyright (c) 2011-2023 Baptiste Daroussin <[email protected]>
* Copyright (c) 2011-2012 Julien Laffaye <[email protected]>
* Copyright (c) 2011 Philippe Pepiot <[email protected]>
* Copyright (c) 2011-2012 Marin Atanasov Nikolov <[email protected]>
Expand Down Expand Up @@ -597,18 +597,19 @@ exec_buf(xstring *res, char **argv) {
}

static struct category *
category_new(char *categorypath, const char *category)
category_new(int portsfd, const char *category)
{
struct category *cat = NULL;
xstring *makecmd;
char *results, *d;
char *argv[5];

makecmd = xstring_new();
fchdir(portsfd);

argv[0] = "make";
argv[1] = "-C";
argv[2] = categorypath;
argv[2] = (char *)category;
argv[3] = "-VSUBDIR";
argv[4] = NULL;

Expand All @@ -635,28 +636,24 @@ category_new(char *categorypath, const char *category)
}

static bool
validate_origin(const char *portsdir, const char *origin)
validate_origin(int portsfd, const char *origin)
{
struct category *cat;
char *category, *buf;
char categorypath[MAXPATHLEN];

/* If the origin does not contain a / ignore it like for
* "base"
*/
if (strchr(origin, '/') == NULL)
return (false);

snprintf(categorypath, MAXPATHLEN, "%s/%s", portsdir, origin);

buf = strrchr(categorypath, '/');
category = xstrdup(origin);
buf = strrchr(category, '/');
buf[0] = '\0';
category = strrchr(categorypath, '/');
category++;

cat = pkghash_get_value(categories, category);
if (cat == NULL)
cat = category_new(categorypath, category);
cat = category_new(portsfd, category);
if (cat == NULL)
return (false);

Expand All @@ -670,8 +667,7 @@ validate_origin(const char *portsdir, const char *origin)
}

static const char *
port_version(xstring *cmd, const char *portsdir, const char *origin,
const char *pkgname)
port_version(xstring *cmd, int portsfd, const char *origin, const char *pkgname)
{
char *output, *walk, *name;
char *version = NULL;
Expand All @@ -681,13 +677,10 @@ port_version(xstring *cmd, const char *portsdir, const char *origin,
in the ports and category Makefiles, then extract the
version from the port itself. */

if (validate_origin(portsdir, origin)) {
fprintf(cmd->fp, "%s/%s", portsdir, origin);

fflush(cmd->fp);
if (validate_origin(portsfd, origin)) {
argv[0] = "make";
argv[1] = "-C";
argv[2] = cmd->buf;
argv[2] = (char *)origin;
argv[3] = "flavors-package-names";
argv[4] = NULL;

Expand Down Expand Up @@ -723,14 +716,16 @@ do_source_ports(unsigned int opt, char limchar, char *pattern, match_t match,
const char *name = NULL;
const char *origin = NULL;
const char *version = NULL;
int portsfd;

if ( (opt & VERSION_SOURCES) != VERSION_SOURCE_PORTS ) {
usage_version();
return (EXIT_FAILURE);
}

if (chdir(portsdir) != 0)
err(EXIT_FAILURE, "Cannot chdir to %s\n", portsdir);
portsfd = open(portsdir, O_DIRECTORY);
if (portsfd == -1)
err(EXIT_FAILURE, "Cannot open '%s'", portsdir);

if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK)
return (EXIT_FAILURE);
Expand Down Expand Up @@ -761,7 +756,7 @@ do_source_ports(unsigned int opt, char limchar, char *pattern, match_t match,
strcmp(name, matchname) != 0)
continue;

version = port_version(cmd, portsdir, origin, name);
version = port_version(cmd, portsfd, origin, name);
print_version(pkg, "port", version, limchar, opt);
xstring_reset(cmd);
}
Expand Down

0 comments on commit 2064bee

Please sign in to comment.