@@ -70,6 +70,19 @@ static int write_rr(struct string_list *rr, int out_fd)
7070 return 0 ;
7171}
7272
73+ static void ferr_write (const void * p , size_t count , FILE * fp , int * err )
74+ {
75+ if (!count || * err )
76+ return ;
77+ if (fwrite (p , count , 1 , fp ) != 1 )
78+ * err = errno ;
79+ }
80+
81+ static inline void ferr_puts (const char * s , FILE * fp , int * err )
82+ {
83+ ferr_write (s , strlen (s ), fp , err );
84+ }
85+
7386static int handle_file (const char * path ,
7487 unsigned char * sha1 , const char * output )
7588{
@@ -82,6 +95,7 @@ static int handle_file(const char *path,
8295 struct strbuf one = STRBUF_INIT , two = STRBUF_INIT ;
8396 FILE * f = fopen (path , "r" );
8497 FILE * out = NULL ;
98+ int wrerror = 0 ;
8599
86100 if (!f )
87101 return error ("Could not open %s" , path );
@@ -118,11 +132,11 @@ static int handle_file(const char *path,
118132 hunk_no ++ ;
119133 hunk = RR_CONTEXT ;
120134 if (out ) {
121- fputs ("<<<<<<<\n" , out );
122- fwrite (one .buf , one .len , 1 , out );
123- fputs ("=======\n" , out );
124- fwrite (two .buf , two .len , 1 , out );
125- fputs (">>>>>>>\n" , out );
135+ ferr_puts ("<<<<<<<\n" , out , & wrerror );
136+ ferr_write (one .buf , one .len , out , & wrerror );
137+ ferr_puts ("=======\n" , out , & wrerror );
138+ ferr_write (two .buf , two .len , out , & wrerror );
139+ ferr_puts (">>>>>>>\n" , out , & wrerror );
126140 }
127141 if (sha1 ) {
128142 git_SHA1_Update (& ctx , one .buf ? one .buf : "" ,
@@ -139,7 +153,7 @@ static int handle_file(const char *path,
139153 else if (hunk == RR_SIDE_2 )
140154 strbuf_addstr (& two , buf );
141155 else if (out )
142- fputs (buf , out );
156+ ferr_puts (buf , out , & wrerror );
143157 continue ;
144158 bad :
145159 hunk = 99 ; /* force error exit */
@@ -149,15 +163,21 @@ static int handle_file(const char *path,
149163 strbuf_release (& two );
150164
151165 fclose (f );
152- if (out )
153- fclose (out );
166+ if (wrerror )
167+ error ("There were errors while writing %s (%s)" ,
168+ path , strerror (wrerror ));
169+ if (out && fclose (out ))
170+ wrerror = error ("Failed to flush %s: %s" ,
171+ path , strerror (errno ));
154172 if (sha1 )
155173 git_SHA1_Final (sha1 , & ctx );
156174 if (hunk != RR_CONTEXT ) {
157175 if (output )
158176 unlink (output );
159177 return error ("Could not parse conflict hunks in %s" , path );
160178 }
179+ if (wrerror )
180+ return -1 ;
161181 return hunk_no ;
162182}
163183
@@ -200,9 +220,13 @@ static int merge(const char *name, const char *path)
200220 if (!ret ) {
201221 FILE * f = fopen (path , "w" );
202222 if (!f )
203- return error ("Could not write to %s" , path );
204- fwrite (result .ptr , result .size , 1 , f );
205- fclose (f );
223+ return error ("Could not open %s: %s" , path ,
224+ strerror (errno ));
225+ if (fwrite (result .ptr , result .size , 1 , f ) != 1 )
226+ error ("Could not write %s: %s" , path , strerror (errno ));
227+ if (fclose (f ))
228+ return error ("Writing %s failed: %s" , path ,
229+ strerror (errno ));
206230 }
207231
208232 free (cur .ptr );
0 commit comments