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);
404 hdisk_conf = lookup_holdingdisk(il->data);
406 hdisk = holdingdisk_get_diskdir(hdisk_conf);
409 proceed = per_disk_fn(datap,
414 if (proceed && stop_at != STOP_AT_DISK)
415 holding_walk_disk(hdisk,
425 * holding_get_* functions
430 } holding_get_datap_t;
432 /* Functor for holding_get_*; adds 'element' or 'fqpath' to
438 G_GNUC_UNUSED char *base,
443 holding_get_datap_t *data = (holding_get_datap_t *)datap;
446 if (is_cruft) return 0;
449 data->result = g_slist_insert_sorted(data->result,
453 data->result = g_slist_insert_sorted(data->result,
457 /* don't proceed any deeper */
462 holding_get_disks(void)
464 holding_get_datap_t data;
466 data.fullpaths = 1; /* ignored anyway */
468 holding_walk((gpointer)&data,
470 holding_get_walk_fn, NULL, NULL, NULL);
480 holding_get_datap_t data;
482 data.fullpaths = fullpaths;
485 holding_walk_dir(hdir, (gpointer)&data,
487 holding_get_walk_fn, NULL);
489 holding_walk((gpointer)&data,
491 NULL, NULL, holding_get_walk_fn, NULL);
498 holding_get_file_chunks(char *hfile)
500 holding_get_datap_t data;
504 holding_walk_file(hfile, (gpointer)&data,
505 holding_get_walk_fn);
511 holding_get_files_for_flush(
514 GSList *file_list, *file_elt;
518 GSList *result_list = NULL;
520 /* loop over *all* files, checking each one's datestamp against the expressions
522 file_list = holding_get_files(NULL, 1);
523 for (file_elt = file_list; file_elt != NULL; file_elt = file_elt->next) {
524 /* get info on that file */
525 if (!holding_file_get_dumpfile((char *)file_elt->data, &file))
528 if (file.type != F_DUMPFILE) {
529 dumpfile_free_data(&file);
535 /* loop over date args, until we find a match */
536 for (date = dateargs; date !=NULL; date = date->next) {
537 if (strcmp((char *)date->data, file.datestamp) == 0) {
543 /* if no date list was provided, then all dates match */
547 dumpfile_free_data(&file);
551 /* passed all tests -- we'll flush this file */
552 result_list = g_slist_insert_sorted(result_list,
553 stralloc(file_elt->data),
555 dumpfile_free_data(&file);
558 if (file_list) slist_free_full(file_list, g_free);
564 holding_get_all_datestamps(void)
566 GSList *all_files, *file;
567 GSList *datestamps = NULL;
569 /* enumerate all files */
570 all_files = holding_get_files(NULL, 1);
571 for (file = all_files; file != NULL; file = file->next) {
573 if (!holding_file_get_dumpfile((char *)file->data, &dfile))
575 if (!g_slist_find_custom(datestamps, dfile.datestamp,
576 g_compare_strings)) {
577 datestamps = g_slist_insert_sorted(datestamps,
578 stralloc(dfile.datestamp),
581 dumpfile_free_data(&dfile);
584 slist_free_full(all_files, g_free);
596 off_t size = (off_t)0;
599 /* (note: we don't use holding_get_file_chunks here because that would
600 * entail opening each file twice) */
602 /* Loop through all cont_filenames (subsequent chunks) */
603 filename = stralloc(hfile);
604 while (filename != NULL && filename[0] != '\0') {
605 /* stat the file for its size */
606 if (stat(filename, &finfo) == -1) {
607 dbprintf(_("stat %s: %s\n"), filename, strerror(errno));
611 size += (finfo.st_size+(off_t)1023)/(off_t)1024;
613 size -= (off_t)(DISK_BLOCK_BYTES / 1024);
615 /* get the header to look for cont_filename */
616 if (!holding_file_get_dumpfile(filename, &file)) {
617 dbprintf(_("holding_file_size: open of %s failed.\n"), filename);
622 /* on to the next chunk */
623 filename = newstralloc(filename, file.cont_filename);
624 dumpfile_free_data(&file);
638 chunklist = holding_get_file_chunks(hfile);
642 for (chunk = chunklist; chunk != NULL; chunk = chunk->next) {
643 if (unlink((char *)chunk->data)<0) {
644 dbprintf(_("holding_file_unlink: could not unlink %s: %s\n"),
645 (char *)chunk->data, strerror(errno));
653 holding_file_get_dumpfile(
657 char buffer[DISK_BLOCK_BYTES];
660 memset(buffer, 0, sizeof(buffer));
663 file->type = F_UNKNOWN;
664 if((fd = robust_open(fname, O_RDONLY, 0)) == -1)
667 if(full_read(fd, buffer, SIZEOF(buffer)) != sizeof(buffer)) {
673 parse_file_header(buffer, file, SIZEOF(buffer));
682 corrupt_dle_fn corrupt_dle;
683 FILE *verbose_output;
684 } holding_cleanup_datap_t;
687 holding_cleanup_disk(
689 G_GNUC_UNUSED char *base,
690 G_GNUC_UNUSED char *element,
694 holding_cleanup_datap_t *data = (holding_cleanup_datap_t *)datap;
696 if (data->verbose_output) {
698 g_fprintf(data->verbose_output,
699 _("Invalid holding disk '%s'\n"), fqpath);
701 g_fprintf(data->verbose_output,
702 _("Cleaning up holding disk '%s'\n"), fqpath);
711 G_GNUC_UNUSED char *base,
716 holding_cleanup_datap_t *data = (holding_cleanup_datap_t *)datap;
719 if (data->verbose_output)
720 g_fprintf(data->verbose_output,
721 _("Invalid holding directory '%s'\n"), fqpath);
725 /* try removing it */
726 if (rmdir(fqpath) == 0) {
727 /* success, so don't try to walk into it */
728 if (data->verbose_output)
729 g_fprintf(data->verbose_output,
730 _(" ..removed empty directory '%s'\n"), element);
734 if (data->verbose_output)
735 g_fprintf(data->verbose_output,
736 _(" ..cleaning up holding directory '%s'\n"), element);
742 holding_cleanup_file(
744 G_GNUC_UNUSED char *base,
749 holding_cleanup_datap_t *data = (holding_cleanup_datap_t *)datap;
756 if (data->verbose_output)
757 g_fprintf(data->verbose_output,
758 _("Invalid holding file '%s'\n"), element);
763 stat = holding_file_get_dumpfile(fqpath, &file);
766 if (data->verbose_output)
767 g_fprintf(data->verbose_output,
768 _("Could not read read header from '%s'\n"), element);
769 dumpfile_free_data(&file);
773 if (file.type != F_DUMPFILE && file.type != F_CONT_DUMPFILE) {
774 if (data->verbose_output)
775 g_fprintf(data->verbose_output,
776 _("File '%s' is not a dump file\n"), element);
777 dumpfile_free_data(&file);
781 if(file.dumplevel < 0 || file.dumplevel > 399) {
782 if (data->verbose_output)
783 g_fprintf(data->verbose_output,
784 _("File '%s' has invalid level %d\n"), element, file.dumplevel);
785 dumpfile_free_data(&file);
789 dp = lookup_disk(file.name, file.disk);
792 if (data->verbose_output)
793 g_fprintf(data->verbose_output,
794 _("File '%s' is for '%s:%s', which is not in the disklist\n"),
795 element, file.name, file.disk);
796 dumpfile_free_data(&file);
800 if ((l = strlen(element)) >= 7 && strncmp(&fqpath[l-4],".tmp",4) == 0) {
803 /* generate a name without '.tmp' */
804 destname = stralloc(fqpath);
805 destname[strlen(destname) - 4] = '\0';
807 /* OK, it passes muster -- rename it to salvage some data,
808 * and mark the DLE as corrupted */
809 if (data->verbose_output)
810 g_fprintf(data->verbose_output,
811 _("Processing partial holding file '%s'\n"), element);
813 if(rename_tmp_holding(destname, 0)) {
814 if (data->corrupt_dle)
815 data->corrupt_dle(dp->host->hostname, dp->name);
817 dbprintf(_("rename_tmp_holding(%s) failed\n"), destname);
818 if (data->verbose_output)
819 g_fprintf(data->verbose_output,
820 _("Rename of '%s' to '%s' failed.\n"), element, destname);
826 dumpfile_free_data(&file);
832 corrupt_dle_fn corrupt_dle,
833 FILE *verbose_output)
835 holding_cleanup_datap_t data;
836 data.corrupt_dle = corrupt_dle;
837 data.verbose_output = verbose_output;
839 holding_walk((gpointer)&data,
841 holding_cleanup_disk,
843 holding_cleanup_file,
848 * Application support
852 holding_set_origsize(
858 char buffer[DISK_BLOCK_BYTES];
862 if((fd = robust_open(holding_file, O_RDWR, 0)) == -1) {
863 dbprintf(_("holding_set_origsize: open of %s failed: %s\n"),
864 holding_file, strerror(errno));
868 buflen = full_read(fd, buffer, SIZEOF(buffer));
870 dbprintf(_("holding_set_origsize: %s: empty file?\n"), holding_file);
873 parse_file_header(buffer, &file, (size_t)buflen);
874 lseek(fd, (off_t)0, SEEK_SET);
875 file.orig_size = orig_size;
876 read_buffer = build_header(&file, NULL, DISK_BLOCK_BYTES);
877 full_write(fd, read_buffer, DISK_BLOCK_BYTES);
878 dumpfile_free_data(&file);
890 char buffer[DISK_BLOCK_BYTES];
893 char *filename_tmp = NULL;
895 memset(buffer, 0, sizeof(buffer));
896 filename = stralloc(holding_file);
897 while(filename != NULL && filename[0] != '\0') {
898 filename_tmp = newvstralloc(filename_tmp, filename, ".tmp", NULL);
899 if((fd = robust_open(filename_tmp,O_RDONLY, 0)) == -1) {
900 dbprintf(_("rename_tmp_holding: open of %s failed: %s\n"),filename_tmp,strerror(errno));
902 amfree(filename_tmp);
905 buflen = full_read(fd, buffer, SIZEOF(buffer));
908 if(rename(filename_tmp, filename) != 0) {
909 dbprintf(_("rename_tmp_holding: could not rename \"%s\" to \"%s\": %s"),
910 filename_tmp, filename, strerror(errno));
914 dbprintf(_("rename_tmp_holding: %s: empty file?\n"), filename);
916 amfree(filename_tmp);
919 parse_file_header(buffer, &file, (size_t)buflen);
922 if((fd = robust_open(filename, O_RDWR, 0)) == -1) {
923 dbprintf(_("rename_tmp_holdingX: open of %s failed: %s\n"),
924 filename, strerror(errno));
925 dumpfile_free_data(&file);
927 amfree(filename_tmp);
932 if (debug_holding > 1)
933 dump_dumpfile_t(&file);
934 header = build_header(&file, NULL, DISK_BLOCK_BYTES);
935 if (!header) /* this shouldn't happen */
936 error(_("header does not fit in %zd bytes"), (size_t)DISK_BLOCK_BYTES);
937 if (full_write(fd, header, DISK_BLOCK_BYTES) != DISK_BLOCK_BYTES) {
938 dbprintf(_("rename_tmp_holding: writing new header failed: %s"),
940 dumpfile_free_data(&file);
942 amfree(filename_tmp);
950 filename = newstralloc(filename, file.cont_filename);
951 dumpfile_free_data(&file);
954 amfree(filename_tmp);
963 struct stat stat_hdp;
966 if (mkpdir(diskdir, 0770, (uid_t)-1, (gid_t)-1) != 0 && errno != EEXIST) {
967 log_add(L_WARNING, _("WARNING: could not create parents of %s: %s"),
968 diskdir, strerror(errno));
971 else if (mkdir(diskdir, 0770) != 0 && errno != EEXIST) {
972 log_add(L_WARNING, _("WARNING: could not create %s: %s"),
973 diskdir, strerror(errno));
976 else if (stat(diskdir, &stat_hdp) == -1) {
977 log_add(L_WARNING, _("WARNING: could not stat %s: %s"),
978 diskdir, strerror(errno));
982 if (!S_ISDIR((stat_hdp.st_mode))) {
983 log_add(L_WARNING, _("WARNING: %s is not a directory"),
987 else if (access(diskdir,W_OK) != 0) {
988 log_add(L_WARNING, _("WARNING: directory %s is not writable"),