author | Lars Hjemli <hjemli@gmail.com> | 2008-03-27 08:22:13 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-03-27 08:22:13 (UTC) |
commit | ee4056bd2c902a12dea67874368863fe60ea5a5f (patch) (unidiff) | |
tree | d54da54ace7b8999cf8785d877a0a9cad5262a0c | |
parent | dc3282f0baa14949439593729a45fbe143e3622c (diff) | |
download | cgit-ee4056bd2c902a12dea67874368863fe60ea5a5f.zip cgit-ee4056bd2c902a12dea67874368863fe60ea5a5f.tar.gz cgit-ee4056bd2c902a12dea67874368863fe60ea5a5f.tar.bz2 |
Add cache.h
The functions found in cache.c are only used by cgit.c, so there's no
point in rebuilding all object files when the cache interface is changed.
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | cache.c | 1 | ||||
-rw-r--r-- | cache.h | 23 | ||||
-rw-r--r-- | cgit.c | 1 | ||||
-rw-r--r-- | cgit.h | 14 |
4 files changed, 25 insertions, 14 deletions
@@ -1,119 +1,120 @@ | |||
1 | /* cache.c: cache management | 1 | /* cache.c: cache management |
2 | * | 2 | * |
3 | * Copyright (C) 2006 Lars Hjemli | 3 | * Copyright (C) 2006 Lars Hjemli |
4 | * | 4 | * |
5 | * Licensed under GNU General Public License v2 | 5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include "cgit.h" | 9 | #include "cgit.h" |
10 | #include "cache.h" | ||
10 | 11 | ||
11 | const int NOLOCK = -1; | 12 | const int NOLOCK = -1; |
12 | 13 | ||
13 | char *cache_safe_filename(const char *unsafe) | 14 | char *cache_safe_filename(const char *unsafe) |
14 | { | 15 | { |
15 | static char buf[4][PATH_MAX]; | 16 | static char buf[4][PATH_MAX]; |
16 | static int bufidx; | 17 | static int bufidx; |
17 | char *s; | 18 | char *s; |
18 | char c; | 19 | char c; |
19 | 20 | ||
20 | bufidx++; | 21 | bufidx++; |
21 | bufidx &= 3; | 22 | bufidx &= 3; |
22 | s = buf[bufidx]; | 23 | s = buf[bufidx]; |
23 | 24 | ||
24 | while(unsafe && (c = *unsafe++) != 0) { | 25 | while(unsafe && (c = *unsafe++) != 0) { |
25 | if (c == '/' || c == ' ' || c == '&' || c == '|' || | 26 | if (c == '/' || c == ' ' || c == '&' || c == '|' || |
26 | c == '>' || c == '<' || c == '.') | 27 | c == '>' || c == '<' || c == '.') |
27 | c = '_'; | 28 | c = '_'; |
28 | *s++ = c; | 29 | *s++ = c; |
29 | } | 30 | } |
30 | *s = '\0'; | 31 | *s = '\0'; |
31 | return buf[bufidx]; | 32 | return buf[bufidx]; |
32 | } | 33 | } |
33 | 34 | ||
34 | int cache_exist(struct cacheitem *item) | 35 | int cache_exist(struct cacheitem *item) |
35 | { | 36 | { |
36 | if (stat(item->name, &item->st)) { | 37 | if (stat(item->name, &item->st)) { |
37 | item->st.st_mtime = 0; | 38 | item->st.st_mtime = 0; |
38 | return 0; | 39 | return 0; |
39 | } | 40 | } |
40 | return 1; | 41 | return 1; |
41 | } | 42 | } |
42 | 43 | ||
43 | int cache_create_dirs() | 44 | int cache_create_dirs() |
44 | { | 45 | { |
45 | char *path; | 46 | char *path; |
46 | 47 | ||
47 | path = fmt("%s", ctx.cfg.cache_root); | 48 | path = fmt("%s", ctx.cfg.cache_root); |
48 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 49 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
49 | return 0; | 50 | return 0; |
50 | 51 | ||
51 | if (!ctx.repo) | 52 | if (!ctx.repo) |
52 | return 0; | 53 | return 0; |
53 | 54 | ||
54 | path = fmt("%s/%s", ctx.cfg.cache_root, | 55 | path = fmt("%s/%s", ctx.cfg.cache_root, |
55 | cache_safe_filename(ctx.repo->url)); | 56 | cache_safe_filename(ctx.repo->url)); |
56 | 57 | ||
57 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 58 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
58 | return 0; | 59 | return 0; |
59 | 60 | ||
60 | if (ctx.qry.page) { | 61 | if (ctx.qry.page) { |
61 | path = fmt("%s/%s/%s", ctx.cfg.cache_root, | 62 | path = fmt("%s/%s/%s", ctx.cfg.cache_root, |
62 | cache_safe_filename(ctx.repo->url), | 63 | cache_safe_filename(ctx.repo->url), |
63 | ctx.qry.page); | 64 | ctx.qry.page); |
64 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 65 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
65 | return 0; | 66 | return 0; |
66 | } | 67 | } |
67 | return 1; | 68 | return 1; |
68 | } | 69 | } |
69 | 70 | ||
70 | int cache_refill_overdue(const char *lockfile) | 71 | int cache_refill_overdue(const char *lockfile) |
71 | { | 72 | { |
72 | struct stat st; | 73 | struct stat st; |
73 | 74 | ||
74 | if (stat(lockfile, &st)) | 75 | if (stat(lockfile, &st)) |
75 | return 0; | 76 | return 0; |
76 | else | 77 | else |
77 | return (time(NULL) - st.st_mtime > ctx.cfg.cache_max_create_time); | 78 | return (time(NULL) - st.st_mtime > ctx.cfg.cache_max_create_time); |
78 | } | 79 | } |
79 | 80 | ||
80 | int cache_lock(struct cacheitem *item) | 81 | int cache_lock(struct cacheitem *item) |
81 | { | 82 | { |
82 | int i = 0; | 83 | int i = 0; |
83 | char *lockfile = xstrdup(fmt("%s.lock", item->name)); | 84 | char *lockfile = xstrdup(fmt("%s.lock", item->name)); |
84 | 85 | ||
85 | top: | 86 | top: |
86 | if (++i > ctx.cfg.max_lock_attempts) | 87 | if (++i > ctx.cfg.max_lock_attempts) |
87 | die("cache_lock: unable to lock %s: %s", | 88 | die("cache_lock: unable to lock %s: %s", |
88 | item->name, strerror(errno)); | 89 | item->name, strerror(errno)); |
89 | 90 | ||
90 | item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); | 91 | item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); |
91 | 92 | ||
92 | if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) | 93 | if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) |
93 | goto top; | 94 | goto top; |
94 | 95 | ||
95 | if (item->fd == NOLOCK && errno == EEXIST && | 96 | if (item->fd == NOLOCK && errno == EEXIST && |
96 | cache_refill_overdue(lockfile) && !unlink(lockfile)) | 97 | cache_refill_overdue(lockfile) && !unlink(lockfile)) |
97 | goto top; | 98 | goto top; |
98 | 99 | ||
99 | free(lockfile); | 100 | free(lockfile); |
100 | return (item->fd > 0); | 101 | return (item->fd > 0); |
101 | } | 102 | } |
102 | 103 | ||
103 | int cache_unlock(struct cacheitem *item) | 104 | int cache_unlock(struct cacheitem *item) |
104 | { | 105 | { |
105 | close(item->fd); | 106 | close(item->fd); |
106 | return (rename(fmt("%s.lock", item->name), item->name) == 0); | 107 | return (rename(fmt("%s.lock", item->name), item->name) == 0); |
107 | } | 108 | } |
108 | 109 | ||
109 | int cache_cancel_lock(struct cacheitem *item) | 110 | int cache_cancel_lock(struct cacheitem *item) |
110 | { | 111 | { |
111 | return (unlink(fmt("%s.lock", item->name)) == 0); | 112 | return (unlink(fmt("%s.lock", item->name)) == 0); |
112 | } | 113 | } |
113 | 114 | ||
114 | int cache_expired(struct cacheitem *item) | 115 | int cache_expired(struct cacheitem *item) |
115 | { | 116 | { |
116 | if (item->ttl < 0) | 117 | if (item->ttl < 0) |
117 | return 0; | 118 | return 0; |
118 | return item->st.st_mtime + item->ttl * 60 < time(NULL); | 119 | return item->st.st_mtime + item->ttl * 60 < time(NULL); |
119 | } | 120 | } |
@@ -0,0 +1,23 @@ | |||
1 | /* | ||
2 | * Since git has it's own cache.h which we include, | ||
3 | * lets test on CGIT_CACHE_H to avoid confusion | ||
4 | */ | ||
5 | |||
6 | #ifndef CGIT_CACHE_H | ||
7 | #define CGIT_CACHE_H | ||
8 | |||
9 | struct cacheitem { | ||
10 | char *name; | ||
11 | struct stat st; | ||
12 | int ttl; | ||
13 | int fd; | ||
14 | }; | ||
15 | |||
16 | extern char *cache_safe_filename(const char *unsafe); | ||
17 | extern int cache_lock(struct cacheitem *item); | ||
18 | extern int cache_unlock(struct cacheitem *item); | ||
19 | extern int cache_cancel_lock(struct cacheitem *item); | ||
20 | extern int cache_exist(struct cacheitem *item); | ||
21 | extern int cache_expired(struct cacheitem *item); | ||
22 | |||
23 | #endif /* CGIT_CACHE_H */ | ||
@@ -1,455 +1,456 @@ | |||
1 | /* cgit.c: cgi for the git scm | 1 | /* cgit.c: cgi for the git scm |
2 | * | 2 | * |
3 | * Copyright (C) 2006 Lars Hjemli | 3 | * Copyright (C) 2006 Lars Hjemli |
4 | * | 4 | * |
5 | * Licensed under GNU General Public License v2 | 5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include "cgit.h" | 9 | #include "cgit.h" |
10 | #include "cache.h" | ||
10 | #include "cmd.h" | 11 | #include "cmd.h" |
11 | #include "ui-shared.h" | 12 | #include "ui-shared.h" |
12 | 13 | ||
13 | const char *cgit_version = CGIT_VERSION; | 14 | const char *cgit_version = CGIT_VERSION; |
14 | 15 | ||
15 | void config_cb(const char *name, const char *value) | 16 | void config_cb(const char *name, const char *value) |
16 | { | 17 | { |
17 | if (!strcmp(name, "root-title")) | 18 | if (!strcmp(name, "root-title")) |
18 | ctx.cfg.root_title = xstrdup(value); | 19 | ctx.cfg.root_title = xstrdup(value); |
19 | else if (!strcmp(name, "css")) | 20 | else if (!strcmp(name, "css")) |
20 | ctx.cfg.css = xstrdup(value); | 21 | ctx.cfg.css = xstrdup(value); |
21 | else if (!strcmp(name, "logo")) | 22 | else if (!strcmp(name, "logo")) |
22 | ctx.cfg.logo = xstrdup(value); | 23 | ctx.cfg.logo = xstrdup(value); |
23 | else if (!strcmp(name, "index-header")) | 24 | else if (!strcmp(name, "index-header")) |
24 | ctx.cfg.index_header = xstrdup(value); | 25 | ctx.cfg.index_header = xstrdup(value); |
25 | else if (!strcmp(name, "index-info")) | 26 | else if (!strcmp(name, "index-info")) |
26 | ctx.cfg.index_info = xstrdup(value); | 27 | ctx.cfg.index_info = xstrdup(value); |
27 | else if (!strcmp(name, "logo-link")) | 28 | else if (!strcmp(name, "logo-link")) |
28 | ctx.cfg.logo_link = xstrdup(value); | 29 | ctx.cfg.logo_link = xstrdup(value); |
29 | else if (!strcmp(name, "module-link")) | 30 | else if (!strcmp(name, "module-link")) |
30 | ctx.cfg.module_link = xstrdup(value); | 31 | ctx.cfg.module_link = xstrdup(value); |
31 | else if (!strcmp(name, "virtual-root")) { | 32 | else if (!strcmp(name, "virtual-root")) { |
32 | ctx.cfg.virtual_root = trim_end(value, '/'); | 33 | ctx.cfg.virtual_root = trim_end(value, '/'); |
33 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) | 34 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) |
34 | ctx.cfg.virtual_root = ""; | 35 | ctx.cfg.virtual_root = ""; |
35 | } else if (!strcmp(name, "nocache")) | 36 | } else if (!strcmp(name, "nocache")) |
36 | ctx.cfg.nocache = atoi(value); | 37 | ctx.cfg.nocache = atoi(value); |
37 | else if (!strcmp(name, "snapshots")) | 38 | else if (!strcmp(name, "snapshots")) |
38 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); | 39 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); |
39 | else if (!strcmp(name, "enable-index-links")) | 40 | else if (!strcmp(name, "enable-index-links")) |
40 | ctx.cfg.enable_index_links = atoi(value); | 41 | ctx.cfg.enable_index_links = atoi(value); |
41 | else if (!strcmp(name, "enable-log-filecount")) | 42 | else if (!strcmp(name, "enable-log-filecount")) |
42 | ctx.cfg.enable_log_filecount = atoi(value); | 43 | ctx.cfg.enable_log_filecount = atoi(value); |
43 | else if (!strcmp(name, "enable-log-linecount")) | 44 | else if (!strcmp(name, "enable-log-linecount")) |
44 | ctx.cfg.enable_log_linecount = atoi(value); | 45 | ctx.cfg.enable_log_linecount = atoi(value); |
45 | else if (!strcmp(name, "cache-root")) | 46 | else if (!strcmp(name, "cache-root")) |
46 | ctx.cfg.cache_root = xstrdup(value); | 47 | ctx.cfg.cache_root = xstrdup(value); |
47 | else if (!strcmp(name, "cache-root-ttl")) | 48 | else if (!strcmp(name, "cache-root-ttl")) |
48 | ctx.cfg.cache_root_ttl = atoi(value); | 49 | ctx.cfg.cache_root_ttl = atoi(value); |
49 | else if (!strcmp(name, "cache-repo-ttl")) | 50 | else if (!strcmp(name, "cache-repo-ttl")) |
50 | ctx.cfg.cache_repo_ttl = atoi(value); | 51 | ctx.cfg.cache_repo_ttl = atoi(value); |
51 | else if (!strcmp(name, "cache-static-ttl")) | 52 | else if (!strcmp(name, "cache-static-ttl")) |
52 | ctx.cfg.cache_static_ttl = atoi(value); | 53 | ctx.cfg.cache_static_ttl = atoi(value); |
53 | else if (!strcmp(name, "cache-dynamic-ttl")) | 54 | else if (!strcmp(name, "cache-dynamic-ttl")) |
54 | ctx.cfg.cache_dynamic_ttl = atoi(value); | 55 | ctx.cfg.cache_dynamic_ttl = atoi(value); |
55 | else if (!strcmp(name, "max-message-length")) | 56 | else if (!strcmp(name, "max-message-length")) |
56 | ctx.cfg.max_msg_len = atoi(value); | 57 | ctx.cfg.max_msg_len = atoi(value); |
57 | else if (!strcmp(name, "max-repodesc-length")) | 58 | else if (!strcmp(name, "max-repodesc-length")) |
58 | ctx.cfg.max_repodesc_len = atoi(value); | 59 | ctx.cfg.max_repodesc_len = atoi(value); |
59 | else if (!strcmp(name, "max-commit-count")) | 60 | else if (!strcmp(name, "max-commit-count")) |
60 | ctx.cfg.max_commit_count = atoi(value); | 61 | ctx.cfg.max_commit_count = atoi(value); |
61 | else if (!strcmp(name, "summary-log")) | 62 | else if (!strcmp(name, "summary-log")) |
62 | ctx.cfg.summary_log = atoi(value); | 63 | ctx.cfg.summary_log = atoi(value); |
63 | else if (!strcmp(name, "summary-branches")) | 64 | else if (!strcmp(name, "summary-branches")) |
64 | ctx.cfg.summary_branches = atoi(value); | 65 | ctx.cfg.summary_branches = atoi(value); |
65 | else if (!strcmp(name, "summary-tags")) | 66 | else if (!strcmp(name, "summary-tags")) |
66 | ctx.cfg.summary_tags = atoi(value); | 67 | ctx.cfg.summary_tags = atoi(value); |
67 | else if (!strcmp(name, "agefile")) | 68 | else if (!strcmp(name, "agefile")) |
68 | ctx.cfg.agefile = xstrdup(value); | 69 | ctx.cfg.agefile = xstrdup(value); |
69 | else if (!strcmp(name, "renamelimit")) | 70 | else if (!strcmp(name, "renamelimit")) |
70 | ctx.cfg.renamelimit = atoi(value); | 71 | ctx.cfg.renamelimit = atoi(value); |
71 | else if (!strcmp(name, "robots")) | 72 | else if (!strcmp(name, "robots")) |
72 | ctx.cfg.robots = xstrdup(value); | 73 | ctx.cfg.robots = xstrdup(value); |
73 | else if (!strcmp(name, "clone-prefix")) | 74 | else if (!strcmp(name, "clone-prefix")) |
74 | ctx.cfg.clone_prefix = xstrdup(value); | 75 | ctx.cfg.clone_prefix = xstrdup(value); |
75 | else if (!strcmp(name, "repo.group")) | 76 | else if (!strcmp(name, "repo.group")) |
76 | ctx.cfg.repo_group = xstrdup(value); | 77 | ctx.cfg.repo_group = xstrdup(value); |
77 | else if (!strcmp(name, "repo.url")) | 78 | else if (!strcmp(name, "repo.url")) |
78 | ctx.repo = cgit_add_repo(value); | 79 | ctx.repo = cgit_add_repo(value); |
79 | else if (!strcmp(name, "repo.name")) | 80 | else if (!strcmp(name, "repo.name")) |
80 | ctx.repo->name = xstrdup(value); | 81 | ctx.repo->name = xstrdup(value); |
81 | else if (ctx.repo && !strcmp(name, "repo.path")) | 82 | else if (ctx.repo && !strcmp(name, "repo.path")) |
82 | ctx.repo->path = trim_end(value, '/'); | 83 | ctx.repo->path = trim_end(value, '/'); |
83 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) | 84 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) |
84 | ctx.repo->clone_url = xstrdup(value); | 85 | ctx.repo->clone_url = xstrdup(value); |
85 | else if (ctx.repo && !strcmp(name, "repo.desc")) | 86 | else if (ctx.repo && !strcmp(name, "repo.desc")) |
86 | ctx.repo->desc = xstrdup(value); | 87 | ctx.repo->desc = xstrdup(value); |
87 | else if (ctx.repo && !strcmp(name, "repo.owner")) | 88 | else if (ctx.repo && !strcmp(name, "repo.owner")) |
88 | ctx.repo->owner = xstrdup(value); | 89 | ctx.repo->owner = xstrdup(value); |
89 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) | 90 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) |
90 | ctx.repo->defbranch = xstrdup(value); | 91 | ctx.repo->defbranch = xstrdup(value); |
91 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) | 92 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) |
92 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ | 93 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ |
93 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) | 94 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) |
94 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); | 95 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); |
95 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) | 96 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) |
96 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); | 97 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); |
97 | else if (ctx.repo && !strcmp(name, "repo.module-link")) | 98 | else if (ctx.repo && !strcmp(name, "repo.module-link")) |
98 | ctx.repo->module_link= xstrdup(value); | 99 | ctx.repo->module_link= xstrdup(value); |
99 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { | 100 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { |
100 | if (*value == '/') | 101 | if (*value == '/') |
101 | ctx.repo->readme = xstrdup(value); | 102 | ctx.repo->readme = xstrdup(value); |
102 | else | 103 | else |
103 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); | 104 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); |
104 | } else if (!strcmp(name, "include")) | 105 | } else if (!strcmp(name, "include")) |
105 | cgit_read_config(value, config_cb); | 106 | cgit_read_config(value, config_cb); |
106 | } | 107 | } |
107 | 108 | ||
108 | static void querystring_cb(const char *name, const char *value) | 109 | static void querystring_cb(const char *name, const char *value) |
109 | { | 110 | { |
110 | if (!strcmp(name,"r")) { | 111 | if (!strcmp(name,"r")) { |
111 | ctx.qry.repo = xstrdup(value); | 112 | ctx.qry.repo = xstrdup(value); |
112 | ctx.repo = cgit_get_repoinfo(value); | 113 | ctx.repo = cgit_get_repoinfo(value); |
113 | } else if (!strcmp(name, "p")) { | 114 | } else if (!strcmp(name, "p")) { |
114 | ctx.qry.page = xstrdup(value); | 115 | ctx.qry.page = xstrdup(value); |
115 | } else if (!strcmp(name, "url")) { | 116 | } else if (!strcmp(name, "url")) { |
116 | cgit_parse_url(value); | 117 | cgit_parse_url(value); |
117 | } else if (!strcmp(name, "qt")) { | 118 | } else if (!strcmp(name, "qt")) { |
118 | ctx.qry.grep = xstrdup(value); | 119 | ctx.qry.grep = xstrdup(value); |
119 | } else if (!strcmp(name, "q")) { | 120 | } else if (!strcmp(name, "q")) { |
120 | ctx.qry.search = xstrdup(value); | 121 | ctx.qry.search = xstrdup(value); |
121 | } else if (!strcmp(name, "h")) { | 122 | } else if (!strcmp(name, "h")) { |
122 | ctx.qry.head = xstrdup(value); | 123 | ctx.qry.head = xstrdup(value); |
123 | ctx.qry.has_symref = 1; | 124 | ctx.qry.has_symref = 1; |
124 | } else if (!strcmp(name, "id")) { | 125 | } else if (!strcmp(name, "id")) { |
125 | ctx.qry.sha1 = xstrdup(value); | 126 | ctx.qry.sha1 = xstrdup(value); |
126 | ctx.qry.has_sha1 = 1; | 127 | ctx.qry.has_sha1 = 1; |
127 | } else if (!strcmp(name, "id2")) { | 128 | } else if (!strcmp(name, "id2")) { |
128 | ctx.qry.sha2 = xstrdup(value); | 129 | ctx.qry.sha2 = xstrdup(value); |
129 | ctx.qry.has_sha1 = 1; | 130 | ctx.qry.has_sha1 = 1; |
130 | } else if (!strcmp(name, "ofs")) { | 131 | } else if (!strcmp(name, "ofs")) { |
131 | ctx.qry.ofs = atoi(value); | 132 | ctx.qry.ofs = atoi(value); |
132 | } else if (!strcmp(name, "path")) { | 133 | } else if (!strcmp(name, "path")) { |
133 | ctx.qry.path = trim_end(value, '/'); | 134 | ctx.qry.path = trim_end(value, '/'); |
134 | } else if (!strcmp(name, "name")) { | 135 | } else if (!strcmp(name, "name")) { |
135 | ctx.qry.name = xstrdup(value); | 136 | ctx.qry.name = xstrdup(value); |
136 | } | 137 | } |
137 | } | 138 | } |
138 | 139 | ||
139 | static void prepare_context(struct cgit_context *ctx) | 140 | static void prepare_context(struct cgit_context *ctx) |
140 | { | 141 | { |
141 | memset(ctx, 0, sizeof(ctx)); | 142 | memset(ctx, 0, sizeof(ctx)); |
142 | ctx->cfg.agefile = "info/web/last-modified"; | 143 | ctx->cfg.agefile = "info/web/last-modified"; |
143 | ctx->cfg.cache_dynamic_ttl = 5; | 144 | ctx->cfg.cache_dynamic_ttl = 5; |
144 | ctx->cfg.cache_max_create_time = 5; | 145 | ctx->cfg.cache_max_create_time = 5; |
145 | ctx->cfg.cache_repo_ttl = 5; | 146 | ctx->cfg.cache_repo_ttl = 5; |
146 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; | 147 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; |
147 | ctx->cfg.cache_root_ttl = 5; | 148 | ctx->cfg.cache_root_ttl = 5; |
148 | ctx->cfg.cache_static_ttl = -1; | 149 | ctx->cfg.cache_static_ttl = -1; |
149 | ctx->cfg.css = "/cgit.css"; | 150 | ctx->cfg.css = "/cgit.css"; |
150 | ctx->cfg.logo = "/git-logo.png"; | 151 | ctx->cfg.logo = "/git-logo.png"; |
151 | ctx->cfg.max_commit_count = 50; | 152 | ctx->cfg.max_commit_count = 50; |
152 | ctx->cfg.max_lock_attempts = 5; | 153 | ctx->cfg.max_lock_attempts = 5; |
153 | ctx->cfg.max_msg_len = 60; | 154 | ctx->cfg.max_msg_len = 60; |
154 | ctx->cfg.max_repodesc_len = 60; | 155 | ctx->cfg.max_repodesc_len = 60; |
155 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; | 156 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; |
156 | ctx->cfg.renamelimit = -1; | 157 | ctx->cfg.renamelimit = -1; |
157 | ctx->cfg.robots = "index, nofollow"; | 158 | ctx->cfg.robots = "index, nofollow"; |
158 | ctx->cfg.root_title = "Git repository browser"; | 159 | ctx->cfg.root_title = "Git repository browser"; |
159 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; | 160 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; |
160 | ctx->page.mimetype = "text/html"; | 161 | ctx->page.mimetype = "text/html"; |
161 | ctx->page.charset = PAGE_ENCODING; | 162 | ctx->page.charset = PAGE_ENCODING; |
162 | ctx->page.filename = NULL; | 163 | ctx->page.filename = NULL; |
163 | } | 164 | } |
164 | 165 | ||
165 | static int cgit_prepare_cache(struct cacheitem *item) | 166 | static int cgit_prepare_cache(struct cacheitem *item) |
166 | { | 167 | { |
167 | if (!ctx.repo && ctx.qry.repo) { | 168 | if (!ctx.repo && ctx.qry.repo) { |
168 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, | 169 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, |
169 | "Bad request"); | 170 | "Bad request"); |
170 | cgit_print_http_headers(&ctx); | 171 | cgit_print_http_headers(&ctx); |
171 | cgit_print_docstart(&ctx); | 172 | cgit_print_docstart(&ctx); |
172 | cgit_print_pageheader(&ctx); | 173 | cgit_print_pageheader(&ctx); |
173 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); | 174 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); |
174 | cgit_print_docend(); | 175 | cgit_print_docend(); |
175 | return 0; | 176 | return 0; |
176 | } | 177 | } |
177 | 178 | ||
178 | if (!ctx.repo) { | 179 | if (!ctx.repo) { |
179 | item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); | 180 | item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); |
180 | item->ttl = ctx.cfg.cache_root_ttl; | 181 | item->ttl = ctx.cfg.cache_root_ttl; |
181 | return 1; | 182 | return 1; |
182 | } | 183 | } |
183 | 184 | ||
184 | if (!ctx.qry.page) { | 185 | if (!ctx.qry.page) { |
185 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, | 186 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, |
186 | cache_safe_filename(ctx.repo->url), | 187 | cache_safe_filename(ctx.repo->url), |
187 | cache_safe_filename(ctx.qry.raw))); | 188 | cache_safe_filename(ctx.qry.raw))); |
188 | item->ttl = ctx.cfg.cache_repo_ttl; | 189 | item->ttl = ctx.cfg.cache_repo_ttl; |
189 | } else { | 190 | } else { |
190 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, | 191 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, |
191 | cache_safe_filename(ctx.repo->url), | 192 | cache_safe_filename(ctx.repo->url), |
192 | ctx.qry.page, | 193 | ctx.qry.page, |
193 | cache_safe_filename(ctx.qry.raw))); | 194 | cache_safe_filename(ctx.qry.raw))); |
194 | if (ctx.qry.has_symref) | 195 | if (ctx.qry.has_symref) |
195 | item->ttl = ctx.cfg.cache_dynamic_ttl; | 196 | item->ttl = ctx.cfg.cache_dynamic_ttl; |
196 | else if (ctx.qry.has_sha1) | 197 | else if (ctx.qry.has_sha1) |
197 | item->ttl = ctx.cfg.cache_static_ttl; | 198 | item->ttl = ctx.cfg.cache_static_ttl; |
198 | else | 199 | else |
199 | item->ttl = ctx.cfg.cache_repo_ttl; | 200 | item->ttl = ctx.cfg.cache_repo_ttl; |
200 | } | 201 | } |
201 | return 1; | 202 | return 1; |
202 | } | 203 | } |
203 | 204 | ||
204 | struct refmatch { | 205 | struct refmatch { |
205 | char *req_ref; | 206 | char *req_ref; |
206 | char *first_ref; | 207 | char *first_ref; |
207 | int match; | 208 | int match; |
208 | }; | 209 | }; |
209 | 210 | ||
210 | int find_current_ref(const char *refname, const unsigned char *sha1, | 211 | int find_current_ref(const char *refname, const unsigned char *sha1, |
211 | int flags, void *cb_data) | 212 | int flags, void *cb_data) |
212 | { | 213 | { |
213 | struct refmatch *info; | 214 | struct refmatch *info; |
214 | 215 | ||
215 | info = (struct refmatch *)cb_data; | 216 | info = (struct refmatch *)cb_data; |
216 | if (!strcmp(refname, info->req_ref)) | 217 | if (!strcmp(refname, info->req_ref)) |
217 | info->match = 1; | 218 | info->match = 1; |
218 | if (!info->first_ref) | 219 | if (!info->first_ref) |
219 | info->first_ref = xstrdup(refname); | 220 | info->first_ref = xstrdup(refname); |
220 | return info->match; | 221 | return info->match; |
221 | } | 222 | } |
222 | 223 | ||
223 | char *find_default_branch(struct cgit_repo *repo) | 224 | char *find_default_branch(struct cgit_repo *repo) |
224 | { | 225 | { |
225 | struct refmatch info; | 226 | struct refmatch info; |
226 | 227 | ||
227 | info.req_ref = repo->defbranch; | 228 | info.req_ref = repo->defbranch; |
228 | info.first_ref = NULL; | 229 | info.first_ref = NULL; |
229 | info.match = 0; | 230 | info.match = 0; |
230 | for_each_branch_ref(find_current_ref, &info); | 231 | for_each_branch_ref(find_current_ref, &info); |
231 | if (info.match) | 232 | if (info.match) |
232 | return info.req_ref; | 233 | return info.req_ref; |
233 | else | 234 | else |
234 | return info.first_ref; | 235 | return info.first_ref; |
235 | } | 236 | } |
236 | 237 | ||
237 | static int prepare_repo_cmd(struct cgit_context *ctx) | 238 | static int prepare_repo_cmd(struct cgit_context *ctx) |
238 | { | 239 | { |
239 | char *tmp; | 240 | char *tmp; |
240 | unsigned char sha1[20]; | 241 | unsigned char sha1[20]; |
241 | int nongit = 0; | 242 | int nongit = 0; |
242 | 243 | ||
243 | setenv("GIT_DIR", ctx->repo->path, 1); | 244 | setenv("GIT_DIR", ctx->repo->path, 1); |
244 | setup_git_directory_gently(&nongit); | 245 | setup_git_directory_gently(&nongit); |
245 | if (nongit) { | 246 | if (nongit) { |
246 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, | 247 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, |
247 | "config error"); | 248 | "config error"); |
248 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); | 249 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); |
249 | ctx->repo = NULL; | 250 | ctx->repo = NULL; |
250 | cgit_print_http_headers(ctx); | 251 | cgit_print_http_headers(ctx); |
251 | cgit_print_docstart(ctx); | 252 | cgit_print_docstart(ctx); |
252 | cgit_print_pageheader(ctx); | 253 | cgit_print_pageheader(ctx); |
253 | cgit_print_error(tmp); | 254 | cgit_print_error(tmp); |
254 | cgit_print_docend(); | 255 | cgit_print_docend(); |
255 | return 1; | 256 | return 1; |
256 | } | 257 | } |
257 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); | 258 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); |
258 | 259 | ||
259 | if (!ctx->qry.head) { | 260 | if (!ctx->qry.head) { |
260 | ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); | 261 | ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); |
261 | ctx->repo->defbranch = ctx->qry.head; | 262 | ctx->repo->defbranch = ctx->qry.head; |
262 | } | 263 | } |
263 | 264 | ||
264 | if (!ctx->qry.head) { | 265 | if (!ctx->qry.head) { |
265 | cgit_print_http_headers(ctx); | 266 | cgit_print_http_headers(ctx); |
266 | cgit_print_docstart(ctx); | 267 | cgit_print_docstart(ctx); |
267 | cgit_print_pageheader(ctx); | 268 | cgit_print_pageheader(ctx); |
268 | cgit_print_error("Repository seems to be empty"); | 269 | cgit_print_error("Repository seems to be empty"); |
269 | cgit_print_docend(); | 270 | cgit_print_docend(); |
270 | return 1; | 271 | return 1; |
271 | } | 272 | } |
272 | 273 | ||
273 | if (get_sha1(ctx->qry.head, sha1)) { | 274 | if (get_sha1(ctx->qry.head, sha1)) { |
274 | tmp = xstrdup(ctx->qry.head); | 275 | tmp = xstrdup(ctx->qry.head); |
275 | ctx->qry.head = ctx->repo->defbranch; | 276 | ctx->qry.head = ctx->repo->defbranch; |
276 | cgit_print_http_headers(ctx); | 277 | cgit_print_http_headers(ctx); |
277 | cgit_print_docstart(ctx); | 278 | cgit_print_docstart(ctx); |
278 | cgit_print_pageheader(ctx); | 279 | cgit_print_pageheader(ctx); |
279 | cgit_print_error(fmt("Invalid branch: %s", tmp)); | 280 | cgit_print_error(fmt("Invalid branch: %s", tmp)); |
280 | cgit_print_docend(); | 281 | cgit_print_docend(); |
281 | return 1; | 282 | return 1; |
282 | } | 283 | } |
283 | return 0; | 284 | return 0; |
284 | } | 285 | } |
285 | 286 | ||
286 | static void process_request(struct cgit_context *ctx) | 287 | static void process_request(struct cgit_context *ctx) |
287 | { | 288 | { |
288 | struct cgit_cmd *cmd; | 289 | struct cgit_cmd *cmd; |
289 | 290 | ||
290 | cmd = cgit_get_cmd(ctx); | 291 | cmd = cgit_get_cmd(ctx); |
291 | if (!cmd) { | 292 | if (!cmd) { |
292 | ctx->page.title = "cgit error"; | 293 | ctx->page.title = "cgit error"; |
293 | ctx->repo = NULL; | 294 | ctx->repo = NULL; |
294 | cgit_print_http_headers(ctx); | 295 | cgit_print_http_headers(ctx); |
295 | cgit_print_docstart(ctx); | 296 | cgit_print_docstart(ctx); |
296 | cgit_print_pageheader(ctx); | 297 | cgit_print_pageheader(ctx); |
297 | cgit_print_error("Invalid request"); | 298 | cgit_print_error("Invalid request"); |
298 | cgit_print_docend(); | 299 | cgit_print_docend(); |
299 | return; | 300 | return; |
300 | } | 301 | } |
301 | 302 | ||
302 | if (cmd->want_repo && prepare_repo_cmd(ctx)) | 303 | if (cmd->want_repo && prepare_repo_cmd(ctx)) |
303 | return; | 304 | return; |
304 | 305 | ||
305 | if (cmd->want_layout) { | 306 | if (cmd->want_layout) { |
306 | cgit_print_http_headers(ctx); | 307 | cgit_print_http_headers(ctx); |
307 | cgit_print_docstart(ctx); | 308 | cgit_print_docstart(ctx); |
308 | cgit_print_pageheader(ctx); | 309 | cgit_print_pageheader(ctx); |
309 | } | 310 | } |
310 | 311 | ||
311 | cmd->fn(ctx); | 312 | cmd->fn(ctx); |
312 | 313 | ||
313 | if (cmd->want_layout) | 314 | if (cmd->want_layout) |
314 | cgit_print_docend(); | 315 | cgit_print_docend(); |
315 | } | 316 | } |
316 | 317 | ||
317 | static long ttl_seconds(long ttl) | 318 | static long ttl_seconds(long ttl) |
318 | { | 319 | { |
319 | if (ttl<0) | 320 | if (ttl<0) |
320 | return 60 * 60 * 24 * 365; | 321 | return 60 * 60 * 24 * 365; |
321 | else | 322 | else |
322 | return ttl * 60; | 323 | return ttl * 60; |
323 | } | 324 | } |
324 | 325 | ||
325 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) | 326 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) |
326 | { | 327 | { |
327 | int stdout2; | 328 | int stdout2; |
328 | 329 | ||
329 | if (use_cache) { | 330 | if (use_cache) { |
330 | stdout2 = chk_positive(dup(STDOUT_FILENO), | 331 | stdout2 = chk_positive(dup(STDOUT_FILENO), |
331 | "Preserving STDOUT"); | 332 | "Preserving STDOUT"); |
332 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); | 333 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); |
333 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); | 334 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); |
334 | } | 335 | } |
335 | 336 | ||
336 | ctx.page.modified = time(NULL); | 337 | ctx.page.modified = time(NULL); |
337 | ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); | 338 | ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); |
338 | process_request(&ctx); | 339 | process_request(&ctx); |
339 | 340 | ||
340 | if (use_cache) { | 341 | if (use_cache) { |
341 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); | 342 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); |
342 | chk_positive(dup2(stdout2, STDOUT_FILENO), | 343 | chk_positive(dup2(stdout2, STDOUT_FILENO), |
343 | "Restoring original STDOUT"); | 344 | "Restoring original STDOUT"); |
344 | chk_zero(close(stdout2), "Closing temporary STDOUT"); | 345 | chk_zero(close(stdout2), "Closing temporary STDOUT"); |
345 | } | 346 | } |
346 | } | 347 | } |
347 | 348 | ||
348 | static void cgit_check_cache(struct cacheitem *item) | 349 | static void cgit_check_cache(struct cacheitem *item) |
349 | { | 350 | { |
350 | int i = 0; | 351 | int i = 0; |
351 | 352 | ||
352 | top: | 353 | top: |
353 | if (++i > ctx.cfg.max_lock_attempts) { | 354 | if (++i > ctx.cfg.max_lock_attempts) { |
354 | die("cgit_refresh_cache: unable to lock %s: %s", | 355 | die("cgit_refresh_cache: unable to lock %s: %s", |
355 | item->name, strerror(errno)); | 356 | item->name, strerror(errno)); |
356 | } | 357 | } |
357 | if (!cache_exist(item)) { | 358 | if (!cache_exist(item)) { |
358 | if (!cache_lock(item)) { | 359 | if (!cache_lock(item)) { |
359 | sleep(1); | 360 | sleep(1); |
360 | goto top; | 361 | goto top; |
361 | } | 362 | } |
362 | if (!cache_exist(item)) { | 363 | if (!cache_exist(item)) { |
363 | cgit_fill_cache(item, 1); | 364 | cgit_fill_cache(item, 1); |
364 | cache_unlock(item); | 365 | cache_unlock(item); |
365 | } else { | 366 | } else { |
366 | cache_cancel_lock(item); | 367 | cache_cancel_lock(item); |
367 | } | 368 | } |
368 | } else if (cache_expired(item) && cache_lock(item)) { | 369 | } else if (cache_expired(item) && cache_lock(item)) { |
369 | if (cache_expired(item)) { | 370 | if (cache_expired(item)) { |
370 | cgit_fill_cache(item, 1); | 371 | cgit_fill_cache(item, 1); |
371 | cache_unlock(item); | 372 | cache_unlock(item); |
372 | } else { | 373 | } else { |
373 | cache_cancel_lock(item); | 374 | cache_cancel_lock(item); |
374 | } | 375 | } |
375 | } | 376 | } |
376 | } | 377 | } |
377 | 378 | ||
378 | static void cgit_print_cache(struct cacheitem *item) | 379 | static void cgit_print_cache(struct cacheitem *item) |
379 | { | 380 | { |
380 | static char buf[4096]; | 381 | static char buf[4096]; |
381 | ssize_t i; | 382 | ssize_t i; |
382 | 383 | ||
383 | int fd = open(item->name, O_RDONLY); | 384 | int fd = open(item->name, O_RDONLY); |
384 | if (fd<0) | 385 | if (fd<0) |
385 | die("Unable to open cached file %s", item->name); | 386 | die("Unable to open cached file %s", item->name); |
386 | 387 | ||
387 | while((i=read(fd, buf, sizeof(buf))) > 0) | 388 | while((i=read(fd, buf, sizeof(buf))) > 0) |
388 | write(STDOUT_FILENO, buf, i); | 389 | write(STDOUT_FILENO, buf, i); |
389 | 390 | ||
390 | close(fd); | 391 | close(fd); |
391 | } | 392 | } |
392 | 393 | ||
393 | static void cgit_parse_args(int argc, const char **argv) | 394 | static void cgit_parse_args(int argc, const char **argv) |
394 | { | 395 | { |
395 | int i; | 396 | int i; |
396 | 397 | ||
397 | for (i = 1; i < argc; i++) { | 398 | for (i = 1; i < argc; i++) { |
398 | if (!strncmp(argv[i], "--cache=", 8)) { | 399 | if (!strncmp(argv[i], "--cache=", 8)) { |
399 | ctx.cfg.cache_root = xstrdup(argv[i]+8); | 400 | ctx.cfg.cache_root = xstrdup(argv[i]+8); |
400 | } | 401 | } |
401 | if (!strcmp(argv[i], "--nocache")) { | 402 | if (!strcmp(argv[i], "--nocache")) { |
402 | ctx.cfg.nocache = 1; | 403 | ctx.cfg.nocache = 1; |
403 | } | 404 | } |
404 | if (!strncmp(argv[i], "--query=", 8)) { | 405 | if (!strncmp(argv[i], "--query=", 8)) { |
405 | ctx.qry.raw = xstrdup(argv[i]+8); | 406 | ctx.qry.raw = xstrdup(argv[i]+8); |
406 | } | 407 | } |
407 | if (!strncmp(argv[i], "--repo=", 7)) { | 408 | if (!strncmp(argv[i], "--repo=", 7)) { |
408 | ctx.qry.repo = xstrdup(argv[i]+7); | 409 | ctx.qry.repo = xstrdup(argv[i]+7); |
409 | } | 410 | } |
410 | if (!strncmp(argv[i], "--page=", 7)) { | 411 | if (!strncmp(argv[i], "--page=", 7)) { |
411 | ctx.qry.page = xstrdup(argv[i]+7); | 412 | ctx.qry.page = xstrdup(argv[i]+7); |
412 | } | 413 | } |
413 | if (!strncmp(argv[i], "--head=", 7)) { | 414 | if (!strncmp(argv[i], "--head=", 7)) { |
414 | ctx.qry.head = xstrdup(argv[i]+7); | 415 | ctx.qry.head = xstrdup(argv[i]+7); |
415 | ctx.qry.has_symref = 1; | 416 | ctx.qry.has_symref = 1; |
416 | } | 417 | } |
417 | if (!strncmp(argv[i], "--sha1=", 7)) { | 418 | if (!strncmp(argv[i], "--sha1=", 7)) { |
418 | ctx.qry.sha1 = xstrdup(argv[i]+7); | 419 | ctx.qry.sha1 = xstrdup(argv[i]+7); |
419 | ctx.qry.has_sha1 = 1; | 420 | ctx.qry.has_sha1 = 1; |
420 | } | 421 | } |
421 | if (!strncmp(argv[i], "--ofs=", 6)) { | 422 | if (!strncmp(argv[i], "--ofs=", 6)) { |
422 | ctx.qry.ofs = atoi(argv[i]+6); | 423 | ctx.qry.ofs = atoi(argv[i]+6); |
423 | } | 424 | } |
424 | } | 425 | } |
425 | } | 426 | } |
426 | 427 | ||
427 | int main(int argc, const char **argv) | 428 | int main(int argc, const char **argv) |
428 | { | 429 | { |
429 | struct cacheitem item; | 430 | struct cacheitem item; |
430 | const char *cgit_config_env = getenv("CGIT_CONFIG"); | 431 | const char *cgit_config_env = getenv("CGIT_CONFIG"); |
431 | 432 | ||
432 | prepare_context(&ctx); | 433 | prepare_context(&ctx); |
433 | item.st.st_mtime = time(NULL); | 434 | item.st.st_mtime = time(NULL); |
434 | cgit_repolist.length = 0; | 435 | cgit_repolist.length = 0; |
435 | cgit_repolist.count = 0; | 436 | cgit_repolist.count = 0; |
436 | cgit_repolist.repos = NULL; | 437 | cgit_repolist.repos = NULL; |
437 | 438 | ||
438 | cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, | 439 | cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, |
439 | config_cb); | 440 | config_cb); |
440 | if (getenv("SCRIPT_NAME")) | 441 | if (getenv("SCRIPT_NAME")) |
441 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); | 442 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); |
442 | if (getenv("QUERY_STRING")) | 443 | if (getenv("QUERY_STRING")) |
443 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); | 444 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); |
444 | cgit_parse_args(argc, argv); | 445 | cgit_parse_args(argc, argv); |
445 | cgit_parse_query(ctx.qry.raw, querystring_cb); | 446 | cgit_parse_query(ctx.qry.raw, querystring_cb); |
446 | if (!cgit_prepare_cache(&item)) | 447 | if (!cgit_prepare_cache(&item)) |
447 | return 0; | 448 | return 0; |
448 | if (ctx.cfg.nocache) { | 449 | if (ctx.cfg.nocache) { |
449 | cgit_fill_cache(&item, 0); | 450 | cgit_fill_cache(&item, 0); |
450 | } else { | 451 | } else { |
451 | cgit_check_cache(&item); | 452 | cgit_check_cache(&item); |
452 | cgit_print_cache(&item); | 453 | cgit_print_cache(&item); |
453 | } | 454 | } |
454 | return 0; | 455 | return 0; |
455 | } | 456 | } |
@@ -1,241 +1,227 @@ | |||
1 | #ifndef CGIT_H | 1 | #ifndef CGIT_H |
2 | #define CGIT_H | 2 | #define CGIT_H |
3 | 3 | ||
4 | 4 | ||
5 | #include <git-compat-util.h> | 5 | #include <git-compat-util.h> |
6 | #include <cache.h> | 6 | #include <cache.h> |
7 | #include <grep.h> | 7 | #include <grep.h> |
8 | #include <object.h> | 8 | #include <object.h> |
9 | #include <tree.h> | 9 | #include <tree.h> |
10 | #include <commit.h> | 10 | #include <commit.h> |
11 | #include <tag.h> | 11 | #include <tag.h> |
12 | #include <diff.h> | 12 | #include <diff.h> |
13 | #include <diffcore.h> | 13 | #include <diffcore.h> |
14 | #include <refs.h> | 14 | #include <refs.h> |
15 | #include <revision.h> | 15 | #include <revision.h> |
16 | #include <log-tree.h> | 16 | #include <log-tree.h> |
17 | #include <archive.h> | 17 | #include <archive.h> |
18 | #include <xdiff/xdiff.h> | 18 | #include <xdiff/xdiff.h> |
19 | #include <utf8.h> | 19 | #include <utf8.h> |
20 | 20 | ||
21 | 21 | ||
22 | /* | 22 | /* |
23 | * Dateformats used on misc. pages | 23 | * Dateformats used on misc. pages |
24 | */ | 24 | */ |
25 | #define FMT_LONGDATE "%Y-%m-%d %H:%M:%S" | 25 | #define FMT_LONGDATE "%Y-%m-%d %H:%M:%S" |
26 | #define FMT_SHORTDATE "%Y-%m-%d" | 26 | #define FMT_SHORTDATE "%Y-%m-%d" |
27 | 27 | ||
28 | 28 | ||
29 | /* | 29 | /* |
30 | * Limits used for relative dates | 30 | * Limits used for relative dates |
31 | */ | 31 | */ |
32 | #define TM_MIN 60 | 32 | #define TM_MIN 60 |
33 | #define TM_HOUR (TM_MIN * 60) | 33 | #define TM_HOUR (TM_MIN * 60) |
34 | #define TM_DAY (TM_HOUR * 24) | 34 | #define TM_DAY (TM_HOUR * 24) |
35 | #define TM_WEEK (TM_DAY * 7) | 35 | #define TM_WEEK (TM_DAY * 7) |
36 | #define TM_YEAR (TM_DAY * 365) | 36 | #define TM_YEAR (TM_DAY * 365) |
37 | #define TM_MONTH (TM_YEAR / 12.0) | 37 | #define TM_MONTH (TM_YEAR / 12.0) |
38 | 38 | ||
39 | 39 | ||
40 | /* | 40 | /* |
41 | * Default encoding | 41 | * Default encoding |
42 | */ | 42 | */ |
43 | #define PAGE_ENCODING "UTF-8" | 43 | #define PAGE_ENCODING "UTF-8" |
44 | 44 | ||
45 | typedef void (*configfn)(const char *name, const char *value); | 45 | typedef void (*configfn)(const char *name, const char *value); |
46 | typedef void (*filepair_fn)(struct diff_filepair *pair); | 46 | typedef void (*filepair_fn)(struct diff_filepair *pair); |
47 | typedef void (*linediff_fn)(char *line, int len); | 47 | typedef void (*linediff_fn)(char *line, int len); |
48 | 48 | ||
49 | struct cacheitem { | ||
50 | char *name; | ||
51 | struct stat st; | ||
52 | int ttl; | ||
53 | int fd; | ||
54 | }; | ||
55 | |||
56 | struct cgit_repo { | 49 | struct cgit_repo { |
57 | char *url; | 50 | char *url; |
58 | char *name; | 51 | char *name; |
59 | char *path; | 52 | char *path; |
60 | char *desc; | 53 | char *desc; |
61 | char *owner; | 54 | char *owner; |
62 | char *defbranch; | 55 | char *defbranch; |
63 | char *group; | 56 | char *group; |
64 | char *module_link; | 57 | char *module_link; |
65 | char *readme; | 58 | char *readme; |
66 | char *clone_url; | 59 | char *clone_url; |
67 | int snapshots; | 60 | int snapshots; |
68 | int enable_log_filecount; | 61 | int enable_log_filecount; |
69 | int enable_log_linecount; | 62 | int enable_log_linecount; |
70 | }; | 63 | }; |
71 | 64 | ||
72 | struct cgit_repolist { | 65 | struct cgit_repolist { |
73 | int length; | 66 | int length; |
74 | int count; | 67 | int count; |
75 | struct cgit_repo *repos; | 68 | struct cgit_repo *repos; |
76 | }; | 69 | }; |
77 | 70 | ||
78 | struct commitinfo { | 71 | struct commitinfo { |
79 | struct commit *commit; | 72 | struct commit *commit; |
80 | char *author; | 73 | char *author; |
81 | char *author_email; | 74 | char *author_email; |
82 | unsigned long author_date; | 75 | unsigned long author_date; |
83 | char *committer; | 76 | char *committer; |
84 | char *committer_email; | 77 | char *committer_email; |
85 | unsigned long committer_date; | 78 | unsigned long committer_date; |
86 | char *subject; | 79 | char *subject; |
87 | char *msg; | 80 | char *msg; |
88 | char *msg_encoding; | 81 | char *msg_encoding; |
89 | }; | 82 | }; |
90 | 83 | ||
91 | struct taginfo { | 84 | struct taginfo { |
92 | char *tagger; | 85 | char *tagger; |
93 | char *tagger_email; | 86 | char *tagger_email; |
94 | int tagger_date; | 87 | int tagger_date; |
95 | char *msg; | 88 | char *msg; |
96 | }; | 89 | }; |
97 | 90 | ||
98 | struct refinfo { | 91 | struct refinfo { |
99 | const char *refname; | 92 | const char *refname; |
100 | struct object *object; | 93 | struct object *object; |
101 | union { | 94 | union { |
102 | struct taginfo *tag; | 95 | struct taginfo *tag; |
103 | struct commitinfo *commit; | 96 | struct commitinfo *commit; |
104 | }; | 97 | }; |
105 | }; | 98 | }; |
106 | 99 | ||
107 | struct reflist { | 100 | struct reflist { |
108 | struct refinfo **refs; | 101 | struct refinfo **refs; |
109 | int alloc; | 102 | int alloc; |
110 | int count; | 103 | int count; |
111 | }; | 104 | }; |
112 | 105 | ||
113 | struct cgit_query { | 106 | struct cgit_query { |
114 | int has_symref; | 107 | int has_symref; |
115 | int has_sha1; | 108 | int has_sha1; |
116 | char *raw; | 109 | char *raw; |
117 | char *repo; | 110 | char *repo; |
118 | char *page; | 111 | char *page; |
119 | char *search; | 112 | char *search; |
120 | char *grep; | 113 | char *grep; |
121 | char *head; | 114 | char *head; |
122 | char *sha1; | 115 | char *sha1; |
123 | char *sha2; | 116 | char *sha2; |
124 | char *path; | 117 | char *path; |
125 | char *name; | 118 | char *name; |
126 | int ofs; | 119 | int ofs; |
127 | }; | 120 | }; |
128 | 121 | ||
129 | struct cgit_config { | 122 | struct cgit_config { |
130 | char *agefile; | 123 | char *agefile; |
131 | char *cache_root; | 124 | char *cache_root; |
132 | char *clone_prefix; | 125 | char *clone_prefix; |
133 | char *css; | 126 | char *css; |
134 | char *index_header; | 127 | char *index_header; |
135 | char *index_info; | 128 | char *index_info; |
136 | char *logo; | 129 | char *logo; |
137 | char *logo_link; | 130 | char *logo_link; |
138 | char *module_link; | 131 | char *module_link; |
139 | char *repo_group; | 132 | char *repo_group; |
140 | char *robots; | 133 | char *robots; |
141 | char *root_title; | 134 | char *root_title; |
142 | char *script_name; | 135 | char *script_name; |
143 | char *virtual_root; | 136 | char *virtual_root; |
144 | int cache_dynamic_ttl; | 137 | int cache_dynamic_ttl; |
145 | int cache_max_create_time; | 138 | int cache_max_create_time; |
146 | int cache_repo_ttl; | 139 | int cache_repo_ttl; |
147 | int cache_root_ttl; | 140 | int cache_root_ttl; |
148 | int cache_static_ttl; | 141 | int cache_static_ttl; |
149 | int enable_index_links; | 142 | int enable_index_links; |
150 | int enable_log_filecount; | 143 | int enable_log_filecount; |
151 | int enable_log_linecount; | 144 | int enable_log_linecount; |
152 | int max_commit_count; | 145 | int max_commit_count; |
153 | int max_lock_attempts; | 146 | int max_lock_attempts; |
154 | int max_msg_len; | 147 | int max_msg_len; |
155 | int max_repodesc_len; | 148 | int max_repodesc_len; |
156 | int nocache; | 149 | int nocache; |
157 | int renamelimit; | 150 | int renamelimit; |
158 | int snapshots; | 151 | int snapshots; |
159 | int summary_branches; | 152 | int summary_branches; |
160 | int summary_log; | 153 | int summary_log; |
161 | int summary_tags; | 154 | int summary_tags; |
162 | }; | 155 | }; |
163 | 156 | ||
164 | struct cgit_page { | 157 | struct cgit_page { |
165 | time_t modified; | 158 | time_t modified; |
166 | time_t expires; | 159 | time_t expires; |
167 | char *mimetype; | 160 | char *mimetype; |
168 | char *charset; | 161 | char *charset; |
169 | char *filename; | 162 | char *filename; |
170 | char *title; | 163 | char *title; |
171 | }; | 164 | }; |
172 | 165 | ||
173 | struct cgit_context { | 166 | struct cgit_context { |
174 | struct cgit_query qry; | 167 | struct cgit_query qry; |
175 | struct cgit_config cfg; | 168 | struct cgit_config cfg; |
176 | struct cgit_repo *repo; | 169 | struct cgit_repo *repo; |
177 | struct cgit_page page; | 170 | struct cgit_page page; |
178 | }; | 171 | }; |
179 | 172 | ||
180 | struct cgit_snapshot_format { | 173 | struct cgit_snapshot_format { |
181 | const char *suffix; | 174 | const char *suffix; |
182 | const char *mimetype; | 175 | const char *mimetype; |
183 | write_archive_fn_t write_func; | 176 | write_archive_fn_t write_func; |
184 | int bit; | 177 | int bit; |
185 | }; | 178 | }; |
186 | 179 | ||
187 | extern const char *cgit_version; | 180 | extern const char *cgit_version; |
188 | 181 | ||
189 | extern struct cgit_repolist cgit_repolist; | 182 | extern struct cgit_repolist cgit_repolist; |
190 | extern struct cgit_context ctx; | 183 | extern struct cgit_context ctx; |
191 | extern const struct cgit_snapshot_format cgit_snapshot_formats[]; | 184 | extern const struct cgit_snapshot_format cgit_snapshot_formats[]; |
192 | 185 | ||
193 | extern struct cgit_repo *cgit_add_repo(const char *url); | 186 | extern struct cgit_repo *cgit_add_repo(const char *url); |
194 | extern struct cgit_repo *cgit_get_repoinfo(const char *url); | 187 | extern struct cgit_repo *cgit_get_repoinfo(const char *url); |
195 | extern void cgit_repo_config_cb(const char *name, const char *value); | 188 | extern void cgit_repo_config_cb(const char *name, const char *value); |
196 | 189 | ||
197 | extern int chk_zero(int result, char *msg); | 190 | extern int chk_zero(int result, char *msg); |
198 | extern int chk_positive(int result, char *msg); | 191 | extern int chk_positive(int result, char *msg); |
199 | extern int chk_non_negative(int result, char *msg); | 192 | extern int chk_non_negative(int result, char *msg); |
200 | 193 | ||
201 | extern int hextoint(char c); | 194 | extern int hextoint(char c); |
202 | extern char *trim_end(const char *str, char c); | 195 | extern char *trim_end(const char *str, char c); |
203 | extern char *strlpart(char *txt, int maxlen); | 196 | extern char *strlpart(char *txt, int maxlen); |
204 | extern char *strrpart(char *txt, int maxlen); | 197 | extern char *strrpart(char *txt, int maxlen); |
205 | 198 | ||
206 | extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); | 199 | extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); |
207 | extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, | 200 | extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, |
208 | int flags, void *cb_data); | 201 | int flags, void *cb_data); |
209 | 202 | ||
210 | extern void *cgit_free_commitinfo(struct commitinfo *info); | 203 | extern void *cgit_free_commitinfo(struct commitinfo *info); |
211 | 204 | ||
212 | extern int cgit_diff_files(const unsigned char *old_sha1, | 205 | extern int cgit_diff_files(const unsigned char *old_sha1, |
213 | const unsigned char *new_sha1, | 206 | const unsigned char *new_sha1, |
214 | linediff_fn fn); | 207 | linediff_fn fn); |
215 | 208 | ||
216 | extern void cgit_diff_tree(const unsigned char *old_sha1, | 209 | extern void cgit_diff_tree(const unsigned char *old_sha1, |
217 | const unsigned char *new_sha1, | 210 | const unsigned char *new_sha1, |
218 | filepair_fn fn, const char *prefix); | 211 | filepair_fn fn, const char *prefix); |
219 | 212 | ||
220 | extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); | 213 | extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); |
221 | 214 | ||
222 | extern char *fmt(const char *format,...); | 215 | extern char *fmt(const char *format,...); |
223 | 216 | ||
224 | extern int cgit_read_config(const char *filename, configfn fn); | 217 | extern int cgit_read_config(const char *filename, configfn fn); |
225 | extern int cgit_parse_query(char *txt, configfn fn); | 218 | extern int cgit_parse_query(char *txt, configfn fn); |
226 | extern struct commitinfo *cgit_parse_commit(struct commit *commit); | 219 | extern struct commitinfo *cgit_parse_commit(struct commit *commit); |
227 | extern struct taginfo *cgit_parse_tag(struct tag *tag); | 220 | extern struct taginfo *cgit_parse_tag(struct tag *tag); |
228 | extern void cgit_parse_url(const char *url); | 221 | extern void cgit_parse_url(const char *url); |
229 | 222 | ||
230 | extern char *cache_safe_filename(const char *unsafe); | ||
231 | extern int cache_lock(struct cacheitem *item); | ||
232 | extern int cache_unlock(struct cacheitem *item); | ||
233 | extern int cache_cancel_lock(struct cacheitem *item); | ||
234 | extern int cache_exist(struct cacheitem *item); | ||
235 | extern int cache_expired(struct cacheitem *item); | ||
236 | |||
237 | extern const char *cgit_repobasename(const char *reponame); | 223 | extern const char *cgit_repobasename(const char *reponame); |
238 | 224 | ||
239 | extern int cgit_parse_snapshots_mask(const char *str); | 225 | extern int cgit_parse_snapshots_mask(const char *str); |
240 | 226 | ||
241 | #endif /* CGIT_H */ | 227 | #endif /* CGIT_H */ |