Imported Upstream version 3.3.3
[debian/amanda] / server-src / list_dir.c
1 /*
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.
5  * All Rights Reserved.
6  *
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.
16  *
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.
23  *
24  * Authors: the Amanda Development Team.  Its members are listed in a
25  * file named AUTHORS, in the root directory of this distribution.
26  */
27 /* $Id: list_dir.c,v 1.19 2006/07/05 11:22:49 martinea Exp $
28  *
29  * manage directory listings from index files
30  */
31
32 #include "amanda.h"
33 #include "disk_history.h"
34 #include "list_dir.h"
35
36 DIR_ITEM *dir_list = NULL; /* first dir entry */
37 DIR_ITEM *dir_last = NULL; /* last dir entry  */
38 DIR_ITEM *cur_list = NULL; /* current dir entry,for speeding up search */
39
40 DIR_ITEM *
41 get_dir_list(void)
42 {
43     return dir_list;
44 }
45
46
47 void
48 clear_dir_list(void)
49 {
50     DIR_ITEM *this;
51
52     if (dir_list == NULL)
53         return;
54
55     do
56     {
57         this = dir_list;
58         dir_list = dir_list->next;
59         amfree(this->path);
60         amfree(this);
61     } while (dir_list != NULL);
62
63     dir_last = NULL;
64     cur_list = NULL;
65 }
66
67 /* add item to list if path not already on list                     */
68 /* Since this function is almost called with increasing path order, */
69 /* we keep a pointer on the last element added (cur_list), this     */
70 /* reduce the time for the search of a path.                        */
71 /* It's true because the output of the index file is sorted         */
72 /* Maybe it could be more efficient if the index was sorted when    */
73 /* it is generated                                                  */
74
75 int
76 add_dir_list_item(
77     DUMP_ITEM * dump,
78     const char *path)
79 {
80     DIR_ITEM *cur;
81
82     if (dir_list == NULL)
83     {
84         dir_list = (DIR_ITEM *)alloc(SIZEOF(DIR_ITEM));
85         dir_list->next = NULL;
86         dir_list->dump = dump;
87         dir_list->path = stralloc(path);
88         dir_last=dir_list;
89         cur_list=dir_list;
90         return 0; /* added */
91     }
92
93     if (strcmp(path, "/") != 0 &&
94         strcmp(path, dir_last->path) == 0) {
95         return 0; /* found */
96     }
97
98     /* add at head of list */
99     if(strcmp(path,dir_list->path) < 0)
100     {
101         cur_list = (DIR_ITEM *)alloc(SIZEOF(DIR_ITEM));
102         cur_list->next = dir_list;
103         cur_list->dump = dump;
104         cur_list->path = stralloc(path);
105         dir_list = cur_list;
106         return 0; /* added */
107     }
108
109     /* if smaller than last path */
110     if(strcmp(path,dir_last->path) < 0)
111     {
112         if(cur_list==NULL)
113             cur_list=dir_list;
114
115         /* reset cur_list if path is smaller than cur_list->path */
116         if(strcmp(path,cur_list->path) < 0)
117             cur_list=dir_list;
118
119         if(strcmp(path,cur_list->path) == 0)
120             return 0; /* found */
121
122         while (cur_list->next!=NULL && (strcmp(path,cur_list->next->path) > 0))
123         {
124             cur_list=cur_list->next;
125         }
126
127         if (cur_list->next && strcmp(path, cur_list->next->path) == 0)
128         {
129             cur_list=cur_list->next;
130             return 0; /* found */
131         }
132
133         /* add at cur_list */
134         cur = (DIR_ITEM *)alloc(SIZEOF(DIR_ITEM));
135         cur->next = cur_list->next;
136         cur->dump = dump;
137         cur->path = stralloc(path);
138         cur_list->next=cur;
139         cur_list=cur;
140         return 0; /* added */
141     }
142     else /* add at end of list */
143     {
144         dir_last->next = (DIR_ITEM *)alloc(SIZEOF(DIR_ITEM));
145         dir_last=dir_last->next;
146         dir_last->next = NULL;
147         dir_last->dump = dump;
148         dir_last->path = stralloc(path);
149         return 0; /* added */
150     }
151 }