From 5b38176c95fa07fbfe4590c782287df799659c6c Mon Sep 17 00:00:00 2001 From: David Rose Date: Tue, 5 Nov 2002 00:09:33 +0000 Subject: [PATCH] fix touch() on windows --- dtool/src/dtoolutil/Sources.pp | 7 ++++++ dtool/src/dtoolutil/filename.cxx | 35 +++++++++++++++++++++++++++--- dtool/src/dtoolutil/test_touch.cxx | 35 ++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 dtool/src/dtoolutil/test_touch.cxx diff --git a/dtool/src/dtoolutil/Sources.pp b/dtool/src/dtoolutil/Sources.pp index e99e952842..feacf716b9 100644 --- a/dtool/src/dtoolutil/Sources.pp +++ b/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 + diff --git a/dtool/src/dtoolutil/filename.cxx b/dtool/src/dtoolutil/filename.cxx index c1a3458d76..2baa5f1b39 100644 --- a/dtool/src/dtoolutil/filename.cxx +++ b/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 } //////////////////////////////////////////////////////////////////// diff --git a/dtool/src/dtoolutil/test_touch.cxx b/dtool/src/dtoolutil/test_touch.cxx new file mode 100644 index 0000000000..3ed6bae2c2 --- /dev/null +++ b/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 +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + +#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); +}