aboutsummaryrefslogtreecommitdiffstats
path: root/cache.c
diff options
context:
space:
mode:
Diffstat (limited to 'cache.c')
-rw-r--r--cache.c28
1 files changed, 26 insertions, 2 deletions
diff --git a/cache.c b/cache.c
index fcd461f..801e63f 100644
--- a/cache.c
+++ b/cache.c
@@ -13,6 +13,9 @@
13 * 13 *
14 */ 14 */
15 15
16#ifdef HAVE_LINUX_SENDFILE
17#include <sys/sendfile.h>
18#endif
16#include "cgit.h" 19#include "cgit.h"
17#include "cache.h" 20#include "cache.h"
18#include "html.h" 21#include "html.h"
@@ -30,7 +33,6 @@ struct cache_slot {
30 const char *lock_name; 33 const char *lock_name;
31 int match; 34 int match;
32 struct stat cache_st; 35 struct stat cache_st;
33 struct stat lock_st;
34 int bufsize; 36 int bufsize;
35 char buf[CACHE_BUFSIZE]; 37 char buf[CACHE_BUFSIZE];
36}; 38};
@@ -81,6 +83,23 @@ static int close_slot(struct cache_slot *slot)
81/* Print the content of the active cache slot (but skip the key). */ 83/* Print the content of the active cache slot (but skip the key). */
82static int print_slot(struct cache_slot *slot) 84static int print_slot(struct cache_slot *slot)
83{ 85{
86#ifdef HAVE_LINUX_SENDFILE
87 off_t start_off;
88 int ret;
89
90 start_off = slot->keylen + 1;
91
92 do {
93 ret = sendfile(STDOUT_FILENO, slot->cache_fd, &start_off,
94 slot->cache_st.st_size - start_off);
95 if (ret < 0) {
96 if (errno == EAGAIN || errno == EINTR)
97 continue;
98 return errno;
99 }
100 return 0;
101 } while (1);
102#else
84 ssize_t i, j; 103 ssize_t i, j;
85 104
86 i = lseek(slot->cache_fd, slot->keylen + 1, SEEK_SET); 105 i = lseek(slot->cache_fd, slot->keylen + 1, SEEK_SET);
@@ -97,6 +116,7 @@ static int print_slot(struct cache_slot *slot)
97 return errno; 116 return errno;
98 else 117 else
99 return 0; 118 return 0;
119#endif
100} 120}
101 121
102/* Check if the slot has expired */ 122/* Check if the slot has expired */
@@ -188,6 +208,10 @@ static int fill_slot(struct cache_slot *slot)
188 /* Generate cache content */ 208 /* Generate cache content */
189 slot->fn(); 209 slot->fn();
190 210
211 /* update stat info */
212 if (fstat(slot->lock_fd, &slot->cache_st))
213 return errno;
214
191 /* Restore stdout */ 215 /* Restore stdout */
192 if (dup2(tmp, STDOUT_FILENO) == -1) 216 if (dup2(tmp, STDOUT_FILENO) == -1)
193 return errno; 217 return errno;
@@ -319,7 +343,7 @@ int cache_process(int size, const char *path, const char *key, int ttl,
319 int result; 343 int result;
320 344
321 /* If the cache is disabled, just generate the content */ 345 /* If the cache is disabled, just generate the content */
322 if (size <= 0) { 346 if (size <= 0 || ttl == 0) {
323 fn(); 347 fn();
324 return 0; 348 return 0;
325 } 349 }