mirror of
https://github.com/fosslinux/live-bootstrap.git
synced 2026-03-24 20:16:32 +01:00
fix(payload-import): add precise read/write/truncation diagnostics and clean partial outputs on copy failure
This commit is contained in:
parent
38f8769fce
commit
e821482aaf
1 changed files with 23 additions and 4 deletions
|
|
@ -37,7 +37,8 @@ static int read_exact(FILE *in, void *buf, unsigned int len)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int copy_exact(FILE *in, FILE *out, unsigned int len)
|
static int copy_exact(FILE *in, FILE *out, unsigned int len,
|
||||||
|
const char *device, const char *name, const char *out_path)
|
||||||
{
|
{
|
||||||
unsigned char *buf;
|
unsigned char *buf;
|
||||||
unsigned int remaining = len;
|
unsigned int remaining = len;
|
||||||
|
|
@ -50,16 +51,34 @@ static int copy_exact(FILE *in, FILE *out, unsigned int len)
|
||||||
|
|
||||||
while (remaining > 0) {
|
while (remaining > 0) {
|
||||||
unsigned int chunk = remaining;
|
unsigned int chunk = remaining;
|
||||||
|
unsigned int copied = len - remaining;
|
||||||
|
size_t nread;
|
||||||
size_t written;
|
size_t written;
|
||||||
if (chunk > COPY_BUFSZ) {
|
if (chunk > COPY_BUFSZ) {
|
||||||
chunk = COPY_BUFSZ;
|
chunk = COPY_BUFSZ;
|
||||||
}
|
}
|
||||||
if (read_exact(in, buf, chunk) != 0) {
|
nread = fread(buf, 1, chunk, in);
|
||||||
|
if (nread != chunk) {
|
||||||
|
if (feof(in)) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"payload-import: truncated payload while reading %s from %s "
|
||||||
|
"(offset=%u wanted=%u got=%u)\n",
|
||||||
|
name, device, copied, chunk, (unsigned int)nread);
|
||||||
|
} else {
|
||||||
|
fprintf(stderr,
|
||||||
|
"payload-import: read error while reading %s from %s "
|
||||||
|
"(offset=%u): %s\n",
|
||||||
|
name, device, copied, strerror(errno));
|
||||||
|
}
|
||||||
free(buf);
|
free(buf);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
written = fwrite(buf, 1, chunk, out);
|
written = fwrite(buf, 1, chunk, out);
|
||||||
if (written != chunk) {
|
if (written != chunk) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"payload-import: write error while writing %s to %s "
|
||||||
|
"(offset=%u wanted=%u wrote=%u): %s\n",
|
||||||
|
name, out_path, copied, chunk, (unsigned int)written, strerror(errno));
|
||||||
free(buf);
|
free(buf);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
@ -254,10 +273,10 @@ static int extract_payload(const char *device, const char *dest_dir)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (copy_exact(in, out, data_len) != 0) {
|
if (copy_exact(in, out, data_len, device, name, out_path) != 0) {
|
||||||
fprintf(stderr, "payload-import: failed while copying %s\n", name);
|
|
||||||
free(name);
|
free(name);
|
||||||
fclose(out);
|
fclose(out);
|
||||||
|
unlink(out_path);
|
||||||
fclose(in);
|
fclose(in);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue