Mercurial > hg > octave-shane > gnulib-hg
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 |
rev | line source |
---|---|
11922 | 1 /* Test changing to a directory named by a file descriptor. |
17587 | 2 Copyright (C) 2009-2014 Free Software Foundation, Inc. |
11922 | 3 |
4 This program is free software: you can redistribute it and/or modify | |
5 it under the terms of the GNU General Public License as published by | |
6 the Free Software Foundation; either version 3 of the License, or | |
7 (at your option) any later version. | |
8 | |
9 This program is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
16 | |
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */ | |
18 | |
19 #include <config.h> | |
20 | |
21 #include <unistd.h> | |
22 | |
12489 | 23 #include "signature.h" |
24 SIGNATURE_CHECK (fchdir, int, (int)); | |
25 | |
11922 | 26 #include <errno.h> |
27 #include <fcntl.h> | |
28 #include <stdlib.h> | |
29 #include <string.h> | |
30 | |
12402 | 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 | 33 |
34 int | |
12197
e45d9bb2233e
tests: avoid several compiler warnings
Eric Blake <ebb9@byu.net>
parents:
11922
diff
changeset
|
35 main (void) |
11922 | 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 | 39 int i; |
40 | |
15685
e186705ad0d3
fchdir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents:
14079
diff
changeset
|
41 cwd = getcwd (NULL, 0); |
11922 | 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 | 45 ASSERT (0 <= fd); |
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 | 61 { |
62 int bad_fd = open ("/dev/null", O_RDONLY); | |
63 ASSERT (0 <= bad_fd); | |
64 errno = 0; | |
65 ASSERT (fchdir (bad_fd) == -1); | |
66 ASSERT (errno == ENOTDIR); | |
67 ASSERT (close (bad_fd) == 0); | |
68 } | |
69 | |
70 /* Repeat test twice, once in '.' and once in '..'. */ | |
71 for (i = 0; i < 2; i++) | |
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 | 74 ASSERT (fchdir (fd) == 0); |
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 | 82 } |
83 | |
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 | 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 | 106 } |
107 | |
108 free (cwd); | |
109 return 0; | |
110 } |