cache.c (view raw)
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
11const int NOLOCK = -1;
12
13int cache_exist(struct cacheitem *item)
14{
15 if (stat(item->name, &item->st)) {
16 item->st.st_mtime = 0;
17 return 0;
18 }
19 return 1;
20}
21
22int cache_create_dirs()
23{
24 char *path;
25
26 path = fmt("%s", cgit_cache_root);
27 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
28 return 0;
29
30 if (!cgit_query_repo)
31 return 0;
32
33 path = fmt("%s/%s", cgit_cache_root, cgit_query_repo);
34 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
35 return 0;
36
37 if (cgit_query_page) {
38 path = fmt("%s/%s/%s", cgit_cache_root, cgit_query_repo,
39 cgit_query_page);
40 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
41 return 0;
42 }
43 return 1;
44}
45
46int cache_refill_overdue(const char *lockfile)
47{
48 struct stat st;
49
50 if (stat(lockfile, &st))
51 return 0;
52 else
53 return (time(NULL) - st.st_mtime > cgit_cache_max_create_time);
54}
55
56int cache_lock(struct cacheitem *item)
57{
58 int i = 0;
59 char *lockfile = xstrdup(fmt("%s.lock", item->name));
60
61 top:
62 if (++i > cgit_max_lock_attempts)
63 die("cache_lock: unable to lock %s: %s",
64 item->name, strerror(errno));
65
66 item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
67
68 if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs())
69 goto top;
70
71 if (item->fd == NOLOCK && errno == EEXIST &&
72 cache_refill_overdue(lockfile) && !unlink(lockfile))
73 goto top;
74
75 free(lockfile);
76 return (item->fd > 0);
77}
78
79int cache_unlock(struct cacheitem *item)
80{
81 close(item->fd);
82 return (rename(fmt("%s.lock", item->name), item->name) == 0);
83}
84
85int cache_cancel_lock(struct cacheitem *item)
86{
87 return (unlink(fmt("%s.lock", item->name)) == 0);
88}
89
90int cache_expired(struct cacheitem *item)
91{
92 if (item->ttl < 0)
93 return 0;
94 return item->st.st_mtime + item->ttl * 60 < time(NULL);
95}