]> git.cameronkatri.com Git - cgit.git/blob - ui-blob.c
ui-log: ban strcpy()
[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(lookup_commit_reference(&oid)->maybe_tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
60
61 done:
62 free(path_items.match);
63 return walk_tree_ctx.found_path;
64 }
65
66 int cgit_print_file(char *path, const char *head, int file_only)
67 {
68 struct object_id oid;
69 enum object_type type;
70 char *buf;
71 unsigned long size;
72 struct commit *commit;
73 struct pathspec_item path_items = {
74 .match = path,
75 .len = strlen(path)
76 };
77 struct pathspec paths = {
78 .nr = 1,
79 .items = &path_items
80 };
81 struct walk_tree_context walk_tree_ctx = {
82 .match_path = path,
83 .matched_oid = &oid,
84 .found_path = 0,
85 .file_only = file_only
86 };
87
88 if (get_oid(head, &oid))
89 return -1;
90 type = oid_object_info(the_repository, &oid, &size);
91 if (type == OBJ_COMMIT) {
92 commit = lookup_commit_reference(&oid);
93 read_tree_recursive(commit->maybe_tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
94 if (!walk_tree_ctx.found_path)
95 return -1;
96 type = oid_object_info(the_repository, &oid, &size);
97 }
98 if (type == OBJ_BAD)
99 return -1;
100 buf = read_object_file(&oid, &type, &size);
101 if (!buf)
102 return -1;
103 buf[size] = '\0';
104 html_raw(buf, size);
105 free(buf);
106 return 0;
107 }
108
109 void cgit_print_blob(const char *hex, char *path, const char *head, int file_only)
110 {
111 struct object_id oid;
112 enum object_type type;
113 char *buf;
114 unsigned long size;
115 struct commit *commit;
116 struct pathspec_item path_items = {
117 .match = path,
118 .len = path ? strlen(path) : 0
119 };
120 struct pathspec paths = {
121 .nr = 1,
122 .items = &path_items
123 };
124 struct walk_tree_context walk_tree_ctx = {
125 .match_path = path,
126 .matched_oid = &oid,
127 .found_path = 0,
128 .file_only = file_only
129 };
130
131 if (hex) {
132 if (get_oid_hex(hex, &oid)) {
133 cgit_print_error_page(400, "Bad request",
134 "Bad hex value: %s", hex);
135 return;
136 }
137 } else {
138 if (get_oid(head, &oid)) {
139 cgit_print_error_page(404, "Not found",
140 "Bad ref: %s", head);
141 return;
142 }
143 }
144
145 type = oid_object_info(the_repository, &oid, &size);
146
147 if ((!hex) && type == OBJ_COMMIT && path) {
148 commit = lookup_commit_reference(&oid);
149 read_tree_recursive(commit->maybe_tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
150 type = oid_object_info(the_repository, &oid, &size);
151 }
152
153 if (type == OBJ_BAD) {
154 cgit_print_error_page(404, "Not found",
155 "Bad object name: %s", hex);
156 return;
157 }
158
159 buf = read_object_file(&oid, &type, &size);
160 if (!buf) {
161 cgit_print_error_page(500, "Internal server error",
162 "Error reading object %s", hex);
163 return;
164 }
165
166 buf[size] = '\0';
167 if (buffer_is_binary(buf, size))
168 ctx.page.mimetype = "application/octet-stream";
169 else
170 ctx.page.mimetype = "text/plain";
171 ctx.page.filename = path;
172
173 html("X-Content-Type-Options: nosniff\n");
174 html("Content-Security-Policy: default-src 'none'\n");
175 cgit_print_http_headers();
176 html_raw(buf, size);
177 free(buf);
178 }