11#include "cache.h"
2+ #include "pkt-line.h"
23#include <sys/wait.h>
34
45static const char receive_pack_usage [] = "git-receive-pack [--unpack=executable] <git-dir> [heads]" ;
@@ -26,61 +27,9 @@ static int path_match(const char *path, int nr, char **match)
2627 return 0 ;
2728}
2829
29- static void safe_write (int fd , const void * buf , unsigned n )
30- {
31- while (n ) {
32- int ret = write (fd , buf , n );
33- if (ret > 0 ) {
34- buf += ret ;
35- n -= ret ;
36- continue ;
37- }
38- if (!ret )
39- die ("write error (disk full?)" );
40- if (errno == EAGAIN || errno == EINTR )
41- continue ;
42- die ("write error (%s)" , strerror (errno ));
43- }
44- }
45-
46- /*
47- * If we buffered things up above (we don't, but we should),
48- * we'd flush it here
49- */
50- static void flush_safe (int fd )
51- {
52- }
53-
54- /*
55- * Write a packetized stream, where each line is preceded by
56- * its length (including the header) as a 4-byte hex number.
57- * A length of 'zero' means end of stream (and a length of 1-3
58- * would be an error).
59- */
60- #define hex (a ) (hexchar[(a) & 15])
61- static void packet_write (const char * fmt , ...)
62- {
63- static char buffer [1000 ];
64- static char hexchar [] = "0123456789abcdef" ;
65- va_list args ;
66- unsigned n ;
67-
68- va_start (args , fmt );
69- n = vsnprintf (buffer + 4 , sizeof (buffer ) - 4 , fmt , args );
70- va_end (args );
71- if (n >= sizeof (buffer )- 4 )
72- die ("protocol error: impossibly long line" );
73- n += 4 ;
74- buffer [0 ] = hex (n >> 12 );
75- buffer [1 ] = hex (n >> 8 );
76- buffer [2 ] = hex (n >> 4 );
77- buffer [3 ] = hex (n );
78- safe_write (1 , buffer , n );
79- }
80-
8130static void show_ref (const char * path , unsigned char * sha1 )
8231{
83- packet_write ("%s %s\n" , sha1_to_hex (sha1 ), path );
32+ packet_write (1 , "%s %s\n" , sha1_to_hex (sha1 ), path );
8433}
8534
8635static int read_ref (const char * path , unsigned char * sha1 )
@@ -137,66 +86,6 @@ static void write_head_info(const char *base, int nr, char **match)
13786 }
13887}
13988
140- /*
141- * This is all pretty stupid, but we use this packetized line
142- * format to make a streaming format possible without ever
143- * over-running the read buffers. That way we'll never read
144- * into what might be the pack data (which should go to another
145- * process entirely).
146- *
147- * The writing side could use stdio, but since the reading
148- * side can't, we stay with pure read/write interfaces.
149- */
150- static void safe_read (int fd , void * buffer , unsigned size )
151- {
152- int n = 0 ;
153-
154- while (n < size ) {
155- int ret = read (0 , buffer + n , size - n );
156- if (ret < 0 ) {
157- if (errno == EINTR || errno == EAGAIN )
158- continue ;
159- die ("read error (%s)" , strerror (errno ));
160- }
161- if (!ret )
162- die ("unexpected EOF" );
163- n += ret ;
164- }
165- }
166-
167- static int safe_read_line (char * buffer , unsigned size )
168- {
169- int n , len ;
170-
171- safe_read (0 , buffer , 4 );
172-
173- len = 0 ;
174- for (n = 0 ; n < 4 ; n ++ ) {
175- unsigned char c = buffer [n ];
176- len <<= 4 ;
177- if (c >= '0' && c <= '9' ) {
178- len += c - '0' ;
179- continue ;
180- }
181- if (c >= 'a' && c <= 'f' ) {
182- len += c - 'a' + 10 ;
183- continue ;
184- }
185- if (c >= 'A' && c <= 'F' ) {
186- len += c - 'A' + 10 ;
187- continue ;
188- }
189- die ("protocol error: bad line length character" );
190- }
191- if (!len )
192- return 0 ;
193- if (len < 4 || len >= size )
194- die ("protocol error: bad line length %d" , len );
195- safe_read (0 , buffer + 4 , len - 4 );
196- buffer [len ] = 0 ;
197- return len ;
198- }
199-
20089struct line {
20190 struct line * next ;
20291 char data [0 ];
@@ -213,7 +102,7 @@ static void execute_commands(void)
213102 struct line * line = commands ;
214103
215104 while (line ) {
216- printf ( "%s" , line -> data );
105+ fprintf ( stderr , "%s" , line -> data );
217106 line = line -> next ;
218107 }
219108}
@@ -223,7 +112,7 @@ static void read_head_info(void)
223112 struct line * * p = & commands ;
224113 for (;;) {
225114 static char line [1000 ];
226- int len = safe_read_line ( line , sizeof (line ));
115+ int len = packet_read_line ( 0 , line , sizeof (line ));
227116 struct line * n ;
228117 if (!len )
229118 break ;
@@ -242,8 +131,8 @@ static void unpack(void)
242131 if (pid < 0 )
243132 die ("unpack fork failed" );
244133 if (!pid ) {
245- char * const envp [] = { "GIT_DIR= ." , NULL } ;
246- execle (unpacker , unpacker , NULL , envp );
134+ setenv ( "GIT_DIR" , " ." , 1 ) ;
135+ execlp (unpacker , unpacker , NULL );
247136 die ("unpack execute failed" );
248137 }
249138
@@ -311,8 +200,7 @@ int main(int argc, char **argv)
311200 write_head_info ("refs/" , nr_heads , heads );
312201
313202 /* EOF */
314- safe_write (1 , "0000" , 4 );
315- flush_safe (1 );
203+ packet_flush (1 );
316204
317205 read_head_info ();
318206 unpack ();
0 commit comments