ENH: add a touch -E command to cmake
This commit is contained in:
parent
ab7f11a239
commit
5050706ae3
|
@ -941,6 +941,8 @@ void CMakeCommandUsage(const char* program)
|
|||
<< " tar [cxt][vfz] file.tar file/dir1 file/dir2 ... - create a tar "
|
||||
"archive\n"
|
||||
<< " time command [args] ... - run command and return elapsed time\n"
|
||||
<< " touch file - touch a file.\n"
|
||||
<< " touch_nocreate file - touch a file but do not create it.\n"
|
||||
#if defined(_WIN32) && !defined(__CYGWIN__)
|
||||
<< " write_regv key value - write registry value\n"
|
||||
<< " delete_regv key - delete registry value\n"
|
||||
|
@ -1096,6 +1098,34 @@ int cmake::ExecuteCMakeCommand(std::vector<std::string>& args)
|
|||
}
|
||||
return 0;
|
||||
}
|
||||
// Touch file
|
||||
else if (args[1] == "touch" && args.size() > 2)
|
||||
{
|
||||
for (std::string::size_type cc = 2; cc < args.size(); cc ++)
|
||||
{
|
||||
// Complain if the file could not be removed, still exists,
|
||||
// and the -f option was not given.
|
||||
if(!cmSystemTools::Touch(args[cc].c_str(), true))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
// Touch file
|
||||
else if (args[1] == "touch_nocreate" && args.size() > 2)
|
||||
{
|
||||
for (std::string::size_type cc = 2; cc < args.size(); cc ++)
|
||||
{
|
||||
// Complain if the file could not be removed, still exists,
|
||||
// and the -f option was not given.
|
||||
if(!cmSystemTools::Touch(args[cc].c_str(), false))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Clock command
|
||||
else if (args[1] == "time" && args.size() > 2)
|
||||
|
|
|
@ -50,6 +50,7 @@
|
|||
|
||||
// support for realpath call
|
||||
#ifndef _WIN32
|
||||
#include <utime.h>
|
||||
#include <limits.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/wait.h>
|
||||
|
@ -75,6 +76,11 @@
|
|||
# define HAVE_TTY_INFO 1
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#include <sys/utime.h>
|
||||
#else
|
||||
#include <utime.h>
|
||||
#endif
|
||||
|
||||
// This is a hack to prevent warnings about these functions being
|
||||
// declared but not referenced.
|
||||
|
@ -836,6 +842,36 @@ bool SystemTools::FileExists(const char* filename)
|
|||
}
|
||||
}
|
||||
|
||||
bool SystemTools::Touch(const char* filename, bool create)
|
||||
{
|
||||
if(create && !SystemTools::FileExists(filename))
|
||||
{
|
||||
FILE* file = fopen(filename, "a+b");
|
||||
if(file)
|
||||
{
|
||||
fclose(file);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#ifdef _MSC_VER
|
||||
#define utime _utime
|
||||
#define utimbuf _utimbuf
|
||||
#endif
|
||||
struct stat fromStat;
|
||||
if(stat(filename, &fromStat) < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
struct utimbuf buf;
|
||||
buf.actime = fromStat.st_atime;
|
||||
buf.modtime = SystemTools::GetTime();
|
||||
if(utime(filename, &buf) < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SystemTools::FileTimeCompare(const char* f1, const char* f2,
|
||||
int* result)
|
||||
|
|
|
@ -279,6 +279,11 @@ public:
|
|||
*/
|
||||
static unsigned long FileLength(const char *filename);
|
||||
|
||||
/**
|
||||
Change the modification time or create a file
|
||||
*/
|
||||
static bool Touch(const char* filename, bool create);
|
||||
|
||||
/**
|
||||
* Compare file modification times.
|
||||
* Return true for successful comparison and false for error.
|
||||
|
|
Loading…
Reference in New Issue