hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame io/file_change_detection.c

Packit 745f38
/* Detecting file changes using modification times.
Packit 745f38
   Copyright (C) 2017-2020 Free Software Foundation, Inc.
Packit 745f38
   This file is part of the GNU C Library.
Packit 745f38
Packit 745f38
   The GNU C Library is free software; you can redistribute it and/or
Packit 745f38
   modify it under the terms of the GNU Lesser General Public
Packit 745f38
   License as published by the Free Software Foundation; either
Packit 745f38
   version 2.1 of the License, or (at your option) any later version.
Packit 745f38
Packit 745f38
   The GNU C Library is distributed in the hope that it will be useful,
Packit 745f38
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 745f38
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 745f38
   Lesser General Public License for more details.
Packit 745f38
Packit 745f38
   You should have received a copy of the GNU Lesser General Public
Packit 745f38
   License along with the GNU C Library; if not, see
Packit 745f38
   <https://www.gnu.org/licenses/>.  */
Packit 745f38
Packit 745f38
#include <file_change_detection.h>
Packit 745f38
Packit 745f38
#include <errno.h>
Packit 745f38
#include <stddef.h>
Packit 745f38
Packit 745f38
bool
Packit 745f38
__file_is_unchanged (const struct file_change_detection *left,
Packit 745f38
                     const struct file_change_detection *right)
Packit 745f38
{
Packit 745f38
  if (left->size < 0 || right->size < 0)
Packit 745f38
    /* Negative sizes are used as markers and never match.  */
Packit 745f38
    return false;
Packit 745f38
  else if (left->size == 0 && right->size == 0)
Packit 745f38
    /* Both files are empty or do not exist, so they have the same
Packit 745f38
       content, no matter what the other fields indicate.  */
Packit 745f38
    return true;
Packit 745f38
  else
Packit 745f38
    return left->size == right->size
Packit 745f38
      && left->ino == right->ino
Packit 745f38
      && left->mtime.tv_sec == right->mtime.tv_sec
Packit 745f38
      && left->mtime.tv_nsec == right->mtime.tv_nsec
Packit 745f38
      && left->ctime.tv_sec == right->ctime.tv_sec
Packit 745f38
      && left->ctime.tv_nsec == right->ctime.tv_nsec;
Packit 745f38
}
Packit 745f38
libc_hidden_def (__file_is_unchanged)
Packit 745f38
Packit 745f38
void
Packit 745f38
__file_change_detection_for_stat (struct file_change_detection *file,
Packit 745f38
                                  const struct stat64 *st)
Packit 745f38
{
Packit 745f38
  if (S_ISDIR (st->st_mode))
Packit 745f38
    /* Treat as empty file.  */
Packit 745f38
    file->size = 0;
Packit 745f38
  else if (!S_ISREG (st->st_mode))
Packit 745f38
    /* Non-regular files cannot be cached.  */
Packit 745f38
    file->size = -1;
Packit 745f38
  else
Packit 745f38
    {
Packit 745f38
      file->size = st->st_size;
Packit 745f38
      file->ino = st->st_ino;
Packit 745f38
      file->mtime = st->st_mtim;
Packit 745f38
      file->ctime = st->st_ctim;
Packit 745f38
    }
Packit 745f38
}
Packit 745f38
libc_hidden_def (__file_change_detection_for_stat)
Packit 745f38
Packit 745f38
bool
Packit 745f38
__file_change_detection_for_path (struct file_change_detection *file,
Packit 745f38
                                  const char *path)
Packit 745f38
{
Packit 745f38
  struct stat64 st;
Packit 745f38
  if (stat64 (path, &st) != 0)
Packit 745f38
    switch (errno)
Packit 745f38
      {
Packit 745f38
      case EACCES:
Packit 745f38
      case EISDIR:
Packit 745f38
      case ELOOP:
Packit 745f38
      case ENOENT:
Packit 745f38
      case ENOTDIR:
Packit 745f38
      case EPERM:
Packit 745f38
        /* Ignore errors due to file system contents.  Instead, treat
Packit 745f38
           the file as empty.  */
Packit 745f38
        file->size = 0;
Packit 745f38
        return true;
Packit 745f38
      default:
Packit 745f38
        /* Other errors are fatal.  */
Packit 745f38
        return false;
Packit 745f38
      }
Packit 745f38
  else /* stat64 was successfull.  */
Packit 745f38
    {
Packit 745f38
      __file_change_detection_for_stat (file, &st);
Packit 745f38
      return true;
Packit 745f38
    }
Packit 745f38
}
Packit 745f38
libc_hidden_def (__file_change_detection_for_path)
Packit 745f38
Packit 745f38
bool
Packit 745f38
__file_change_detection_for_fp (struct file_change_detection *file,
Packit 745f38
                                FILE *fp)
Packit 745f38
{
Packit 745f38
  if (fp == NULL)
Packit 745f38
    {
Packit 745f38
      /* The file does not exist.  */
Packit 745f38
      file->size = 0;
Packit 745f38
      return true;
Packit 745f38
    }
Packit 745f38
  else
Packit 745f38
    {
Packit 745f38
      struct stat64 st;
Packit 745f38
      if (fstat64 (__fileno (fp), &st) != 0)
Packit 745f38
        /* If we already have a file descriptor, all errors are fatal.  */
Packit 745f38
        return false;
Packit 745f38
      else
Packit 745f38
        {
Packit 745f38
          __file_change_detection_for_stat (file, &st);
Packit 745f38
          return true;
Packit 745f38
        }
Packit 745f38
    }
Packit 745f38
}
Packit 745f38
libc_hidden_def (__file_change_detection_for_fp)