]>
Commit | Line | Data |
---|---|---|
adbb8027 | 1 | /* Test that values of pathconf and fpathconf are consistent for a file. |
f7a9f785 | 2 | Copyright (C) 2013-2016 Free Software Foundation, Inc. |
adbb8027 SP |
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); | |
850c6760 | 44 | dirbuf = xmalloc (dirbuflen); |
adbb8027 SP |
45 | |
46 | snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name); | |
47 | if (mkdtemp (dirbuf) == NULL) | |
48 | { | |
49 | printf ("Cannot create temporary directory: %s\n", strerror (errno)); | |
50 | exit (1); | |
51 | } | |
52 | ||
53 | add_temp_file (dirbuf); | |
54 | ||
55 | dir_fd = open (dirbuf, O_RDONLY); | |
56 | if (dir_fd == -1) | |
57 | { | |
58 | printf ("Cannot open directory: %s\n", strerror (errno)); | |
59 | exit (1); | |
60 | } | |
61 | } | |
62 | ||
63 | ||
64 | static int | |
65 | do_test (void) | |
66 | { | |
67 | int ret = 0; | |
68 | static const char *fifo_name = "some-fifo"; | |
69 | ||
70 | size_t filenamelen = strlen (dirbuf) + strlen (fifo_name) + 2; | |
850c6760 | 71 | char *filename = xmalloc (filenamelen); |
adbb8027 SP |
72 | |
73 | snprintf (filename, filenamelen, "%s/%s", dirbuf, fifo_name); | |
74 | ||
75 | /* Create a fifo in the directory. */ | |
76 | int e = mkfifo (filename, 0777); | |
77 | if (e == -1) | |
78 | { | |
79 | printf ("fifo creation failed (%s)\n", strerror (errno)); | |
80 | ret = 1; | |
81 | goto out_nofifo; | |
82 | } | |
83 | ||
84 | long dir_pathconf = pathconf (dirbuf, _PC_PIPE_BUF); | |
85 | ||
86 | if (dir_pathconf < 0) | |
87 | { | |
88 | printf ("pathconf on directory failed: %s\n", strerror (errno)); | |
89 | ret = 1; | |
90 | goto out_nofifo; | |
91 | } | |
92 | ||
93 | long fifo_pathconf = pathconf (filename, _PC_PIPE_BUF); | |
94 | ||
95 | if (fifo_pathconf < 0) | |
96 | { | |
97 | printf ("pathconf on file failed: %s\n", strerror (errno)); | |
98 | ret = 1; | |
99 | goto out_nofifo; | |
100 | } | |
101 | ||
102 | int fifo = open (filename, O_RDONLY | O_NONBLOCK); | |
103 | ||
104 | if (fifo < 0) | |
105 | { | |
106 | printf ("fifo open failed (%s)\n", strerror (errno)); | |
107 | ret = 1; | |
108 | goto out_nofifo; | |
109 | } | |
110 | ||
111 | long dir_fpathconf = fpathconf (dir_fd, _PC_PIPE_BUF); | |
112 | ||
113 | if (dir_fpathconf < 0) | |
114 | { | |
115 | printf ("fpathconf on directory failed: %s\n", strerror (errno)); | |
116 | ret = 1; | |
117 | goto out; | |
118 | } | |
119 | ||
120 | long fifo_fpathconf = fpathconf (fifo, _PC_PIPE_BUF); | |
121 | ||
122 | if (fifo_fpathconf < 0) | |
123 | { | |
124 | printf ("fpathconf on file failed: %s\n", strerror (errno)); | |
125 | ret = 1; | |
126 | goto out; | |
127 | } | |
128 | ||
129 | if (fifo_pathconf != fifo_fpathconf) | |
130 | { | |
131 | printf ("fifo pathconf (%ld) != fifo fpathconf (%ld)\n", fifo_pathconf, | |
132 | fifo_fpathconf); | |
133 | ret = 1; | |
134 | goto out; | |
135 | } | |
136 | ||
137 | if (dir_pathconf != fifo_pathconf) | |
138 | { | |
139 | printf ("directory pathconf (%ld) != fifo pathconf (%ld)\n", | |
140 | dir_pathconf, fifo_pathconf); | |
141 | ret = 1; | |
142 | goto out; | |
143 | } | |
144 | ||
145 | if (dir_fpathconf != fifo_fpathconf) | |
146 | { | |
147 | printf ("directory fpathconf (%ld) != fifo fpathconf (%ld)\n", | |
148 | dir_fpathconf, fifo_fpathconf); | |
149 | ret = 1; | |
150 | goto out; | |
151 | } | |
152 | ||
153 | out: | |
154 | close (fifo); | |
155 | out_nofifo: | |
156 | close (dir_fd); | |
157 | ||
158 | if (unlink (filename) != 0) | |
159 | { | |
160 | printf ("Could not remove fifo (%s)\n", strerror (errno)); | |
161 | ret = 1; | |
162 | } | |
163 | ||
164 | if (rmdir (dirbuf) != 0) | |
165 | { | |
166 | printf ("Could not remove directory (%s)\n", strerror (errno)); | |
167 | ret = 1; | |
168 | } | |
169 | ||
170 | return ret; | |
171 | } |