aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>2021-02-16 13:28:10 +0100
committerDenis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>2021-02-16 13:39:13 +0100
commitf72c03f81c6d2cbf5510838a8ae0c99a11f43048 (patch)
treea27a38c25e75252ef28ab0af39979744f97abfcc
parent89bd4a5d1e07e63f9f0f72c02dab81329454b26e (diff)
downloadhardware_replicant_libsamsung-ipc-f72c03f81c6d2cbf5510838a8ae0c99a11f43048.tar.gz
hardware_replicant_libsamsung-ipc-f72c03f81c6d2cbf5510838a8ae0c99a11f43048.tar.bz2
hardware_replicant_libsamsung-ipc-f72c03f81c6d2cbf5510838a8ae0c99a11f43048.zip
samsung-ipc/utils: add data_read and data_write
These wrappers are meant to handle the case where read or writes handles a smaller number of bytes than requested. This way that handling doesn't need to be duplicated everywhere in libsamsung-ipc. Having access to the ipc_client struct could enable future logging of read and writes without having to change any of the code already using data_read and data_write. Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
-rw-r--r--include/samsung-ipc.h3
-rw-r--r--samsung-ipc/utils.c43
2 files changed, 46 insertions, 0 deletions
diff --git a/include/samsung-ipc.h b/include/samsung-ipc.h
index 25106c6..53f1f72 100644
--- a/include/samsung-ipc.h
+++ b/include/samsung-ipc.h
@@ -154,6 +154,9 @@ int ipc_rfs_header_setup(struct ipc_rfs_header *header,
int ipc_rfs_message_setup(const struct ipc_rfs_header *header,
struct ipc_message *message);
+ssize_t data_read(struct ipc_client *client, int fd, void *buf, size_t count);
+ssize_t data_write(struct ipc_client *client, int fd, const void *buf,
+ size_t count);
void *file_data_read(struct ipc_client *client, const char *path, size_t size,
size_t chunk_size, unsigned int offset);
int file_data_write(struct ipc_client *client, const char *path,
diff --git a/samsung-ipc/utils.c b/samsung-ipc/utils.c
index c0734ac..44cbe2d 100644
--- a/samsung-ipc/utils.c
+++ b/samsung-ipc/utils.c
@@ -38,6 +38,49 @@
#include <samsung-ipc.h>
+ssize_t data_read(__attribute__((unused)) struct ipc_client *client, int fd,
+ void *buf, size_t count)
+{
+ /* From read(2): "According to POSIX.1, if count is greater than
+ * SSIZE_MAX, the result is implementation-defined"
+ */
+ ssize_t remaining = (ssize_t)count;
+
+ while (remaining > 0) {
+ ssize_t rc;
+
+ rc = read(fd, buf, count);
+ if (rc == -1)
+ /* errno is passed to the caller */
+ return rc;
+ remaining -= rc;
+ }
+
+ return count;
+}
+
+ssize_t data_write(__attribute__((unused)) struct ipc_client *client, int fd,
+ const void *buf, size_t count)
+{
+ /* From write(2): "According to POSIX.1, if count is greater than
+ * SSIZE_MAX, the result is implementation-defined"
+ */
+ ssize_t remaining = (ssize_t)count;
+
+ while (remaining > 0) {
+ ssize_t rc;
+
+ rc = write(fd, buf, count);
+ if (rc == -1)
+ /* errno is passed to the caller */
+ return rc;
+ remaining -= rc;
+ }
+
+ return count;
+
+}
+
void *file_data_read(struct ipc_client *client, const char *path, size_t size,
size_t chunk_size, unsigned int offset)
{