Merge "Check all lseek calls succeed."
This commit is contained in:
+1
-1
@@ -42,7 +42,7 @@ set_usb_driver(bool enabled) {
|
|||||||
ui->Print("failed to open driver control: %s\n", strerror(errno));
|
ui->Print("failed to open driver control: %s\n", strerror(errno));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (write(fd, enabled ? "1" : "0", 1) < 0) {
|
if (TEMP_FAILURE_RETRY(write(fd, enabled ? "1" : "0", 1)) == -1) {
|
||||||
ui->Print("failed to set driver control: %s\n", strerror(errno));
|
ui->Print("failed to set driver control: %s\n", strerror(errno));
|
||||||
}
|
}
|
||||||
if (close(fd) < 0) {
|
if (close(fd) < 0) {
|
||||||
|
|||||||
+27
-24
@@ -422,20 +422,19 @@ int WriteToPartition(unsigned char* data, size_t len,
|
|||||||
int attempt;
|
int attempt;
|
||||||
|
|
||||||
for (attempt = 0; attempt < 2; ++attempt) {
|
for (attempt = 0; attempt < 2; ++attempt) {
|
||||||
lseek(fd, start, SEEK_SET);
|
if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) {
|
||||||
|
printf("failed seek on %s: %s\n",
|
||||||
|
partition, strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
while (start < len) {
|
while (start < len) {
|
||||||
size_t to_write = len - start;
|
size_t to_write = len - start;
|
||||||
if (to_write > 1<<20) to_write = 1<<20;
|
if (to_write > 1<<20) to_write = 1<<20;
|
||||||
|
|
||||||
ssize_t written = write(fd, data+start, to_write);
|
ssize_t written = TEMP_FAILURE_RETRY(write(fd, data+start, to_write));
|
||||||
if (written < 0) {
|
if (written == -1) {
|
||||||
if (errno == EINTR) {
|
printf("failed write writing to %s: %s\n", partition, strerror(errno));
|
||||||
written = 0;
|
return -1;
|
||||||
} else {
|
|
||||||
printf("failed write writing to %s (%s)\n",
|
|
||||||
partition, strerror(errno));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
start += written;
|
start += written;
|
||||||
}
|
}
|
||||||
@@ -460,13 +459,20 @@ int WriteToPartition(unsigned char* data, size_t len,
|
|||||||
// won't just be reading the cache.
|
// won't just be reading the cache.
|
||||||
sync();
|
sync();
|
||||||
int dc = open("/proc/sys/vm/drop_caches", O_WRONLY);
|
int dc = open("/proc/sys/vm/drop_caches", O_WRONLY);
|
||||||
write(dc, "3\n", 2);
|
if (TEMP_FAILURE_RETRY(write(dc, "3\n", 2)) == -1) {
|
||||||
|
printf("write to /proc/sys/vm/drop_caches failed: %s\n", strerror(errno));
|
||||||
|
} else {
|
||||||
|
printf(" caches dropped\n");
|
||||||
|
}
|
||||||
close(dc);
|
close(dc);
|
||||||
sleep(1);
|
sleep(1);
|
||||||
printf(" caches dropped\n");
|
|
||||||
|
|
||||||
// verify
|
// verify
|
||||||
lseek(fd, 0, SEEK_SET);
|
if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) {
|
||||||
|
printf("failed to seek back to beginning of %s: %s\n",
|
||||||
|
partition, strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
unsigned char buffer[4096];
|
unsigned char buffer[4096];
|
||||||
start = len;
|
start = len;
|
||||||
size_t p;
|
size_t p;
|
||||||
@@ -476,15 +482,12 @@ int WriteToPartition(unsigned char* data, size_t len,
|
|||||||
|
|
||||||
size_t so_far = 0;
|
size_t so_far = 0;
|
||||||
while (so_far < to_read) {
|
while (so_far < to_read) {
|
||||||
ssize_t read_count = read(fd, buffer+so_far, to_read-so_far);
|
ssize_t read_count =
|
||||||
if (read_count < 0) {
|
TEMP_FAILURE_RETRY(read(fd, buffer+so_far, to_read-so_far));
|
||||||
if (errno == EINTR) {
|
if (read_count == -1) {
|
||||||
read_count = 0;
|
printf("verify read error %s at %zu: %s\n",
|
||||||
} else {
|
partition, p, strerror(errno));
|
||||||
printf("verify read error %s at %zu: %s\n",
|
return -1;
|
||||||
partition, p, strerror(errno));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if ((size_t)read_count < to_read) {
|
if ((size_t)read_count < to_read) {
|
||||||
printf("short verify read %s at %zu: %zd %zu %s\n",
|
printf("short verify read %s at %zu: %zd %zu %s\n",
|
||||||
@@ -625,8 +628,8 @@ ssize_t FileSink(const unsigned char* data, ssize_t len, void* token) {
|
|||||||
ssize_t done = 0;
|
ssize_t done = 0;
|
||||||
ssize_t wrote;
|
ssize_t wrote;
|
||||||
while (done < (ssize_t) len) {
|
while (done < (ssize_t) len) {
|
||||||
wrote = write(fd, data+done, len-done);
|
wrote = TEMP_FAILURE_RETRY(write(fd, data+done, len-done));
|
||||||
if (wrote <= 0) {
|
if (wrote == -1) {
|
||||||
printf("error writing %d bytes: %s\n", (int)(len-done), strerror(errno));
|
printf("error writing %d bytes: %s\n", (int)(len-done), strerror(errno));
|
||||||
return done;
|
return done;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,19 +36,17 @@ struct file_data {
|
|||||||
static int read_block_file(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size) {
|
static int read_block_file(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size) {
|
||||||
struct file_data* fd = (struct file_data*)cookie;
|
struct file_data* fd = (struct file_data*)cookie;
|
||||||
|
|
||||||
if (lseek(fd->fd, block * fd->block_size, SEEK_SET) < 0) {
|
off64_t offset = ((off64_t) block) * fd->block_size;
|
||||||
printf("seek on sdcard failed: %s\n", strerror(errno));
|
if (TEMP_FAILURE_RETRY(lseek64(fd->fd, offset, SEEK_SET)) == -1) {
|
||||||
|
fprintf(stderr, "seek on sdcard failed: %s\n", strerror(errno));
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (fetch_size > 0) {
|
while (fetch_size > 0) {
|
||||||
ssize_t r = read(fd->fd, buffer, fetch_size);
|
ssize_t r = TEMP_FAILURE_RETRY(read(fd->fd, buffer, fetch_size));
|
||||||
if (r < 0) {
|
if (r == -1) {
|
||||||
if (r != -EINTR) {
|
fprintf(stderr, "read on sdcard failed: %s\n", strerror(errno));
|
||||||
printf("read on sdcard failed: %s\n", strerror(errno));
|
return -EIO;
|
||||||
return -EIO;
|
|
||||||
}
|
|
||||||
r = 0;
|
|
||||||
}
|
}
|
||||||
fetch_size -= r;
|
fetch_size -= r;
|
||||||
buffer += r;
|
buffer += r;
|
||||||
|
|||||||
+7
-9
@@ -442,14 +442,12 @@ int run_fuse_sideload(struct provider_vtab* vtab, void* cookie,
|
|||||||
}
|
}
|
||||||
uint8_t request_buffer[sizeof(struct fuse_in_header) + PATH_MAX*8];
|
uint8_t request_buffer[sizeof(struct fuse_in_header) + PATH_MAX*8];
|
||||||
for (;;) {
|
for (;;) {
|
||||||
ssize_t len = read(fd.ffd, request_buffer, sizeof(request_buffer));
|
ssize_t len = TEMP_FAILURE_RETRY(read(fd.ffd, request_buffer, sizeof(request_buffer)));
|
||||||
if (len < 0) {
|
if (len == -1) {
|
||||||
if (errno != EINTR) {
|
perror("read request");
|
||||||
perror("read request");
|
if (errno == ENODEV) {
|
||||||
if (errno == ENODEV) {
|
result = -1;
|
||||||
result = -1;
|
break;
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -508,7 +506,7 @@ int run_fuse_sideload(struct provider_vtab* vtab, void* cookie,
|
|||||||
outhdr.len = sizeof(outhdr);
|
outhdr.len = sizeof(outhdr);
|
||||||
outhdr.error = result;
|
outhdr.error = result;
|
||||||
outhdr.unique = hdr->unique;
|
outhdr.unique = hdr->unique;
|
||||||
write(fd.ffd, &outhdr, sizeof(outhdr));
|
TEMP_FAILURE_RETRY(write(fd.ffd, &outhdr, sizeof(outhdr)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -165,7 +166,7 @@ void ev_dispatch(void) {
|
|||||||
|
|
||||||
int ev_get_input(int fd, uint32_t epevents, input_event* ev) {
|
int ev_get_input(int fd, uint32_t epevents, input_event* ev) {
|
||||||
if (epevents & EPOLLIN) {
|
if (epevents & EPOLLIN) {
|
||||||
ssize_t r = read(fd, ev, sizeof(*ev));
|
ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev)));
|
||||||
if (r == sizeof(*ev)) {
|
if (r == sizeof(*ev)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-4
@@ -27,11 +27,13 @@ static int getFileStartAndLength(int fd, off_t *start_, size_t *length_)
|
|||||||
assert(start_ != NULL);
|
assert(start_ != NULL);
|
||||||
assert(length_ != NULL);
|
assert(length_ != NULL);
|
||||||
|
|
||||||
start = lseek(fd, 0L, SEEK_CUR);
|
// TODO: isn't start always 0 for the single call site? just use fstat instead?
|
||||||
end = lseek(fd, 0L, SEEK_END);
|
|
||||||
(void) lseek(fd, start, SEEK_SET);
|
|
||||||
|
|
||||||
if (start == (off_t) -1 || end == (off_t) -1) {
|
start = TEMP_FAILURE_RETRY(lseek(fd, 0L, SEEK_CUR));
|
||||||
|
end = TEMP_FAILURE_RETRY(lseek(fd, 0L, SEEK_END));
|
||||||
|
|
||||||
|
if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1 ||
|
||||||
|
start == (off_t) -1 || end == (off_t) -1) {
|
||||||
LOGE("could not determine length of file\n");
|
LOGE("could not determine length of file\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-4
@@ -675,13 +675,11 @@ static bool writeProcessFunction(const unsigned char *data, int dataLen,
|
|||||||
}
|
}
|
||||||
ssize_t soFar = 0;
|
ssize_t soFar = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
ssize_t n = write(fd, data+soFar, dataLen-soFar);
|
ssize_t n = TEMP_FAILURE_RETRY(write(fd, data+soFar, dataLen-soFar));
|
||||||
if (n <= 0) {
|
if (n <= 0) {
|
||||||
LOGE("Error writing %zd bytes from zip file from %p: %s\n",
|
LOGE("Error writing %zd bytes from zip file from %p: %s\n",
|
||||||
dataLen-soFar, data+soFar, strerror(errno));
|
dataLen-soFar, data+soFar, strerror(errno));
|
||||||
if (errno != EINTR) {
|
return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else if (n > 0) {
|
} else if (n > 0) {
|
||||||
soFar += n;
|
soFar += n;
|
||||||
if (soFar == dataLen) return true;
|
if (soFar == dataLen) return true;
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ int main(int argc, char **argv) {
|
|||||||
if (fd < 0) die("error opening %s", argv[2]);
|
if (fd < 0) die("error opening %s", argv[2]);
|
||||||
|
|
||||||
char header[HEADER_SIZE];
|
char header[HEADER_SIZE];
|
||||||
int headerlen = read(fd, header, sizeof(header));
|
int headerlen = TEMP_FAILURE_RETRY(read(fd, header, sizeof(header)));
|
||||||
if (headerlen <= 0) die("error reading %s header", argv[2]);
|
if (headerlen <= 0) die("error reading %s header", argv[2]);
|
||||||
|
|
||||||
MtdReadContext *in = mtd_read_partition(partition);
|
MtdReadContext *in = mtd_read_partition(partition);
|
||||||
@@ -104,7 +104,7 @@ int main(int argc, char **argv) {
|
|||||||
if (wrote != headerlen) die("error writing %s", argv[1]);
|
if (wrote != headerlen) die("error writing %s", argv[1]);
|
||||||
|
|
||||||
int len;
|
int len;
|
||||||
while ((len = read(fd, buf, sizeof(buf))) > 0) {
|
while ((len = TEMP_FAILURE_RETRY(read(fd, buf, sizeof(buf)))) > 0) {
|
||||||
wrote = mtd_write_data(out, buf, len);
|
wrote = mtd_write_data(out, buf, len);
|
||||||
if (wrote != len) die("error writing %s", argv[1]);
|
if (wrote != len) die("error writing %s", argv[1]);
|
||||||
}
|
}
|
||||||
@@ -125,13 +125,13 @@ int main(int argc, char **argv) {
|
|||||||
if (mtd_partition_info(partition, NULL, &block_size, NULL))
|
if (mtd_partition_info(partition, NULL, &block_size, NULL))
|
||||||
die("error getting %s block size", argv[1]);
|
die("error getting %s block size", argv[1]);
|
||||||
|
|
||||||
if (lseek(fd, headerlen, SEEK_SET) != headerlen)
|
if (TEMP_FAILURE_RETRY(lseek(fd, headerlen, SEEK_SET)) != headerlen)
|
||||||
die("error rewinding %s", argv[2]);
|
die("error rewinding %s", argv[2]);
|
||||||
|
|
||||||
int left = block_size - headerlen;
|
int left = block_size - headerlen;
|
||||||
while (left < 0) left += block_size;
|
while (left < 0) left += block_size;
|
||||||
while (left > 0) {
|
while (left > 0) {
|
||||||
len = read(fd, buf, left > (int)sizeof(buf) ? (int)sizeof(buf) : left);
|
len = TEMP_FAILURE_RETRY(read(fd, buf, left > (int)sizeof(buf) ? (int)sizeof(buf) : left));
|
||||||
if (len <= 0) die("error reading %s", argv[2]);
|
if (len <= 0) die("error reading %s", argv[2]);
|
||||||
if (mtd_write_data(out, buf, len) != len)
|
if (mtd_write_data(out, buf, len) != len)
|
||||||
die("error writing %s", argv[1]);
|
die("error writing %s", argv[1]);
|
||||||
|
|||||||
+22
-32
@@ -108,7 +108,7 @@ mtd_scan_partitions()
|
|||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
goto bail;
|
goto bail;
|
||||||
}
|
}
|
||||||
nbytes = read(fd, buf, sizeof(buf) - 1);
|
nbytes = TEMP_FAILURE_RETRY(read(fd, buf, sizeof(buf) - 1));
|
||||||
close(fd);
|
close(fd);
|
||||||
if (nbytes < 0) {
|
if (nbytes < 0) {
|
||||||
goto bail;
|
goto bail;
|
||||||
@@ -279,12 +279,6 @@ MtdReadContext *mtd_read_partition(const MtdPartition *partition)
|
|||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Seeks to a location in the partition. Don't mix with reads of
|
|
||||||
// anything other than whole blocks; unpredictable things will result.
|
|
||||||
void mtd_read_skip_to(const MtdReadContext* ctx, size_t offset) {
|
|
||||||
lseek64(ctx->fd, offset, SEEK_SET);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int read_block(const MtdPartition *partition, int fd, char *data)
|
static int read_block(const MtdPartition *partition, int fd, char *data)
|
||||||
{
|
{
|
||||||
struct mtd_ecc_stats before, after;
|
struct mtd_ecc_stats before, after;
|
||||||
@@ -293,13 +287,18 @@ static int read_block(const MtdPartition *partition, int fd, char *data)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
loff_t pos = lseek64(fd, 0, SEEK_CUR);
|
loff_t pos = TEMP_FAILURE_RETRY(lseek64(fd, 0, SEEK_CUR));
|
||||||
|
if (pos == -1) {
|
||||||
|
printf("mtd: read_block: couldn't SEEK_CUR: %s\n", strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
ssize_t size = partition->erase_size;
|
ssize_t size = partition->erase_size;
|
||||||
int mgbb;
|
int mgbb;
|
||||||
|
|
||||||
while (pos + size <= (int) partition->size) {
|
while (pos + size <= (int) partition->size) {
|
||||||
if (lseek64(fd, pos, SEEK_SET) != pos || read(fd, data, size) != size) {
|
if (TEMP_FAILURE_RETRY(lseek64(fd, pos, SEEK_SET)) != pos ||
|
||||||
|
TEMP_FAILURE_RETRY(read(fd, data, size)) != size) {
|
||||||
printf("mtd: read error at 0x%08llx (%s)\n",
|
printf("mtd: read error at 0x%08llx (%s)\n",
|
||||||
pos, strerror(errno));
|
pos, strerror(errno));
|
||||||
} else if (ioctl(fd, ECCGETSTATS, &after)) {
|
} else if (ioctl(fd, ECCGETSTATS, &after)) {
|
||||||
@@ -409,8 +408,11 @@ static int write_block(MtdWriteContext *ctx, const char *data)
|
|||||||
const MtdPartition *partition = ctx->partition;
|
const MtdPartition *partition = ctx->partition;
|
||||||
int fd = ctx->fd;
|
int fd = ctx->fd;
|
||||||
|
|
||||||
off_t pos = lseek(fd, 0, SEEK_CUR);
|
off_t pos = TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_CUR));
|
||||||
if (pos == (off_t) -1) return 1;
|
if (pos == (off_t) -1) {
|
||||||
|
printf("mtd: write_block: couldn't SEEK_CUR: %s\n", strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
ssize_t size = partition->erase_size;
|
ssize_t size = partition->erase_size;
|
||||||
while (pos + size <= (int) partition->size) {
|
while (pos + size <= (int) partition->size) {
|
||||||
@@ -435,15 +437,15 @@ static int write_block(MtdWriteContext *ctx, const char *data)
|
|||||||
pos, strerror(errno));
|
pos, strerror(errno));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (lseek(fd, pos, SEEK_SET) != pos ||
|
if (TEMP_FAILURE_RETRY(lseek(fd, pos, SEEK_SET)) != pos ||
|
||||||
write(fd, data, size) != size) {
|
TEMP_FAILURE_RETRY(write(fd, data, size)) != size) {
|
||||||
printf("mtd: write error at 0x%08lx (%s)\n",
|
printf("mtd: write error at 0x%08lx (%s)\n",
|
||||||
pos, strerror(errno));
|
pos, strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
char verify[size];
|
char verify[size];
|
||||||
if (lseek(fd, pos, SEEK_SET) != pos ||
|
if (TEMP_FAILURE_RETRY(lseek(fd, pos, SEEK_SET)) != pos ||
|
||||||
read(fd, verify, size) != size) {
|
TEMP_FAILURE_RETRY(read(fd, verify, size)) != size) {
|
||||||
printf("mtd: re-read error at 0x%08lx (%s)\n",
|
printf("mtd: re-read error at 0x%08lx (%s)\n",
|
||||||
pos, strerror(errno));
|
pos, strerror(errno));
|
||||||
continue;
|
continue;
|
||||||
@@ -512,8 +514,11 @@ off_t mtd_erase_blocks(MtdWriteContext *ctx, int blocks)
|
|||||||
ctx->stored = 0;
|
ctx->stored = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
off_t pos = lseek(ctx->fd, 0, SEEK_CUR);
|
off_t pos = TEMP_FAILURE_RETRY(lseek(ctx->fd, 0, SEEK_CUR));
|
||||||
if ((off_t) pos == (off_t) -1) return pos;
|
if ((off_t) pos == (off_t) -1) {
|
||||||
|
printf("mtd_erase_blocks: couldn't SEEK_CUR: %s\n", strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
const int total = (ctx->partition->size - pos) / ctx->partition->erase_size;
|
const int total = (ctx->partition->size - pos) / ctx->partition->erase_size;
|
||||||
if (blocks < 0) blocks = total;
|
if (blocks < 0) blocks = total;
|
||||||
@@ -554,18 +559,3 @@ int mtd_write_close(MtdWriteContext *ctx)
|
|||||||
free(ctx);
|
free(ctx);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return the offset of the first good block at or after pos (which
|
|
||||||
* might be pos itself).
|
|
||||||
*/
|
|
||||||
off_t mtd_find_write_start(MtdWriteContext *ctx, off_t pos) {
|
|
||||||
int i;
|
|
||||||
for (i = 0; i < ctx->bad_block_count; ++i) {
|
|
||||||
if (ctx->bad_block_offsets[i] == pos) {
|
|
||||||
pos += ctx->partition->erase_size;
|
|
||||||
} else if (ctx->bad_block_offsets[i] > pos) {
|
|
||||||
return pos;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return pos;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -49,12 +49,10 @@ typedef struct MtdWriteContext MtdWriteContext;
|
|||||||
MtdReadContext *mtd_read_partition(const MtdPartition *);
|
MtdReadContext *mtd_read_partition(const MtdPartition *);
|
||||||
ssize_t mtd_read_data(MtdReadContext *, char *data, size_t data_len);
|
ssize_t mtd_read_data(MtdReadContext *, char *data, size_t data_len);
|
||||||
void mtd_read_close(MtdReadContext *);
|
void mtd_read_close(MtdReadContext *);
|
||||||
void mtd_read_skip_to(const MtdReadContext *, size_t offset);
|
|
||||||
|
|
||||||
MtdWriteContext *mtd_write_partition(const MtdPartition *);
|
MtdWriteContext *mtd_write_partition(const MtdPartition *);
|
||||||
ssize_t mtd_write_data(MtdWriteContext *, const char *data, size_t data_len);
|
ssize_t mtd_write_data(MtdWriteContext *, const char *data, size_t data_len);
|
||||||
off_t mtd_erase_blocks(MtdWriteContext *, int blocks); /* 0 ok, -1 for all */
|
off_t mtd_erase_blocks(MtdWriteContext *, int blocks); /* 0 ok, -1 for all */
|
||||||
off_t mtd_find_write_start(MtdWriteContext *ctx, off_t pos);
|
|
||||||
int mtd_write_close(MtdWriteContext *);
|
int mtd_write_close(MtdWriteContext *);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ int main(int argc __attribute__((unused)), char **argv __attribute__((unused)))
|
|||||||
snprintf(fn, sizeof(fn), "%s/%s", kPartitions[i], "dirty");
|
snprintf(fn, sizeof(fn), "%s/%s", kPartitions[i], "dirty");
|
||||||
fd = open(fn, O_WRONLY|O_CREAT, 0444);
|
fd = open(fn, O_WRONLY|O_CREAT, 0444);
|
||||||
if (fd >= 0) { // Don't sweat it if we can't write the file.
|
if (fd >= 0) { // Don't sweat it if we can't write the file.
|
||||||
write(fd, fn, sizeof(fn)); // write, you know, some data
|
TEMP_FAILURE_RETRY(write(fd, fn, sizeof(fn))); // write, you know, some data
|
||||||
close(fd);
|
close(fd);
|
||||||
unlink(fn);
|
unlink(fn);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -251,7 +251,7 @@ bool RecoveryUI::IsUsbConnected() {
|
|||||||
|
|
||||||
char buf;
|
char buf;
|
||||||
// USB is connected if android_usb state is CONNECTED or CONFIGURED.
|
// USB is connected if android_usb state is CONNECTED or CONFIGURED.
|
||||||
int connected = (read(fd, &buf, 1) == 1) && (buf == 'C');
|
int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C');
|
||||||
if (close(fd) < 0) {
|
if (close(fd) < 0) {
|
||||||
printf("failed to close /sys/class/android_usb/android0/state: %s\n",
|
printf("failed to close /sys/class/android_usb/android0/state: %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
|
|||||||
+12
-8
@@ -65,12 +65,15 @@ static struct fstab* fstab = NULL;
|
|||||||
static int write_at_offset(unsigned char* buffer, size_t size,
|
static int write_at_offset(unsigned char* buffer, size_t size,
|
||||||
int wfd, off64_t offset)
|
int wfd, off64_t offset)
|
||||||
{
|
{
|
||||||
lseek64(wfd, offset, SEEK_SET);
|
if (TEMP_FAILURE_RETRY(lseek64(wfd, offset, SEEK_SET)) == -1) {
|
||||||
|
ALOGE("error seeking to offset %lld: %s\n", offset, strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
size_t written = 0;
|
size_t written = 0;
|
||||||
while (written < size) {
|
while (written < size) {
|
||||||
ssize_t wrote = write(wfd, buffer + written, size - written);
|
ssize_t wrote = TEMP_FAILURE_RETRY(write(wfd, buffer + written, size - written));
|
||||||
if (wrote < 0) {
|
if (wrote == -1) {
|
||||||
ALOGE("error writing offset %lld: %s\n", offset, strerror(errno));
|
ALOGE("error writing offset %lld: %s\n", (offset + written), strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
written += wrote;
|
written += wrote;
|
||||||
@@ -275,8 +278,9 @@ int produce_block_map(const char* path, const char* map_file, const char* blk_de
|
|||||||
if (encrypted) {
|
if (encrypted) {
|
||||||
size_t so_far = 0;
|
size_t so_far = 0;
|
||||||
while (so_far < sb.st_blksize && pos < sb.st_size) {
|
while (so_far < sb.st_blksize && pos < sb.st_size) {
|
||||||
ssize_t this_read = read(fd, buffers[tail] + so_far, sb.st_blksize - so_far);
|
ssize_t this_read =
|
||||||
if (this_read < 0) {
|
TEMP_FAILURE_RETRY(read(fd, buffers[tail] + so_far, sb.st_blksize - so_far));
|
||||||
|
if (this_read == -1) {
|
||||||
ALOGE("failed to read: %s\n", strerror(errno));
|
ALOGE("failed to read: %s\n", strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -340,8 +344,8 @@ void wipe_misc() {
|
|||||||
size_t written = 0;
|
size_t written = 0;
|
||||||
size_t size = sizeof(zeroes);
|
size_t size = sizeof(zeroes);
|
||||||
while (written < size) {
|
while (written < size) {
|
||||||
ssize_t w = write(fd, zeroes, size-written);
|
ssize_t w = TEMP_FAILURE_RETRY(write(fd, zeroes, size-written));
|
||||||
if (w < 0 && errno != EINTR) {
|
if (w == -1) {
|
||||||
ALOGE("zero write failed: %s\n", strerror(errno));
|
ALOGE("zero write failed: %s\n", strerror(errno));
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+20
-28
@@ -113,13 +113,12 @@ static int range_overlaps(RangeSet* r1, RangeSet* r2) {
|
|||||||
static int read_all(int fd, uint8_t* data, size_t size) {
|
static int read_all(int fd, uint8_t* data, size_t size) {
|
||||||
size_t so_far = 0;
|
size_t so_far = 0;
|
||||||
while (so_far < size) {
|
while (so_far < size) {
|
||||||
ssize_t r = read(fd, data+so_far, size-so_far);
|
ssize_t r = TEMP_FAILURE_RETRY(read(fd, data+so_far, size-so_far));
|
||||||
if (r < 0 && errno != EINTR) {
|
if (r == -1) {
|
||||||
fprintf(stderr, "read failed: %s\n", strerror(errno));
|
fprintf(stderr, "read failed: %s\n", strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
|
||||||
so_far += r;
|
|
||||||
}
|
}
|
||||||
|
so_far += r;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -127,13 +126,12 @@ static int read_all(int fd, uint8_t* data, size_t size) {
|
|||||||
static int write_all(int fd, const uint8_t* data, size_t size) {
|
static int write_all(int fd, const uint8_t* data, size_t size) {
|
||||||
size_t written = 0;
|
size_t written = 0;
|
||||||
while (written < size) {
|
while (written < size) {
|
||||||
ssize_t w = write(fd, data+written, size-written);
|
ssize_t w = TEMP_FAILURE_RETRY(write(fd, data+written, size-written));
|
||||||
if (w < 0 && errno != EINTR) {
|
if (w == -1) {
|
||||||
fprintf(stderr, "write failed: %s\n", strerror(errno));
|
fprintf(stderr, "write failed: %s\n", strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
|
||||||
written += w;
|
|
||||||
}
|
}
|
||||||
|
written += w;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fsync(fd) == -1) {
|
if (fsync(fd) == -1) {
|
||||||
@@ -144,19 +142,13 @@ static int write_all(int fd, const uint8_t* data, size_t size) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int check_lseek(int fd, off64_t offset, int whence) {
|
static bool check_lseek(int fd, off64_t offset, int whence) {
|
||||||
while (true) {
|
off64_t rc = TEMP_FAILURE_RETRY(lseek64(fd, offset, whence));
|
||||||
off64_t ret = lseek64(fd, offset, whence);
|
if (rc == -1) {
|
||||||
if (ret < 0) {
|
fprintf(stderr, "lseek64 failed: %s\n", strerror(errno));
|
||||||
if (errno != EINTR) {
|
return false;
|
||||||
fprintf(stderr, "lseek64 failed: %s\n", strerror(errno));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return 0;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void allocate(size_t size, uint8_t** buffer, size_t* buffer_alloc) {
|
static void allocate(size_t size, uint8_t** buffer, size_t* buffer_alloc) {
|
||||||
@@ -213,8 +205,8 @@ static ssize_t RangeSinkWrite(const uint8_t* data, ssize_t size, void* token) {
|
|||||||
rss->p_remain = (rss->tgt->pos[rss->p_block * 2 + 1] -
|
rss->p_remain = (rss->tgt->pos[rss->p_block * 2 + 1] -
|
||||||
rss->tgt->pos[rss->p_block * 2]) * BLOCKSIZE;
|
rss->tgt->pos[rss->p_block * 2]) * BLOCKSIZE;
|
||||||
|
|
||||||
if (check_lseek(rss->fd, (off64_t)rss->tgt->pos[rss->p_block*2] * BLOCKSIZE,
|
if (!check_lseek(rss->fd, (off64_t)rss->tgt->pos[rss->p_block*2] * BLOCKSIZE,
|
||||||
SEEK_SET) == -1) {
|
SEEK_SET)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -306,7 +298,7 @@ static int ReadBlocks(RangeSet* src, uint8_t* buffer, int fd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < src->count; ++i) {
|
for (i = 0; i < src->count; ++i) {
|
||||||
if (check_lseek(fd, (off64_t) src->pos[i * 2] * BLOCKSIZE, SEEK_SET) == -1) {
|
if (!check_lseek(fd, (off64_t) src->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -332,7 +324,7 @@ static int WriteBlocks(RangeSet* tgt, uint8_t* buffer, int fd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < tgt->count; ++i) {
|
for (i = 0; i < tgt->count; ++i) {
|
||||||
if (check_lseek(fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET) == -1) {
|
if (!check_lseek(fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1217,7 +1209,7 @@ static int PerformCommandZero(CommandParameters* params) {
|
|||||||
|
|
||||||
if (params->canwrite) {
|
if (params->canwrite) {
|
||||||
for (i = 0; i < tgt->count; ++i) {
|
for (i = 0; i < tgt->count; ++i) {
|
||||||
if (check_lseek(params->fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET) == -1) {
|
if (!check_lseek(params->fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
|
||||||
goto pczout;
|
goto pczout;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1271,7 +1263,7 @@ static int PerformCommandNew(CommandParameters* params) {
|
|||||||
rss.p_block = 0;
|
rss.p_block = 0;
|
||||||
rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE;
|
rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE;
|
||||||
|
|
||||||
if (check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET) == -1) {
|
if (!check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET)) {
|
||||||
goto pcnout;
|
goto pcnout;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1367,7 +1359,7 @@ static int PerformCommandDiff(CommandParameters* params) {
|
|||||||
rss.p_block = 0;
|
rss.p_block = 0;
|
||||||
rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE;
|
rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE;
|
||||||
|
|
||||||
if (check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET) == -1) {
|
if (!check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET)) {
|
||||||
goto pcdout;
|
goto pcdout;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1906,7 +1898,7 @@ Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) {
|
|||||||
|
|
||||||
int i, j;
|
int i, j;
|
||||||
for (i = 0; i < rs->count; ++i) {
|
for (i = 0; i < rs->count; ++i) {
|
||||||
if (check_lseek(fd, (off64_t)rs->pos[i*2] * BLOCKSIZE, SEEK_SET) == -1) {
|
if (!check_lseek(fd, (off64_t)rs->pos[i*2] * BLOCKSIZE, SEEK_SET)) {
|
||||||
ErrorAbort(state, "failed to seek %s: %s", blockdev_filename->data,
|
ErrorAbort(state, "failed to seek %s: %s", blockdev_filename->data,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
goto done;
|
goto done;
|
||||||
|
|||||||
Reference in New Issue
Block a user