2021-05-17 00:39:33 +08:00
|
|
|
package bisync
|
|
|
|
|
|
|
|
import (
|
bisync: full support for comparing checksum, size, modtime - fixes #5679 fixes #5683 fixes #5684 fixes #5675
Before this change, bisync could only detect changes based on modtime, and
would refuse to run if either path lacked modtime support. This made bisync
unavailable for many of rclone's backends. Additionally, bisync did not account
for the Fs's precision when comparing modtimes, meaning that they could only be
reliably compared within the same side -- not against the opposite side. Size
and checksum (even when available) were ignored completely for deltas.
After this change, bisync now fully supports comparing based on any combination
of size, modtime, and checksum, lifting the prior restriction on backends
without modtime support. The comparison logic considers the backend's
precision, hash types, and other features as appropriate.
The comparison features optionally use a new --compare flag (which takes any
combination of size,modtime,checksum) and even supports some combinations not
otherwise supported in `sync` (like comparing all three at the same time.) By
default (without the --compare flag), bisync inherits the same comparison
options as `sync` (that is: size and modtime by default, unless modified with
flags such as --checksum or --size-only.) If the --compare flag is set, it will
override these defaults.
If --compare includes checksum and both remotes support checksums but have no
hash types in common with each other, checksums will be considered only for
comparisons within the same side (to determine what has changed since the prior
sync), but not for comparisons against the opposite side. If one side supports
checksums and the other does not, checksums will only be considered on the side
that supports them. When comparing with checksum and/or size without modtime,
bisync cannot determine whether a file is newer or older -- only whether it is
changed or unchanged. (If it is changed on both sides, bisync still does the
standard equality-check to avoid declaring a sync conflict unless it absolutely
has to.)
Also included are some new flags to customize the checksum comparison behavior
on backends where hashes are slow or unavailable. --no-slow-hash and
--slow-hash-sync-only allow selectively ignoring checksums on backends such as
local where they are slow. --download-hash allows computing them by downloading
when (and only when) they're otherwise not available. Of course, this option
probably won't be practical with large files, but may be a good option for
syncing small-but-important files with maximum accuracy (for example, a source
code repo on a crypt remote.) An additional advantage over methods like
cryptcheck is that the original file is not required for comparison (for
example, --download-hash can be used to bisync two different crypt remotes with
different passwords.)
Additionally, all of the above are now considered during the final --check-sync
for much-improved accuracy (before this change, it only compared filenames!)
Many other details are explained in the included docs.
2023-12-01 08:44:38 +08:00
|
|
|
"encoding/json"
|
2021-05-17 00:39:33 +08:00
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/rclone/rclone/fs"
|
2023-11-18 01:14:38 +08:00
|
|
|
"github.com/rclone/rclone/lib/encoder"
|
2023-11-06 19:59:41 +08:00
|
|
|
"github.com/rclone/rclone/lib/terminal"
|
2021-05-17 00:39:33 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func (b *bisyncRun) indentf(tag, file, format string, args ...interface{}) {
|
|
|
|
b.indent(tag, file, fmt.Sprintf(format, args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bisyncRun) indent(tag, file, msg string) {
|
|
|
|
logf := fs.Infof
|
|
|
|
switch {
|
|
|
|
case tag == "ERROR":
|
|
|
|
tag = ""
|
|
|
|
logf = fs.Errorf
|
|
|
|
case tag == "INFO":
|
|
|
|
tag = ""
|
|
|
|
case strings.HasPrefix(tag, "!"):
|
|
|
|
tag = tag[1:]
|
|
|
|
logf = fs.Logf
|
|
|
|
}
|
2023-11-06 19:59:41 +08:00
|
|
|
|
2024-03-06 22:26:53 +08:00
|
|
|
if b.opt.DryRun {
|
|
|
|
logf = fs.Logf
|
|
|
|
}
|
|
|
|
|
bisync: full support for comparing checksum, size, modtime - fixes #5679 fixes #5683 fixes #5684 fixes #5675
Before this change, bisync could only detect changes based on modtime, and
would refuse to run if either path lacked modtime support. This made bisync
unavailable for many of rclone's backends. Additionally, bisync did not account
for the Fs's precision when comparing modtimes, meaning that they could only be
reliably compared within the same side -- not against the opposite side. Size
and checksum (even when available) were ignored completely for deltas.
After this change, bisync now fully supports comparing based on any combination
of size, modtime, and checksum, lifting the prior restriction on backends
without modtime support. The comparison logic considers the backend's
precision, hash types, and other features as appropriate.
The comparison features optionally use a new --compare flag (which takes any
combination of size,modtime,checksum) and even supports some combinations not
otherwise supported in `sync` (like comparing all three at the same time.) By
default (without the --compare flag), bisync inherits the same comparison
options as `sync` (that is: size and modtime by default, unless modified with
flags such as --checksum or --size-only.) If the --compare flag is set, it will
override these defaults.
If --compare includes checksum and both remotes support checksums but have no
hash types in common with each other, checksums will be considered only for
comparisons within the same side (to determine what has changed since the prior
sync), but not for comparisons against the opposite side. If one side supports
checksums and the other does not, checksums will only be considered on the side
that supports them. When comparing with checksum and/or size without modtime,
bisync cannot determine whether a file is newer or older -- only whether it is
changed or unchanged. (If it is changed on both sides, bisync still does the
standard equality-check to avoid declaring a sync conflict unless it absolutely
has to.)
Also included are some new flags to customize the checksum comparison behavior
on backends where hashes are slow or unavailable. --no-slow-hash and
--slow-hash-sync-only allow selectively ignoring checksums on backends such as
local where they are slow. --download-hash allows computing them by downloading
when (and only when) they're otherwise not available. Of course, this option
probably won't be practical with large files, but may be a good option for
syncing small-but-important files with maximum accuracy (for example, a source
code repo on a crypt remote.) An additional advantage over methods like
cryptcheck is that the original file is not required for comparison (for
example, --download-hash can be used to bisync two different crypt remotes with
different passwords.)
Additionally, all of the above are now considered during the final --check-sync
for much-improved accuracy (before this change, it only compared filenames!)
Many other details are explained in the included docs.
2023-12-01 08:44:38 +08:00
|
|
|
if tag == "Path1" {
|
|
|
|
tag = Color(terminal.CyanFg, "Path1")
|
|
|
|
} else {
|
|
|
|
tag = Color(terminal.BlueFg, tag)
|
|
|
|
}
|
2023-11-06 19:59:41 +08:00
|
|
|
msg = Color(terminal.MagentaFg, msg)
|
2024-05-31 21:25:27 +08:00
|
|
|
msg = strings.ReplaceAll(msg, "Queue copy to", Color(terminal.GreenFg, "Queue copy to"))
|
|
|
|
msg = strings.ReplaceAll(msg, "Queue delete", Color(terminal.RedFg, "Queue delete"))
|
2023-11-06 19:59:41 +08:00
|
|
|
file = Color(terminal.CyanFg, escapePath(file, false))
|
|
|
|
logf(nil, "- %-18s%-43s - %s", tag, msg, file)
|
2021-05-17 00:39:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// escapePath will escape control characters in path.
|
|
|
|
// It won't quote just due to backslashes on Windows.
|
|
|
|
func escapePath(path string, forceQuotes bool) string {
|
2023-11-18 01:14:38 +08:00
|
|
|
path = encode(path)
|
2021-05-17 00:39:33 +08:00
|
|
|
test := path
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
test = strings.ReplaceAll(path, "\\", "/")
|
|
|
|
}
|
|
|
|
if strconv.Quote(test) != `"`+test+`"` {
|
|
|
|
return strconv.Quote(path)
|
|
|
|
}
|
|
|
|
if forceQuotes {
|
|
|
|
return `"` + path + `"`
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
func quotePath(path string) string {
|
|
|
|
return escapePath(path, true)
|
|
|
|
}
|
2023-11-06 19:59:41 +08:00
|
|
|
|
2024-11-12 01:54:59 +08:00
|
|
|
// Colors controls whether terminal colors are enabled
|
|
|
|
var Colors bool
|
2023-12-24 04:31:33 +08:00
|
|
|
|
2023-11-06 19:59:41 +08:00
|
|
|
// Color handles terminal colors for bisync
|
|
|
|
func Color(style string, s string) string {
|
2023-12-24 04:31:33 +08:00
|
|
|
if !Colors {
|
|
|
|
return s
|
|
|
|
}
|
2023-11-06 19:59:41 +08:00
|
|
|
terminal.Start()
|
|
|
|
return style + s + terminal.Reset
|
|
|
|
}
|
2023-11-18 01:14:38 +08:00
|
|
|
|
|
|
|
func encode(s string) string {
|
|
|
|
return encoder.OS.ToStandardPath(encoder.OS.FromStandardPath(s))
|
|
|
|
}
|
bisync: full support for comparing checksum, size, modtime - fixes #5679 fixes #5683 fixes #5684 fixes #5675
Before this change, bisync could only detect changes based on modtime, and
would refuse to run if either path lacked modtime support. This made bisync
unavailable for many of rclone's backends. Additionally, bisync did not account
for the Fs's precision when comparing modtimes, meaning that they could only be
reliably compared within the same side -- not against the opposite side. Size
and checksum (even when available) were ignored completely for deltas.
After this change, bisync now fully supports comparing based on any combination
of size, modtime, and checksum, lifting the prior restriction on backends
without modtime support. The comparison logic considers the backend's
precision, hash types, and other features as appropriate.
The comparison features optionally use a new --compare flag (which takes any
combination of size,modtime,checksum) and even supports some combinations not
otherwise supported in `sync` (like comparing all three at the same time.) By
default (without the --compare flag), bisync inherits the same comparison
options as `sync` (that is: size and modtime by default, unless modified with
flags such as --checksum or --size-only.) If the --compare flag is set, it will
override these defaults.
If --compare includes checksum and both remotes support checksums but have no
hash types in common with each other, checksums will be considered only for
comparisons within the same side (to determine what has changed since the prior
sync), but not for comparisons against the opposite side. If one side supports
checksums and the other does not, checksums will only be considered on the side
that supports them. When comparing with checksum and/or size without modtime,
bisync cannot determine whether a file is newer or older -- only whether it is
changed or unchanged. (If it is changed on both sides, bisync still does the
standard equality-check to avoid declaring a sync conflict unless it absolutely
has to.)
Also included are some new flags to customize the checksum comparison behavior
on backends where hashes are slow or unavailable. --no-slow-hash and
--slow-hash-sync-only allow selectively ignoring checksums on backends such as
local where they are slow. --download-hash allows computing them by downloading
when (and only when) they're otherwise not available. Of course, this option
probably won't be practical with large files, but may be a good option for
syncing small-but-important files with maximum accuracy (for example, a source
code repo on a crypt remote.) An additional advantage over methods like
cryptcheck is that the original file is not required for comparison (for
example, --download-hash can be used to bisync two different crypt remotes with
different passwords.)
Additionally, all of the above are now considered during the final --check-sync
for much-improved accuracy (before this change, it only compared filenames!)
Many other details are explained in the included docs.
2023-12-01 08:44:38 +08:00
|
|
|
|
|
|
|
// prettyprint formats JSON for improved readability in debug logs
|
|
|
|
func prettyprint(in any, label string, level fs.LogLevel) {
|
|
|
|
inBytes, err := json.MarshalIndent(in, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
fs.Debugf(nil, "failed to marshal input: %v", err)
|
|
|
|
}
|
|
|
|
if level == fs.LogLevelDebug {
|
|
|
|
fs.Debugf(nil, "%s: \n%s\n", label, string(inBytes))
|
|
|
|
} else if level == fs.LogLevelInfo {
|
|
|
|
fs.Infof(nil, "%s: \n%s\n", label, string(inBytes))
|
|
|
|
}
|
|
|
|
}
|