]> git.cameronkatri.com Git - cgit.git/blob - ui-blob.c
tests: successfully validate rc versions
[cgit.git] / ui-blob.c
1 /* ui-blob.c: show blob content
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 #include "ui-blob.h"
11 #include "html.h"
12 #include "ui-shared.h"
13
14 struct walk_tree_context {
15 const char *match_path;
16 struct object_id *matched_oid;
17 unsigned int found_path:1;
18 unsigned int file_only:1;
19 };
20
21 static int walk_tree(const struct object_id *oid, struct strbuf *base,
22 const char *pathname, unsigned mode, int stage, void *cbdata)
23 {
24 struct walk_tree_context *walk_tree_ctx = cbdata;
25
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 oidcpy(walk_tree_ctx->matched_oid, oid);
32 walk_tree_ctx->found_path = 1;
33 return 0;
34 }
35
36 int cgit_ref_path_exists(const char *path, const char *ref, int file_only)
37 {
38 struct object_id oid;
39 unsigned long size;
40 struct pathspec_item path_items = {
41 .match = xstrdup(path),
42 .len = strlen(path)
43 };
44 struct pathspec paths = {
45 .nr = 1,
46 .items = &path_items
47 };
48 struct walk_tree_context walk_tree_ctx = {
49 .match_path = path,
50 .matched_oid = &oid,
51 .found_path = 0,
52 .file_only = file_only
53 };
54
55 if (get_oid(ref, &oid))
56 goto done;
57 if (oid_object_info(the_repository, &oid, &size) != OBJ_COMMIT)
58 goto done;
59 read_tree_recursive(the_repository, lookup_commit_reference(the_repository, &oid)->maybe_tree,
60 "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
61
62 done:
63 free(path_items.match);
64 return walk_tree_ctx.found_path;
65 }
66
67 int cgit_print_file(char *path, const char *head, int file_only)
68 {
69 struct object_id oid;
70 enum object_type type;
71 char *buf;
72 unsigned long size;
73 struct commit *commit;
74 struct pathspec_item path_items = {
75 .match = path,
76 .len = strlen(path)
77 };
78 struct pathspec paths = {
79 .nr = 1,
80 .items = &path_items
81 };
82 struct walk_tree_context walk_tree_ctx = {
83 .match_path = path,
84 .matched_oid = &oid,
85 .found_path = 0,
86 .file_only = file_only
87 };
88
89 if (get_oid(head, &oid))
90 return -1;
91 type = oid_object_info(the_repository, &oid, &size);
92 if (type == OBJ_COMMIT) {
93 commit = lookup_commit_reference(the_repository, &oid);
94 read_tree_recursive(the_repository, commit->maybe_tree,
95 "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
96 if (!walk_tree_ctx.found_path)
97 return -1;
98 type = oid_object_info(the_repository, &oid, &size);
99 }
100 if (type == OBJ_BAD)
101 return -1;
102 buf = read_object_file(&oid, &type, &size);
103 if (!buf)
104 return -1;
105 buf[size] = '\0';
106 html_raw(buf, size);
107 free(buf);
108 return 0;
109 }
110
111 void cgit_print_blob(const char *hex, char *path, const char *head, int file_only)
112 {
113 struct object_id oid;
114 enum object_type type;
115 char *buf;
116 unsigned long size;
117 struct commit *commit;
118 struct pathspec_item path_items = {
119 .match = path,
120 .len = path ? strlen(path) : 0
121 };
122 struct pathspec paths = {
123 .nr = 1,
124 .items = &path_items
125 };
126 struct walk_tree_context walk_tree_ctx = {
127 .match_path = path,
128 .matched_oid = &oid,
129 .found_path = 0,
130 .file_only = file_only
131 };
132
133 if (hex) {
134 if (get_oid_hex(hex, &oid)) {
135 cgit_print_error_page(400, "Bad request",
136 "Bad hex value: %s", hex);
137 return;
138 }
139 } else {
140 if (get_oid(head, &oid)) {
141 cgit_print_error_page(404, "Not found",
142 "Bad ref: %s", head);
143 return;
144 }
145 }
146
147 type = oid_object_info(the_repository, &oid, &size);
148
149 if ((!hex) && type == OBJ_COMMIT && path) {
150 commit = lookup_commit_reference(the_repository, &oid);
151 read_tree_recursive(the_repository, commit->maybe_tree,
152 "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
153 type = oid_object_info(the_repository, &oid, &size);
154 }
155
156 if (type == OBJ_BAD) {
157 cgit_print_error_page(404, "Not found",
158 "Bad object name: %s", hex);
159 return;
160 }
161
162 buf = read_object_file(&oid, &type, &size);
163 if (!buf) {
164 cgit_print_error_page(500, "Internal server error",
165 "Error reading object %s", hex);
166 return;
167 }
168
169 buf[size] = '\0';
170 if (buffer_is_binary(buf, size))
171 ctx.page.mimetype = "application/octet-stream";
172 else
173 ctx.page.mimetype = "text/plain";
174 ctx.page.filename = path;
175
176 html("X-Content-Type-Options: nosniff\n");
177 html("Content-Security-Policy: default-src 'none'\n");
178 cgit_print_http_headers();
179 html_raw(buf, size);
180 free(buf);
181 }