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.
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.
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.
24 * Authors: the Amanda Development Team. Its members are listed in a
25 * file named AUTHORS, in the root directory of this distribution.
28 * $Id: holding.c,v 1.56 2006/06/09 23:07:26 martinea Exp $
30 * Functions to access holding disk
37 #include "fileheader.h"
43 /* Is fname a directory?
45 * @param fname: filename (fully qualified)
48 static int is_dir(char *fname);
50 /* Is fname an empty file?
52 * @param fname: filename (fully qualified)
55 static int is_emptyfile(char *fname);
57 /* sanity check that datestamp is of the form YYYYMMDD or
60 * @param fname: a filename (without directory)
63 static int is_datestr(char *fname);
74 if(stat(fname, &statbuf) == -1) return 0;
76 return (statbuf.st_mode & S_IFDIR) == S_IFDIR;
85 if(stat(fname, &statbuf) == -1) return 0;
87 return ((statbuf.st_mode & S_IFDIR) != S_IFDIR) &&
88 (statbuf.st_size == (off_t)0);
96 int ch, num, date, year, month, hour, minute, second;
99 /* must be 8 digits */
100 for(cp = fname; (ch = *cp) != '\0'; cp++) {
105 if(ch != '\0' || (cp-fname != 8 && cp-fname != 14)) {
109 /* sanity check year, month, and day */
111 strncpy(ymd, fname, 8);
115 month = (num / 100) % 100;
117 if(year<1990 || year>2100 || month<1 || month>12 || date<1 || date>31)
123 /* sanity check hour, minute, and second */
124 strncpy(hms, fname+8, 6);
128 minute = (num / 100) % 100;
130 if(hour> 23 || minute>59 || second>59)
133 /* yes, we passed all the checks */
139 * Recursion functions
141 * These implement a general-purpose walk down the holding-* hierarchy.
144 /* Perform a custom action for this holding element (disk, dir, file, chunk).
146 * If the element is not cruft, the next step into the tree will only take place
147 * if this function returns a nonzero value.
149 * The walk is depth-first, with the callback for an element invoked
150 * before entering that element. Callbacks may depend on this behavior.
152 * @param datap: generic user-data pointer
153 * @param base: the parent of the element being examined, or NULL for
155 * @param element: the name of the element being examined
156 * @param fqpath: fully qualified path to 'element'
157 * @param is_cruft: nonzero if this element doesn't belong here
158 * @returns: nonzero if the walk should descend into this element.
160 typedef int (*holding_walk_fn)(
174 /* Recurse over all holding chunks in a holding file.
176 * Call per_chunk_fn for each chunk of the given file
178 * datap is passed, unchanged, to all holding_walk_fns.
180 * @param hfile: holding file to examine (fully qualified path)
181 * @param datap: generic user-data pointer
182 * @param per_chunk_fn: function to call for each holding chunk
184 static void holding_walk_file(
187 holding_walk_fn per_chunk_fn)
190 char *filename = NULL;
192 /* Loop through all cont_filenames (subsequent chunks) */
193 filename = stralloc(hfile);
194 while (filename != NULL && filename[0] != '\0') {
197 /* get the header to look for cont_filename */
198 if (!holding_file_get_dumpfile(filename, &file)) {
210 /* and go on to the next chunk if this wasn't cruft */
212 filename = stralloc(file.cont_filename);
213 dumpfile_free_data(&file);
219 /* Recurse over all holding files in a holding directory.
221 * Call per_file_fn for each file, and so on, stopping at the level given by
224 * datap is passed, unchanged, to all holding_walk_fns.
226 * @param hdir: holding directory to examine (fully qualified path)
227 * @param datap: generic user-data pointer
228 * @param stop_at: do not proceed beyond this level of the hierarchy
229 * @param per_file_fn: function to call for each holding file
230 * @param per_chunk_fn: function to call for each holding chunk
232 static void holding_walk_dir(
236 holding_walk_fn per_file_fn,
237 holding_walk_fn per_chunk_fn)
240 struct dirent *workdir;
246 if ((dir = opendir(hdir)) == NULL) {
248 dbprintf(_("Warning: could not open holding dir %s: %s\n"),
249 hdir, strerror(errno));
253 while ((workdir = readdir(dir)) != NULL) {
256 if (is_dot_or_dotdot(workdir->d_name))
257 continue; /* expected cruft */
259 hfile = newvstralloc(hfile,
260 hdir, "/", workdir->d_name,
263 /* filter out various undesirables */
264 if (is_emptyfile(hfile))
271 if (!(dumpf_ok=holding_file_get_dumpfile(hfile, &dumpf)) ||
272 dumpf.type != F_DUMPFILE) {
273 if (dumpf_ok && dumpf.type == F_CONT_DUMPFILE)
274 continue; /* silently skip expected file */
279 if (dumpf.dumplevel < 0 || dumpf.dumplevel > 9) {
284 proceed = per_file_fn(datap,
289 if (!is_cruft && proceed && stop_at != STOP_AT_FILE)
290 holding_walk_file(hfile,
293 dumpfile_free_data(&dumpf);
300 /* Recurse over all holding directories in a holding disk.
302 * Call per_dir_fn for each dir, and so on, stopping at the level given by
305 * datap is passed, unchanged, to all holding_walk_fns.
307 * @param hdisk: holding disk to examine (fully qualified path)
308 * @param datap: generic user-data pointer
309 * @param stop_at: do not proceed beyond this level of the hierarchy
310 * @param per_dir_fn: function to call for each holding dir
311 * @param per_file_fn: function to call for each holding file
312 * @param per_chunk_fn: function to call for each holding chunk
319 holding_walk_fn per_dir_fn,
320 holding_walk_fn per_file_fn,
321 holding_walk_fn per_chunk_fn)
324 struct dirent *workdir;
328 if ((dir = opendir(hdisk)) == NULL) {
330 dbprintf(_("Warning: could not open holding disk %s: %s\n"),
331 hdisk, strerror(errno));
335 while ((workdir = readdir(dir)) != NULL) {
338 if (is_dot_or_dotdot(workdir->d_name))
339 continue; /* expected cruft */
341 hdir = newvstralloc(hdir,
342 hdisk, "/", workdir->d_name,
348 } else if (!is_datestr(workdir->d_name)) {
349 /* EXT2/3 leave these in the root of each volume */
350 if (strcmp(workdir->d_name, "lost+found") == 0)
351 continue; /* expected cruft */
353 is_cruft = 1; /* unexpected */
357 proceed = per_dir_fn(datap,
362 if (!is_cruft && proceed && stop_at != STOP_AT_DIR)
363 holding_walk_dir(hdir,
374 /* Recurse over all holding disks.
376 * Call per_disk_fn for each disk, per_dir_fn for each dir, and so on, stopping
377 * at the level given by stop_at.
379 * datap is passed, unchanged, to all holding_walk_fns.
381 * @param datap: generic user-data pointer
382 * @param stop_at: do not proceed beyond this level of the hierarchy
383 * @param per_disk_fn: function to call for each holding disk
384 * @param per_dir_fn: function to call for each holding dir
385 * @param per_file_fn: function to call for each holding file
386 * @param per_chunk_fn: function to call for each holding chunk
392 holding_walk_fn per_disk_fn,
393 holding_walk_fn per_dir_fn,
394 holding_walk_fn per_file_fn,
395 holding_walk_fn per_chunk_fn)
398 holdingdisk_t *hdisk_conf;
402 for (il = getconf_identlist(CNF_HOLDINGDISK);
405 hdisk_conf = lookup_holdingdisk(il->data);
407 hdisk = holdingdisk_get_diskdir(hdisk_conf);
410 proceed = per_disk_fn(datap,
415 if (proceed && stop_at != STOP_AT_DISK)
416 holding_walk_disk(hdisk,
426 * holding_get_* functions
431 } holding_get_datap_t;
433 /* Functor for holding_get_*; adds 'element' or 'fqpath' to
439 G_GNUC_UNUSED char *base,
444 holding_get_datap_t *data = (holding_get_datap_t *)datap;
447 if (is_cruft) return 0;
450 data->result = g_slist_insert_sorted(data->result,
454 data->result = g_slist_insert_sorted(data->result,
458 /* don't proceed any deeper */
463 holding_get_disks(void)
465 holding_get_datap_t data;
467 data.fullpaths = 1; /* ignored anyway */
469 holding_walk((gpointer)&data,
471 holding_get_walk_fn, NULL, NULL, NULL);
481 holding_get_datap_t data;
483 data.fullpaths = fullpaths;
486 holding_walk_dir(hdir, (gpointer)&data,
488 holding_get_walk_fn, NULL);
490 holding_walk((gpointer)&data,
492 NULL, NULL, holding_get_walk_fn, NULL);
499 holding_get_file_chunks(char *hfile)
501 holding_get_datap_t data;
505 holding_walk_file(hfile, (gpointer)&data,
506 holding_get_walk_fn);
512 holding_get_files_for_flush(
515 GSList *file_list, *file_elt;
519 GSList *result_list = NULL;
521 /* loop over *all* files, checking each one's datestamp against the expressions
523 file_list = holding_get_files(NULL, 1);
524 for (file_elt = file_list; file_elt != NULL; file_elt = file_elt->next) {
525 /* get info on that file */
526 if (!holding_file_get_dumpfile((char *)file_elt->data, &file))
529 if (file.type != F_DUMPFILE) {
530 dumpfile_free_data(&file);
536 /* loop over date args, until we find a match */
537 for (date = dateargs; date !=NULL; date = date->next) {
538 if (strcmp((char *)date->data, file.datestamp) == 0) {
544 /* if no date list was provided, then all dates match */
548 dumpfile_free_data(&file);
552 /* passed all tests -- we'll flush this file */
553 result_list = g_slist_insert_sorted(result_list,
554 stralloc(file_elt->data),
556 dumpfile_free_data(&file);
559 if (file_list) slist_free_full(file_list, g_free);
565 holding_get_all_datestamps(void)
567 GSList *all_files, *file;
568 GSList *datestamps = NULL;
570 /* enumerate all files */
571 all_files = holding_get_files(NULL, 1);
572 for (file = all_files; file != NULL; file = file->next) {
574 if (!holding_file_get_dumpfile((char *)file->data, &dfile))
576 if (!g_slist_find_custom(datestamps, dfile.datestamp,
577 g_compare_strings)) {
578 datestamps = g_slist_insert_sorted(datestamps,
579 stralloc(dfile.datestamp),
582 dumpfile_free_data(&dfile);
585 slist_free_full(all_files, g_free);
597 off_t size = (off_t)0;
600 /* (note: we don't use holding_get_file_chunks here because that would
601 * entail opening each file twice) */
603 /* Loop through all cont_filenames (subsequent chunks) */
604 filename = stralloc(hfile);
605 while (filename != NULL && filename[0] != '\0') {
606 /* stat the file for its size */
607 if (stat(filename, &finfo) == -1) {
608 dbprintf(_("stat %s: %s\n"), filename, strerror(errno));
612 size += (finfo.st_size+(off_t)1023)/(off_t)1024;
614 size -= (off_t)(DISK_BLOCK_BYTES / 1024);
616 /* get the header to look for cont_filename */
617 if (!holding_file_get_dumpfile(filename, &file)) {
618 dbprintf(_("holding_file_size: open of %s failed.\n"), filename);
623 /* on to the next chunk */
624 filename = newstralloc(filename, file.cont_filename);
625 dumpfile_free_data(&file);
639 chunklist = holding_get_file_chunks(hfile);
643 for (chunk = chunklist; chunk != NULL; chunk = chunk->next) {
644 if (unlink((char *)chunk->data)<0) {
645 dbprintf(_("holding_file_unlink: could not unlink %s: %s\n"),
646 (char *)chunk->data, strerror(errno));
654 holding_file_get_dumpfile(
658 char buffer[DISK_BLOCK_BYTES];
661 memset(buffer, 0, sizeof(buffer));
664 file->type = F_UNKNOWN;
665 if((fd = robust_open(fname, O_RDONLY, 0)) == -1)
668 if(full_read(fd, buffer, SIZEOF(buffer)) != sizeof(buffer)) {
674 parse_file_header(buffer, file, SIZEOF(buffer));
683 corrupt_dle_fn corrupt_dle;
684 FILE *verbose_output;
685 } holding_cleanup_datap_t;
688 holding_cleanup_disk(
690 G_GNUC_UNUSED char *base,
691 G_GNUC_UNUSED char *element,
695 holding_cleanup_datap_t *data = (holding_cleanup_datap_t *)datap;
697 if (data->verbose_output) {
699 g_fprintf(data->verbose_output,
700 _("Invalid holding disk '%s'\n"), fqpath);
702 g_fprintf(data->verbose_output,
703 _("Cleaning up holding disk '%s'\n"), fqpath);
712 G_GNUC_UNUSED char *base,
717 holding_cleanup_datap_t *data = (holding_cleanup_datap_t *)datap;
720 if (data->verbose_output)
721 g_fprintf(data->verbose_output,
722 _("Invalid holding directory '%s'\n"), fqpath);
726 /* try removing it */
727 if (rmdir(fqpath) == 0) {
728 /* success, so don't try to walk into it */
729 if (data->verbose_output)
730 g_fprintf(data->verbose_output,
731 _(" ..removed empty directory '%s'\n"), element);
735 if (data->verbose_output)
736 g_fprintf(data->verbose_output,
737 _(" ..cleaning up holding directory '%s'\n"), element);
743 holding_cleanup_file(
745 G_GNUC_UNUSED char *base,
750 holding_cleanup_datap_t *data = (holding_cleanup_datap_t *)datap;
757 if (data->verbose_output)
758 g_fprintf(data->verbose_output,
759 _("Invalid holding file '%s'\n"), element);
764 stat = holding_file_get_dumpfile(fqpath, &file);
767 if (data->verbose_output)
768 g_fprintf(data->verbose_output,
769 _("Could not read read header from '%s'\n"), element);
770 dumpfile_free_data(&file);
774 if (file.type != F_DUMPFILE && file.type != F_CONT_DUMPFILE) {
775 if (data->verbose_output)
776 g_fprintf(data->verbose_output,
777 _("File '%s' is not a dump file\n"), element);
778 dumpfile_free_data(&file);
782 if(file.dumplevel < 0 || file.dumplevel > 399) {
783 if (data->verbose_output)
784 g_fprintf(data->verbose_output,
785 _("File '%s' has invalid level %d\n"), element, file.dumplevel);
786 dumpfile_free_data(&file);
790 dp = lookup_disk(file.name, file.disk);
793 if (data->verbose_output)
794 g_fprintf(data->verbose_output,
795 _("File '%s' is for '%s:%s', which is not in the disklist\n"),
796 element, file.name, file.disk);
797 dumpfile_free_data(&file);
801 if ((l = strlen(element)) >= 7 && strncmp(&fqpath[l-4],".tmp",4) == 0) {
804 /* generate a name without '.tmp' */
805 destname = stralloc(fqpath);
806 destname[strlen(destname) - 4] = '\0';
808 /* OK, it passes muster -- rename it to salvage some data,
809 * and mark the DLE as corrupted */
810 if (data->verbose_output)
811 g_fprintf(data->verbose_output,
812 _("Processing partial holding file '%s'\n"), element);
814 if(rename_tmp_holding(destname, 0)) {
815 if (data->corrupt_dle)
816 data->corrupt_dle(dp->host->hostname, dp->name);
818 dbprintf(_("rename_tmp_holding(%s) failed\n"), destname);
819 if (data->verbose_output)
820 g_fprintf(data->verbose_output,
821 _("Rename of '%s' to '%s' failed.\n"), element, destname);
827 dumpfile_free_data(&file);
833 corrupt_dle_fn corrupt_dle,
834 FILE *verbose_output)
836 holding_cleanup_datap_t data;
837 data.corrupt_dle = corrupt_dle;
838 data.verbose_output = verbose_output;
840 holding_walk((gpointer)&data,
842 holding_cleanup_disk,
844 holding_cleanup_file,
849 * Application support
853 holding_set_origsize(
859 char buffer[DISK_BLOCK_BYTES];
863 if((fd = robust_open(holding_file, O_RDWR, 0)) == -1) {
864 dbprintf(_("holding_set_origsize: open of %s failed: %s\n"),
865 holding_file, strerror(errno));
869 buflen = full_read(fd, buffer, SIZEOF(buffer));
871 dbprintf(_("holding_set_origsize: %s: empty file?\n"), holding_file);
874 parse_file_header(buffer, &file, (size_t)buflen);
875 lseek(fd, (off_t)0, SEEK_SET);
876 file.orig_size = orig_size;
877 read_buffer = build_header(&file, NULL, DISK_BLOCK_BYTES);
878 full_write(fd, read_buffer, DISK_BLOCK_BYTES);
879 dumpfile_free_data(&file);
891 char buffer[DISK_BLOCK_BYTES];
894 char *filename_tmp = NULL;
896 memset(buffer, 0, sizeof(buffer));
897 filename = stralloc(holding_file);
898 while(filename != NULL && filename[0] != '\0') {
899 filename_tmp = newvstralloc(filename_tmp, filename, ".tmp", NULL);
900 if((fd = robust_open(filename_tmp,O_RDONLY, 0)) == -1) {
901 dbprintf(_("rename_tmp_holding: open of %s failed: %s\n"),filename_tmp,strerror(errno));
903 amfree(filename_tmp);
906 buflen = full_read(fd, buffer, SIZEOF(buffer));
909 if(rename(filename_tmp, filename) != 0) {
910 dbprintf(_("rename_tmp_holding: could not rename \"%s\" to \"%s\": %s"),
911 filename_tmp, filename, strerror(errno));
915 dbprintf(_("rename_tmp_holding: %s: empty file?\n"), filename);
917 amfree(filename_tmp);
920 parse_file_header(buffer, &file, (size_t)buflen);
923 if((fd = robust_open(filename, O_RDWR, 0)) == -1) {
924 dbprintf(_("rename_tmp_holdingX: open of %s failed: %s\n"),
925 filename, strerror(errno));
926 dumpfile_free_data(&file);
928 amfree(filename_tmp);
933 if (debug_holding > 1)
934 dump_dumpfile_t(&file);
935 header = build_header(&file, NULL, DISK_BLOCK_BYTES);
936 if (!header) /* this shouldn't happen */
937 error(_("header does not fit in %zd bytes"), (size_t)DISK_BLOCK_BYTES);
938 if (full_write(fd, header, DISK_BLOCK_BYTES) != DISK_BLOCK_BYTES) {
939 dbprintf(_("rename_tmp_holding: writing new header failed: %s"),
941 dumpfile_free_data(&file);
943 amfree(filename_tmp);
951 filename = newstralloc(filename, file.cont_filename);
952 dumpfile_free_data(&file);
955 amfree(filename_tmp);
964 struct stat stat_hdp;
967 if (mkpdir(diskdir, 0770, (uid_t)-1, (gid_t)-1) != 0 && errno != EEXIST) {
968 log_add(L_WARNING, _("WARNING: could not create parents of %s: %s"),
969 diskdir, strerror(errno));
972 else if (mkdir(diskdir, 0770) != 0 && errno != EEXIST) {
973 log_add(L_WARNING, _("WARNING: could not create %s: %s"),
974 diskdir, strerror(errno));
977 else if (stat(diskdir, &stat_hdp) == -1) {
978 log_add(L_WARNING, _("WARNING: could not stat %s: %s"),
979 diskdir, strerror(errno));
983 if (!S_ISDIR((stat_hdp.st_mode))) {
984 log_add(L_WARNING, _("WARNING: %s is not a directory"),
988 else if (access(diskdir,W_OK) != 0) {
989 log_add(L_WARNING, _("WARNING: directory %s is not writable"),