Browse Source

fix touch() on windows

David Rose 23 years ago
parent
commit
5b38176c95

+ 7 - 0
dtool/src/dtoolutil/Sources.pp

@@ -31,3 +31,10 @@
   #define SOURCES test_pfstream.cxx
 #end test_bin_target
 
+#begin test_bin_target
+  #define TARGET test_touch
+  #define LOCAL_LIBS dtoolbase dtoolutil
+
+  #define SOURCES test_touch.cxx
+#end test_bin_target
+

+ 32 - 3
dtool/src/dtoolutil/filename.cxx

@@ -1321,7 +1321,36 @@ open_read_write(fstream &stream) const {
 ////////////////////////////////////////////////////////////////////
 bool Filename::
 touch() const {
-#ifdef HAVE_UTIME_H
+#ifdef WIN32_VC
+  // In Windows, we have to use the Windows API to do this reliably.
+
+  // First, guarantee the file exists (and also get its handle).
+  string os_specific = to_os_specific();
+  HANDLE fhandle;
+  fhandle = CreateFile(os_specific.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE,
+                       NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+  if (fhandle == INVALID_HANDLE_VALUE) {
+    return false;
+  }
+
+  // Now update the file time and date.
+  SYSTEMTIME sysnow;
+  FILETIME ftnow;
+  GetSystemTime(&sysnow);
+  if (!SystemTimeToFileTime(&sysnow, &ftnow)) {
+    CloseHandle(fhandle);
+    return false;
+  }
+  
+  if (!SetFileTime(fhandle, NULL, NULL, &ftnow)) {
+    CloseHandle(fhandle);
+    return false;
+  }
+
+  CloseHandle(fhandle);
+  return true;
+
+#elif defined(HAVE_UTIME_H)
   // Most Unix systems can do this explicitly.
 
   string os_specific = to_os_specific();
@@ -1354,14 +1383,14 @@ touch() const {
     return false;
   }
   return true;
-#else  // HAVE_UTIME_H
+#else  // WIN32, HAVE_UTIME_H
   // Other systems may not have an explicit control over the
   // modification time.  For these systems, we'll just temporarily
   // open the file in append mode, then close it again (it gets closed
   // when the ofstream goes out of scope).
   ofstream file;
   return open_append(file);
-#endif  // HAVE_UTIME_H
+#endif  // WIN32, HAVE_UTIME_H
 }
 
 ////////////////////////////////////////////////////////////////////

+ 35 - 0
dtool/src/dtoolutil/test_touch.cxx

@@ -0,0 +1,35 @@
+// Filename: test_pfstream.cxx
+// Created by:  drose (04Nov02)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#include "dtoolbase.h"
+#include "filename.h"
+
+int 
+main(int argc, char *argv[]) {
+  if (argc < 2) {
+    cout << "test_touch filename [filename ... ]\n";
+    return (1);
+  }
+
+  for (int i = 1; i < argc; i++) {
+    Filename filename(argv[i]);
+    filename.touch();
+  }
+
+  return (0);
+}