filter: don't use dlsym unnecessarily
[cgit.git] / ui-blob.c
blob388a0177a30487959c0fb35d40cff64ce278aedf
1 /* ui-blob.c: show blob content
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
9 #include "cgit.h"
10 #include "ui-blob.h"
11 #include "html.h"
12 #include "ui-shared.h"
14 struct walk_tree_context {
15 const char *match_path;
16 unsigned char *matched_sha1;
17 unsigned int found_path:1;
18 unsigned int file_only:1;
21 static int walk_tree(const unsigned char *sha1, struct strbuf *base,
22 const char *pathname, unsigned mode, int stage, void *cbdata)
24 struct walk_tree_context *walk_tree_ctx = cbdata;
26 if (walk_tree_ctx->file_only && !S_ISREG(mode))
27 return READ_TREE_RECURSIVE;
28 if (strncmp(base->buf, walk_tree_ctx->match_path, base->len)
29 || strcmp(walk_tree_ctx->match_path + base->len, pathname))
30 return READ_TREE_RECURSIVE;
31 memmove(walk_tree_ctx->matched_sha1, sha1, 20);
32 walk_tree_ctx->found_path = 1;
33 return 0;
36 int cgit_ref_path_exists(const char *path, const char *ref, int file_only)
38 unsigned char sha1[20];
39 unsigned long size;
40 struct pathspec_item path_items = {
41 .match = path,
42 .len = strlen(path)
44 struct pathspec paths = {
45 .nr = 1,
46 .items = &path_items
48 struct walk_tree_context walk_tree_ctx = {
49 .match_path = path,
50 .matched_sha1 = sha1,
51 .found_path = 0,
52 .file_only = file_only
55 if (get_sha1(ref, sha1))
56 return 0;
57 if (sha1_object_info(sha1, &size) != OBJ_COMMIT)
58 return 0;
59 read_tree_recursive(lookup_commit_reference(sha1)->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
60 return walk_tree_ctx.found_path;
63 int cgit_print_file(char *path, const char *head, int file_only)
65 unsigned char sha1[20];
66 enum object_type type;
67 char *buf;
68 unsigned long size;
69 struct commit *commit;
70 struct pathspec_item path_items = {
71 .match = path,
72 .len = strlen(path)
74 struct pathspec paths = {
75 .nr = 1,
76 .items = &path_items
78 struct walk_tree_context walk_tree_ctx = {
79 .match_path = path,
80 .matched_sha1 = sha1,
81 .found_path = 0,
82 .file_only = file_only
85 if (get_sha1(head, sha1))
86 return -1;
87 type = sha1_object_info(sha1, &size);
88 if (type == OBJ_COMMIT && path) {
89 commit = lookup_commit_reference(sha1);
90 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
91 if (!walk_tree_ctx.found_path)
92 return -1;
93 type = sha1_object_info(sha1, &size);
95 if (type == OBJ_BAD)
96 return -1;
97 buf = read_sha1_file(sha1, &type, &size);
98 if (!buf)
99 return -1;
100 buf[size] = '\0';
101 html_raw(buf, size);
102 return 0;
105 void cgit_print_blob(const char *hex, char *path, const char *head, int file_only)
107 unsigned char sha1[20];
108 enum object_type type;
109 char *buf;
110 unsigned long size;
111 struct commit *commit;
112 struct pathspec_item path_items = {
113 .match = path,
114 .len = path ? strlen(path) : 0
116 struct pathspec paths = {
117 .nr = 1,
118 .items = &path_items
120 struct walk_tree_context walk_tree_ctx = {
121 .match_path = path,
122 .matched_sha1 = sha1,
123 .found_path = 0,
124 .file_only = file_only
127 if (hex) {
128 if (get_sha1_hex(hex, sha1)) {
129 cgit_print_error("Bad hex value: %s", hex);
130 return;
132 } else {
133 if (get_sha1(head, sha1)) {
134 cgit_print_error("Bad ref: %s", head);
135 return;
139 type = sha1_object_info(sha1, &size);
141 if ((!hex) && type == OBJ_COMMIT && path) {
142 commit = lookup_commit_reference(sha1);
143 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
144 type = sha1_object_info(sha1,&size);
147 if (type == OBJ_BAD) {
148 cgit_print_error("Bad object name: %s", hex);
149 return;
152 buf = read_sha1_file(sha1, &type, &size);
153 if (!buf) {
154 cgit_print_error("Error reading object %s", hex);
155 return;
158 buf[size] = '\0';
159 ctx.page.mimetype = ctx.qry.mimetype;
160 if (!ctx.page.mimetype) {
161 if (buffer_is_binary(buf, size))
162 ctx.page.mimetype = "application/octet-stream";
163 else
164 ctx.page.mimetype = "text/plain";
166 ctx.page.filename = path;
167 cgit_print_http_headers();
168 html_raw(buf, size);