1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003-2007 Tim Kientzle
5  * All rights reserved.
6  */
7 #include "test.h"
8 #if defined(HAVE_UTIME_H)
9 #include <utime.h>
10 #elif defined(HAVE_SYS_UTIME_H)
11 #include <sys/utime.h>
12 #endif
13 
DEFINE_TEST(test_option_u)14 DEFINE_TEST(test_option_u)
15 {
16           struct utimbuf times;
17           char *p;
18           size_t s;
19           int r;
20 
21           /* Create a file. */
22           assertMakeFile("f", 0644, "a");
23 
24           /* Copy the file to the "copy" dir. */
25           r = systemf("echo f| %s -pd copy >copy.out 2>copy.err",
26               testprog);
27           assertEqualInt(r, 0);
28 
29           /* Check that the file contains only a single "a" */
30           p = slurpfile(&s, "copy/f");
31           assertEqualInt(s, 1);
32           assertEqualMem(p, "a", 1);
33           free(p);
34 
35           /* Recreate the file with a single "b" */
36           assertMakeFile("f", 0644, "b");
37 
38           /* Set the mtime to the distant past. */
39           memset(&times, 0, sizeof(times));
40           times.actime = 1;
41           times.modtime = 3;
42           assertEqualInt(0, utime("f", &times));
43 
44           /* Copy the file to the "copy" dir. */
45           r = systemf("echo f| %s -pd copy >copy.out 2>copy.err",
46               testprog);
47           assertEqualInt(r, 0);
48 
49           /* Verify that the file hasn't changed (it wasn't overwritten) */
50           p = slurpfile(&s, "copy/f");
51           assertEqualInt(s, 1);
52           assertEqualMem(p, "a", 1);
53           free(p);
54 
55           /* Copy the file to the "copy" dir with -u (force) */
56           r = systemf("echo f| %s -pud copy >copy.out 2>copy.err",
57               testprog);
58           assertEqualInt(r, 0);
59 
60           /* Verify that the file has changed (it was overwritten) */
61           p = slurpfile(&s, "copy/f");
62           assertEqualInt(s, 1);
63           assertEqualMem(p, "b", 1);
64           free(p);
65 }
66