From 2c8e55d90fe1640975885b4aeee4b36a5f312099 Mon Sep 17 00:00:00 2001
From: michaelvillani
Date: Sun, 8 Nov 2026 20:26:29 +0000
Subject: [PATCH] feat(sync): check a conflict against the bytes before asking
- SMB, FTP and WebDAV tag files by size and time, which a copy made
separately never matches, so a remote added with the saves already on
it asked once per file. Drive and Dropbox already settle this with a
content hash.
- With two or more sides changed and the 3DS holding a copy, each changed
remote without a content hash is checked: a known size that differs
settles it; otherwise the remote copy is downloaded beside the save and
compared byte for byte. A match drops out of the prompt and is recorded
as in step, with nothing copied.
- A copy that differs is kept: if that side wins the prompt it is moved
into place instead of being downloaded again. Unused copies are
removed however the decision ends.
- [Sync] CompareUpTo= (KB, default 4096, 0 = no limit) bounds the
download made before the prompt; CompareConflicts=false turns the check
off. A bad value is a startup issue and the default applies.
- FTP (SIZE), SMB and WebDAV (getcontentlength) now report a file's size.
- The local walk skips names ending in .3dstmp, so a download or
comparison cut short is never synced as a save.
- The configurator's step 2 has a Conflicts block for both settings,
shown when an SMB, FTP or WebDAV remote is configured; inifile.js reads
and writes [Sync] and keeps its other lines.
Co-Authored-By: Claude Opus 5.5
---
AGENTS.md | 8 +-
README.md | 27 ++-
docs/configurator.html | 23 +++
docs/static/css/index.css | 9 +
docs/static/js/index.js | 25 ++-
docs/static/js/inifile.js | 41 +++-
source/main.cpp | 40 ++++
source/modules/ftpremote.cpp | 6 +-
source/modules/ftpremote.h | 3 +-
source/modules/remoteparse.cpp | 5 +
source/modules/remoteparse.h | 1 +
source/modules/smbremote.cpp | 1 +
source/modules/syncprovider.h | 4 +
source/modules/webdavremote.cpp | 2 +
source/sync/remoteconfig.cpp | 38 ++++
source/sync/remoteconfig.h | 15 ++
source/sync/syncengine.cpp | 106 +++++++++-
source/sync/syncengine.h | 5 +
source/utils/fsutil.cpp | 39 ++++
source/utils/fsutil.h | 4 +
tests/Makefile | 2 +-
tests/configurator/inifile.test.js | 25 +++
tests/mocks.h | 4 +
tests/test_compare.cpp | 312 +++++++++++++++++++++++++++++
tests/test_remoteparse.cpp | 17 ++
25 files changed, 751 insertions(+), 11 deletions(-)
create mode 100644 tests/test_compare.cpp
diff --git a/AGENTS.md b/AGENTS.md
index f0df07b..f84eebc 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -151,7 +151,8 @@ Keep the format `major.minor.micro`: the Makefile splits it on the dots and `mak
- A **read-only** participant (`Role=source`, `Participant::readOnly`) is the reverse: never written to. It can win like any side, but is never sent a version, so its recorded generation stays the one it really holds; a change of its own that lost is recorded (tag only) so it is not raised again. Being behind is its normal state, and a change it makes from an older generation than the 3DS's is a conflict with the 3DS, by the generation rule below. `syncEntry()` leaves it out of an upload-only entry, and `runSync()` does not offer it `legacyUpload()`.
- **Generations, not timestamps, say who is behind.** The manifest keeps the 3DS's own copy under `local|` (mtime, size, `generation`), and each remote's record holds the generation it last received. The generation goes up whenever the 3DS's copy changes. A remote whose tag moved is *changed*; one whose generation is older is *behind* and is sent the current version — unless it is both, which is a conflict with the 3DS, because its edit started from a version the 3DS has since replaced. Without generations a remote that was offline for a run looks like it made the change it missed.
- **Manifests from before the n-way engine** have no `local|` entry and no `"gen"`. The newest remote record becomes the 3DS baseline, and a record without a generation counts as holding the baseline's when its recorded mtime/size match it (`sameStat()`), which makes the first run after an upgrade decide what the pairwise engine would have. `"gen"` is only written when non-zero, so such a manifest round-trips byte for byte, and an older 3DSync ignores both additions.
-- Change detection compares the local mtime **and** size against the `local|` record, plus each remote's `tag`. `SyncProvider::localTag()` is the content-based fallback for stale mtimes and is only meaningful where the remote's tag is derivable from file contents (Drive's MD5, Dropbox's `content_hash`); it returns "" elsewhere. It also settles a remote that changed to the very bytes the 3DS has (no conflict, nothing copied) — which is what lets a remote added with copies already on it avoid a conflict per file. Size+mtime remotes cannot prove that and still ask.
+- Change detection compares the local mtime **and** size against the `local|` record, plus each remote's `tag`. `SyncProvider::localTag()` is the content-based fallback for stale mtimes and is only meaningful where the remote's tag is derivable from file contents (Drive's MD5, Dropbox's `content_hash`); it returns "" elsewhere. It also settles a remote that changed to the very bytes the 3DS has (no conflict, nothing copied) — which is what lets a remote added with copies already on it avoid a conflict per file. Size+mtime remotes cannot prove that from the listing; for a *conflict* the engine then compares the bytes (see the next point).
+- **A conflict is checked against the bytes before anyone is asked.** With two or more sides changed and the 3DS holding a copy, `sameAsLocalCopy()` in `syncengine.cpp` takes each changed remote without a content hash: a known `RemoteFileInfo::size` that differs from the 3DS's settles it; otherwise, within `SyncEntry::compareUpTo` (`[Sync] CompareUpTo=` in KB, default 4096, 0 = no limit (`COMPARE_UP_TO_UNLIMITED`), parsed by `parseCompareUpTo()` in `remoteconfig.cpp`; a `SyncEntry`'s own default of 0 never compares, which keeps the other engine tests free of comparison downloads; `[Sync] CompareConflicts=false` (`parseCompareConflicts()`) sets that 0 whatever `CompareUpTo=` says), it `download()`s the remote copy to `.compare.3dstmp` and `compareFiles()` (`utils/fsutil`) it. A match is `sameAsLocal` — out of the prompt, recorded as in step, nothing copied. A copy that differs is kept as `Side::fetched`: if that side wins the prompt, `downloadFrom()` moves it into place with `replaceLocalFile()` instead of downloading it again, and `FetchedCopies` removes every unused one however `syncFile()` returns. A failed or over-size check leaves the conflict as it was. FTP (`SIZE`), SMB and WebDAV (`getcontentlength`) fill `size`; Drive and Dropbox never need it. `recurse_dir()` skips names ending in `.3dstmp`, so a comparison or download cut short is never synced as a save.
- **Path names keep their spelling on the remote** (#71). INIReader lowercases every key, so `configuredPaths()` gives a `SyncEntry` both: `remoteName`, the folder as written (`INIReader::GetSpelling()`), which the providers, the log and the picker get; and `name`, lowercased, which is what `Only=` (`takesPart()`), `[Filter]` (`forEntry()`) and Drive's `Folder.=` (`_pathFolders`, looked up through `asciiLower()`) compare. Versions before this one sent the lowercased name, so a case-sensitive server may hold `gba` where the INI says `GBA`: for a name with capitals, `resolveRoot()` in `syncengine.cpp` asks `SyncProvider::findRoot()` — which never creates anything — for the name as written, then lowercased, and only calls `ensureRoot()` (as written) when neither exists. FTP (a name-only listing, refused = missing), WebDAV (Depth 0 `PROPFIND`, 404 expected) and SMB (`smb2_stat()`) implement it; Dropbox and Drive compare names ignoring case and keep the default, which finds nothing. A lowercase name costs no extra request.
- **The extension filter** (`[Filter]`, `ExtensionFilter` and `PathFilters` in `sync/extfilter.h`) is per path and defaults to all files. `Extensions=` is the list for every path; a key naming a path (`roms/nds=dsv`, lowercased like every INIReader key) replaces it for that one, `*` included. `parsePathFilters()` collects keys naming no configured path in `unknown`, which `configIssues()` turns into a startup issue; a path named `extensions` cannot have its own line. It is applied twice on purpose: `getConfiguredSyncPaths()` drops non-matching local files, because a provider's `legacyUpload()` only ever sees `localFiles`; and `syncEntry()` filters both `localFiles` and every remote listing, so a remote-only file it excludes is never downloaded. Filtered files are skipped *before* the decision table, so their manifest entries are neither updated nor removed — widening the filter again must not look like a first sync and raise a conflict for every file. The menu card shows `PathFilters::describe()` under FILES (it is in `signature()`): the default's list, or "per path" once any path has its own. A run prints an entry's filter under that entry's header, when it has one. List separators are commas and whitespace, never `;` (inih's inline comment); the configurator refuses a `;` for that reason.
- Manifest keys are `Participant::keyPrefix + "|" + localPath`, the prefix being `manifestScope()`: `SyncProvider::manifestPrefix()` for an unnamed section, plus `#
+
+
Conflicts
+
When a save changed both on the 3DS and on an SMB, FTP or WebDAV
+ remote, 3DSync first downloads the remote copy to see whether it is the same file. It only asks
+ you when the two really differ.
+
+
+
+
+
+
+
+ Blank: 4096 (4 MB),
+ enough for any save. 0: any size, which can take minutes for a ROM on a
+ slow server.
+
+
+
Back
diff --git a/docs/static/css/index.css b/docs/static/css/index.css
index ec81de3..68824c3 100644
--- a/docs/static/css/index.css
+++ b/docs/static/css/index.css
@@ -603,6 +603,15 @@ main {
margin: 0.2rem 0 0.8rem 0;
}
+/* Step 2: [Sync] CompareConflicts= and CompareUpTo=, for SMB, FTP and WebDAV. */
+.conflicts-section {
+ display: none;
+}
+
+.compare-option {
+ margin: 0.2rem 0 0.4rem 0;
+}
+
/* Step 2: the Drive folder chosen per folder, for 3DSync's own client */
.drive-folders-section {
display: none;
diff --git a/docs/static/js/index.js b/docs/static/js/index.js
index 44e1add..4186bd7 100644
--- a/docs/static/js/index.js
+++ b/docs/static/js/index.js
@@ -122,7 +122,8 @@ $(function () {
// What the steps build, and what the file is written from (IniFile in
// inifile.js describes it): config.remotes as they are saved in step 1,
// folders and file types as step 2 confirms them.
- let config = { remotes: [], folders: [], filter: '', extras: [] };
+ let config = { remotes: [], folders: [], filter: '', sync: { compare: true, compareUpTo: '', other: [] },
+ extras: [] };
let draft = null;
let draftSnapshot = '';
let nextUid = 1;
@@ -436,6 +437,9 @@ $(function () {
if (f.filter) folderFilters[f.name.toLowerCase()] = f.filter;
});
$('#file-filter').val(config.filter);
+ $('#compare-conflicts').prop('checked', config.sync.compare !== false);
+ $('#compare-up-to').val(config.sync.compareUpTo);
+ updateCompareUpTo();
M.updateTextFields();
renderRemotes();
renderRouting();
@@ -1301,6 +1305,12 @@ $(function () {
});
}
+ // The size only matters while the check is on.
+ function updateCompareUpTo() {
+ $('#compare-up-to').prop('disabled', !$('#compare-conflicts').prop('checked'));
+ }
+ $('#compare-conflicts').on('change', updateCompareUpTo);
+
$('#file-filter').on('input', function () {
$('#folder-filters input.folder-filter').attr('placeholder', $(this).val().trim() || 'all files');
});
@@ -1331,6 +1341,10 @@ $(function () {
function renderRouting() {
let names = currentFolderNames();
$('#routing-section').toggle(config.remotes.length > 1);
+ // Drive and Dropbox prove a copy is the same with a content hash.
+ $('#conflicts-section').toggle(config.remotes.some(function (r) {
+ return r.type === 'smb' || r.type === 'ftp' || r.type === 'webdav';
+ }));
let $table = $('
');
let $head = $('
').append($('
Folder
'));
config.remotes.forEach(function (remote) {
@@ -1718,9 +1732,18 @@ $(function () {
if (filterProblem) error = true;
renderRouting();
if (folderFiltersProblem()) error = true;
+ // The 3DS takes a whole number of KB and nothing else.
+ let $compareUpTo = $('#compare-up-to');
+ $compareUpTo.val($compareUpTo.val().trim());
+ let compareProblem = /^\d*$/.test($compareUpTo.val()) ? null
+ : 'A whole number of KB, e.g. 4096; 0 for any size';
+ setFieldError($compareUpTo, compareProblem);
+ if (compareProblem) error = true;
folders.forEach(function (f) { f.filter = (folderFilters[f.name.toLowerCase()] || '').trim(); });
config.folders = folders;
config.filter = $filter.val();
+ config.sync = { compare: $('#compare-conflicts').prop('checked'), compareUpTo: $compareUpTo.val(),
+ other: (config.sync && config.sync.other) || [] };
let routing = error ? null : routingProblem();
$('#routing-note').text(routing || '').toggleClass('warn', !!routing);
if (routing) {
diff --git a/docs/static/js/inifile.js b/docs/static/js/inifile.js
index 5ec2665..65d85ab 100644
--- a/docs/static/js/inifile.js
+++ b/docs/static/js/inifile.js
@@ -8,6 +8,7 @@
// sealedSection, excluded, driveFolders }],
// folders: [{ name, path, recursive, direction, filter }],
// filter: '', // [Filter] Extensions=
+// sync: { compare, compareUpTo, other }, // [Sync], see below
// extras: [] // sections the page has no form for, as read
// }
//
@@ -24,6 +25,13 @@
// fields['gdrive-expert-mode'] says which. driveFolders maps a folder name
// (lowercased) to the Drive folder chosen for it, { id, title }, written as
// Folder.= in the remote's section.
+//
+// sync is [Sync]: compare is CompareConflicts= (false: a conflict on SMB, FTP
+// or WebDAV is always asked about, without downloading the remote copy to
+// see whether it is the same file), compareUpTo is CompareUpTo= as typed (KB;
+// '' for the 3DS's default of 4096, '0' for no limit), and other holds the
+// section's other lines, written back as they were. The section is only
+// written when it says something.
(function (root) {
'use strict';
@@ -165,6 +173,12 @@
if (config.filter) filters.push(['Extensions', config.filter]);
config.folders.forEach(function (f) { if (f.filter) filters.push([f.name, f.filter]); });
if (filters.length) parts.push(sectionText('Filter', filters, false));
+ let sync = config.sync || {};
+ let syncPairs = [];
+ if (sync.compare === false) syncPairs.push(['CompareConflicts', 'false']);
+ if (sync.compareUpTo) syncPairs.push(['CompareUpTo', sync.compareUpTo]);
+ syncPairs = syncPairs.concat(sync.other || []);
+ if (syncPairs.length) parts.push(sectionText('Sync', syncPairs, false));
tail.forEach(function (x) { parts.push(sectionText(x.name, x.pairs, masked)); });
return (headText ? headText + '\n' : '') + parts.join('\n');
}
@@ -291,6 +305,29 @@
return remote;
}
+ // The 3DS reads CompareConflicts= as on for anything it does not know, and
+ // says so at startup; the page does the same, and keeps a CompareUpTo= it
+ // would refuse in the field, where Confirm points it out.
+ const SWITCH_VALUES = { 'true': true, yes: true, '1': true, on: true,
+ 'false': false, no: false, '0': false, off: false };
+
+ function readSync(section, sync, report) {
+ section.pairs.forEach(function (pair) {
+ let key = pair[0].toLowerCase();
+ let value = pair[1].trim();
+ if (key === 'compareconflicts') {
+ let on = SWITCH_VALUES[value.toLowerCase()];
+ if (value === '' || on === undefined) {
+ if (value !== '')
+ report.problems.push('[Sync] CompareConflicts=' + value + ' is neither true nor false, so '
+ + 'conflicts are compared.');
+ sync.compare = true;
+ } else sync.compare = on;
+ } else if (key === 'compareupto') sync.compareUpTo = value;
+ else sync.other.push(pair);
+ });
+ }
+
// Returns { config, report }. report: remotes and folders read, how many
// values were sealed, the sections kept as they were, and every line
// that could not be used, for the page to list. config is null when the
@@ -300,7 +337,8 @@
let report = { remotes: 0, folders: 0, sealed: 0, kept: [], problems: [] };
if (parsed.badLines.length)
report.problems.push('Line ' + parsed.badLines.join(', ') + ' could not be read and was left out.');
- let config = { remotes: [], folders: [], filter: '', extras: [] };
+ let config = { remotes: [], folders: [], filter: '', sync: { compare: true, compareUpTo: '', other: [] },
+ extras: [] };
let filterPairs = [];
parsed.sections.forEach(function (section) {
let key = section.name.toLowerCase();
@@ -313,6 +351,7 @@
direction: kind.direction, filter: '' });
});
} else if (key === 'filter') filterPairs = section.pairs;
+ else if (key === 'sync') readSync(section, config.sync, report);
else if (section.pairs.length) {
config.extras.push(section);
report.kept.push(section.name === '' ? 'the lines before the first section' : '[' + section.name + ']');
diff --git a/source/main.cpp b/source/main.cpp
index 8f14008..82b5c66 100644
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -28,6 +28,7 @@
#include "utils/debug.h"
#include "utils/configseal.h"
#include "utils/fsutil.h"
+#include "utils/pathutil.h"
#include "utils/secrets.h"
#include "utils/timeutil.h"
#include "version.h"
@@ -57,6 +58,11 @@ std::vector recurse_dir(std::string basepath, std::string additiona
paths.insert(paths.end(), sub.begin(), sub.end());
}
}
+ else if (isTempTransferName(childAdditional))
+ {
+ // A transfer or comparison cut short; never a save to sync.
+ debugf("skipping %s, left from an earlier run\n", childFull.c_str());
+ }
else
{
paths.push_back(childAdditional);
@@ -142,6 +148,13 @@ static std::vector configuredPaths(const INIReader &reader)
{
std::vector paths;
PathFilters filters = configuredFilters(reader);
+ long long compareUpTo;
+ bool compare;
+ // Both report a bad value through configIssues().
+ parseCompareUpTo(reader.Get("sync", "compareupto", ""), compareUpTo);
+ parseCompareConflicts(reader.Get("sync", "compareconflicts", ""), compare);
+ if (!compare)
+ compareUpTo = 0; // a SyncEntry's "never"
for (auto &kv : reader.GetValues())
{
@@ -157,6 +170,7 @@ static std::vector configuredPaths(const INIReader &reader)
path.entry.remoteName = reader.GetSpelling(kv.first);
path.entry.direction = dir;
path.entry.filter = filters.forEntry(path.entry.name);
+ path.entry.compareUpTo = compareUpTo;
paths.push_back(path);
}
return paths;
@@ -635,6 +649,32 @@ static std::vector configIssues(const INIReader &reader,
issue.leftOut = false;
issues.push_back(issue);
}
+ long long compareUpTo;
+ if (!parseCompareUpTo(reader.Get("sync", "compareupto", ""), compareUpTo))
+ {
+ char line[48];
+ snprintf(line, sizeof(line), " in KB, e.g. %lld. 0 is no limit;",
+ COMPARE_UP_TO_DEFAULT_KB);
+ ConfigIssue issue;
+ issue.section = "sync";
+ issue.brief = "[Sync] CompareUpTo= is no number";
+ issue.lines = {"[Sync] CompareUpTo= must be a whole number,", line,
+ " until fixed, the default is used."};
+ issue.leftOut = false;
+ issues.push_back(issue);
+ }
+ bool compare;
+ if (!parseCompareConflicts(reader.Get("sync", "compareconflicts", ""), compare))
+ {
+ ConfigIssue issue;
+ issue.section = "sync";
+ issue.brief = "[Sync] CompareConflicts= unclear";
+ issue.lines = {"[Sync] CompareConflicts= must be true or",
+ " false. Until fixed, conflicts are",
+ " compared as by default."};
+ issue.leftOut = false;
+ issues.push_back(issue);
+ }
return issues;
}
diff --git a/source/modules/ftpremote.cpp b/source/modules/ftpremote.cpp
index bf2c7f4..e459dd0 100644
--- a/source/modules/ftpremote.cpp
+++ b/source/modules/ftpremote.cpp
@@ -292,7 +292,7 @@ long FtpRemote::_onListEntry(const void *transferInfo, void *userdata, int remai
return CURL_CHUNK_BGN_FUNC_SKIP;
}
-std::string FtpRemote::_tagFor(const std::string &path)
+std::string FtpRemote::_tagFor(const std::string &path, long long *sizeOut)
{
_prepare();
_curl.setURL(_urlFor(path));
@@ -312,6 +312,8 @@ std::string FtpRemote::_tagFor(const std::string &path)
if (size < 0 || mtime < 0)
debugf("%s: server reported size=%lld mtime=%ld — the change tag will "
"not be reliable\n", path.c_str(), (long long)size, mtime);
+ if (sizeOut)
+ *sizeOut = (long long)(size < 0 ? -1 : size);
return sizeTimeTag((long long)(size < 0 ? -1 : size), (long long)mtime);
}
@@ -369,7 +371,7 @@ bool FtpRemote::_listDir(const std::string &dirPath, const std::string &prefix,
RemoteFileInfo info;
info.id = childPath;
info.relPath = childRel;
- info.tag = _tagFor(childPath);
+ info.tag = _tagFor(childPath, &info.size);
out[childRel] = info;
}
if (filtered > 0)
diff --git a/source/modules/ftpremote.h b/source/modules/ftpremote.h
index 501fa70..5de2afd 100644
--- a/source/modules/ftpremote.h
+++ b/source/modules/ftpremote.h
@@ -79,7 +79,8 @@ class FtpRemote : public SyncProvider
bool _listDir(const std::string &dirPath, const std::string &prefix,
const ExtensionFilter &filter, std::map &out);
// MDTM + SIZE for one file, as ":". "" when unavailable.
- std::string _tagFor(const std::string &path);
+ // sizeOut, when given, receives the size alone (-1 when unknown).
+ std::string _tagFor(const std::string &path, long long *sizeOut = NULL);
};
#endif
diff --git a/source/modules/remoteparse.cpp b/source/modules/remoteparse.cpp
index ee951da..1cd31ff 100644
--- a/source/modules/remoteparse.cpp
+++ b/source/modules/remoteparse.cpp
@@ -2,6 +2,7 @@
#include
#include
+#include
#include
#include "../utils/json.h"
@@ -85,6 +86,10 @@ void parseDavResponses(const std::string &xml, const std::string &rootHref,
entry.isCollection = (collection != std::string::npos && collection < end);
entry.tag = davTag(clean, start, end);
+ std::string length = xmlTagText(clean, "getcontentlength", start, end);
+ char *endp = NULL;
+ long long size = length.empty() ? -1 : strtoll(length.c_str(), &endp, 10);
+ entry.size = (endp == NULL || *endp != '\0' || size < 0) ? -1 : size;
out.push_back(entry);
}
}
diff --git a/source/modules/remoteparse.h b/source/modules/remoteparse.h
index 48828c9..c014284 100644
--- a/source/modules/remoteparse.h
+++ b/source/modules/remoteparse.h
@@ -21,6 +21,7 @@ struct DavEntry
std::string relPath; // relative to the PROPFIND root, '/'-prefixed
std::string tag;
bool isCollection;
+ long long size; // getcontentlength, -1 when absent or unreadable
};
// Parse a multistatus body. rootHref is the listing root as the server spells
diff --git a/source/modules/smbremote.cpp b/source/modules/smbremote.cpp
index 7ba84e2..6f1cede 100644
--- a/source/modules/smbremote.cpp
+++ b/source/modules/smbremote.cpp
@@ -266,6 +266,7 @@ bool SmbRemote::_listInto(const std::string &dirPath, const std::string &prefix,
info.id = childPath;
info.relPath = childRel;
info.tag = _tagFor(ent->st);
+ info.size = (long long)ent->st.smb2_size;
out[childRel] = info;
}
}
diff --git a/source/modules/syncprovider.h b/source/modules/syncprovider.h
index c8ed459..0ec0ff1 100644
--- a/source/modules/syncprovider.h
+++ b/source/modules/syncprovider.h
@@ -20,6 +20,10 @@ struct RemoteFileInfo
// whether the remote side changed: an MD5 for Drive, an ETag for WebDAV, or
// ":" where the server offers nothing better.
std::string tag;
+ // Bytes, or -1 when the listing did not say. Only read where a conflict
+ // is checked by comparing contents: a different size settles it without
+ // a download, and a size over [Sync] CompareUpTo= skips the check.
+ long long size = -1;
};
// A remote that performSync() can sync a local folder against. Everything the
diff --git a/source/modules/webdavremote.cpp b/source/modules/webdavremote.cpp
index 9093516..3fa3ad7 100644
--- a/source/modules/webdavremote.cpp
+++ b/source/modules/webdavremote.cpp
@@ -304,6 +304,7 @@ bool WebDavRemote::_listDepthOne(const std::string &root, const std::string &pre
info.id = childPath;
info.relPath = childRel;
info.tag = entry.tag;
+ info.size = entry.size;
out[childRel] = info;
}
return true;
@@ -332,6 +333,7 @@ bool WebDavRemote::list(const std::string &root, const ExtensionFilter &filter,
info.id = root + entry.relPath;
info.relPath = entry.relPath;
info.tag = entry.tag;
+ info.size = entry.size;
out[entry.relPath] = info;
}
return true;
diff --git a/source/sync/remoteconfig.cpp b/source/sync/remoteconfig.cpp
index aaa6784..72c2eeb 100644
--- a/source/sync/remoteconfig.cpp
+++ b/source/sync/remoteconfig.cpp
@@ -1,6 +1,7 @@
#include "remoteconfig.h"
#include
+#include
#include "../utils/pathutil.h"
@@ -310,3 +311,40 @@ std::string manifestScope(const RemoteConfig &remote, const std::string &provide
{
return remote.label.empty() ? providerPrefix : providerPrefix + "#" + remote.label;
}
+
+const long long COMPARE_UP_TO_DEFAULT_KB = 4096;
+const long long COMPARE_UP_TO_UNLIMITED = std::numeric_limits::max();
+
+bool parseCompareUpTo(const std::string &value, long long &bytes)
+{
+ bytes = COMPARE_UP_TO_DEFAULT_KB * 1024;
+ size_t start = value.find_first_not_of(" \t");
+ if (start == std::string::npos)
+ return true;
+ size_t end = value.find_last_not_of(" \t") + 1;
+ long long kb = 0;
+ for (size_t i = start; i < end; i++)
+ {
+ if (!isdigit((unsigned char)value[i]) || kb > 1000000000LL)
+ return false;
+ kb = kb * 10 + (value[i] - '0');
+ }
+ bytes = kb == 0 ? COMPARE_UP_TO_UNLIMITED : kb * 1024;
+ return true;
+}
+
+bool parseCompareConflicts(const std::string &value, bool &on)
+{
+ on = true;
+ std::string v = asciiLower(value);
+ size_t start = v.find_first_not_of(" \t");
+ if (start == std::string::npos)
+ return true;
+ v = v.substr(start, v.find_last_not_of(" \t") + 1 - start);
+ if (v == "false" || v == "no" || v == "0" || v == "off")
+ {
+ on = false;
+ return true;
+ }
+ return v == "true" || v == "yes" || v == "1" || v == "on";
+}
diff --git a/source/sync/remoteconfig.h b/source/sync/remoteconfig.h
index 083bd41..8891738 100644
--- a/source/sync/remoteconfig.h
+++ b/source/sync/remoteconfig.h
@@ -121,4 +121,19 @@ std::string remoteNames(const std::vector &remotes);
// a labelled one. Renaming a label therefore starts its state afresh.
std::string manifestScope(const RemoteConfig &remote, const std::string &providerPrefix);
+// [Sync] CompareUpTo=: the largest file, in KB, that a conflict on a remote
+// without content hashes (SMB, FTP, WebDAV) is downloaded for, to check
+// whether the two copies really differ. Missing or empty is the default; 0
+// is no limit, and bytes is then COMPARE_UP_TO_UNLIMITED. False, with bytes
+// left at the default, for anything but a whole number.
+extern const long long COMPARE_UP_TO_DEFAULT_KB;
+extern const long long COMPARE_UP_TO_UNLIMITED;
+bool parseCompareUpTo(const std::string &value, long long &bytes);
+
+// [Sync] CompareConflicts=: false turns that check off, whatever
+// CompareUpTo= says, so every such conflict is asked about. true/yes/1/on
+// or false/no/0/off, any case; missing or empty is on. False, with on left
+// true, for anything else.
+bool parseCompareConflicts(const std::string &value, bool &on);
+
#endif
diff --git a/source/sync/syncengine.cpp b/source/sync/syncengine.cpp
index a424c9a..07a1afd 100644
--- a/source/sync/syncengine.cpp
+++ b/source/sync/syncengine.cpp
@@ -29,6 +29,23 @@ struct Side
long long held; // generation it holds as of its last sync, 0: unknown
bool changed; // new there, or its tag moved since its last sync
bool sameAsLocal; // provably holds the 3DS's current bytes
+ // Its copy, fetched to compare with the 3DS's and found different: if
+ // this side wins the conflict, it is moved into place instead of being
+ // downloaded again. "" when there is none.
+ std::string fetched;
+};
+
+// Removes the comparison copies no side used, however syncFile() returns.
+struct FetchedCopies
+{
+ std::vector &sides;
+ explicit FetchedCopies(std::vector &s) : sides(s) {}
+ ~FetchedCopies()
+ {
+ for (auto &s : sides)
+ if (!s.fetched.empty() && remove(s.fetched.c_str()) != 0)
+ debugErrno("removing the comparison copy", s.fetched);
+ }
};
struct LocalFile
@@ -105,6 +122,20 @@ static bool downloadFrom(Side &s, const std::string &localPath, SyncSummary &sum
// provider to remember it.
mkparents(localPath);
+ // The conflict check already fetched this version in full.
+ if (!s.fetched.empty())
+ {
+ std::string fetched = s.fetched;
+ s.fetched.clear(); // replaceLocalFile() consumes it either way
+ if (replaceLocalFile(fetched, localPath))
+ {
+ debugf(" used the copy fetched to compare, no second download\n");
+ summary.downloaded++;
+ return true;
+ }
+ debugf(" could not move the compared copy into place, downloading again\n");
+ }
+
if (!s.p->provider->download(*s.file, localPath))
{
debugf("download failed: %s %s -> %s\n", s.p->label.c_str(),
@@ -120,6 +151,57 @@ static bool downloadFrom(Side &s, const std::string &localPath, SyncSummary &sum
return true;
}
+// ---------------------------------------------------------------------------
+// sameAsLocalCopy — is a conflicting side really a different version?
+// ---------------------------------------------------------------------------
+// Drive and Dropbox prove it with a content hash before a conflict is ever
+// raised. SMB, FTP and WebDAV tag with size and time, which a copy made
+// separately (a remote added with the saves already on it, a file copied
+// back by hand) never matches; the only proof is the bytes. So within the
+// configured size the remote copy is downloaded to a temp file beside the
+// save and compared: a different size settles it without a download, and
+// anything that cannot be checked is a conflict, as before.
+// ---------------------------------------------------------------------------
+static bool sameAsLocalCopy(Side &s, int index, const std::string &localPath,
+ long long localSize, long long compareUpTo)
+{
+ const char *label = s.p->label.c_str();
+ if (!s.p->provider->localTag(localPath).empty())
+ return false; // a content hash already said they differ
+ if (s.file->size >= 0 && s.file->size != localSize)
+ {
+ debugf(" %s: %lld bytes against the 3DS's %lld, a real conflict\n", label,
+ s.file->size, localSize);
+ return false;
+ }
+ long long size = s.file->size >= 0 ? s.file->size : localSize;
+ if (size > compareUpTo)
+ {
+ debugf(" %s: %lld bytes, over CompareUpTo=, not compared\n", label, size);
+ return false;
+ }
+
+ // One per side, as several can be compared for one conflict. Ends in the
+ // transfer suffix, so a leftover is never synced as a save.
+ char suffix[24];
+ snprintf(suffix, sizeof(suffix), ".compare%d", index);
+ std::string tmpPath = localPath + suffix + TEMP_SUFFIX;
+ if (!s.p->provider->download(*s.file, tmpPath))
+ {
+ debugf(" %s: could not fetch it to compare\n", label);
+ remove(tmpPath.c_str());
+ return false;
+ }
+ int same = compareFiles(tmpPath, localPath);
+ debugf(" %s: compared %lld bytes, %s\n", label, size,
+ same == 1 ? "the same as the 3DS" : "they differ");
+ if (same == 0)
+ s.fetched = tmpPath; // kept in case this side wins
+ else if (remove(tmpPath.c_str()) != 0)
+ debugErrno("removing the comparison copy", tmpPath);
+ return same == 1;
+}
+
// ---------------------------------------------------------------------------
// syncFile — decide one file with every side in view
// ---------------------------------------------------------------------------
@@ -127,7 +209,7 @@ static bool downloadFrom(Side &s, const std::string &localPath, SyncSummary &sum
// ---------------------------------------------------------------------------
static bool syncFile(std::vector &live, Manifest &manifest,
const std::string &relPath, const std::string &localPath,
- SyncSummary &summary, SyncUi &ui)
+ long long compareUpTo, SyncSummary &summary, SyncUi &ui)
{
LocalFile local = statLocal(localPath);
const std::string localKey = LOCAL_PREFIX + localPath;
@@ -147,6 +229,7 @@ static bool syncFile(std::vector &live, Manifest &manifest,
s.sameAsLocal = false;
sides.push_back(s);
}
+ FetchedCopies fetchedCopies(sides);
// --- The 3DS's copy as of the last sync ---
bool hasBase = manifest.has(localKey);
@@ -280,6 +363,24 @@ static bool syncFile(std::vector &live, Manifest &manifest,
break;
}
+ // --- A conflict may be the same bytes, reached separately ---
+ if (candidates.size() > 1 && local.exists && compareUpTo > 0)
+ {
+ for (size_t k = 0; k < candidates.size();)
+ {
+ int c = candidates[k];
+ if (c >= 0 && sameAsLocalCopy(sides[c], c, localPath, local.size, compareUpTo))
+ {
+ sides[c].sameAsLocal = true;
+ candidates.erase(candidates.begin() + k);
+ }
+ else
+ k++;
+ }
+ if (ui.cancelRequested())
+ return false;
+ }
+
// --- Pick the version every side ends up with ---
int source = -2; // -2: none
if (candidates.size() == 1)
@@ -570,7 +671,8 @@ bool syncEntry(std::vector &participants, Manifest &manifest,
if (live.empty())
break;
- if (!syncFile(live, manifest, relPath, entry.localBase + relPath, summary, ui))
+ if (!syncFile(live, manifest, relPath, entry.localBase + relPath, entry.compareUpTo,
+ summary, ui))
return false;
}
diff --git a/source/sync/syncengine.h b/source/sync/syncengine.h
index 4b782cc..59baeb8 100644
--- a/source/sync/syncengine.h
+++ b/source/sync/syncengine.h
@@ -28,6 +28,11 @@ struct SyncEntry
SyncDirection direction;
// Files on either side that do not match are left out of the sync.
ExtensionFilter filter;
+ // A conflict with a remote that has no content hash: the largest file,
+ // in bytes, downloaded to see whether it really differs from the 3DS's
+ // copy. 0 never compares (an entry built in code); [Sync] CompareUpTo=0
+ // sets no limit instead (COMPARE_UP_TO_UNLIMITED).
+ long long compareUpTo = 0;
};
// ---------------------------------------------------------------------------
diff --git a/source/utils/fsutil.cpp b/source/utils/fsutil.cpp
index 1199434..e937905 100644
--- a/source/utils/fsutil.cpp
+++ b/source/utils/fsutil.cpp
@@ -6,6 +6,7 @@
#include
#include
#include
+#include
// EEXIST is the normal outcome for every component but the last, so only other
@@ -72,3 +73,41 @@ bool replaceLocalFile(const std::string &tmpPath, const std::string &localPath)
debugErrno("removing the backup", bakPath);
return true;
}
+
+int compareFiles(const std::string &a, const std::string &b)
+{
+ FILE *fa = fopen(a.c_str(), "rb");
+ if (!fa)
+ {
+ debugErrno("opening to compare", a);
+ return -1;
+ }
+ FILE *fb = fopen(b.c_str(), "rb");
+ if (!fb)
+ {
+ debugErrno("opening to compare", b);
+ fclose(fa);
+ return -1;
+ }
+
+ static const size_t CHUNK = 16 * 1024;
+ std::vector bufA(CHUNK), bufB(CHUNK);
+ int result = 1;
+ while (result == 1)
+ {
+ size_t na = fread(&bufA[0], 1, CHUNK, fa);
+ size_t nb = fread(&bufB[0], 1, CHUNK, fb);
+ if (na != nb || memcmp(&bufA[0], &bufB[0], na) != 0)
+ result = 0;
+ else if (na < CHUNK)
+ break; // both ended here
+ }
+ if (ferror(fa) || ferror(fb))
+ {
+ debugf("reading %s or %s to compare failed\n", a.c_str(), b.c_str());
+ result = -1;
+ }
+ fclose(fa);
+ fclose(fb);
+ return result;
+}
diff --git a/source/utils/fsutil.h b/source/utils/fsutil.h
index 16328f9..c0a1b83 100644
--- a/source/utils/fsutil.h
+++ b/source/utils/fsutil.h
@@ -19,4 +19,8 @@ FILE *openTempFor(const std::string &localPath, std::string &tmpPathOut);
// restored if the swap fails. Removes tmpPath on failure.
bool replaceLocalFile(const std::string &tmpPath, const std::string &localPath);
+// Byte for byte: 1 when the two files are identical, 0 when they differ, -1
+// when either cannot be read (said through debugf()).
+int compareFiles(const std::string &a, const std::string &b);
+
#endif
diff --git a/tests/Makefile b/tests/Makefile
index 1f9601f..4b34297 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -42,7 +42,7 @@ UI_UNITS := ../source/ui/canvas.cpp ../source/ui/fontdata.cpp \
TEST_SRC := framework.cpp mocks.cpp main.cpp \
test_debug.cpp test_engine.cpp test_extfilter.cpp test_json.cpp test_manifest.cpp test_nway.cpp \
- test_remoteconfig.cpp test_rootname.cpp \
+ test_remoteconfig.cpp test_rootname.cpp test_compare.cpp \
test_path.cpp \
test_remoteparse.cpp test_time.cpp test_url.cpp test_xml.cpp \
test_ui.cpp
diff --git a/tests/configurator/inifile.test.js b/tests/configurator/inifile.test.js
index 0ab83b4..1658c77 100644
--- a/tests/configurator/inifile.test.js
+++ b/tests/configurator/inifile.test.js
@@ -155,3 +155,28 @@ test('an imported Drive section with a client of its own stays in expert mode, s
assert.equal(sealed.config.remotes[0].fields['gdrive-expert-mode'], true);
assert.equal(sealed.config.remotes[0].auth.refreshToken, '');
});
+
+test('[Sync] is written only when it says something', () => {
+ let base = { remotes: [remote('ftp', { entries: [['Host', 'h']] })],
+ folders: [{ name: 'saves', path: '/s', recursive: true, direction: 'both', filter: '' }], filter: '', extras: [] };
+ assert.doesNotMatch(IniFile.toIni(base, false), /\[Sync\]/);
+ base.sync = { compare: true, compareUpTo: '', other: [] };
+ assert.doesNotMatch(IniFile.toIni(base, false), /\[Sync\]/);
+ base.sync = { compare: false, compareUpTo: '512', other: [['Later', 'x']] };
+ assert.match(IniFile.toIni(base, false), /\n\[Sync\]\nCompareConflicts=false\nCompareUpTo=512\nLater=x\n$/);
+});
+
+test('[Sync] is read back into the form', () => {
+ let { config, report } = IniFile.fromIni('[FTP]\nHost=h\n[Paths]\nsaves=/s\n'
+ + '[Sync]\nCompareConflicts=Off\nCompareUpTo=0\nLater=x\n');
+ assert.deepEqual(config.sync, { compare: false, compareUpTo: '0', other: [['Later', 'x']] });
+ assert.deepEqual(report.kept, []);
+ assert.match(IniFile.toIni(config, false), /\[Sync\]\nCompareConflicts=false\nCompareUpTo=0\nLater=x\n$/);
+
+ let odd = IniFile.fromIni('[FTP]\nHost=h\n[Paths]\nsaves=/s\n[Sync]\nCompareConflicts=maybe\n');
+ assert.equal(odd.config.sync.compare, true);
+ assert.match(odd.report.problems.join(' '), /CompareConflicts=maybe/);
+
+ let none = IniFile.fromIni('[FTP]\nHost=h\n[Paths]\nsaves=/s\n');
+ assert.deepEqual(none.config.sync, { compare: true, compareUpTo: '', other: [] });
+});
diff --git a/tests/mocks.h b/tests/mocks.h
index c0c44ae..41e615f 100644
--- a/tests/mocks.h
+++ b/tests/mocks.h
@@ -46,6 +46,8 @@ class MockRemote : public SyncProvider
// With contentTags on, tags are derived from file contents, the way Drive's
// MD5 and Dropbox's content_hash are — which is what makes localTag() work.
bool contentTags = false;
+ // Whether list() reports sizes, as SMB, FTP and WebDAV do.
+ bool reportSizes = true;
std::string prefix = "mock";
// Recorded calls, for assertions.
@@ -100,6 +102,8 @@ class MockRemote : public SyncProvider
info.relPath = entry.first;
info.tag = entry.second.tag;
info.id = entry.second.id.empty() ? ("id" + entry.first) : entry.second.id;
+ if (reportSizes)
+ info.size = (long long)entry.second.content.size();
out[entry.first] = info;
}
return true;
diff --git a/tests/test_compare.cpp b/tests/test_compare.cpp
new file mode 100644
index 0000000..5bf2e34
--- /dev/null
+++ b/tests/test_compare.cpp
@@ -0,0 +1,312 @@
+// A conflict with a remote that has no content hash (SMB, FTP, WebDAV) is
+// checked against the bytes before anyone is asked: within [Sync]
+// CompareUpTo= the remote copy is downloaded and compared with the 3DS's.
+
+#include "framework.h"
+#include "mocks.h"
+
+#include "../source/sync/remoteconfig.h"
+#include "../source/utils/fsutil.h"
+#include "../source/utils/pathutil.h"
+
+namespace
+{
+
+const char *SAVE = "/Game/001.sav";
+
+struct Fixture
+{
+ TempDir local;
+ MockRemote remote;
+ MockUi ui;
+ Manifest manifest;
+ SyncSummary summary;
+ SyncEntry entry;
+
+ Fixture() : manifest(std::string("/tmp/3dsync-tests-manifest-unused.json"))
+ {
+ entry.localBase = local.path();
+ entry.remoteName = "saves";
+ entry.direction = SYNC_BOTH;
+ entry.compareUpTo = 1024;
+ entry.localFiles.push_back(SAVE);
+ ui.answers = {"skip"};
+ }
+
+ bool run()
+ {
+ summary = SyncSummary();
+ return performSync(remote, manifest, entry, summary, false, ui);
+ }
+
+ // First sync with a copy on both sides: a conflict unless they match.
+ void bothHave(const std::string &onThe3ds, const std::string &onTheRemote)
+ {
+ local.write(SAVE, onThe3ds, 1700000000);
+ remote.setRemote(SAVE, onTheRemote);
+ }
+
+ bool leftATempFile() const { return local.exists(std::string(SAVE) + ".compare0" + TEMP_SUFFIX); }
+};
+
+} // namespace
+
+TEST(compare, the_same_bytes_are_no_conflict)
+{
+ Fixture f;
+ f.bothHave("same save", "same save");
+
+ CHECK(f.run());
+ CHECK(f.ui.conflictsRaised.empty());
+ CHECK_EQ(f.remote.downloaded.size(), (size_t)1); // the comparison, and nothing else
+ CHECK(f.remote.uploaded.empty());
+ CHECK(!f.leftATempFile());
+ CHECK_STR_EQ(f.local.read(SAVE), "same save");
+
+ // Recorded as in step: the next run has nothing to compare or copy.
+ f.remote.downloaded.clear();
+ CHECK(f.run());
+ CHECK(f.ui.conflictsRaised.empty());
+ CHECK(f.remote.downloaded.empty());
+ CHECK(f.remote.uploaded.empty());
+}
+
+TEST(compare, different_bytes_of_the_same_size_are_asked_about)
+{
+ Fixture f;
+ f.bothHave("save A", "save B");
+
+ CHECK(f.run());
+ CHECK_EQ(f.ui.conflictsRaised.size(), (size_t)1);
+ CHECK_EQ(f.remote.downloaded.size(), (size_t)1);
+ CHECK(!f.leftATempFile());
+ CHECK_STR_EQ(f.local.read(SAVE), "save A"); // skipped: nothing replaced
+}
+
+TEST(compare, the_compared_copy_is_used_when_the_remote_wins)
+{
+ Fixture f;
+ f.bothHave("save A", "save B");
+ f.ui.answers = {"Mock"};
+
+ CHECK(f.run());
+ CHECK_EQ(f.ui.conflictsRaised.size(), (size_t)1);
+ CHECK_EQ(f.remote.downloaded.size(), (size_t)1); // fetched once, to compare
+ CHECK_STR_EQ(f.local.read(SAVE), "save B");
+ CHECK_EQ(f.summary.downloaded, 1);
+ CHECK(!f.leftATempFile());
+
+ // In step afterwards.
+ f.remote.downloaded.clear();
+ f.ui.conflictsRaised.clear();
+ CHECK(f.run());
+ CHECK(f.ui.conflictsRaised.empty());
+ CHECK(f.remote.downloaded.empty());
+}
+
+TEST(compare, the_compared_copy_is_dropped_when_the_3ds_wins)
+{
+ Fixture f;
+ f.bothHave("save A", "save B");
+ f.ui.answers = {"3DS"};
+
+ CHECK(f.run());
+ CHECK_STR_EQ(f.local.read(SAVE), "save A");
+ CHECK_STR_EQ(f.remote.files[SAVE].content, "save A");
+ CHECK(!f.leftATempFile());
+}
+
+TEST(compare, a_different_size_needs_no_download)
+{
+ Fixture f;
+ f.bothHave("short", "a longer save");
+
+ CHECK(f.run());
+ CHECK_EQ(f.ui.conflictsRaised.size(), (size_t)1);
+ CHECK(f.remote.downloaded.empty());
+}
+
+TEST(compare, without_a_size_the_bytes_decide)
+{
+ Fixture f;
+ f.remote.reportSizes = false;
+ f.bothHave("same save", "same save");
+
+ CHECK(f.run());
+ CHECK(f.ui.conflictsRaised.empty());
+ CHECK_EQ(f.remote.downloaded.size(), (size_t)1);
+}
+
+TEST(compare, a_file_over_the_limit_is_asked_about)
+{
+ Fixture f;
+ f.entry.compareUpTo = 4;
+ f.bothHave("same save", "same save");
+
+ CHECK(f.run());
+ CHECK_EQ(f.ui.conflictsRaised.size(), (size_t)1);
+ CHECK(f.remote.downloaded.empty());
+}
+
+TEST(compare, no_limit_compares_any_size)
+{
+ Fixture f;
+ f.entry.compareUpTo = COMPARE_UP_TO_UNLIMITED;
+ std::string big(100000, 's');
+ f.bothHave(big, big);
+
+ CHECK(f.run());
+ CHECK(f.ui.conflictsRaised.empty());
+ CHECK_EQ(f.remote.downloaded.size(), (size_t)1);
+}
+
+TEST(compare, an_entry_without_a_limit_set_never_compares)
+{
+ Fixture f;
+ f.entry.compareUpTo = 0;
+ f.bothHave("same save", "same save");
+
+ CHECK(f.run());
+ CHECK_EQ(f.ui.conflictsRaised.size(), (size_t)1);
+ CHECK(f.remote.downloaded.empty());
+}
+
+TEST(compare, a_content_hash_is_trusted_without_a_download)
+{
+ Fixture f;
+ f.remote.contentTags = true;
+ f.bothHave("save A", "save B");
+
+ CHECK(f.run());
+ CHECK_EQ(f.ui.conflictsRaised.size(), (size_t)1);
+ CHECK(f.remote.downloaded.empty());
+}
+
+TEST(compare, a_failed_download_is_asked_about)
+{
+ Fixture f;
+ f.remote.failDownloads = true;
+ f.bothHave("same save", "same save");
+
+ CHECK(f.run());
+ CHECK_EQ(f.ui.conflictsRaised.size(), (size_t)1);
+ CHECK(!f.leftATempFile());
+}
+
+TEST(compare, both_changed_to_the_same_bytes_after_a_sync)
+{
+ Fixture f;
+ f.bothHave("old", "old");
+ f.entry.compareUpTo = 0;
+ f.ui.answers = {"3DS"};
+ CHECK(f.run()); // in step from here on
+ f.entry.compareUpTo = 1024;
+ f.ui.conflictsRaised.clear();
+ f.remote.downloaded.clear();
+ f.remote.uploaded.clear();
+
+ // The same edit reached both sides separately, e.g. copied by hand.
+ f.local.write(SAVE, "new save", 1700000900);
+ f.remote.setRemote(SAVE, "new save");
+
+ CHECK(f.run());
+ CHECK(f.ui.conflictsRaised.empty());
+ CHECK(f.remote.uploaded.empty());
+ CHECK_EQ(f.remote.downloaded.size(), (size_t)1);
+}
+
+TEST(compare, compare_files)
+{
+ TempDir dir;
+ dir.write("/a", "abc", 1700000000);
+ dir.write("/b", "abc", 1700000000);
+ dir.write("/c", "abd", 1700000000);
+ dir.write("/d", "abcd", 1700000000);
+ dir.write("/e", "", 1700000000);
+ dir.write("/f", "", 1700000000);
+ std::string big(40000, 'x'), big2 = big;
+ big2[39999] = 'y';
+ dir.write("/g", big, 1700000000);
+ dir.write("/h", big, 1700000000);
+ dir.write("/i", big2, 1700000000);
+ std::string p = dir.path();
+ CHECK_EQ(compareFiles(p + "/a", p + "/b"), 1);
+ CHECK_EQ(compareFiles(p + "/a", p + "/c"), 0);
+ CHECK_EQ(compareFiles(p + "/a", p + "/d"), 0);
+ CHECK_EQ(compareFiles(p + "/d", p + "/a"), 0);
+ CHECK_EQ(compareFiles(p + "/e", p + "/f"), 1);
+ CHECK_EQ(compareFiles(p + "/g", p + "/h"), 1); // past one read chunk
+ CHECK_EQ(compareFiles(p + "/g", p + "/i"), 0);
+ CHECK_EQ(compareFiles(p + "/a", p + "/missing"), -1);
+}
+
+TEST(compare, compare_conflicts_setting)
+{
+ bool on = false;
+ CHECK(parseCompareConflicts("", on));
+ CHECK(on);
+ CHECK(parseCompareConflicts("false", on));
+ CHECK(!on);
+ CHECK(parseCompareConflicts(" Off ", on));
+ CHECK(!on);
+ CHECK(parseCompareConflicts("0", on));
+ CHECK(!on);
+ CHECK(parseCompareConflicts("YES", on));
+ CHECK(on);
+ CHECK(!parseCompareConflicts("never", on));
+ CHECK(on); // the default until it is fixed
+}
+
+TEST(compare, compare_up_to_setting)
+{
+ long long bytes = 0;
+ CHECK(parseCompareUpTo("", bytes));
+ CHECK_EQ(bytes, 4096LL * 1024);
+ CHECK(parseCompareUpTo(" 512 ", bytes));
+ CHECK_EQ(bytes, 512LL * 1024);
+ CHECK(parseCompareUpTo("0", bytes));
+ CHECK_EQ(bytes, COMPARE_UP_TO_UNLIMITED);
+ CHECK(!parseCompareUpTo("4MB", bytes));
+ CHECK_EQ(bytes, 4096LL * 1024); // the default until it is fixed
+ CHECK(!parseCompareUpTo("-1", bytes));
+ CHECK(!parseCompareUpTo("1.5", bytes));
+ CHECK(!parseCompareUpTo("99999999999999", bytes));
+}
+
+TEST(compare, with_several_remotes_only_the_winners_copy_is_kept)
+{
+ TempDir local;
+ MockRemote a, b;
+ a.prefix = "a";
+ b.prefix = "b";
+ MockUi ui;
+ ui.answers = {"B"};
+ Manifest manifest(std::string("/tmp/3dsync-tests-manifest-unused.json"));
+ SyncSummary summary;
+ SyncEntry entry;
+ entry.localBase = local.path();
+ entry.remoteName = "saves";
+ entry.direction = SYNC_BOTH;
+ entry.compareUpTo = 1024;
+ entry.localFiles.push_back(SAVE);
+ local.write(SAVE, "3ds", 1700000000);
+ a.setRemote(SAVE, "aaa");
+ b.setRemote(SAVE, "bbb");
+
+ std::vector parts;
+ parts.push_back(Participant(a, "A", a.prefix));
+ parts.push_back(Participant(b, "B", b.prefix));
+ CHECK(syncEntry(parts, manifest, entry, summary, ui));
+
+ CHECK_EQ(ui.conflictsRaised.size(), (size_t)1);
+ CHECK_EQ(a.downloaded.size(), (size_t)1); // compared
+ CHECK_EQ(b.downloaded.size(), (size_t)1); // compared, then used
+ CHECK_STR_EQ(local.read(SAVE), "bbb");
+ CHECK_STR_EQ(a.files[SAVE].content, "bbb");
+ for (int i = 0; i < 3; i++)
+ {
+ char name[48];
+ snprintf(name, sizeof(name), "%s.compare%d%s", SAVE, i, TEMP_SUFFIX);
+ CHECK(!local.exists(name));
+ }
+}
diff --git a/tests/test_remoteparse.cpp b/tests/test_remoteparse.cpp
index 5adc24f..a47bb9a 100644
--- a/tests/test_remoteparse.cpp
+++ b/tests/test_remoteparse.cpp
@@ -168,6 +168,23 @@ TEST(webdav_parse, reads_files_and_collections)
CHECK(!out[1].isCollection);
}
+TEST(webdav_parse, reads_the_size_where_given)
+{
+ std::vector out;
+ parseDavResponses(DAV_BODY, "/dav/3DS/", out);
+ CHECK_EQ(out[1].size, 32768LL);
+ CHECK_EQ(out[0].size, -1LL); // a collection sends none
+
+ const char *odd =
+ "/dav/3DS/x.sav"
+ "12kB"
+ "";
+ std::vector unreadable;
+ parseDavResponses(odd, "/dav/3DS/", unreadable);
+ CHECK_EQ(unreadable.size(), (size_t)1);
+ CHECK_EQ(unreadable[0].size, -1LL);
+}
+
TEST(webdav_parse, prefers_the_etag_and_strips_its_quoting)
{
std::vector out;