[Add] Time stamp checking for files.

This commit is contained in:
Rtch90 2012-09-04 23:18:06 +01:00
parent 273fdaaf10
commit aef731817d
2 changed files with 99 additions and 0 deletions

View File

@ -0,0 +1,82 @@
#include <assert.h>
#include <fcntl.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "InputStreamWrapper.h"
#include "FileTimestampChecker.h"
using namespace saracraft;
/*bool FileTimestampChecker::IsFileNewerThanFile(const char* file, const char* secondFile) {
assert
}*/
bool FileTimestampChecker::IsFileNewerOrSameThanFile(const char* file, const char* secondfile) {
if(GetFileTimestamp(file) >= GetFileTimestamp(secondfile))
return true;
else
return false;
}
bool FileTimestampChecker::IsFileNewerOrAlmostSameThanFile(const char* file, const char* secondfile) {
// WARNING: Max 60 seconds older file is also accepted as newer..
if(GetFileTimestamp(file) + 60 >= GetFileTimestamp(secondfile))
return true;
else
return false;
}
bool FileTimestampChecker::IsFileUpToDateComparedTo(const char* file, const char* secondfile) {
FILE* f = fopen(file, "rb");
if(f) {
fclose(f);
return FileTimestampChecker::IsFileNewerOrAlmostSameThanFile(file, secondfile);
}
filesystem::SC_FILE* fp = filesystem::sc_fopen(file, "rb");
if(fp) {
filesystem::sc_fclose(fp);
return true;
}
return false;
}
#ifndef _MSC_VER
#define _stat stat
#ifndef _WIN32_
#define _fileno fileno
#endif
#define _fstat fstat
#endif
int FileTimestampChecker::GetFileTimestamp(const char* file) {
struct _stat buf;
FILE* f;
int fh, result;
int ret;
f = fopen(file, "rb");
if(f == NULL) return -1;
fh = _fileno(f);
result = _fstat(fh, &buf);
if(result != 0) {
return -1;
} else {
ret = int(buf.st_mtime);
}
fclose(f);
return ret;
}

View File

@ -0,0 +1,17 @@
#pragma once
class FileTimestampChecker {
public:
// Check if the given file with given name has been modified after another
// file of given name.
// Return true if file *IS* newer.
// This is not needed....
//static bool IsFileNewerThanFile(const char* file, const char* secondfile);
static bool IsFileNewerOrSameThanFile(const char* file, const char* secondfile);
static bool IsFileNewerOrAlmostSameThanFile(const char* file, const char* secondfile);
static bool IsFileUpToDateComparedTo(const char* file, const char* secondfile);
static int GetFileTimestamp(const char *file);
};