[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tyndur-devel] [PATCH 1/4] libc: lio_compat_tell() auslagern



* libc: Bisher hatte ftell() direkt die Funktionalität, um sowohl auf
  LIOv1 als auch LIOv2 die Position zurückgeben zu können. Wenn man es
  raussplittet, ist nicht nur der Code sauberer, sondern man kann es
  auch ohne stdio.h benutzen.

Signed-off-by: Kevin Wolf <kevin@xxxxxxxxxx>
---
 src/modules/include/lostio.h         |  3 +++
 src/modules/lib/lostio/client/seek.c | 33 ++++++++++++++++++++++++++++++++-
 src/modules/lib/stdlibc/file.c       | 25 ++++---------------------
 3 files changed, 39 insertions(+), 22 deletions(-)

diff --git a/src/modules/include/lostio.h b/src/modules/include/lostio.h
index 5846be6..58fe314 100644
--- a/src/modules/include/lostio.h
+++ b/src/modules/include/lostio.h
@@ -239,6 +239,9 @@ int lio_compat_eof(io_resource_t* io_res);
 /// Stream-Position setzen
 bool lio_compat_seek(io_resource_t* io_res, uint64_t offset, int origin);
 
+/// Stream-Position abfragen
+int64_t lio_compat_tell(io_resource_t* io_res);
+
 
 /******************************************************************************
  * LIO2-Server                                                                *
diff --git a/src/modules/lib/lostio/client/seek.c b/src/modules/lib/lostio/client/seek.c
index 94d05cc..c7fdcf0 100644
--- a/src/modules/lib/lostio/client/seek.c
+++ b/src/modules/lib/lostio/client/seek.c
@@ -30,7 +30,8 @@
 #include <stdio.h>
 #include <rpc.h>
 #include <stdint.h>
-#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
 static bool newlio_flseek(io_resource_t* io_res, uint64_t offset, int origin)
 {
@@ -75,3 +76,33 @@ bool lio_compat_seek(io_resource_t* io_res, uint64_t offset, int origin)
         (char*) &seek_request) == 0);
 }
 
+/**
+ * Aktuelle Cursorposition im Dateihandle abfragen
+ *
+ * @param io_res Dateihandle
+ * @return Aktuelle Position in Bytes vom Dateianfang oder -1 bei Fehler
+ */
+int64_t lio_compat_tell(io_resource_t* io_res)
+{
+    io_tell_request_t tell_request;
+    long result;
+
+    if (IS_LIO2(io_res)) {
+        int64_t pos = lio_seek(io_res->lio2_stream, 0, LIO_SEEK_CUR);
+        return (pos < 0 ? -1 : pos);
+    }
+
+    tell_request.id = io_res->id;
+
+    //Da fuer qwords keine Funktion existiert um sie direkt uber RPC zu
+    // empfangen, muss hier ein kleiner Umweg gemacht werden.
+    response_t* resp = rpc_get_response(io_res->pid, "IO_TELL ",
+        sizeof(io_tell_request_t), (char*) &(tell_request));
+    memcpy(&result, resp->data, sizeof(long));
+
+    //Antwort und Daten wieder freigeben
+    free(resp->data);
+    free(resp);
+
+    return result;
+}
diff --git a/src/modules/lib/stdlibc/file.c b/src/modules/lib/stdlibc/file.c
index fc99e17..7482c41 100644
--- a/src/modules/lib/stdlibc/file.c
+++ b/src/modules/lib/stdlibc/file.c
@@ -666,29 +666,12 @@ long ftell(FILE* io_res)
         return EOF;
     }
 
-    if (IS_LIO2_FILE(io_res)) {
-        int64_t pos = lio_seek(io_res->res->lio2_stream, 0, LIO_SEEK_CUR);
-        result = (pos < 0 ? -1 : pos);
-        goto out;
+    if (!IS_LIO2_FILE(io_res)) {
+        //Sonst könnte die Angabe falsch sein
+        fflush(io_res);
     }
 
-    io_tell_request_t tell_request;
-    tell_request.id = io_res->res->id;
-    
-    //Sonst könnte die Angabe falsch sein
-    fflush(io_res);
-
-    //Da fuer qwords keine Funktion existiert um sie direkt uber RPC zu
-    // empfangen, muss hier ein kleiner Umweg gemacht werden.
-    response_t* resp = rpc_get_response(io_res->res->pid, "IO_TELL ",
-        sizeof(io_tell_request_t), (char*) &(tell_request));
-    memcpy(&result, resp->data, sizeof(long));
-
-    //Antwort und Daten wieder freigeben
-    free(resp->data);
-    free(resp);
-
-out:
+    result = lio_compat_tell(io_res->res);
     if (result >= 0) {
         result += io_res->buffer_pos - io_res->ungetc_count;
         if (result < 0) {
-- 
2.1.2