a2cf7d
From: Florian Weimer <fweimer@redhat.com>
a2cf7d
Date: Tue, 11 Feb 2020 12:52:06 +0000 (+0100)
a2cf7d
Subject: Add internal <file_change_detection.h> header file
a2cf7d
X-Git-Url: https://sourceware.org/git/?p=glibc.git;a=commitdiff_plain;h=6c80c6e8767b860a5e18e136d04a80be2a8dce15
a2cf7d
a2cf7d
Add internal <file_change_detection.h> header file
a2cf7d
a2cf7d
The code started out with bits form resolv/resolv_conf.c, but it
a2cf7d
was enhanced to deal with directories and FIFOs in a more predictable
a2cf7d
manner.  A test case is included as well.
a2cf7d
a2cf7d
This will be used to implement the /etc/resolv.conf change detection.
a2cf7d
a2cf7d
This currently lives in a header file only.  Once there are multiple
a2cf7d
users, the implementations should be moved into C files.
a2cf7d
---
a2cf7d
a2cf7d
diff -rupN a/include/file_change_detection.h b/include/file_change_detection.h
a2cf7d
--- a/include/file_change_detection.h	1969-12-31 19:00:00.000000000 -0500
a2cf7d
+++ b/include/file_change_detection.h	2020-03-25 16:57:24.227929816 -0400
a2cf7d
@@ -0,0 +1,140 @@
a2cf7d
+/* Detecting file changes using modification times.
a2cf7d
+   Copyright (C) 2017-2020 Free Software Foundation, Inc.
a2cf7d
+   This file is part of the GNU C Library.
a2cf7d
+
a2cf7d
+   The GNU C Library is free software; you can redistribute it and/or
a2cf7d
+   modify it under the terms of the GNU Lesser General Public
a2cf7d
+   License as published by the Free Software Foundation; either
a2cf7d
+   version 2.1 of the License, or (at your option) any later version.
a2cf7d
+
a2cf7d
+   The GNU C Library is distributed in the hope that it will be useful,
a2cf7d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
a2cf7d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
a2cf7d
+   Lesser General Public License for more details.
a2cf7d
+
a2cf7d
+   You should have received a copy of the GNU Lesser General Public
a2cf7d
+   License along with the GNU C Library; if not, see
a2cf7d
+   <https://www.gnu.org/licenses/>.  */
a2cf7d
+
a2cf7d
+#include <errno.h>
a2cf7d
+#include <stdbool.h>
a2cf7d
+#include <stddef.h>
a2cf7d
+#include <stdio.h>
a2cf7d
+#include <sys/stat.h>
a2cf7d
+#include <sys/types.h>
a2cf7d
+
a2cf7d
+/* Items for identifying a particular file version.  Excerpt from
a2cf7d
+   struct stat64.  */
a2cf7d
+struct file_change_detection
a2cf7d
+{
a2cf7d
+  /* Special values: 0 if file does not exist.  -1 to force mismatch
a2cf7d
+     with the next comparison.  */
a2cf7d
+  off64_t size;
a2cf7d
+
a2cf7d
+  ino64_t ino;
a2cf7d
+  struct timespec mtime;
a2cf7d
+  struct timespec ctime;
a2cf7d
+};
a2cf7d
+
a2cf7d
+/* Returns true if *LEFT and *RIGHT describe the same version of the
a2cf7d
+   same file.  */
a2cf7d
+static bool __attribute__ ((unused))
a2cf7d
+file_is_unchanged (const struct file_change_detection *left,
a2cf7d
+                   const struct file_change_detection *right)
a2cf7d
+{
a2cf7d
+  if (left->size < 0 || right->size < 0)
a2cf7d
+    /* Negative sizes are used as markers and never match.  */
a2cf7d
+    return false;
a2cf7d
+  else if (left->size == 0 && right->size == 0)
a2cf7d
+    /* Both files are empty or do not exist, so they have the same
a2cf7d
+       content, no matter what the other fields indicate.  */
a2cf7d
+    return true;
a2cf7d
+  else
a2cf7d
+    return left->size == right->size
a2cf7d
+      && left->ino == right->ino
a2cf7d
+      && left->mtime.tv_sec == right->mtime.tv_sec
a2cf7d
+      && left->mtime.tv_nsec == right->mtime.tv_nsec
a2cf7d
+      && left->ctime.tv_sec == right->ctime.tv_sec
a2cf7d
+      && left->ctime.tv_nsec == right->ctime.tv_nsec;
a2cf7d
+}
a2cf7d
+
a2cf7d
+/* Extract file change information to *FILE from the stat buffer
a2cf7d
+   *ST.  */
a2cf7d
+static void __attribute__ ((unused))
a2cf7d
+file_change_detection_for_stat (struct file_change_detection *file,
a2cf7d
+                                const struct stat64 *st)
a2cf7d
+{
a2cf7d
+  if (S_ISDIR (st->st_mode))
a2cf7d
+    /* Treat as empty file.  */
a2cf7d
+    file->size = 0;
a2cf7d
+  else if (!S_ISREG (st->st_mode))
a2cf7d
+    /* Non-regular files cannot be cached.  */
a2cf7d
+    file->size = -1;
a2cf7d
+  else
a2cf7d
+    {
a2cf7d
+      file->size = st->st_size;
a2cf7d
+      file->ino = st->st_ino;
a2cf7d
+      file->mtime = st->st_mtim;
a2cf7d
+      file->ctime = st->st_ctim;
a2cf7d
+    }
a2cf7d
+}
a2cf7d
+
a2cf7d
+/* Writes file change information for PATH to *FILE.  Returns true on
a2cf7d
+   success.  For benign errors, *FILE is cleared, and true is
a2cf7d
+   returned.  For errors indicating resource outages and the like,
a2cf7d
+   false is returned.  */
a2cf7d
+static bool __attribute__ ((unused))
a2cf7d
+file_change_detection_for_path (struct file_change_detection *file,
a2cf7d
+                                const char *path)
a2cf7d
+{
a2cf7d
+  struct stat64 st;
a2cf7d
+  if (stat64 (path, &st) != 0)
a2cf7d
+    switch (errno)
a2cf7d
+      {
a2cf7d
+      case EACCES:
a2cf7d
+      case EISDIR:
a2cf7d
+      case ELOOP:
a2cf7d
+      case ENOENT:
a2cf7d
+      case ENOTDIR:
a2cf7d
+      case EPERM:
a2cf7d
+        /* Ignore errors due to file system contents.  Instead, treat
a2cf7d
+           the file as empty.  */
a2cf7d
+        file->size = 0;
a2cf7d
+        return true;
a2cf7d
+      default:
a2cf7d
+        /* Other errors are fatal.  */
a2cf7d
+        return false;
a2cf7d
+      }
a2cf7d
+  else /* stat64 was successfull.  */
a2cf7d
+    {
a2cf7d
+      file_change_detection_for_stat (file, &st);
a2cf7d
+      return true;
a2cf7d
+    }
a2cf7d
+}
a2cf7d
+
a2cf7d
+/* Writes file change information for the stream FP to *FILE.  Returns
a2cf7d
+   ture on success, false on failure.  If FP is NULL, treat the file
a2cf7d
+   as non-existing.  */
a2cf7d
+static bool __attribute__ ((unused))
a2cf7d
+file_change_detection_for_fp (struct file_change_detection *file,
a2cf7d
+                              FILE *fp)
a2cf7d
+{
a2cf7d
+  if (fp == NULL)
a2cf7d
+    {
a2cf7d
+      /* The file does not exist.  */
a2cf7d
+      file->size = 0;
a2cf7d
+      return true;
a2cf7d
+    }
a2cf7d
+  else
a2cf7d
+    {
a2cf7d
+      struct stat64 st;
a2cf7d
+      if (fstat64 (__fileno (fp), &st) != 0)
a2cf7d
+        /* If we already have a file descriptor, all errors are fatal.  */
a2cf7d
+        return false;
a2cf7d
+      else
a2cf7d
+        {
a2cf7d
+          file_change_detection_for_stat (file, &st);
a2cf7d
+          return true;
a2cf7d
+        }
a2cf7d
+    }
a2cf7d
+}
a2cf7d
diff -rupN a/io/Makefile b/io/Makefile
a2cf7d
--- a/io/Makefile	2020-03-25 16:55:42.442195992 -0400
a2cf7d
+++ b/io/Makefile	2020-03-25 16:58:48.571023810 -0400
a2cf7d
@@ -74,6 +74,7 @@ tests		:= test-utime test-stat test-stat
a2cf7d
 		   tst-posix_fallocate tst-posix_fallocate64 \
a2cf7d
 		   tst-fts tst-fts-lfs tst-open-tmpfile \
a2cf7d
 		   tst-copy_file_range tst-getcwd-abspath \
a2cf7d
+		   tst-file_change_detection
a2cf7d
 
a2cf7d
 # Likewise for statx, but we do not need static linking here.
a2cf7d
 tests-internal += tst-statx
a2cf7d
diff -rupN a/io/tst-file_change_detection.c b/io/tst-file_change_detection.c
a2cf7d
--- a/io/tst-file_change_detection.c	1969-12-31 19:00:00.000000000 -0500
a2cf7d
+++ b/io/tst-file_change_detection.c	2020-03-25 16:57:24.242930366 -0400
a2cf7d
@@ -0,0 +1,206 @@
a2cf7d
+/* Test for <file_change_detection.c>.
a2cf7d
+   Copyright (C) 2020 Free Software Foundation, Inc.
a2cf7d
+   This file is part of the GNU C Library.
a2cf7d
+
a2cf7d
+   The GNU C Library is free software; you can redistribute it and/or
a2cf7d
+   modify it under the terms of the GNU Lesser General Public
a2cf7d
+   License as published by the Free Software Foundation; either
a2cf7d
+   version 2.1 of the License, or (at your option) any later version.
a2cf7d
+
a2cf7d
+   The GNU C Library is distributed in the hope that it will be useful,
a2cf7d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
a2cf7d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
a2cf7d
+   Lesser General Public License for more details.
a2cf7d
+
a2cf7d
+   You should have received a copy of the GNU Lesser General Public
a2cf7d
+   License along with the GNU C Library; if not, see
a2cf7d
+   <https://www.gnu.org/licenses/>.  */
a2cf7d
+
a2cf7d
+/* The header uses the internal __fileno symbol, which is not
a2cf7d
+   available outside of libc (even to internal tests).  */
a2cf7d
+#define __fileno(fp) fileno (fp)
a2cf7d
+
a2cf7d
+#include <file_change_detection.h>
a2cf7d
+
a2cf7d
+#include <array_length.h>
a2cf7d
+#include <stdlib.h>
a2cf7d
+#include <support/check.h>
a2cf7d
+#include <support/support.h>
a2cf7d
+#include <support/temp_file.h>
a2cf7d
+#include <support/test-driver.h>
a2cf7d
+#include <support/xstdio.h>
a2cf7d
+#include <support/xunistd.h>
a2cf7d
+#include <unistd.h>
a2cf7d
+
a2cf7d
+static void
a2cf7d
+all_same (struct file_change_detection *array, size_t length)
a2cf7d
+{
a2cf7d
+  for (size_t i = 0; i < length; ++i)
a2cf7d
+    for (size_t j = 0; j < length; ++j)
a2cf7d
+      {
a2cf7d
+        if (test_verbose > 0)
a2cf7d
+          printf ("info: comparing %zu and %zu\n", i, j);
a2cf7d
+        TEST_VERIFY (file_is_unchanged (array + i, array + j));
a2cf7d
+      }
a2cf7d
+}
a2cf7d
+
a2cf7d
+static void
a2cf7d
+all_different (struct file_change_detection *array, size_t length)
a2cf7d
+{
a2cf7d
+  for (size_t i = 0; i < length; ++i)
a2cf7d
+    for (size_t j = 0; j < length; ++j)
a2cf7d
+      {
a2cf7d
+        if (i == j)
a2cf7d
+          continue;
a2cf7d
+        if (test_verbose > 0)
a2cf7d
+          printf ("info: comparing %zu and %zu\n", i, j);
a2cf7d
+        TEST_VERIFY (!file_is_unchanged (array + i, array + j));
a2cf7d
+      }
a2cf7d
+}
a2cf7d
+
a2cf7d
+static int
a2cf7d
+do_test (void)
a2cf7d
+{
a2cf7d
+  /* Use a temporary directory with various paths.  */
a2cf7d
+  char *tempdir = support_create_temp_directory ("tst-file_change_detection-");
a2cf7d
+
a2cf7d
+  char *path_dangling = xasprintf ("%s/dangling", tempdir);
a2cf7d
+  char *path_does_not_exist = xasprintf ("%s/does-not-exist", tempdir);
a2cf7d
+  char *path_empty1 = xasprintf ("%s/empty1", tempdir);
a2cf7d
+  char *path_empty2 = xasprintf ("%s/empty2", tempdir);
a2cf7d
+  char *path_fifo = xasprintf ("%s/fifo", tempdir);
a2cf7d
+  char *path_file1 = xasprintf ("%s/file1", tempdir);
a2cf7d
+  char *path_file2 = xasprintf ("%s/file2", tempdir);
a2cf7d
+  char *path_loop = xasprintf ("%s/loop", tempdir);
a2cf7d
+  char *path_to_empty1 = xasprintf ("%s/to-empty1", tempdir);
a2cf7d
+  char *path_to_file1 = xasprintf ("%s/to-file1", tempdir);
a2cf7d
+
a2cf7d
+  add_temp_file (path_dangling);
a2cf7d
+  add_temp_file (path_empty1);
a2cf7d
+  add_temp_file (path_empty2);
a2cf7d
+  add_temp_file (path_fifo);
a2cf7d
+  add_temp_file (path_file1);
a2cf7d
+  add_temp_file (path_file2);
a2cf7d
+  add_temp_file (path_loop);
a2cf7d
+  add_temp_file (path_to_empty1);
a2cf7d
+  add_temp_file (path_to_file1);
a2cf7d
+
a2cf7d
+  xsymlink ("target-does-not-exist", path_dangling);
a2cf7d
+  support_write_file_string (path_empty1, "");
a2cf7d
+  support_write_file_string (path_empty2, "");
a2cf7d
+  TEST_COMPARE (mknod (path_fifo, 0777 | S_IFIFO, 0), 0);
a2cf7d
+  support_write_file_string (path_file1, "line\n");
a2cf7d
+  support_write_file_string (path_file2, "line\n");
a2cf7d
+  xsymlink ("loop", path_loop);
a2cf7d
+  xsymlink ("empty1", path_to_empty1);
a2cf7d
+  xsymlink ("file1", path_to_file1);
a2cf7d
+
a2cf7d
+  FILE *fp_file1 = xfopen (path_file1, "r");
a2cf7d
+  FILE *fp_file2 = xfopen (path_file2, "r");
a2cf7d
+  FILE *fp_empty1 = xfopen (path_empty1, "r");
a2cf7d
+  FILE *fp_empty2 = xfopen (path_empty2, "r");
a2cf7d
+
a2cf7d
+  /* Test for the same (empty) files.  */
a2cf7d
+  {
a2cf7d
+    struct file_change_detection fcd[10];
a2cf7d
+    int i = 0;
a2cf7d
+    /* Two empty files always have the same contents.  */
a2cf7d
+    TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_empty1));
a2cf7d
+    TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_empty2));
a2cf7d
+    /* So does a missing file (which is treated as empty).  */
a2cf7d
+    TEST_VERIFY (file_change_detection_for_path (&fcd[i++],
a2cf7d
+                                                 path_does_not_exist));
a2cf7d
+    /* And a symbolic link loop.  */
a2cf7d
+    TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_loop));
a2cf7d
+    /* And a dangling symbolic link.  */
a2cf7d
+    TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_dangling));
a2cf7d
+    /* And a directory.  */
a2cf7d
+    TEST_VERIFY (file_change_detection_for_path (&fcd[i++], tempdir));
a2cf7d
+    /* And a symbolic link to an empty file.  */
a2cf7d
+    TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_to_empty1));
a2cf7d
+    /* Likewise for access the file via a FILE *.  */
a2cf7d
+    TEST_VERIFY (file_change_detection_for_fp (&fcd[i++], fp_empty1));
a2cf7d
+    TEST_VERIFY (file_change_detection_for_fp (&fcd[i++], fp_empty2));
a2cf7d
+    /* And a NULL FILE * (missing file).  */
a2cf7d
+    TEST_VERIFY (file_change_detection_for_fp (&fcd[i++], NULL));
a2cf7d
+    TEST_COMPARE (i, array_length (fcd));
a2cf7d
+
a2cf7d
+    all_same (fcd, array_length (fcd));
a2cf7d
+  }
a2cf7d
+
a2cf7d
+  /* Symbolic links are resolved.  */
a2cf7d
+  {
a2cf7d
+    struct file_change_detection fcd[3];
a2cf7d
+    int i = 0;
a2cf7d
+    TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_file1));
a2cf7d
+    TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_to_file1));
a2cf7d
+    TEST_VERIFY (file_change_detection_for_fp (&fcd[i++], fp_file1));
a2cf7d
+    TEST_COMPARE (i, array_length (fcd));
a2cf7d
+    all_same (fcd, array_length (fcd));
a2cf7d
+  }
a2cf7d
+
a2cf7d
+  /* Test for different files.  */
a2cf7d
+  {
a2cf7d
+    struct file_change_detection fcd[5];
a2cf7d
+    int i = 0;
a2cf7d
+    /* The other files are not empty.  */
a2cf7d
+    TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_empty1));
a2cf7d
+    /* These two files have the same contents, but have different file
a2cf7d
+       identity.  */
a2cf7d
+    TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_file1));
a2cf7d
+    TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_file2));
a2cf7d
+    /* FIFOs are always different, even with themselves.  */
a2cf7d
+    TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_fifo));
a2cf7d
+    TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_fifo));
a2cf7d
+    TEST_COMPARE (i, array_length (fcd));
a2cf7d
+    all_different (fcd, array_length (fcd));
a2cf7d
+
a2cf7d
+    /* Replacing the file with its symbolic link does not make a
a2cf7d
+       difference.  */
a2cf7d
+    TEST_VERIFY (file_change_detection_for_path (&fcd[1], path_to_file1));
a2cf7d
+    all_different (fcd, array_length (fcd));
a2cf7d
+  }
a2cf7d
+
a2cf7d
+  /* Wait for a file change.  Depending on file system time stamp
a2cf7d
+     resolution, this subtest blocks for a while.  */
a2cf7d
+  for (int use_stdio = 0; use_stdio < 2; ++use_stdio)
a2cf7d
+    {
a2cf7d
+      struct file_change_detection initial;
a2cf7d
+      TEST_VERIFY (file_change_detection_for_path (&initial, path_file1));
a2cf7d
+      while (true)
a2cf7d
+        {
a2cf7d
+          support_write_file_string (path_file1, "line\n");
a2cf7d
+          struct file_change_detection current;
a2cf7d
+          if (use_stdio)
a2cf7d
+            TEST_VERIFY (file_change_detection_for_fp (&current, fp_file1));
a2cf7d
+          else
a2cf7d
+            TEST_VERIFY (file_change_detection_for_path (&current, path_file1));
a2cf7d
+          if (!file_is_unchanged (&initial, &current))
a2cf7d
+            break;
a2cf7d
+          /* Wait for a bit to reduce system load.  */
a2cf7d
+          usleep (100 * 1000);
a2cf7d
+        }
a2cf7d
+    }
a2cf7d
+
a2cf7d
+  fclose (fp_empty1);
a2cf7d
+  fclose (fp_empty2);
a2cf7d
+  fclose (fp_file1);
a2cf7d
+  fclose (fp_file2);
a2cf7d
+
a2cf7d
+  free (path_dangling);
a2cf7d
+  free (path_does_not_exist);
a2cf7d
+  free (path_empty1);
a2cf7d
+  free (path_empty2);
a2cf7d
+  free (path_fifo);
a2cf7d
+  free (path_file1);
a2cf7d
+  free (path_file2);
a2cf7d
+  free (path_loop);
a2cf7d
+  free (path_to_empty1);
a2cf7d
+  free (path_to_file1);
a2cf7d
+
a2cf7d
+  free (tempdir);
a2cf7d
+
a2cf7d
+  return 0;
a2cf7d
+}
a2cf7d
+
a2cf7d
+#include <support/test-driver.c>