Exemplo n.º 1
0
static void
prepare (void)
{
  size_t test_dir_len = strlen (test_dir);
  static const char dir_name[] = "/tst-pathconf.XXXXXX";

  size_t dirbuflen = test_dir_len + sizeof (dir_name);
  dirbuf = xmalloc (dirbuflen);

  snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
  if (mkdtemp (dirbuf) == NULL)
    {
      printf ("Cannot create temporary directory: %s\n", strerror (errno));
      exit (1);
    }

  add_temp_file (dirbuf);

  dir_fd = open (dirbuf, O_RDONLY);
  if (dir_fd == -1)
    {
      printf ("Cannot open directory: %s\n", strerror (errno));
      exit (1);
    }
}
static void
prepare (int argc, char *argv[])
{
  char *buf;
  int off;
  asprintf (&buf, "cp %s %n%s-copy", argv[0], &off, argv[0]);
  if (buf == NULL)
    {
      puts ("asprintf  failed");
      exit (1);
    }
  if (system (buf) != 0)
    {
      puts ("system  failed");
      exit (1);
    }

  /* Make it not executable.  */
  copy = buf + off;
  if (chmod (copy, 0666) != 0)
    {
      puts ("chmod  failed");
      exit (1);
    }

  add_temp_file (copy);
}
Exemplo n.º 3
0
static void
prepare (void)
{
  size_t test_dir_len = strlen (test_dir);
  static const char dir_name[] = "/tst-mknodat.XXXXXX";

  size_t dirbuflen = test_dir_len + sizeof (dir_name);
  char *dirbuf = malloc (dirbuflen);
  if (dirbuf == NULL)
    {
      puts ("out of memory");
      exit (1);
    }

  snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
  if (mkdtemp (dirbuf) == NULL)
    {
      puts ("cannot create temporary directory");
      exit (1);
    }

  add_temp_file (dirbuf);

  dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY);
  if (dir_fd == -1)
    {
      puts ("cannot open directory");
      exit (1);
    }
}
Exemplo n.º 4
0
/* We have a preparation function.  */
void
do_prepare (int argc, char *argv[])
{
   size_t name_len;

   name_len = strlen (test_dir);
   name1 = xmalloc (name_len + sizeof ("/execXXXXXX"));
   mempcpy (mempcpy (name1, test_dir, name_len),
	    "/execXXXXXX", sizeof ("/execXXXXXX"));
   add_temp_file (name1);

   name2 = xmalloc (name_len + sizeof ("/execXXXXXX"));
   mempcpy (mempcpy (name2, test_dir, name_len),
	    "/execXXXXXX", sizeof ("/execXXXXXX"));
   add_temp_file (name2);
}
Exemplo n.º 5
0
/* Preparation.  */
void
do_prepare (int argc, char *argv[])
{
  char test_dir_len;

  test_dir_len = strlen (test_dir);

  /* Generate the circular symlinks.  */
  name1 = malloc (test_dir_len + sizeof ("/canonXXXXXX"));
  mempcpy (mempcpy (name1, test_dir, test_dir_len),
	   "/canonXXXXXX", sizeof ("/canonXXXXXX"));
  name2 = strdup (name1);

  add_temp_file (mktemp (name1));
  add_temp_file (mktemp (name2));
}
void
do_prepare (int argc, char *argv[])
{
   size_t name_len;

   name_len = strlen (test_dir);
   name = malloc (name_len + sizeof ("/fcntlXXXXXX"));
   mempcpy (mempcpy (name, test_dir, name_len),
	    "/fcntlXXXXXX", sizeof ("/fcntlXXXXXX"));
   add_temp_file (name);
}
Exemplo n.º 7
0
static void
do_prepare (int argc, char *argv[])
{
  size_t name_len;

  name_len = strlen (test_dir);
  name = malloc (name_len + sizeof ("/utmpXXXXXX"));
  mempcpy (mempcpy (name, test_dir, name_len),
	   "/utmpXXXXXX", sizeof ("/utmpXXXXXX"));
  add_temp_file (name);

  /* Open our test file.  */
  fd = mkstemp (name);
  if (fd == -1)
    error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
}
Exemplo n.º 8
0
static void
prepare (void)
{
  fd = mkstemp (fname);
  if (fd == -1)
    {
      printf ("mkstemp failed: %m\n");
      exit (1);
    }
  add_temp_file (fname);
  if (ftruncate (fd, 1000) < 0)
    {
      printf ("ftruncate failed: %m\n");
      exit (1);
    }
}
Exemplo n.º 9
0
void
do_prepare (int argc, char *argv[])
{
   size_t name_len;

   name_len = strlen (test_dir);
   name = malloc (name_len + sizeof ("/fcntlXXXXXX"));
   mempcpy (mempcpy (name, test_dir, name_len),
	    "/fcntlXXXXXX", sizeof ("/fcntlXXXXXX"));
  /* Create the temporary file.  */
  fd = mkstemp (name);
  if (fd == -1)
    {
      printf ("cannot open temporary file: %m\n");
      exit (1);
    }
   add_temp_file (name);
}
Exemplo n.º 10
0
static void
make_dir (const char *dirname)
{
  char *name;
  if (asprintf (&name, "%s/%s", fts_test_dir, dirname) < 0)
    {
      puts ("out of memory");
      exit (1);
    }

  if (mkdir (name, 0700) < 0)
    {
      printf ("cannot create dir \"%s\": %m\n", name);
      exit (1);
    }

  add_temp_file (name);
}
Exemplo n.º 11
0
void
do_prepare (int argc, char *argv[])
{
   size_t name_len;

#define FNAME FNAME2(TRUNCATE)
#define FNAME2(s) "/" STRINGIFY(s) "XXXXXX"

   name_len = strlen (test_dir);
   name = malloc (name_len + sizeof (FNAME));
   mempcpy (mempcpy (name, test_dir, name_len), FNAME, sizeof (FNAME));
   add_temp_file (name);

   /* Open our test file.   */
   fd = mkstemp (name);
   if (fd == -1)
     error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
}
Exemplo n.º 12
0
static void
prepare (void)
{
#if _POSIX_CHOWN_RESTRICTED == 0
  if (pathconf (test_dir, _PC_CHOWN_RESTRICTED) != 0)
#endif
    {
      uid_t uid = getuid ();
      if (uid != 0)
	{
	  puts ("need root privileges");
	  exit (0);
	}
    }

  size_t test_dir_len = strlen (test_dir);
  static const char dir_name[] = "/tst-fchownat.XXXXXX";

  size_t dirbuflen = test_dir_len + sizeof (dir_name);
  char *dirbuf = malloc (dirbuflen);
  if (dirbuf == NULL)
    {
      puts ("out of memory");
      exit (1);
    }

  snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
  if (mkdtemp (dirbuf) == NULL)
    {
      puts ("cannot create temporary directory");
      exit (1);
    }

  add_temp_file (dirbuf);

  dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY);
  if (dir_fd == -1)
    {
      puts ("cannot open directory");
      exit (1);
    }
}
Exemplo n.º 13
0
static int
do_test (void)
{
  char *dir = support_create_temp_directory ("tst-xreadlink-");
  char *symlink_name = xasprintf ("%s/symlink", dir);
  add_temp_file (symlink_name);

  /* The limit 10000 is arbitrary and simply there to prevent an
     attempt to exhaust all available disk space.  */
  for (int size = 1; size < 10000; ++size)
    {
      char *contents = xmalloc (size + 1);
      for (int i = 0; i < size; ++i)
        contents[i] = 'a' + (rand () % 26);
      contents[size] = '\0';
      if (symlink (contents, symlink_name) != 0)
        {
          if (errno == ENAMETOOLONG)
            {
              printf ("info: ENAMETOOLONG failure at %d bytes\n", size);
              free (contents);
              break;
            }
          FAIL_EXIT1 ("symlink (%d bytes): %m", size);
        }

      char *readlink_result = xreadlink (symlink_name);
      TEST_VERIFY (strcmp (readlink_result, contents) == 0);
      free (readlink_result);
      xunlink (symlink_name);
      free (contents);
    }

  /* Create an empty file to suppress the temporary file deletion
     warning.  */
  xclose (xopen (symlink_name, O_WRONLY | O_CREAT, 0));

  free (symlink_name);
  free (dir);

  return 0;
}
Exemplo n.º 14
0
static void
make_file (const char *filename)
{
  char *name;
  if (asprintf (&name, "%s/%s", fts_test_dir, filename) < 0)
    {
      puts ("out of memory");
      exit (1);
    }

  int fd = open (name, O_WRONLY | O_CREAT | O_EXCL, 0600);
  if (fd < 0)
    {
      printf ("cannot create file \"%s\": %m\n", name);
      exit (1);
    }
  close (fd);

  add_temp_file (name);
}
Exemplo n.º 15
0
void
do_prepare (int argc, char *argv[])
{
  size_t name_len;
  struct rlimit64 rlim;

  name_len = strlen (test_dir);
  name = malloc (name_len + sizeof ("/lfsXXXXXX"));
  mempcpy (mempcpy (name, test_dir, name_len),
           "/lfsXXXXXX", sizeof ("/lfsXXXXXX"));
  add_temp_file (name);

  /* Open our test file.   */
  fd = mkstemp64 (name);
  if (fd == -1)
    {
      if (errno == ENOSYS)
	{
	  /* Fail silently.  */
	  error (0, 0, "open64 is not supported");
	  exit (EXIT_SUCCESS);
	}
      else
	error (EXIT_FAILURE, errno, "cannot create temporary file");
    }

  if (getrlimit64 (RLIMIT_FSIZE, &rlim) != 0)
    {
      error (0, errno, "cannot get resource limit");
      exit (0);
    }
  if (rlim.rlim_cur < TWO_GB + 200)
    {
      rlim.rlim_cur = TWO_GB + 200;
      if (setrlimit64 (RLIMIT_FSIZE, &rlim) != 0)
	{
	  error (0, errno, "cannot reset file size limits");
	  exit (0);
	}
    }
}
Exemplo n.º 16
0
void
do_prepare (int argc, char *argv[])
{
  size_t name_len;

  name_len = strlen (test_dir);
  name = malloc (name_len + sizeof ("/aioXXXXXX"));
  mempcpy (mempcpy (name, test_dir, name_len),
	   "/aioXXXXXX", sizeof ("/aioXXXXXX"));
  add_temp_file (name);

  /* Open our test file.   */
  fd = mkstemp (name);
  if (fd == -1)
    error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);

  int sz = set_o_direct (fd);
  if (sz != -1)
    {
      blksz = sz;
      printf ("Using O_DIRECT with block size %d\n", blksz);
    }
}
Exemplo n.º 17
0
static int
do_test (void)
{
  char *buf;
  int fd;
  FILE *fp;
  int ch;
  struct stat st1;
  struct stat st2;

  buf = (char *) malloc (strlen (test_dir) + sizeof "/tst-atime.XXXXXX");
  if (buf == NULL)
    {
      printf ("cannot allocate memory: %m\n");
      return 1;
    }
  stpcpy (stpcpy (buf, test_dir), "/tst-atime.XXXXXX");

  fd = mkstemp (buf);
  if (fd == -1)
    {
      printf ("cannot open temporary file: %m\n");
      return 1;
    }

#ifdef ST_NOATIME
  /* Make sure the filesystem doesn't have the noatime option set.  If
     statvfs is not available just continue.  */
  struct statvfs sv;
  int e = fstatvfs (fd, &sv);
  if (e != ENOSYS)
    {
      if (e != 0)
	{
	  printf ("cannot statvfs '%s': %m\n", buf);
	  return 1;
	}

      if ((sv.f_flag & ST_NOATIME) != 0)
	{
	  puts ("Bah!  The filesystem is mounted with noatime");
	  return 0;
	}
    }
#endif

  /* Make sure it gets removed.  */
  add_temp_file (buf);

  if (write (fd, "some string\n", 12) != 12)
    {
      printf ("cannot write temporary file: %m\n");
      return 1;
    }

  if (lseek (fd, 0, SEEK_SET) == (off_t) -1)
    {
      printf ("cannot reposition temporary file: %m\n");
      return 1;
    }

  fp = fdopen (fd, "r");
  if (fp == NULL)
    {
      printf ("cannot create stream: %m\n");
      return 1;
    }

  if (fstat (fd, &st1) == -1)
    {
      printf ("first stat failed: %m\n");
      return 1;
    }

  sleep (2);

  ch = fgetc (fp);
  if (ch != 's')
    {
      printf ("did not read correct character: got '%c', expected 's'\n", ch);
      return 1;
    }

  if (fstat (fd, &st2) == -1)
    {
      printf ("second stat failed: %m\n");
      return 1;
    }

  if (st1.st_atime > st2.st_atime)
    {
      puts ("second atime smaller");
      return 1;
    }
  else if (st1.st_atime == st2.st_atime)
    {
      puts ("atime has not changed");
      return 1;
    }

  fclose (fp);

  return 0;
}
Exemplo n.º 18
0
static int
do_test (void)
{
  size_t i;
  wint_t wc;
  FILE *fp;
  int fd;

  fname = (char *) malloc (strlen (test_dir) + sizeof "/bug-ungetwc2.XXXXXX");
  if (fname == NULL)
    {
      puts ("no memory");
      return 1;
    }
  strcpy (stpcpy (fname, test_dir), "/bug-ungetwc2.XXXXXX");
  fd = mkstemp (fname);
  if (fd == -1)
    {
      printf ("cannot open temporary file: %m\n");
      return 1;
    }
  add_temp_file (fname);

  printf ("\nNote: This program runs on %s locale.\n\n", test_locale);

  if (setlocale (LC_ALL, test_locale) == NULL)
    {
      fprintf (stderr, "Cannot use `%s' locale.\n", test_locale);
      exit (EXIT_FAILURE);
    }

  /* Output to the file. */
  if ((fp = fdopen (fd, "w")) == NULL)
    {
      setlocale (LC_ALL, "C");
      fprintf (stderr, "Cannot make `%s' file.\n", fname);
      exit (EXIT_FAILURE);
    }
  fprintf (fp, "%ls", write_wchars);
  fclose (fp);

  /* Read from the file. */
  fp = fopen (fname, "r");
  if (fp == NULL)
    {
      setlocale (LC_ALL, "C");
      error (EXIT_FAILURE, errno, "cannot open %s", fname);
    }

  printf ("%s is opened.\n", fname);

  for (i = 0; i < last_pos; i++)
    {
      wc = getwc (fp);
      printf ("> `%lc' is gotten.\n", wc);
    }

  /* Unget a wide character. */
  ungetwc (unget_wchar, fp);
  printf ("< `%lc' is ungotten.\n", unget_wchar);

  /* Reget the wide character. */
  wc = getwc (fp);
  printf ("> `%lc' is regotten.\n", wc);

  fflush (stdout);
  fclose (fp);

  return 0;
}
Exemplo n.º 19
0
int
do_test (void)
{
  const wchar_t str[] = L"This is a test of putwc\n";
  wchar_t buf[100];
  size_t n = 0;
  FILE *fp;
  int res = 0;

  add_temp_file (outname);

  fp = fopen (outname, "w+");
  if (fp == NULL)
    error (EXIT_FAILURE, errno, "cannot open temporary file");

  for (n = 0; str[n] != L'\0'; ++n)
    putwc (str[n], fp);

  /* First try reading after rewinding.  */
  rewind (fp);

  wmemset (buf, L'\0', sizeof (buf) / sizeof (buf[0]));
  n = 0;
  while (! feof (fp) && n < sizeof (buf) - 1)
    {
      buf[n] = getwc (fp);
      if (buf[n] == WEOF)
	break;
      ++n;
    }
  buf[n] = L'\0';

  if (wcscmp (buf, L"This is a test of putwc\n") != 0)
    {
      puts ("first comparison failed");
      res = 1;
    }

  /* Now close the file, open it again, and read again.  */
  if (fclose (fp) != 0)
    {
      printf ("failure during fclose: %m\n");
      res = 1;
    }

  fp = fopen (outname, "r");
  if (fp == NULL)
    {
      printf ("cannot reopen file: %m\n");
      return 1;
    }

  /* We can remove the file now.  */
  remove (outname);

  wmemset (buf, L'\0', sizeof (buf) / sizeof (buf[0]));
  n = 0;
  while (! feof (fp) && n < sizeof (buf) - 1)
    {
      buf[n] = getwc (fp);
      if (buf[n] == WEOF)
	break;
      ++n;
    }
  buf[n] = L'\0';

  if (wcscmp (buf, L"This is a test of putwc\n") != 0)
    {
      puts ("second comparison failed");
      res = 1;
    }

  if (fclose (fp) != 0)
    {
      printf ("failure during fclose: %m\n");
      res = 1;
    }

  /* Next test: write a bit more than a few bytes.  */
  fp = fopen (outname, "w");
  if (fp == NULL)
    error (EXIT_FAILURE, errno, "cannot open temporary file");

  for (n = 0; n < 4098; ++n)
    putwc (n & 255, fp);

  fclose (fp);

  return res;
}