657ad0103cb7e4c751c271f3ca0bf1a906928ed8
[debian/amanda] / server-src / amcleanupdisk.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  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: amcleanupdisk.c,v 1.1.2.6.4.3.2.2 2002/11/05 01:59:23 martinea Exp $
28  */
29 #include "amanda.h"
30
31 #include "conffile.h"
32 #include "diskfile.h"
33 #include "clock.h"
34 #include "version.h"
35 #include "holding.h"
36 #include "infofile.h"
37 #include "server_util.h"
38
39 sl_t *holding_list;
40 char *datestamp;
41
42 /* local functions */
43 int main P((int argc, char **argv));
44 void check_holdingdisk P((char *diskdir, char *datestamp));
45 void check_disks P((void));
46
47 int main(main_argc, main_argv)
48 int main_argc;
49 char **main_argv;
50 {
51     struct passwd *pw;
52     char *dumpuser;
53     int fd;
54     disklist_t *diskqp;
55     char *conffile;
56     char *conf_diskfile;
57     char *conf_infofile;
58
59     for(fd = 3; fd < FD_SETSIZE; fd++) {
60         /*
61          * Make sure nobody spoofs us with a lot of extra open files
62          * that would cause an open we do to get a very high file
63          * descriptor, which in turn might be used as an index into
64          * an array (e.g. an fd_set).
65          */
66         close(fd);
67     }
68
69     safe_cd();
70
71     set_pname("amcleanupdisk");
72
73     if(main_argc != 2) {
74         error("Usage: amcleanupdisk%s <confdir>", versionsuffix());
75     }
76
77     config_name = main_argv[1];
78
79     config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
80     conffile = stralloc2(config_dir, CONFFILE_NAME);
81     if(read_conffile(conffile)) {
82         error("errors processing config file \"%s\"", conffile);
83     }
84     amfree(conffile);
85     conf_diskfile = getconf_str(CNF_DISKFILE);
86     if (*conf_diskfile == '/') {
87         conf_diskfile = stralloc(conf_diskfile);
88     } else {
89         conf_diskfile = stralloc2(config_dir, conf_diskfile);
90     }
91     if((diskqp = read_diskfile(conf_diskfile)) == NULL) {
92         error("could not load disklist %s", conf_diskfile);
93     }
94     amfree(conf_diskfile);
95     conf_infofile = getconf_str(CNF_INFOFILE);
96     if (*conf_infofile == '/') {
97         conf_infofile = stralloc(conf_infofile);
98     } else {
99         conf_infofile = stralloc2(config_dir, conf_infofile);
100     }
101     if(open_infofile(conf_infofile)) {
102         error("could not open info db \"%s\"", conf_infofile);
103     }
104     amfree(conf_infofile);
105
106     datestamp = construct_datestamp(NULL);
107
108     dumpuser = getconf_str(CNF_DUMPUSER);
109     if((pw = getpwnam(dumpuser)) == NULL) {
110         error("dumpuser %s not found in password file", dumpuser);
111     }
112     if(pw->pw_uid != getuid()) {
113         error("must run amcleanupdisk as user %s", dumpuser);
114     }
115
116     holding_list = pick_all_datestamp(1);
117
118     check_disks();
119
120     close_infofile();
121
122     free_sl(holding_list);
123     holding_list = NULL;
124     amfree(config_dir);
125     return 0;
126 }
127
128
129 void check_holdingdisk(diskdir, datestamp)
130 char *diskdir, *datestamp;
131 {
132     DIR *workdir;
133     struct dirent *entry;
134     char *dirname = NULL;
135     char *tmpname = NULL;
136     char *destname = NULL;
137     char *hostname = NULL;
138     char *diskname = NULL;
139     disk_t *dp;
140     filetype_t filetype;
141     info_t info;
142     int level;
143     int dl, l;
144
145     dirname = vstralloc(diskdir, "/", datestamp, NULL);
146     dl = strlen(dirname);
147
148     if((workdir = opendir(dirname)) == NULL) {
149         amfree(dirname);
150         return;
151     }
152
153     while((entry = readdir(workdir)) != NULL) {
154         if(is_dot_or_dotdot(entry->d_name)) {
155             continue;
156         }
157
158         if((l = strlen(entry->d_name)) < 7 ) {
159             continue;
160         }
161
162         if(strncmp(&entry->d_name[l-4],".tmp",4) != 0) {
163             continue;
164         }
165
166         tmpname = newvstralloc(tmpname,
167                                dirname, "/", entry->d_name,
168                                NULL);
169
170         destname = newstralloc(destname, tmpname);
171         destname[dl + 1 + l - 4] = '\0';
172
173         amfree(hostname);
174         amfree(diskname);
175         filetype = get_amanda_names(tmpname, &hostname, &diskname, &level);
176         if(filetype != F_DUMPFILE) {
177             continue;
178         }
179
180         dp = lookup_disk(hostname, diskname);
181
182         if (dp == NULL) {
183             continue;
184         }
185
186         if(level < 0 || level > 9) {
187             continue;
188         }
189
190         if(rename_tmp_holding(destname, 0)) {
191             get_info(dp->host->hostname, dp->name, &info);
192             info.command &= ~FORCE_BUMP;
193             info.command |= FORCE_NO_BUMP;
194             if(put_info(dp->host->hostname, dp->name, &info)) {
195                 error("could not put info record for %s:%s: %s",
196                       dp->host->hostname, dp->name, strerror(errno));
197             }
198         } else {
199             fprintf(stderr,"rename_tmp_holding(%s) failed\n", destname);
200         }
201     }
202     closedir(workdir);
203
204     /* try to zap the potentially empty working dir */
205     /* ignore any errors -- it either works or it doesn't */
206     (void) rmdir(dirname);
207
208     amfree(diskname);
209     amfree(hostname);
210     amfree(destname);
211     amfree(dirname);
212 }
213
214
215 void check_disks()
216 {
217     holdingdisk_t *hdisk;
218     sle_t *dir;
219
220     for(dir = holding_list->first; dir !=NULL; dir = dir->next) {
221         for(hdisk = getconf_holdingdisks(); hdisk != NULL; hdisk = hdisk->next)
222             check_holdingdisk(hdisk->diskdir, dir->name);
223     }
224 }