Blame hurd/path-lookup.c

Packit 6c4009
/* Filename lookup using a search path
Packit 6c4009
   Copyright (C) 1995-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Written by Miles Bader <miles@gnu.ai.mit.edu>
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <hurd.h>
Packit 6c4009
#include <hurd/lookup.h>
Packit 6c4009
Packit 6c4009
/* If FILE_NAME contains a '/', or PATH is NULL, call FUN with FILE_NAME, and
Packit 6c4009
   return the result (if PREFIXED_NAME is non-NULL, setting *PREFIXED_NAME to
Packit 6c4009
   NULL).  Otherwise, call FUN repeatedly with FILE_NAME prefixed with each
Packit 6c4009
   successive `:' separated element of PATH, returning whenever FUN returns
Packit 6c4009
   0 (if PREFIXED_NAME is non-NULL, setting *PREFIXED_NAME to the resulting
Packit 6c4009
   prefixed path).  If FUN never returns 0, return the first non-ENOENT
Packit 6c4009
   return value, or ENOENT if there is none.  */
Packit 6c4009
error_t
Packit 6c4009
file_name_path_scan (const char *file_name, const char *path,
Packit 6c4009
		     error_t (*fun)(const char *name),
Packit 6c4009
		     char **prefixed_name)
Packit 6c4009
{
Packit 6c4009
  if (path == NULL || strchr (file_name, '/'))
Packit 6c4009
    {
Packit 6c4009
      if (prefixed_name)
Packit 6c4009
	*prefixed_name = 0;
Packit 6c4009
      return (*fun)(file_name);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      error_t real_err = 0;
Packit 6c4009
      size_t file_name_len = strlen (file_name);
Packit 6c4009
Packit 6c4009
      for (;;)
Packit 6c4009
	{
Packit 6c4009
	  error_t err;
Packit 6c4009
	  const char *next = strchr (path, ':') ?: path + strlen (path);
Packit 6c4009
	  size_t pfx_len = next - path;
Packit 6c4009
	  char pfxed_name[pfx_len + 2 + file_name_len + 1];
Packit 6c4009
Packit 6c4009
	  if (pfx_len == 0)
Packit 6c4009
	    pfxed_name[pfx_len++] = '.';
Packit 6c4009
	  else
Packit 6c4009
	    memcpy (pfxed_name, path, pfx_len);
Packit 6c4009
	  if (pfxed_name[pfx_len - 1] != '/')
Packit 6c4009
	    pfxed_name[pfx_len++] = '/';
Packit 6c4009
	  memcpy (pfxed_name + pfx_len, file_name, file_name_len + 1);
Packit 6c4009
Packit 6c4009
	  err = (*fun)(pfxed_name);
Packit 6c4009
	  if (err == 0)
Packit 6c4009
	    {
Packit 6c4009
	      if (prefixed_name)
Packit 6c4009
		*prefixed_name = __strdup (pfxed_name);
Packit 6c4009
	      return 0;
Packit 6c4009
	    }
Packit 6c4009
	  if (!real_err && err != ENOENT)
Packit 6c4009
	    real_err = err;
Packit 6c4009
Packit 6c4009
	  if (*next == '\0')
Packit 6c4009
	    return real_err ?: ENOENT;
Packit 6c4009
	  else
Packit 6c4009
	    path = next + 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Lookup FILE_NAME and return the node opened with FLAGS & MODE in result
Packit 6c4009
   (see hurd_file_name_lookup for details), but a simple filename (without
Packit 6c4009
   any directory prefixes) will be consecutively prefixed with the pathnames
Packit 6c4009
   in the `:' separated list PATH until one succeeds in a successful lookup.
Packit 6c4009
   If none succeed, then the first error that wasn't ENOENT is returned, or
Packit 6c4009
   ENOENT if no other errors were returned.  If PREFIXED_NAME is non-NULL,
Packit 6c4009
   then if RESULT is looked up directly, *PREFIXED_NAME is set to NULL, and
Packit 6c4009
   if it is looked up using a prefix from PATH, *PREFIXED_NAME is set to
Packit 6c4009
   malloced storage containing the prefixed name.  */
Packit 6c4009
error_t
Packit 6c4009
__hurd_file_name_path_lookup (error_t (*use_init_port)
Packit 6c4009
			        (int which, error_t (*operate) (mach_port_t)),
Packit 6c4009
			      file_t (*get_dtable_port) (int fd),
Packit 6c4009
			      error_t (*lookup)
Packit 6c4009
			        (file_t dir, const char *name, int flags, mode_t mode,
Packit 6c4009
			         retry_type *do_retry, string_t retry_name,
Packit 6c4009
			         mach_port_t *result),
Packit 6c4009
			      const char *file_name, const char *path,
Packit 6c4009
			      int flags, mode_t mode,
Packit 6c4009
			      file_t *result, char **prefixed_name)
Packit 6c4009
{
Packit 6c4009
  error_t scan_lookup (const char *name)
Packit 6c4009
    {
Packit 6c4009
      return
Packit 6c4009
	__hurd_file_name_lookup (use_init_port, get_dtable_port, lookup,
Packit 6c4009
				 name, flags, mode, result);
Packit 6c4009
    }
Packit 6c4009
  return file_name_path_scan (file_name, path, scan_lookup, prefixed_name);
Packit 6c4009
}
Packit 6c4009
strong_alias (__hurd_file_name_path_lookup, hurd_file_name_path_lookup)
Packit 6c4009
Packit 6c4009
file_t
Packit 6c4009
file_name_path_lookup (const char *file_name, const char *path,
Packit 6c4009
		       int flags, mode_t mode, char **prefixed_name)
Packit 6c4009
{
Packit 6c4009
  error_t err;
Packit 6c4009
  file_t result;
Packit 6c4009
Packit 6c4009
  err = __hurd_file_name_path_lookup (&_hurd_ports_use, &__getdport, 0,
Packit 6c4009
				      file_name, path, flags, mode,
Packit 6c4009
				      &result, prefixed_name);
Packit 6c4009
Packit 6c4009
  return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;
Packit 6c4009
}