|
Packit Service |
0b5847 |
/* Detecting file changes using modification times.
|
|
Packit Service |
0b5847 |
Copyright (C) 2017-2020 Free Software Foundation, Inc.
|
|
Packit Service |
0b5847 |
This file is part of the GNU C Library.
|
|
Packit Service |
0b5847 |
|
|
Packit Service |
0b5847 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit Service |
0b5847 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
0b5847 |
License as published by the Free Software Foundation; either
|
|
Packit Service |
0b5847 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
0b5847 |
|
|
Packit Service |
0b5847 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit Service |
0b5847 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
0b5847 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
0b5847 |
Lesser General Public License for more details.
|
|
Packit Service |
0b5847 |
|
|
Packit Service |
0b5847 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
0b5847 |
License along with the GNU C Library; if not, see
|
|
Packit Service |
0b5847 |
<https://www.gnu.org/licenses/>. */
|
|
Packit Service |
0b5847 |
|
|
Packit Service |
0b5847 |
#include <errno.h>
|
|
Packit Service |
0b5847 |
#include <stdbool.h>
|
|
Packit Service |
0b5847 |
#include <stddef.h>
|
|
Packit Service |
0b5847 |
#include <stdio.h>
|
|
Packit Service |
0b5847 |
#include <sys/stat.h>
|
|
Packit Service |
0b5847 |
#include <sys/types.h>
|
|
Packit Service |
0b5847 |
|
|
Packit Service |
0b5847 |
/* Items for identifying a particular file version. Excerpt from
|
|
Packit Service |
0b5847 |
struct stat64. */
|
|
Packit Service |
0b5847 |
struct file_change_detection
|
|
Packit Service |
0b5847 |
{
|
|
Packit Service |
0b5847 |
/* Special values: 0 if file does not exist. -1 to force mismatch
|
|
Packit Service |
0b5847 |
with the next comparison. */
|
|
Packit Service |
0b5847 |
off64_t size;
|
|
Packit Service |
0b5847 |
|
|
Packit Service |
0b5847 |
ino64_t ino;
|
|
Packit Service |
0b5847 |
struct timespec mtime;
|
|
Packit Service |
0b5847 |
struct timespec ctime;
|
|
Packit Service |
0b5847 |
};
|
|
Packit Service |
0b5847 |
|
|
Packit Service |
0b5847 |
/* Returns true if *LEFT and *RIGHT describe the same version of the
|
|
Packit Service |
0b5847 |
same file. */
|
|
Packit Service |
0b5847 |
static bool __attribute__ ((unused))
|
|
Packit Service |
0b5847 |
file_is_unchanged (const struct file_change_detection *left,
|
|
Packit Service |
0b5847 |
const struct file_change_detection *right)
|
|
Packit Service |
0b5847 |
{
|
|
Packit Service |
0b5847 |
if (left->size < 0 || right->size < 0)
|
|
Packit Service |
0b5847 |
/* Negative sizes are used as markers and never match. */
|
|
Packit Service |
0b5847 |
return false;
|
|
Packit Service |
0b5847 |
else if (left->size == 0 && right->size == 0)
|
|
Packit Service |
0b5847 |
/* Both files are empty or do not exist, so they have the same
|
|
Packit Service |
0b5847 |
content, no matter what the other fields indicate. */
|
|
Packit Service |
0b5847 |
return true;
|
|
Packit Service |
0b5847 |
else
|
|
Packit Service |
0b5847 |
return left->size == right->size
|
|
Packit Service |
0b5847 |
&& left->ino == right->ino
|
|
Packit Service |
0b5847 |
&& left->mtime.tv_sec == right->mtime.tv_sec
|
|
Packit Service |
0b5847 |
&& left->mtime.tv_nsec == right->mtime.tv_nsec
|
|
Packit Service |
0b5847 |
&& left->ctime.tv_sec == right->ctime.tv_sec
|
|
Packit Service |
0b5847 |
&& left->ctime.tv_nsec == right->ctime.tv_nsec;
|
|
Packit Service |
0b5847 |
}
|
|
Packit Service |
0b5847 |
|
|
Packit Service |
0b5847 |
/* Extract file change information to *FILE from the stat buffer
|
|
Packit Service |
0b5847 |
*ST. */
|
|
Packit Service |
0b5847 |
static void __attribute__ ((unused))
|
|
Packit Service |
0b5847 |
file_change_detection_for_stat (struct file_change_detection *file,
|
|
Packit Service |
0b5847 |
const struct stat64 *st)
|
|
Packit Service |
0b5847 |
{
|
|
Packit Service |
0b5847 |
if (S_ISDIR (st->st_mode))
|
|
Packit Service |
0b5847 |
/* Treat as empty file. */
|
|
Packit Service |
0b5847 |
file->size = 0;
|
|
Packit Service |
0b5847 |
else if (!S_ISREG (st->st_mode))
|
|
Packit Service |
0b5847 |
/* Non-regular files cannot be cached. */
|
|
Packit Service |
0b5847 |
file->size = -1;
|
|
Packit Service |
0b5847 |
else
|
|
Packit Service |
0b5847 |
{
|
|
Packit Service |
0b5847 |
file->size = st->st_size;
|
|
Packit Service |
0b5847 |
file->ino = st->st_ino;
|
|
Packit Service |
0b5847 |
file->mtime = st->st_mtim;
|
|
Packit Service |
0b5847 |
file->ctime = st->st_ctim;
|
|
Packit Service |
0b5847 |
}
|
|
Packit Service |
0b5847 |
}
|
|
Packit Service |
0b5847 |
|
|
Packit Service |
0b5847 |
/* Writes file change information for PATH to *FILE. Returns true on
|
|
Packit Service |
0b5847 |
success. For benign errors, *FILE is cleared, and true is
|
|
Packit Service |
0b5847 |
returned. For errors indicating resource outages and the like,
|
|
Packit Service |
0b5847 |
false is returned. */
|
|
Packit Service |
0b5847 |
static bool __attribute__ ((unused))
|
|
Packit Service |
0b5847 |
file_change_detection_for_path (struct file_change_detection *file,
|
|
Packit Service |
0b5847 |
const char *path)
|
|
Packit Service |
0b5847 |
{
|
|
Packit Service |
0b5847 |
struct stat64 st;
|
|
Packit Service |
0b5847 |
if (stat64 (path, &st) != 0)
|
|
Packit Service |
0b5847 |
switch (errno)
|
|
Packit Service |
0b5847 |
{
|
|
Packit Service |
0b5847 |
case EACCES:
|
|
Packit Service |
0b5847 |
case EISDIR:
|
|
Packit Service |
0b5847 |
case ELOOP:
|
|
Packit Service |
0b5847 |
case ENOENT:
|
|
Packit Service |
0b5847 |
case ENOTDIR:
|
|
Packit Service |
0b5847 |
case EPERM:
|
|
Packit Service |
0b5847 |
/* Ignore errors due to file system contents. Instead, treat
|
|
Packit Service |
0b5847 |
the file as empty. */
|
|
Packit Service |
0b5847 |
file->size = 0;
|
|
Packit Service |
0b5847 |
return true;
|
|
Packit Service |
0b5847 |
default:
|
|
Packit Service |
0b5847 |
/* Other errors are fatal. */
|
|
Packit Service |
0b5847 |
return false;
|
|
Packit Service |
0b5847 |
}
|
|
Packit Service |
0b5847 |
else /* stat64 was successfull. */
|
|
Packit Service |
0b5847 |
{
|
|
Packit Service |
0b5847 |
file_change_detection_for_stat (file, &st);
|
|
Packit Service |
0b5847 |
return true;
|
|
Packit Service |
0b5847 |
}
|
|
Packit Service |
0b5847 |
}
|
|
Packit Service |
0b5847 |
|
|
Packit Service |
0b5847 |
/* Writes file change information for the stream FP to *FILE. Returns
|
|
Packit Service |
0b5847 |
ture on success, false on failure. If FP is NULL, treat the file
|
|
Packit Service |
0b5847 |
as non-existing. */
|
|
Packit Service |
0b5847 |
static bool __attribute__ ((unused))
|
|
Packit Service |
0b5847 |
file_change_detection_for_fp (struct file_change_detection *file,
|
|
Packit Service |
0b5847 |
FILE *fp)
|
|
Packit Service |
0b5847 |
{
|
|
Packit Service |
0b5847 |
if (fp == NULL)
|
|
Packit Service |
0b5847 |
{
|
|
Packit Service |
0b5847 |
/* The file does not exist. */
|
|
Packit Service |
0b5847 |
file->size = 0;
|
|
Packit Service |
0b5847 |
return true;
|
|
Packit Service |
0b5847 |
}
|
|
Packit Service |
0b5847 |
else
|
|
Packit Service |
0b5847 |
{
|
|
Packit Service |
0b5847 |
struct stat64 st;
|
|
Packit Service |
0b5847 |
if (fstat64 (__fileno (fp), &st) != 0)
|
|
Packit Service |
0b5847 |
/* If we already have a file descriptor, all errors are fatal. */
|
|
Packit Service |
0b5847 |
return false;
|
|
Packit Service |
0b5847 |
else
|
|
Packit Service |
0b5847 |
{
|
|
Packit Service |
0b5847 |
file_change_detection_for_stat (file, &st);
|
|
Packit Service |
0b5847 |
return true;
|
|
Packit Service |
0b5847 |
}
|
|
Packit Service |
0b5847 |
}
|
|
Packit Service |
0b5847 |
}
|