summaryrefslogtreecommitdiffabout
path: root/src/backup.c
Unidiff
Diffstat (limited to 'src/backup.c') (more/less context) (show whitespace changes)
-rw-r--r--src/backup.c165
1 files changed, 165 insertions, 0 deletions
diff --git a/src/backup.c b/src/backup.c
new file mode 100644
index 0000000..312375d
--- a/dev/null
+++ b/src/backup.c
@@ -0,0 +1,165 @@
1/* wydawca - automatic release submission daemon
2 Copyright (C) 2011 Sergey Poznyakoff
3
4 Wydawca is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 3 of the License, or (at your
7 option) any later version.
8
9 Wydawca is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with wydawca. If not, see <http://www.gnu.org/licenses/>. */
16
17#include "wydawca.h"
18
19char const *simple_backup_suffix = "~";
20
21static const char *
22split_filename (char const *file, char **pdir)
23{
24 const char *p = strrchr (file, '/');
25
26 if (!p)
27 {
28 *pdir = grecs_strdup (".");
29 p = file;
30 }
31 else
32 {
33 size_t len = p - file;
34 char *dir = grecs_malloc (len + 1);
35 memcpy (dir, file, len);
36 dir[len] = 0;
37 *pdir = dir;
38 p++;
39 }
40 return p;
41}
42
43#define MINSUFSIZE 8
44#define ISDIGIT(c) ('0' <= (c) && (c) <= '9')
45
46static char *
47get_backup_suffix (char const *file, enum backup_type type)
48{
49 char *dirname;
50 const char *basename;
51 size_t baselen;
52 DIR *dir;
53 struct dirent *ent;
54 char *lastsuf = NULL;
55 size_t lastsuflen = 0;
56 size_t lastsufsize = 0;
57 int carry;
58 char *newsuf;
59 char *q;
60
61 if (type == simple_backups)
62 return grecs_strdup (simple_backup_suffix);
63
64 basename = split_filename (file, &dirname);
65 baselen = strlen (basename);
66 dir = opendir (dirname);
67 if (!dir)
68 {
69 int ec = errno;
70 free (dirname);
71 errno = ec;
72 return NULL;
73 }
74
75 while ((ent = readdir (dir)))
76 {
77 size_t len = strlen (ent->d_name);
78 const char *p;
79 size_t suflen;
80
81 if (len < baselen + 4 || memcmp (ent->d_name, basename, baselen))
82 continue;
83 p = ent->d_name + baselen;
84 suflen = len - baselen;
85 if (p[0] == '.' && p[1] == '~' && p[suflen-1] == '~' &&
86 (suflen > lastsuflen
87 || (suflen == lastsuflen &&
88 memcmp (p, lastsuf, lastsuflen) > 0)))
89 {
90 carry = 1;
91 for (q = (char*) p + suflen - 2; q > p + 1 && ISDIGIT (*q); q--)
92 if (*q != '9')
93 carry = 0;
94 q++;
95 if (!ISDIGIT (*q))
96 continue;
97
98 if (suflen > lastsufsize)
99 {
100 lastsufsize = suflen;
101 if (!lastsuf)
102 {
103 if (lastsufsize < MINSUFSIZE)
104 lastsufsize = MINSUFSIZE;
105 lastsuf = grecs_malloc (lastsufsize);
106 }
107 else
108 lastsuf = grecs_realloc (lastsuf, lastsufsize);
109 }
110 memcpy (lastsuf, p, suflen);
111 lastsuflen = suflen;
112 }
113 }
114 closedir (dir);
115 free (dirname);
116
117 if (lastsuf)
118 {
119 size_t newsuflen;
120
121 newsuflen = lastsuflen + carry;
122 newsuf = grecs_malloc (newsuflen + 1);
123 newsuf[0] = '.';
124 newsuf[1] = '~';
125 newsuf[2] = '0';
126 memcpy (newsuf + 2 + carry, lastsuf + 2, lastsuflen - 3);
127 newsuf[newsuflen-1] = '~';
128 newsuf[newsuflen] = 0;
129
130 for (q = newsuf + newsuflen - 2; *q == '9'; q--)
131 *q = '0';
132 ++*q;
133 free (lastsuf);
134 }
135 else if (type == numbered_existing_backups)
136 newsuf = grecs_strdup (simple_backup_suffix);
137 else
138 newsuf = grecs_strdup (".~1~");
139 return newsuf;
140}
141
142char *
143find_backup_file_name (char const *file, enum backup_type type)
144{
145 size_t flen;
146 char *suffix;
147 char *newname;
148
149 if (type == no_backups)
150 {
151 errno = 0;
152 return NULL;
153 }
154
155 suffix = get_backup_suffix (file, type);
156 if (!suffix)
157 return NULL;
158 flen = strlen (file);
159 newname = grecs_malloc (flen + strlen (suffix) + 1);
160 memcpy (newname, file, flen);
161 strcpy (newname + flen, suffix);
162 free (suffix);
163 /* FIXME: Check newname length */
164 return newname;
165}

Return to:

Send suggestions and report system problems to the System administrator.