[Replicant] [libsamsung-ipc] [PATCH 08/11] samsung-ipc/utils: Add file_data_size

Denis 'GNUtoo' Carikli GNUtoo at cyberdimension.org
Sun Oct 11 22:22:19 UTC 2020


Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo at cyberdimension.org>
---
 include/samsung-ipc.h |  1 +
 samsung-ipc/utils.c   | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)

diff --git a/include/samsung-ipc.h b/include/samsung-ipc.h
index b5a8315..a03c748 100644
--- a/include/samsung-ipc.h
+++ b/include/samsung-ipc.h
@@ -155,6 +155,7 @@ void *file_data_read(struct ipc_client *client, const char *path, size_t size,
 int file_data_write(struct ipc_client *client, const char *path,
 		    const void *data, size_t size, size_t chunk_size,
 		    unsigned int offset);
+off_t file_data_size(struct ipc_client *client, const char *path);
 int network_iface_up(const char *iface, int domain, int type);
 int network_iface_down(const char *iface, int domain, int type);
 int sysfs_value_read(const char *path);
diff --git a/samsung-ipc/utils.c b/samsung-ipc/utils.c
index 0e6bc2f..3fb4adc 100644
--- a/samsung-ipc/utils.c
+++ b/samsung-ipc/utils.c
@@ -193,6 +193,47 @@ complete:
 	return rc;
 }
 
+off_t file_data_size(struct ipc_client *client, const char *path)
+{
+	int fd = -1;
+	off_t rc = 0;
+	int err = 0;
+
+	if (path == NULL) {
+		ipc_client_log(client, "%s: Failed: path is NULL",
+			       __func__);
+		err = ENOENT;
+		goto error;
+	}
+
+	fd = open(path, O_RDONLY);
+	if (fd < 0) {
+		err = errno;
+		ipc_client_log(client, "%s: open %s failed with error %d: %s",
+			       __func__, path, err, strerror(err));
+		goto error;
+	}
+
+	rc = lseek(fd, 0, SEEK_END);
+	if (rc == -1) {
+		err = errno;
+		ipc_client_log(client, "%s: seek %s failed with error %d: %s",
+			       __func__, path, err, strerror(err));
+		goto error;
+	}
+
+error:
+	if (fd >= 0)
+		close(fd);
+
+	if (err) {
+		errno = err;
+		return -1;
+	} else {
+		return rc;
+	}
+}
+
 int network_iface_up(const char *iface, int domain, int type)
 {
 	struct ifreq ifr;
-- 
2.28.0



More information about the Replicant mailing list