2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
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.
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.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: holding.c,v 1.17.2.12.4.3.2.10 2003/06/14 13:47:05 martinea Exp $
29 * Functions to access holding disk
35 #include "fileheader.h"
39 static sl_t *scan_holdingdisk P((sl_t *holding_list, char *diskdir, int verbose));
46 if(stat(fname, &statbuf) == -1) return 0;
48 return (statbuf.st_mode & S_IFDIR) == S_IFDIR;
51 int is_emptyfile(fname)
56 if(stat(fname, &statbuf) == -1) return 0;
58 return (statbuf.st_mode & S_IFDIR) != S_IFDIR && statbuf.st_size == 0;
63 /* sanity check on datestamp of the form YYYYMMDD */
66 int ch, num, date, year, month;
68 /* must be 8 digits */
69 for(cp = fname; (ch = *cp) != '\0'; cp++) {
74 if(ch != '\0' || cp-fname != 8) {
78 /* sanity check year, month, and day */
82 month = (num / 100) % 100;
84 if(year<1990 || year>2100 || month<1 || month>12 || date<1 || date>31)
87 /* yes, we passed all the checks */
100 if((dir = opendir(fname)) == NULL)
104 while(!gotentry && (entry = readdir(dir)) != NULL) {
105 gotentry = !is_dot_or_dotdot(entry->d_name);
113 sl_t *scan_holdingdisk(holding_list, diskdir, verbose)
119 struct dirent *workdir;
120 char *entryname = NULL;
122 if((topdir = opendir(diskdir)) == NULL) {
123 if(verbose && errno != ENOENT)
124 printf("Warning: could not open holding dir %s: %s\n",
125 diskdir, strerror(errno));
129 /* find all directories of the right format */
132 printf("Scanning %s...\n", diskdir);
133 while((workdir = readdir(topdir)) != NULL) {
134 if(is_dot_or_dotdot(workdir->d_name)
135 || strcmp(workdir->d_name, "lost+found") == 0) {
138 entryname = newvstralloc(entryname,
139 diskdir, "/", workdir->d_name, NULL);
141 printf(" %s: ", workdir->d_name);
143 if(!is_dir(entryname)) {
145 puts("skipping cruft file, perhaps you should delete it.");
146 } else if(!is_datestr(workdir->d_name)) {
148 puts("skipping cruft directory, perhaps you should delete it.");
150 holding_list = insert_sort_sl(holding_list, workdir->d_name);
152 puts("found Amanda directory.");
161 sl_t *scan_holdingdir(holding_list, holdp, datestamp)
163 holdingdisk_t *holdp;
167 struct dirent *entry;
168 char *dirname = NULL;
169 char *destname = NULL;
173 dirname = vstralloc(holdp->diskdir, "/", datestamp, NULL);
174 if((workdir = opendir(dirname)) == NULL) {
176 log_add(L_INFO, "%s: could not open dir: %s",
177 dirname, strerror(errno));
182 while((entry = readdir(workdir)) != NULL) {
183 if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
186 if(is_emptyfile(entry->d_name))
189 destname = newvstralloc(destname,
190 dirname, "/", entry->d_name,
192 get_dumpfile(destname, &file);
193 if( file.type != F_DUMPFILE) {
194 if( file.type != F_CONT_DUMPFILE )
195 log_add(L_INFO, "%s: ignoring cruft file.", entry->d_name);
199 dp = lookup_disk(file.name, file.disk);
202 log_add(L_INFO, "%s: disk %s:%s not in database, skipping it.",
203 entry->d_name, file.name, file.disk);
207 if(file.dumplevel < 0 || file.dumplevel > 9) {
208 log_add(L_INFO, "%s: ignoring file with bogus dump level %d.",
209 entry->d_name, file.dumplevel);
213 holding_list = append_sl(holding_list, destname);
222 sl_t *get_flush(dateargs, datestamp, amflush, verbose)
224 char *datestamp; /* don't do this date */
225 int amflush, verbose;
230 sle_t *date, *next_date;
231 holdingdisk_t *hdisk;
232 char current_dir[1000];
234 getcwd(current_dir, 999);
236 holding_list = new_sl();
241 date_list = pick_all_datestamp(verbose);
242 for(date = date_list->first; date != NULL;) {
243 next_date = date->next;
245 for(datearg=dateargs->first; datearg != NULL && ok==0;
246 datearg = datearg->next) {
247 ok = match_datestamp(datearg->name, date->name);
249 if(ok == 0) { /* remove dir */
250 remove_sl(date_list, date);
256 date_list = pick_datestamp(verbose);
259 date_list = pick_all_datestamp(verbose);
262 for(date = date_list->first; date !=NULL; date = date->next) {
263 if(!datestamp || strcmp(datestamp,date->name) != 0) {
264 for(hdisk = getconf_holdingdisks(); hdisk != NULL;
265 hdisk = hdisk->next) {
266 holding_list = scan_holdingdir(holding_list, hdisk, date->name);
274 return(holding_list);
278 sl_t *pick_all_datestamp(verbose)
281 sl_t *holding_list = NULL;
282 holdingdisk_t *hdisk;
284 holding_list = new_sl();
285 for(hdisk = getconf_holdingdisks(); hdisk != NULL; hdisk = hdisk->next)
286 holding_list = scan_holdingdisk(holding_list, hdisk->diskdir, verbose);
292 sl_t *pick_datestamp(verbose)
296 sl_t *r_holding_list = NULL;
298 char **directories = NULL;
303 char max_char = '\0', chupper = '\0';
305 holding_list = pick_all_datestamp(verbose);
307 if(holding_list->nb_element == 0) {
310 else if(holding_list->nb_element == 1 || !verbose) {
314 directories = alloc((holding_list->nb_element) * sizeof(char *));
315 for(dir = holding_list->first, i=0; dir != NULL; dir = dir->next,i++) {
316 directories[i] = dir->name;
320 puts("\nMultiple Amanda directories, please pick one by letter:");
321 for(dir = holding_list->first, max_char = 'A';
322 dir != NULL && max_char <= 'Z';
323 dir = dir->next, max_char++) {
324 printf(" %c. %s\n", max_char, dir->name);
327 printf("Select directories to flush [A..%c]: [ALL] ", max_char);
328 fflush(stdout); fflush(stderr);
330 if ((answer = agets(stdin)) == NULL) {
335 while ((ch = *a++) != '\0' && isspace(ch)) {}
336 if(ch == '\0' || strncasecmp(a, "ALL", 3) == 0) {
340 if (isspace(ch) || ch == ',') {
343 chupper = toupper(ch);
344 if (chupper < 'A' || chupper > max_char) {
345 free_sl(r_holding_list);
346 r_holding_list = NULL;
349 r_holding_list = append_sl(r_holding_list,
350 directories[chupper - 'A']);
351 } while ((ch = *a++) != '\0');
352 if (r_holding_list && ch == '\0') {
353 free_sl(holding_list);
354 holding_list = r_holding_list;
365 filetype_t get_amanda_names(fname, hostname, diskname, level)
366 char *fname, **hostname, **diskname;
370 char buffer[DISK_BLOCK_BYTES];
372 *hostname = *diskname = NULL;
374 if((fd = open(fname, O_RDONLY)) == -1)
377 if(fullread(fd, buffer, sizeof(buffer)) != sizeof(buffer)) {
383 parse_file_header(buffer,&file,sizeof(buffer));
384 if(file.type != F_DUMPFILE && file.type != F_CONT_DUMPFILE) {
387 *hostname = stralloc(file.name);
388 *diskname = stralloc(file.disk);
389 *level = file.dumplevel;
395 void get_dumpfile(fname, file)
399 char buffer[DISK_BLOCK_BYTES];
403 file->type = F_UNKNOWN;
404 if((fd = open(fname, O_RDONLY)) == -1)
407 if(fullread(fd, buffer, sizeof(buffer)) != sizeof(buffer)) {
413 parse_file_header(buffer,file,sizeof(buffer));
418 long size_holding_files(holding_file)
423 char buffer[DISK_BLOCK_BYTES];
429 filename = stralloc(holding_file);
430 while(filename != NULL && filename[0] != '\0') {
431 if((fd = open(filename,O_RDONLY)) == -1) {
432 fprintf(stderr,"size_holding_files: open of %s failed: %s\n",filename,strerror(errno));
436 buflen = fullread(fd, buffer, sizeof(buffer));
437 parse_file_header(buffer, &file, buflen);
439 if(stat(filename, &finfo) == -1) {
440 printf("stat %s: %s\n", filename, strerror(errno));
443 size += (finfo.st_size+1023)/1024;
444 filename = newstralloc(filename, file.cont_filename);
451 int unlink_holding_files( holding_file )
456 char buffer[DISK_BLOCK_BYTES];
460 filename = stralloc(holding_file);
461 while(filename != NULL && filename[0] != '\0') {
462 if((fd = open(filename,O_RDONLY)) == -1) {
463 fprintf(stderr,"unlink_holding_files: open of %s failed: %s\n",filename,strerror(errno));
467 buflen = fullread(fd, buffer, sizeof(buffer));
468 parse_file_header(buffer, &file, buflen);
471 filename = newstralloc(filename,file.cont_filename);
478 int rename_tmp_holding( holding_file, complete )
484 char buffer[DISK_BLOCK_BYTES];
487 char *filename_tmp = NULL;
489 filename = stralloc(holding_file);
490 while(filename != NULL && filename[0] != '\0') {
491 filename_tmp = newvstralloc(filename_tmp, filename, ".tmp", NULL);
492 if((fd = open(filename_tmp,O_RDONLY)) == -1) {
493 fprintf(stderr,"rename_tmp_holding: open of %s failed: %s\n",filename_tmp,strerror(errno));
495 amfree(filename_tmp);
498 buflen = fullread(fd, buffer, sizeof(buffer));
500 fprintf(stderr,"rename_tmp_holding: %s: empty file?\n", filename_tmp);
502 amfree(filename_tmp);
506 parse_file_header(buffer, &file, buflen);
509 if((fd = open(filename_tmp,O_RDWR)) == -1) {
510 fprintf(stderr, "rename_tmp_holdingX: open of %s failed: %s\n",
511 filename_tmp, strerror(errno));
513 amfree(filename_tmp);
518 build_header(buffer, &file, sizeof(buffer));
519 fullwrite(fd, buffer, sizeof(buffer));
522 if(rename(filename_tmp, filename) != 0) {
524 "rename_tmp_holding(): could not rename \"%s\" to \"%s\": %s",
525 filename_tmp, filename, strerror(errno));
527 filename = newstralloc(filename, file.cont_filename);
530 amfree(filename_tmp);
535 void cleanup_holdingdisk(diskdir, verbose)
540 struct dirent *workdir;
542 if((topdir = opendir(diskdir)) == NULL) {
543 if(verbose && errno != ENOENT)
544 printf("Warning: could not open holding dir %s: %s\n",
545 diskdir, strerror(errno));
549 /* find all directories of the right format */
552 printf("Scanning %s...\n", diskdir);
554 while((workdir = readdir(topdir)) != NULL) {
555 if(is_dot_or_dotdot(workdir->d_name)
556 || strcmp(workdir->d_name, "lost+found") == 0) {
560 printf(" %s: ", workdir->d_name);
561 if(!is_dir(workdir->d_name)) {
563 puts("skipping cruft file, perhaps you should delete it.");
565 else if(!is_datestr(workdir->d_name)) {
567 puts("skipping cruft directory, perhaps you should delete it.");
569 else if(rmdir(workdir->d_name) == 0) {
571 puts("deleted empty Amanda directory.");
578 int mkholdingdir(diskdir)
581 struct stat stat_hdp;
584 if (mkpdir(diskdir, 0770, (uid_t)-1, (gid_t)-1) != 0 && errno != EEXIST) {
585 log_add(L_WARNING, "WARNING: could not create parents of %s: %s",
586 diskdir, strerror(errno));
589 else if (mkdir(diskdir, 0770) != 0 && errno != EEXIST) {
590 log_add(L_WARNING, "WARNING: could not create %s: %s",
591 diskdir, strerror(errno));
594 else if (stat(diskdir, &stat_hdp) == -1) {
595 log_add(L_WARNING, "WARNING: could not stat %s: %s",
596 diskdir, strerror(errno));
600 if (!S_ISDIR((stat_hdp.st_mode))) {
601 log_add(L_WARNING, "WARNING: %s is not a directory",
605 else if (access(diskdir,W_OK) != 0) {
606 log_add(L_WARNING, "WARNING: directory %s is not writable",