diff options
-rw-r--r-- | cache.c | 57 | ||||
-rw-r--r-- | cgit.c | 72 | ||||
-rw-r--r-- | scan-tree.c | 160 | ||||
-rw-r--r-- | ui-log.c | 33 | ||||
-rw-r--r-- | ui-plain.c | 6 | ||||
-rw-r--r-- | ui-refs.c | 10 | ||||
-rw-r--r-- | ui-repolist.c | 28 | ||||
-rw-r--r-- | ui-shared.c | 63 | ||||
-rw-r--r-- | ui-snapshot.c | 60 | ||||
-rw-r--r-- | ui-summary.c | 12 | ||||
-rw-r--r-- | ui-tag.c | 14 | ||||
-rw-r--r-- | ui-tree.c | 33 |
12 files changed, 305 insertions, 243 deletions
@@ -312,9 +312,9 @@ int cache_process(int size, const char *path, const char *key, int ttl, | |||
312 | cache_fill_fn fn, void *cbdata) | 312 | cache_fill_fn fn, void *cbdata) |
313 | { | 313 | { |
314 | unsigned long hash; | 314 | unsigned long hash; |
315 | int len, i; | 315 | int i; |
316 | char filename[1024]; | 316 | struct strbuf filename = STRBUF_INIT; |
317 | char lockname[1024 + 5]; /* 5 = ".lock" */ | 317 | struct strbuf lockname = STRBUF_INIT; |
318 | struct cache_slot slot; | 318 | struct cache_slot slot; |
319 | 319 | ||
320 | /* If the cache is disabled, just generate the content */ | 320 | /* If the cache is disabled, just generate the content */ |
@@ -329,32 +329,22 @@ int cache_process(int size, const char *path, const char *key, int ttl, | |||
329 | fn(cbdata); | 329 | fn(cbdata); |
330 | return 0; | 330 | return 0; |
331 | } | 331 | } |
332 | len = strlen(path); | ||
333 | if (len > sizeof(filename) - 10) { /* 10 = "/01234567\0" */ | ||
334 | cache_log("[cgit] Cache path too long, caching is disabled: %s\n", | ||
335 | path); | ||
336 | fn(cbdata); | ||
337 | return 0; | ||
338 | } | ||
339 | if (!key) | 332 | if (!key) |
340 | key = ""; | 333 | key = ""; |
341 | hash = hash_str(key) % size; | 334 | hash = hash_str(key) % size; |
342 | strcpy(filename, path); | 335 | strbuf_addstr(&filename, path); |
343 | if (filename[len - 1] != '/') | 336 | strbuf_ensure_end(&filename, '/'); |
344 | filename[len++] = '/'; | ||
345 | for (i = 0; i < 8; i++) { | 337 | for (i = 0; i < 8; i++) { |
346 | sprintf(filename + len++, "%x", | 338 | strbuf_addf(&filename, "%x", (unsigned char)(hash & 0xf)); |
347 | (unsigned char)(hash & 0xf)); | ||
348 | hash >>= 4; | 339 | hash >>= 4; |
349 | } | 340 | } |
350 | filename[len] = '\0'; | 341 | strbuf_addbuf(&lockname, &filename); |
351 | strcpy(lockname, filename); | 342 | strbuf_addstr(&lockname, ".lock"); |
352 | strcpy(lockname + len, ".lock"); | ||
353 | slot.fn = fn; | 343 | slot.fn = fn; |
354 | slot.cbdata = cbdata; | 344 | slot.cbdata = cbdata; |
355 | slot.ttl = ttl; | 345 | slot.ttl = ttl; |
356 | slot.cache_name = filename; | 346 | slot.cache_name = strbuf_detach(&filename, NULL); |
357 | slot.lock_name = lockname; | 347 | slot.lock_name = strbuf_detach(&lockname, NULL); |
358 | slot.key = key; | 348 | slot.key = key; |
359 | slot.keylen = strlen(key); | 349 | slot.keylen = strlen(key); |
360 | return process_slot(&slot); | 350 | return process_slot(&slot); |
@@ -381,18 +371,13 @@ int cache_ls(const char *path) | |||
381 | struct dirent *ent; | 371 | struct dirent *ent; |
382 | int err = 0; | 372 | int err = 0; |
383 | struct cache_slot slot; | 373 | struct cache_slot slot; |
384 | char fullname[1024]; | 374 | struct strbuf fullname = STRBUF_INIT; |
385 | char *name; | 375 | size_t prefixlen; |
386 | 376 | ||
387 | if (!path) { | 377 | if (!path) { |
388 | cache_log("[cgit] cache path not specified\n"); | 378 | cache_log("[cgit] cache path not specified\n"); |
389 | return -1; | 379 | return -1; |
390 | } | 380 | } |
391 | if (strlen(path) > 1024 - 10) { | ||
392 | cache_log("[cgit] cache path too long: %s\n", | ||
393 | path); | ||
394 | return -1; | ||
395 | } | ||
396 | dir = opendir(path); | 381 | dir = opendir(path); |
397 | if (!dir) { | 382 | if (!dir) { |
398 | err = errno; | 383 | err = errno; |
@@ -400,30 +385,28 @@ int cache_ls(const char *path) | |||
400 | path, strerror(err), err); | 385 | path, strerror(err), err); |
401 | return err; | 386 | return err; |
402 | } | 387 | } |
403 | strcpy(fullname, path); | 388 | strbuf_addstr(&fullname, path); |
404 | name = fullname + strlen(path); | 389 | strbuf_ensure_end(&fullname, '/'); |
405 | if (*(name - 1) != '/') { | 390 | prefixlen = fullname.len; |
406 | *name++ = '/'; | ||
407 | *name = '\0'; | ||
408 | } | ||
409 | slot.cache_name = fullname; | ||
410 | while ((ent = readdir(dir)) != NULL) { | 391 | while ((ent = readdir(dir)) != NULL) { |
411 | if (strlen(ent->d_name) != 8) | 392 | if (strlen(ent->d_name) != 8) |
412 | continue; | 393 | continue; |
413 | strcpy(name, ent->d_name); | 394 | strbuf_setlen(&fullname, prefixlen); |
395 | strbuf_addstr(&fullname, ent->d_name); | ||
414 | if ((err = open_slot(&slot)) != 0) { | 396 | if ((err = open_slot(&slot)) != 0) { |
415 | cache_log("[cgit] unable to open path %s: %s (%d)\n", | 397 | cache_log("[cgit] unable to open path %s: %s (%d)\n", |
416 | fullname, strerror(err), err); | 398 | fullname.buf, strerror(err), err); |
417 | continue; | 399 | continue; |
418 | } | 400 | } |
419 | printf("%s %s %10"PRIuMAX" %s\n", | 401 | printf("%s %s %10"PRIuMAX" %s\n", |
420 | name, | 402 | fullname.buf, |
421 | sprintftime("%Y-%m-%d %H:%M:%S", | 403 | sprintftime("%Y-%m-%d %H:%M:%S", |
422 | slot.cache_st.st_mtime), | 404 | slot.cache_st.st_mtime), |
423 | (uintmax_t)slot.cache_st.st_size, | 405 | (uintmax_t)slot.cache_st.st_size, |
424 | slot.buf); | 406 | slot.buf); |
425 | close_slot(&slot); | 407 | close_slot(&slot); |
426 | } | 408 | } |
409 | slot.cache_name = strbuf_detach(&fullname, NULL); | ||
427 | closedir(dir); | 410 | closedir(dir); |
428 | return 0; | 411 | return 0; |
429 | } | 412 | } |
@@ -468,8 +468,8 @@ static int prepare_repo_cmd(struct cgit_context *ctx) | |||
468 | if (nongit) { | 468 | if (nongit) { |
469 | const char *name = ctx->repo->name; | 469 | const char *name = ctx->repo->name; |
470 | rc = errno; | 470 | rc = errno; |
471 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, | 471 | ctx->page.title = fmtalloc("%s - %s", ctx->cfg.root_title, |
472 | "config error"); | 472 | "config error"); |
473 | ctx->repo = NULL; | 473 | ctx->repo = NULL; |
474 | cgit_print_http_headers(ctx); | 474 | cgit_print_http_headers(ctx); |
475 | cgit_print_docstart(ctx); | 475 | cgit_print_docstart(ctx); |
@@ -479,7 +479,7 @@ static int prepare_repo_cmd(struct cgit_context *ctx) | |||
479 | cgit_print_docend(); | 479 | cgit_print_docend(); |
480 | return 1; | 480 | return 1; |
481 | } | 481 | } |
482 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); | 482 | ctx->page.title = fmtalloc("%s - %s", ctx->repo->name, ctx->repo->desc); |
483 | 483 | ||
484 | if (!ctx->repo->defbranch) | 484 | if (!ctx->repo->defbranch) |
485 | ctx->repo->defbranch = guess_defbranch(); | 485 | ctx->repo->defbranch = guess_defbranch(); |
@@ -577,21 +577,16 @@ static int cmp_repos(const void *a, const void *b) | |||
577 | static char *build_snapshot_setting(int bitmap) | 577 | static char *build_snapshot_setting(int bitmap) |
578 | { | 578 | { |
579 | const struct cgit_snapshot_format *f; | 579 | const struct cgit_snapshot_format *f; |
580 | char *result = xstrdup(""); | 580 | struct strbuf result = STRBUF_INIT; |
581 | char *tmp; | ||
582 | int len; | ||
583 | 581 | ||
584 | for (f = cgit_snapshot_formats; f->suffix; f++) { | 582 | for (f = cgit_snapshot_formats; f->suffix; f++) { |
585 | if (f->bit & bitmap) { | 583 | if (f->bit & bitmap) { |
586 | tmp = result; | 584 | if (result.len) |
587 | result = xstrdup(fmt("%s%s ", tmp, f->suffix)); | 585 | strbuf_addch(&result, ' '); |
588 | free(tmp); | 586 | strbuf_addstr(&result, f->suffix); |
589 | } | 587 | } |
590 | } | 588 | } |
591 | len = strlen(result); | 589 | return strbuf_detach(&result, NULL); |
592 | if (len) | ||
593 | result[len - 1] = '\0'; | ||
594 | return result; | ||
595 | } | 590 | } |
596 | 591 | ||
597 | static char *get_first_line(char *txt) | 592 | static char *get_first_line(char *txt) |
@@ -639,7 +634,7 @@ static void print_repo(FILE *f, struct cgit_repo *repo) | |||
639 | fprintf(f, "repo.source-filter=%s\n", repo->source_filter->cmd); | 634 | fprintf(f, "repo.source-filter=%s\n", repo->source_filter->cmd); |
640 | if (repo->snapshots != ctx.cfg.snapshots) { | 635 | if (repo->snapshots != ctx.cfg.snapshots) { |
641 | char *tmp = build_snapshot_setting(repo->snapshots); | 636 | char *tmp = build_snapshot_setting(repo->snapshots); |
642 | fprintf(f, "repo.snapshots=%s\n", tmp); | 637 | fprintf(f, "repo.snapshots=%s\n", tmp ? tmp : ""); |
643 | free(tmp); | 638 | free(tmp); |
644 | } | 639 | } |
645 | if (repo->max_stats != ctx.cfg.max_stats) | 640 | if (repo->max_stats != ctx.cfg.max_stats) |
@@ -661,20 +656,22 @@ static void print_repolist(FILE *f, struct cgit_repolist *list, int start) | |||
661 | */ | 656 | */ |
662 | static int generate_cached_repolist(const char *path, const char *cached_rc) | 657 | static int generate_cached_repolist(const char *path, const char *cached_rc) |
663 | { | 658 | { |
664 | char *locked_rc; | 659 | struct strbuf locked_rc = STRBUF_INIT; |
660 | int result = 0; | ||
665 | int idx; | 661 | int idx; |
666 | FILE *f; | 662 | FILE *f; |
667 | 663 | ||
668 | locked_rc = xstrdup(fmt("%s.lock", cached_rc)); | 664 | strbuf_addf(&locked_rc, "%s.lock", cached_rc); |
669 | f = fopen(locked_rc, "wx"); | 665 | f = fopen(locked_rc.buf, "wx"); |
670 | if (!f) { | 666 | if (!f) { |
671 | /* Inform about the error unless the lockfile already existed, | 667 | /* Inform about the error unless the lockfile already existed, |
672 | * since that only means we've got concurrent requests. | 668 | * since that only means we've got concurrent requests. |
673 | */ | 669 | */ |
674 | if (errno != EEXIST) |