Blame posix/bug-glob2.c

Packit 6c4009
/* Test glob memory management.
Packit 6c4009
   for the filesystem access functions.
Packit 6c4009
   Copyright (C) 2001-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
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 <errno.h>
Packit 6c4009
#include <error.h>
Packit 6c4009
#include <dirent.h>
Packit 6c4009
#include <glob.h>
Packit 6c4009
#include <mcheck.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
Packit 6c4009
// #define DEBUG
Packit 6c4009
#ifdef DEBUG
Packit 6c4009
# define PRINTF(fmt, args...) \
Packit 6c4009
  do					\
Packit 6c4009
    {					\
Packit 6c4009
      int save_errno = errno;		\
Packit 6c4009
      printf (fmt, ##args);		\
Packit 6c4009
      errno = save_errno;		\
Packit 6c4009
    } while (0)
Packit 6c4009
#else
Packit 6c4009
# define PRINTF(fmt, args...)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#define LONG_NAME \
Packit 6c4009
  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
Packit 6c4009
  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
Packit 6c4009
  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
Packit 6c4009
  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
Packit 6c4009
  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
Packit 6c4009
  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
Packit 6c4009
  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
Packit 6c4009
  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
Packit 6c4009
  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
Packit 6c4009
  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Packit 6c4009
Packit 6c4009
static struct
Packit 6c4009
{
Packit 6c4009
  const char *name;
Packit 6c4009
  int level;
Packit 6c4009
  int type;
Packit 6c4009
  mode_t mode;
Packit 6c4009
} filesystem[] =
Packit 6c4009
{
Packit 6c4009
  { ".", 1, DT_DIR, 0755 },
Packit 6c4009
  { "..", 1, DT_DIR, 0755 },
Packit 6c4009
  { "dir", 1, DT_DIR, 0755 },
Packit 6c4009
    { ".", 2, DT_DIR, 0755 },
Packit 6c4009
    { "..", 2, DT_DIR, 0755 },
Packit 6c4009
    { "readable", 2, DT_DIR, 0755 },
Packit 6c4009
      { ".", 3, DT_DIR, 0755 },
Packit 6c4009
      { "..", 3, DT_DIR, 0755 },
Packit 6c4009
      { "a", 3, DT_REG, 0644 },
Packit 6c4009
      { LONG_NAME, 3, DT_REG, 0644 },
Packit 6c4009
    { "unreadable", 2, DT_DIR, 0111 },
Packit 6c4009
      { ".", 3, DT_DIR, 0111 },
Packit 6c4009
      { "..", 3, DT_DIR, 0755 },
Packit 6c4009
      { "a", 3, DT_REG, 0644 },
Packit 6c4009
    { "zz-readable", 2, DT_DIR, 0755 },
Packit 6c4009
      { ".", 3, DT_DIR, 0755 },
Packit 6c4009
      { "..", 3, DT_DIR, 0755 },
Packit 6c4009
      { "a", 3, DT_REG, 0644 }
Packit 6c4009
};
Packit 6c4009
#define nfiles (sizeof (filesystem) / sizeof (filesystem[0]))
Packit 6c4009
Packit 6c4009
Packit 6c4009
typedef struct
Packit 6c4009
{
Packit 6c4009
  int level;
Packit 6c4009
  int idx;
Packit 6c4009
  struct dirent d;
Packit 6c4009
  char room_for_dirent[sizeof (LONG_NAME)];
Packit 6c4009
} my_DIR;
Packit 6c4009
Packit 6c4009
Packit 6c4009
static long int
Packit 6c4009
find_file (const char *s)
Packit 6c4009
{
Packit 6c4009
  int level = 1;
Packit 6c4009
  long int idx = 0;
Packit 6c4009
Packit 6c4009
  if (strcmp (s, ".") == 0)
Packit 6c4009
    return 0;
Packit 6c4009
Packit 6c4009
  if (s[0] == '.' && s[1] == '/')
Packit 6c4009
    s += 2;
Packit 6c4009
Packit 6c4009
  while (*s != '\0')
Packit 6c4009
    {
Packit 6c4009
      char *endp = strchrnul (s, '/');
Packit 6c4009
Packit 6c4009
      PRINTF ("looking for %.*s, level %d\n", (int) (endp - s), s, level);
Packit 6c4009
Packit 6c4009
      while (idx < nfiles && filesystem[idx].level >= level)
Packit 6c4009
	{
Packit 6c4009
	  if (filesystem[idx].level == level
Packit 6c4009
	      && memcmp (s, filesystem[idx].name, endp - s) == 0
Packit 6c4009
	      && filesystem[idx].name[endp - s] == '\0')
Packit 6c4009
	    break;
Packit 6c4009
	  ++idx;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (idx == nfiles || filesystem[idx].level < level)
Packit 6c4009
	{
Packit 6c4009
	  errno = ENOENT;
Packit 6c4009
	  return -1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (*endp == '\0')
Packit 6c4009
	return idx + 1;
Packit 6c4009
Packit 6c4009
      if (filesystem[idx].type != DT_DIR
Packit 6c4009
	  && (idx + 1 >= nfiles
Packit 6c4009
	      || filesystem[idx].level >= filesystem[idx + 1].level))
Packit 6c4009
	{
Packit 6c4009
	  errno = ENOTDIR;
Packit 6c4009
	  return -1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      ++idx;
Packit 6c4009
Packit 6c4009
      s = endp + 1;
Packit 6c4009
      ++level;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  errno = ENOENT;
Packit 6c4009
  return -1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
my_opendir (const char *s)
Packit 6c4009
{
Packit 6c4009
  long int idx = find_file (s);
Packit 6c4009
  my_DIR *dir;
Packit 6c4009
Packit 6c4009
  if (idx == -1)
Packit 6c4009
    {
Packit 6c4009
      PRINTF ("my_opendir(\"%s\") == NULL (%m)\n", s);
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if ((filesystem[idx].mode & 0400) == 0)
Packit 6c4009
    {
Packit 6c4009
      errno = EACCES;
Packit 6c4009
      PRINTF ("my_opendir(\"%s\") == NULL (%m)\n", s);
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  dir = (my_DIR *) malloc (sizeof (my_DIR));
Packit 6c4009
  if (dir == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot allocate directory handle: %m\n");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  dir->level = filesystem[idx].level;
Packit 6c4009
  dir->idx = idx;
Packit 6c4009
Packit 6c4009
  PRINTF ("my_opendir(\"%s\") == { level: %d, idx: %ld }\n",
Packit 6c4009
	  s, filesystem[idx].level, idx);
Packit 6c4009
Packit 6c4009
  return dir;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static struct dirent *
Packit 6c4009
my_readdir (void *gdir)
Packit 6c4009
{
Packit 6c4009
  my_DIR *dir = gdir;
Packit 6c4009
Packit 6c4009
  if (dir->idx == -1)
Packit 6c4009
    {
Packit 6c4009
      PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
Packit 6c4009
	      dir->level, (long int) dir->idx);
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  while (dir->idx < nfiles && filesystem[dir->idx].level > dir->level)
Packit 6c4009
    ++dir->idx;
Packit 6c4009
Packit 6c4009
  if (dir->idx == nfiles || filesystem[dir->idx].level < dir->level)
Packit 6c4009
    {
Packit 6c4009
      dir->idx = -1;
Packit 6c4009
      PRINTF ("my_readdir ({ level: %d, idx: %ld }) = NULL\n",
Packit 6c4009
	      dir->level, (long int) dir->idx);
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  dir->d.d_ino = 1;		/* glob should not skip this entry.  */
Packit 6c4009
Packit 6c4009
  dir->d.d_type = filesystem[dir->idx].type;
Packit 6c4009
Packit 6c4009
  strcpy (dir->d.d_name, filesystem[dir->idx].name);
Packit 6c4009
Packit 6c4009
  PRINTF ("my_readdir ({ level: %d, idx: %ld }) = { d_ino: %ld, d_type: %d, d_name: \"%s\" }\n",
Packit 6c4009
	  dir->level, (long int) dir->idx, dir->d.d_ino, dir->d.d_type,
Packit 6c4009
	  dir->d.d_name);
Packit 6c4009
Packit 6c4009
  ++dir->idx;
Packit 6c4009
Packit 6c4009
  return &dir->d;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
my_closedir (void *dir)
Packit 6c4009
{
Packit 6c4009
  PRINTF ("my_closedir ()\n");
Packit 6c4009
  free (dir);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* We use this function for lstat as well since we don't have any.  */
Packit 6c4009
static int
Packit 6c4009
my_stat (const char *name, struct stat *st)
Packit 6c4009
{
Packit 6c4009
  long int idx = find_file (name);
Packit 6c4009
Packit 6c4009
  if (idx == -1)
Packit 6c4009
    {
Packit 6c4009
      PRINTF ("my_stat (\"%s\", ...) = -1 (%m)\n", name);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  memset (st, '\0', sizeof (*st));
Packit 6c4009
Packit 6c4009
  if (filesystem[idx].type == DT_UNKNOWN)
Packit 6c4009
    st->st_mode = DTTOIF (idx + 1 < nfiles
Packit 6c4009
			  && filesystem[idx].level < filesystem[idx + 1].level
Packit 6c4009
			  ? DT_DIR : DT_REG) | filesystem[idx].mode;
Packit 6c4009
  else
Packit 6c4009
    st->st_mode = DTTOIF (filesystem[idx].type) | filesystem[idx].mode;
Packit 6c4009
Packit 6c4009
  PRINTF ("my_stat (\"%s\", { st_mode: %o }) = 0\n", name, st->st_mode);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
init_glob_altdirfuncs (glob_t *pglob)
Packit 6c4009
{
Packit 6c4009
  pglob->gl_closedir = my_closedir;
Packit 6c4009
  pglob->gl_readdir = my_readdir;
Packit 6c4009
  pglob->gl_opendir = my_opendir;
Packit 6c4009
  pglob->gl_lstat = my_stat;
Packit 6c4009
  pglob->gl_stat = my_stat;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  mtrace ();
Packit 6c4009
Packit 6c4009
  glob_t gl;
Packit 6c4009
  memset (&gl, 0, sizeof (gl));
Packit 6c4009
  init_glob_altdirfuncs (&gl);
Packit 6c4009
Packit 6c4009
  if (glob ("dir/*able/*", GLOB_ERR | GLOB_ALTDIRFUNC, NULL, &gl)
Packit 6c4009
      != GLOB_ABORTED)
Packit 6c4009
    {
Packit 6c4009
      puts ("glob did not fail with GLOB_ABORTED");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  globfree (&gl);
Packit 6c4009
Packit 6c4009
  memset (&gl, 0, sizeof (gl));
Packit 6c4009
  init_glob_altdirfuncs (&gl);
Packit 6c4009
Packit 6c4009
  gl.gl_offs = 3;
Packit 6c4009
  if (glob ("dir2/*", GLOB_DOOFFS, NULL, &gl) != GLOB_NOMATCH)
Packit 6c4009
    {
Packit 6c4009
      puts ("glob did not fail with GLOB_NOMATCH");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  globfree (&gl);
Packit 6c4009
Packit 6c4009
  muntrace ();
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"