#include "aaspi_file_info.h" #include "aaspi_path.h" #include #include // Needed for Windows as they are not defined... #ifndef S_ISDIR #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) #endif #ifndef S_ISREG #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) #endif #ifdef _WIN32 static __time64_t time(__time64_t *t) { SYSTEMTIME currentTime; FILETIME ftCurrentTime; GetSystemTime(¤tTime); SystemTimeToFileTime(¤tTime, &ftCurrentTime); //large int from fTime ULARGE_INTEGER inftime; inftime.LowPart = ftCurrentTime.dwLowDateTime; inftime.HighPart = ftCurrentTime.dwHighDateTime; //Large int init to the first second of jan 1 1970 SYSTEMTIME jan1970 = { 1970, 1, 4,1,0,0,0,0}; FILETIME ftjan1970; SystemTimeToFileTime(&jan1970, &ftjan1970); ULARGE_INTEGER largejan1970; largejan1970.LowPart = ftjan1970.dwLowDateTime; largejan1970.HighPart = ftjan1970.dwHighDateTime; //shift from 1601 to 1970 __time64_t value100NS = inftime.QuadPart - largejan1970.QuadPart; //convert from 100 nanosecond intervals to seconds __time64_t result = value100NS / 10000000; if (t) *t=result; return result; } #endif AASPI_FileInfo::AASPI_FileInfo(const string &path) { cur_path=path; err=0; readable=false; writable=false; err=Stat(path,&cur_stat); if (err == 0) { int rv=Access(path,AASPI_READABLE); if (rv == 0) readable=true; rv=Access(path,AASPI_WRITABLE); if (rv == 0) writable=true; } } bool AASPI_FileInfo::isADirectory() { if (err) return false; else return S_ISDIR(cur_stat.st_mode); } bool AASPI_FileInfo::isAFile() { if (err) return false; else return S_ISREG(cur_stat.st_mode); } aaspi_int64 AASPI_FileInfo::size() { if (err) return 0; return cur_stat.st_size; } // Static functions bool AASPI_FileInfo::exists(const string &path) { AASPI_FileInfo afi(path); return afi.exists(); } bool AASPI_FileInfo::isADirectory(const string &path) { AASPI_FileInfo afi(path); return afi.isADirectory(); } bool AASPI_FileInfo::isAFile(const string &path) { AASPI_FileInfo afi(path); return afi.isAFile(); } bool AASPI_FileInfo::isReadable(const string &path) { AASPI_FileInfo afi(path); return afi.isReadable(); } bool AASPI_FileInfo::isWritable(const string &path) { AASPI_FileInfo afi(path); return afi.isWritable(); } aaspi_int64 AASPI_FileInfo::size(const string &path) { AASPI_FileInfo afi(path); return afi.size(); } //! Creates a path to a temporary file with out collisions. //! If there are too many collisions (>1024) then this routine //! returns the empty string which indicates failure. //! \returns Returns empty string on failure. string AASPI_FileInfo::get_temp_file(const string &basepath, const string &basename, const string &suffix) { string rval; int i; int bval=int(time(0)); AASPI_Path bpath(basepath); bpath += basename; for (i=0; i<1024; ++i) { ostringstream oss; oss << bpath.normalize_path(); oss.width(6); oss.fill('0'); oss << hex << ((i+bval) &0xffffff) << suffix; rval=oss.str(); if (! exists(rval)) break; } if (i >= 1024) rval=""; return rval; }