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