summaryrefslogtreecommitdiff
path: root/lib/backup-find.c
blob: 5fd1d094f5c2c61ebaed92a8f5b00b33e0083c70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* backupfile.c -- make Emacs style backup file names

   Copyright 2017-2021 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

#include <config.h>

#include "backup-internal.h"

#include "argmatch.h"
#include "xalloc.h"

#include <stdlib.h>

/* Relative to DIR_FD, return the name of a backup file for the
   existing file FILE, allocated with malloc.  Report an error and
   exit if out of memory.  Do not call this function if
   backup_type == no_backups.  */

char *
find_backup_file_name (int dir_fd, char const *file,
                       enum backup_type backup_type)
{
  char *result = backupfile_internal (dir_fd, file, backup_type, false);
  if (!result)
    xalloc_die ();
  return result;
}

static char const *const backup_args[] =
{
  /* In a series of synonyms, present the most meaningful first, so
     that argmatch_valid be more readable. */
  "none", "off",
  "simple", "never",
  "existing", "nil",
  "numbered", "t",
  NULL
};

static const enum backup_type backup_types[] =
{
  no_backups, no_backups,
  simple_backups, simple_backups,
  numbered_existing_backups, numbered_existing_backups,
  numbered_backups, numbered_backups
};

/* Ensure that these two vectors have the same number of elements,
   not counting the final NULL in the first one.  */
ARGMATCH_VERIFY (backup_args, backup_types);

/* Return the type of backup specified by VERSION.
   If VERSION is NULL or the empty string, return numbered_existing_backups.
   If VERSION is invalid or ambiguous, fail with a diagnostic appropriate
   for the specified CONTEXT.  Unambiguous abbreviations are accepted.  */

enum backup_type
get_version (char const *context, char const *version)
{
  if (version == 0 || *version == 0)
    return numbered_existing_backups;
  else
    return XARGMATCH (context, version, backup_args, backup_types);
}


/* Return the type of backup specified by VERSION.
   If VERSION is NULL, use the value of the envvar VERSION_CONTROL.
   If the specified string is invalid or ambiguous, fail with a diagnostic
   appropriate for the specified CONTEXT.
   Unambiguous abbreviations are accepted.  */

enum backup_type
xget_version (char const *context, char const *version)
{
  if (version && *version)
    return get_version (context, version);
  else
    return get_version ("$VERSION_CONTROL", getenv ("VERSION_CONTROL"));
}