From bf32ff32f848feb6fba9c4b5cb13652e14898f59 Mon Sep 17 00:00:00 2001
From: Julian Oes <julian@oes.ch>
Date: Wed, 24 Apr 2019 10:58:53 +0200
Subject: [PATCH] dataman: make _file_write more readable

This should not be any functional change. The only difference are the
variable scopes, and early return versus nested ifs.
---
 src/modules/dataman/dataman.cpp | 19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)

diff --git a/src/modules/dataman/dataman.cpp b/src/modules/dataman/dataman.cpp
index 2cc751064f..325faa574c 100644
--- a/src/modules/dataman/dataman.cpp
+++ b/src/modules/dataman/dataman.cpp
@@ -495,11 +495,9 @@ static ssize_t
 _file_write(dm_item_t item, unsigned index, dm_persitence_t persistence, const void *buf, size_t count)
 {
 	unsigned char buffer[g_per_item_size[item]];
-	size_t len;
-	int offset;
 
 	/* Get the offset for this item */
-	offset = calculate_offset(item, index);
+	const int offset = calculate_offset(item, index);
 
 	/* If item type or index out of range, return error */
 	if (offset < 0) {
@@ -523,20 +521,17 @@ _file_write(dm_item_t item, unsigned index, dm_persitence_t persistence, const v
 
 	count += DM_SECTOR_HDR_SIZE;
 
-	len = -1;
-
-	/* Seek to the right spot in the data manager file and write the data item */
-	if (lseek(dm_operations_data.file.fd, offset, SEEK_SET) == offset) {
-		if ((len = write(dm_operations_data.file.fd, buffer, count)) == count) {
-			fsync(dm_operations_data.file.fd);        /* Make sure data is written to physical media */
-		}
+	if (lseek(dm_operations_data.file.fd, offset, SEEK_SET) != offset) {
+		return -1;
 	}
 
-	/* Make sure the write succeeded */
-	if (len != count) {
+	if ((write(dm_operations_data.file.fd, buffer, count)) != (ssize_t)count) {
 		return -1;
 	}
 
+	/* Make sure data is written to physical media */
+	fsync(dm_operations_data.file.fd);
+
 	/* All is well... return the number of user data written */
 	return count - DM_SECTOR_HDR_SIZE;
 }
-- 
GitLab