Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2c982da

Browse files
carlosmnvmg
authored andcommitted
fetch: add a generic pack-download function
Taken mostly from the git transport's version, this can be used by any transport that takes its pack data from the network. Signed-off-by: Carlos Martín Nieto <[email protected]>
1 parent 51760bc commit 2c982da

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/fetch.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "remote.h"
1616
#include "refspec.h"
1717
#include "fetch.h"
18+
#include "netops.h"
1819

1920
static int filter_wants(git_remote *remote)
2021
{
@@ -131,3 +132,60 @@ int git_fetch_download_pack(char **out, git_remote *remote)
131132

132133
return remote->transport->download_pack(out, remote->transport, remote->repo);
133134
}
135+
136+
/* Receiving data from a socket and storing it is pretty much the same for git and HTTP */
137+
int git_fetch__download_pack(char **out, const char *buffered, size_t buffered_size,
138+
GIT_SOCKET fd, git_repository *repo)
139+
{
140+
git_filebuf file;
141+
int error;
142+
char buff[1024], path[GIT_PATH_MAX];
143+
static const char suff[] = "/objects/pack/pack-received";
144+
gitno_buffer buf;
145+
146+
147+
git_path_join(path, repo->path_repository, suff);
148+
149+
gitno_buffer_setup(&buf, buff, sizeof(buff), fd);
150+
151+
if (memcmp(buffered, "PACK", strlen("PACK"))) {
152+
return git__throw(GIT_ERROR, "The pack doesn't start with the signature");
153+
}
154+
155+
error = git_filebuf_open(&file, path, GIT_FILEBUF_TEMPORARY);
156+
if (error < GIT_SUCCESS)
157+
goto cleanup;
158+
159+
/* Part of the packfile has been received, don't loose it */
160+
error = git_filebuf_write(&file, buffered, buffered_size);
161+
if (error < GIT_SUCCESS)
162+
goto cleanup;
163+
164+
while (1) {
165+
error = git_filebuf_write(&file, buf.data, buf.offset);
166+
if (error < GIT_SUCCESS)
167+
goto cleanup;
168+
169+
gitno_consume_n(&buf, buf.offset);
170+
error = gitno_recv(&buf);
171+
if (error < GIT_SUCCESS)
172+
goto cleanup;
173+
if (error == 0) /* Orderly shutdown */
174+
break;
175+
}
176+
177+
*out = git__strdup(file.path_lock);
178+
if (*out == NULL) {
179+
error = GIT_ENOMEM;
180+
goto cleanup;
181+
}
182+
183+
/* A bit dodgy, but we need to keep the pack at the temporary path */
184+
error = git_filebuf_commit_at(&file, file.path_lock);
185+
cleanup:
186+
if (error < GIT_SUCCESS)
187+
git_filebuf_cleanup(&file);
188+
189+
return error;
190+
191+
}

src/fetch.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
#ifndef INCLUDE_fetch_h__
88
#define INCLUDE_fetch_h__
99

10+
#include "netops.h"
11+
1012
int git_fetch_negotiate(git_remote *remote);
1113
int git_fetch_download_pack(char **out, git_remote *remote);
1214

15+
int git_fetch__download_pack(char **out, const char *buffered, size_t buffered_size,
16+
GIT_SOCKET fd, git_repository *repo);
17+
1318
#endif

0 commit comments

Comments
 (0)