]> gitweb.hamatoma.de Git - crepublib/commitdiff
temp
authorhama <hama@siduction.net>
Fri, 5 Jun 2015 21:42:08 +0000 (23:42 +0200)
committerhama <hama@siduction.net>
Fri, 5 Jun 2015 21:49:24 +0000 (23:49 +0200)
Fix: Filetime

12 files changed:
base/ReSerializable.cpp
base/ReThread.cpp
base/rebase.hpp
cunit/cuReSerializable.cpp
net/ReUdpConnection.cpp
os/ReDirTools.cpp
os/ReDirTools.hpp
os/ReFileUtils.cpp [new file with mode: 0644]
os/ReFileUtils.hpp [new file with mode: 0644]
os/ReRemoteDir.cpp
os/ReTraverser.cpp
os/reos.hpp

index bdf25015c52af0709720dc4d81afed76d22f6bb1..6ffd9609443d573d84420a32b41af93aea8b9b8b 100644 (file)
@@ -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
 }
index 5a027de1d8fc78527d7a4bc6af899f00597cc2b1..3ded4697b58d4e599c124d7477af69c4a51488fa 100644 (file)
@@ -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);
 }
index 3d03b5f32c22df31c648bf22cbf2028789ef391f..1003c16f61f286352b4d01cc7c49bf24a644cf3c 100644 (file)
 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;
 /**
index f4f5f90eb93fd9680c9323ddff5ce0bae0f5c949..f7de4e45a837540f1b5eb7aa774e05c132ff5eba 100644 (file)
@@ -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);
index a113c916b37d537de1093638e2277f4c1baa4bf7..4d45632efcbaecbeeae7ac20ff9a7cf58ff55a1a 100644 (file)
@@ -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();
index f802f7296bb303a0b1dbe8d22cd928d27e09e1d2..d83356e6e0bdea298f82eb709ab4a6cd3b869b19 100644 (file)
@@ -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: <date> <date_time> <n><unit>
- * Units: m(inutes) h(our) d(ays)
+ * Possible: <n>[<unit>]
+ * 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);
index 8af9434d2e274a447f3434abd98e1b677d535453..be38e9807d87b30c4a7cc0707fc1f987289f65f0 100644 (file)
@@ -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 (file)
index 0000000..b0dd7eb
--- /dev/null
@@ -0,0 +1,56 @@
+/*\r
+ * ReFileUtils.cpp\r
+ * \r
+ * License: Public Domain\r
+ * You can use and modify this file without any restriction.\r
+ * Do what you want.\r
+ * No warranties and disclaimer of any damages.\r
+ * You also can use this license: http://www.wtfpl.net\r
+ * The latest sources: https://github.com/republib\r
+ */\r
+#include "base/rebase.hpp"\r
+#include "os/reos.hpp"\r
+\r
+/**\r
+ * Returns the name of a subdirectory in the temporary directory.\r
+ *\r
+ * If the directory does not exist it will be created.\r
+ *\r
+ * @param node  the name of the subdirectory\r
+ * @return      the full name of the subdirectory\r
+ */\r
+ReByteBuffer ReFileUtils::tempDir(const char* node){\r
+    ReByteBuffer rc;\r
+       if (getenv("TMP") != NULL) {\r
+               rc = getenv("TMP");\r
+       } else if (getenv("TEMP")) {\r
+               rc = getenv("TEMP");\r
+       } else {\r
+#if defined __linux__\r
+               rc = "/tmp/";\r
+#elif defined __WIN32__\r
+        rc = "c:\\temp";\r
+#endif\r
+       }\r
+    if (node != NULL){\r
+        rc.ensureLastChar(OS_SEPARATOR_CHAR);\r
+        rc.append(node);\r
+        _mkdir(rc.str(), ALLPERMS);\r
+    }\r
+    return rc;\r
+}\r
+/**\r
+ * Returns the name of a file in a subdirectory in the temporary directory.\r
+ *\r
+ * If the directory does not exist it will be created.\r
+ *\r
+ * @param node  the name of the subdirectory\r
+ * @param node  the name of the subdirectory\r
+ * @param       the full name of the subdirectory\r
+ */\r
+ReByteBuffer ReFileUtils::tempFile(const char* node, const char* subdir){\r
+    ReByteBuffer rc = tempDir(subdir);\r
+    rc.ensureLastChar(OS_SEPARATOR_CHAR);\r
+    rc.append(node);\r
+    return rc;\r
+}\r
diff --git a/os/ReFileUtils.hpp b/os/ReFileUtils.hpp
new file mode 100644 (file)
index 0000000..5d3972c
--- /dev/null
@@ -0,0 +1,19 @@
+/*\r
+ * ReFileUtils.hpp\r
+ * \r
+ * License: Public Domain\r
+ * You can use and modify this file without any restriction.\r
+ * Do what you want.\r
+ * No warranties and disclaimer of any damages.\r
+ * You also can use this license: http://www.wtfpl.net\r
+ * The latest sources: https://github.com/republib\r
+ */\r
+#if ! defined REFILE_UTILS_HPP\r
+#define REFILE_UTILS_HPP\r
+\r
+class ReFileUtils {\r
+public:\r
+    static ReByteBuffer tempDir(const char* node);\r
+    static ReByteBuffer tempFile(const char* node, const char* subdir);\r
+};\r
+#endif
\ No newline at end of file
index 35943c0f6a30bfca6a369d087a335aaf281c066a..e1fce503a1f9631d6a2c31f6a0cafeee7571082d 100644 (file)
@@ -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;
 }
 
index 19231f576cbfe9e719166b4c2cb3b6ec36c9be89..6acb28e08e5d76f1a1df751f4d5fd8fbcc4a4560 100644 (file)
@@ -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;
 }
 
 /**
index 342a9f143e737d9360f9cfbc6562733838217626..030811b02845f8d17a0cda438f7a7a40e3d34f60 100644 (file)
@@ -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"