66#include "archive.h"
77#include "pkt-line.h"
88#include "sideband.h"
9+ #include "run-command.h"
910
1011static const char upload_archive_usage [] =
1112 "git upload-archive <repo>" ;
1213
1314static const char deadchild [] =
1415"git upload-archive: archiver died with error" ;
1516
16- static const char lostchild [] =
17- "git upload-archive: archiver process was lost" ;
18-
1917#define MAX_ARGS (64)
2018
21- static int run_upload_archive (int argc , const char * * argv , const char * prefix )
19+ int cmd_upload_archive_writer (int argc , const char * * argv , const char * prefix )
2220{
2321 const char * sent_argv [MAX_ARGS ];
2422 const char * arg_cmd = "argument " ;
@@ -96,48 +94,33 @@ static ssize_t process_input(int child_fd, int band)
9694
9795int cmd_upload_archive (int argc , const char * * argv , const char * prefix )
9896{
99- pid_t writer ;
100- int fd1 [ 2 ], fd2 [ 2 ];
97+ struct child_process writer = { argv } ;
98+
10199 /*
102100 * Set up sideband subprocess.
103101 *
104102 * We (parent) monitor and read from child, sending its fd#1 and fd#2
105103 * multiplexed out to our fd#1. If the child dies, we tell the other
106104 * end over channel #3.
107105 */
108- if (pipe (fd1 ) < 0 || pipe (fd2 ) < 0 ) {
109- int err = errno ;
110- packet_write (1 , "NACK pipe failed on the remote side\n" );
111- die ("upload-archive: %s" , strerror (err ));
112- }
113- writer = fork ();
114- if (writer < 0 ) {
106+ argv [0 ] = "upload-archive--writer" ;
107+ writer .out = writer .err = -1 ;
108+ writer .git_cmd = 1 ;
109+ if (start_command (& writer )) {
115110 int err = errno ;
116- packet_write (1 , "NACK fork failed on the remote side \n" );
111+ packet_write (1 , "NACK unable to spawn subprocess \n" );
117112 die ("upload-archive: %s" , strerror (err ));
118113 }
119- if (!writer ) {
120- /* child - connect fd#1 and fd#2 to the pipe */
121- dup2 (fd1 [1 ], 1 );
122- dup2 (fd2 [1 ], 2 );
123- close (fd1 [1 ]); close (fd2 [1 ]);
124- close (fd1 [0 ]); close (fd2 [0 ]); /* we do not read from pipe */
125-
126- exit (run_upload_archive (argc , argv , prefix ));
127- }
128114
129- /* parent - read from child, multiplex and send out to fd#1 */
130- close (fd1 [1 ]); close (fd2 [1 ]); /* we do not write to pipe */
131115 packet_write (1 , "ACK\n" );
132116 packet_flush (1 );
133117
134118 while (1 ) {
135119 struct pollfd pfd [2 ];
136- int status ;
137120
138- pfd [0 ].fd = fd1 [ 0 ] ;
121+ pfd [0 ].fd = writer . out ;
139122 pfd [0 ].events = POLLIN ;
140- pfd [1 ].fd = fd2 [ 0 ] ;
123+ pfd [1 ].fd = writer . err ;
141124 pfd [1 ].events = POLLIN ;
142125 if (poll (pfd , 2 , -1 ) < 0 ) {
143126 if (errno != EINTR ) {
@@ -156,9 +139,7 @@ int cmd_upload_archive(int argc, const char **argv, const char *prefix)
156139 if (process_input (pfd [0 ].fd , 1 ))
157140 continue ;
158141
159- if (waitpid (writer , & status , 0 ) < 0 )
160- error_clnt ("%s" , lostchild );
161- else if (!WIFEXITED (status ) || WEXITSTATUS (status ) > 0 )
142+ if (finish_command (& writer ))
162143 error_clnt ("%s" , deadchild );
163144 packet_flush (1 );
164145 break ;
0 commit comments