Include <sys/types.h> and fix compilation on an old PPC/Debian system
[elinks/images.git] / src / cache / cache.h
blob584e07f09e8759c312e7dab92383238074102844
1 #ifndef EL__CACHE_CACHE_H
2 #define EL__CACHE_CACHE_H
4 #include "main/object.h"
5 #include "util/lists.h"
6 #include "util/time.h"
8 struct listbox_item;
9 struct uri;
11 /* This enum describes the level of caching of certain cache entry. That is,
12 * under what conditions shall it be reloaded, if ever. The one with lowest
13 * value is most agressively cached, however the cache is most reluctant to
14 * cache the one with highest value. */
15 /* TODO: Invert order to be more intuitive. But be careful, many tests rely on
16 * current order. --Miciah,Zas */
17 enum cache_mode {
18 CACHE_MODE_INCREMENT = -1,
19 CACHE_MODE_ALWAYS,
20 CACHE_MODE_NORMAL,
21 CACHE_MODE_CHECK_IF_MODIFIED,
22 CACHE_MODE_FORCE_RELOAD,
23 CACHE_MODE_NEVER,
26 struct cache_entry {
27 OBJECT_HEAD(struct cache_entry);
29 /* Items in this list are ALLOCATED IN A NON-STANDARD WAY! Thus if you
30 * are gonna mess with them (you shouldn't), you need to use the
31 * mmap suite. */
32 struct list_head frag; /* -> struct fragment */
34 struct uri *uri; /* Identifier for the cached data */
35 struct uri *proxy_uri; /* Proxy identifier or same as @uri */
36 struct uri *redirect; /* Location we were redirected to */
38 unsigned char *head; /* The protocol header */
39 unsigned char *content_type; /* MIME type: <type> "/" <subtype> */
40 unsigned char *last_modified; /* Latest modification date */
41 unsigned char *etag; /* ETag value from the HTTP header */
42 unsigned char *ssl_info; /* SSL ciphers used during transfer */
43 unsigned char *encoding_info; /* Encoding used during transfer */
45 unsigned int id; /* Change each time entry is modified. */
47 off_t length; /* The expected and complete size */
48 off_t data_size; /* The actual size of all fragments */
50 struct listbox_item *box_item; /* Dialog data for cache manager */
52 timeval_T max_age; /* Expiration time */
54 unsigned int expire:1; /* Whether to honour max_age */
55 unsigned int preformatted:1; /* Has content been preformatted? */
56 unsigned int redirect_get:1; /* Follow redirect using get method? */
57 unsigned int incomplete:1; /* Has all data been downloaded? */
58 unsigned int valid:1; /* Is cache entry usable? */
60 /* This is a mark for internal workings of garbage_collection(), whether
61 * the cache_entry should be busted or not. You are not likely to see
62 * an entry with this set to 1 in wild nature ;-). */
63 unsigned int gc_target:1; /* The GC touch of death */
65 enum cache_mode cache_mode; /* Reload condition */
68 struct fragment {
69 LIST_HEAD(struct fragment);
71 off_t offset;
72 off_t length;
73 off_t real_length;
74 unsigned char data[1]; /* Must be last */
78 /* Searches the cache for an entry matching the URI. Returns NULL if no one
79 * matches. */
80 struct cache_entry *find_in_cache(struct uri *uri);
82 /* Searches the cache for a matching entry else a new one is added. Returns
83 * NULL if allocation fails. */
84 struct cache_entry *get_cache_entry(struct uri *uri);
86 /* Searches the cache for a matching entry and checks if it is still valid and
87 * usable. Returns NULL if the @cache_mode suggests to reload it again. */
88 struct cache_entry *get_validated_cache_entry(struct uri *uri, enum cache_mode cache_mode);
90 /* Checks if a dangling cache entry pointer is still valid. */
91 int cache_entry_is_valid(struct cache_entry *cached);
93 /* Follow all redirects and return the resulting cache entry or NULL if there
94 * are missing redirects. */
95 struct cache_entry *follow_cached_redirects(struct cache_entry *cached);
97 /* Works like find_in_cache(), but will follow cached redirects using
98 * follow_cached_redirects(). */
99 struct cache_entry *get_redirected_cache_entry(struct uri *uri);
101 /* Add a fragment to the @cached object at the given @offset containing @length
102 * bytes from the @data pointer. */
103 /* Returns -1 upon error,
104 * 1 if cache entry was enlarged,
105 * 0 if only old data were overwritten. */
106 int add_fragment(struct cache_entry *cached, off_t offset,
107 const unsigned char *data, ssize_t length);
109 /* Defragments the cache entry and returns the resulting fragment containing the
110 * complete source of all currently downloaded fragments. Returns NULL if
111 * validation of the fragments fails. */
112 struct fragment *get_cache_fragment(struct cache_entry *cached);
114 /* Should be called when creation of a new cache has been completed. Most
115 * importantly, it will updates cached->incomplete. */
116 void normalize_cache_entry(struct cache_entry *cached, off_t length);
118 void free_entry_to(struct cache_entry *cached, off_t offset);
119 void delete_entry_content(struct cache_entry *cached);
120 void delete_cache_entry(struct cache_entry *cached);
122 /* Sets up the cache entry to redirect to a new location
123 * @location decides where to redirect to by resolving it relative to the
124 * entry's URI.
125 * @get controls the method should be used when handling the redirect.
126 * @incomplete will become the new value of the incomplete member if it
127 * is >= 0.
128 * Returns the URI being redirected to or NULL if allocation failed.
130 struct uri *
131 redirect_cache(struct cache_entry *cached, unsigned char *location,
132 int get, int incomplete);
134 /* The garbage collector trigger. If @whole is zero, remove unused cache
135 * entries which are bigger than the cache size limit set by user. For @zero
136 * being one, remove all unused cache entries. */
137 void garbage_collection(int whole);
139 /* Used by the resource and memory info dialogs for getting information about
140 * the cache. */
141 unsigned longlong get_cache_size(void);
142 int get_cache_entry_count(void);
143 int get_cache_entry_used_count(void);
144 int get_cache_entry_loading_count(void);
146 #endif