Change git_connect() to return a struct child_process instead of a pid_t.

This prepares the API of git_connect() and finish_connect() to operate on
a struct child_process. Currently, we just use that object as a placeholder
for the pid that we used to return. A follow-up patch will change the
implementation of git_connect() and finish_connect() to make full use
of the object.

Old code had early-return-on-error checks at the calling sites of
git_connect(), but since git_connect() dies on errors anyway, these checks
were removed.

[sp: Corrected style nit of "conn == NULL" to "!conn"]

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
maint
Johannes Sixt 2007-10-19 21:47:53 +02:00 committed by Shawn O. Pearce
parent ca5bb5d539
commit 98158e9cfd
7 changed files with 33 additions and 43 deletions

View File

@ -30,7 +30,7 @@ static int run_remote_archiver(const char *remote, int argc,
{ {
char *url, buf[LARGE_PACKET_MAX]; char *url, buf[LARGE_PACKET_MAX];
int fd[2], i, len, rv; int fd[2], i, len, rv;
pid_t pid; struct child_process *conn;
const char *exec = "git-upload-archive"; const char *exec = "git-upload-archive";
int exec_at = 0; int exec_at = 0;


@ -46,9 +46,7 @@ static int run_remote_archiver(const char *remote, int argc,
} }


url = xstrdup(remote); url = xstrdup(remote);
pid = git_connect(fd, url, exec, 0); conn = git_connect(fd, url, exec, 0);
if (pid < 0)
return pid;


for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
if (i == exec_at) if (i == exec_at)
@ -76,7 +74,7 @@ static int run_remote_archiver(const char *remote, int argc,
rv = recv_sideband("archive", fd[0], 1, 2); rv = recv_sideband("archive", fd[0], 1, 2);
close(fd[0]); close(fd[0]);
close(fd[1]); close(fd[1]);
rv |= finish_connect(pid); rv |= finish_connect(conn);


return !!rv; return !!rv;
} }

View File

@ -762,7 +762,7 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
{ {
int i, ret; int i, ret;
int fd[2]; int fd[2];
pid_t pid; struct child_process *conn;
struct ref *ref; struct ref *ref;
struct stat st; struct stat st;


@ -773,16 +773,14 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
st.st_mtime = 0; st.st_mtime = 0;
} }


pid = git_connect(fd, (char *)dest, uploadpack, conn = git_connect(fd, (char *)dest, uploadpack,
args.verbose ? CONNECT_VERBOSE : 0); args.verbose ? CONNECT_VERBOSE : 0);
if (pid < 0)
return NULL;
if (heads && nr_heads) if (heads && nr_heads)
nr_heads = remove_duplicates(nr_heads, heads); nr_heads = remove_duplicates(nr_heads, heads);
ref = do_fetch_pack(fd, nr_heads, heads, pack_lockfile); ref = do_fetch_pack(fd, nr_heads, heads, pack_lockfile);
close(fd[0]); close(fd[0]);
close(fd[1]); close(fd[1]);
ret = finish_connect(pid); ret = finish_connect(conn);


if (!ret && nr_heads) { if (!ret && nr_heads) {
/* If the heads to pull were given, we should have /* If the heads to pull were given, we should have

View File

@ -503,8 +503,8 @@ struct ref {
#define REF_TAGS (1u << 2) #define REF_TAGS (1u << 2)


#define CONNECT_VERBOSE (1u << 0) #define CONNECT_VERBOSE (1u << 0)
extern pid_t git_connect(int fd[2], char *url, const char *prog, int flags); extern struct child_process *git_connect(int fd[2], char *url, const char *prog, int flags);
extern int finish_connect(pid_t pid); extern int finish_connect(struct child_process *conn);
extern int path_match(const char *path, int nr, char **match); extern int path_match(const char *path, int nr, char **match);
extern int get_ack(int fd, unsigned char *result_sha1); extern int get_ack(int fd, unsigned char *result_sha1);
extern struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match, unsigned int flags); extern struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match, unsigned int flags);

View File

@ -468,21 +468,22 @@ char *get_port(char *host)
} }


/* /*
* This returns 0 if the transport protocol does not need fork(2), * This returns NULL if the transport protocol does not need fork(2), or a
* or a process id if it does. Once done, finish the connection * struct child_process object if it does. Once done, finish the connection
* with finish_connect() with the value returned from this function * with finish_connect() with the value returned from this function
* (it is safe to call finish_connect() with 0 to support the former * (it is safe to call finish_connect() with NULL to support the former
* case). * case).
* *
* Does not return a negative value on error; it just dies. * If it returns, the connect is successful; it just dies on errors.
*/ */
pid_t git_connect(int fd[2], char *url, const char *prog, int flags) struct child_process *git_connect(int fd[2], char *url,
const char *prog, int flags)
{ {
char *host, *path = url; char *host, *path = url;
char *end; char *end;
int c; int c;
int pipefd[2][2]; int pipefd[2][2];
pid_t pid; struct child_process *conn;
enum protocol protocol = PROTO_LOCAL; enum protocol protocol = PROTO_LOCAL;
int free_path = 0; int free_path = 0;
char *port = NULL; char *port = NULL;
@ -568,15 +569,16 @@ pid_t git_connect(int fd[2], char *url, const char *prog, int flags)
free(target_host); free(target_host);
if (free_path) if (free_path)
free(path); free(path);
return 0; return NULL;
} }


if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0) if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
die("unable to create pipe pair for communication"); die("unable to create pipe pair for communication");
pid = fork(); conn = xcalloc(1, sizeof(*conn));
if (pid < 0) conn->pid = fork();
if (conn->pid < 0)
die("unable to fork"); die("unable to fork");
if (!pid) { if (!conn->pid) {
struct strbuf cmd; struct strbuf cmd;


strbuf_init(&cmd, MAX_CMD_LEN); strbuf_init(&cmd, MAX_CMD_LEN);
@ -625,17 +627,18 @@ pid_t git_connect(int fd[2], char *url, const char *prog, int flags)
close(pipefd[1][0]); close(pipefd[1][0]);
if (free_path) if (free_path)
free(path); free(path);
return pid; return conn;
} }


int finish_connect(pid_t pid) int finish_connect(struct child_process *conn)
{ {
if (pid == 0) if (!conn)
return 0; return 0;


while (waitpid(pid, NULL, 0) < 0) { while (waitpid(conn->pid, NULL, 0) < 0) {
if (errno != EINTR) if (errno != EINTR)
return -1; return -1;
} }
free(conn);
return 0; return 0;
} }

View File

@ -25,7 +25,7 @@ int main(int argc, char **argv)
int i, ret; int i, ret;
char *dest = NULL; char *dest = NULL;
int fd[2]; int fd[2];
pid_t pid; struct child_process *conn;
int nongit = 0; int nongit = 0;
unsigned flags = 0; unsigned flags = 0;


@ -64,12 +64,10 @@ int main(int argc, char **argv)
if (!dest || i != argc - 1) if (!dest || i != argc - 1)
usage(peek_remote_usage); usage(peek_remote_usage);


pid = git_connect(fd, dest, uploadpack, 0); conn = git_connect(fd, dest, uploadpack, 0);
if (pid < 0)
return 1;
ret = peek_remote(fd, flags); ret = peek_remote(fd, flags);
close(fd[0]); close(fd[0]);
close(fd[1]); close(fd[1]);
ret |= finish_connect(pid); ret |= finish_connect(conn);
return !!ret; return !!ret;
} }

View File

@ -365,7 +365,7 @@ int main(int argc, char **argv)
char *dest = NULL; char *dest = NULL;
char **heads = NULL; char **heads = NULL;
int fd[2], ret; int fd[2], ret;
pid_t pid; struct child_process *conn;
char *remote_name = NULL; char *remote_name = NULL;
struct remote *remote = NULL; struct remote *remote = NULL;


@ -433,12 +433,10 @@ int main(int argc, char **argv)
} }
} }


pid = git_connect(fd, dest, receivepack, verbose ? CONNECT_VERBOSE : 0); conn = git_connect(fd, dest, receivepack, verbose ? CONNECT_VERBOSE : 0);
if (pid < 0)
return 1;
ret = send_pack(fd[0], fd[1], remote, nr_heads, heads); ret = send_pack(fd[0], fd[1], remote, nr_heads, heads);
close(fd[0]); close(fd[0]);
close(fd[1]); close(fd[1]);
ret |= finish_connect(pid); ret |= finish_connect(conn);
return !!ret; return !!ret;
} }

View File

@ -601,18 +601,13 @@ static struct ref *get_refs_via_connect(const struct transport *transport)
struct git_transport_data *data = transport->data; struct git_transport_data *data = transport->data;
struct ref *refs; struct ref *refs;
int fd[2]; int fd[2];
pid_t pid;
char *dest = xstrdup(transport->url); char *dest = xstrdup(transport->url);

struct child_process *conn = git_connect(fd, dest, data->uploadpack, 0);
pid = git_connect(fd, dest, data->uploadpack, 0);

if (pid < 0)
die("Failed to connect to \"%s\"", transport->url);


get_remote_heads(fd[0], &refs, 0, NULL, 0); get_remote_heads(fd[0], &refs, 0, NULL, 0);
packet_flush(fd[1]); packet_flush(fd[1]);


finish_connect(pid); finish_connect(conn);


free(dest); free(dest);