refactor the filter process code into a reusable module
Subsystems (2)
v5 cover refactor the filter process code into a reusable module
Hopefully final spin. Updates patch 6 to rename additional instances of "filter" in comment and error messages.
Refactor the filter.<driver>.process code into a separate sub-process module that can be used to reduce the cost of starting up a sub-process for multiple commands. It does this by keeping the external process running and processing all commands by communicating over standard input and standard output using the packet format (pkt-line) based protocol. Full documentation is in Documentation/technical/api-sub-process.txt.
This code is refactored from:
Commit edcc85814c ("convert: add filter.<driver>.process option", 2016-10-16)
keeps the external process running and processes all commandsBen Peart (8):
pkt-line: add packet_read_line_gently()
convert: move packet_write_list() into pkt-line as packet_writel()
convert: Split start_multi_file_filter into two separate functions
convert: Separate generic structures and variables from the filter
specific ones
convert: Update generic functions to only use generic data structures
convert: rename reusable sub-process functions
sub-process: move sub-process functions into separate files
convert: Update subprocess_read_status to not die on EOF
Documentation/technical/api-sub-process.txt | 54 ++++++++++ Makefile | 1 + convert.c | 159 +++++----------------------- pkt-line.c | 31 ++++++ pkt-line.h | 11 ++ sub-process.c | 120 +++++++++++++++++++++ sub-process.h | 46 ++++++++ 7 files changed, 292 insertions(+), 130 deletions(-) create mode 100644 Documentation/technical/api-sub-process.txt create mode 100644 sub-process.c create mode 100644 sub-process.h
Signature · 1 line
2.12.0.windows.1.31.g1548525701.dirty
PATCH v5 1/8 pkt-line: add packet_read_line_gently()
Add packet_read_line_gently() to enable reading a line without dying on EOF.
--- pkt-line.c | 12 ++++++++++++ pkt-line.h | 10 ++++++++++ 2 files changed, 22 insertions(+)
pkt-line.c +12 -0
@@ -323,6 +323,18 @@ char *packet_read_line(int fd, int *len_p)return packet_read_line_generic (fd,NULL ,NULL , len_p); } +int packet_read_line_gently (int fd,int *dst_len,char ** dst_line) +{ +int len =packet_read (fd,NULL ,NULL , + packet_buffer,sizeof (packet_buffer), +PACKET_READ_CHOMP_NEWLINE |PACKET_READ_GENTLE_ON_EOF ); +if (dst_len) + *dst_len = len; +if (dst_line) + *dst_line = (len >0 ) ? packet_buffer :NULL ; +return len; +} +char *packet_read_line_buf (char **src,size_t *src_len,int *dst_len) {return packet_read_line_generic (-1 , src, src_len, dst_len);
pkt-line.h +10 -0
@@ -74,6 +74,16 @@ int packet_read(int fd, char **src_buffer, size_t *src_len, charchar *packet_read_line (int fd,int *size);/* +* Convenience wrapper for packet_read that sets the PACKET_READ_GENTLE_ON_EOF +* and CHOMP_NEWLINE options. The return value specifies the number of bytes +* read into the buffer or -1 on truncated input. if the *dst_line parameter +* is not NULL it will return NULL for a flush packet and otherwise points to +* a static buffer (that may be overwritten by subsequent calls). If the size +* parameter is not NULL, the length of the packet is written to it. +*/ +int packet_read_line_gently (int fd,int *size,char ** dst_line); + +/* * Same as packet_read_line, but read from a buf rather than a descriptor; * see packet_read for details on how src_* is used. */
Signature · 1 line
2.12.0.windows.1.31.g1548525701.dirty
Re: [PATCH v5 1/8] pkt-line: add packet_read_line_gently()
Quoted 17 lines · Ben Peart
Add
packet_read_line_gently()to enable reading a line without dying on EOF.Signed-off-by Ben Peart <[email protected]>---
pkt-line.c| 12 ++++++++++++pkt-line.h| 10 ++++++++++2 files changed, 22 insertions(+)pkt-line.c +0
-0
pkt-line.c char *packet_read_line(int fd, int *len_p) · line 330
Quoted 8 lines
pkt-line.c +5
-0@@ -323,6 +323,18 @@ char *packet_read_line(int fd, int *len_p)return packet_read_line_generic (fd,NULL ,NULL , len_p); } +int packet_read_line_gently (int fd,int *dst_len,char ** dst_line) +{ +int len =packet_read (fd,NULL ,NULL , + packet_buffer,sizeof (packet_buffer), +PACKET_READ_CHOMP_NEWLINE |PACKET_READ_GENTLE_ON_EOF );
Really minor nit: you could align the parameters according to fd similar to the "packet_read_line_generic" code (tab width 8).
pkt-line.c char *packet_read_line(int fd, int *len_p) · line 334
Quoted 4 lines
pkt-line.c +4
-0+if (dst_len) + *dst_len = len; +if (dst_line) + *dst_line = (len >0 ) ? packet_buffer :NULL ;
Minor nit: The explicit check "len > 0" is necessary here as len can be "-1". The original "packet_read_line_generic" just checks for "len". I think it would be nice if the code would be consistent and both would check "len > 0".
Quoted 11 lines
pkt-line.c +3
-0+return len; +} +char *packet_read_line_buf (char **src,size_t *src_len,int *dst_len) {return packet_read_line_generic (-1 , src, src_len, dst_len);pkt-line.h +0
-0
pkt-line.h int packet_read(int fd, char **src_buffer, size_t *src_len, char · line 79
Quoted 6 lines
pkt-line.h +3
-0@@ -74,6 +74,16 @@ int packet_read(int fd, char **src_buffer, size_t *src_len, charchar *packet_read_line (int fd,int *size);/* +* Convenience wrapper for packet_read that sets the PACKET_READ_GENTLE_ON_EOF +* and CHOMP_NEWLINE options. The return value specifies the number of bytes +* read into the buffer or -1 on truncated input. if the *dst_line parameter
s/if/If/
pkt-line.h int packet_read(int fd, char **src_buffer, size_t *src_len, char · line 80
Quoted 1 line
pkt-line.h +1
-0+* is not NULL it will return NULL for a flush packet and otherwise points to
This sentences is a bit confusing to me. Maybe:
If the *dst_line parameter is not NULL, then it will point to a static buffer (that may be overwritten by subsequent calls) or it will return NULL for a flush packet.
... feel free to completely ignore this as I am no native speaker.
pkt-line.h int packet_read(int fd, char **src_buffer, size_t *src_len, char · line 89
Quoted 9 lines
pkt-line.h +6
-0+* a static buffer (that may be overwritten by subsequent calls). If the size +* parameter is not NULL, the length of the packet is written to it. +*/ +int packet_read_line_gently (int fd,int *size,char ** dst_line); + +/* * Same as packet_read_line, but read from a buf rather than a descriptor; * see packet_read for details on how src_* is used. */
If you send another round then you could address the minor nits. If not, then this patch as it is looks good to me.
Thanks,
Lars
PATCH v5 2/8 convert: move packet_write_list() into pkt-line as packet_writel()
In reply to Ben PeartAdd packet_writel() which writes multiple lines in a single call and then calls packet_flush_gently(). Update convert.c to use the new packet_writel() function from pkt-line.
--- convert.c | 23 ++--------------------- pkt-line.c | 19 +++++++++++++++++++ pkt-line.h | 1 + 3 files changed, 22 insertions(+), 21 deletions(-)
convert.c +2 -21
@@ -521,25 +521,6 @@ static struct cmd2process *find_multi_file_filter_entry(struct hashmap *hashmap,return hashmap_get (hashmap, &key,NULL ); } -static int packet_write_list (int fd,const char *line, ...) -{ -va_list args; -int err; -va_start (args, line); -for (;;) { -if (!line) -break ; -if (strlen (line) >LARGE_PACKET_DATA_MAX ) -return -1 ; - err =packet_write_fmt_gently (fd,"%s\n" , line); -if (err) -return err; - line = va_arg(args, const char*); - } -va_end (args); -return packet_flush_gently (fd); -} -static void read_multi_file_filter_status (int fd,struct strbuf *status) {struct strbuf **pair;
@@ -616,7 +597,7 @@ static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, conssigchain_push (SIGPIPE ,SIG_IGN ); - err =packet_write_list (process->in,"git-filter-client" ,"version=2" ,NULL ); + err =packet_writel (process->in,"git-filter-client" ,"version=2" ,NULL );if (err) goto done;
@@ -632,7 +613,7 @@ static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, consif (err) goto done; - err =packet_write_list (process->in,"capability=clean" ,"capability=smudge" ,NULL ); + err =packet_writel (process->in,"capability=clean" ,"capability=smudge" ,NULL );for (;;) { cap_buf =packet_read_line (process->out,NULL );
pkt-line.c +19 -0
@@ -171,6 +171,25 @@ int packet_write_fmt_gently(int fd, const char *fmt, ...)return status; } +int packet_writel (int fd,const char *line, ...) +{ +va_list args; +int err; +va_start (args, line); +for (;;) { +if (!line) +break ; +if (strlen (line) >LARGE_PACKET_DATA_MAX ) +return -1 ; + err =packet_write_fmt_gently (fd,"%s\n" , line); +if (err) +return err; + line = va_arg(args, const char*); + } +va_end (args); +return packet_flush_gently (fd); +} +static int packet_write_gently (const int fd_out,const char *buf,size_t size) {static char packet_write_buffer[LARGE_PACKET_MAX ];
pkt-line.h +1 -0
@@ -25,6 +25,7 @@ void packet_buf_flush(struct strbuf *buf);void packet_buf_write (struct strbuf *buf,const char *fmt, ...) __attribute__((format (printf,2 ,3 )));int packet_flush_gently (int fd);int packet_write_fmt_gently (int fd,const char *fmt, ...) __attribute__((format (printf,2 ,3 ))); +int packet_writel (int fd,const char *line, ...);int write_packetized_from_fd (int fd_in,int fd_out);int write_packetized_from_buf (const char *src_in,size_t len,int fd_out);
Signature · 1 line
2.12.0.windows.1.31.g1548525701.dirty
Re: [PATCH v5 2/8] convert: move packet_write_list() into pkt-line as packet_writel()
Quoted 106 lines · Ben Peart
Add
packet_writel()which writes multiple lines in a single call and then callspacket_flush_gently(). Updateconvert.cto use the newpacket_writel()function from pkt-line.Signed-off-by Ben Peart <[email protected]>---
convert.c | 23 ++--------------------- pkt-line.c | 19 +++++++++++++++++++ pkt-line.h | 1 + 3 files changed, 22 insertions(+), 21 deletions(-)convert.c +2
-21@@ -521,25 +521,6 @@ static struct cmd2process *find_multi_file_filter_entry(struct hashmap *hashmap,return hashmap_get (hashmap, &key,NULL ); } -static int packet_write_list (int fd,const char *line, ...) -{ -va_list args; -int err; -va_start (args, line); -for (;;) { -if (!line) -break ; -if (strlen (line) >LARGE_PACKET_DATA_MAX ) -return -1 ; - err =packet_write_fmt_gently (fd,"%s\n" , line); -if (err) -return err; - line = va_arg(args, const char*); - } -va_end (args); -return packet_flush_gently (fd); -} -static void read_multi_file_filter_status (int fd,struct strbuf *status) {struct strbuf **pair;@@ -616,7 +597,7 @@ static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, conssigchain_push (SIGPIPE ,SIG_IGN ); - err =packet_write_list (process->in,"git-filter-client" ,"version=2" ,NULL ); + err =packet_writel (process->in,"git-filter-client" ,"version=2" ,NULL );if (err) goto done;@@ -632,7 +613,7 @@ static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, consif (err) goto done; - err =packet_write_list (process->in,"capability=clean" ,"capability=smudge" ,NULL ); + err =packet_writel (process->in,"capability=clean" ,"capability=smudge" ,NULL );for (;;) { cap_buf =packet_read_line (process->out,NULL );pkt-line.c +19
-0@@ -171,6 +171,25 @@ int packet_write_fmt_gently(int fd, const char *fmt, ...)return status; } +int packet_writel (int fd,const char *line, ...) +{ +va_list args; +int err; +va_start (args, line); +for (;;) { +if (!line) +break ; +if (strlen (line) >LARGE_PACKET_DATA_MAX ) +return -1 ; + err =packet_write_fmt_gently (fd,"%s\n" , line); +if (err) +return err; + line = va_arg(args, const char*); + } +va_end (args); +return packet_flush_gently (fd); +} +static int packet_write_gently (const int fd_out,const char *buf,size_t size) {static char packet_write_buffer[LARGE_PACKET_MAX ];pkt-line.h +1
-0@@ -25,6 +25,7 @@ void packet_buf_flush(struct strbuf *buf);void packet_buf_write (struct strbuf *buf,const char *fmt, ...) __attribute__((format (printf,2 ,3 )));int packet_flush_gently (int fd);int packet_write_fmt_gently (int fd,const char *fmt, ...) __attribute__((format (printf,2 ,3 ))); +int packet_writel (int fd,const char *line, ...);int write_packetized_from_fd (int fd_in,int fd_out);int write_packetized_from_buf (const char *src_in,size_t len,int fd_out);Signature · 1 line
2.12.0.windows.1.31.g1548525701.dirty
Looks good to me.
Thanks,
Lars
PATCH v5 3/8 convert: Split start_multi_file_filter into two separate functions
In reply to Ben PeartTo enable future reuse of the filter.<driver>.process infrastructure, split start_multi_file_filter into two separate parts.
start_multi_file_filter will now only contain the generic logic to manage the creation and tracking of the child process in a hashmap.
start_multi_file_filter_fn is a protocol specific initialization function that will negotiate the multi-file-filter interface version and capabilities.
--- convert.c | 63 ++++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 38 insertions(+), 25 deletions(-)
convert.c +38 -25
@@ -565,35 +565,14 @@ static void stop_multi_file_filter(struct child_process *process)finish_command (process); } -static struct cmd2process *start_multi_file_filter (struct hashmap *hashmap,const char *cmd) +static int start_multi_file_filter_fn (struct cmd2process *entry) {int err; -struct cmd2process *entry; -struct child_process *process; -const char *argv[] = { cmd,NULL };struct string_list cap_list =STRING_LIST_INIT_NODUP ;char *cap_buf;const char *cap_name; - - entry =xmalloc (sizeof (*entry)); - entry->cmd = cmd; - entry->supported_capabilities =0 ; - process = &entry->process; - -child_process_init (process); - process->argv = argv; - process->use_shell =1 ; - process->in =-1 ; - process->out =-1 ; - process->clean_on_exit =1 ; - process->clean_on_exit_handler = stop_multi_file_filter; - -if (start_command (process)) { -error ("cannot fork to run external filter '%s'" , cmd); -return NULL ; - } - -hashmap_entry_init (entry,strhash (cmd)); +struct child_process *process = &entry->process; +const char *cmd = entry->cmd;sigchain_push (SIGPIPE ,SIG_IGN );
@@ -642,7 +621,41 @@ static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, cons done:sigchain_pop (SIGPIPE ); -if (err || errno ==EPIPE ) { +if (err || errno ==EPIPE ) + err = err ? err : errno; + +return err; +} + +static struct cmd2process *start_multi_file_filter (struct hashmap *hashmap,const char *cmd) +{ +int err; +struct cmd2process *entry; +struct child_process *process; +const char *argv[] = { cmd,NULL }; + + entry =xmalloc (sizeof (*entry)); + entry->cmd = cmd; + entry->supported_capabilities =0 ; + process = &entry->process; + +child_process_init (process); + process->argv = argv; + process->use_shell =1 ; + process->in =-1 ; + process->out =-1 ; + process->clean_on_exit =1 ; + process->clean_on_exit_handler = stop_multi_file_filter; + +if (start_command (process)) { +error ("cannot fork to run external filter '%s'" , cmd); +return NULL ; + } + +hashmap_entry_init (entry,strhash (cmd)); + + err =start_multi_file_filter_fn (entry); +if (err) {error ("initialization for external filter '%s' failed" , cmd);kill_multi_file_filter (hashmap, entry);return NULL ;
Signature · 1 line
2.12.0.windows.1.31.g1548525701.dirty
Re: [PATCH v5 3/8] convert: Split start_multi_file_filter into two separate functions
Quoted 65 lines · Ben Peart
To enable future reuse of the filter.<driver>.process infrastructure, split
start_multi_file_filterinto two separate parts.
start_multi_file_filterwill now only contain the generic logic to manage the creation and tracking of the child process in a hashmap.
start_multi_file_filter_fnis a protocol specific initialization function that will negotiate the multi-file-filter interface version and capabilities.Signed-off-by Ben Peart <[email protected]>---
convert.c| 63 ++++++++++++++++++++++++++++++++++++++-------------------------1 file changed, 38 insertions(+), 25 deletions(-)convert.c +3
-24@@ -565,35 +565,14 @@ static void stop_multi_file_filter(struct child_process *process)finish_command (process); } -static struct cmd2process *start_multi_file_filter (struct hashmap *hashmap,const char *cmd) +static int start_multi_file_filter_fn (struct cmd2process *entry) {int err; -struct cmd2process *entry; -struct child_process *process; -const char *argv[] = { cmd,NULL };struct string_list cap_list =STRING_LIST_INIT_NODUP ;char *cap_buf;const char *cap_name; - - entry =xmalloc (sizeof (*entry)); - entry->cmd = cmd; - entry->supported_capabilities =0 ; - process = &entry->process; - -child_process_init (process); - process->argv = argv; - process->use_shell =1 ; - process->in =-1 ; - process->out =-1 ; - process->clean_on_exit =1 ; - process->clean_on_exit_handler = stop_multi_file_filter; - -if (start_command (process)) { -error ("cannot fork to run external filter '%s'" , cmd); -return NULL ; - } - -hashmap_entry_init (entry,strhash (cmd)); +struct child_process *process = &entry->process; +const char *cmd = entry->cmd;sigchain_push (SIGPIPE ,SIG_IGN );@@ -642,7 +621,41 @@ static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, consdone:
sigchain_pop(SIGPIPE);
convert.c static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, cons · line 625
Quoted 3 lines
convert.c +2
-1-if (err || errno ==EPIPE ) { +if (err || errno ==EPIPE ) + err = err ? err : errno;
Nice! I should have done this here, too:
https://github.com/git/git/blob/b14f27f91770e0f99f64135348977a0ce1c7993a/convert.c#L755
This is clearly a bug in my code. I'll send a patch shortly.
Quoted 33 lines
convert.c +33
-0+ +return err; +} + +static struct cmd2process *start_multi_file_filter (struct hashmap *hashmap,const char *cmd) +{ +int err; +struct cmd2process *entry; +struct child_process *process; +const char *argv[] = { cmd,NULL }; + + entry =xmalloc (sizeof (*entry)); + entry->cmd = cmd; + entry->supported_capabilities =0 ; + process = &entry->process; + +child_process_init (process); + process->argv = argv; + process->use_shell =1 ; + process->in =-1 ; + process->out =-1 ; + process->clean_on_exit =1 ; + process->clean_on_exit_handler = stop_multi_file_filter; + +if (start_command (process)) { +error ("cannot fork to run external filter '%s'" , cmd); +return NULL ; + } + +hashmap_entry_init (entry,strhash (cmd)); + + err =start_multi_file_filter_fn (entry); +if (err) {
Quoted 5 lines
error("initialization for external filter '%s' failed", cmd); kill_multi_file_filter(hashmap, entry); return NULL;Signature · 1 line
2.12.0.windows.1.31.g1548525701.dirty
Looks good to me.
Thanks,
Lars
Re: [PATCH v5 3/8] convert: Split start_multi_file_filter into two separate functions
In reply to Ben Peartconvert.c static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, cons · line 628
Quoted 10 lines · Ben Peart
convert.c +5
-1@@ -642,7 +621,41 @@ static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, cons done:sigchain_pop (SIGPIPE ); -if (err || errno ==EPIPE ) { +if (err || errno ==EPIPE ) + err = err ? err : errno; + +return err; +}
This isn't a new problem introduced by your patch, but this use of errno seems funny to me. Specifically:
1. Do we need to save errno before calling sigchain_pop()? It's making syscalls (though admittedly they are unlikely to fail).
2. If err is 0, then nothing failed. Who would have set errno? Aren't we reading whatever cruft happened to be in errno before the function started?
So I'm confused about what case would trigger on this errno check at all.
Also, I'm not quite sure what the return value of the function is supposed to be; usually we'd use 0 for success and negative for errors. But here we might return a negative value that we got from the packet_* functions, or we might return EPIPE, which is positive. I don't think it's a huge deal because the caller checks "if (err)", but perhaps it should be "-errno" for consistency.
-Peff
Re: [PATCH v5 3/8] convert: Split start_multi_file_filter into two separate functions
Quoted 9 lines · Jeff King
Quoted 4 lines · Ben Peart
(quoted diff) +0
-0@@ -642,7 +621,41 @@ static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, consdone:
sigchain_pop(SIGPIPE);
Quoted 9 lines
convert.c static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, cons · line 628
Quoted 6 lines
convert.c +5
-1-if (err || errno ==EPIPE ) { +if (err || errno ==EPIPE ) + err = err ? err : errno; + +return err; +}This isn't a new problem introduced by your patch, but this use of errno seems funny to me. Specifically:
I introduced these lines, therefore I try to answer :-)
Quoted 2 lines
1. Do we need to save errno before calling
sigchain_pop()? It's making syscalls (though admittedly they are unlikely to fail).
What if we add the following right before sigchain_pop() ?
if (errno == EPIPE) err = -1;
Quoted 3 lines
2. If err is 0, then nothing failed. Who would have set errno? Aren't we reading whatever cruft happened to be in errno before the function started?
Yeah, looks like you're right:
https://www.securecoding.cert.org/confluence/pages/viewpage.action?pageId=6619179
According to this article we shouldn't even check *only* for errno. At least we should add
errno = 0;
at the beginning of the function, no?
This means we have many areas in Git where we don't handle errno correctly. E.g. right in convert.c where I stole code from:
https://github.com/git/git/commit/0c4dd67a048b39470b9b95912e4912fecc405a85#diff-7949b716ab0a83e8c422a0d6336f19d6R361
Should that be addressed?
- Lars
Re: [PATCH v5 3/8] convert: Split start_multi_file_filter into two separate functions
Quoted 7 lines · Lars Schneider
Quoted 2 lines
1. Do we need to save errno before calling
sigchain_pop()? It's making syscalls (though admittedly they are unlikely to fail).What if we add the following right before
sigchain_pop()?if (errno == EPIPE) err = -1;
Yes, that would be fine (though again, this runs against point 2 below).
Quoted 11 lines
Quoted 3 lines
2. If err is 0, then nothing failed. Who would have set errno? Aren't we reading whatever cruft happened to be in errno before the function started?
Yeah, looks like you're right:
https://www.securecoding.cert.org/confluence/pages/viewpage.action?pageId=6619179According to this article we shouldn't even check *only* for errno. At least we should add
errno = 0;at the beginning of the function, no?
If you initialize errno to 0 right before a syscall, then yes, you can trust it without checking the return value of the syscall. I wouldn't trust it before calling more complicated functions, though. Not even xwrite(), which may see EINTR and keep going (which is OK for checking for EPIPE, but not checking generally for errno values).
Quoted 5 lines
This means we have many areas in Git where we don't handle errno correctly. E.g. right in
convert.cwhere I stole code from:
https://github.com/git/git/commit/0c4dd67a048b39470b9b95912e4912fecc405a85#diff-7949b716ab0a83e8c422a0d6336f19d6R361Should that be addressed?
That one is questionable code, but I don't think it behaves incorrectly. After the write_in_full() call finishes, then either:
1. write_err is 0, and conditional is a noop
2. write_err is non-zero, and errno is relevant
I do think it would be more clear as:
if (write_err && errno == EPIPE)
write_err = 0;
similar to the code right below it.
-Peff
Re: [PATCH v5 3/8] convert: Split start_multi_file_filter into two separate functions
Quoted 27 lines · Jeff King
Quoted 7 lines · Lars Schneider
Quoted 2 lines
1. Do we need to save errno before calling
sigchain_pop()? It's making syscalls (though admittedly they are unlikely to fail).What if we add the following right before
sigchain_pop()?if (errno == EPIPE) err = -1;Yes, that would be fine (though again, this runs against point 2 below).
Quoted 11 lines
Quoted 3 lines
2. If err is 0, then nothing failed. Who would have set errno? Aren't we reading whatever cruft happened to be in errno before the function started?
Yeah, looks like you're right:
https://www.securecoding.cert.org/confluence/pages/viewpage.action?pageId=6619179According to this article we shouldn't even check *only* for errno. At least we should add
errno = 0;at the beginning of the function, no?
Quoted 5 lines
If you initialize errno to 0 right before a syscall, then yes, you can trust it without checking the return value of the syscall. I wouldn't trust it before calling more complicated functions, though. Not even
xwrite(), which may see EINTR and keep going (which is OK for checking for EPIPE, but not checking generally for errno values).
Should we remove all the errno checks here as we don't have any direct "write" etc syscalls anyways then?
- Lars
Re: [PATCH v5 3/8] convert: Split start_multi_file_filter into two separate functions
Quoted 8 lines · Lars Schneider
Quoted 5 lines
If you initialize errno to 0 right before a syscall, then yes, you can trust it without checking the return value of the syscall. I wouldn't trust it before calling more complicated functions, though. Not even
xwrite(), which may see EINTR and keep going (which is OK for checking for EPIPE, but not checking generally for errno values).Should we remove all the errno checks here as we don't have any direct "write" etc syscalls anyways then?
Yeah, you should be trusting the return value from the various sub-functions. Usually you'd check "errno == EPIPE" only when you saw an error return but you want to _ignore_ EPIPE. This is what filter_buffer_or_fd() is doing.
But the code here is the opposite case. It definitely wants to treat EPIPE as an error. But that should be happening already because any EPIPE we get would come with an error-return from one of the packet_write() functions.
So I would say that "err || errno == EPIPE" here can just become "err", and ditto in apply_multi_file_filter().
-Peff
Re: [PATCH v5 3/8] convert: Split start_multi_file_filter into two separate functions
Quoted 14 lines · Jeff King
Quoted 7 lines · Lars Schneider
Quoted 5 lines
If you initialize errno to 0 right before a syscall, then yes, you can trust it without checking the return value of the syscall. I wouldn't trust it before calling more complicated functions, though. Not even
xwrite(), which may see EINTR and keep going (which is OK for checking for EPIPE, but not checking generally for errno values).Should we remove all the errno checks here as we don't have any direct "write" etc syscalls anyways then?
Yeah, you should be trusting the return value from the various sub-functions. Usually you'd check "errno == EPIPE" only when you saw an error return but you want to _ignore_ EPIPE. This is what
filter_buffer_or_fd()is doing.
Quoted 7 lines
But the code here is the opposite case. It definitely wants to treat EPIPE as an error. But that should be happening already because any EPIPE we get would come with an error-return from one of the
packet_write()functions.So I would say that "err || errno == EPIPE" here can just become "err", and ditto in
apply_multi_file_filter().
I'll update it this way in the next spin of the patch series.
Quoted 1 line
-Peff
PATCH v5 4/8 convert: Separate generic structures and variables from the filter specific ones
In reply to Ben PeartTo enable future reuse of the filter.<driver>.process infrastructure, split the cmd2process structure into two separate parts.
subprocess_entry will now contain the generic data required to manage the creation and tracking of the child process in a hashmap. Also move all knowledge of the hashmap into the generic functions.
cmd2process is a filter protocol specific structure that is used to track the negotiated capabilities of the filter.
--- convert.c | 57 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 26 deletions(-)
convert.c +31 -26
@@ -496,29 +496,40 @@ static int apply_single_file_filter(const char *path, const char *src, size_t le#define CAP_CLEAN (1u<<0)#define CAP_SMUDGE (1u<<1) -struct cmd2process { +struct subprocess_entry {struct hashmap_entry ent;/* must be the first member! */ -unsigned int supported_capabilities;const char *cmd;struct child_process process; }; +struct cmd2process { +struct subprocess_entry subprocess;/* must be the first member! */ +unsigned int supported_capabilities; +}; +static int cmd_process_map_initialized;static struct hashmap cmd_process_map; -static int cmd2process_cmp (const struct cmd2process *e1, -const struct cmd2process *e2, +static int cmd2process_cmp (const struct subprocess_entry *e1, +const struct subprocess_entry *e2,const void *unused) {return strcmp (e1->cmd, e2->cmd); } -static struct cmd2process *find_multi_file_filter_entry (struct hashmap *hashmap,const char *cmd) +static struct subprocess_entry *find_multi_file_filter_entry (const char *cmd) { -struct cmd2process key; +struct subprocess_entry key; + +if (!cmd_process_map_initialized) { + cmd_process_map_initialized =1 ; +hashmap_init (&cmd_process_map, (hashmap_cmp_fn )cmd2process_cmp,0 ); +return NULL ; + } +hashmap_entry_init (&key,strhash (cmd)); key.cmd = cmd; -return hashmap_get (hashmap, &key,NULL ); +return hashmap_get (&cmd_process_map, &key,NULL ); }static void read_multi_file_filter_status (int fd,struct strbuf *status)
@@ -541,7 +552,7 @@ static void read_multi_file_filter_status(int fd, struct strbuf *status) } } -static void kill_multi_file_filter (struct hashmap *hashmap,struct cmd2process *entry) +static void kill_multi_file_filter (struct subprocess_entry *entry) {if (!entry)return ;
@@ -550,7 +561,7 @@ static void kill_multi_file_filter(struct hashmap *hashmap, struct cmd2process *kill (entry->process.pid,SIGTERM );finish_command (&entry->process); -hashmap_remove (hashmap, entry,NULL ); +hashmap_remove (&cmd_process_map, entry,NULL );free (entry); }
@@ -571,8 +582,8 @@ static int start_multi_file_filter_fn(struct cmd2process *entry)struct string_list cap_list =STRING_LIST_INIT_NODUP ;char *cap_buf;const char *cap_name; -struct child_process *process = &entry->process; -const char *cmd = entry->cmd; +struct child_process *process = &entry->subprocess.process; +const char *cmd = entry->subprocess.cmd;sigchain_push (SIGPIPE ,SIG_IGN );
@@ -627,7 +638,7 @@ static int start_multi_file_filter_fn(struct cmd2process *entry)return err; } -static struct cmd2process *start_multi_file_filter (struct hashmap *hashmap,const char *cmd) +static struct cmd2process *start_multi_file_filter (const char *cmd) {int err;struct cmd2process *entry;
@@ -635,9 +646,9 @@ static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, consconst char *argv[] = { cmd,NULL }; entry =xmalloc (sizeof (*entry)); - entry->cmd = cmd; + entry->subprocess.cmd = cmd; entry->supported_capabilities =0 ; - process = &entry->process; + process = &entry->subprocess.process;child_process_init (process); process->argv = argv;
@@ -657,11 +668,11 @@ static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, cons err =start_multi_file_filter_fn (entry);if (err) {error ("initialization for external filter '%s' failed" , cmd); -kill_multi_file_filter (hashmap, entry); +kill_multi_file_filter (&entry->subprocess);return NULL ; } -hashmap_add (hashmap, entry); +hashmap_add (&cmd_process_map, entry);return entry; }
@@ -676,22 +687,16 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t lenstruct strbuf filter_status =STRBUF_INIT ;const char *filter_type; -if (!cmd_process_map_initialized) { - cmd_process_map_initialized =1 ; -hashmap_init (&cmd_process_map, (hashmap_cmp_fn ) cmd2process_cmp,0 ); - entry =NULL ; - }else { - entry =find_multi_file_filter_entry (&cmd_process_map, cmd); - } + entry = (struct cmd2process *)find_multi_file_filter_entry (cmd);fflush (NULL );if (!entry) { - entry =start_multi_file_filter (&cmd_process_map, cmd); + entry =start_multi_file_filter (cmd);if (!entry)return 0 ; } - process = &entry->process; + process = &entry->subprocess.process;if (!(wanted_capability & entry->supported_capabilities))return 0 ;
@@ -762,7 +767,7 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len* Force shutdown and restart if another blob requires filtering. */ error ("external filter '%s' failed" , cmd); -kill_multi_file_filter (&cmd_process_map, entry); +kill_multi_file_filter (&entry->subprocess); } }else {strbuf_swap (dst, &nbuf);
Signature · 1 line
2.12.0.windows.1.31.g1548525701.dirty
Re: [PATCH v5 4/8] convert: Separate generic structures and variables from the filter specific ones
Quoted 43 lines · Ben Peart
To enable future reuse of the filter.<driver>.process infrastructure, split the cmd2process structure into two separate parts.
subprocess_entrywill now contain the generic data required to manage the creation and tracking of the child process in a hashmap. Also move all knowledge of the hashmap into the generic functions.cmd2process is a filter protocol specific structure that is used to track the negotiated capabilities of the filter.
Signed-off-by Ben Peart <[email protected]>---
convert.c| 57 +++++++++++++++++++++++++++++++--------------------------1 file changed, 31 insertions(+), 26 deletions(-)convert.c +6
-3@@ -496,29 +496,40 @@ static int apply_single_file_filter(const char *path, const char *src, size_t le#define CAP_CLEAN (1u<<0)#define CAP_SMUDGE (1u<<1) -struct cmd2process { +struct subprocess_entry {struct hashmap_entry ent;/* must be the first member! */ -unsigned int supported_capabilities;const char *cmd;struct child_process process; }; +struct cmd2process { +struct subprocess_entry subprocess;/* must be the first member! */ +unsigned int supported_capabilities; +}; +static int cmd_process_map_initialized;static struct hashmap cmd_process_map; -static int cmd2process_cmp(const struct cmd2process *e1,
convert.c static int apply_single_file_filter(const char *path, const char *src, size_t le · line 520
Quoted 10 lines
convert.c +3
-2-const struct cmd2process *e2, +static int cmd2process_cmp (const struct subprocess_entry *e1, +const struct subprocess_entry *e2,const void *unused) {return strcmp (e1->cmd, e2->cmd); } -static struct cmd2process *find_multi_file_filter_entry (struct hashmap *hashmap,const char *cmd) +static struct subprocess_entry *find_multi_file_filter_entry (const char *cmd)
I am curious why you removed the hashmap parameter (here and in other pars of this patch). I know the parameter is not strictly necessary as the hashmap is a global variable anyways. However, I think it eases code maintainability in the long run if a function is "as pure as possible" (IOW does rely on global state as less as possible).
As I consider this personal preference I think either way is fine.
convert.c static int apply_single_file_filter(const char *path, const char *src, size_t le · line 529
Quoted 10 lines
convert.c +8
-1{ -struct cmd2process key; +struct subprocess_entry key; + +if (!cmd_process_map_initialized) { + cmd_process_map_initialized =1 ; +hashmap_init (&cmd_process_map, (hashmap_cmp_fn )cmd2process_cmp,0 ); +return NULL ; + } +
I am a bit in doubt about this one. If the process map was not initialized yet, then I would expect "find_multi_file_filter_entry" to return NULL. Creating the hash map as side effect seems a bit unexpected to me.
The rest of the patch looks good to me.
Thanks,
Lars
Re: [PATCH v5 4/8] convert: Separate generic structures and variables from the filter specific ones
Quoted 7 lines · Lars Schneider
convert.c static int apply_single_file_filter(const char *path, const char *src, size_t le · line 520
Quoted 2 lines
convert.c +1
-1-static struct cmd2process *find_multi_file_filter_entry (struct hashmap *hashmap,const char *cmd) +static struct subprocess_entry *find_multi_file_filter_entry (const char *cmd)I am curious why you removed the hashmap parameter (here and in other pars of this patch). I know the parameter is not strictly necessary as the hashmap is a global variable anyways. However, I think it eases code maintainability in the long run if a function is "as pure as possible" (IOW does rely on global state as less as possible).
If the original relied on a global hashmap and this update kept the code like so, I wouldn't mind the end result of this series (i.e. rely on it being global). But that is not the case. It is making the code worse by stopping passing the hashmap through the callchain.
Re: [PATCH v5 4/8] convert: Separate generic structures and variables from the filter specific ones
Quoted 8 lines · Junio C Hamano
Quoted 6 lines · Lars Schneider
convert.c static int apply_single_file_filter(const char *path, const char *src, size_t le · line 520
Quoted 2 lines
convert.c +1
-1-static struct cmd2process *find_multi_file_filter_entry (struct hashmap *hashmap,const char *cmd) +static struct subprocess_entry *find_multi_file_filter_entry (const char *cmd)I am curious why you removed the hashmap parameter (here and in other pars of this patch). I know the parameter is not strictly necessary as the hashmap is a global variable anyways. However, I think it eases code maintainability in the long run if a function is "as pure as possible" (IOW does rely on global state as less as possible).
Quoted 5 lines
If the original relied on a global hashmap and this update kept the code like so, I wouldn't mind the end result of this series (i.e. rely on it being global). But that is not the case. It is making the code worse by stopping passing the hashmap through the callchain.
I debated this myself but there were a couple of things that pushed me to this simpler model. First, it simplified the interface a little as you don't need an explicit init call with state to detect if it has already been initialized and you don't have to pass the hashmap into the various functions. Since it was already a global, I didn't feel like this made it any worse.
Second, since the hashmap key is the exact command that was executed if you ever had two hashmaps with the same key, you'd end up with two copies of the same background process running. I couldn't come up with any scenario where you would want two copies of the exact same command running at the same time so again went with the simpler model.
While I was typing this, I realized that since the background process is a versioned interface, if there were two different parts of the code using background processes and if they both wanted to run the same background process and if they wanted to use different versions of the interface, then you could want two different copies.
That said, I tend to build for things that are needed now rather than those that might be needed in the future. If I've missed a scenario, I'm happy to change the interface.
Re: [PATCH v5 4/8] convert: Separate generic structures and variables from the filter specific ones
Quoted 6 lines · Ben Peart
Quoted 5 lines · Junio C Hamano
Quoted 2 lines · Lars Schneider
However, I think it eases code maintainability in the long run if a function is "as pure as possible" (IOW does rely on global state as less as possible).
Quoted 8 lines
Quoted 5 lines
If the original relied on a global hashmap and this update kept the code like so, I wouldn't mind the end result of this series (i.e. rely on it being global). But that is not the case. It is making the code worse by stopping passing the hashmap through the callchain.
... Since it was already a global, I didn't feel
like this made it any worse.
The code before your series can easily lose the globals with the attached patch, _exactly_ because it is prepared to be reusable by a new caller that supplies its own hashmap by passing it through the callchain. The child-process machinery Lars made for his conversion thing, which you are trying to split out to make it reusable, can be used by somebody other than apply_multi_file_filter() if you do not lose the hashmap; what the new caller needs to do is to supply its own hashmap so that they do not interact with the set of processes used by Lars's conversion machinery.
If we want to lose the global _after_ applying this patch 4/8, don't we have to essentially _undo_ 4/8? How can it not be seen as making it worse?
convert.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
convert.c +3 -3
@@ -503,9 +503,6 @@ struct cmd2process {struct child_process process; }; -static int cmd_process_map_initialized; -static struct hashmap cmd_process_map; -static int cmd2process_cmp (const struct cmd2process *e1,const struct cmd2process *e2,const void *unused)
@@ -682,6 +679,9 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t lenstruct strbuf filter_status =STRBUF_INIT ;const char *filter_type; +static int cmd_process_map_initialized; +static struct hashmap cmd_process_map; +if (!cmd_process_map_initialized) { cmd_process_map_initialized =1 ;hashmap_init (&cmd_process_map, (hashmap_cmp_fn ) cmd2process_cmp,0 );
Re: [PATCH v5 4/8] convert: Separate generic structures and variables from the filter specific ones
Quoted 24 lines · Junio C Hamano
Quoted 5 lines · Ben Peart
Quoted 4 lines · Junio C Hamano
Quoted 2 lines · Lars Schneider
However, I think it eases code maintainability in the long run if a function is "as pure as possible" (IOW does rely on global state as less as possible).
Quoted 7 lines
Quoted 5 lines
If the original relied on a global hashmap and this update kept the code like so, I wouldn't mind the end result of this series (i.e. rely on it being global). But that is not the case. It is making the code worse by stopping passing the hashmap through the callchain.
... Since it was already a global, I didn't feel
like this made it any worse.The code before your series can easily lose the globals with the attached patch, _exactly_ because it is prepared to be reusable by a new caller that supplies its own hashmap by passing it through the callchain. The child-process machinery Lars made for his conversion thing, which you are trying to split out to make it reusable, can be used by somebody other than
apply_multi_file_filter()if you do not lose the hashmap; what the new caller needs to do is to supply its own hashmap so that they do not interact with the set of processes used by Lars's conversion machinery.
Quoted 3 lines
If we want to lose the global _after_ applying this patch 4/8, don't we have to essentially _undo_ 4/8? How can it not be seen as making it worse?
That's fine. I'll flip it back in the next spin and enable multiple pools of sub-processes. We'll still need 4/8 but it will be a smaller change.
Quoted 29 lines
convert.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)convert.c +3
-3@@ -503,9 +503,6 @@ struct cmd2process {struct child_process process; }; -static int cmd_process_map_initialized; -static struct hashmap cmd_process_map; -static int cmd2process_cmp (const struct cmd2process *e1,const struct cmd2process *e2,const void *unused)@@ -682,6 +679,9 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t lenstruct strbuf filter_status =STRBUF_INIT ;const char *filter_type; +static int cmd_process_map_initialized; +static struct hashmap cmd_process_map; +if (!cmd_process_map_initialized) { cmd_process_map_initialized =1 ;hashmap_init (&cmd_process_map, (hashmap_cmp_fn ) cmd2process_cmp,0 );
PATCH v5 5/8 convert: Update generic functions to only use generic data structures
In reply to Ben PeartUpdate all functions that are going to be moved into a reusable module so that they only work with the reusable data structures. Move code that is specific to the filter out into the filter specific functions.
--- convert.c | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-)
convert.c +28 -18
@@ -576,14 +576,15 @@ static void stop_multi_file_filter(struct child_process *process)finish_command (process); } -static int start_multi_file_filter_fn (struct cmd2process *entry) +static int start_multi_file_filter_fn (struct subprocess_entry *subprocess) {int err; +struct cmd2process *entry = (struct cmd2process *)subprocess;struct string_list cap_list =STRING_LIST_INIT_NODUP ;char *cap_buf;const char *cap_name; -struct child_process *process = &entry->subprocess.process; -const char *cmd = entry->subprocess.cmd; +struct child_process *process = &subprocess->process; +const char *cmd = subprocess->cmd;sigchain_push (SIGPIPE ,SIG_IGN );
@@ -638,17 +639,21 @@ static int start_multi_file_filter_fn(struct cmd2process *entry)return err; } -static struct cmd2process *start_multi_file_filter (const char *cmd) +typedef int (*subprocess_start_fn )(struct subprocess_entry *entry); +int start_multi_file_filter (struct subprocess_entry *entry,const char *cmd, +subprocess_start_fn startfn) {int err; -struct cmd2process *entry;struct child_process *process;const char *argv[] = { cmd,NULL }; - entry =xmalloc (sizeof (*entry)); - entry->subprocess.cmd = cmd; - entry->supported_capabilities =0 ; - process = &entry->subprocess.process; +if (!cmd_process_map_initialized) { + cmd_process_map_initialized =1 ; +hashmap_init (&cmd_process_map, (hashmap_cmp_fn )cmd2process_cmp,0 ); + } + + entry->cmd = cmd; + process = &entry->process;child_process_init (process); process->argv = argv;
@@ -658,22 +663,23 @@ static struct cmd2process *start_multi_file_filter(const char *cmd) process->clean_on_exit =1 ; process->clean_on_exit_handler = stop_multi_file_filter; -if (start_command (process)) { + err =start_command (process); +if (err) {error ("cannot fork to run external filter '%s'" , cmd); -return NULL ; +return err; }hashmap_entry_init (entry,strhash (cmd)); - err =start_multi_file_filter_fn (entry); + err =startfn (entry);if (err) {error ("initialization for external filter '%s' failed" , cmd); -kill_multi_file_filter (&entry->subprocess); -return NULL ; +kill_multi_file_filter (entry); +return err; }hashmap_add (&cmd_process_map, entry); -return entry; +return 0 ; } static int apply_multi_file_filter(const char *path, const char *src, size_t len,
@@ -692,9 +698,13 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t lenfflush (NULL );if (!entry) { - entry =start_multi_file_filter (cmd); -if (!entry) + entry =xmalloc (sizeof (*entry)); + entry->supported_capabilities =0 ; + +if (start_multi_file_filter (&entry->subprocess, cmd, start_multi_file_filter_fn)) { +free (entry);return 0 ; + } } process = &entry->subprocess.process;
@@ -767,7 +777,7 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len* Force shutdown and restart if another blob requires filtering. */ error ("external filter '%s' failed" , cmd); -kill_multi_file_filter (&entry->subprocess); +kill_multi_file_filter ((struct subprocess_entry *)entry); } }else {strbuf_swap (dst, &nbuf);
Signature · 1 line
2.12.0.windows.1.31.g1548525701.dirty
Re: [PATCH v5 5/8] convert: Update generic functions to only use generic data structures
Quoted 17 lines · Ben Peart
Update all functions that are going to be moved into a reusable module so that they only work with the reusable data structures. Move code that is specific to the filter out into the filter specific functions.
Signed-off-by Ben Peart <[email protected]>---
convert.c| 46 ++++++++++++++++++++++++++++------------------1 file changed, 28 insertions(+), 18 deletions(-)convert.c +0
-0
convert.c static void stop_multi_file_filter(struct child_process *process) · line 582
Quoted 8 lines
convert.c +2
-1@@ -576,14 +576,15 @@ static void stop_multi_file_filter(struct child_process *process)finish_command (process); } -static int start_multi_file_filter_fn (struct cmd2process *entry) +static int start_multi_file_filter_fn (struct subprocess_entry *subprocess) {int err; +struct cmd2process *entry = (struct cmd2process *)subprocess;
I wonder if a "subprocess_entry" should rather have a generic data pointer for any extra information. However, this is probably bikeshedding and I think this solution is equally good.
Quoted 18 lines
convert.c +2
-2struct string_list cap_list =STRING_LIST_INIT_NODUP ;char *cap_buf;const char *cap_name; -struct child_process *process = &entry->subprocess.process; -const char *cmd = entry->subprocess.cmd; +struct child_process *process = &subprocess->process; +const char *cmd = subprocess->cmd;sigchain_push (SIGPIPE ,SIG_IGN );@@ -638,17 +639,21 @@ static int start_multi_file_filter_fn(struct cmd2process *entry)return err; }static int apply_multi_file_filter(const char *path, const char *src,
size_tlen,
convert.c static int apply_multi_file_filter(const char *path, const char *src, size_t len · line 702
Quoted 7 lines
convert.c +2
-2@@ -692,9 +698,13 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t lenfflush (NULL );if (!entry) { - entry =start_multi_file_filter (cmd); -if (!entry) + entry =xmalloc (sizeof (*entry)); + entry->supported_capabilities =0 ;
If we would use a generic data pointer then we could initialize supported_capabilities in "start_multi_file_filter_fn".
Apart from the bike shedding above this patch looks good to me. Minor nit: It breaks t0021 "invalid process filter must fail (and not hang!)" test case but I assume this is corrected in a later patch.
Cheers,
Lars
PATCH v5 6/8 convert: rename reusable sub-process functions
In reply to Ben PeartDo a mechanical rename of the functions that will become the reusable sub-process module.
--- convert.c | 53 +++++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 26 deletions(-)
convert.c +27 -26
@@ -507,8 +507,8 @@ struct cmd2process {unsigned int supported_capabilities; }; -static int cmd_process_map_initialized; -static struct hashmap cmd_process_map; +static int subprocess_map_initialized; +static struct hashmap subprocess_map; static int cmd2process_cmp(const struct subprocess_entry *e1, const struct subprocess_entry *e2,
@@ -517,22 +517,22 @@ static int cmd2process_cmp(const struct subprocess_entry *e1,return strcmp (e1->cmd, e2->cmd); } -static struct subprocess_entry *find_multi_file_filter_entry (const char *cmd) +static struct subprocess_entry *subprocess_find_entry (const char *cmd) {struct subprocess_entry key; -if (!cmd_process_map_initialized) { - cmd_process_map_initialized =1 ; -hashmap_init (&cmd_process_map, (hashmap_cmp_fn )cmd2process_cmp,0 ); +if (!subprocess_map_initialized) { + subprocess_map_initialized =1 ; +hashmap_init (&subprocess_map, (hashmap_cmp_fn )cmd2process_cmp,0 );return NULL ; }hashmap_entry_init (&key,strhash (cmd)); key.cmd = cmd; -return hashmap_get (&cmd_process_map, &key,NULL ); +return hashmap_get (&subprocess_map, &key,NULL ); } -static void read_multi_file_filter_status (int fd,struct strbuf *status) +static void subprocess_read_status (int fd,struct strbuf *status) {struct strbuf **pair;char *line;
@@ -552,7 +552,7 @@ static void read_multi_file_filter_status(int fd, struct strbuf *status) } } -static void kill_multi_file_filter (struct subprocess_entry *entry) +static void subprocess_stop (struct subprocess_entry *entry) {if (!entry)return ;
@@ -561,14 +561,14 @@ static void kill_multi_file_filter(struct subprocess_entry *entry)kill (entry->process.pid,SIGTERM );finish_command (&entry->process); -hashmap_remove (&cmd_process_map, entry,NULL ); +hashmap_remove (&subprocess_map, entry,NULL );free (entry); } -static void stop_multi_file_filter (struct child_process *process) +static void subprocess_exit_handler (struct child_process *process) {sigchain_push (SIGPIPE ,SIG_IGN ); -/* Closing the pipe signals the filter to initiate a shutdown. */ +/* Closing the pipe signals the subprocess to initiate a shutdown. */ close (process->in);close (process->out);sigchain_pop (SIGPIPE );
@@ -640,16 +640,16 @@ static int start_multi_file_filter_fn(struct subprocess_entry *subprocess) }typedef int (*subprocess_start_fn )(struct subprocess_entry *entry); -int start_multi_file_filter (struct subprocess_entry *entry,const char *cmd, +int subprocess_start (struct subprocess_entry *entry,const char *cmd,subprocess_start_fn startfn) {int err;struct child_process *process;const char *argv[] = { cmd,NULL }; -if (!cmd_process_map_initialized) { - cmd_process_map_initialized =1 ; -hashmap_init (&cmd_process_map, (hashmap_cmp_fn )cmd2process_cmp,0 ); +if (!subprocess_map_initialized) { + subprocess_map_initialized =1 ; +hashmap_init (&subprocess_map, (hashmap_cmp_fn )cmd2process_cmp,0 ); } entry->cmd = cmd;
@@ -661,11 +661,11 @@ int start_multi_file_filter(struct subprocess_entry *entry, const char *cmd, process->in =-1 ; process->out =-1 ; process->clean_on_exit =1 ; - process->clean_on_exit_handler = stop_multi_file_filter; + process->clean_on_exit_handler = subprocess_exit_handler; err =start_command (process);if (err) { -error ("cannot fork to run external filter '%s'" , cmd); +error ("cannot fork to run subprocess '%s'" , cmd);return err; }
@@ -673,12 +673,12 @@ int start_multi_file_filter(struct subprocess_entry *entry, const char *cmd, err =startfn (entry);if (err) { -error ("initialization for external filter '%s' failed" , cmd); -kill_multi_file_filter (entry); +error ("initialization for subprocess '%s' failed" , cmd); +subprocess_stop (entry);return err; } -hashmap_add (&cmd_process_map, entry); +hashmap_add (&subprocess_map, entry);return 0 ; }
@@ -693,7 +693,7 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t lenstruct strbuf filter_status =STRBUF_INIT ;const char *filter_type; - entry = (struct cmd2process *)find_multi_file_filter_entry (cmd); + entry = (struct cmd2process *)subprocess_find_entry (cmd);fflush (NULL );
@@ -701,7 +701,7 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len entry =xmalloc (sizeof (*entry)); entry->supported_capabilities =0 ; -if (start_multi_file_filter (&entry->subprocess, cmd, start_multi_file_filter_fn)) { +if (subprocess_start (&entry->subprocess, cmd, start_multi_file_filter_fn)) {free (entry);return 0 ; }
@@ -746,7 +746,7 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t lenif (err) goto done; -read_multi_file_filter_status (process->out, &filter_status); +subprocess_read_status (process->out, &filter_status); err =strcmp (filter_status.buf,"success" );if (err) goto done;
@@ -755,7 +755,7 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t lenif (err) goto done; -read_multi_file_filter_status (process->out, &filter_status); +subprocess_read_status (process->out, &filter_status); err =strcmp (filter_status.buf,"success" ); done:
@@ -777,7 +777,8 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len* Force shutdown and restart if another blob requires filtering. */ error ("external filter '%s' failed" , cmd); -kill_multi_file_filter ((struct subprocess_entry *)entry); +subprocess_stop ((struct subprocess_entry *)entry); +free (entry); } }else {strbuf_swap (dst, &nbuf);
Signature · 1 line
2.12.0.windows.1.31.g1548525701.dirty
Re: [PATCH v5 6/8] convert: rename reusable sub-process functions
Quoted 40 lines · Ben Peart
Do a mechanical rename of the functions that will become the reusable sub-process module.
Signed-off-by Ben Peart <[email protected]>---
convert.c| 53 +++++++++++++++++++++++++++--------------------------1 file changed, 27 insertions(+), 26 deletions(-)convert.c +1
-1@@ -507,8 +507,8 @@ struct cmd2process {unsigned int supported_capabilities; }; -static void kill_multi_file_filter (struct subprocess_entry *entry) +static void subprocess_stop (struct subprocess_entry *entry){ if (!entry) return;convert.c +1
-1@@ -561,14 +561,14 @@ static void kill_multi_file_filter(struct subprocess_entry *entry)kill (entry->process.pid,SIGTERM );finish_command (&entry->process); -hashmap_remove (&cmd_process_map, entry,NULL ); +hashmap_remove (&subprocess_map, entry,NULL );free (entry); }
convert.c static int apply_multi_file_filter(const char *path, const char *src, size_t len · line 781
Quoted 6 lines
convert.c +2
-1@@ -777,7 +777,8 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len* Force shutdown and restart if another blob requires filtering. */ error ("external filter '%s' failed" , cmd); -kill_multi_file_filter ((struct subprocess_entry *)entry); +subprocess_stop ((struct subprocess_entry *)entry); +free (entry);
I think we perform a double "free" here and in subprocess_stop(), no? Everything else looks good to me!
Thanks,
Lars
PATCH v5 7/8 sub-process: move sub-process functions into separate files
In reply to Ben PeartMove the sub-proces functions into sub-process.h/c. Add documentation for the new module in Documentation/technical/api-sub-process.txt
--- Documentation/technical/api-sub-process.txt | 54 +++++++++++++ Makefile | 1 + convert.c | 119 +--------------------------- sub-process.c | 116 +++++++++++++++++++++++++++ sub-process.h | 46 +++++++++++ 5 files changed, 218 insertions(+), 118 deletions(-) create mode 100644 Documentation/technical/api-sub-process.txt create mode 100644 sub-process.c create mode 100644 sub-process.h
Documentation/technical/api-sub-process.txt new file +54 -0
@@ -0,0 +1,54 @@ +sub-process API +=============== + +The sub-process API makes it possible to run background sub-processes +that should run until the git command exits and communicate with it +through stdin and stdout. This reduces the overhead of having to fork +a new process each time it needs to be communicated with. + +The sub-processes are kept in a hashmap by command name and looked up +via the subprocess_find_entry function. If an existing instance can not +be found then a new process should be created and started. When the +parent git command terminates, all sub-processes are also terminated. + +This API is based on the run-command API. + +Data structures +--------------- + +* `struct subprocess_entry` + +The sub-process structure. Members should not be accessed directly. + +Types +----- + +'int(*subprocess_start_fn)(struct subprocess_entry *entry)':: + + User-supplied function to initialize the sub-process. This is + typically used to negoiate the interface version and capabilities. + + +Functions +--------- + +`subprocess_start`:: + + Start a subprocess and add it to the subprocess hashmap. + +`subprocess_stop`:: + + Kill a subprocess and remove it from the subprocess hashmap. + +`subprocess_find_entry`:: + + Find a subprocess in the subprocess hashmap. + +`subprocess_get_child_process`:: + + Get the underlying `struct child_process` from a subprocess. + +`subprocess_read_status`:: + + Helper function to read packets looking for the last "status=<foo>" + key/value pair.
Makefile +1 -0
@@ -838,6 +838,7 @@ LIB_OBJS += streaming.o LIB_OBJS += string-list.o LIB_OBJS += submodule.o LIB_OBJS += submodule-config.o +LIB_OBJS += sub-process.o LIB_OBJS += symlinks.o LIB_OBJS += tag.o LIB_OBJS += tempfile.o
convert.c +1 -118
142 lines not on this page · raw message
sub-process.c new file +116 -0
116 lines not on this page · raw message
sub-process.h new file +46 -0
46 lines not on this page · raw message
Signature · 1 line
2.12.0.windows.1.31.g1548525701.dirty
Re: [PATCH v5 7/8] sub-process: move sub-process functions into separate files
Quoted 24 lines · Ben Peart
Move the sub-proces functions into sub-process.h/c. Add documentation for the new module in
Documentation/technical/api-sub-process.txtSigned-off-by Ben Peart <[email protected]>---
Documentation/technical/api-sub-process.txt | 54 +++++++++++++ Makefile | 1 + convert.c | 119 +--------------------------- sub-process.c | 116 +++++++++++++++++++++++++++ sub-process.h | 46 +++++++++++ 5 files changed, 218 insertions(+), 118 deletions(-)create mode 100644
Documentation/technical/api-sub-process.txtcreate mode 100644sub-process.c
create mode 100644sub-process.hDocumentation/technical/api-sub-process.txt new file +0
-0
Documentation/technical/api-sub-process.txt line 7
Quoted 7 lines
Documentation/technical/api-sub-process.txt new file +7
-0@@ -0,0 +1,54 @@ +sub-process API +=============== + +The sub-process API makes it possible to run background sub-processes +that should run until the git command exits and communicate with it +through stdin and stdout. This reduces the overhead of having to fork +a new process each time it needs to be communicated with.
Minor nit, maybe:
The sub-process API makes it possible to run background sub-processes for the entire lifetime of a Git invocation. If Git needs to communicate with an external process multiple times, then this can reduces the process invocation overhead. Git and the sub-process communicate through stdin and stdout.
Feel free to ignore as this is bike-shedding.
Quoted 12 lines
Documentation/technical/api-sub-process.txt new file +12
-0+ +The sub-processes are kept in a hashmap by command name and looked up +via the subprocess_find_entry function. If an existing instance can not +be found then a new process should be created and started. When the +parent git command terminates, all sub-processes are also terminated. + +This API is based on the run-command API. + +Data structures +--------------- + +* `struct subprocess_entry`
Documentation/technical/api-sub-process.txt line 29
Quoted 10 lines
Documentation/technical/api-sub-process.txt new file +10
-0+ +The sub-process structure. Members should not be accessed directly. + +Types +----- + +'int(*subprocess_start_fn)(struct subprocess_entry *entry)':: + + User-supplied function to initialize the sub-process. This is + typically used to negoiate the interface version and capabilities.
s/negoiate/negotiate/
Quoted 6 lines
Documentation/technical/api-sub-process.txt new file +6
-0+ + +Functions +--------- + +`subprocess_start`::
Documentation/technical/api-sub-process.txt line 45
Quoted 10 lines
Documentation/technical/api-sub-process.txt new file +10
-0+ + Start a subprocess and add it to the subprocess hashmap. + +`subprocess_stop`:: + + Kill a subprocess and remove it from the subprocess hashmap. + +`subprocess_find_entry`:: + + Find a subprocess in the subprocess hashmap.
As mentioned in an earlier review, this function also initializes the hashmap as (maybe to the user unexpected?) side effect.
Quoted 346 lines
Documentation/technical/api-sub-process.txt new file +9
-0+ +`subprocess_get_child_process`:: + + Get the underlying `struct child_process` from a subprocess. + +`subprocess_read_status`:: + + Helper function to read packets looking for the last "status=<foo>" + key/value pair.Makefile +0
-0@@ -838,6 +838,7 @@ LIB_OBJS += streaming.o
LIB_OBJS+= string-list.oLIB_OBJS+= submodule.oLIB_OBJS+= submodule-config.o
+LIB_OBJS += sub-process.oLIB_OBJS+= symlinks.oLIB_OBJS+= tag.oLIB_OBJS+= tempfile.oconvert.c +1
-118@@ -4,6 +4,7 @@#include "quote.h" #include "sigchain.h" #include "pkt-line.h" +#include "sub-process.h" /* * convert.c - convert a file when checking it out and checking it in. @@ -496,86 +497,11 @@ static int apply_single_file_filter(const char *path, const char *src, size_t le#define CAP_CLEAN (1u<<0)#define CAP_SMUDGE (1u<<1) -struct subprocess_entry { -struct hashmap_entry ent;/* must be the first member! */ -const char *cmd; -struct child_process process; -}; -struct cmd2process {struct subprocess_entry subprocess;/* must be the first member! */ unsigned int supported_capabilities; }; -static int subprocess_map_initialized; -static struct hashmap subprocess_map; - -static int cmd2process_cmp (const struct subprocess_entry *e1, -const struct subprocess_entry *e2, -const void *unused) -{ -return strcmp (e1->cmd, e2->cmd); -} - -static struct subprocess_entry *subprocess_find_entry (const char *cmd) -{ -struct subprocess_entry key; - -if (!subprocess_map_initialized) { - subprocess_map_initialized =1 ; -hashmap_init (&subprocess_map, (hashmap_cmp_fn )cmd2process_cmp,0 ); -return NULL ; - } - -hashmap_entry_init (&key,strhash (cmd)); - key.cmd = cmd; -return hashmap_get (&subprocess_map, &key,NULL ); -} - -static void subprocess_read_status (int fd,struct strbuf *status) -{ -struct strbuf **pair; -char *line; -for (;;) { - line =packet_read_line (fd,NULL ); -if (!line) -break ; - pair =strbuf_split_str (line,'=' ,2 ); -if (pair[0 ] && pair[0 ]->len && pair[1 ]) { -/* the last "status=<foo>" line wins */ -if (!strcmp (pair[0 ]->buf,"status=" )) { -strbuf_reset (status); -strbuf_addbuf (status, pair[1 ]); - } - } -strbuf_list_free (pair); - } -} - -static void subprocess_stop (struct subprocess_entry *entry) -{ -if (!entry) -return ; - - entry->process.clean_on_exit =0 ; -kill (entry->process.pid,SIGTERM ); -finish_command (&entry->process); - -hashmap_remove (&subprocess_map, entry,NULL ); -free (entry); -} - -static void subprocess_exit_handler (struct child_process *process) -{ -sigchain_push (SIGPIPE ,SIG_IGN ); -/* Closing the pipe signals the subprocess to initiate a shutdown. */ -close (process->in); -close (process->out); -sigchain_pop (SIGPIPE ); -/* Finish command will wait until the shutdown is complete. */ -finish_command (process); -} -static int start_multi_file_filter_fn (struct subprocess_entry *subprocess) {int err;@@ -639,49 +565,6 @@ static int start_multi_file_filter_fn(struct subprocess_entry *subprocess)return err; } -typedef int (*subprocess_start_fn )(struct subprocess_entry *entry); -int subprocess_start (struct subprocess_entry *entry,const char *cmd, -subprocess_start_fn startfn) -{ -int err; -struct child_process *process; -const char *argv[] = { cmd,NULL }; - -if (!subprocess_map_initialized) { - subprocess_map_initialized =1 ; -hashmap_init (&subprocess_map, (hashmap_cmp_fn )cmd2process_cmp,0 ); - } - - entry->cmd = cmd; - process = &entry->process; - -child_process_init (process); - process->argv = argv; - process->use_shell =1 ; - process->in =-1 ; - process->out =-1 ; - process->clean_on_exit =1 ; - process->clean_on_exit_handler = subprocess_exit_handler; - - err =start_command (process); -if (err) { -error ("cannot fork to run subprocess '%s'" , cmd); -return err; - } - -hashmap_entry_init (entry,strhash (cmd)); - - err =startfn (entry); -if (err) { -error ("initialization for subprocess '%s' failed" , cmd); -subprocess_stop (entry); -return err; - } - -hashmap_add (&subprocess_map, entry); -return 0 ; -} -static int apply_multi_file_filter (const char *path,const char *src,size_t len,int fd,struct strbuf *dst,const char *cmd,const unsigned int wanted_capability)sub-process.c new file +116
-0@@ -0,0 +1,116 @@ +/* +* Generic implementation of background process infrastructure. +*/ +#include "sub-process.h" +#include "sigchain.h" +#include "pkt-line.h" + +static int subprocess_map_initialized; +static struct hashmap subprocess_map; + +static int cmd2process_cmp (const struct subprocess_entry *e1, +const struct subprocess_entry *e2, +const void *unused) +{ +return strcmp (e1->cmd, e2->cmd); +} + +struct subprocess_entry *subprocess_find_entry (const char *cmd) +{ +struct subprocess_entry key; + +if (!subprocess_map_initialized) { + subprocess_map_initialized =1 ; +hashmap_init (&subprocess_map, (hashmap_cmp_fn )cmd2process_cmp,0 ); +return NULL ; + } + +hashmap_entry_init (&key,strhash (cmd)); + key.cmd = cmd; +return hashmap_get (&subprocess_map, &key,NULL ); +} + +void subprocess_read_status (int fd,struct strbuf *status) +{ +struct strbuf **pair; +char *line; +for (;;) { + line =packet_read_line (fd,NULL ); +if (!line) +break ; + pair =strbuf_split_str (line,'=' ,2 ); +if (pair[0 ] && pair[0 ]->len && pair[1 ]) { +/* the last "status=<foo>" line wins */ +if (!strcmp (pair[0 ]->buf,"status=" )) { +strbuf_reset (status); +strbuf_addbuf (status, pair[1 ]); + } + } +strbuf_list_free (pair); + } +} + +void subprocess_stop (struct subprocess_entry *entry) +{ +if (!entry) +return ; + + entry->process.clean_on_exit =0 ; +kill (entry->process.pid,SIGTERM ); +finish_command (&entry->process); + +hashmap_remove (&subprocess_map, entry,NULL ); +} + +static void subprocess_exit_handler (struct child_process *process) +{ +sigchain_push (SIGPIPE ,SIG_IGN ); +/* Closing the pipe signals the subprocess to initiate a shutdown. */ +close (process->in); +close (process->out); +sigchain_pop (SIGPIPE ); +/* Finish command will wait until the shutdown is complete. */ +finish_command (process); +} + +int subprocess_start (struct subprocess_entry *entry,const char *cmd, +subprocess_start_fn startfn) +{ +int err; +struct child_process *process; +const char *argv[] = { cmd,NULL }; + +if (!subprocess_map_initialized) { + subprocess_map_initialized =1 ; +hashmap_init (&subprocess_map, (hashmap_cmp_fn )cmd2process_cmp,0 ); + } + + entry->cmd = cmd; + process = &entry->process; + +child_process_init (process); + process->argv = argv; + process->use_shell =1 ; + process->in =-1 ; + process->out =-1 ; + process->clean_on_exit =1 ; + process->clean_on_exit_handler = subprocess_exit_handler; + + err =start_command (process); +if (err) { +error ("cannot fork to run subprocess '%s'" , cmd); +return err; + } + +hashmap_entry_init (entry,strhash (cmd)); + + err =startfn (entry); +if (err) { +error ("initialization for subprocess '%s' failed" , cmd); +subprocess_stop (entry); +return err; + } + +hashmap_add (&subprocess_map, entry); +return 0 ; +}sub-process.h new file +46
-0@@ -0,0 +1,46 @@ +#ifndef SUBPROCESS_H +#define SUBPROCESS_H + +#include "git-compat-util.h" +#include "hashmap.h" +#include "run-command.h" + +/* +* Generic implementation of background process infrastructure. +* See Documentation/technical/api-background-process.txt. +*/ + +/* data structures */ + +struct subprocess_entry { +struct hashmap_entry ent;/* must be the first member! */ +const char *cmd; +struct child_process process; +}; + +/* subprocess functions */ + +typedef int (*subprocess_start_fn )(struct subprocess_entry *entry); +int subprocess_start (struct subprocess_entry *entry,const char *cmd, +subprocess_start_fn startfn); + +void subprocess_stop (struct subprocess_entry *entry); + +struct subprocess_entry *subprocess_find_entry (const char *cmd); + +/* subprocess helper functions */ + +static inline struct child_process *subprocess_get_child_process ( +struct subprocess_entry *entry) +{ +return &entry->process; +} + +/* +* Helper function that will read packets looking for "status=<foo>" +* key/value pairs and return the value from the last "status" packet +*/ + +void subprocess_read_status (int fd,struct strbuf *status); + +#endif Signature · 1 line
2.12.0.windows.1.31.g1548525701.dirty
Looks all good to me. This patch also fixes the failing t0021 "invalid process filter must fail (and not hang!)" introduced here:
http://public-inbox.org/git/[email protected]/
TBH, I don't see why this fixes the test, though.
Thanks,
Lars
PATCH v5 8/8 convert: Update subprocess_read_status to not die on EOF
In reply to Ben PeartEnable sub-processes to gracefully handle when the process dies by updating subprocess_read_status to return an error on EOF instead of dying.
Update apply_multi_file_filter to take advantage of the revised subprocess_read_status.
--- convert.c | 10 ++++++++-- sub-process.c | 10 +++++++--- sub-process.h | 2 +- 3 files changed, 16 insertions(+), 6 deletions(-)
convert.c +8 -2
@@ -629,7 +629,10 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t lenif (err) goto done; -subprocess_read_status (process->out, &filter_status); + err =subprocess_read_status (process->out, &filter_status); +if (err) + goto done; + err =strcmp (filter_status.buf,"success" );if (err) goto done;
@@ -638,7 +641,10 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t lenif (err) goto done; -subprocess_read_status (process->out, &filter_status); + err =subprocess_read_status (process->out, &filter_status); +if (err) + goto done; + err =strcmp (filter_status.buf,"success" ); done:
sub-process.c +7 -3
@@ -30,13 +30,15 @@ struct subprocess_entry *subprocess_find_entry(const char *cmd)return hashmap_get (&subprocess_map, &key,NULL ); } -void subprocess_read_status (int fd,struct strbuf *status) +int subprocess_read_status (int fd,struct strbuf *status) {struct strbuf **pair;char *line; +int len; +for (;;) { - line =packet_read_line (fd,NULL ); -if (!line) + len =packet_read_line_gently (fd,NULL , &line); +if ((len ==-1 ) || !line)break ; pair =strbuf_split_str (line,'=' ,2 );if (pair[0 ] && pair[0 ]->len && pair[1 ]) {
@@ -48,6 +50,8 @@ void subprocess_read_status(int fd, struct strbuf *status) }strbuf_list_free (pair); } + +return len ==-1 ? len :0 ; }void subprocess_stop (struct subprocess_entry *entry)
sub-process.h +1 -1
@@ -41,6 +41,6 @@ static inline struct child_process *subprocess_get_child_process(* key/value pairs and return the value from the last "status" packet */ -void subprocess_read_status (int fd,struct strbuf *status); +int subprocess_read_status (int fd,struct strbuf *status);#endif
Signature · 1 line
2.12.0.windows.1.31.g1548525701.dirty
Re: [PATCH v5 8/8] convert: Update subprocess_read_status to not die on EOF
Quoted 55 lines · Ben Peart
Enable sub-processes to gracefully handle when the process dies by updating
subprocess_read_statusto return an error on EOF instead of dying.Update
apply_multi_file_filterto take advantage of the revisedsubprocess_read_status.Signed-off-by Ben Peart <[email protected]>---
convert.c | 10 ++++++++-- sub-process.c | 10 +++++++--- sub-process.h | 2 +- 3 files changed, 16 insertions(+), 6 deletions(-)convert.c +8
-2@@ -629,7 +629,10 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t lenif (err) goto done; -subprocess_read_status (process->out, &filter_status); + err =subprocess_read_status (process->out, &filter_status); +if (err) + goto done; + err =strcmp (filter_status.buf,"success" );if (err) goto done;@@ -638,7 +641,10 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t lenif (err) goto done; -subprocess_read_status (process->out, &filter_status); + err =subprocess_read_status (process->out, &filter_status); +if (err) + goto done; + err =strcmp (filter_status.buf,"success" );done:
sub-process.c +1
-1@@ -30,13 +30,15 @@ struct subprocess_entry *subprocess_find_entry(const char *cmd)return hashmap_get (&subprocess_map, &key,NULL ); } -void subprocess_read_status (int fd,struct strbuf *status) +int subprocess_read_status (int fd,struct strbuf *status)
sub-process.c struct subprocess_entry *subprocess_find_entry(const char *cmd) · line 41
Quoted 10 lines
sub-process.c +4
-2{struct strbuf **pair;char *line; +int len; +for (;;) { - line =packet_read_line (fd,NULL ); -if (!line) + len =packet_read_line_gently (fd,NULL , &line); +if ((len ==-1 ) || !line)
Minor nit: Maybe "if ((len < 0) || !line)" ?
sub-process.c void subprocess_read_status(int fd, struct strbuf *status) · line 54
Quoted 9 lines
sub-process.c +2
-0break ; pair =strbuf_split_str (line,'=' ,2 );if (pair[0 ] && pair[0 ]->len && pair[1 ]) {@@ -48,6 +50,8 @@ void subprocess_read_status(int fd, struct strbuf *status) }strbuf_list_free (pair); } + +return len ==-1 ? len :0 ;
Minor nit: Maybe "return len < 0 ? len : 0;
Quoted 18 lines
sub-process.c +0
-0}void subprocess_stop (struct subprocess_entry *entry)sub-process.h +1
-1@@ -41,6 +41,6 @@ static inline struct child_process *subprocess_get_child_process(* key/value pairs and return the value from the last "status" packet */ -void subprocess_read_status (int fd,struct strbuf *status); +int subprocess_read_status (int fd,struct strbuf *status);#endif Signature · 1 line
2.12.0.windows.1.31.g1548525701.dirty
Looks good to me.
Thanks,
Lars