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