@matheusbsbs / matheus-bagliano / commits / cc41fa8da9
Fork 0

upload-pack: Use start_command() to run pack-objects in create_pack_file().

This gets rid of an explicit fork/exec. Since upload-pack has to coordinate two processes (rev-list and pack-objects), we cannot use the normal finish_command(), but have to monitor the processes explicitly. Hence, the waitpid() call remains. Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Johannes Sixt committed Oct 19, 2007 at 21:47 UTC cc41fa8da9b9e9d23221d3be47a80409a89732d4
1 file changed +44 -61
upload-pack.c
+44 -61
@@ -9,6 +9,7 @@
9 #include "diff.h"
10 #include "revision.h"
11 #include "list-objects.h"
12 +#include "run-command.h"
13
14 static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
15
@@ -98,16 +99,18 @@ static void show_edge(struct commit *commit)
99
100 static void create_pack_file(void)
101 {
101 - /* Pipes between rev-list to pack-objects, pack-objects to us
102 - * and pack-objects error stream for progress bar.
102 + /* Pipe from rev-list to pack-objects
103 */
104 - int lp_pipe[2], pu_pipe[2], pe_pipe[2];
105 - pid_t pid_rev_list, pid_pack_objects;
104 + int lp_pipe[2];
105 + pid_t pid_rev_list;
106 + struct child_process pack_objects;
107 int create_full_pack = (nr_our_refs == want_obj.nr && !have_obj.nr);
108 char data[8193], progress[128];
109 char abort_msg[] = "aborting due to possible repository "
110 "corruption on the remote side.";
111 int buffered = -1;
112 + const char *argv[10];
113 + int arg = 0;
114
115 if (pipe(lp_pipe) < 0)
116 die("git-upload-pack: unable to create pipe");
@@ -154,52 +157,32 @@ static void create_pack_file(void)
157 exit(0);
158 }
159
157 - if (pipe(pu_pipe) < 0)
158 - die("git-upload-pack: unable to create pipe");
159 - if (pipe(pe_pipe) < 0)
160 - die("git-upload-pack: unable to create pipe");
161 - pid_pack_objects = fork();
162 - if (pid_pack_objects < 0) {
160 + /* writable pipe end must not be inherited by pack_objects */
161 + close(lp_pipe[1]);
162 +
163 + argv[arg++] = "pack-objects";
164 + argv[arg++] = "--stdout";
165 + if (!no_progress)
166 + argv[arg++] = "--progress";
167 + if (use_ofs_delta)
168 + argv[arg++] = "--delta-base-offset";
169 + argv[arg++] = NULL;
170 +
171 + memset(&pack_objects, 0, sizeof(pack_objects));
172 + pack_objects.in = lp_pipe[0]; /* start_command closes it */
173 + pack_objects.out = -1;
174 + pack_objects.err = -1;
175 + pack_objects.git_cmd = 1;
176 + pack_objects.argv = argv;
177 + if (start_command(&pack_objects)) {
178 /* daemon sets things up to ignore TERM */
179 kill(pid_rev_list, SIGKILL);
180 die("git-upload-pack: unable to fork git-pack-objects");
181 }
167 - if (!pid_pack_objects) {
168 - const char *argv[10];
169 - int i = 0;
170 -
171 - dup2(lp_pipe[0], 0);
172 - dup2(pu_pipe[1], 1);
173 - dup2(pe_pipe[1], 2);
174 -
175 - close(lp_pipe[0]);
176 - close(lp_pipe[1]);
177 - close(pu_pipe[0]);
178 - close(pu_pipe[1]);
179 - close(pe_pipe[0]);
180 - close(pe_pipe[1]);
181 -
182 - argv[i++] = "pack-objects";
183 - argv[i++] = "--stdout";
184 - if (!no_progress)
185 - argv[i++] = "--progress";
186 - if (use_ofs_delta)
187 - argv[i++] = "--delta-base-offset";
188 - argv[i++] = NULL;
189 -
190 - execv_git_cmd(argv);
191 - kill(pid_rev_list, SIGKILL);
192 - die("git-upload-pack: unable to exec git-pack-objects");
193 - }
194 -
195 - close(lp_pipe[0]);
196 - close(lp_pipe[1]);
182
198 - /* We read from pe_pipe[0] to capture stderr output for
199 - * progress bar, and pu_pipe[0] to capture the pack data.
183 + /* We read from pack_objects.err to capture stderr output for
184 + * progress bar, and pack_objects.out to capture the pack data.
185 */
201 - close(pe_pipe[1]);
202 - close(pu_pipe[1]);
186
187 while (1) {
188 const char *who;
@@ -214,14 +197,14 @@ static void create_pack_file(void)
197 pollsize = 0;
198 pe = pu = -1;
199
217 - if (0 <= pu_pipe[0]) {
218 - pfd[pollsize].fd = pu_pipe[0];
200 + if (0 <= pack_objects.out) {
201 + pfd[pollsize].fd = pack_objects.out;
202 pfd[pollsize].events = POLLIN;
203 pu = pollsize;
204 pollsize++;
205 }
223 - if (0 <= pe_pipe[0]) {
224 - pfd[pollsize].fd = pe_pipe[0];
206 + if (0 <= pack_objects.err) {
207 + pfd[pollsize].fd = pack_objects.err;
208 pfd[pollsize].events = POLLIN;
209 pe = pollsize;
210 pollsize++;
@@ -254,13 +237,13 @@ static void create_pack_file(void)
237 *cp++ = buffered;
238 outsz++;
239 }
257 - sz = xread(pu_pipe[0], cp,
240 + sz = xread(pack_objects.out, cp,
241 sizeof(data) - outsz);
242 if (0 < sz)
243 ;
244 else if (sz == 0) {
262 - close(pu_pipe[0]);
263 - pu_pipe[0] = -1;
245 + close(pack_objects.out);
246 + pack_objects.out = -1;
247 }
248 else
249 goto fail;
@@ -279,13 +262,13 @@ static void create_pack_file(void)
262 /* Status ready; we ship that in the side-band
263 * or dump to the standard error.
264 */
282 - sz = xread(pe_pipe[0], progress,
265 + sz = xread(pack_objects.err, progress,
266 sizeof(progress));
267 if (0 < sz)
268 send_client_data(2, progress, sz);
269 else if (sz == 0) {
287 - close(pe_pipe[0]);
288 - pe_pipe[0] = -1;
270 + close(pack_objects.err);
271 + pack_objects.err = -1;
272 }
273 else
274 goto fail;
@@ -293,12 +276,12 @@ static void create_pack_file(void)
276 }
277
278 /* See if the children are still there */
296 - if (pid_rev_list || pid_pack_objects) {
279 + if (pid_rev_list || pack_objects.pid) {
280 pid = waitpid(-1, &status, WNOHANG);
281 if (!pid)
282 continue;
283 who = ((pid == pid_rev_list) ? "git-rev-list" :
301 - (pid == pid_pack_objects) ? "git-pack-objects" :
284 + (pid == pack_objects.pid) ? "git-pack-objects" :
285 NULL);
286 if (!who) {
287 if (pid < 0) {
@@ -317,9 +300,9 @@ static void create_pack_file(void)
300 }
301 if (pid == pid_rev_list)
302 pid_rev_list = 0;
320 - if (pid == pid_pack_objects)
321 - pid_pack_objects = 0;
322 - if (pid_rev_list || pid_pack_objects)
303 + if (pid == pack_objects.pid)
304 + pack_objects.pid = 0;
305 + if (pid_rev_list || pack_objects.pid)
306 continue;
307 }
308
@@ -340,8 +323,8 @@ static void create_pack_file(void)
323 return;
324 }
325 fail:
343 - if (pid_pack_objects)
344 - kill(pid_pack_objects, SIGKILL);
326 + if (pack_objects.pid)
327 + kill(pack_objects.pid, SIGKILL);
328 if (pid_rev_list)
329 kill(pid_rev_list, SIGKILL);
330 send_client_data(3, abort_msg, sizeof(abort_msg));