annotate tests/test-fchdir.c @ 17622:71e733c62cee

savedir: add sorting arg to savedir, streamsavedir; remove fdsavedir Patch based on an idea by Dick Streefland in <https://savannah.gnu.org/patch/?7892>. * NEWS: Document this. * lib/savedir.c (NAME_SIZE_DEFAULT): Remove. (direntry_t, comparison_function): New types. (direntry_cmp_name): New function. (direntry_cmp_inode) [D_INO_IN_DIRENT]: New function. (streamsavedir, savedir): New arg OPTION. (streamsavedir): Simplify memory allocation. (fdsavedir): Remove. * lib/savedir.h (enum savedir_option): New type. (streamsavedir, savedir): New arg OPTION. (fdsavedir): Remove.
author Sergey Poznyakoff <gray@gnu.org.ua>
date Thu, 13 Feb 2014 22:01:39 +0200
parents 344018b6e5d7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11922
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
1 /* Test changing to a directory named by a file descriptor.
17587
344018b6e5d7 maint: update copyright
Eric Blake <eblake@redhat.com>
parents: 17417
diff changeset
2 Copyright (C) 2009-2014 Free Software Foundation, Inc.
11922
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
3
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
7 (at your option) any later version.
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
8
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
12 GNU General Public License for more details.
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
13
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
16
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
18
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
19 #include <config.h>
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
20
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
21 #include <unistd.h>
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
22
12489
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12457
diff changeset
23 #include "signature.h"
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12457
diff changeset
24 SIGNATURE_CHECK (fchdir, int, (int));
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12457
diff changeset
25
11922
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
26 #include <errno.h>
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
27 #include <fcntl.h>
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
28 #include <stdlib.h>
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
29 #include <string.h>
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
30
12402
af3596b94c7a fchdir: fix logic bugs
Eric Blake <ebb9@byu.net>
parents: 12197
diff changeset
31 #include "cloexec.h"
12496
a48d3d749ca5 Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents: 12489
diff changeset
32 #include "macros.h"
11922
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
33
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
34 int
12197
e45d9bb2233e tests: avoid several compiler warnings
Eric Blake <ebb9@byu.net>
parents: 11922
diff changeset
35 main (void)
11922
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
36 {
15685
e186705ad0d3 fchdir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
37 char *cwd;
e186705ad0d3 fchdir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
38 int fd;
11922
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
39 int i;
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
40
15685
e186705ad0d3 fchdir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
41 cwd = getcwd (NULL, 0);
11922
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
42 ASSERT (cwd);
15685
e186705ad0d3 fchdir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
43
e186705ad0d3 fchdir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
44 fd = open (".", O_RDONLY);
11922
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
45 ASSERT (0 <= fd);
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
46
15685
e186705ad0d3 fchdir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
47 /* Test behaviour for invalid file descriptors. */
e186705ad0d3 fchdir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
48 {
e186705ad0d3 fchdir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
49 errno = 0;
e186705ad0d3 fchdir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
50 ASSERT (fchdir (-1) == -1);
e186705ad0d3 fchdir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
51 ASSERT (errno == EBADF);
e186705ad0d3 fchdir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
52 }
e186705ad0d3 fchdir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
53 {
17295
3a09cc104f4c tests: don't assume fd 99 is closed
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
54 close (99);
15685
e186705ad0d3 fchdir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
55 errno = 0;
e186705ad0d3 fchdir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
56 ASSERT (fchdir (99) == -1);
e186705ad0d3 fchdir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
57 ASSERT (errno == EBADF);
e186705ad0d3 fchdir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
58 }
e186705ad0d3 fchdir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
59
e186705ad0d3 fchdir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
60 /* Check for other failure cases. */
11922
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
61 {
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
62 int bad_fd = open ("/dev/null", O_RDONLY);
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
63 ASSERT (0 <= bad_fd);
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
64 errno = 0;
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
65 ASSERT (fchdir (bad_fd) == -1);
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
66 ASSERT (errno == ENOTDIR);
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
67 ASSERT (close (bad_fd) == 0);
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
68 }
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
69
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
70 /* Repeat test twice, once in '.' and once in '..'. */
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
71 for (i = 0; i < 2; i++)
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
72 {
17417
3e765e2f4c46 parse-datetime, tests: don't use "string" + int
Paul Eggert <eggert@cs.ucla.edu>
parents: 17295
diff changeset
73 ASSERT (chdir (&".."[1 - i]) == 0);
11922
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
74 ASSERT (fchdir (fd) == 0);
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
75 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12402
diff changeset
76 size_t len = strlen (cwd) + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12402
diff changeset
77 char *new_dir = malloc (len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12402
diff changeset
78 ASSERT (new_dir);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12402
diff changeset
79 ASSERT (getcwd (new_dir, len) == new_dir);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12402
diff changeset
80 ASSERT (strcmp (cwd, new_dir) == 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12402
diff changeset
81 free (new_dir);
11922
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
82 }
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
83
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
84 /* For second iteration, use a cloned fd, to ensure that dup
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12402
diff changeset
85 remembers whether an fd was associated with a directory. */
11922
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
86 if (!i)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12402
diff changeset
87 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12402
diff changeset
88 int new_fd = dup (fd);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12402
diff changeset
89 ASSERT (0 <= new_fd);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12402
diff changeset
90 ASSERT (close (fd) == 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12402
diff changeset
91 ASSERT (dup2 (new_fd, fd) == fd);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12402
diff changeset
92 ASSERT (close (new_fd) == 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12402
diff changeset
93 ASSERT (dup_cloexec (fd) == new_fd);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12402
diff changeset
94 ASSERT (dup2 (new_fd, fd) == fd);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12402
diff changeset
95 ASSERT (close (new_fd) == 0);
12457
2a3833485e1c fcntl: use to simplify other modules
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
96 ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, new_fd) == new_fd);
2a3833485e1c fcntl: use to simplify other modules
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
97 ASSERT (close (fd) == 0);
2a3833485e1c fcntl: use to simplify other modules
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
98 ASSERT (fcntl (new_fd, F_DUPFD, fd) == fd);
2a3833485e1c fcntl: use to simplify other modules
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
99 ASSERT (close (new_fd) == 0);
13073
03de1efbfdd3 Resolve conflict between the two kinds of module indicators.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
100 #if GNULIB_TEST_DUP3
12457
2a3833485e1c fcntl: use to simplify other modules
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
101 ASSERT (dup3 (fd, new_fd, 0) == new_fd);
2a3833485e1c fcntl: use to simplify other modules
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
102 ASSERT (dup3 (new_fd, fd, 0) == fd);
2a3833485e1c fcntl: use to simplify other modules
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
103 ASSERT (close (new_fd) == 0);
2a3833485e1c fcntl: use to simplify other modules
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
104 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12402
diff changeset
105 }
11922
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
106 }
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
107
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
108 free (cwd);
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
109 return 0;
9750527ffbab fchdir: port to mingw
Eric Blake <ebb9@byu.net>
parents:
diff changeset
110 }