author | Lars Hjemli <hjemli@gmail.com> | 2006-12-11 08:57:58 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2006-12-11 08:57:58 (UTC) |
commit | f5069d88dff7a7ed2f4665904b03e906cca75a7c (patch) (side-by-side diff) | |
tree | 4c9bfa3aaf931af4a345ffb9563b19825c38b628 | |
parent | 76827d8679d1d2bd46e8cddf7da2ce4178e1d676 (diff) | |
download | cgit-f5069d88dff7a7ed2f4665904b03e906cca75a7c.zip cgit-f5069d88dff7a7ed2f4665904b03e906cca75a7c.tar.gz cgit-f5069d88dff7a7ed2f4665904b03e906cca75a7c.tar.bz2 |
Fix cache algorithm loophole
This closes the door for unneccessary calls to cgit_fill_cache().
Noticed by Linus.
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | cache.c | 6 | ||||
-rw-r--r-- | cgit.c | 18 | ||||
-rw-r--r-- | cgit.h | 3 |
3 files changed, 16 insertions, 11 deletions
@@ -1,94 +1,98 @@ /* cache.c: cache management * * Copyright (C) 2006 Lars Hjemli * * Licensed under GNU General Public License v2 * (see COPYING for full license text) */ #include "cgit.h" const int NOLOCK = -1; -int cache_lookup(struct cacheitem *item) +void cache_prepare(struct cacheitem *item) { if (!cgit_query_repo) { item->name = xstrdup(fmt("%s/index.html", cgit_cache_root)); item->ttl = cgit_cache_root_ttl; } else if (!cgit_query_page) { item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root, cgit_query_repo)); item->ttl = cgit_cache_repo_ttl; } else { item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, cgit_query_repo, cgit_query_page, cgit_querystring)); if (cgit_query_has_symref) item->ttl = cgit_cache_dynamic_ttl; else if (cgit_query_has_sha1) item->ttl = cgit_cache_static_ttl; else item->ttl = cgit_cache_repo_ttl; } +} + +int cache_exist(struct cacheitem *item) +{ if (stat(item->name, &item->st)) { item->st.st_mtime = 0; return 0; } return 1; } int cache_create_dirs() { char *path; if (!cgit_query_repo) return 0; path = fmt("%s/%s", cgit_cache_root, cgit_query_repo); if (mkdir(path, S_IRWXU) && errno!=EEXIST) return 0; if (cgit_query_page) { path = fmt("%s/%s/%s", cgit_cache_root, cgit_query_repo, cgit_query_page); if (mkdir(path, S_IRWXU) && errno!=EEXIST) return 0; } return 1; } int cache_lock(struct cacheitem *item) { int ret; char *lockfile = fmt("%s.lock", item->name); top: item->fd = open(lockfile, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR|S_IWUSR); if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) goto top; if (item->fd == NOLOCK && errno == EEXIST) { struct stat st; time_t t; if (stat(lockfile, &st)) return ret; t = time(NULL); if (t-st.st_mtime > cgit_cache_max_create_time && !unlink(lockfile)) goto top; return 0; } return (item->fd > 0); } int cache_unlock(struct cacheitem *item) { close(item->fd); return (rename(fmt("%s.lock", item->name), item->name) == 0); } int cache_expired(struct cacheitem *item) { if (item->ttl < 0) return 0; return item->st.st_mtime + item->ttl * 60 < time(NULL); } @@ -276,233 +276,233 @@ static void cgit_print_repolist() static void cgit_print_branches() { html("<table class='list'>"); html("<tr><th>Branch name</th><th>Head commit</th></tr>\n"); for_each_branch_ref(cgit_print_branch_cb, NULL); html("</table>"); } static int get_one_line(char *txt) { char *t; for(t=txt; *t != '\n' && t != '\0'; t++) ; *t = '\0'; return t-txt-1; } static void cgit_print_commit_shortlog(struct commit *commit) { char *h, *t, *p; char *tree = NULL, *author = NULL, *subject = NULL; int len; time_t sec; struct tm *time; char buf[32]; h = t = commit->buffer; if (strncmp(h, "tree ", 5)) die("Bad commit format: %s", sha1_to_hex(commit->object.sha1)); len = get_one_line(h); tree = h+5; h += len + 2; while (!strncmp(h, "parent ", 7)) h += get_one_line(h) + 2; if (!strncmp(h, "author ", 7)) { author = h+7; h += get_one_line(h) + 2; t = author; while(t!=h && *t!='<') t++; *t='\0'; p = t; while(--t!=author && *t==' ') *t='\0'; while(++p!=h && *p!='>') ; while(++p!=h && !isdigit(*p)) ; t = p; while(++p && isdigit(*p)) ; *p = '\0'; sec = atoi(t); time = gmtime(&sec); } while((len = get_one_line(h)) > 0) h += len+2; h++; len = get_one_line(h); subject = h; html("<tr><td>"); strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time); html_txt(buf); html("</td><td>"); char *qry = fmt("id=%s", sha1_to_hex(commit->object.sha1)); char *url = cgit_pageurl(cgit_query_repo, "view", qry); html_link_open(url, NULL, NULL); html_txt(subject); html_link_close(); html("</td><td>"); html_txt(author); html("</td></tr>\n"); } static void cgit_print_log(const char *tip, int ofs, int cnt) { struct rev_info rev; struct commit *commit; const char *argv[2] = {NULL, tip}; int n = 0; init_revisions(&rev, NULL); rev.abbrev = DEFAULT_ABBREV; rev.commit_format = CMIT_FMT_DEFAULT; rev.verbose_header = 1; rev.show_root_diff = 0; setup_revisions(2, argv, &rev, NULL); prepare_revision_walk(&rev); html("<h2>Log</h2>"); html("<table class='list'>"); html("<tr><th>Date</th><th>Message</th><th>Author</th></tr>\n"); while ((commit = get_revision(&rev)) != NULL && n++ < 100) { cgit_print_commit_shortlog(commit); free(commit->buffer); commit->buffer = NULL; free_commit_list(commit->parents); commit->parents = NULL; } html("</table>\n"); } static void cgit_print_repo_summary() { html("<h2>"); html_txt("Repo summary page"); html("</h2>"); cgit_print_branches(); } static void cgit_print_object(char *hex) { unsigned char sha1[20]; //struct object *object; char type[20]; unsigned char *buf; unsigned long size; if (get_sha1_hex(hex, sha1)){ htmlf(cgit_error, "Bad hex value"); return; } if (sha1_object_info(sha1, type, NULL)){ htmlf(cgit_error, "Bad object name"); return; } buf = read_sha1_file(sha1, type, &size); if (!buf) { htmlf(cgit_error, "Error reading object"); return; } buf[size] = '\0'; html("<h2>Object view</h2>"); htmlf("sha1=%s<br/>type=%s<br/>size=%i<br/>", hex, type, size); html("<pre>"); html_txt(buf); html("</pre>"); } static void cgit_print_repo_page() { if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) || cgit_read_config("info/cgit", cgit_repo_config_cb)) { char *title = fmt("%s - %s", cgit_root_title, "Bad request"); cgit_print_docstart(title); cgit_print_pageheader(title); htmlf(cgit_lib_error, "Unable to scan repository", strerror(errno)); cgit_print_docend(); return; } setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1); char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc); cgit_print_docstart(title); cgit_print_pageheader(title); if (!cgit_query_page) cgit_print_repo_summary(); else if (!strcmp(cgit_query_page, "log")) { cgit_print_log(cgit_query_head, 0, 100); } else if (!strcmp(cgit_query_page, "view")) { cgit_print_object(cgit_query_sha1); } cgit_print_docend(); } static void cgit_fill_cache(struct cacheitem *item) { htmlfd = item->fd; item->st.st_mtime = time(NULL); if (cgit_query_repo) cgit_print_repo_page(); else cgit_print_repolist(); } static void cgit_refresh_cache(struct cacheitem *item) { + cache_prepare(item); top: - if (!cache_lookup(item)) { - if (cache_lock(item)) { - cgit_fill_cache(item); - cache_unlock(item); - } else { + if (!cache_exist(item)) { + if (!cache_lock(item)) { sched_yield(); goto top; } - } else if (cache_expired(item)) { - if (cache_lock(item)) { + if (!cache_exist(item)) cgit_fill_cache(item); - cache_unlock(item); - } + cache_unlock(item); + } else if (cache_expired(item) && cache_lock(item)) { + if (cache_expired(item)) + cgit_fill_cache(item); + cache_unlock(item); } } static void cgit_print_cache(struct cacheitem *item) { static char buf[4096]; ssize_t i; int fd = open(item->name, O_RDONLY); if (fd<0) die("Unable to open cached file %s", item->name); while((i=read(fd, buf, sizeof(buf))) > 0) write(STDOUT_FILENO, buf, i); close(fd); } int main(int argc, const char **argv) { cgit_read_config("/etc/cgitrc", cgit_global_config_cb); cgit_querystring = xstrdup(getenv("QUERY_STRING")); cgit_parse_query(cgit_querystring, cgit_querystring_cb); cgit_refresh_cache(&cacheitem); cgit_print_cache(&cacheitem); return 0; } @@ -1,64 +1,65 @@ #ifndef CGIT_H #define CGIT_H #include "git.h" #include <openssl/sha.h> #include <ctype.h> #include <sched.h> typedef void (*configfn)(const char *name, const char *value); struct cacheitem { char *name; struct stat st; int ttl; int fd; }; extern char *cgit_root; extern char *cgit_root_title; extern char *cgit_css; extern char *cgit_logo; extern char *cgit_logo_link; extern char *cgit_virtual_root; extern char *cgit_cache_root; extern int cgit_cache_root_ttl; extern int cgit_cache_repo_ttl; extern int cgit_cache_dynamic_ttl; extern int cgit_cache_static_ttl; extern int cgit_cache_max_create_time; extern char *cgit_repo_name; extern char *cgit_repo_desc; extern char *cgit_repo_owner; extern int cgit_query_has_symref; extern int cgit_query_has_sha1; extern char *cgit_querystring; extern char *cgit_query_repo; extern char *cgit_query_page; extern char *cgit_query_head; extern char *cgit_query_sha1; extern int htmlfd; extern char *fmt(const char *format,...); extern void html(const char *txt); extern void htmlf(const char *format,...); extern void html_txt(char *txt); extern void html_attr(char *txt); extern void html_link_open(char *url, char *title, char *class); extern void html_link_close(void); extern int cgit_read_config(const char *filename, configfn fn); -extern int cache_lookup(struct cacheitem *item); +extern void cache_prepare(struct cacheitem *item); extern int cache_lock(struct cacheitem *item); extern int cache_unlock(struct cacheitem *item); +extern int cache_exist(struct cacheitem *item); extern int cache_expired(struct cacheitem *item); #endif /* CGIT_H */ |