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.56 2006/06/09 23:07:26 martinea Exp $
29 * Functions to access holding disk
36 #include "fileheader.h"
42 /* Is fname a directory?
44 * @param fname: filename (fully qualified)
47 static int is_dir(char *fname);
49 /* Is fname an empty file?
51 * @param fname: filename (fully qualified)
54 static int is_emptyfile(char *fname);
56 /* sanity check that datestamp is of the form YYYYMMDD or
59 * @param fname: a filename (without directory)
62 static int is_datestr(char *fname);
73 if(stat(fname, &statbuf) == -1) return 0;
75 return (statbuf.st_mode & S_IFDIR) == S_IFDIR;
84 if(stat(fname, &statbuf) == -1) return 0;
86 return ((statbuf.st_mode & S_IFDIR) != S_IFDIR) &&
87 (statbuf.st_size == (off_t)0);
95 int ch, num, date, year, month, hour, minute, second;
98 /* must be 8 digits */
99 for(cp = fname; (ch = *cp) != '\0'; cp++) {
104 if(ch != '\0' || (cp-fname != 8 && cp-fname != 14)) {
108 /* sanity check year, month, and day */
110 strncpy(ymd, fname, 8);
114 month = (num / 100) % 100;
116 if(year<1990 || year>2100 || month<1 || month>12 || date<1 || date>31)
122 /* sanity check hour, minute, and second */
123 strncpy(hms, fname+8, 6);
127 minute = (num / 100) % 100;
129 if(hour> 23 || minute>59 || second>59)
132 /* yes, we passed all the checks */
138 * Recursion functions
140 * These implement a general-purpose walk down the holding-* hierarchy.
143 /* Perform a custom action for this holding element (disk, dir, file, chunk).
145 * If the element is not cruft, the next step into the tree will only take place
146 * if this function returns a nonzero value.
148 * The walk is depth-first, with the callback for an element invoked
149 * before entering that element. Callbacks may depend on this behavior.
151 * @param datap: generic user-data pointer
152 * @param base: the parent of the element being examined, or NULL for
154 * @param element: the name of the element being examined
155 * @param fqpath: fully qualified path to 'element'
156 * @param is_cruft: nonzero if this element doesn't belong here
157 * @returns: nonzero if the walk should descend into this element.
159 typedef int (*holding_walk_fn)(
173 /* Recurse over all holding chunks in a holding file.
175 * Call per_chunk_fn for each chunk of the given file
177 * datap is passed, unchanged, to all holding_walk_fns.
179 * @param hfile: holding file to examine (fully qualified path)
180 * @param datap: generic user-data pointer
181 * @param per_chunk_fn: function to call for each holding chunk
183 static void holding_walk_file(
186 holding_walk_fn per_chunk_fn)
189 char *filename = NULL;
191 /* Loop through all cont_filenames (subsequent chunks) */
192 filename = stralloc(hfile);
193 while (filename != NULL && filename[0] != '\0') {
196 /* get the header to look for cont_filename */
197 if (!holding_file_get_dumpfile(filename, &file)) {
209 /* and go on to the next chunk if this wasn't cruft */
211 filename = stralloc(file.cont_filename);
212 dumpfile_free_data(&file);
218 /* Recurse over all holding files in a holding directory.
220 * Call per_file_fn for each file, and so on, stopping at the level given by
223 * datap is passed, unchanged, to all holding_walk_fns.
225 * @param hdir: holding directory to examine (fully qualified path)
226 * @param datap: generic user-data pointer
227 * @param stop_at: do not proceed beyond this level of the hierarchy
228 * @param per_file_fn: function to call for each holding file
229 * @param per_chunk_fn: function to call for each holding chunk
231 static void holding_walk_dir(
235 holding_walk_fn per_file_fn,
236 holding_walk_fn per_chunk_fn)
239 struct dirent *workdir;
245 if ((dir = opendir(hdir)) == NULL) {
247 dbprintf(_("Warning: could not open holding dir %s: %s\n"),
248 hdir, strerror(errno));
252 while ((workdir = readdir(dir)) != NULL) {
255 if (is_dot_or_dotdot(workdir->d_name))
256 continue; /* expected cruft */
258 hfile = newvstralloc(hfile,
259 hdir, "/", workdir->d_name,
262 /* filter out various undesirables */
263 if (is_emptyfile(hfile))
270 if (!(dumpf_ok=holding_file_get_dumpfile(hfile, &dumpf)) ||
271 dumpf.type != F_DUMPFILE) {
272 if (dumpf_ok && dumpf.type == F_CONT_DUMPFILE)
273 continue; /* silently skip expected file */
278 if (dumpf.dumplevel < 0 || dumpf.dumplevel > 9) {
283 proceed = per_file_fn(datap,
288 if (!is_cruft && proceed && stop_at != STOP_AT_FILE)
289 holding_walk_file(hfile,
292 dumpfile_free_data(&dumpf);
299 /* Recurse over all holding directories in a holding disk.
301 * Call per_dir_fn for each dir, and so on, stopping at the level given by
304 * datap is passed, unchanged, to all holding_walk_fns.
306 * @param hdisk: holding disk to examine (fully qualified path)
307 * @param datap: generic user-data pointer
308 * @param stop_at: do not proceed beyond this level of the hierarchy
309 * @param per_dir_fn: function to call for each holding dir
310 * @param per_file_fn: function to call for each holding file
311 * @param per_chunk_fn: function to call for each holding chunk
318 holding_walk_fn per_dir_fn,
319 holding_walk_fn per_file_fn,
320 holding_walk_fn per_chunk_fn)
323 struct dirent *workdir;
327 if ((dir = opendir(hdisk)) == NULL) {
329 dbprintf(_("Warning: could not open holding disk %s: %s\n"),
330 hdisk, strerror(errno));
334 while ((workdir = readdir(dir)) != NULL) {
337 if (is_dot_or_dotdot(workdir->d_name))
338 continue; /* expected cruft */
340 hdir = newvstralloc(hdir,
341 hdisk, "/", workdir->d_name,
347 } else if (!is_datestr(workdir->d_name)) {
348 /* EXT2/3 leave these in the root of each volume */
349 if (strcmp(workdir->d_name, "lost+found") == 0)
350 continue; /* expected cruft */
352 is_cruft = 1; /* unexpected */
356 proceed = per_dir_fn(datap,
361 if (!is_cruft && proceed && stop_at != STOP_AT_DIR)
362 holding_walk_dir(hdir,
373 /* Recurse over all holding disks.
375 * Call per_disk_fn for each disk, per_dir_fn for each dir, and so on, stopping
376 * at the level given by stop_at.
378 * datap is passed, unchanged, to all holding_walk_fns.
380 * @param datap: generic user-data pointer
381 * @param stop_at: do not proceed beyond this level of the hierarchy
382 * @param per_disk_fn: function to call for each holding disk
383 * @param per_dir_fn: function to call for each holding dir
384 * @param per_file_fn: function to call for each holding file
385 * @param per_chunk_fn: function to call for each holding chunk
391 holding_walk_fn per_disk_fn,
392 holding_walk_fn per_dir_fn,
393 holding_walk_fn per_file_fn,
394 holding_walk_fn per_chunk_fn)
397 holdingdisk_t *hdisk_conf;
401 for (il = getconf_identlist(CNF_HOLDINGDISK);
405 hdisk_conf = lookup_holdingdisk(il->data);
407 hdisk = holdingdisk_get_diskdir(hdisk_conf);
412 proceed = per_disk_fn(datap,
417 if (proceed && stop_at != STOP_AT_DISK)
418 holding_walk_disk(hdisk,
428 * holding_get_* functions
433 } holding_get_datap_t;
435 /* Functor for holding_get_*; adds 'element' or 'fqpath' to
441 G_GNUC_UNUSED char *base,
446 holding_get_datap_t *data = (holding_get_datap_t *)datap;
449 if (is_cruft) return 0;
452 data->result = g_slist_insert_sorted(data->result,
456 data->result = g_slist_insert_sorted(data->result,
460 /* don't proceed any deeper */
465 holding_get_disks(void)
467 holding_get_datap_t data;
469 data.fullpaths = 1; /* ignored anyway */
471 holding_walk((gpointer)&data,
473 holding_get_walk_fn, NULL, NULL, NULL);
483 holding_get_datap_t data;
485 data.fullpaths = fullpaths;
488 holding_walk_dir(hdir, (gpointer)&data,
490 holding_get_walk_fn, NULL);
492 holding_walk((gpointer)&data,
494 NULL, NULL, holding_get_walk_fn, NULL);
501 holding_get_file_chunks(char *hfile)
503 holding_get_datap_t data;
507 holding_walk_file(hfile, (gpointer)&data,
508 holding_get_walk_fn);
514 holding_get_files_for_flush(
517 GSList *file_list, *file_elt;
521 GSList *result_list = NULL;
523 /* loop over *all* files, checking each one's datestamp against the expressions
525 file_list = holding_get_files(NULL, 1);
526 for (file_elt = file_list; file_elt != NULL; file_elt = file_elt->next) {
527 /* get info on that file */
528 if (!holding_file_get_dumpfile((char *)file_elt->data, &file))
531 if (file.type != F_DUMPFILE) {
532 dumpfile_free_data(&file);
538 /* loop over date args, until we find a match */
539 for (date = dateargs; date !=NULL; date = date->next) {
540 if (strcmp((char *)date->data, file.datestamp) == 0) {
546 /* if no date list was provided, then all dates match */
550 dumpfile_free_data(&file);
554 /* passed all tests -- we'll flush this file */
555 result_list = g_slist_insert_sorted(result_list,
556 stralloc(file_elt->data),
558 dumpfile_free_data(&file);
561 if (file_list) g_slist_free_full(file_list);
567 holding_get_all_datestamps(void)
569 GSList *all_files, *file;
570 GSList *datestamps = NULL;
572 /* enumerate all files */
573 all_files = holding_get_files(NULL, 1);
574 for (file = all_files; file != NULL; file = file->next) {
576 if (!holding_file_get_dumpfile((char *)file->data, &dfile))
578 if (!g_slist_find_custom(datestamps, dfile.datestamp,
579 g_compare_strings)) {
580 datestamps = g_slist_insert_sorted(datestamps,
581 stralloc(dfile.datestamp),
584 dumpfile_free_data(&dfile);
587 g_slist_free_full(all_files);
599 off_t size = (off_t)0;
602 /* (note: we don't use holding_get_file_chunks here because that would
603 * entail opening each file twice) */
605 /* Loop through all cont_filenames (subsequent chunks) */
606 filename = stralloc(hfile);
607 while (filename != NULL && filename[0] != '\0') {
608 /* stat the file for its size */
609 if (stat(filename, &finfo) == -1) {
610 dbprintf(_("stat %s: %s\n"), filename, strerror(errno));
614 size += (finfo.st_size+(off_t)1023)/(off_t)1024;
616 size -= (off_t)(DISK_BLOCK_BYTES / 1024);
618 /* get the header to look for cont_filename */
619 if (!holding_file_get_dumpfile(filename, &file)) {
620 dbprintf(_("holding_file_size: open of %s failed.\n"), filename);
625 /* on to the next chunk */
626 filename = newstralloc(filename, file.cont_filename);
627 dumpfile_free_data(&file);
641 chunklist = holding_get_file_chunks(hfile);
645 for (chunk = chunklist; chunk != NULL; chunk = chunk->next) {
646 if (unlink((char *)chunk->data)<0) {
647 dbprintf(_("holding_file_unlink: could not unlink %s: %s\n"),
648 (char *)chunk->data, strerror(errno));
656 holding_file_get_dumpfile(
660 char buffer[DISK_BLOCK_BYTES];
663 memset(buffer, 0, sizeof(buffer));
666 file->type = F_UNKNOWN;
667 if((fd = robust_open(fname, O_RDONLY, 0)) == -1)
670 if(full_read(fd, buffer, SIZEOF(buffer)) != sizeof(buffer)) {
676 parse_file_header(buffer, file, SIZEOF(buffer));
685 corrupt_dle_fn corrupt_dle;
686 FILE *verbose_output;
687 } holding_cleanup_datap_t;
690 holding_cleanup_disk(
692 G_GNUC_UNUSED char *base,
693 G_GNUC_UNUSED char *element,
697 holding_cleanup_datap_t *data = (holding_cleanup_datap_t *)datap;
699 if (data->verbose_output) {
701 g_fprintf(data->verbose_output,
702 _("Invalid holding disk '%s'\n"), fqpath);
704 g_fprintf(data->verbose_output,
705 _("Cleaning up holding disk '%s'\n"), fqpath);
714 G_GNUC_UNUSED char *base,
719 holding_cleanup_datap_t *data = (holding_cleanup_datap_t *)datap;
722 if (data->verbose_output)
723 g_fprintf(data->verbose_output,
724 _("Invalid holding directory '%s'\n"), fqpath);
728 /* try removing it */
729 if (rmdir(fqpath) == 0) {
730 /* success, so don't try to walk into it */
731 if (data->verbose_output)
732 g_fprintf(data->verbose_output,
733 _(" ..removed empty directory '%s'\n"), element);
737 if (data->verbose_output)
738 g_fprintf(data->verbose_output,
739 _(" ..cleaning up holding directory '%s'\n"), element);
745 holding_cleanup_file(
747 G_GNUC_UNUSED char *base,
752 holding_cleanup_datap_t *data = (holding_cleanup_datap_t *)datap;
759 if (data->verbose_output)
760 g_fprintf(data->verbose_output,
761 _("Invalid holding file '%s'\n"), element);
766 stat = holding_file_get_dumpfile(fqpath, &file);
769 if (data->verbose_output)
770 g_fprintf(data->verbose_output,
771 _("Could not read read header from '%s'\n"), element);
772 dumpfile_free_data(&file);
776 if (file.type != F_DUMPFILE && file.type != F_CONT_DUMPFILE) {
777 if (data->verbose_output)
778 g_fprintf(data->verbose_output,
779 _("File '%s' is not a dump file\n"), element);
780 dumpfile_free_data(&file);
784 if(file.dumplevel < 0 || file.dumplevel > 9) {
785 if (data->verbose_output)
786 g_fprintf(data->verbose_output,
787 _("File '%s' has invalid level %d\n"), element, file.dumplevel);
788 dumpfile_free_data(&file);
792 dp = lookup_disk(file.name, file.disk);
795 if (data->verbose_output)
796 g_fprintf(data->verbose_output,
797 _("File '%s' is for '%s:%s', which is not in the disklist\n"),
798 element, file.name, file.disk);
799 dumpfile_free_data(&file);
803 if ((l = strlen(element)) >= 7 && strncmp(&fqpath[l-4],".tmp",4) == 0) {
806 /* generate a name without '.tmp' */
807 destname = stralloc(fqpath);
808 destname[strlen(destname) - 4] = '\0';
810 /* OK, it passes muster -- rename it to salvage some data,
811 * and mark the DLE as corrupted */
812 if (data->verbose_output)
813 g_fprintf(data->verbose_output,
814 _("Processing partial holding file '%s'\n"), element);
816 if(rename_tmp_holding(destname, 0)) {
817 if (data->corrupt_dle)
818 data->corrupt_dle(dp->host->hostname, dp->name);
820 dbprintf(_("rename_tmp_holding(%s) failed\n"), destname);
821 if (data->verbose_output)
822 g_fprintf(data->verbose_output,
823 _("Rename of '%s' to '%s' failed.\n"), element, destname);
829 dumpfile_free_data(&file);
835 corrupt_dle_fn corrupt_dle,
836 FILE *verbose_output)
838 holding_cleanup_datap_t data;
839 data.corrupt_dle = corrupt_dle;
840 data.verbose_output = verbose_output;
842 holding_walk((gpointer)&data,
844 holding_cleanup_disk,
846 holding_cleanup_file,
851 * Application support
855 holding_set_origsize(
861 char buffer[DISK_BLOCK_BYTES];
865 if((fd = robust_open(holding_file, O_RDWR, 0)) == -1) {
866 dbprintf(_("holding_set_origsize: open of %s failed: %s\n"),
867 holding_file, strerror(errno));
871 buflen = full_read(fd, buffer, SIZEOF(buffer));
873 dbprintf(_("holding_set_origsize: %s: empty file?\n"), holding_file);
876 parse_file_header(buffer, &file, (size_t)buflen);
877 lseek(fd, (off_t)0, SEEK_SET);
878 file.orig_size = orig_size;
879 read_buffer = build_header(&file, NULL, DISK_BLOCK_BYTES);
880 full_write(fd, read_buffer, DISK_BLOCK_BYTES);
881 dumpfile_free_data(&file);
893 char buffer[DISK_BLOCK_BYTES];
896 char *filename_tmp = NULL;
898 memset(buffer, 0, sizeof(buffer));
899 filename = stralloc(holding_file);
900 while(filename != NULL && filename[0] != '\0') {
901 filename_tmp = newvstralloc(filename_tmp, filename, ".tmp", NULL);
902 if((fd = robust_open(filename_tmp,O_RDONLY, 0)) == -1) {
903 dbprintf(_("rename_tmp_holding: open of %s failed: %s\n"),filename_tmp,strerror(errno));
905 amfree(filename_tmp);
908 buflen = full_read(fd, buffer, SIZEOF(buffer));
911 if(rename(filename_tmp, filename) != 0) {
912 dbprintf(_("rename_tmp_holding: could not rename \"%s\" to \"%s\": %s"),
913 filename_tmp, filename, strerror(errno));
917 dbprintf(_("rename_tmp_holding: %s: empty file?\n"), filename);
919 amfree(filename_tmp);
922 parse_file_header(buffer, &file, (size_t)buflen);
925 if((fd = robust_open(filename, O_RDWR, 0)) == -1) {
926 dbprintf(_("rename_tmp_holdingX: open of %s failed: %s\n"),
927 filename, strerror(errno));
928 dumpfile_free_data(&file);
930 amfree(filename_tmp);
935 if (debug_holding > 1)
936 dump_dumpfile_t(&file);
937 header = build_header(&file, NULL, DISK_BLOCK_BYTES);
938 if (!header) /* this shouldn't happen */
939 error(_("header does not fit in %zd bytes"), (size_t)DISK_BLOCK_BYTES);
940 if (full_write(fd, header, DISK_BLOCK_BYTES) != DISK_BLOCK_BYTES) {
941 dbprintf(_("rename_tmp_holding: writing new header failed: %s"),
943 dumpfile_free_data(&file);
945 amfree(filename_tmp);
953 filename = newstralloc(filename, file.cont_filename);
954 dumpfile_free_data(&file);
957 amfree(filename_tmp);
966 struct stat stat_hdp;
969 if (mkpdir(diskdir, 0770, (uid_t)-1, (gid_t)-1) != 0 && errno != EEXIST) {
970 log_add(L_WARNING, _("WARNING: could not create parents of %s: %s"),
971 diskdir, strerror(errno));
974 else if (mkdir(diskdir, 0770) != 0 && errno != EEXIST) {
975 log_add(L_WARNING, _("WARNING: could not create %s: %s"),
976 diskdir, strerror(errno));
979 else if (stat(diskdir, &stat_hdp) == -1) {
980 log_add(L_WARNING, _("WARNING: could not stat %s: %s"),
981 diskdir, strerror(errno));
985 if (!S_ISDIR((stat_hdp.st_mode))) {
986 log_add(L_WARNING, _("WARNING: %s is not a directory"),
990 else if (access(diskdir,W_OK) != 0) {
991 log_add(L_WARNING, _("WARNING: directory %s is not writable"),