]> git.cameronkatri.com Git - cgit.git/blob - shared.c
ui-ssdiff: ban strncpy()
[cgit.git] / shared.c
1 /* shared.c: global vars + some callback functions
2 *
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9 #include "cgit.h"
10
11 struct cgit_repolist cgit_repolist;
12 struct cgit_context ctx;
13
14 int chk_zero(int result, char *msg)
15 {
16 if (result != 0)
17 die_errno("%s", msg);
18 return result;
19 }
20
21 int chk_positive(int result, char *msg)
22 {
23 if (result <= 0)
24 die_errno("%s", msg);
25 return result;
26 }
27
28 int chk_non_negative(int result, char *msg)
29 {
30 if (result < 0)
31 die_errno("%s", msg);
32 return result;
33 }
34
35 char *cgit_default_repo_desc = "[no description]";
36 struct cgit_repo *cgit_add_repo(const char *url)
37 {
38 struct cgit_repo *ret;
39
40 if (++cgit_repolist.count > cgit_repolist.length) {
41 if (cgit_repolist.length == 0)
42 cgit_repolist.length = 8;
43 else
44 cgit_repolist.length *= 2;
45 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
46 cgit_repolist.length *
47 sizeof(struct cgit_repo));
48 }
49
50 ret = &cgit_repolist.repos[cgit_repolist.count-1];
51 memset(ret, 0, sizeof(struct cgit_repo));
52 ret->url = trim_end(url, '/');
53 ret->name = ret->url;
54 ret->path = NULL;
55 ret->desc = cgit_default_repo_desc;
56 ret->extra_head_content = NULL;
57 ret->owner = NULL;
58 ret->homepage = NULL;
59 ret->section = ctx.cfg.section;
60 ret->snapshots = ctx.cfg.snapshots;
61 ret->enable_commit_graph = ctx.cfg.enable_commit_graph;
62 ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
63 ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
64 ret->enable_remote_branches = ctx.cfg.enable_remote_branches;
65 ret->enable_subject_links = ctx.cfg.enable_subject_links;
66 ret->enable_html_serving = ctx.cfg.enable_html_serving;
67 ret->max_stats = ctx.cfg.max_stats;
68 ret->branch_sort = ctx.cfg.branch_sort;
69 ret->commit_sort = ctx.cfg.commit_sort;
70 ret->module_link = ctx.cfg.module_link;
71 ret->readme = ctx.cfg.readme;
72 ret->mtime = -1;
73 ret->about_filter = ctx.cfg.about_filter;
74 ret->commit_filter = ctx.cfg.commit_filter;
75 ret->source_filter = ctx.cfg.source_filter;
76 ret->email_filter = ctx.cfg.email_filter;
77 ret->owner_filter = ctx.cfg.owner_filter;
78 ret->clone_url = ctx.cfg.clone_url;
79 ret->submodules.strdup_strings = 1;
80 ret->hide = ret->ignore = 0;
81 return ret;
82 }
83
84 struct cgit_repo *cgit_get_repoinfo(const char *url)
85 {
86 int i;
87 struct cgit_repo *repo;
88
89 for (i = 0; i < cgit_repolist.count; i++) {
90 repo = &cgit_repolist.repos[i];
91 if (repo->ignore)
92 continue;
93 if (!strcmp(repo->url, url))
94 return repo;
95 }
96 return NULL;
97 }
98
99 void cgit_free_commitinfo(struct commitinfo *info)
100 {
101 free(info->author);
102 free(info->author_email);
103 free(info->committer);
104 free(info->committer_email);
105 free(info->subject);
106 free(info->msg);
107 free(info->msg_encoding);
108 free(info);
109 }
110
111 char *trim_end(const char *str, char c)
112 {
113 int len;
114
115 if (str == NULL)
116 return NULL;
117 len = strlen(str);
118 while (len > 0 && str[len - 1] == c)
119 len--;
120 if (len == 0)
121 return NULL;
122 return xstrndup(str, len);
123 }
124
125 char *ensure_end(const char *str, char c)
126 {
127 size_t len = strlen(str);
128 char *result;
129
130 if (len && str[len - 1] == c)
131 return xstrndup(str, len);
132
133 result = xmalloc(len + 2);
134 memcpy(result, str, len);
135 result[len] = '/';
136 result[len + 1] = '\0';
137 return result;
138 }
139
140 void strbuf_ensure_end(struct strbuf *sb, char c)
141 {
142 if (!sb->len || sb->buf[sb->len - 1] != c)
143 strbuf_addch(sb, c);
144 }
145
146 void cgit_add_ref(struct reflist *list, struct refinfo *ref)
147 {
148 size_t size;
149
150 if (list->count >= list->alloc) {
151 list->alloc += (list->alloc ? list->alloc : 4);
152 size = list->alloc * sizeof(struct refinfo *);
153 list->refs = xrealloc(list->refs, size);
154 }
155 list->refs[list->count++] = ref;
156 }
157
158 static struct refinfo *cgit_mk_refinfo(const char *refname, const struct object_id *oid)
159 {
160 struct refinfo *ref;
161
162 ref = xmalloc(sizeof (struct refinfo));
163 ref->refname = xstrdup(refname);
164 ref->object = parse_object(oid);
165 switch (ref->object->type) {
166 case OBJ_TAG:
167 ref->tag = cgit_parse_tag((struct tag *)ref->object);
168 break;
169 case OBJ_COMMIT:
170 ref->commit = cgit_parse_commit((struct commit *)ref->object);
171 break;
172 }
173 return ref;
174 }
175
176 void cgit_free_taginfo(struct taginfo *tag)
177 {
178 if (tag->tagger)
179 free(tag->tagger);
180 if (tag->tagger_email)
181 free(tag->tagger_email);
182 if (tag->msg)
183 free(tag->msg);
184 free(tag);
185 }
186
187 static void cgit_free_refinfo(struct refinfo *ref)
188 {
189 if (ref->refname)
190 free((char *)ref->refname);
191 switch (ref->object->type) {
192 case OBJ_TAG:
193 cgit_free_taginfo(ref->tag);
194 break;
195 case OBJ_COMMIT:
196 cgit_free_commitinfo(ref->commit);
197 break;
198 }
199 free(ref);
200 }
201
202 void cgit_free_reflist_inner(struct reflist *list)
203 {
204 int i;
205
206 for (i = 0; i < list->count; i++) {
207 cgit_free_refinfo(list->refs[i]);
208 }
209 free(list->refs);
210 }
211
212 int cgit_refs_cb(const char *refname, const struct object_id *oid, int flags,
213 void *cb_data)
214 {
215 struct reflist *list = (struct reflist *)cb_data;
216 struct refinfo *info = cgit_mk_refinfo(refname, oid);
217
218 if (info)
219 cgit_add_ref(list, info);
220 return 0;
221 }
222
223 void cgit_diff_tree_cb(struct diff_queue_struct *q,
224 struct diff_options *options, void *data)
225 {
226 int i;
227
228 for (i = 0; i < q->nr; i++) {
229 if (q->queue[i]->status == 'U')
230 continue;
231 ((filepair_fn)data)(q->queue[i]);
232 }
233 }
234
235 static int load_mmfile(mmfile_t *file, const struct object_id *oid)
236 {
237 enum object_type type;
238
239 if (is_null_oid(oid)) {
240 file->ptr = (char *)"";
241 file->size = 0;
242 } else {
243 file->ptr = read_object_file(oid, &type,
244 (unsigned long *)&file->size);
245 }
246 return 1;
247 }
248
249 /*
250 * Receive diff-buffers from xdiff and concatenate them as
251 * needed across multiple callbacks.
252 *
253 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
254 * ripped from git and modified to use globals instead of
255 * a special callback-struct.
256 */
257 static char *diffbuf = NULL;
258 static int buflen = 0;
259
260 static int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
261 {
262 int i;
263
264 for (i = 0; i < nbuf; i++) {
265 if (mb[i].ptr[mb[i].size-1] != '\n') {
266 /* Incomplete line */
267 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
268 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
269 buflen += mb[i].size;
270 continue;
271 }
272
273 /* we have a complete line */
274 if (!diffbuf) {
275 ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
276 continue;
277 }
278 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
279 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
280 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
281 free(diffbuf);
282 diffbuf = NULL;
283 buflen = 0;
284 }
285 if (diffbuf) {
286 ((linediff_fn)priv)(diffbuf, buflen);
287 free(diffbuf);
288 diffbuf = NULL;
289 buflen = 0;
290 }
291 return 0;
292 }
293
294 int cgit_diff_files(const struct object_id *old_oid,
295 const struct object_id *new_oid, unsigned long *old_size,
296 unsigned long *new_size, int *binary, int context,
297 int ignorews, linediff_fn fn)
298 {
299 mmfile_t file1, file2;
300 xpparam_t diff_params;
301 xdemitconf_t emit_params;
302 xdemitcb_t emit_cb;
303
304 if (!load_mmfile(&file1, old_oid) || !load_mmfile(&file2, new_oid))
305 return 1;
306
307 *old_size = file1.size;
308 *new_size = file2.size;
309
310 if ((file1.ptr && buffer_is_binary(file1.ptr, file1.size)) ||
311 (file2.ptr && buffer_is_binary(file2.ptr, file2.size))) {
312 *binary = 1;
313 if (file1.size)
314 free(file1.ptr);
315 if (file2.size)
316 free(file2.ptr);
317 return 0;
318 }
319
320 memset(&diff_params, 0, sizeof(diff_params));
321 memset(&emit_params, 0, sizeof(emit_params));
322 memset(&emit_cb, 0, sizeof(emit_cb));
323 diff_params.flags = XDF_NEED_MINIMAL;
324 if (ignorews)
325 diff_params.flags |= XDF_IGNORE_WHITESPACE;
326 emit_params.ctxlen = context > 0 ? context : 3;
327 emit_params.flags = XDL_EMIT_FUNCNAMES;
328 emit_cb.outf = filediff_cb;
329 emit_cb.priv = fn;
330 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
331 if (file1.size)
332 free(file1.ptr);
333 if (file2.size)
334 free(file2.ptr);
335 return 0;
336 }
337
338 void cgit_diff_tree(const struct object_id *old_oid,
339 const struct object_id *new_oid,
340 filepair_fn fn, const char *prefix, int ignorews)
341 {
342 struct diff_options opt;
343 struct pathspec_item item;
344
345 memset(&item, 0, sizeof(item));
346 diff_setup(&opt);
347 opt.output_format = DIFF_FORMAT_CALLBACK;
348 opt.detect_rename = 1;
349 opt.rename_limit = ctx.cfg.renamelimit;
350 opt.flags.recursive = 1;
351 if (ignorews)
352 DIFF_XDL_SET(&opt, IGNORE_WHITESPACE);
353 opt.format_callback = cgit_diff_tree_cb;
354 opt.format_callback_data = fn;
355 if (prefix) {
356 item.match = xstrdup(prefix);
357 item.len = strlen(prefix);
358 opt.pathspec.nr = 1;
359 opt.pathspec.items = &item;
360 }
361 diff_setup_done(&opt);
362
363 if (old_oid && !is_null_oid(old_oid))
364 diff_tree_oid(old_oid, new_oid, "", &opt);
365 else
366 diff_root_tree_oid(new_oid, "", &opt);
367 diffcore_std(&opt);
368 diff_flush(&opt);
369
370 free(item.match);
371 }
372
373 void cgit_diff_commit(struct commit *commit, filepair_fn fn, const char *prefix)
374 {
375 const struct object_id *old_oid = NULL;
376
377 if (commit->parents)
378 old_oid = &commit->parents->item->object.oid;
379 cgit_diff_tree(old_oid, &commit->object.oid, fn, prefix,
380 ctx.qry.ignorews);
381 }
382
383 int cgit_parse_snapshots_mask(const char *str)
384 {
385 struct string_list tokens = STRING_LIST_INIT_DUP;
386 struct string_list_item *item;
387 const struct cgit_snapshot_format *f;
388 int rv = 0;
389
390 /* favor legacy setting */
391 if (atoi(str))
392 return 1;
393
394 if (strcmp(str, "all") == 0)
395 return INT_MAX;
396
397 string_list_split(&tokens, str, ' ', -1);
398 string_list_remove_empty_items(&tokens, 0);
399
400 for_each_string_list_item(item, &tokens) {
401 for (f = cgit_snapshot_formats; f->suffix; f++) {
402 if (!strcmp(item->string, f->suffix) ||
403 !strcmp(item->string, f->suffix + 1)) {
404 rv |= cgit_snapshot_format_bit(f);
405 break;
406 }
407 }
408 }
409
410 string_list_clear(&tokens, 0);
411 return rv;
412 }
413
414 typedef struct {
415 char * name;
416 char * value;
417 } cgit_env_var;
418
419 void cgit_prepare_repo_env(struct cgit_repo * repo)
420 {
421 cgit_env_var env_vars[] = {
422 { .name = "CGIT_REPO_URL", .value = repo->url },
423 { .name = "CGIT_REPO_NAME", .value = repo->name },
424 { .name = "CGIT_REPO_PATH", .value = repo->path },
425 { .name = "CGIT_REPO_OWNER", .value = repo->owner },
426 { .name = "CGIT_REPO_DEFBRANCH", .value = repo->defbranch },
427 { .name = "CGIT_REPO_SECTION", .value = repo->section },
428 { .name = "CGIT_REPO_CLONE_URL", .value = repo->clone_url }
429 };
430 int env_var_count = ARRAY_SIZE(env_vars);
431 cgit_env_var *p, *q;
432 static char *warn = "cgit warning: failed to set env: %s=%s\n";
433
434 p = env_vars;
435 q = p + env_var_count;
436 for (; p < q; p++)
437 if (p->value && setenv(p->name, p->value, 1))
438 fprintf(stderr, warn, p->name, p->value);
439 }
440
441 /* Read the content of the specified file into a newly allocated buffer,
442 * zeroterminate the buffer and return 0 on success, errno otherwise.
443 */
444 int readfile(const char *path, char **buf, size_t *size)
445 {
446 int fd, e;
447 struct stat st;
448
449 fd = open(path, O_RDONLY);
450 if (fd == -1)
451 return errno;
452 if (fstat(fd, &st)) {
453 e = errno;
454 close(fd);
455 return e;
456 }
457 if (!S_ISREG(st.st_mode)) {
458 close(fd);
459 return EISDIR;
460 }
461 *buf = xmalloc(st.st_size + 1);
462 *size = read_in_full(fd, *buf, st.st_size);
463 e = errno;
464 (*buf)[*size] = '\0';
465 close(fd);
466 return (*size == st.st_size ? 0 : e);
467 }
468
469 static int is_token_char(char c)
470 {
471 return isalnum(c) || c == '_';
472 }
473
474 /* Replace name with getenv(name), return pointer to zero-terminating char
475 */
476 static char *expand_macro(char *name, int maxlength)
477 {
478 char *value;
479 size_t len;
480
481 len = 0;
482 value = getenv(name);
483 if (value) {
484 len = strlen(value) + 1;
485 if (len > maxlength)
486 len = maxlength;
487 strlcpy(name, value, len);
488 --len;
489 }
490 return name + len;
491 }
492
493 #define EXPBUFSIZE (1024 * 8)
494
495 /* Replace all tokens prefixed by '$' in the specified text with the
496 * value of the named environment variable.
497 * NB: the return value is a static buffer, i.e. it must be strdup'd
498 * by the caller.
499 */
500 char *expand_macros(const char *txt)
501 {
502 static char result[EXPBUFSIZE];
503 char *p, *start;
504 int len;
505
506 p = result;
507 start = NULL;
508 while (p < result + EXPBUFSIZE - 1 && txt && *txt) {
509 *p = *txt;
510 if (start) {
511 if (!is_token_char(*txt)) {
512 if (p - start > 0) {
513 *p = '\0';
514 len = result + EXPBUFSIZE - start - 1;
515 p = expand_macro(start, len) - 1;
516 }
517 start = NULL;
518 txt--;
519 }
520 p++;
521 txt++;
522 continue;
523 }
524 if (*txt == '$') {
525 start = p;
526 txt++;
527 continue;
528 }
529 p++;
530 txt++;
531 }
532 *p = '\0';
533 if (start && p - start > 0) {
534 len = result + EXPBUFSIZE - start - 1;
535 p = expand_macro(start, len);
536 *p = '\0';
537 }
538 return result;
539 }
540
541 char *get_mimetype_for_filename(const char *filename)
542 {
543 char *ext, *mimetype, *token, line[1024], *saveptr;
544 FILE *file;
545 struct string_list_item *mime;
546
547 if (!filename)
548 return NULL;
549
550 ext = strrchr(filename, '.');
551 if (!ext)
552 return NULL;
553 ++ext;
554 if (!ext[0])
555 return NULL;
556 mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
557 if (mime)
558 return xstrdup(mime->util);
559
560 if (!ctx.cfg.mimetype_file)
561 return NULL;
562 file = fopen(ctx.cfg.mimetype_file, "r");
563 if (!file)
564 return NULL;
565 while (fgets(line, sizeof(line), file)) {
566 if (!line[0] || line[0] == '#')
567 continue;
568 mimetype = strtok_r(line, " \t\r\n", &saveptr);
569 while ((token = strtok_r(NULL, " \t\r\n", &saveptr))) {
570 if (!strcasecmp(ext, token)) {
571 fclose(file);
572 return xstrdup(mimetype);
573 }
574 }
575 }
576 fclose(file);
577 return NULL;
578 }