27d33a701f4331406d38a7270e25931d6e7634fe
[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.2.1 2005/09/20 21:31:52 jrjackson 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     disklist_t *diskqp;
54     char *conffile;
55     char *conf_diskfile;
56     char *conf_infofile;
57
58     safe_fd(-1, 0);
59     safe_cd();
60
61     set_pname("amcleanupdisk");
62
63     if(main_argc != 2) {
64         error("Usage: amcleanupdisk%s <confdir>", versionsuffix());
65     }
66
67     config_name = main_argv[1];
68
69     config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
70     conffile = stralloc2(config_dir, CONFFILE_NAME);
71     if(read_conffile(conffile)) {
72         error("errors processing config file \"%s\"", conffile);
73     }
74     amfree(conffile);
75     conf_diskfile = getconf_str(CNF_DISKFILE);
76     if (*conf_diskfile == '/') {
77         conf_diskfile = stralloc(conf_diskfile);
78     } else {
79         conf_diskfile = stralloc2(config_dir, conf_diskfile);
80     }
81     if((diskqp = read_diskfile(conf_diskfile)) == NULL) {
82         error("could not load disklist %s", conf_diskfile);
83     }
84     amfree(conf_diskfile);
85     conf_infofile = getconf_str(CNF_INFOFILE);
86     if (*conf_infofile == '/') {
87         conf_infofile = stralloc(conf_infofile);
88     } else {
89         conf_infofile = stralloc2(config_dir, conf_infofile);
90     }
91     if(open_infofile(conf_infofile)) {
92         error("could not open info db \"%s\"", conf_infofile);
93     }
94     amfree(conf_infofile);
95
96     datestamp = construct_datestamp(NULL);
97
98     dumpuser = getconf_str(CNF_DUMPUSER);
99     if((pw = getpwnam(dumpuser)) == NULL) {
100         error("dumpuser %s not found in password file", dumpuser);
101     }
102     if(pw->pw_uid != getuid()) {
103         error("must run amcleanupdisk as user %s", dumpuser);
104     }
105
106     holding_list = pick_all_datestamp(1);
107
108     check_disks();
109
110     close_infofile();
111
112     free_sl(holding_list);
113     holding_list = NULL;
114     amfree(config_dir);
115     return 0;
116 }
117
118
119 void check_holdingdisk(diskdir, datestamp)
120 char *diskdir, *datestamp;
121 {
122     DIR *workdir;
123     struct dirent *entry;
124     char *dirname = NULL;
125     char *tmpname = NULL;
126     char *destname = NULL;
127     char *hostname = NULL;
128     char *diskname = NULL;
129     disk_t *dp;
130     filetype_t filetype;
131     info_t info;
132     int level;
133     int dl, l;
134
135     dirname = vstralloc(diskdir, "/", datestamp, NULL);
136     dl = strlen(dirname);
137
138     if((workdir = opendir(dirname)) == NULL) {
139         amfree(dirname);
140         return;
141     }
142
143     while((entry = readdir(workdir)) != NULL) {
144         if(is_dot_or_dotdot(entry->d_name)) {
145             continue;
146         }
147
148         if((l = strlen(entry->d_name)) < 7 ) {
149             continue;
150         }
151
152         if(strncmp(&entry->d_name[l-4],".tmp",4) != 0) {
153             continue;
154         }
155
156         tmpname = newvstralloc(tmpname,
157                                dirname, "/", entry->d_name,
158                                NULL);
159
160         destname = newstralloc(destname, tmpname);
161         destname[dl + 1 + l - 4] = '\0';
162
163         amfree(hostname);
164         amfree(diskname);
165         filetype = get_amanda_names(tmpname, &hostname, &diskname, &level);
166         if(filetype != F_DUMPFILE) {
167             continue;
168         }
169
170         dp = lookup_disk(hostname, diskname);
171
172         if (dp == NULL) {
173             continue;
174         }
175
176         if(level < 0 || level > 9) {
177             continue;
178         }
179
180         if(rename_tmp_holding(destname, 0)) {
181             get_info(dp->host->hostname, dp->name, &info);
182             info.command &= ~FORCE_BUMP;
183             info.command |= FORCE_NO_BUMP;
184             if(put_info(dp->host->hostname, dp->name, &info)) {
185                 error("could not put info record for %s:%s: %s",
186                       dp->host->hostname, dp->name, strerror(errno));
187             }
188         } else {
189             fprintf(stderr,"rename_tmp_holding(%s) failed\n", destname);
190         }
191     }
192     closedir(workdir);
193
194     /* try to zap the potentially empty working dir */
195     /* ignore any errors -- it either works or it doesn't */
196     (void) rmdir(dirname);
197
198     amfree(diskname);
199     amfree(hostname);
200     amfree(destname);
201     amfree(dirname);
202 }
203
204
205 void check_disks()
206 {
207     holdingdisk_t *hdisk;
208     sle_t *dir;
209
210     for(dir = holding_list->first; dir !=NULL; dir = dir->next) {
211         for(hdisk = getconf_holdingdisks(); hdisk != NULL; hdisk = hdisk->next)
212             check_holdingdisk(hdisk->diskdir, dir->name);
213     }
214 }