updates from orpheus for windoze build
[feisty_meow.git] / nucleus / library / filesystem / huge_file.cpp
index 4c218c56673b1b4be612ca6e5bc64c29f2124006..5d8e7dc0db9393ba887ff9c9d7b1d37a7d8bd9b5 100644 (file)
 #include <basis/byte_array.h>
 #include <basis/functions.h>
 #include <basis/guards.h>
+#include <application/windoze_helper.h>
 
 #include <stdio.h>
+//#ifndef __WIN32__
+#include <sys/time.h>
+//#else
+//  #include <time.h>
+//#endif
 
 #undef LOG
 #define LOG(to_print) printf("%s::%s: %s\n", static_class_name(), func, astring(to_print).s())
@@ -42,6 +48,8 @@ huge_file::~huge_file()
   WHACK(_real_file);
 }
 
+const astring &huge_file::name() const { return _real_file->name(); }
+
 void huge_file::flush() { _real_file->flush(); }
 
 bool huge_file::truncate() { return _real_file->truncate(); }
@@ -124,7 +132,7 @@ double huge_file::length()
         // something malfunctioned.  we should always be able to get back to
         // the last good size we found if the file is static.
         LOG(a_sprintf("failed to seek back to best highest %.0f on ",
-            best_highest) + _real_file->filename());
+            best_highest) + _real_file->name());
         // try to repair our ideas about the file by starting the process
         // over.
 //hmmm: count the number of times restarted and bail after N.
@@ -134,7 +142,7 @@ double huge_file::length()
           // the heck with this.  we can't even go back to the start.  this
           // file seems to be screwed up now.
           LOG(astring("failed to seek back to start of file!  on ")
-              + _real_file->filename());
+              + _real_file->name());
           return 0;
         }
         // reset the rest of the positions for our failed attempt to return
@@ -254,7 +262,7 @@ outcome huge_file::seek(double new_position, byte_filer::origins origin)
 
 outcome huge_file::read(byte_array &to_fill, int desired_size, int &size_read)
 {
-//  FUNCDEF("read");
+  FUNCDEF("read");
   size_read = 0;
   int ret = _real_file->read(to_fill, desired_size);
   if (ret < 0)
@@ -266,7 +274,7 @@ outcome huge_file::read(byte_array &to_fill, int desired_size, int &size_read)
 
 outcome huge_file::write(const byte_array &to_write, int &size_written)
 {
-//  FUNCDEF("write");
+  FUNCDEF("write");
   size_written = 0;
   int ret = _real_file->write(to_write);
   if (ret < 0)
@@ -276,5 +284,44 @@ outcome huge_file::write(const byte_array &to_write, int &size_written)
   return OKAY;
 }
 
+basis::outcome huge_file::touch()
+{
+  FUNCDEF("touch")
+  if (filename(_real_file->name()).exists()) {
+    // file exists, so just update time.
+#ifndef __WIN32__
+    int ret = utimes(_real_file->name().observe(), NULL_POINTER);
+    if (ret != 0)
+      return FAILURE;
+#else
+    // open the file, although the function says create in its name...
+    HANDLE f = CreateFile(_real_file->name().observe(),
+        GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
+        NULL_POINTER, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL_POINTER);
+    if (!f) {
+      LOG(a_sprintf("failed to open file %s", _real_file->name().observe()));
+      return FAILURE;
+    }
+    // get current system time in UTC.
+    SYSTEMTIME *st = new SYSTEMTIME;
+    GetSystemTime(st);
+    // convert system time into file time.
+    FILETIME *t = new FILETIME;
+    SystemTimeToFileTime(st, t);
+    // set the file's time.
+    SetFileTime(f, NULL_POINTER, t, t);
+#endif
+  } else {
+    // file doesn't exist yet.
+    byte_array junk(1);
+    int written;
+    outcome ret = write(junk, written);
+    if (ret != OKAY) ret;
+    if (!truncate())
+      return FAILURE;
+  }
+  return OKAY;
+}
+
 } //namespace.