taskfs fix up, minor refactoring
[lunaix-os.git] / lunaix-os / kernel / fs / twimap.c
index e72f6c2e20dc50428d138d7bbdc3fa004a1a49cf..6a6dd0cad05903b0e78ac9cd1c5726cef77ef537 100644 (file)
@@ -6,7 +6,7 @@
 #include <klibc/strfmt.h>
 #include <klibc/string.h>
 
-#include <sys/mm/pagetable.h>
+#include <asm/pagetable.h>
 
 #define TWIMAP_BUFFER_SIZE PAGE_SIZE
 
@@ -39,16 +39,22 @@ int
 twimap_read(struct twimap* map, void* buffer, size_t len, size_t fpos)
 {
     map->buffer = valloc(TWIMAP_BUFFER_SIZE);
+    map->size_acc = 0;
+
     map->reset(map);
 
     // FIXME what if TWIMAP_BUFFER_SIZE is not big enough?
 
-    size_t pos = 0;
-    do {
+    size_t pos = map->size_acc;
+    while (pos <= fpos) {
         map->size_acc = 0;
         map->read(map);
         pos += map->size_acc;
-    } while (pos <= fpos && map->go_next(map));
+        
+        if (!map->go_next(map)) {
+            break;
+        }
+    }
 
     if (pos <= fpos) {
         vfree(map->buffer);
@@ -62,12 +68,16 @@ twimap_read(struct twimap* map, void* buffer, size_t len, size_t fpos)
     size_t acc_size = MIN(len, map->size_acc - (pos - fpos)), rdlen = acc_size;
     memcpy(buffer, map->buffer + (pos - fpos), acc_size);
 
-    while (acc_size < len && map->go_next(map)) {
+    while (acc_size < len) {
         map->size_acc = 0;
         map->read(map);
         rdlen = MIN(len - acc_size, map->size_acc);
         memcpy(buffer + acc_size, map->buffer, rdlen);
         acc_size += rdlen;
+
+        if (!map->go_next(map)) {
+            break;
+        }
     }
 
     if (acc_size <= len - 1) {