2008-02-23 22:45:33 +01:00
|
|
|
/* ui-blob.c: show blob content
|
|
|
|
*
|
2014-01-08 15:10:49 +01:00
|
|
|
* Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
|
2008-02-23 22:45:33 +01:00
|
|
|
*
|
|
|
|
* Licensed under GNU General Public License v2
|
|
|
|
* (see COPYING for full license text)
|
|
|
|
*/
|
|
|
|
|
2007-05-09 00:48:09 +02:00
|
|
|
#include "cgit.h"
|
2013-04-06 12:37:59 +02:00
|
|
|
#include "ui-blob.h"
|
2008-02-23 22:45:33 +01:00
|
|
|
#include "html.h"
|
2008-03-24 16:50:57 +01:00
|
|
|
#include "ui-shared.h"
|
2007-05-09 00:48:09 +02:00
|
|
|
|
2013-03-03 17:27:46 +01:00
|
|
|
struct walk_tree_context {
|
2013-05-25 16:32:37 +02:00
|
|
|
const char *match_path;
|
2016-10-11 08:55:34 +02:00
|
|
|
struct object_id *matched_oid;
|
2015-03-08 17:32:17 +01:00
|
|
|
unsigned int found_path:1;
|
|
|
|
unsigned int file_only:1;
|
2013-03-03 17:27:46 +01:00
|
|
|
};
|
2008-06-24 23:33:24 +02:00
|
|
|
|
2015-02-07 14:18:28 +01:00
|
|
|
static int walk_tree(const unsigned char *sha1, struct strbuf *base,
|
|
|
|
const char *pathname, unsigned mode, int stage, void *cbdata)
|
2013-03-03 17:27:46 +01:00
|
|
|
{
|
|
|
|
struct walk_tree_context *walk_tree_ctx = cbdata;
|
|
|
|
|
2013-05-26 15:20:02 +02:00
|
|
|
if (walk_tree_ctx->file_only && !S_ISREG(mode))
|
|
|
|
return READ_TREE_RECURSIVE;
|
2015-02-07 14:18:28 +01:00
|
|
|
if (strncmp(base->buf, walk_tree_ctx->match_path, base->len)
|
|
|
|
|| strcmp(walk_tree_ctx->match_path + base->len, pathname))
|
2008-06-24 23:33:24 +02:00
|
|
|
return READ_TREE_RECURSIVE;
|
2016-10-11 08:55:34 +02:00
|
|
|
hashcpy(walk_tree_ctx->matched_oid->hash, sha1);
|
2013-03-03 17:27:46 +01:00
|
|
|
walk_tree_ctx->found_path = 1;
|
2008-06-24 23:33:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-26 15:20:02 +02:00
|
|
|
int cgit_ref_path_exists(const char *path, const char *ref, int file_only)
|
2013-05-25 16:32:37 +02:00
|
|
|
{
|
2016-09-29 21:38:49 +02:00
|
|
|
struct object_id oid;
|
2013-05-26 15:20:02 +02:00
|
|
|
unsigned long size;
|
|
|
|
struct pathspec_item path_items = {
|
|
|
|
.match = path,
|
|
|
|
.len = strlen(path)
|
|
|
|
};
|
|
|
|
struct pathspec paths = {
|
|
|
|
.nr = 1,
|
|
|
|
.items = &path_items
|
|
|
|
};
|
|
|
|
struct walk_tree_context walk_tree_ctx = {
|
|
|
|
.match_path = path,
|
2016-10-11 08:55:34 +02:00
|
|
|
.matched_oid = &oid,
|
2013-05-26 15:20:02 +02:00
|
|
|
.found_path = 0,
|
|
|
|
.file_only = file_only
|
|
|
|
};
|
2013-05-25 16:32:37 +02:00
|
|
|
|
2016-09-29 21:38:49 +02:00
|
|
|
if (get_oid(ref, &oid))
|
2013-05-26 15:20:02 +02:00
|
|
|
return 0;
|
2016-09-29 21:38:49 +02:00
|
|
|
if (sha1_object_info(oid.hash, &size) != OBJ_COMMIT)
|
2013-05-26 15:20:02 +02:00
|
|
|
return 0;
|
2016-09-29 21:38:49 +02:00
|
|
|
read_tree_recursive(lookup_commit_reference(oid.hash)->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
|
2013-05-26 15:20:02 +02:00
|
|
|
return walk_tree_ctx.found_path;
|
2013-05-25 16:32:37 +02:00
|
|
|
}
|
|
|
|
|
2013-05-26 15:20:02 +02:00
|
|
|
int cgit_print_file(char *path, const char *head, int file_only)
|
2007-05-09 00:48:09 +02:00
|
|
|
{
|
2016-09-29 21:38:49 +02:00
|
|
|
struct object_id oid;
|
2010-08-04 00:45:42 +02:00
|
|
|
enum object_type type;
|
|
|
|
char *buf;
|
|
|
|
unsigned long size;
|
|
|
|
struct commit *commit;
|
2013-03-02 13:32:11 +01:00
|
|
|
struct pathspec_item path_items = {
|
|
|
|
.match = path,
|
|
|
|
.len = strlen(path)
|
|
|
|
};
|
|
|
|
struct pathspec paths = {
|
|
|
|
.nr = 1,
|
|
|
|
.items = &path_items
|
|
|
|
};
|
2013-03-03 17:27:46 +01:00
|
|
|
struct walk_tree_context walk_tree_ctx = {
|
|
|
|
.match_path = path,
|
2016-10-11 08:55:34 +02:00
|
|
|
.matched_oid = &oid,
|
2013-05-26 15:20:02 +02:00
|
|
|
.found_path = 0,
|
|
|
|
.file_only = file_only
|
2013-03-03 17:27:46 +01:00
|
|
|
};
|
|
|
|
|
2016-09-29 21:38:49 +02:00
|
|
|
if (get_oid(head, &oid))
|
2010-08-04 00:45:42 +02:00
|
|
|
return -1;
|
2016-09-29 21:38:49 +02:00
|
|
|
type = sha1_object_info(oid.hash, &size);
|
2015-10-09 00:23:58 +02:00
|
|
|
if (type == OBJ_COMMIT) {
|
2016-09-29 21:38:49 +02:00
|
|
|
commit = lookup_commit_reference(oid.hash);
|
2013-03-03 17:27:46 +01:00
|
|
|
read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
|
|
|
|
if (!walk_tree_ctx.found_path)
|
2010-08-04 00:45:42 +02:00
|
|
|
return -1;
|
2016-09-29 21:38:49 +02:00
|
|
|
type = sha1_object_info(oid.hash, &size);
|
2010-08-04 00:45:42 +02:00
|
|
|
}
|
|
|
|
if (type == OBJ_BAD)
|
|
|
|
return -1;
|
2016-09-29 21:38:49 +02:00
|
|
|
buf = read_sha1_file(oid.hash, &type, &size);
|
2010-08-04 00:45:42 +02:00
|
|
|
if (!buf)
|
|
|
|
return -1;
|
|
|
|
buf[size] = '\0';
|
2010-09-04 20:18:16 +02:00
|
|
|
html_raw(buf, size);
|
2015-10-10 16:56:24 +02:00
|
|
|
free(buf);
|
2010-08-04 00:45:42 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2007-05-09 00:48:09 +02:00
|
|
|
|
2013-05-26 15:20:02 +02:00
|
|
|
void cgit_print_blob(const char *hex, char *path, const char *head, int file_only)
|
2010-08-04 00:45:42 +02:00
|
|
|
{
|
2016-09-29 21:38:49 +02:00
|
|
|
struct object_id oid;
|
2007-05-09 00:48:09 +02:00
|
|
|
enum object_type type;
|
2009-07-25 11:51:19 +02:00
|
|
|
char *buf;
|
2007-05-09 00:48:09 +02:00
|
|
|
unsigned long size;
|
2008-06-24 23:33:24 +02:00
|
|
|
struct commit *commit;
|
2013-03-02 13:32:11 +01:00
|
|
|
struct pathspec_item path_items = {
|
|
|
|
.match = path,
|
2013-04-07 16:06:23 +02:00
|
|
|
.len = path ? strlen(path) : 0
|
2013-03-02 13:32:11 +01:00
|
|
|
};
|
|
|
|
struct pathspec paths = {
|
|
|
|
.nr = 1,
|
|
|
|
.items = &path_items
|
|
|
|
};
|
2013-03-03 17:27:46 +01:00
|
|
|
struct walk_tree_context walk_tree_ctx = {
|
|
|
|
.match_path = path,
|
2016-10-11 08:55:34 +02:00
|
|
|
.matched_oid = &oid,
|
2013-05-26 15:20:02 +02:00
|
|
|
.found_path = 0,
|
|
|
|
.file_only = file_only
|
2013-03-03 17:27:46 +01:00
|
|
|
};
|
2007-05-09 00:48:09 +02:00
|
|
|
|
2008-06-24 23:33:24 +02:00
|
|
|
if (hex) {
|
2016-09-29 21:38:49 +02:00
|
|
|
if (get_oid_hex(hex, &oid)) {
|
2015-08-14 13:47:06 +02:00
|
|
|
cgit_print_error_page(400, "Bad request",
|
|
|
|
"Bad hex value: %s", hex);
|
2008-06-24 23:33:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2016-09-29 21:38:49 +02:00
|
|
|
if (get_oid(head, &oid)) {
|
2015-08-14 13:47:06 +02:00
|
|
|
cgit_print_error_page(404, "Not found",
|
|
|
|
"Bad ref: %s", head);
|
2008-06-24 23:33:24 +02:00
|
|
|
return;
|
|
|
|
}
|
2007-05-09 00:48:09 +02:00
|
|
|
}
|
|
|
|
|
2016-09-29 21:38:49 +02:00
|
|
|
type = sha1_object_info(oid.hash, &size);
|
2008-06-24 23:33:24 +02:00
|
|
|
|
2013-03-04 05:21:33 +01:00
|
|
|
if ((!hex) && type == OBJ_COMMIT && path) {
|
2016-09-29 21:38:49 +02:00
|
|
|
commit = lookup_commit_reference(oid.hash);
|
2013-03-03 17:27:46 +01:00
|
|
|
read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
|
2016-09-29 21:38:49 +02:00
|
|
|
type = sha1_object_info(oid.hash, &size);
|
2008-06-24 23:33:24 +02:00
|
|
|
}
|
|
|
|
|
2007-05-09 00:48:09 +02:00
|
|
|
if (type == OBJ_BAD) {
|
2015-08-14 13:47:06 +02:00
|
|
|
cgit_print_error_page(404, "Not found",
|
|
|
|
"Bad object name: %s", hex);
|
2007-05-09 00:48:09 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-29 21:38:49 +02:00
|
|
|
buf = read_sha1_file(oid.hash, &type, &size);
|
2007-05-09 00:48:09 +02:00
|
|
|
if (!buf) {
|
2015-08-14 13:47:06 +02:00
|
|
|
cgit_print_error_page(500, "Internal server error",
|
|
|
|
"Error reading object %s", hex);
|
2007-05-09 00:48:09 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[size] = '\0';
|
2016-01-14 14:31:53 +01:00
|
|
|
if (buffer_is_binary(buf, size))
|
|
|
|
ctx.page.mimetype = "application/octet-stream";
|
|
|
|
else
|
|
|
|
ctx.page.mimetype = "text/plain";
|
2008-03-24 00:51:19 +01:00
|
|
|
ctx.page.filename = path;
|
2016-01-14 14:43:43 +01:00
|
|
|
|
|
|
|
html("X-Content-Type-Options: nosniff\n");
|
|
|
|
html("Content-Security-Policy: default-src 'none'\n");
|
2014-01-15 21:53:15 +01:00
|
|
|
cgit_print_http_headers();
|
2010-09-04 20:18:16 +02:00
|
|
|
html_raw(buf, size);
|
2015-10-10 16:56:23 +02:00
|
|
|
free(buf);
|
2007-05-09 00:48:09 +02:00
|
|
|
}
|