📄 diskfile.c
字号:
/* * Amanda, The Advanced Maryland Automatic Network Disk Archiver * Copyright (c) 1991-1998 University of Maryland at College Park * All Rights Reserved. * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that * the above copyright notice appear in all copies and that both that * copyright notice and this permission notice appear in supporting * documentation, and that the name of U.M. not be used in advertising or * publicity pertaining to distribution of the software without specific, * written prior permission. U.M. makes no representations about the * suitability of this software for any purpose. It is provided "as is" * without express or implied warranty. * * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M. * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * Author: James da Silva, Systems Design and Analysis Group * Computer Science Department * University of Maryland at College Park *//* * $Id: diskfile.c,v 1.95 2006/07/26 15:17:37 martinea Exp $ * * read disklist file */#include "amanda.h"#include "arglist.h"#include "conffile.h"#include "diskfile.h"#include "util.h"static am_host_t *hostlist;static netif_t *all_netifs;/* local functions */static char *upcase(char *st);static int parse_diskline(disklist_t *, const char *, FILE *, int *, char **);static void disk_parserror(const char *, int, const char *, ...) G_GNUC_PRINTF(3, 4);intread_diskfile( const char *filename, disklist_t *lst){ FILE *diskf; int line_num; char *line; /* initialize */ hostlist = NULL; lst->head = lst->tail = NULL; line_num = 0; if ((diskf = fopen(filename, "r")) == NULL) { return -1; /*NOTREACHED*/ } while ((line = agets(diskf)) != NULL) { line_num++; if (line[0] != '\0') { if (parse_diskline(lst, filename, diskf, &line_num, &line) < 0) { amfree(line); afclose(diskf); return (-1); } } amfree(line); } afclose(diskf); return (0);}am_host_t *lookup_host( const char *hostname){ am_host_t *p; for (p = hostlist; p != NULL; p = p->next) { if(strcasecmp(p->hostname, hostname) == 0) return p; } return (NULL);}disk_t *lookup_disk( const char *hostname, const char *diskname){ am_host_t *host; disk_t *disk; host = lookup_host(hostname); if (host == NULL) return (NULL); for (disk = host->disks; disk != NULL; disk = disk->hostnext) { if (strcmp(disk->name, diskname) == 0) return (disk); } return (NULL);}/* * put disk on end of queue */voidenqueue_disk( disklist_t *list, disk_t * disk){ if(list->tail == NULL) list->head = disk; else list->tail->next = disk; disk->prev = list->tail; list->tail = disk; disk->next = NULL;}/* * put disk on head of queue */voidheadqueue_disk( disklist_t *list, disk_t * disk){ if(list->head == NULL) list->tail = disk; else list->head->prev = disk; disk->next = list->head; list->head = disk; disk->prev = NULL;}/* * insert in sorted order */voidinsert_disk( disklist_t *list, disk_t * disk, int (*cmp)(disk_t *a, disk_t *b)){ disk_t *prev, *ptr; prev = NULL; ptr = list->head; while(ptr != NULL) { if(cmp(disk, ptr) < 0) break; prev = ptr; ptr = ptr->next; } disk->next = ptr; disk->prev = prev; if(prev == NULL) list->head = disk; else prev->next = disk; if(ptr == NULL) list->tail = disk; else ptr->prev = disk;}disk_t *add_disk( disklist_t *list, char * hostname, char * diskname){ disk_t *disk; am_host_t *host; disk = alloc(SIZEOF(disk_t)); disk->line = 0; disk->tape_splitsize = (off_t)0; disk->split_diskbuffer = NULL; disk->fallback_splitsize = (off_t)0; disk->hostname = stralloc(hostname); disk->name = stralloc(diskname); disk->device = stralloc(diskname); disk->spindle = -1; disk->up = NULL; disk->compress = COMP_NONE; disk->encrypt = ENCRYPT_NONE; disk->start_t = 0; disk->todo = 1; disk->index = 1; disk->exclude_list = NULL; disk->exclude_file = NULL; disk->include_list = NULL; disk->include_file = NULL; host = lookup_host(hostname); if(host == NULL) { host = alloc(SIZEOF(am_host_t)); host->next = hostlist; hostlist = host; host->hostname = stralloc(hostname); host->disks = NULL; host->inprogress = 0; host->maxdumps = 1; host->netif = NULL; host->start_t = 0; host->up = NULL; host->features = NULL; } enqueue_disk(list, disk); disk->host = host; disk->hostnext = host->disks; host->disks = disk; return disk;}/* * check if disk is present in list. Return true if so, false otherwise. */intfind_disk( disklist_t *list, disk_t * disk){ disk_t *t; t = list->head; while ((t != NULL) && (t != disk)) { t = t->next; } return (t == disk);}/* * sort a whole queue */voidsort_disk( disklist_t *in, disklist_t *out, int (*cmp)(disk_t *a, disk_t *b)){ disklist_t *tmp; disk_t *disk; tmp = in; /* just in case in == out */ out->head = (disk_t *)0; out->tail = (disk_t *)0; while((disk = dequeue_disk(tmp))) insert_disk(out, disk, cmp);}/* * remove disk from front of queue */disk_t *dequeue_disk( disklist_t *list){ disk_t *disk; if(list->head == NULL) return NULL; disk = list->head; list->head = disk->next; if(list->head == NULL) list->tail = NULL; else list->head->prev = NULL; disk->prev = disk->next = NULL; /* for debugging */ return disk;}voidremove_disk( disklist_t *list, disk_t * disk){ if(disk->prev == NULL) list->head = disk->next; else disk->prev->next = disk->next; if(disk->next == NULL) list->tail = disk->prev; else disk->next->prev = disk->prev; disk->prev = disk->next = NULL;}voidfree_disklist( disklist_t* dl){ disk_t *dp; am_host_t *host, *hostnext; while (dl->head != NULL) { dp = dequeue_disk(dl); amfree(dp->name); amfree(dp->hostname); amfree(dp->device); free_sl(dp->exclude_file); free_sl(dp->exclude_list); free_sl(dp->include_file); free_sl(dp->include_list); free(dp); } for(host=hostlist; host != NULL; host = hostnext) { amfree(host->hostname); am_release_feature_set(host->features); host->features = NULL; hostnext = host->next; amfree(host); } hostlist=NULL;}static char *upcase( char *st){ char *s = st; while(*s) { if(islower((int)*s)) *s = (char)toupper((int)*s); s++; } return st;}/* return 0 on success *//* return -1 on error */static intparse_diskline( disklist_t *lst, const char *filename, FILE * diskf, int * line_num_p, /*@keep@*/ char ** line_p){ am_host_t *host; disk_t *disk; dumptype_t *dtype; netif_t *netif = NULL; interface_t *cfg_if = NULL; char *hostname = NULL; char *diskname, *diskdevice; char *dumptype; char *s, *fp; int ch, dup = 0; char *line = *line_p; int line_num = *line_num_p; struct tm *stm; time_t st; char *shost, *sdisk; am_host_t *p; disk_t *dp; assert(filename != NULL); assert(line_num > 0); assert(line != NULL); s = line; ch = *s++; skip_whitespace(s, ch); if(ch == '\0' || ch == '#') return (0); fp = s - 1; skip_non_whitespace(s, ch); s[-1] = '\0'; host = lookup_host(fp); if (host == NULL) { hostname = stralloc(fp); } else { hostname = stralloc(host->hostname); if (strcmp(host->hostname, fp) != 0) { disk_parserror(filename, line_num, "Same host with different case: \"%s\" and \"%s\".", host->hostname, fp); return -1; } } shost = sanitise_filename(hostname); for (p = hostlist; p != NULL; p = p->next) { char *shostp = sanitise_filename(p->hostname); if (strcmp(hostname, p->hostname) && !strcmp(shost, shostp)) { disk_parserror(filename, line_num, "Two hosts are mapping to the same name: \"%s\" and \"%s\"", p->hostname, hostname); return(-1); } else if (strcasecmp(hostname, p->hostname) && match_host(hostname, p->hostname) && match_host(p->hostname, hostname)) { disk_parserror(filename, line_num, _("Duplicate host name: \"%s\" and \"%s\""), p->hostname, hostname); return(-1); } amfree(shostp); } amfree(shost); skip_whitespace(s, ch); if(ch == '\0' || ch == '#') { disk_parserror(filename, line_num, _("disk device name expected")); amfree(hostname); return (-1); } fp = s - 1; skip_quoted_string(s, ch); s[-1] = '\0'; diskname = unquote_string(fp); skip_whitespace(s, ch); if(ch == '\0' || ch == '#') { disk_parserror(filename, line_num, _("disk dumptype expected")); amfree(hostname); amfree(diskname); return (-1); } fp = s - 1; skip_quoted_string(s, ch); s[-1] = '\0'; /* diskdevice */ dumptype = NULL; diskdevice = NULL; if(fp[0] != '{') { dumptype = unquote_string(fp); if ((dtype = lookup_dumptype(dumptype)) == NULL) { diskdevice = dumptype; skip_whitespace(s, ch); if(ch == '\0' || ch == '#') { disk_parserror(filename, line_num, _("disk dumptype '%s' not found"), dumptype); amfree(hostname); amfree(diskdevice); amfree(diskname); return (-1); } fp = s - 1; skip_quoted_string(s, ch); s[-1] = '\0'; if (fp[0] != '{') { dumptype = unquote_string(fp); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -