Replace custome date parser QDateTime::fromString(value, Qt::RFC2822Date)
authorHannah von Reth <hannah.vonreth@owncloud.com>
Mon, 19 Oct 2020 12:47:58 +0000 (14:47 +0200)
committerKevin Ottens <kevin.ottens@nextcloud.com>
Tue, 15 Dec 2020 10:01:52 +0000 (11:01 +0100)
src/csync/CMakeLists.txt
src/csync/csync.h
src/csync/csync_util.cpp [deleted file]
src/csync/csync_util.h [deleted file]
src/csync/vio/csync_vio_local_unix.cpp
src/csync/vio/csync_vio_local_win.cpp
src/libsync/discovery.cpp
src/libsync/discoveryphase.cpp
src/libsync/owncloudpropagator.h
src/libsync/propagatedownload.cpp

index 49197e72004b543109714be59ab9052a852b453c..8e4e6a3d8451ada411e51bd48f25839b323c151a 100644 (file)
@@ -37,7 +37,6 @@ endif()
 set(csync_SRCS
   csync.cpp
   csync_exclude.cpp
-  csync_util.cpp
 
   std/c_time.cpp
 
index a57f38015eacd4b43d11e9849102f349c2809f24..0c418f9addaa2d5c76d33e0b1991d8d0f47a8726 100644 (file)
@@ -207,8 +207,6 @@ struct OCSYNC_EXPORT csync_file_stat_s {
   { }
 };
 
-time_t OCSYNC_EXPORT oc_httpdate_parse( const char *date );
-
 /**
  * }@
  */
diff --git a/src/csync/csync_util.cpp b/src/csync/csync_util.cpp
deleted file mode 100644 (file)
index e0d30ba..0000000
+++ /dev/null
@@ -1,161 +0,0 @@
-/*
- * libcsync -- a library to sync a directory with another
- *
- * Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org>
- * Copyright (c) 2012-2013 by Klaas Freitag <freitag@owncloud.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "config_csync.h"
-
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE
-#endif
-
-#include <cerrno>
-#include <climits>
-#include <cstdio>
-#include <ctime>
-
-#include "common/c_jhash.h"
-#include "csync_util.h"
-
-#include <QtCore/QLoggingCategory>
-
-Q_LOGGING_CATEGORY(lcCSyncUtils, "nextcloud.sync.csync.utils", QtInfoMsg)
-
-struct csync_memstat_s {
-  int size;
-  int resident;
-  int shared;
-  int trs;
-  int drs;
-  int lrs;
-  int dt;
-};
-
-void csync_memstat_check() {
-  int s = 0;
-  struct csync_memstat_s m;
-  FILE* fp = nullptr;
-
-  /* get process memory stats */
-  fp = fopen("/proc/self/statm","r");
-  if (!fp) {
-    return;
-  }
-  s = fscanf(fp, "%d%d%d%d%d%d%d", &m.size, &m.resident, &m.shared, &m.trs,
-      &m.drs, &m.lrs, &m.dt);
-  fclose(fp);
-  if (s == EOF) {
-    return;
-  }
-
-  qCInfo(lcCSyncUtils, "Memory: %dK total size, %dK resident, %dK shared",
-      m.size * 4, m.resident * 4, m.shared * 4);
-}
-
-#ifndef HAVE_TIMEGM
-#ifdef _WIN32
-static int is_leap(unsigned y) {
-    y += 1900;
-    return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
-}
-
-static time_t timegm(struct tm *tm) {
-    static const unsigned ndays[2][12] = {
-    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
-    {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} };
-
-    time_t res = 0;
-    int i;
-
-    for (i = 70; i < tm->tm_year; ++i)
-        res += is_leap(i) ? 366 : 365;
-
-    for (i = 0; i < tm->tm_mon; ++i)
-        res += ndays[is_leap(tm->tm_year)][i];
-     res += tm->tm_mday - 1;
-     res *= 24;
-     res += tm->tm_hour;
-     res *= 60;
-     res += tm->tm_min;
-     res *= 60;
-     res += tm->tm_sec;
-     return res;
-}
-#else
-/* A hopefully portable version of timegm */
-static time_t timegm(struct tm *tm ) {
-     time_t ret;
-     char *tz;
-
-     tz = getenv("TZ");
-     setenv("TZ", "", 1);
-     tzset();
-     ret = mktime(tm);
-     if (tz)
-         setenv("TZ", tz, 1);
-     else
-         unsetenv("TZ");
-     tzset();
-     return ret;
-}
-#endif /* Platform switch */
-#endif /* HAVE_TIMEGM */
-
-#define RFC1123_FORMAT "%3s, %02d %3s %4d %02d:%02d:%02d GMT"
-static const char short_months[12][4] = {
-    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
-    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
-};
-/*
- * This function is borrowed from libneon's ne_httpdate_parse.
- * Unfortunately that one converts to local time but here UTC is
- * needed.
- * This one uses timegm instead, which returns UTC.
- */
-time_t oc_httpdate_parse( const char *date ) {
-    struct tm gmt;
-    char wkday[4], mon[4];
-    int n = 0;
-    time_t result = 0;
-
-    memset(&gmt, 0, sizeof(struct tm));
-
-    /*  it goes: Sun, 06 Nov 1994 08:49:37 GMT */
-    n = sscanf(date, RFC1123_FORMAT,
-               wkday, &gmt.tm_mday, mon, &gmt.tm_year, &gmt.tm_hour,
-               &gmt.tm_min, &gmt.tm_sec);
-    /* Is it portable to check n==7 here? */
-    gmt.tm_year -= 1900;
-    for (n=0; n<12; n++)
-        if (strcmp(mon, short_months[n]) == 0)
-            break;
-    /* tm_mon comes out as 12 if the month is corrupt, which is desired,
-     * since the mktime will then fail */
-    gmt.tm_mon = n;
-    gmt.tm_isdst = -1;
-    result = timegm(&gmt);
-    return result;
-}
-
-
-bool csync_is_collision_safe_hash(const QByteArray &checksum_header)
-{
-    return checksum_header.startsWith("SHA")
-        || checksum_header.startsWith("MD5:");
-}
diff --git a/src/csync/csync_util.h b/src/csync/csync_util.h
deleted file mode 100644 (file)
index c71f7c9..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * libcsync -- a library to sync a directory with another
- *
- * Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org>
- * Copyright (c) 2012-2013 by Klaas Freitag <freitag@owncloud.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#ifndef _CSYNC_UTIL_H
-#define _CSYNC_UTIL_H
-
-#include <cstdint>
-
-#include "csync.h"
-
-void OCSYNC_EXPORT csync_memstat_check();
-
-/* Returns true if we're reasonably certain that hash equality
- * for the header means content equality.
- *
- * Cryptographic safety is not required - this is mainly
- * intended to rule out checksums like Adler32 that are not intended for
- * hashing and have high likelihood of collision with particular inputs.
- */
-bool OCSYNC_EXPORT csync_is_collision_safe_hash(const QByteArray &checksum_header);
-
-#endif /* _CSYNC_UTIL_H */
index d83c946733fd585c9646e72fcf95192f42ab0c71..5ebec53a86633e71b7d5176b10e865d9608448fb 100644 (file)
@@ -30,7 +30,7 @@
 
 #include "c_private.h"
 #include "c_lib.h"
-#include "csync_util.h"
+#include "csync.h"
 
 #include "vio/csync_vio_local.h"
 #include "common/vfs.h"
index ac0eb95e771d9f9e39dc0d3962ed7d4ba76aa936..a2cbdaf56e9dfaf601a7ed46bfa5dfb01b28c14d 100644 (file)
@@ -31,7 +31,7 @@
 
 #include "c_private.h"
 #include "c_lib.h"
-#include "csync_util.h"
+#include "csync.h"
 #include "vio/csync_vio_local.h"
 #include "common/filesystembase.h"
 
index b4a42200fd9a7787de4034868f6d52709080779a..3a1c311c1b091be5bcee2651dd54cc7b458da0a8 100644 (file)
@@ -25,7 +25,7 @@
 #include <QThreadPool>
 #include "common/checksums.h"
 #include "csync_exclude.h"
-#include "csync_util.h"
+#include "csync.h"
 
 
 namespace OCC {
index bbe6280f39bf63fa55bf541f7d5ad6426ed21697..63640cab64606d7e2aa651ffa8538caedacbb6b6 100644 (file)
@@ -30,6 +30,7 @@
 #include <QFileInfo>
 #include <QTextCodec>
 #include <cstring>
+#include <QDateTime>
 
 
 namespace OCC {
@@ -386,11 +387,15 @@ static void propertyMapToRemoteInfo(const QMap<QString, QString> &map, RemoteInf
     for (auto it = map.constBegin(); it != map.constEnd(); ++it) {
         QString property = it.key();
         QString value = it.value();
-        if (property == "resourcetype") {
-            result.isDirectory = value.contains("collection");
-        } else if (property == "getlastmodified") {
-            result.modtime = oc_httpdate_parse(value.toUtf8());
-        } else if (property == "getcontentlength") {
+        if (property == QLatin1String("resourcetype")) {
+            result.isDirectory = value.contains(QLatin1String("collection"));
+        } else if (property == QLatin1String("getlastmodified")) {
+            const auto date = QDateTime::fromString(value, Qt::RFC2822Date);
+            if (date.isValid()) {
+                qCCritical(lcDiscovery) << "Failed to parse getlastmodified:" << value;
+            }
+            result.modtime = date.toTime_t();
+        } else if (property == QLatin1String("getcontentlength")) {
             // See #4573, sometimes negative size values are returned
             bool ok = false;
             qlonglong ll = value.toLongLong(&ok);
index ec4faf649d52d6ae1cb83096d1a4385e16933e9b..e16e27cf8df032a5a2f007dcd7d408990e67c661 100644 (file)
@@ -24,7 +24,7 @@
 #include <QIODevice>
 #include <QMutex>
 
-#include "csync_util.h"
+#include "csync.h"
 #include "syncfileitem.h"
 #include "common/syncjournaldb.h"
 #include "bandwidthmanager.h"
index ee44354e2561a7a9a3f10873ff4acf8e60e81b3b..52df7bd602ee148b3e3a806653d0a3ed141192fe 100644 (file)
@@ -449,6 +449,12 @@ void PropagateDownloadFile::startAfterIsEncryptedIsChecked()
     // Maybe it's not a real conflict and no download is necessary!
     // If the hashes are collision safe and identical, we assume the content is too.
     // For weak checksums, we only do that if the mtimes are also identical.
+
+    const auto csync_is_collision_safe_hash = [](const QByteArray &checksum_header)
+    {
+        return checksum_header.startsWith("SHA")
+            || checksum_header.startsWith("MD5:");
+    };
     if (_item->_instruction == CSYNC_INSTRUCTION_CONFLICT
         && _item->_size == _item->_previousSize
         && !_item->_checksumHeader.isEmpty()