Merge branch 'jc/xwrite-cleanup'

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()
maint
Junio C Hamano 2024-03-15 16:06:00 -07:00
commit d4636aea6f
4 changed files with 9 additions and 25 deletions

View File

@ -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"));
}

@ -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);

View File

@ -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;
}


View File

@ -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;

View File

@ -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;
@ -247,7 +247,7 @@ cleanup:
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;