]> gitweb.ps.run Git - ps-cgit/blob - cache.c
Enable url=value querystring parameter
[ps-cgit] / cache.c
1 /* cache.c: cache management
2  *
3  * Copyright (C) 2006 Lars Hjemli
4  *
5  * Licensed under GNU General Public License v2
6  *   (see COPYING for full license text)
7  */
8
9 #include "cgit.h"
10
11 const int NOLOCK = -1;
12
13 char *cache_safe_filename(const char *unsafe)
14 {
15         static char buf[PATH_MAX];
16         char *s = buf;
17         char c;
18
19         while(unsafe && (c = *unsafe++) != 0) {
20                 if (c == '/' || c == ' ' || c == '&' || c == '|' || 
21                     c == '>' || c == '<' || c == '.')
22                         c = '_';
23                 *s++ = c;
24         }
25         *s = '\0';
26         return buf;
27 }
28
29 int cache_exist(struct cacheitem *item)
30 {
31         if (stat(item->name, &item->st)) {
32                 item->st.st_mtime = 0;
33                 return 0;
34         }
35         return 1;
36 }
37
38 int cache_create_dirs()
39 {
40         char *path;
41
42         path = fmt("%s", cgit_cache_root);
43         if (mkdir(path, S_IRWXU) && errno!=EEXIST)
44                 return 0;
45
46         if (!cgit_repo)
47                 return 0;
48
49         path = fmt("%s/%s", cgit_cache_root,
50                    cache_safe_filename(cgit_repo->url));
51
52         if (mkdir(path, S_IRWXU) && errno!=EEXIST)
53                 return 0;
54
55         if (cgit_query_page) {
56                 path = fmt("%s/%s/%s", cgit_cache_root,
57                            cache_safe_filename(cgit_repo->url),
58                            cgit_query_page);
59                 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
60                         return 0;
61         }
62         return 1;
63 }
64
65 int cache_refill_overdue(const char *lockfile)
66 {
67         struct stat st;
68
69         if (stat(lockfile, &st))
70                 return 0;
71         else
72                 return (time(NULL) - st.st_mtime > cgit_cache_max_create_time);
73 }
74
75 int cache_lock(struct cacheitem *item)
76 {
77         int i = 0;
78         char *lockfile = xstrdup(fmt("%s.lock", item->name));
79
80  top:
81         if (++i > cgit_max_lock_attempts)
82                 die("cache_lock: unable to lock %s: %s",
83                     item->name, strerror(errno));
84
85         item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
86
87         if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs())
88                 goto top;
89
90         if (item->fd == NOLOCK && errno == EEXIST &&
91             cache_refill_overdue(lockfile) && !unlink(lockfile))
92                         goto top;
93
94         free(lockfile);
95         return (item->fd > 0);
96 }
97
98 int cache_unlock(struct cacheitem *item)
99 {
100         close(item->fd);
101         return (rename(fmt("%s.lock", item->name), item->name) == 0);
102 }
103
104 int cache_cancel_lock(struct cacheitem *item)
105 {
106         return (unlink(fmt("%s.lock", item->name)) == 0);
107 }
108
109 int cache_expired(struct cacheitem *item)
110 {
111         if (item->ttl < 0)
112                 return 0;
113         return item->st.st_mtime + item->ttl * 60 < time(NULL);
114 }