@matheusbsbs / matheus-bagliano / commits / 546bb58232
Fork 0

Use the asyncronous function infrastructure to run the content filter.

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:48 UTC 546bb5823249678bc6ad11e65661d896ed83448a
1 file changed +28 -33
convert.c
+28 -33
@@ -192,15 +192,21 @@ static int crlf_to_worktree(const char *path, const char *src, size_t len,
192 return 1;
193 }
194
195 -static int filter_buffer(int fd, const char *src,
196 - unsigned long size, const char *cmd)
195 +struct filter_params {
196 + const char *src;
197 + unsigned long size;
198 + const char *cmd;
199 +};
200 +
201 +static int filter_buffer(int fd, void *data)
202 {
203 /*
204 * Spawn cmd and feed the buffer contents through its stdin.
205 */
206 struct child_process child_process;
207 + struct filter_params *params = (struct filter_params *)data;
208 int write_err, status;
203 - const char *argv[] = { "sh", "-c", cmd, NULL };
209 + const char *argv[] = { "sh", "-c", params->cmd, NULL };
210
211 memset(&child_process, 0, sizeof(child_process));
212 child_process.argv = argv;
@@ -208,17 +214,17 @@ static int filter_buffer(int fd, const char *src,
214 child_process.out = fd;
215
216 if (start_command(&child_process))
211 - return error("cannot fork to run external filter %s", cmd);
217 + return error("cannot fork to run external filter %s", params->cmd);
218
213 - write_err = (write_in_full(child_process.in, src, size) < 0);
219 + write_err = (write_in_full(child_process.in, params->src, params->size) < 0);
220 if (close(child_process.in))
221 write_err = 1;
222 if (write_err)
217 - error("cannot feed the input to external filter %s", cmd);
223 + error("cannot feed the input to external filter %s", params->cmd);
224
225 status = finish_command(&child_process);
226 if (status)
221 - error("external filter %s failed %d", cmd, -status);
227 + error("external filter %s failed %d", params->cmd, -status);
228 return (write_err || status);
229 }
230
@@ -231,47 +237,36 @@ static int apply_filter(const char *path, const char *src, size_t len,
237 *
238 * (child --> cmd) --> us
239 */
234 - int pipe_feed[2];
235 - int status, ret = 1;
236 - struct child_process child_process;
240 + int ret = 1;
241 struct strbuf nbuf;
242 + struct async async;
243 + struct filter_params params;
244
245 if (!cmd)
246 return 0;
247
242 - memset(&child_process, 0, sizeof(child_process));
243 -
244 - if (pipe(pipe_feed) < 0) {
245 - error("cannot create pipe to run external filter %s", cmd);
246 - return 0;
247 - }
248 + memset(&async, 0, sizeof(async));
249 + async.proc = filter_buffer;
250 + async.data = &params;
251 + params.src = src;
252 + params.size = len;
253 + params.cmd = cmd;
254
255 fflush(NULL);
250 - child_process.pid = fork();
251 - if (child_process.pid < 0) {
252 - error("cannot fork to run external filter %s", cmd);
253 - close(pipe_feed[0]);
254 - close(pipe_feed[1]);
255 - return 0;
256 - }
257 - if (!child_process.pid) {
258 - close(pipe_feed[0]);
259 - exit(filter_buffer(pipe_feed[1], src, len, cmd));
260 - }
261 - close(pipe_feed[1]);
256 + if (start_async(&async))
257 + return 0; /* error was already reported */
258
259 strbuf_init(&nbuf, 0);
264 - if (strbuf_read(&nbuf, pipe_feed[0], len) < 0) {
260 + if (strbuf_read(&nbuf, async.out, len) < 0) {
261 error("read from external filter %s failed", cmd);
262 ret = 0;
263 }
268 - if (close(pipe_feed[0])) {
264 + if (close(async.out)) {
265 error("read from external filter %s failed", cmd);
266 ret = 0;
267 }
272 - status = finish_command(&child_process);
273 - if (status) {
274 - error("external filter %s failed %d", cmd, -status);
268 + if (finish_async(&async)) {
269 + error("external filter %s failed", cmd);
270 ret = 0;
271 }
272