(root)/
findutils-4.9.0/
find/
sharefile.c
       1  /* sharefile.c -- open files just once.
       2     Copyright (C) 2008-2022 Free Software Foundation, Inc.
       3  
       4     This program is free software: you can redistribute it and/or modify
       5     it under the terms of the GNU General Public License as published by
       6     the Free Software Foundation, either version 3 of the License, or
       7     (at your option) any later version.
       8  
       9     This program is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12     GNU General Public License for more details.
      13  
      14     You should have received a copy of the GNU General Public License
      15     along with this program.  If not, see <https://www.gnu.org/licenses/>.
      16  */
      17  
      18  /* config.h always comes first. */
      19  #include <config.h>
      20  
      21  /* system headers. */
      22  #include <assert.h>
      23  #include <errno.h>
      24  #include <stdlib.h>
      25  #include <string.h>
      26  #include <sys/stat.h>
      27  #include <sys/types.h>
      28  
      29  /* gnulib headers. */
      30  #include "cloexec.h"
      31  #include "hash.h"
      32  #include "stdio--.h"
      33  
      34  /* find headers. */
      35  #include "sharefile.h"
      36  #include "defs.h"
      37  
      38  
      39  enum
      40    {
      41      DefaultHashTableSize = 11
      42    };
      43  
      44  struct sharefile
      45  {
      46    char *mode;
      47    Hash_table *table;
      48  };
      49  
      50  
      51  /*
      52   * We cannot use the name to determine that two strings represent the
      53   * same file, since that test would be fooled by symbolic links.
      54   * Instead we use the device and inode number.
      55   *
      56   * However, we remember the name of each file that we opened.  This
      57   * allows us to issue a fatal error message when (flushing and)
      58   * closing a file fails.
      59   */
      60  struct SharefileEntry
      61  {
      62    dev_t device;
      63    ino_t inode;
      64    char *name; /* not the only name for this file; error messages only */
      65    FILE *fp;
      66  };
      67  
      68  
      69  static bool
      70  entry_comparator (const void *av, const void *bv)
      71  {
      72    const struct SharefileEntry *a=av, *b=bv;
      73    return (a->inode == b->inode) && (a->device == b->device);
      74  }
      75  
      76  static void
      77  entry_free (void *pv)
      78  {
      79    struct SharefileEntry *p = pv;
      80    if (p->fp)
      81      {
      82        if (0 != fclose (p->fp))
      83  	fatal_nontarget_file_error (errno, p->name);
      84      }
      85    free (p->name);
      86    free (p);
      87  }
      88  
      89  static size_t
      90  entry_hashfunc (const void *pv, size_t buckets)
      91  {
      92    const struct SharefileEntry *p = pv;
      93    return (p->device ^ p->inode) % buckets;
      94  }
      95  
      96  
      97  
      98  sharefile_handle
      99  sharefile_init (const char *mode)
     100  {
     101    struct Hash_tuning;
     102  
     103    struct sharefile *p = malloc (sizeof (struct sharefile));
     104    if (p)
     105      {
     106        p->mode = strdup (mode);
     107        if (p->mode)
     108  	{
     109  	  p->table = hash_initialize (DefaultHashTableSize, NULL,
     110  				      entry_hashfunc,
     111  				      entry_comparator,
     112  				      entry_free);
     113  	  if (p->table)
     114  	    {
     115  	      return p;
     116  	    }
     117  	  else
     118  	    {
     119  	      free (p->mode);
     120  	      free (p);
     121  	    }
     122  	}
     123        else
     124  	{
     125  	  free (p);
     126  	}
     127      }
     128    return NULL;
     129  }
     130  
     131  void
     132  sharefile_destroy (sharefile_handle pv)
     133  {
     134    struct sharefile *p = pv;
     135    free (p->mode);
     136    hash_free (p->table);
     137  }
     138  
     139  
     140  FILE *
     141  sharefile_fopen (sharefile_handle h, const char *filename)
     142  {
     143    struct sharefile *p = h;
     144    struct SharefileEntry *new_entry;
     145  
     146    new_entry = malloc (sizeof (struct SharefileEntry));
     147    if (!new_entry)
     148      return NULL;
     149  
     150    new_entry->name = strdup (filename);
     151    if (NULL == new_entry->name)
     152      {
     153        free (new_entry);
     154        return NULL;
     155      }
     156  
     157    if (NULL == (new_entry->fp = fopen (filename, p->mode)))
     158      {
     159        entry_free (new_entry);
     160        return NULL;
     161      }
     162    else
     163      {
     164        struct stat st;
     165        const int fd = fileno (new_entry->fp);
     166        assert (fd >= 0);
     167  
     168        set_cloexec_flag (fd, true);
     169        if (fstat (fd, &st) < 0)
     170          {
     171  	  entry_free (new_entry);
     172            return NULL;
     173          }
     174        else
     175          {
     176  	  void *existing;
     177  
     178            new_entry->device = st.st_dev;
     179            new_entry->inode = st.st_ino;
     180  
     181            existing = hash_lookup (p->table, new_entry);
     182            if (existing)	    /* We have previously opened that file. */
     183  	    {
     184  	      entry_free (new_entry); /* don't need new_entry. */
     185  	      return ((const struct SharefileEntry*)existing)->fp;
     186  	    }
     187            else /* We didn't open it already */
     188  	    {
     189  	      if (hash_insert (p->table, new_entry))
     190  		{
     191  		  return new_entry->fp;
     192  		}
     193  	      else			/* failed to insert in hashtable. */
     194  		{
     195  		  const int save_errno = errno;
     196  		  entry_free (new_entry);
     197  		  errno = save_errno;
     198  		  return NULL;
     199  		}
     200  	    }
     201          }
     202      }
     203  }