]> git.sesse.net Git - betaftpd/blob - dcache.c
Moved most of the dcache stuff into its own file. Updated Makefile.in accordingly.
[betaftpd] / dcache.c
1 /*  dcache.c: BetaFTPD directory listing cache
2     Copyright (C) 2000 Steinar H. Gunderson
3
4     This program is is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License, version 2 if the
6     License as published by the Free Software Foundation.
7
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12
13     You should have received a copy of the GNU General Public License
14     along with this program; if not, write to the Free Software
15     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 */
17
18 #if HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21
22 #if HAVE_TIME_H
23 #include <time.h>
24 #endif
25
26 #if HAVE_SYS_TIME_H
27 #include <sys/time.h>
28 #endif
29
30 #if HAVE_PWD_H
31 #include <pwd.h>
32 #endif
33
34 #if HAVE_SYS_TYPES_H
35 #include <sys/types.h>
36 #endif
37
38 #if HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
40 #endif
41
42 #if HAVE_SYS_STAT_H
43 #include <sys/stat.h>
44 #endif
45
46 #if HAVE_NETINET_IN_H
47 #include <netinet/in.h>
48 #endif
49
50 #if HAVE_STDLIB_H
51 #include <stdlib.h>
52 #endif
53
54 #if HAVE_STRING_H
55 #include <string.h>
56 #endif
57
58 #if HAVE_STRINGS_H
59 #include <strings.h>
60 #endif
61
62 #include <ftpd.h>
63
64 #if WANT_DCACHE
65 #include <dcache.h>
66
67 extern struct dcache *first_dcache;
68
69 /*
70  * alloc_new_dcache():
71  *              Allocates a new directory cache entry (type `struct dcache'),
72  *              and adds it to the linked list.
73  */
74 struct dcache *alloc_new_dcache()
75 {
76         struct dcache *d = (struct dcache *)(malloc(sizeof(struct dcache)));
77
78         if (d == NULL) return d;
79
80         d->use_count = 0;
81         d->last_used = 0;
82         strcpy(d->dir_name, "");
83         d->dir_data = NULL;
84
85         add_to_linked_list((struct list_element *)first_dcache,
86                            (struct list_element *)d);
87
88         return d;
89 }
90
91 /*
92  * time_out_dcache():
93  *              Time out expired directory listing cache entries.
94  *              Uses much of the same code as time_out_sockets().
95  */
96 void time_out_dcache()
97 {
98         struct dcache *d = NULL, *next = first_dcache->next_dcache;
99         time_t now = time(NULL);
100
101         /* run through the linked list */
102         while (next != NULL) {
103                 d = next;
104                 next = d->next_dcache;
105
106                 if (d->use_count == 0 && (now - d->last_used > 900)) {
107                         destroy_dcache(d);
108                 }
109         }
110 }
111
112 /*
113  * populate_dcache():
114  *              Add a new entry in the dcache, using information from
115  *              the given file transfer object, and the current directory
116  *              (cwd).
117  */
118 void populate_dcache(struct ftran * const f, const char * const cwd,
119                      const char * const pattern, const struct list_options * const lo)
120 {
121         struct stat buf;
122         struct dcache *d = alloc_new_dcache();
123         if (d != NULL && stat(cwd, &buf) > -1) {
124                 d->use_count++;
125                 f->dir_cache = d;
126                 d->dir_data = f->file_data;
127                 d->dir_size = f->size;
128                 d->generated = buf.st_mtime;
129
130                 strcpy(d->dir_name, cwd);
131                 strncpy(d->pattern, pattern, 255);
132                 d->pattern[255] = 0;
133                 d->lo = *lo;
134         }
135 }
136 #endif /* WANT_DCACHE */