Imported Upstream version 3.3.3
[debian/amanda] / server-src / amindex.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
5  * All Rights Reserved.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and its
8  * documentation for any purpose is hereby granted without fee, provided that
9  * the above copyright notice appear in all copies and that both that
10  * copyright notice and this permission notice appear in supporting
11  * documentation, and that the name of U.M. not be used in advertising or
12  * publicity pertaining to distribution of the software without specific,
13  * written prior permission.  U.M. makes no representations about the
14  * suitability of this software for any purpose.  It is provided "as is"
15  * without express or implied warranty.
16  *
17  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
19  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Author: James da Silva, Systems Design and Analysis Group
25  *                         Computer Science Department
26  *                         University of Maryland at College Park
27  */
28 /*
29  * $Id: amindex.c,v 1.15 2006/05/25 01:47:19 johnfranks Exp $
30  *
31  * index control
32  */
33
34 #include "amanda.h"
35 #include "conffile.h"
36 #include "amindex.h"
37
38 char *
39 getindexfname(
40     char *      host,
41     char *      disk,
42     char *      date,
43     int         level)
44 {
45   char *conf_indexdir;
46   char *buf;
47   char level_str[NUM_STR_SIZE];
48   char datebuf[14 + 1];
49   char *dc = NULL;
50   char *pc;
51   int ch;
52
53   if (date != NULL) {
54     dc = date;
55     pc = datebuf;
56     while (pc < datebuf + SIZEOF(datebuf)) {
57       ch = *dc++;
58       *pc++ = (char)ch;
59       if (ch == '\0') {
60         break;
61       } else if (! isdigit (ch)) {
62         pc--;
63       }
64     }
65     datebuf[SIZEOF(datebuf)-1] = '\0';
66     dc = datebuf;
67
68     g_snprintf(level_str, SIZEOF(level_str), "%d", level);
69   }
70
71   host = sanitise_filename(host);
72   if (disk != NULL) {
73     disk = sanitise_filename(disk);
74   }
75
76   conf_indexdir = config_dir_relative(getconf_str(CNF_INDEXDIR));
77   /*
78    * Note: vstralloc() will stop at the first NULL, which might be
79    * "disk" or "dc" (datebuf) rather than the full file name.
80    */
81   buf = vstralloc(conf_indexdir, "/",
82                   host, "/",
83                   disk, "/",
84                   dc, "_",
85                   level_str, COMPRESS_SUFFIX,
86                   NULL);
87
88   amfree(conf_indexdir);
89   amfree(host);
90   amfree(disk);
91
92   return buf;
93 }
94
95 char *
96 getheaderfname(
97     char *      host,
98     char *      disk,
99     char *      date,
100     int         level)
101 {
102   char *conf_indexdir;
103   char *buf;
104   char level_str[NUM_STR_SIZE];
105   char datebuf[14 + 1];
106   char *dc = NULL;
107   char *pc;
108   int ch;
109
110   if (date != NULL) {
111     dc = date;
112     pc = datebuf;
113     while (pc < datebuf + sizeof(datebuf)) {
114       ch = *dc++;
115       *pc++ = (char)ch;
116       if (ch == '\0') {
117         break;
118       } else if (! isdigit (ch)) {
119         pc--;
120       }
121     }
122     datebuf[sizeof(datebuf)-1] = '\0';
123     dc = datebuf;
124
125     g_snprintf(level_str, sizeof(level_str), "%d", level);
126   }
127
128   host = sanitise_filename(host);
129   if (disk != NULL) {
130     disk = sanitise_filename(disk);
131   }
132
133   conf_indexdir = config_dir_relative(getconf_str(CNF_INDEXDIR));
134   /*
135    * Note: g_strjoin(NULL, ) will stop at the first NULL, which might be
136    * "disk" or "dc" (datebuf) rather than the full file name.
137    */
138   buf = g_strjoin(NULL, conf_indexdir, "/",
139                   host, "/",
140                   disk, "/",
141                   dc, "_",
142                   level_str, ".header",
143                   NULL);
144
145   amfree(conf_indexdir);
146   amfree(host);
147   amfree(disk);
148
149   return buf;
150 }
151
152 char *
153 getoldindexfname(
154     char *      host,
155     char *      disk,
156     char *      date,
157     int         level)
158 {
159   char *conf_indexdir;
160   char *buf;
161   char level_str[NUM_STR_SIZE];
162   char datebuf[14 + 1];
163   char *dc = NULL;
164   char *pc;
165   int ch;
166
167   if (date != NULL) {
168     dc = date;
169     pc = datebuf;
170     while (pc < datebuf + SIZEOF(datebuf)) {
171       ch = *dc++;
172       *pc++ = (char)ch;
173       if (ch == '\0') {
174         break;
175       } else if (! isdigit (ch)) {
176         pc--;
177       }
178     }
179     datebuf[SIZEOF(datebuf)-1] = '\0';
180     dc = datebuf;
181
182     g_snprintf(level_str, SIZEOF(level_str), "%d", level);
183   }
184
185   host = old_sanitise_filename(host);
186   if (disk != NULL) {
187     disk = old_sanitise_filename(disk);
188   }
189
190   conf_indexdir = config_dir_relative(getconf_str(CNF_INDEXDIR));
191   /*
192    * Note: vstralloc() will stop at the first NULL, which might be
193    * "disk" or "dc" (datebuf) rather than the full file name.
194    */
195   buf = vstralloc(conf_indexdir, "/",
196                   host, "/",
197                   disk, "/",
198                   dc, "_",
199                   level_str, COMPRESS_SUFFIX,
200                   NULL);
201
202   amfree(conf_indexdir);
203   amfree(host);
204   amfree(disk);
205
206   return buf;
207 }