bool MkPath::writeFileDateTime(const QFileInfo &destination) { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"writeFileDateTime("+destination.absoluteFilePath()+")"); /** Why not do it with Qt? Because it not support setModificationTime(), and get the time with Qt, that's mean use local time where in C is UTC time */ #ifdef Q_OS_UNIX #ifdef Q_OS_LINUX return utime(destination.absoluteFilePath().toLatin1().data(),&butime)==0; #else //mainly for mac return utime(destination.absoluteFilePath().toLatin1().data(),&butime)==0; #endif #else #ifdef Q_OS_WIN32 #ifdef ULTRACOPIER_PLUGIN_SET_TIME_UNIX_WAY return utime(destination.toLatin1().data(),&butime)==0; #else wchar_t filePath[65535]; if(destination.absoluteFilePath().contains(regRead)) filePath[QDir::toNativeSeparators(QStringLiteral("\\\\?\\")+destination.absoluteFilePath()).toWCharArray(filePath)]=L'\0'; else filePath[QDir::toNativeSeparators(destination.absoluteFilePath()).toWCharArray(filePath)]=L'\0'; HANDLE hFileDestination = CreateFileW(filePath, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); if(hFileDestination == INVALID_HANDLE_VALUE) { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"open failed to write: "+QString::fromWCharArray(filePath)+", error: "+QString::number(GetLastError())); return false; } FILETIME ftCreate, ftAccess, ftWrite; ftCreate.dwLowDateTime=this->ftCreateL; ftCreate.dwHighDateTime=this->ftCreateH; ftAccess.dwLowDateTime=this->ftAccessL; ftAccess.dwHighDateTime=this->ftAccessH; ftWrite.dwLowDateTime=this->ftWriteL; ftWrite.dwHighDateTime=this->ftWriteH; if(!SetFileTime(hFileDestination, &ftCreate, &ftAccess, &ftWrite)) { CloseHandle(hFileDestination); ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"unable to set the file time"); return false; } CloseHandle(hFileDestination); return true; #endif #else return false; #endif #endif return false; }
//fonction to edit the file date time bool MkPath::readFileDateTime(const QFileInfo &source) { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"readFileDateTime("+source.absoluteFilePath()+")"); /** Why not do it with Qt? Because it not support setModificationTime(), and get the time with Qt, that's mean use local time where in C is UTC time */ #ifdef Q_OS_UNIX #ifdef Q_OS_LINUX struct stat info; if(stat(source.absoluteFilePath().toLatin1().data(),&info)!=0) return false; time_t ctime=info.st_ctim.tv_sec; time_t actime=info.st_atim.tv_sec; time_t modtime=info.st_mtim.tv_sec; //this function avalaible on unix and mingw butime.actime=actime; butime.modtime=modtime; Q_UNUSED(ctime); return true; #else //mainly for mac QFileInfo fileInfo(source); time_t ctime=fileInfo.created().toTime_t(); time_t actime=fileInfo.lastRead().toTime_t(); time_t modtime=fileInfo.lastModified().toTime_t(); //this function avalaible on unix and mingw utimbuf butime; butime.actime=actime; butime.modtime=modtime; Q_UNUSED(ctime); return true; #endif #else #ifdef Q_OS_WIN32 #ifdef ULTRACOPIER_PLUGIN_SET_TIME_UNIX_WAY struct stat info; if(stat(source.toLatin1().data(),&info)!=0) return false; time_t ctime=info.st_ctim.tv_sec; time_t actime=info.st_atim.tv_sec; time_t modtime=info.st_mtim.tv_sec; //this function avalaible on unix and mingw butime.actime=actime; butime.modtime=modtime; Q_UNUSED(ctime); return true; #else wchar_t filePath[65535]; if(source.absoluteFilePath().contains(regRead)) filePath[QDir::toNativeSeparators(QStringLiteral("\\\\?\\")+source.absoluteFilePath()).toWCharArray(filePath)]=L'\0'; else filePath[QDir::toNativeSeparators(source.absoluteFilePath()).toWCharArray(filePath)]=L'\0'; HANDLE hFileSouce = CreateFileW(filePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY | FILE_FLAG_BACKUP_SEMANTICS, NULL); if(hFileSouce == INVALID_HANDLE_VALUE) { ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"open failed to read: "+QString::fromWCharArray(filePath)+", error: "+QString::number(GetLastError())); return false; } FILETIME ftCreate, ftAccess, ftWrite; if(!GetFileTime(hFileSouce, &ftCreate, &ftAccess, &ftWrite)) { CloseHandle(hFileSouce); ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"unable to get the file time"); return false; } this->ftCreateL=ftCreate.dwLowDateTime; this->ftCreateH=ftCreate.dwHighDateTime; this->ftAccessL=ftAccess.dwLowDateTime; this->ftAccessH=ftAccess.dwHighDateTime; this->ftWriteL=ftWrite.dwLowDateTime; this->ftWriteH=ftWrite.dwHighDateTime; CloseHandle(hFileSouce); return true; #endif #else return false; #endif #endif return false; }