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