Blame io/tst-fts.c

Packit 6c4009
/* Simple test for some fts functions.
Packit 6c4009
   Copyright (C) 2015-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 <sys/types.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <fts.h>
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <error.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
static void prepare (void);
Packit 6c4009
static int do_test (void);
Packit 6c4009
#define PREPARE(argc, argv)     prepare ()
Packit 6c4009
#define TEST_FUNCTION           do_test ()
Packit 6c4009
#include "../test-skeleton.c"
Packit 6c4009
Packit 6c4009
static char *fts_test_dir;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
make_dir (const char *dirname)
Packit 6c4009
{
Packit 6c4009
  char *name;
Packit 6c4009
  if (asprintf (&name, "%s/%s", fts_test_dir, dirname) < 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("out of memory");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (mkdir (name, 0700) < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot create dir \"%s\": %m\n", name);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  add_temp_file (name);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
make_file (const char *filename)
Packit 6c4009
{
Packit 6c4009
  char *name;
Packit 6c4009
  if (asprintf (&name, "%s/%s", fts_test_dir, filename) < 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("out of memory");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int fd = open (name, O_WRONLY | O_CREAT | O_EXCL, 0600);
Packit 6c4009
  if (fd < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot create file \"%s\": %m\n", name);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  close (fd);
Packit 6c4009
Packit 6c4009
  add_temp_file (name);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
prepare (void)
Packit 6c4009
{
Packit 6c4009
  char *dirbuf;
Packit 6c4009
  char dir_name[] = "/tst-fts.XXXXXX";
Packit 6c4009
Packit 6c4009
  if (asprintf (&dirbuf, "%s%s", test_dir, dir_name) < 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("out of memory");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (mkdtemp (dirbuf) == NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("cannot create temporary directory");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  add_temp_file (dirbuf);
Packit 6c4009
  fts_test_dir = dirbuf;
Packit 6c4009
Packit 6c4009
  make_file ("12");
Packit 6c4009
  make_file ("345");
Packit 6c4009
  make_file ("6789");
Packit 6c4009
Packit 6c4009
  make_dir ("aaa");
Packit 6c4009
  make_file ("aaa/1234");
Packit 6c4009
  make_file ("aaa/5678");
Packit 6c4009
Packit 6c4009
  make_dir ("bbb");
Packit 6c4009
  make_file ("bbb/1234");
Packit 6c4009
  make_file ("bbb/5678");
Packit 6c4009
  make_file ("bbb/90ab");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Largest name wins, otherwise strcmp.  */
Packit 6c4009
static int
Packit 6c4009
compare_ents (const FTSENT **ent1, const FTSENT **ent2)
Packit 6c4009
{
Packit 6c4009
  short len1 = (*ent1)->fts_namelen;
Packit 6c4009
  short len2 = (*ent2)->fts_namelen;
Packit 6c4009
  if (len1 != len2)
Packit 6c4009
    return len1 - len2;
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      const char *name1 = (*ent1)->fts_name;
Packit 6c4009
      const char *name2 = (*ent2)->fts_name;
Packit 6c4009
      return strcmp (name1, name2);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Count the number of files seen as children.  */
Packit 6c4009
static int files = 0;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
children (FTS *fts)
Packit 6c4009
{
Packit 6c4009
  FTSENT *child = fts_children (fts, 0);
Packit 6c4009
  if (child == NULL && errno != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: fts_children: %m\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  while (child != NULL)
Packit 6c4009
    {
Packit 6c4009
      short level = child->fts_level;
Packit 6c4009
      const char *name = child->fts_name;
Packit 6c4009
      if (child->fts_info == FTS_F || child->fts_info == FTS_NSOK)
Packit 6c4009
	{
Packit 6c4009
	  files++;
Packit 6c4009
	  printf ("%*s%s\n", 2 * level, "", name);
Packit 6c4009
	}
Packit 6c4009
      child = child->fts_link;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Count the number of dirs seen in the test.  */
Packit 6c4009
static int dirs = 0;
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  char *paths[2] = { fts_test_dir, NULL };
Packit 6c4009
  FTS *fts;
Packit 6c4009
  fts = fts_open (paths, FTS_LOGICAL, &compare_ents);
Packit 6c4009
  if (fts == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: fts_open: %m\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  FTSENT *ent;
Packit 6c4009
  while ((ent = fts_read (fts)) != NULL)
Packit 6c4009
    {
Packit 6c4009
      const char *name = ent->fts_name;
Packit 6c4009
      short level = ent->fts_level;
Packit 6c4009
      switch (ent->fts_info)
Packit 6c4009
	{
Packit 6c4009
	case FTS_F:
Packit 6c4009
	  /* Don't show anything, children will have on parent dir.  */
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case FTS_D:
Packit 6c4009
	  printf ("%*s%s =>\n", 2 * level, "", name);
Packit 6c4009
	  children (fts);
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case FTS_DP:
Packit 6c4009
	  dirs++;
Packit 6c4009
	  printf ("%*s<= %s\n", 2 * level, "", name);
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case FTS_NS:
Packit 6c4009
	case FTS_ERR:
Packit 6c4009
	  printf ("FAIL: fts_read ent: %s\n", strerror (ent->fts_errno));
Packit 6c4009
	  exit (1);
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	default:
Packit 6c4009
	  printf ("FAIL: unexpected fts_read ent %s\n", name);
Packit 6c4009
	  exit (1);
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  /* fts_read returns NULL when done (and clears errno)
Packit 6c4009
     or when an error occured (with errno set).  */
Packit 6c4009
  if (errno != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: fts_read: %m\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (fts_close (fts) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: fts_close: %m\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (files != 8)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: Unexpected number of files: %d\n", files);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (dirs != 3)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: Unexpected number of dirs: %d\n", dirs);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  puts ("PASS");
Packit 6c4009
  return 0;
Packit 6c4009
}