Skip to content

Commit

Permalink
Merge branch 'jc/xwrite-cleanup'
Browse files Browse the repository at this point in the history
Uses of xwrite() helper have been audited and updated for better
error checking and simpler code.

* jc/xwrite-cleanup:
  repack: check error writing to pack-objects subprocess
  sideband: avoid short write(2)
  unpack: replace xwrite() loop with write_in_full()
  • Loading branch information
gitster committed Mar 15, 2024
2 parents 06ac518 + 4c9355f commit d4636ae
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 25 deletions.
17 changes: 3 additions & 14 deletions builtin/index-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1524,14 +1524,12 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
struct strbuf pack_name = STRBUF_INIT;
struct strbuf index_name = STRBUF_INIT;
struct strbuf rev_index_name = STRBUF_INIT;
int err;

if (!from_stdin) {
close(input_fd);
} else {
fsync_component_or_die(FSYNC_COMPONENT_PACK, output_fd, curr_pack_name);
err = close(output_fd);
if (err)
if (close(output_fd))
die_errno(_("error while closing pack file"));
}

Expand Down Expand Up @@ -1566,17 +1564,8 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
write_or_die(1, buf.buf, buf.len);
strbuf_release(&buf);

/*
* Let's just mimic git-unpack-objects here and write
* the last part of the input buffer to stdout.
*/
while (input_len) {
err = xwrite(1, input_buffer + input_offset, input_len);
if (err <= 0)
break;
input_len -= err;
input_offset += err;
}
/* Write the last part of the buffer to stdout */
write_in_full(1, input_buffer + input_offset, input_len);
}

strbuf_release(&rev_index_name);
Expand Down
5 changes: 3 additions & 2 deletions builtin/repack.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,9 @@ static int write_oid(const struct object_id *oid,
die(_("could not start pack-objects to repack promisor objects"));
}

xwrite(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz);
xwrite(cmd->in, "\n", 1);
if (write_in_full(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz) < 0 ||
write_in_full(cmd->in, "\n", 1) < 0)
die(_("failed to feed promisor objects to pack-objects"));
return 0;
}

Expand Down
8 changes: 1 addition & 7 deletions builtin/unpack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -679,13 +679,7 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix UNUSED)
use(the_hash_algo->rawsz);

/* Write the last part of the buffer to stdout */
while (len) {
int ret = xwrite(1, buffer + offset, len);
if (ret <= 0)
break;
len -= ret;
offset += ret;
}
write_in_full(1, buffer + offset, len);

/* All done */
return has_errors;
Expand Down
4 changes: 2 additions & 2 deletions sideband.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ int demultiplex_sideband(const char *me, int status,
}

strbuf_addch(scratch, *brk);
xwrite(2, scratch->buf, scratch->len);
write_in_full(2, scratch->buf, scratch->len);
strbuf_reset(scratch);

b = brk + 1;
Expand All @@ -247,7 +247,7 @@ int demultiplex_sideband(const char *me, int status,
die("%s", scratch->buf);
if (scratch->len) {
strbuf_addch(scratch, '\n');
xwrite(2, scratch->buf, scratch->len);
write_in_full(2, scratch->buf, scratch->len);
}
strbuf_release(scratch);
return 1;
Expand Down

0 comments on commit d4636ae

Please sign in to comment.