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