Blame io/tst-readlinkat.c

Packit 6c4009
#include <dirent.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void prepare (void);
Packit 6c4009
#define PREPARE(argc, argv) prepare ()
Packit 6c4009
Packit 6c4009
static int do_test (void);
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
Packit 6c4009
#include "../test-skeleton.c"
Packit 6c4009
Packit 6c4009
static int dir_fd;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
prepare (void)
Packit 6c4009
{
Packit 6c4009
  size_t test_dir_len = strlen (test_dir);
Packit 6c4009
  static const char dir_name[] = "/tst-symlinkat.XXXXXX";
Packit 6c4009
Packit 6c4009
  size_t dirbuflen = test_dir_len + sizeof (dir_name);
Packit 6c4009
  char *dirbuf = malloc (dirbuflen);
Packit 6c4009
  if (dirbuf == NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("out of memory");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
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
Packit 6c4009
  dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY);
Packit 6c4009
  if (dir_fd == -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("cannot open directory");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  /* fdopendir takes over the descriptor, make a copy.  */
Packit 6c4009
  int dupfd = dup (dir_fd);
Packit 6c4009
  if (dupfd == -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("dup failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (lseek (dupfd, 0, SEEK_SET) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("1st lseek failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* The directory should be empty safe the . and .. files.  */
Packit 6c4009
  DIR *dir = fdopendir (dupfd);
Packit 6c4009
  if (dir == NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("fdopendir failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  struct dirent64 *d;
Packit 6c4009
  while ((d = readdir64 (dir)) != NULL)
Packit 6c4009
    if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
Packit 6c4009
      {
Packit 6c4009
	printf ("temp directory contains file \"%s\"\n", d->d_name);
Packit 6c4009
	return 1;
Packit 6c4009
      }
Packit 6c4009
  closedir (dir);
Packit 6c4009
Packit 6c4009
  static const char symlinkcontent[] = "some-file";
Packit 6c4009
  if (symlinkat (symlinkcontent, dir_fd, "another-file") != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("symlinkat failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  struct stat64 st2;
Packit 6c4009
  if (fstatat64 (dir_fd, "another-file", &st2, AT_SYMLINK_NOFOLLOW) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("fstatat64 failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (!S_ISLNK (st2.st_mode))
Packit 6c4009
    {
Packit 6c4009
      puts ("2nd fstatat64 does not show file is a symlink");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (fstatat64 (dir_fd, symlinkcontent, &st2, AT_SYMLINK_NOFOLLOW) == 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("2nd fstatat64 succeeded");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  char buf[100];
Packit 6c4009
  int n = readlinkat (dir_fd, "another-file", buf, sizeof (buf));
Packit 6c4009
  if (n == -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("readlinkat failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (n != sizeof (symlinkcontent) - 1)
Packit 6c4009
    {
Packit 6c4009
      printf ("readlinkat returned %d, expected %zu\n",
Packit 6c4009
	      n, sizeof (symlinkcontent) - 1);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (strncmp (buf, symlinkcontent, n) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("readlinkat retrieved wrong link content");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (unlinkat (dir_fd, "another-file", 0) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("unlinkat failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  close (dir_fd);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}