1 /* Test truncating a file.
2 Copyright (C) 2017-2020 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
21 #include "signature.h"
22 SIGNATURE_CHECK (truncate
, int, (const char *, off_t
));
27 #include "ignore-value.h"
30 #define BASE "test-truncate.t"
33 main (int argc
, char *argv
[])
35 /* Clean up any trash from prior testsuite runs. */
36 ignore_value (system ("rm -rf " BASE
"*"));
39 int fd
= open (BASE
"file", O_RDWR
| O_TRUNC
| O_CREAT
, 0600);
41 ASSERT (write (fd
, "Hello", 5) == 5);
46 int fd
= open (BASE
"file", O_RDONLY
);
48 ASSERT (lseek (fd
, 0, SEEK_END
) == 5);
52 /* Test increasing the size. */
53 ASSERT (truncate (BASE
"file", 314159) == 0);
55 int fd
= open (BASE
"file", O_RDONLY
);
57 ASSERT (lseek (fd
, 0, SEEK_END
) == 314159);
61 /* Test reducing the size. */
62 ASSERT (truncate (BASE
"file", 3) == 0);
64 int fd
= open (BASE
"file", O_RDONLY
);
66 ASSERT (lseek (fd
, 0, SEEK_END
) == 3);
70 /* Test reducing the size to 0. */
71 ASSERT (truncate (BASE
"file", 0) == 0);
73 int fd
= open (BASE
"file", O_RDONLY
);
75 ASSERT (lseek (fd
, 0, SEEK_END
) == 0);
79 /* Test behaviour for nonexistent files. */
82 ASSERT (truncate ("/nonexistent", 0) == -1);
83 ASSERT (errno
== ENOENT
);
85 /* Test behaviour for directories. */
88 ASSERT (truncate (".", 0) == -1);
89 ASSERT (errno
== EISDIR
90 || /* on native Windows */ errno
== EACCES
);
92 /* Test behaviour for trailing slashes. */
95 ASSERT (truncate (BASE
"file/", 0) == -1);
96 ASSERT (errno
== ENOTDIR
97 || /* on native Windows */ errno
== EINVAL
);
99 /* Test behaviour for invalid lengths. */
102 ASSERT (truncate (BASE
"file", -3) == -1);
103 ASSERT (errno
== EINVAL
);
107 ASSERT (unlink (BASE
"file") == 0);