summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c1
1 files changed, 1 insertions, 0 deletions
diff --git a/cgit.c b/cgit.c
index fba97d7..5dcba76 100644
--- a/cgit.c
+++ b/cgit.c
@@ -60,104 +60,105 @@ static void cgit_fill_cache(struct cacheitem *item)
60} 60}
61 61
62static void cgit_check_cache(struct cacheitem *item) 62static void cgit_check_cache(struct cacheitem *item)
63{ 63{
64 int i = 0; 64 int i = 0;
65 65
66 cache_prepare(item); 66 cache_prepare(item);
67 top: 67 top:
68 if (++i > cgit_max_lock_attempts) { 68 if (++i > cgit_max_lock_attempts) {
69 die("cgit_refresh_cache: unable to lock %s: %s", 69 die("cgit_refresh_cache: unable to lock %s: %s",
70 item->name, strerror(errno)); 70 item->name, strerror(errno));
71 } 71 }
72 if (!cache_exist(item)) { 72 if (!cache_exist(item)) {
73 if (!cache_lock(item)) { 73 if (!cache_lock(item)) {
74 sleep(1); 74 sleep(1);
75 goto top; 75 goto top;
76 } 76 }
77 if (!cache_exist(item)) { 77 if (!cache_exist(item)) {
78 cgit_fill_cache(item); 78 cgit_fill_cache(item);
79 cache_unlock(item); 79 cache_unlock(item);
80 } else { 80 } else {
81 cache_cancel_lock(item); 81 cache_cancel_lock(item);
82 } 82 }
83 } else if (cache_expired(item) && cache_lock(item)) { 83 } else if (cache_expired(item) && cache_lock(item)) {
84 if (cache_expired(item)) { 84 if (cache_expired(item)) {
85 cgit_fill_cache(item); 85 cgit_fill_cache(item);
86 cache_unlock(item); 86 cache_unlock(item);
87 } else { 87 } else {
88 cache_cancel_lock(item); 88 cache_cancel_lock(item);
89 } 89 }
90 } 90 }
91} 91}
92 92
93static void cgit_print_cache(struct cacheitem *item) 93static void cgit_print_cache(struct cacheitem *item)
94{ 94{
95 static char buf[4096]; 95 static char buf[4096];
96 ssize_t i; 96 ssize_t i;
97 97
98 int fd = open(item->name, O_RDONLY); 98 int fd = open(item->name, O_RDONLY);
99 if (fd<0) 99 if (fd<0)
100 die("Unable to open cached file %s", item->name); 100 die("Unable to open cached file %s", item->name);
101 101
102 while((i=read(fd, buf, sizeof(buf))) > 0) 102 while((i=read(fd, buf, sizeof(buf))) > 0)
103 write(STDOUT_FILENO, buf, i); 103 write(STDOUT_FILENO, buf, i);
104 104
105 close(fd); 105 close(fd);
106} 106}
107 107
108static void cgit_parse_args(int argc, const char **argv) 108static void cgit_parse_args(int argc, const char **argv)
109{ 109{
110 int i; 110 int i;
111 111
112 for (i = 1; i < argc; i++) { 112 for (i = 1; i < argc; i++) {
113 if (!strncmp(argv[i], "--root=", 7)) { 113 if (!strncmp(argv[i], "--root=", 7)) {
114 cgit_root = xstrdup(argv[i]+7); 114 cgit_root = xstrdup(argv[i]+7);
115 } 115 }
116 if (!strncmp(argv[i], "--cache=", 8)) { 116 if (!strncmp(argv[i], "--cache=", 8)) {
117 cgit_cache_root = xstrdup(argv[i]+8); 117 cgit_cache_root = xstrdup(argv[i]+8);
118 } 118 }
119 if (!strcmp(argv[i], "--nocache")) { 119 if (!strcmp(argv[i], "--nocache")) {
120 cgit_nocache = 1; 120 cgit_nocache = 1;
121 } 121 }
122 if (!strncmp(argv[i], "--query=", 8)) { 122 if (!strncmp(argv[i], "--query=", 8)) {
123 cgit_querystring = xstrdup(argv[i]+8); 123 cgit_querystring = xstrdup(argv[i]+8);
124 } 124 }
125 if (!strncmp(argv[i], "--repo=", 7)) { 125 if (!strncmp(argv[i], "--repo=", 7)) {
126 cgit_query_repo = xstrdup(argv[i]+7); 126 cgit_query_repo = xstrdup(argv[i]+7);
127 } 127 }
128 if (!strncmp(argv[i], "--page=", 7)) { 128 if (!strncmp(argv[i], "--page=", 7)) {
129 cgit_query_page = xstrdup(argv[i]+7); 129 cgit_query_page = xstrdup(argv[i]+7);
130 } 130 }
131 if (!strncmp(argv[i], "--head=", 7)) { 131 if (!strncmp(argv[i], "--head=", 7)) {
132 cgit_query_head = xstrdup(argv[i]+7); 132 cgit_query_head = xstrdup(argv[i]+7);
133 cgit_query_has_symref = 1; 133 cgit_query_has_symref = 1;
134 } 134 }
135 if (!strncmp(argv[i], "--sha1=", 7)) { 135 if (!strncmp(argv[i], "--sha1=", 7)) {
136 cgit_query_sha1 = xstrdup(argv[i]+7); 136 cgit_query_sha1 = xstrdup(argv[i]+7);
137 cgit_query_has_sha1 = 1; 137 cgit_query_has_sha1 = 1;
138 } 138 }
139 if (!strncmp(argv[i], "--ofs=", 6)) { 139 if (!strncmp(argv[i], "--ofs=", 6)) {
140 cgit_query_ofs = atoi(argv[i]+6); 140 cgit_query_ofs = atoi(argv[i]+6);
141 } 141 }
142 } 142 }
143} 143}
144 144
145int main(int argc, const char **argv) 145int main(int argc, const char **argv)
146{ 146{
147 struct cacheitem item; 147 struct cacheitem item;
148 148
149 cgit_read_config("/etc/cgitrc", cgit_global_config_cb); 149 cgit_read_config("/etc/cgitrc", cgit_global_config_cb);
150 if (getenv("QUERY_STRING")) 150 if (getenv("QUERY_STRING"))
151 cgit_querystring = xstrdup(getenv("QUERY_STRING")); 151 cgit_querystring = xstrdup(getenv("QUERY_STRING"));
152 cgit_parse_args(argc, argv); 152 cgit_parse_args(argc, argv);
153 cgit_parse_query(cgit_querystring, cgit_querystring_cb); 153 cgit_parse_query(cgit_querystring, cgit_querystring_cb);
154 154
155 if (cgit_nocache) { 155 if (cgit_nocache) {
156 cache_prepare(&item);
156 item.fd = STDOUT_FILENO; 157 item.fd = STDOUT_FILENO;
157 cgit_fill_cache(&item); 158 cgit_fill_cache(&item);
158 } else { 159 } else {
159 cgit_check_cache(&item); 160 cgit_check_cache(&item);
160 cgit_print_cache(&item); 161 cgit_print_cache(&item);
161 } 162 }
162 return 0; 163 return 0;
163} 164}