2016-12-01 04:15:13 +00:00
|
|
|
/*
|
|
|
|
* path.c
|
|
|
|
* filesystem path management
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 pkgconf authors (see AUTHORS).
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* This software is provided 'as is' and without any warranty, express or
|
|
|
|
* implied. In no event shall the authors be liable for any damages arising
|
|
|
|
* from the use of this software.
|
|
|
|
*/
|
|
|
|
|
2017-09-18 04:38:25 +00:00
|
|
|
#include <libpkgconf/stdinc.h>
|
2016-12-01 04:15:13 +00:00
|
|
|
#include <libpkgconf/libpkgconf.h>
|
2016-12-30 16:44:01 +00:00
|
|
|
#include <libpkgconf/config.h>
|
|
|
|
|
2017-01-24 05:17:26 +00:00
|
|
|
#if defined(HAVE_CYGWIN_CONV_PATH) && defined(__MSYS__)
|
2017-01-14 02:04:38 +00:00
|
|
|
# include <sys/cygwin.h>
|
|
|
|
#endif
|
|
|
|
|
2017-06-05 02:19:55 +00:00
|
|
|
#if defined(HAVE_SYS_STAT_H) && ! defined(_WIN32)
|
2016-12-30 17:01:15 +00:00
|
|
|
# include <sys/stat.h>
|
|
|
|
# define PKGCONF_CACHE_INODES
|
|
|
|
#endif
|
|
|
|
|
2016-12-30 16:44:01 +00:00
|
|
|
static bool
|
2016-12-30 17:01:15 +00:00
|
|
|
#ifdef PKGCONF_CACHE_INODES
|
|
|
|
path_list_contains_entry(const char *text, pkgconf_list_t *dirlist, struct stat *st)
|
|
|
|
#else
|
2016-12-30 16:44:01 +00:00
|
|
|
path_list_contains_entry(const char *text, pkgconf_list_t *dirlist)
|
2016-12-30 17:01:15 +00:00
|
|
|
#endif
|
2016-12-30 16:44:01 +00:00
|
|
|
{
|
|
|
|
pkgconf_node_t *n;
|
|
|
|
|
|
|
|
PKGCONF_FOREACH_LIST_ENTRY(dirlist->head, n)
|
|
|
|
{
|
|
|
|
pkgconf_path_t *pn = n->data;
|
|
|
|
|
2016-12-30 17:01:15 +00:00
|
|
|
#ifdef PKGCONF_CACHE_INODES
|
2017-01-07 16:57:37 +00:00
|
|
|
if (pn->handle_device == (void *)(intptr_t)st->st_dev && pn->handle_path == (void *)(intptr_t)st->st_ino)
|
2016-12-30 17:01:15 +00:00
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
|
2016-12-30 16:44:01 +00:00
|
|
|
if (!strcmp(text, pn->path))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-01 04:15:13 +00:00
|
|
|
|
2016-12-11 02:14:42 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* libpkgconf `path` module
|
|
|
|
* ========================
|
|
|
|
*
|
|
|
|
* The `path` module provides functions for manipulating lists of paths in a cross-platform manner. Notably,
|
|
|
|
* it is used by the `pkgconf client` to parse the ``PKG_CONFIG_PATH``, ``PKG_CONFIG_LIBDIR`` and related environment
|
|
|
|
* variables.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* .. c:function:: void pkgconf_path_add(const char *text, pkgconf_list_t *dirlist)
|
|
|
|
*
|
2016-12-30 16:44:01 +00:00
|
|
|
* Adds a path node to a path list. If the path is already in the list, do nothing.
|
2016-12-11 02:14:42 +00:00
|
|
|
*
|
|
|
|
* :param char* text: The path text to add as a path node.
|
|
|
|
* :param pkgconf_list_t* dirlist: The path list to add the path node to.
|
2016-12-30 17:13:04 +00:00
|
|
|
* :param bool filter: Whether to perform duplicate filtering.
|
2016-12-11 02:14:42 +00:00
|
|
|
* :return: nothing
|
|
|
|
*/
|
2016-12-01 04:15:13 +00:00
|
|
|
void
|
2016-12-30 17:13:04 +00:00
|
|
|
pkgconf_path_add(const char *text, pkgconf_list_t *dirlist, bool filter)
|
2016-12-01 04:15:13 +00:00
|
|
|
{
|
|
|
|
pkgconf_path_t *node;
|
2017-01-14 02:12:38 +00:00
|
|
|
char path[PKGCONF_BUFSIZE];
|
|
|
|
|
2016-12-30 17:01:15 +00:00
|
|
|
#ifdef PKGCONF_CACHE_INODES
|
|
|
|
struct stat st;
|
2016-12-01 04:15:13 +00:00
|
|
|
|
2017-01-26 22:27:48 +00:00
|
|
|
if (filter)
|
|
|
|
{
|
|
|
|
if (lstat(text, &st) == -1)
|
|
|
|
return;
|
|
|
|
if (S_ISLNK(st.st_mode))
|
|
|
|
{
|
|
|
|
char linkdest[PKGCONF_BUFSIZE];
|
2017-10-16 16:25:52 +00:00
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
memset(linkdest, '\0', sizeof linkdest);
|
|
|
|
len = readlink(text, linkdest, sizeof linkdest);
|
2016-12-30 17:01:15 +00:00
|
|
|
|
2017-01-26 22:27:48 +00:00
|
|
|
if (len != -1 && (size_t)len < sizeof(linkdest) &&
|
|
|
|
stat(linkdest, &st) == -1)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (path_list_contains_entry(text, dirlist, &st))
|
|
|
|
return;
|
|
|
|
}
|
2016-12-30 17:01:15 +00:00
|
|
|
#else
|
2016-12-30 17:13:04 +00:00
|
|
|
if (filter && path_list_contains_entry(text, dirlist))
|
2016-12-30 16:44:01 +00:00
|
|
|
return;
|
2016-12-30 17:01:15 +00:00
|
|
|
#endif
|
2016-12-30 16:44:01 +00:00
|
|
|
|
2017-01-14 02:12:38 +00:00
|
|
|
pkgconf_strlcpy(path, text, sizeof path);
|
|
|
|
pkgconf_path_relocate(path, sizeof path);
|
|
|
|
|
2016-12-01 04:15:13 +00:00
|
|
|
node = calloc(sizeof(pkgconf_path_t), 1);
|
2017-01-14 02:12:38 +00:00
|
|
|
node->path = strdup(path);
|
|
|
|
|
2016-12-30 17:01:15 +00:00
|
|
|
#ifdef PKGCONF_CACHE_INODES
|
2017-01-07 16:52:44 +00:00
|
|
|
if (filter) {
|
|
|
|
node->handle_path = (void *)(intptr_t) st.st_ino;
|
|
|
|
node->handle_device = (void *)(intptr_t) st.st_dev;
|
|
|
|
}
|
2016-12-30 17:01:15 +00:00
|
|
|
#endif
|
2016-12-01 04:15:13 +00:00
|
|
|
|
|
|
|
pkgconf_node_insert_tail(&node->lnode, node, dirlist);
|
|
|
|
}
|
|
|
|
|
2016-12-11 02:14:42 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* .. c:function:: size_t pkgconf_path_split(const char *text, pkgconf_list_t *dirlist)
|
|
|
|
*
|
|
|
|
* Splits a given text input and inserts paths into a path list.
|
|
|
|
*
|
|
|
|
* :param char* text: The path text to split and add as path nodes.
|
|
|
|
* :param pkgconf_list_t* dirlist: The path list to have the path nodes added to.
|
2016-12-30 17:13:04 +00:00
|
|
|
* :param bool filter: Whether to perform duplicate filtering.
|
2016-12-11 02:14:42 +00:00
|
|
|
* :return: number of path nodes added to the path list
|
|
|
|
* :rtype: size_t
|
|
|
|
*/
|
2016-12-01 04:15:13 +00:00
|
|
|
size_t
|
2016-12-30 17:13:04 +00:00
|
|
|
pkgconf_path_split(const char *text, pkgconf_list_t *dirlist, bool filter)
|
2016-12-01 04:15:13 +00:00
|
|
|
{
|
|
|
|
size_t count = 0;
|
|
|
|
char *workbuf, *p, *iter;
|
|
|
|
|
|
|
|
if (text == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
iter = workbuf = strdup(text);
|
|
|
|
while ((p = strtok(iter, PKG_CONFIG_PATH_SEP_S)) != NULL)
|
|
|
|
{
|
2016-12-30 17:13:04 +00:00
|
|
|
pkgconf_path_add(p, dirlist, filter);
|
2016-12-01 04:15:13 +00:00
|
|
|
|
|
|
|
count++, iter = NULL;
|
|
|
|
}
|
|
|
|
free(workbuf);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2016-12-11 02:14:42 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
2017-06-05 02:19:55 +00:00
|
|
|
* .. c:function:: size_t pkgconf_path_build_from_environ(const char *envvarname, const char *fallback, pkgconf_list_t *dirlist)
|
2016-12-11 02:14:42 +00:00
|
|
|
*
|
|
|
|
* Adds the paths specified in an environment variable to a path list. If the environment variable is not set,
|
|
|
|
* an optional default set of paths is added.
|
|
|
|
*
|
2017-06-05 02:19:55 +00:00
|
|
|
* :param char* envvarname: The environment variable to look up.
|
2016-12-11 02:14:42 +00:00
|
|
|
* :param char* fallback: The fallback paths to use if the environment variable is not set.
|
|
|
|
* :param pkgconf_list_t* dirlist: The path list to add the path nodes to.
|
2016-12-30 17:13:04 +00:00
|
|
|
* :param bool filter: Whether to perform duplicate filtering.
|
2016-12-11 02:14:42 +00:00
|
|
|
* :return: number of path nodes added to the path list
|
|
|
|
* :rtype: size_t
|
|
|
|
*/
|
2016-12-01 04:32:17 +00:00
|
|
|
size_t
|
2017-06-05 02:19:55 +00:00
|
|
|
pkgconf_path_build_from_environ(const char *envvarname, const char *fallback, pkgconf_list_t *dirlist, bool filter)
|
2016-12-01 04:32:17 +00:00
|
|
|
{
|
|
|
|
const char *data;
|
|
|
|
|
2017-06-05 02:19:55 +00:00
|
|
|
data = getenv(envvarname);
|
2016-12-01 04:32:17 +00:00
|
|
|
if (data != NULL)
|
2016-12-30 17:13:04 +00:00
|
|
|
return pkgconf_path_split(data, dirlist, filter);
|
2016-12-01 04:32:17 +00:00
|
|
|
|
|
|
|
if (fallback != NULL)
|
2016-12-30 17:13:04 +00:00
|
|
|
return pkgconf_path_split(fallback, dirlist, filter);
|
2016-12-01 04:32:17 +00:00
|
|
|
|
|
|
|
/* no fallback and no environment variable, thusly no nodes added */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-11 02:14:42 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
2016-12-22 01:50:05 +00:00
|
|
|
* .. c:function:: bool pkgconf_path_match_list(const char *path, const pkgconf_list_t *dirlist)
|
2016-12-11 02:14:42 +00:00
|
|
|
*
|
|
|
|
* Checks whether a path has a matching prefix in a path list.
|
|
|
|
*
|
|
|
|
* :param char* path: The path to check against a path list.
|
|
|
|
* :param pkgconf_list_t* dirlist: The path list to check the path against.
|
|
|
|
* :return: true if the path list has a matching prefix, otherwise false
|
|
|
|
* :rtype: bool
|
|
|
|
*/
|
2016-12-01 04:32:17 +00:00
|
|
|
bool
|
2016-12-22 01:50:05 +00:00
|
|
|
pkgconf_path_match_list(const char *path, const pkgconf_list_t *dirlist)
|
2016-12-01 04:32:17 +00:00
|
|
|
{
|
|
|
|
pkgconf_node_t *n = NULL;
|
2017-01-19 16:43:23 +00:00
|
|
|
char relocated[PKGCONF_BUFSIZE];
|
|
|
|
const char *cpath = path;
|
|
|
|
|
|
|
|
pkgconf_strlcpy(relocated, path, sizeof relocated);
|
|
|
|
if (pkgconf_path_relocate(relocated, sizeof relocated))
|
2017-09-08 23:27:04 +00:00
|
|
|
cpath = relocated;
|
2016-12-01 04:32:17 +00:00
|
|
|
|
|
|
|
PKGCONF_FOREACH_LIST_ENTRY(dirlist->head, n)
|
|
|
|
{
|
|
|
|
pkgconf_path_t *pnode = n->data;
|
|
|
|
|
2017-01-19 16:43:23 +00:00
|
|
|
if (!strcmp(pnode->path, cpath))
|
2016-12-01 04:32:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-02 06:04:43 +00:00
|
|
|
|
2016-12-11 02:14:42 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* .. c:function:: void pkgconf_path_free(pkgconf_list_t *dirlist)
|
|
|
|
*
|
|
|
|
* Releases any path nodes attached to the given path list.
|
|
|
|
*
|
|
|
|
* :param pkgconf_list_t* dirlist: The path list to clean up.
|
|
|
|
* :return: nothing
|
|
|
|
*/
|
2016-12-02 06:04:43 +00:00
|
|
|
void
|
|
|
|
pkgconf_path_free(pkgconf_list_t *dirlist)
|
|
|
|
{
|
|
|
|
pkgconf_node_t *n, *tn;
|
|
|
|
|
|
|
|
PKGCONF_FOREACH_LIST_ENTRY_SAFE(dirlist->head, tn, n)
|
|
|
|
{
|
|
|
|
pkgconf_path_t *pnode = n->data;
|
|
|
|
|
|
|
|
free(pnode->path);
|
|
|
|
free(pnode);
|
|
|
|
}
|
|
|
|
}
|
2017-01-14 02:04:38 +00:00
|
|
|
|
2017-01-25 06:30:58 +00:00
|
|
|
static char *
|
|
|
|
normpath(const char *path)
|
|
|
|
{
|
|
|
|
if (!path)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
char *copy = strdup(path);
|
|
|
|
if (NULL == copy)
|
|
|
|
return NULL;
|
|
|
|
char *ptr = copy;
|
|
|
|
|
|
|
|
for (int ii = 0; copy[ii]; ii++)
|
|
|
|
{
|
|
|
|
*ptr++ = path[ii];
|
|
|
|
if ('/' == path[ii])
|
|
|
|
{
|
|
|
|
ii++;
|
|
|
|
while ('/' == path[ii])
|
|
|
|
ii++;
|
|
|
|
ii--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*ptr = '\0';
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2017-01-19 16:31:40 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* .. c:function:: bool pkgconf_path_relocate(char *buf, size_t buflen)
|
|
|
|
*
|
2017-01-25 06:30:58 +00:00
|
|
|
* Relocates a path, possibly calling normpath() or cygwin_conv_path() on it.
|
2017-01-19 16:31:40 +00:00
|
|
|
*
|
|
|
|
* :param char* buf: The path to relocate.
|
|
|
|
* :param size_t buflen: The buffer length the path is contained in.
|
|
|
|
* :return: true on success, false on error
|
|
|
|
* :rtype: bool
|
|
|
|
*/
|
2017-01-14 02:04:38 +00:00
|
|
|
bool
|
|
|
|
pkgconf_path_relocate(char *buf, size_t buflen)
|
|
|
|
{
|
2017-01-24 05:17:26 +00:00
|
|
|
#if defined(HAVE_CYGWIN_CONV_PATH) && defined(__MSYS__)
|
2017-01-14 02:04:38 +00:00
|
|
|
ssize_t size;
|
|
|
|
char *tmpbuf, *ti;
|
|
|
|
|
|
|
|
size = cygwin_conv_path(CCP_POSIX_TO_WIN_A, buf, NULL, 0);
|
|
|
|
if (size < 0 || (size_t) size > buflen)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tmpbuf = malloc(size);
|
|
|
|
if (cygwin_conv_path(CCP_POSIX_TO_WIN_A, buf, tmpbuf, size))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
pkgconf_strlcpy(buf, tmpbuf, buflen);
|
|
|
|
free(tmpbuf);
|
|
|
|
|
|
|
|
/* rewrite any backslash arguments for best compatibility */
|
|
|
|
for (ti = buf; *ti != '\0'; ti++)
|
|
|
|
{
|
|
|
|
if (*ti == '\\')
|
|
|
|
*ti = '/';
|
|
|
|
}
|
2017-01-25 06:30:58 +00:00
|
|
|
#else
|
2017-01-19 16:36:07 +00:00
|
|
|
char *tmpbuf;
|
|
|
|
|
2017-01-25 06:30:58 +00:00
|
|
|
if ((tmpbuf = normpath(buf)) != NULL)
|
2017-01-19 16:36:07 +00:00
|
|
|
{
|
|
|
|
size_t tmpbuflen = strlen(tmpbuf);
|
|
|
|
if (tmpbuflen > buflen)
|
|
|
|
{
|
|
|
|
free(tmpbuf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pkgconf_strlcpy(buf, tmpbuf, buflen);
|
|
|
|
free(tmpbuf);
|
|
|
|
}
|
2017-01-14 02:04:38 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|