(root)/
tar-1.35/
gnu/
backup-find.c
       1  /* backupfile.c -- make Emacs style backup file names
       2  
       3     Copyright 2017-2023 Free Software Foundation, Inc.
       4  
       5     This program is free software: you can redistribute it and/or modify
       6     it under the terms of the GNU General Public License as published by
       7     the Free Software Foundation, either version 3 of the License, or
       8     (at your option) any later version.
       9  
      10     This program is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13     GNU General Public License for more details.
      14  
      15     You should have received a copy of the GNU General Public License
      16     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <config.h>
      19  
      20  #include "backup-internal.h"
      21  
      22  #include "argmatch.h"
      23  #include "xalloc.h"
      24  
      25  #include <stdlib.h>
      26  
      27  /* Relative to DIR_FD, return the name of a backup file for the
      28     existing file FILE, allocated with malloc.  Report an error and
      29     exit if out of memory.  Do not call this function if
      30     backup_type == no_backups.  */
      31  
      32  char *
      33  find_backup_file_name (int dir_fd, char const *file,
      34                         enum backup_type backup_type)
      35  {
      36    char *result = backupfile_internal (dir_fd, file, backup_type, false);
      37    if (!result)
      38      xalloc_die ();
      39    return result;
      40  }
      41  
      42  static char const *const backup_args[] =
      43  {
      44    /* In a series of synonyms, present the most meaningful first, so
      45       that argmatch_valid be more readable. */
      46    "none", "off",
      47    "simple", "never",
      48    "existing", "nil",
      49    "numbered", "t",
      50    NULL
      51  };
      52  
      53  static const enum backup_type backup_types[] =
      54  {
      55    no_backups, no_backups,
      56    simple_backups, simple_backups,
      57    numbered_existing_backups, numbered_existing_backups,
      58    numbered_backups, numbered_backups
      59  };
      60  
      61  /* Ensure that these two vectors have the same number of elements,
      62     not counting the final NULL in the first one.  */
      63  ARGMATCH_VERIFY (backup_args, backup_types);
      64  
      65  /* Return the type of backup specified by VERSION.
      66     If VERSION is NULL or the empty string, return numbered_existing_backups.
      67     If VERSION is invalid or ambiguous, fail with a diagnostic appropriate
      68     for the specified CONTEXT.  Unambiguous abbreviations are accepted.  */
      69  
      70  enum backup_type
      71  get_version (char const *context, char const *version)
      72  {
      73    if (version == 0 || *version == 0)
      74      return numbered_existing_backups;
      75    else
      76      return XARGMATCH (context, version, backup_args, backup_types);
      77  }
      78  
      79  
      80  /* Return the type of backup specified by VERSION.
      81     If VERSION is NULL, use the value of the envvar VERSION_CONTROL.
      82     If the specified string is invalid or ambiguous, fail with a diagnostic
      83     appropriate for the specified CONTEXT.
      84     Unambiguous abbreviations are accepted.  */
      85  
      86  enum backup_type
      87  xget_version (char const *context, char const *version)
      88  {
      89    if (version && *version)
      90      return get_version (context, version);
      91    else
      92      return get_version ("$VERSION_CONTROL", getenv ("VERSION_CONTROL"));
      93  }