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