fix touch() on windows

This commit is contained in:
David Rose 2002-11-05 00:09:33 +00:00
parent 48e742c25c
commit 5b38176c95
3 changed files with 74 additions and 3 deletions

View File

@ -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

View File

@ -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
}
////////////////////////////////////////////////////////////////////

View File

@ -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);
}