From: hama Date: Fri, 5 Jun 2015 21:42:08 +0000 (+0200) Subject: temp X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=0233b02fd49118c55e3e1e3f8667cb9b68c9b53b;p=crepublib temp Fix: Filetime --- diff --git a/base/ReSerializable.cpp b/base/ReSerializable.cpp index bdf2501..6ffd960 100644 --- a/base/ReSerializable.cpp +++ b/base/ReSerializable.cpp @@ -88,7 +88,8 @@ void ReSerializable::packFileTime(ReByteBuffer& sequence, value = (time.tv_sec << 32) + time.tv_nsec; sequence.appendBits64(int64_t(value)); #elif defined __WIN32__ -#error "missing impl" +//#error "missing impl" + assert(false); #endif } /** @@ -120,6 +121,7 @@ void ReSerializable::unpackFileTime(const uint8_t*& sequence, size_t& length, break; } #elif defined __WIN32__ -#error "missing impl" +//#error "missing impl" + assert(false); #endif } diff --git a/base/ReThread.cpp b/base/ReThread.cpp index 5a027de..3ded469 100644 --- a/base/ReThread.cpp +++ b/base/ReThread.cpp @@ -152,7 +152,8 @@ ReThreadPool::ReThreadPool(int maxThreads, ReLogger* logger) : m_logger(logger), m_maxThreads(maxThreads), m_maxKillTimeSec(3), - m_mutexThreads(LC_MUTEX_THREADS) { + m_mutexThreads(LC_MUTEX_THREADS), + m_shouldStop(false){ m_threads = new ReThread*[maxThreads]; memset(m_threads, 0, maxThreads * sizeof *m_threads); } diff --git a/base/rebase.hpp b/base/rebase.hpp index 3d03b5f..1003c16 100644 --- a/base/rebase.hpp +++ b/base/rebase.hpp @@ -44,11 +44,6 @@ typedef u_int64_t uint64_t; typedef u_int8_t uint8_t; typedef __off_t ReFileSize_t; -enum ReOSType { - OS_UNDEF, - OS_LINUX = 'l', - OS_WIN32 = 'w' -}; typedef struct timespec ReFileTime_t; # define _strdup strdup # define _unlink unlink @@ -75,6 +70,8 @@ inline int getLastOSError() { # define OS_SEPARATOR "\\" typedef _int64 int64_t; typedef unsigned long long uint64_t; +typedef short int16_t; +typedef unsigned short uint16_t; typedef unsigned char uint8_t; typedef unsigned long uint32_t; typedef long int int32_t; @@ -87,7 +84,15 @@ typedef FILETIME ReFileTime_t; inline int getLastOSError() { return GetLastError(); } +inline void bzero(void* ptr, size_t size){ + memset(ptr, 0, size); +} #endif +enum ReOSType { + OS_UNDEF, + OS_LINUX = 'l', + OS_WIN32 = 'w' +}; typedef int ReErrNo_t; /** diff --git a/cunit/cuReSerializable.cpp b/cunit/cuReSerializable.cpp index f4f5f90..f7de4e4 100644 --- a/cunit/cuReSerializable.cpp +++ b/cunit/cuReSerializable.cpp @@ -57,6 +57,7 @@ public: packString255(buffer, m_string255); packString64k(buffer, m_string64k); packString4t(buffer, m_string4t); + return buffer; } const char* toString(ReByteBuffer& buffer) { buffer.setLength(0).append("id: ").appendInt(m_serialId); diff --git a/net/ReUdpConnection.cpp b/net/ReUdpConnection.cpp index a113c91..4d45632 100644 --- a/net/ReUdpConnection.cpp +++ b/net/ReUdpConnection.cpp @@ -91,7 +91,7 @@ int ReUdpConnection::receive(int timeout, ReByteBuffer* buffer, bool doLog) { timeval.tv_sec = timeout / 1000; timeval.tv_usec = timeout % 1000 * 1000; select(maxfd + 1, &rdset, NULL, NULL, &timeval); - doRead = FD_ISSET(m_socket, &rdset); + doRead = FD_ISSET(m_socket, &rdset) != 0; } buffer->ensureSize(8096 + 1); size_t size = buffer->capacity(); @@ -150,7 +150,12 @@ void ReUdpConnection::close() { m_logger->sayF(LOG_INFO | GRAN_USER | CAT_NETWORK, LC_UDPCONNECTION_CLOSE_1, i18n("Connection has been closed: $1:$2")) .arg(address()).arg(m_port).end(); - if (::close(m_socket) != 0) +#if defined __linux__ + int rc = ::close(m_socket); +#elif defined __WIN32__ + int rc = closesocket(m_socket); +#endif + if (rc != 0) m_logger->sayF(LOG_ERROR | GRAN_USER | CAT_NETWORK, LC_UDPCONNECTION_CLOSE_2, i18n("socket close failed: $1")).arg( strerror(errno)).end(); diff --git a/os/ReDirTools.cpp b/os/ReDirTools.cpp index f802f72..d83356e 100644 --- a/os/ReDirTools.cpp +++ b/os/ReDirTools.cpp @@ -136,6 +136,7 @@ const char* s_syncUsage[] = const char* s_syncExamples[] = { "dirtool sync --basename-pattern=;*.txt;*.doc e:\\data\\ d:\\backup\\data2", "dirtool sync --type=r --max-size=1G usr etc /media/backup", + "dirtool sync --delete=7d /home/ /backup", NULL }; const char* s_tcpUsage[] = { @@ -404,8 +405,10 @@ ReFileTime_t ReDirOptions::checkDate(const char* value) { /** * Checks whether the given value is a time expression. * - * Possible: - * Units: m(inutes) h(our) d(ays) + * Possible: [] + * Units: b(ytes) k(ilobytes [1000]) K(ilobytes [1024]) + * m(egabytes [1000**2]) M(egabytes [1024**2]) + * g(igabytes [1000**3]) G(igabytes [1024**3]) * * @param value value to check * @return the value (multiplied with the unit factor) @@ -675,6 +678,16 @@ void ReDirOptions::setFilterFromProgramArgs(ReDirEntryFilter& filter) { m_output = stdout; } } + m_programArgs.getString("verbose", buffer); + unsigned int nValue = 0; + int length; + if (buffer.length() == 0) + m_verboseLevel = V_NORMAL; + else if ( (length = ReStringUtils::lengthOfUnsigned(buffer.str(), + buffer.length(), &nValue)) == 0 || length != buffer.length()) + help("not a verbose level (0..5): ", buffer.str()); + else + m_verboseLevel = VerboseLevel(min(V_DEBUG, nValue)); } /** @@ -705,6 +718,7 @@ ReTool::ReTool(const char* usage[], const char* example[], int minArguments, m_traverser(NULL, this, logger), m_filter(), m_start(time(NULL)), + //m_statInfo(), m_logger(logger) { #pragma warning( pop ) } @@ -2026,6 +2040,15 @@ ReDirSync::ReDirSync(ReLogger* logger) : "add", false); m_programArgs.addBool("dry", i18n("does nothing, but says what should be done"), 'Y', "dry", false); + m_programArgs.addString("delete", + i18n( + "delete the files/folders of the target directory not existing in the source directory.\n" + "If a time expression is given only files will be deleted if they are older than this.\n" + "a time expression is a date, a time, a date/time or a relative time.\n" + "relative times are a number and a unit.\n" + "units: m(inutes) h(hours), d(days). Default: m(inutes)\n" + "examples: -D7d --delete=30d -D24h -D2009.3.2/12:00 -D1999.01.01"), + 'E', "delete", false, NULL); m_programArgs.addInt("timediff", i18n("filetime difference is considered to be equal\n" "if the difference is less than this value (in seconds)"), 'I', @@ -2259,6 +2282,9 @@ void ReDirSync::doIt() { bool dry = m_programArgs.getBool("dry"); bool ignoreDate = m_programArgs.getBool("ignoredate"); bool mustExist = m_programArgs.getBool("mustexist"); + if (m_programArgs.getString("delete", buffer)[0] != '\0') + checkDate(buffer.str()); + setFilterFromProgramArgs(filter); int64_t sumSizes = 0; int files = 0; @@ -2325,7 +2351,7 @@ void ReDirSync::doIt() { int diff = int( m_statInfo.st_mtime - entry->filetimeToTime(entry->modified())); - if (!ignoreDate && diff <= maxFileTimeDiff) { + if (!ignoreDate && (diff <= maxFileTimeDiff || diff == 3600)) { if (m_verboseLevel >= V_CHATTER) fprintf(m_output, "=ignored: %s same time\n", targetRelativePath); diff --git a/os/ReDirTools.hpp b/os/ReDirTools.hpp index 8af9434..be38e98 100644 --- a/os/ReDirTools.hpp +++ b/os/ReDirTools.hpp @@ -111,12 +111,12 @@ protected: #elif defined __WIN32__ int ix = name.length() - 1; if (ix >= 0 && name.str()[ix] != OS_SEPARATOR_CHAR) - ix = -1; + ix = -1; else - name.setLength(ix); + name.setLength(ix); bool rc = stat(name.str(), &m_statInfo) == 0; if (ix >= 0) - name.appendChar(OS_SEPARATOR_CHAR); + name.appendChar(OS_SEPARATOR_CHAR); return rc; #endif } @@ -131,7 +131,6 @@ protected: int64_t m_start; struct stat m_statInfo; ReLogger* m_logger; - }; class ReDirBatch: public ReTool { diff --git a/os/ReFileUtils.cpp b/os/ReFileUtils.cpp new file mode 100644 index 0000000..b0dd7eb --- /dev/null +++ b/os/ReFileUtils.cpp @@ -0,0 +1,56 @@ +/* + * ReFileUtils.cpp + * + * License: Public Domain + * You can use and modify this file without any restriction. + * Do what you want. + * No warranties and disclaimer of any damages. + * You also can use this license: http://www.wtfpl.net + * The latest sources: https://github.com/republib + */ +#include "base/rebase.hpp" +#include "os/reos.hpp" + +/** + * Returns the name of a subdirectory in the temporary directory. + * + * If the directory does not exist it will be created. + * + * @param node the name of the subdirectory + * @return the full name of the subdirectory + */ +ReByteBuffer ReFileUtils::tempDir(const char* node){ + ReByteBuffer rc; + if (getenv("TMP") != NULL) { + rc = getenv("TMP"); + } else if (getenv("TEMP")) { + rc = getenv("TEMP"); + } else { +#if defined __linux__ + rc = "/tmp/"; +#elif defined __WIN32__ + rc = "c:\\temp"; +#endif + } + if (node != NULL){ + rc.ensureLastChar(OS_SEPARATOR_CHAR); + rc.append(node); + _mkdir(rc.str(), ALLPERMS); + } + return rc; +} +/** + * Returns the name of a file in a subdirectory in the temporary directory. + * + * If the directory does not exist it will be created. + * + * @param node the name of the subdirectory + * @param node the name of the subdirectory + * @param the full name of the subdirectory + */ +ReByteBuffer ReFileUtils::tempFile(const char* node, const char* subdir){ + ReByteBuffer rc = tempDir(subdir); + rc.ensureLastChar(OS_SEPARATOR_CHAR); + rc.append(node); + return rc; +} diff --git a/os/ReFileUtils.hpp b/os/ReFileUtils.hpp new file mode 100644 index 0000000..5d3972c --- /dev/null +++ b/os/ReFileUtils.hpp @@ -0,0 +1,19 @@ +/* + * ReFileUtils.hpp + * + * License: Public Domain + * You can use and modify this file without any restriction. + * Do what you want. + * No warranties and disclaimer of any damages. + * You also can use this license: http://www.wtfpl.net + * The latest sources: https://github.com/republib + */ +#if ! defined REFILE_UTILS_HPP +#define REFILE_UTILS_HPP + +class ReFileUtils { +public: + static ReByteBuffer tempDir(const char* node); + static ReByteBuffer tempFile(const char* node, const char* subdir); +}; +#endif \ No newline at end of file diff --git a/os/ReRemoteDir.cpp b/os/ReRemoteDir.cpp index 35943c0..e1fce50 100644 --- a/os/ReRemoteDir.cpp +++ b/os/ReRemoteDir.cpp @@ -58,6 +58,7 @@ void ReRemoteDir::populate(const char* path) { * @param sequence IN/OUT: the place for the byte sequence */ ReByteBuffer& ReRemoteDir::serialize(ReByteBuffer& sequence) { + return sequence; } /** @@ -73,6 +74,6 @@ ReRemoteDirService::~ReRemoteDirService() { ReNetCommandHandler::ProcessingState ReRemoteDirService::handleNetCommand( ReByteBuffer& command, ReByteBuffer& data, ReTCPConnection* connection) { - + return ReNetCommandHandler::PS_UNDEF; } diff --git a/os/ReTraverser.cpp b/os/ReTraverser.cpp index 19231f5..6acb28e 100644 --- a/os/ReTraverser.cpp +++ b/os/ReTraverser.cpp @@ -127,6 +127,30 @@ time_t ReDirStatus_t::filetimeToTime(const ReFileTime_t* filetime) { date.QuadPart -= adjust.QuadPart; // converts back from 100-nanoseconds to seconds time_t rc = (time_t) (date.QuadPart / 10000000); +#if defined __WIN32__ + static int s_diffTime = 0x7fffffff; + if (s_diffTime == 0x7fffffff){ + s_diffTime = 0; + ReByteBuffer tempFile = ReFileUtils::tempFile("$$redir$$.tmp", NULL); + const char* filename = tempFile.str(); + FILE* fp = fopen(filename, "w"); + if (fp != NULL){ + struct stat info; + int rcStat = stat(filename, &info); + fclose(fp); + if (rcStat == 0) { + WIN32_FIND_DATAA data; + HANDLE handle = FindFirstFile(filename, &data); + if (handle != INVALID_HANDLE_VALUE){ + time_t other = filetimeToTime(&data.ftLastWriteTime); + s_diffTime = info.st_mtime - other; + FindClose(handle); + } + } + } + } + rc += s_diffTime; +#endif return rc; #endif } @@ -642,10 +666,12 @@ ReByteBuffer& ReDirEntryFilter::serialize(ReByteBuffer& sequence) { sequence.appendBits64(int64_t(value)); value = (m_minAge.tv_sec << 32) + m_minAge.tv_nsec; #elif defined __WIN32__ -#error "missing impl" +// #error "missing impl" + assert(false); #endif sequence.appendBits64(int64_t(value)); packBool(sequence, m_allDirectories); + return sequence; } /** diff --git a/os/reos.hpp b/os/reos.hpp index 342a9f1..030811b 100644 --- a/os/reos.hpp +++ b/os/reos.hpp @@ -40,6 +40,7 @@ inline bool operator >(const ReFileTime_t& time1, const ReFileTime_t& time2) { && time1.dwLowDateTime > time2.dwLowDateTime); #endif } +#include "os/ReFileUtils.hpp" #include "os/ReTraverser.hpp" #include "os/ReDirTools.hpp" #include "net/renet.hpp"