1 /* Detecting file changes using modification times.
2 Copyright (C) 2017-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <https://www.gnu.org/licenses/>. */
19 #include <file_change_detection.h>
25 __file_is_unchanged (const struct file_change_detection
*left
,
26 const struct file_change_detection
*right
)
28 if (left
->size
< 0 || right
->size
< 0)
29 /* Negative sizes are used as markers and never match. */
31 else if (left
->size
== 0 && right
->size
== 0)
32 /* Both files are empty or do not exist, so they have the same
33 content, no matter what the other fields indicate. */
36 return left
->size
== right
->size
37 && left
->ino
== right
->ino
38 && left
->mtime
.tv_sec
== right
->mtime
.tv_sec
39 && left
->mtime
.tv_nsec
== right
->mtime
.tv_nsec
40 && left
->ctime
.tv_sec
== right
->ctime
.tv_sec
41 && left
->ctime
.tv_nsec
== right
->ctime
.tv_nsec
;
43 libc_hidden_def (__file_is_unchanged
)
46 __file_change_detection_for_stat (struct file_change_detection
*file
,
47 const struct __stat64_t64
*st
)
49 if (S_ISDIR (st
->st_mode
))
50 /* Treat as empty file. */
52 else if (!S_ISREG (st
->st_mode
))
53 /* Non-regular files cannot be cached. */
57 file
->size
= st
->st_size
;
58 file
->ino
= st
->st_ino
;
59 file
->mtime
= st
->st_mtim
;
60 file
->ctime
= st
->st_ctim
;
63 libc_hidden_def (__file_change_detection_for_stat
)
66 __file_change_detection_for_path (struct file_change_detection
*file
,
69 struct __stat64_t64 st
;
70 if (__stat64_time64 (path
, &st
) != 0)
79 /* Ignore errors due to file system contents. Instead, treat
84 /* Other errors are fatal. */
87 else /* stat64 was successfull. */
89 __file_change_detection_for_stat (file
, &st
);
93 libc_hidden_def (__file_change_detection_for_path
)
96 __file_change_detection_for_fp (struct file_change_detection
*file
,
101 /* The file does not exist. */
107 struct __stat64_t64 st
;
108 if (__fstat64_time64 (__fileno (fp
), &st
) != 0)
109 /* If we already have a file descriptor, all errors are fatal. */
113 __file_change_detection_for_stat (file
, &st
);
118 libc_hidden_def (__file_change_detection_for_fp
)