Blame posix/tst-pathconf.c

Packit 6c4009
/* Test that values of pathconf and fpathconf are consistent for a file.
Packit 6c4009
   Copyright (C) 2013-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 <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
static char *dirbuf;
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-pathconf.XXXXXX";
Packit 6c4009
Packit 6c4009
  size_t dirbuflen = test_dir_len + sizeof (dir_name);
Packit 6c4009
  dirbuf = xmalloc (dirbuflen);
Packit 6c4009
Packit 6c4009
  snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
Packit 6c4009
  if (mkdtemp (dirbuf) == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("Cannot create temporary directory: %s\n", strerror (errno));
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  add_temp_file (dirbuf);
Packit 6c4009
Packit 6c4009
  dir_fd = open (dirbuf, O_RDONLY);
Packit 6c4009
  if (dir_fd == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("Cannot open directory: %s\n", strerror (errno));
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
  int ret = 0;
Packit 6c4009
  static const char *fifo_name = "some-fifo";
Packit 6c4009
Packit 6c4009
  size_t filenamelen = strlen (dirbuf) + strlen (fifo_name) + 2;
Packit 6c4009
  char *filename = xmalloc (filenamelen);
Packit 6c4009
Packit 6c4009
  snprintf (filename, filenamelen, "%s/%s", dirbuf, fifo_name);
Packit 6c4009
Packit 6c4009
  /* Create a fifo in the directory.  */
Packit 6c4009
  int e = mkfifo (filename, 0777);
Packit 6c4009
  if (e == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("fifo creation failed (%s)\n", strerror (errno));
Packit 6c4009
      ret = 1;
Packit 6c4009
      goto out_nofifo;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  long dir_pathconf = pathconf (dirbuf, _PC_PIPE_BUF);
Packit 6c4009
Packit 6c4009
  if (dir_pathconf < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("pathconf on directory failed: %s\n", strerror (errno));
Packit 6c4009
      ret = 1;
Packit 6c4009
      goto out_nofifo;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  long fifo_pathconf = pathconf (filename, _PC_PIPE_BUF);
Packit 6c4009
Packit 6c4009
  if (fifo_pathconf < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("pathconf on file failed: %s\n", strerror (errno));
Packit 6c4009
      ret = 1;
Packit 6c4009
      goto out_nofifo;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int fifo = open (filename, O_RDONLY | O_NONBLOCK);
Packit 6c4009
Packit 6c4009
  if (fifo < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("fifo open failed (%s)\n", strerror (errno));
Packit 6c4009
      ret = 1;
Packit 6c4009
      goto out_nofifo;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  long dir_fpathconf = fpathconf (dir_fd, _PC_PIPE_BUF);
Packit 6c4009
Packit 6c4009
  if (dir_fpathconf < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("fpathconf on directory failed: %s\n", strerror (errno));
Packit 6c4009
      ret = 1;
Packit 6c4009
      goto out;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  long fifo_fpathconf = fpathconf (fifo, _PC_PIPE_BUF);
Packit 6c4009
Packit 6c4009
  if (fifo_fpathconf < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("fpathconf on file failed: %s\n", strerror (errno));
Packit 6c4009
      ret = 1;
Packit 6c4009
      goto out;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (fifo_pathconf != fifo_fpathconf)
Packit 6c4009
    {
Packit 6c4009
      printf ("fifo pathconf (%ld) != fifo fpathconf (%ld)\n", fifo_pathconf,
Packit 6c4009
	      fifo_fpathconf);
Packit 6c4009
      ret = 1;
Packit 6c4009
      goto out;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (dir_pathconf != fifo_pathconf)
Packit 6c4009
    {
Packit 6c4009
      printf ("directory pathconf (%ld) != fifo pathconf (%ld)\n",
Packit 6c4009
	      dir_pathconf, fifo_pathconf);
Packit 6c4009
      ret = 1;
Packit 6c4009
      goto out;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (dir_fpathconf != fifo_fpathconf)
Packit 6c4009
    {
Packit 6c4009
      printf ("directory fpathconf (%ld) != fifo fpathconf (%ld)\n",
Packit 6c4009
	      dir_fpathconf, fifo_fpathconf);
Packit 6c4009
      ret = 1;
Packit 6c4009
      goto out;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
out:
Packit 6c4009
  close (fifo);
Packit 6c4009
out_nofifo:
Packit 6c4009
  close (dir_fd);
Packit 6c4009
Packit 6c4009
  if (unlink (filename) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("Could not remove fifo (%s)\n", strerror (errno));
Packit 6c4009
      ret = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return ret;
Packit 6c4009
}