summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: e9ee84c)
raw | patch | inline | side by side (parent: e9ee84c)
author | Junio C Hamano <gitster@pobox.com> | |
Thu, 13 Oct 2011 22:19:31 +0000 (15:19 -0700) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Thu, 13 Oct 2011 22:50:21 +0000 (15:50 -0700) |
Move most of the code from read_bundle_header() to parse_bundle_header()
that takes a file descriptor that is already opened for reading, and make
the former responsible only for opening the file and noticing errors.
As a logical consequence of this, is_bundle() helper function can be
implemented as a non-complaining variant of read_bundle_header() that
does not return an open file descriptor, and can be used to tighten
the check used to decide the use of bundle transport in transport_get()
function.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
that takes a file descriptor that is already opened for reading, and make
the former responsible only for opening the file and noticing errors.
As a logical consequence of this, is_bundle() helper function can be
implemented as a non-complaining variant of read_bundle_header() that
does not return an open file descriptor, and can be used to tighten
the check used to decide the use of bundle transport in transport_get()
function.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
bundle.c | patch | blob | history | |
bundle.h | patch | blob | history | |
transport.c | patch | blob | history |
diff --git a/bundle.c b/bundle.c
index 3aa715c40af149f26e68b1bc1ebd9c80cdc49246..105b005366ab2bbc349defff3572a7722e6c1ddf 100644 (file)
--- a/bundle.c
+++ b/bundle.c
return 0;
}
-int read_bundle_header(const char *path, struct bundle_header *header)
+static int parse_bundle_header(int fd, struct bundle_header *header,
+ const char *report_path)
{
struct strbuf buf = STRBUF_INIT;
- int fd = open(path, O_RDONLY);
int status = 0;
- if (fd < 0)
- return error("could not open '%s'", path);
-
/* The bundle header begins with the signature */
if (strbuf_readline_fd(&buf, fd) ||
strcmp(buf.buf, bundle_signature)) {
- error("'%s' does not look like a v2 bundle file", path);
+ if (report_path)
+ error("'%s' does not look like a v2 bundle file",
+ report_path);
status = -1;
goto abort;
}
if (get_sha1_hex(buf.buf, sha1) ||
(40 <= buf.len && !isspace(buf.buf[40])) ||
(!is_prereq && buf.len <= 40)) {
- error("unrecognized header: %s%s (%d)",
- (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
+ if (report_path)
+ error("unrecognized header: %s%s (%d)",
+ (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
status = -1;
break;
} else {
return fd;
}
+int read_bundle_header(const char *path, struct bundle_header *header)
+{
+ int fd = open(path, O_RDONLY);
+
+ if (fd < 0)
+ return error("could not open '%s'", path);
+ return parse_bundle_header(fd, header, path);
+}
+
+int is_bundle(const char *path, int quiet)
+{
+ struct bundle_header header;
+ int fd = open(path, O_RDONLY);
+
+ if (fd < 0)
+ return 0;
+ memset(&header, 0, sizeof(header));
+ fd = parse_bundle_header(fd, &header, quiet ? NULL : path);
+ if (fd >= 0)
+ close(fd);
+ return (fd >= 0);
+}
+
static int list_refs(struct ref_list *r, int argc, const char **argv)
{
int i;
diff --git a/bundle.h b/bundle.h
index e2aedd60d6ad1482bb6da173c853e6ba4805c8d7..c6228e2102c4fe6958d4086061df1f556451cdb9 100644 (file)
--- a/bundle.h
+++ b/bundle.h
struct ref_list references;
};
+int is_bundle(const char *path, int quiet);
int read_bundle_header(const char *path, struct bundle_header *header);
int create_bundle(struct bundle_header *header, const char *path,
int argc, const char **argv);
diff --git a/transport.c b/transport.c
index c9c8056f9de69bd378cd271d70363b5560f13e07..84d6480ce65cf4bc45a344d6e9986696cbd8498f 100644 (file)
--- a/transport.c
+++ b/transport.c
ret->fetch = fetch_objs_via_rsync;
ret->push = rsync_transport_push;
ret->smart_options = NULL;
- } else if (is_local(url) && is_file(url)) {
+ } else if (is_local(url) && is_file(url) && is_bundle(url, 1)) {
struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
ret->data = data;
ret->get_refs_list = get_refs_from_bundle;