Debian Codegraph @

src/cache.h

raw symbols SCIP
1 /*
2 * HTTPDirFS - HTTP Directory Filesystem
3 *
4 * Copyright (C) 2020-2026 Fufu Fang <[email protected]>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * In addition, as a special exception, the copyright holders give
20 * permission to link the code of portions of this program with the OpenSSL
21 * library.
22 */
23
24 #ifndef CACHE_H
25 #define CACHE_H
26 /**
27 * \file cache.h
28 * \brief Permanent cache system header
29 */
30
31 #include <pthread.h>
32 #include <semaphore.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <sys/types.h>
36
37 #include "util.h"
38
39 typedef struct Cache Cache;
40 typedef struct Link Link;
41 struct TransferStruct;
42
43
44 typedef struct ActiveDownload {
45 off_t offset;
46 struct TransferStruct *ts;
47 pthread_cond_t cond;
48 /**
49 * \brief Reference count for lifetime management.
50 * \details Starts at 1 when added to cf->active_dls.
51 * Each waiter thread increments the reference count before waiting.
52 * When unlinked from the list in ActiveDownload_remove, the list's
53 * reference is dropped. The structure is freed only when the reference
54 * count reaches 0.
55 */
56 int refcount;
57 int unlinked;
58 struct ActiveDownload *next;
59 } ActiveDownload;
60
61
62 /**
63 * \brief Type definition for a cache segment
64 */
65 typedef uint8_t Seg;
66
67 /**
68 * \brief cache data type in-memory data structure
69 */
70 struct Cache {
71 /** \brief How many times the cache has been opened */
72 int cache_opened;
73
74 /** \brief the FILE pointer for the data file*/
75 FILE *dfp;
76 /** \brief the FILE pointer for the metadata */
77 FILE *mfp;
78 /** \brief the path to the local cache file */
79 char *path;
80 /** \brief the Link associated with this cache data set */
81 Link *link;
82 /** \brief the block size of the data file */
83 int blksz;
84 /** \brief segment array byte count */
85 long segbc;
86 /** \brief the detail of each segment */
87 Seg *seg;
88
89 /** \brief mutex lock for seek operation */
90 pthread_mutex_t seek_lock;
91 /** \brief mutex lock for write operation */
92 pthread_mutex_t w_lock;
93
94 /** \brief semaphore for the background thread */
95 sys_sem_t bgt_sem;
96
97 /** \brief mutex lock for background download progress */
98 pthread_mutex_t dl_lock;
99 /** \brief active downloads list */
100 ActiveDownload *active_dls;
101
102 /** \brief Count of active waiters on download segments */
103 int waiters;
104 /** \brief Condition variable for cache shutdown synchronization */
105 pthread_cond_t shutdown_cond;
106 /** \brief Flag indicating that cache shutdown is in progress */
107 int shutting_down;
108
109 /** \brief Number of background workers */
110 int num_bg_workers;
111 };
112
113 /**
114 * \brief whether the cache system is enabled
115 */
116 extern int CACHE_SYSTEM_INIT;
117
118 /**
119 * \brief The metadata directory
120 */
121 extern char *META_DIR;
122
123 /**
124 * \brief initialise the cache system directories
125 * \details This function basically sets up the following variables:
126 * - META_DIR
127 * - DATA_DIR
128 *
129 * If these directories do not exist, they will be created.
130 * \note Called by parse_arg_list(), verified to be working
131 */
132 void CacheSystem_init(const char *path, int url_supplied);
133
134 /**
135 * \brief clean up the cache system, freeing meta and data directories
136 */
137 void CacheSystem_cleanup(void);
138
139 /**
140 * \brief clear the content of the cache directory
141 */
142 void CacheSystem_clear(void);
143
144 /**
145 * \brief Return the fullpath to the cache directory
146 */
147 char *CacheSystem_get_cache_dir(void);
148
149 /**
150 * \brief Create directories under the cache directory structure, if they do
151 * not already exist
152 * \return
153 * - -1 failed to create metadata directory.
154 * - -2 failed to create data directory.
155 * - -3 failed to create both metadata and data directory.
156 * \note Called by LinkTable_new()
157 */
158 int CacheDir_create(const char *dirn);
159
160 /**
161 * \brief open a cache file set
162 * \note This function is called by fs_open()
163 */
164 Cache *Cache_open(const char *fn);
165
166 /**
167 * \brief Close a cache data structure
168 * \note This function is called by fs_release()
169 */
170 void Cache_close(Cache *cf);
171
172 /**
173 * \brief create a cache file set if it doesn't exist already
174 * \return
175 * - 0, if the cache file already exists, or was created successfully.
176 * - -1, otherwise
177 * \note Called by fs_open()
178 */
179 int Cache_create(const char *path);
180
181 /**
182 * \brief delete a cache file set
183 * \note Called by fs_open()
184 */
185 void Cache_delete(const char *fn);
186
187 /**
188 * \brief Intelligently read from the cache system
189 * \details If the segment does not exist on the local hard disk, download from
190 * the Internet
191 * \param[in] cf the cache in-memory data structure
192 * \param[out] output_buf the output buffer
193 * \param[in] len the requested segment size
194 * \param[in] offset_start the start of the segment
195 * \return the length of the segment the cache system managed to obtain.
196 * \note Called by fs_read(), verified to be working
197 */
198 long Cache_read(Cache *cf, char *output_buf, off_t len, off_t offset_start);
199
200 /**
201 * \brief Searches the active downloads linked list for a matching offset.
202 * \param[in] cf The cache instance.
203 * \param[in] offset The offset to search for.
204 * \return The active download structure if found, otherwise NULL.
205 * \note Must be called while holding cf->dl_lock.
206 */
207 ActiveDownload *ActiveDownload_find(Cache *cf, off_t offset);
208
209 /**
210 * \brief Decrements the reference count of the ActiveDownload tracker.
211 * \details Destroys the condition variable and frees the structure when the
212 * reference count reaches 0.
213 * \param[in] ad The ActiveDownload tracker to unref.
214 * \note Must be called while holding cf->dl_lock.
215 */
216 void ActiveDownload_unref(ActiveDownload *ad);
217 #endif