1 /*-
2 * Copyright (C) 1990 Free Software Foundation, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * without restriction.
6 *
7 * This program is distributed in the hope that it will be useful, but WITHOUT
8 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9 * FITNESS FOR A PARTICULAR PURPOSE.
10 *
11 * backupfile.c -- make Emacs style backup file names
12 *
13 * David MacKenzie <djm@ai.mit.edu>. Some algorithms adapted from GNU Emacs.
14 *
15 * $OpenBSD: backupfile.c,v 1.20 2009/10/27 23:59:41 deraadt Exp $
16 */
17
18 #include <ctype.h>
19 #include <dirent.h>
20 #include <libgen.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "backupfile.h"
26
27
28 #define ISDIGIT(c) (isascii ((unsigned char)c) && isdigit ((unsigned char)c))
29
30 /* Which type of backup file names are generated. */
31 enum backup_type backup_type = none;
32
33 /*
34 * The extension added to file names to produce a simple (as opposed to
35 * numbered) backup file name.
36 */
37 const char *simple_backup_suffix = "~";
38
39 static char *concat(const char *, const char *);
40 static char *make_version_name(const char *, int);
41 static int max_backup_version(const char *, const char *);
42 static int version_number(const char *, const char *, size_t);
43 static int argmatch(const char *, const char **);
44 static void invalid_arg(const char *, const char *, int);
45
46 /*
47 * Return the name of the new backup file for file FILE, allocated with
48 * malloc. Return 0 if out of memory. FILE must not end with a '/' unless it
49 * is the root directory. Do not call this function if backup_type == none.
50 */
51 char *
find_backup_file_name(const char * file)52 find_backup_file_name(const char *file)
53 {
54 char *dir, *base_versions, *tmp_file;
55 int highest_backup;
56
57 if (backup_type == simple)
58 return concat(file, simple_backup_suffix);
59 tmp_file = strdup(file);
60 if (tmp_file == NULL)
61 return NULL;
62 base_versions = concat(basename(tmp_file), ".~");
63 free(tmp_file);
64 if (base_versions == NULL)
65 return NULL;
66 tmp_file = strdup(file);
67 if (tmp_file == NULL) {
68 free(base_versions);
69 return NULL;
70 }
71 dir = dirname(tmp_file);
72 if (dir == NULL) {
73 free(base_versions);
74 free(tmp_file);
75 return NULL;
76 }
77 highest_backup = max_backup_version(base_versions, dir);
78 free(base_versions);
79 free(tmp_file);
80 if (backup_type == numbered_existing && highest_backup == 0)
81 return concat(file, simple_backup_suffix);
82 return make_version_name(file, highest_backup + 1);
83 }
84
85 /*
86 * Return the number of the highest-numbered backup file for file FILE in
87 * directory DIR. If there are no numbered backups of FILE in DIR, or an
88 * error occurs reading DIR, return 0. FILE should already have ".~" appended
89 * to it.
90 */
91 static int
max_backup_version(const char * file,const char * dir)92 max_backup_version(const char *file, const char *dir)
93 {
94 DIR *dirp;
95 struct dirent *dp;
96 int highest_version, this_version;
97 size_t file_name_length;
98
99 dirp = opendir(dir);
100 if (dirp == NULL)
101 return 0;
102
103 highest_version = 0;
104 file_name_length = strlen(file);
105
106 while ((dp = readdir(dirp)) != NULL) {
107 if (dp->d_namlen <= file_name_length)
108 continue;
109
110 this_version = version_number(file, dp->d_name, file_name_length);
111 if (this_version > highest_version)
112 highest_version = this_version;
113 }
114 closedir(dirp);
115 return highest_version;
116 }
117
118 /*
119 * Return a string, allocated with malloc, containing "FILE.~VERSION~".
120 * Return 0 if out of memory.
121 */
122 static char *
make_version_name(const char * file,int version)123 make_version_name(const char *file, int version)
124 {
125 char *backup_name;
126
127 if (asprintf(&backup_name, "%s.~%d~", file, version) == -1)
128 return NULL;
129 return backup_name;
130 }
131
132 /*
133 * If BACKUP is a numbered backup of BASE, return its version number;
134 * otherwise return 0. BASE_LENGTH is the length of BASE. BASE should
135 * already have ".~" appended to it.
136 */
137 static int
version_number(const char * base,const char * backup,size_t base_length)138 version_number(const char *base, const char *backup, size_t base_length)
139 {
140 int version;
141 const char *p;
142
143 version = 0;
144 if (!strncmp(base, backup, base_length) && ISDIGIT(backup[base_length])) {
145 for (p = &backup[base_length]; ISDIGIT(*p); ++p)
146 version = version * 10 + *p - '0';
147 if (p[0] != '~' || p[1])
148 version = 0;
149 }
150 return version;
151 }
152
153 /*
154 * Return the newly-allocated concatenation of STR1 and STR2. If out of
155 * memory, return 0.
156 */
157 static char *
concat(const char * str1,const char * str2)158 concat(const char *str1, const char *str2)
159 {
160 char *newstr;
161
162 if (asprintf(&newstr, "%s%s", str1, str2) == -1)
163 return NULL;
164 return newstr;
165 }
166
167 /*
168 * If ARG is an unambiguous match for an element of the null-terminated array
169 * OPTLIST, return the index in OPTLIST of the matched element, else -1 if it
170 * does not match any element or -2 if it is ambiguous (is a prefix of more
171 * than one element).
172 */
173 static int
argmatch(const char * arg,const char ** optlist)174 argmatch(const char *arg, const char **optlist)
175 {
176 int i; /* Temporary index in OPTLIST. */
177 size_t arglen; /* Length of ARG. */
178 int matchind = -1; /* Index of first nonexact match. */
179 int ambiguous = 0; /* If nonzero, multiple nonexact match(es). */
180
181 arglen = strlen(arg);
182
183 /* Test all elements for either exact match or abbreviated matches. */
184 for (i = 0; optlist[i]; i++) {
185 if (!strncmp(optlist[i], arg, arglen)) {
186 if (strlen(optlist[i]) == arglen)
187 /* Exact match found. */
188 return i;
189 else if (matchind == -1)
190 /* First nonexact match found. */
191 matchind = i;
192 else
193 /* Second nonexact match found. */
194 ambiguous = 1;
195 }
196 }
197 if (ambiguous)
198 return -2;
199 else
200 return matchind;
201 }
202
203 /*
204 * Error reporting for argmatch. KIND is a description of the type of entity
205 * that was being matched. VALUE is the invalid value that was given. PROBLEM
206 * is the return value from argmatch.
207 */
208 static void
invalid_arg(const char * kind,const char * value,int problem)209 invalid_arg(const char *kind, const char *value, int problem)
210 {
211 fprintf(stderr, "patch: ");
212 if (problem == -1)
213 fprintf(stderr, "invalid");
214 else /* Assume -2. */
215 fprintf(stderr, "ambiguous");
216 fprintf(stderr, " %s `%s'\n", kind, value);
217 }
218
219 static const char *backup_args[] = {
220 "none", "never", "simple", "nil", "existing", "t", "numbered", 0
221 };
222
223 static enum backup_type backup_types[] = {
224 none, simple, simple, numbered_existing,
225 numbered_existing, numbered, numbered
226 };
227
228 /*
229 * Return the type of backup indicated by VERSION. Unique abbreviations are
230 * accepted.
231 */
232 enum backup_type
get_version(const char * version)233 get_version(const char *version)
234 {
235 int i;
236
237 if (version == NULL || *version == '\0')
238 return numbered_existing;
239 i = argmatch(version, backup_args);
240 if (i >= 0)
241 return backup_types[i];
242 invalid_arg("version control type", version, i);
243 exit(2);
244 }
245