c38a30639b6d84876d7c59075ff33d4df2b47eeb
[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.17 2002/03/24 19:25:51 jrjackson 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 *get_dir_list()
40 {
41     return dir_list;
42 }
43
44
45 void clear_dir_list P((void))
46 {
47     DIR_ITEM *this;
48
49     if (dir_list == NULL)
50         return;
51
52     do
53     {
54         this = dir_list;
55         dir_list = dir_list->next;
56         amfree(this->path);
57         amfree(this);
58     } while (dir_list != NULL);
59
60     dir_last = NULL;
61     cur_list = NULL;
62 }
63
64 /* add item to list if path not already on list                     */
65 /* Since this function is almost called with increasing path order, */
66 /* we keep a pointer on the last element added (cur_list), this     */
67 /* reduce the time for the search of a path.                        */
68 /* It's true because the output of the index file is sorted         */
69 /* Maybe it could be more efficient if the index was sorted when    */
70 /* it is generated                                                  */
71 int add_dir_list_item(dump, path)
72 DUMP_ITEM *dump;
73 char *path;
74 {
75     DIR_ITEM *cur;
76
77     if (dir_list == NULL)
78     {
79         dir_list = (DIR_ITEM *)alloc(sizeof(DIR_ITEM));
80         dir_list->next = NULL;
81         dir_list->dump = dump;
82         dir_list->path = stralloc(path);
83         dir_last=dir_list;
84         cur_list=dir_list;
85         return 0; /* added */
86     }
87
88     if(strcmp(path,dir_last->path) == 0)
89         return 0; /* found */
90
91     /* add at head of list */
92     if(strcmp(path,dir_list->path) < 0)
93     {
94         cur_list = (DIR_ITEM *)alloc(sizeof(DIR_ITEM));
95         cur_list->next = dir_list;
96         cur_list->dump = dump;
97         cur_list->path = stralloc(path);
98         dir_list = cur_list;
99         return 0; /* added */
100     }
101
102     /* if smaller than last path */
103     if(strcmp(path,dir_last->path) < 0)
104     {
105         if(cur_list==NULL)
106             cur_list=dir_list;
107
108         /* reset cur_list if path is smaller than cur_list->path */
109         if(strcmp(path,cur_list->path) < 0)
110             cur_list=dir_list;
111
112         if(strcmp(path,cur_list->path) == 0)
113             return 0; /* found */
114
115         while (cur_list->next!=NULL && (strcmp(path,cur_list->next->path) > 0))
116         {
117             cur_list=cur_list->next;
118         }
119
120         if(strcmp(path,cur_list->next->path) == 0)
121         {
122             cur_list=cur_list->next;
123             return 0; /* found */
124         }
125
126         /* add at cur_list */
127         cur = (DIR_ITEM *)alloc(sizeof(DIR_ITEM));
128         cur->next = cur_list->next;
129         cur->dump = dump;
130         cur->path = stralloc(path);
131         cur_list->next=cur;
132         cur_list=cur;
133         return 0; /* added */
134     }
135     else /* add at end of list */
136     {
137         dir_last->next = (DIR_ITEM *)alloc(sizeof(DIR_ITEM));
138         dir_last=dir_last->next;
139         dir_last->next = NULL;
140         dir_last->dump = dump;
141         dir_last->path = stralloc(path);
142         return 0; /* added */
143     }
144 }