lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Test that values of pathconf and fpathconf are consistent for a file. |
| 2 | Copyright (C) 2013-2015 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | |
| 26 | static void prepare (void); |
| 27 | #define PREPARE(argc, argv) prepare () |
| 28 | |
| 29 | static int do_test (void); |
| 30 | #define TEST_FUNCTION do_test () |
| 31 | |
| 32 | #include "../test-skeleton.c" |
| 33 | |
| 34 | static int dir_fd; |
| 35 | static char *dirbuf; |
| 36 | |
| 37 | static void |
| 38 | prepare (void) |
| 39 | { |
| 40 | size_t test_dir_len = strlen (test_dir); |
| 41 | static const char dir_name[] = "/tst-pathconf.XXXXXX"; |
| 42 | |
| 43 | size_t dirbuflen = test_dir_len + sizeof (dir_name); |
| 44 | dirbuf = malloc (dirbuflen); |
| 45 | if (dirbuf == NULL) |
| 46 | { |
| 47 | puts ("Out of memory"); |
| 48 | exit (1); |
| 49 | } |
| 50 | |
| 51 | snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name); |
| 52 | if (mkdtemp (dirbuf) == NULL) |
| 53 | { |
| 54 | printf ("Cannot create temporary directory: %s\n", strerror (errno)); |
| 55 | exit (1); |
| 56 | } |
| 57 | |
| 58 | add_temp_file (dirbuf); |
| 59 | |
| 60 | dir_fd = open (dirbuf, O_RDONLY); |
| 61 | if (dir_fd == -1) |
| 62 | { |
| 63 | printf ("Cannot open directory: %s\n", strerror (errno)); |
| 64 | exit (1); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | |
| 69 | static int |
| 70 | do_test (void) |
| 71 | { |
| 72 | int ret = 0; |
| 73 | static const char *fifo_name = "some-fifo"; |
| 74 | |
| 75 | size_t filenamelen = strlen (dirbuf) + strlen (fifo_name) + 2; |
| 76 | char *filename = malloc (filenamelen); |
| 77 | |
| 78 | snprintf (filename, filenamelen, "%s/%s", dirbuf, fifo_name); |
| 79 | |
| 80 | /* Create a fifo in the directory. */ |
| 81 | int e = mkfifo (filename, 0777); |
| 82 | if (e == -1) |
| 83 | { |
| 84 | printf ("fifo creation failed (%s)\n", strerror (errno)); |
| 85 | ret = 1; |
| 86 | goto out_nofifo; |
| 87 | } |
| 88 | |
| 89 | long dir_pathconf = pathconf (dirbuf, _PC_PIPE_BUF); |
| 90 | |
| 91 | if (dir_pathconf < 0) |
| 92 | { |
| 93 | printf ("pathconf on directory failed: %s\n", strerror (errno)); |
| 94 | ret = 1; |
| 95 | goto out_nofifo; |
| 96 | } |
| 97 | |
| 98 | long fifo_pathconf = pathconf (filename, _PC_PIPE_BUF); |
| 99 | |
| 100 | if (fifo_pathconf < 0) |
| 101 | { |
| 102 | printf ("pathconf on file failed: %s\n", strerror (errno)); |
| 103 | ret = 1; |
| 104 | goto out_nofifo; |
| 105 | } |
| 106 | |
| 107 | int fifo = open (filename, O_RDONLY | O_NONBLOCK); |
| 108 | |
| 109 | if (fifo < 0) |
| 110 | { |
| 111 | printf ("fifo open failed (%s)\n", strerror (errno)); |
| 112 | ret = 1; |
| 113 | goto out_nofifo; |
| 114 | } |
| 115 | |
| 116 | long dir_fpathconf = fpathconf (dir_fd, _PC_PIPE_BUF); |
| 117 | |
| 118 | if (dir_fpathconf < 0) |
| 119 | { |
| 120 | printf ("fpathconf on directory failed: %s\n", strerror (errno)); |
| 121 | ret = 1; |
| 122 | goto out; |
| 123 | } |
| 124 | |
| 125 | long fifo_fpathconf = fpathconf (fifo, _PC_PIPE_BUF); |
| 126 | |
| 127 | if (fifo_fpathconf < 0) |
| 128 | { |
| 129 | printf ("fpathconf on file failed: %s\n", strerror (errno)); |
| 130 | ret = 1; |
| 131 | goto out; |
| 132 | } |
| 133 | |
| 134 | if (fifo_pathconf != fifo_fpathconf) |
| 135 | { |
| 136 | printf ("fifo pathconf (%ld) != fifo fpathconf (%ld)\n", fifo_pathconf, |
| 137 | fifo_fpathconf); |
| 138 | ret = 1; |
| 139 | goto out; |
| 140 | } |
| 141 | |
| 142 | if (dir_pathconf != fifo_pathconf) |
| 143 | { |
| 144 | printf ("directory pathconf (%ld) != fifo pathconf (%ld)\n", |
| 145 | dir_pathconf, fifo_pathconf); |
| 146 | ret = 1; |
| 147 | goto out; |
| 148 | } |
| 149 | |
| 150 | if (dir_fpathconf != fifo_fpathconf) |
| 151 | { |
| 152 | printf ("directory fpathconf (%ld) != fifo fpathconf (%ld)\n", |
| 153 | dir_fpathconf, fifo_fpathconf); |
| 154 | ret = 1; |
| 155 | goto out; |
| 156 | } |
| 157 | |
| 158 | out: |
| 159 | close (fifo); |
| 160 | out_nofifo: |
| 161 | close (dir_fd); |
| 162 | |
| 163 | if (unlink (filename) != 0) |
| 164 | { |
| 165 | printf ("Could not remove fifo (%s)\n", strerror (errno)); |
| 166 | ret = 1; |
| 167 | } |
| 168 | |
| 169 | if (rmdir (dirbuf) != 0) |
| 170 | { |
| 171 | printf ("Could not remove directory (%s)\n", strerror (errno)); |
| 172 | ret = 1; |
| 173 | } |
| 174 | |
| 175 | return ret; |
| 176 | } |