File: cache.c

package info (click to toggle)
yafc 1.1.1.dfsg.1-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,248 kB
  • ctags: 1,679
  • sloc: ansic: 19,338; sh: 10,365; makefile: 155; perl: 38; ruby: 33
file content (153 lines) | stat: -rw-r--r-- 3,283 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* $Id: cache.c,v 1.6 2003/07/12 10:25:41 mhe Exp $
 *
 * cache.c -- remote directory cache
 *
 * Yet Another FTP Client
 * Copyright (C) 1998-2001, Martin Hedenfalk <mhe@stacken.kth.se>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. See COPYING for more details.
 */

#include "syshdr.h"

#include "ftp.h"
#include "xmalloc.h"
#include "strq.h"
#include "gvars.h"

void ftp_cache_list_contents(void)
{
	listitem *li = ftp->cache->first;

	ftp_cache_flush();

	printf("Directory cache contents: (%u items)\n",
		   list_numitem(ftp->cache));

	while(li) {
		printf("%s\n", ((rdirectory *)li->data)->path);
		li = li->next;
	}
}

/* marks the directory PATH to be flushed in the
 * next call to ftp_cache_flush()
 */
void ftp_cache_flush_mark(const char *path)
{
	char *p = ftp_path_absolute(path);
	stripslash(p);

	if(list_search(ftp->dirs_to_flush, (listsearchfunc)strcmp, p) != 0)
		/* already in the flush list */
		return;

	list_additem(ftp->dirs_to_flush, p);
	ftp_trace("marked directory '%s' for flush\n", p);
}

/* marks the directory *containing* PATH to be flushed in
 * the next call to ftp_cache_flush()
 */
void ftp_cache_flush_mark_for(const char *path)
{
	char *e = base_dir_xptr(path);
	if(!e) {
		free(e);
		e = xstrdup(ftp->curdir);
	}
	ftp_cache_flush_mark(e);
	free(e);
}

static int cache_search(rdirectory *rdir, const char *arg)
{
	return strcmp(rdir->path, arg);
}

/* flushes directories marked by ftp_cache_flush_mark*()
 */
void ftp_cache_flush(void)
{
	listitem *e;
	listitem *li = ftp->dirs_to_flush->first;

	while(li) {
		char *dir = (char *)li->data;
		e = list_search(ftp->cache, (listsearchfunc)cache_search, dir);
		if(e) {
			ftp_trace("flushed directory '%s'\n", dir);
			list_delitem(ftp->cache, e);
		} else
			ftp_trace("error flushing directory '%s' (not cached)\n", dir);

		li = li->next;
	}

	list_clear(ftp->dirs_to_flush);
}

void ftp_cache_clear(void)
{
	list_clear(ftp->dirs_to_flush);
	list_clear(ftp->cache);
	ftp_trace("clear whole directory cache\n");
}

/* PATH is NOT quoted */
/* returns a directory from the cache, or 0 if not found
 */
rdirectory *ftp_cache_get_directory(const char *path)
{
	listitem *li;
	char *dir_to_search_for;

	if(path)
		dir_to_search_for = ftp_path_absolute(path);
	else
		dir_to_search_for = xstrdup(ftp->curdir);

	li = list_search(ftp->cache, (listsearchfunc)cache_search,
					 dir_to_search_for);

	if(li && gvCacheTimeout &&
	   ((rdirectory *)li->data)->timestamp + gvCacheTimeout <= time(0))
	{
		/* directory cache has timed out */

		ftp_trace("Directory cache for '%s' has timed out\n",
				  dir_to_search_for);
		ftp_cache_flush_mark(dir_to_search_for);
	}
	free(dir_to_search_for);

	if(!li)
		return 0;

	return (rdirectory *)li->data;
}

/* returns a file from the cache, or 0 if not found
 */
rfile *ftp_cache_get_file(const char *path)
{
	rdirectory *rdir;
	rfile *f;
	char *dir;

	if(!path)
		return 0;

	dir = base_dir_xptr(path);
	rdir = ftp_cache_get_directory(dir);
	free(dir);

	if(rdir) {
		f = rdir_get_file(rdir, base_name_ptr(path));
		return f;
	}
	return 0;
}