Commit 79140c76 authored by topjohnwu's avatar topjohnwu

Proper xxread and xwrite implementation

parent 1f4c595c
...@@ -27,6 +27,6 @@ android.injected.testOnly=false ...@@ -27,6 +27,6 @@ android.injected.testOnly=false
kapt.incremental.apt=true kapt.incremental.apt=true
# Magisk # Magisk
magisk.versionCode=21303 magisk.versionCode=21304
magisk.ndkVersion=21d magisk.ndkVersion=21d
magisk.fullNdkVersion=21.3.6528147 magisk.fullNdkVersion=21.3.6528147
...@@ -61,12 +61,24 @@ int xopenat(int dirfd, const char *pathname, int flags, mode_t mode) { ...@@ -61,12 +61,24 @@ int xopenat(int dirfd, const char *pathname, int flags, mode_t mode) {
return fd; return fd;
} }
// Write exact same size as count
ssize_t xwrite(int fd, const void *buf, size_t count) { ssize_t xwrite(int fd, const void *buf, size_t count) {
int ret = write(fd, buf, count); size_t write_sz = 0;
if (count != ret) { ssize_t ret;
PLOGE("write"); do {
ret = write(fd, (byte *) buf + write_sz, count - write_sz);
if (ret < 0) {
if (errno == EINTR)
continue;
PLOGE("write");
return ret;
}
write_sz += ret;
} while (write_sz != count && ret != 0);
if (write_sz != count) {
PLOGE("write (%zu != %zu)", count, write_sz);
} }
return ret; return write_sz;
} }
// Read error other than EOF // Read error other than EOF
...@@ -81,15 +93,17 @@ ssize_t xread(int fd, void *buf, size_t count) { ...@@ -81,15 +93,17 @@ ssize_t xread(int fd, void *buf, size_t count) {
// Read exact same size as count // Read exact same size as count
ssize_t xxread(int fd, void *buf, size_t count) { ssize_t xxread(int fd, void *buf, size_t count) {
size_t read_sz = 0; size_t read_sz = 0;
int ret = -1; ssize_t ret;
while (read_sz != count && ret != 0) { do {
ret = read(fd, buf, count); ret = read(fd, (byte *) buf + read_sz, count - read_sz);
if (ret < 0) { if (ret < 0) {
if (errno == EINTR)
continue;
PLOGE("read"); PLOGE("read");
return ret; return ret;
} }
read_sz += ret; read_sz += ret;
} } while (read_sz != count && ret != 0);
if (read_sz != count) { if (read_sz != count) {
PLOGE("read (%zu != %zu)", count, read_sz); PLOGE("read (%zu != %zu)", count, read_sz);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment