2022-08-28 19:21:57 +08:00
|
|
|
// Package check provides the check command.
|
2016-08-06 00:12:27 +08:00
|
|
|
package check
|
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2021-07-07 23:34:16 +08:00
|
|
|
"fmt"
|
2020-03-09 18:54:41 +08:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2019-06-17 16:34:30 +08:00
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/cmd"
|
2020-03-09 18:54:41 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
2019-10-11 23:55:04 +08:00
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2021-05-18 23:20:17 +08:00
|
|
|
"github.com/rclone/rclone/fs/hash"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2016-08-06 00:12:27 +08:00
|
|
|
"github.com/spf13/cobra"
|
2020-03-09 18:54:41 +08:00
|
|
|
"github.com/spf13/pflag"
|
2016-08-06 00:12:27 +08:00
|
|
|
)
|
|
|
|
|
2017-02-13 18:48:26 +08:00
|
|
|
// Globals
|
|
|
|
var (
|
2021-07-07 23:34:16 +08:00
|
|
|
download = false
|
|
|
|
oneway = false
|
|
|
|
combined = ""
|
|
|
|
missingOnSrc = ""
|
|
|
|
missingOnDst = ""
|
|
|
|
match = ""
|
|
|
|
differ = ""
|
|
|
|
errFile = ""
|
|
|
|
checkFileHashType = ""
|
2017-02-13 18:48:26 +08:00
|
|
|
)
|
|
|
|
|
2016-08-06 00:12:27 +08:00
|
|
|
func init() {
|
2019-10-11 23:58:11 +08:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2019-10-11 23:55:04 +08:00
|
|
|
cmdFlags := commandDefinition.Flags()
|
2023-07-11 01:34:10 +08:00
|
|
|
flags.BoolVarP(cmdFlags, &download, "download", "", download, "Check by downloading rather than with hash", "")
|
|
|
|
flags.StringVarP(cmdFlags, &checkFileHashType, "checkfile", "C", checkFileHashType, "Treat source:path as a SUM file with hashes of given type", "")
|
2020-03-09 18:54:41 +08:00
|
|
|
AddFlags(cmdFlags)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddFlags adds the check flags to the cmdFlags command
|
|
|
|
func AddFlags(cmdFlags *pflag.FlagSet) {
|
2023-07-11 01:34:10 +08:00
|
|
|
flags.BoolVarP(cmdFlags, &oneway, "one-way", "", oneway, "Check one way only, source files must exist on remote", "")
|
|
|
|
flags.StringVarP(cmdFlags, &combined, "combined", "", combined, "Make a combined report of changes to this file", "")
|
|
|
|
flags.StringVarP(cmdFlags, &missingOnSrc, "missing-on-src", "", missingOnSrc, "Report all files missing from the source to this file", "")
|
|
|
|
flags.StringVarP(cmdFlags, &missingOnDst, "missing-on-dst", "", missingOnDst, "Report all files missing from the destination to this file", "")
|
|
|
|
flags.StringVarP(cmdFlags, &match, "match", "", match, "Report all matching files to this file", "")
|
|
|
|
flags.StringVarP(cmdFlags, &differ, "differ", "", differ, "Report all non-matching files to this file", "")
|
|
|
|
flags.StringVarP(cmdFlags, &errFile, "error", "", errFile, "Report all files with errors (hashing or reading) to this file", "")
|
2020-03-09 18:54:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FlagsHelp describes the flags for the help
|
2020-05-25 15:50:20 +08:00
|
|
|
// Warning! "|" will be replaced by backticks below
|
|
|
|
var FlagsHelp = strings.ReplaceAll(`
|
2020-03-09 18:54:41 +08:00
|
|
|
If you supply the |--one-way| flag, it will only check that files in
|
|
|
|
the source match the files in the destination, not the other way
|
|
|
|
around. This means that extra files in the destination that are not in
|
|
|
|
the source will not be detected.
|
|
|
|
|
2020-09-07 19:10:52 +08:00
|
|
|
The |--differ|, |--missing-on-dst|, |--missing-on-src|, |--match|
|
2020-03-09 18:54:41 +08:00
|
|
|
and |--error| flags write paths, one per line, to the file name (or
|
|
|
|
stdout if it is |-|) supplied. What they write is described in the
|
|
|
|
help below. For example |--differ| will write all paths which are
|
|
|
|
present on both the source and destination but different.
|
|
|
|
|
|
|
|
The |--combined| flag will write a file (or stdout) which contains all
|
|
|
|
file paths with a symbol and then a space and then the path to tell
|
|
|
|
you what happened to it. These are reminiscent of diff files.
|
|
|
|
|
|
|
|
- |= path| means path was found in source and destination and was identical
|
|
|
|
- |- path| means path was missing on the source, so only in the destination
|
|
|
|
- |+ path| means path was missing on the destination, so only in the source
|
|
|
|
- |* path| means path was present in source and destination but different.
|
|
|
|
- |! path| means there was an error reading or hashing the source or dest.
|
2023-03-24 20:43:45 +08:00
|
|
|
|
|
|
|
The default number of parallel checks is 8. See the [--checkers=N](/docs/#checkers-n)
|
|
|
|
option for more information.
|
2020-05-25 15:50:20 +08:00
|
|
|
`, "|", "`")
|
2020-03-09 18:54:41 +08:00
|
|
|
|
|
|
|
// GetCheckOpt gets the options corresponding to the check flags
|
|
|
|
func GetCheckOpt(fsrc, fdst fs.Fs) (opt *operations.CheckOpt, close func(), err error) {
|
|
|
|
closers := []io.Closer{}
|
|
|
|
|
|
|
|
opt = &operations.CheckOpt{
|
|
|
|
Fsrc: fsrc,
|
|
|
|
Fdst: fdst,
|
|
|
|
OneWay: oneway,
|
|
|
|
}
|
|
|
|
|
|
|
|
open := func(name string, pout *io.Writer) error {
|
|
|
|
if name == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if name == "-" {
|
|
|
|
*pout = os.Stdout
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
out, err := os.Create(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*pout = out
|
|
|
|
closers = append(closers, out)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = open(combined, &opt.Combined); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err = open(missingOnSrc, &opt.MissingOnSrc); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err = open(missingOnDst, &opt.MissingOnDst); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err = open(match, &opt.Match); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err = open(differ, &opt.Differ); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err = open(errFile, &opt.Error); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
close = func() {
|
|
|
|
for _, closer := range closers {
|
|
|
|
err := closer.Close()
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Failed to close report output: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return opt, close, nil
|
2016-08-06 00:12:27 +08:00
|
|
|
}
|
|
|
|
|
2019-10-11 23:58:11 +08:00
|
|
|
var commandDefinition = &cobra.Command{
|
2016-08-06 00:12:27 +08:00
|
|
|
Use: "check source:path dest:path",
|
|
|
|
Short: `Checks the files in the source and destination match.`,
|
2020-05-25 15:50:20 +08:00
|
|
|
Long: strings.ReplaceAll(`
|
2017-02-13 18:48:26 +08:00
|
|
|
Checks the files in the source and destination match. It compares
|
2021-11-04 19:50:43 +08:00
|
|
|
sizes and hashes (MD5 or SHA1) and logs a report of files that don't
|
2017-02-13 18:48:26 +08:00
|
|
|
match. It doesn't alter the source or destination.
|
|
|
|
|
2022-06-19 21:51:37 +08:00
|
|
|
For the [crypt](/crypt/) remote there is a dedicated command,
|
|
|
|
[cryptcheck](/commands/rclone_cryptcheck/), that are able to check
|
2022-09-20 15:02:27 +08:00
|
|
|
the checksums of the encrypted files.
|
2022-06-19 21:51:37 +08:00
|
|
|
|
2020-05-25 15:50:20 +08:00
|
|
|
If you supply the |--size-only| flag, it will only compare the sizes not
|
2017-02-13 18:48:26 +08:00
|
|
|
the hashes as well. Use this for a quick check.
|
2016-08-06 00:12:27 +08:00
|
|
|
|
2020-05-25 15:50:20 +08:00
|
|
|
If you supply the |--download| flag, it will download the data from
|
2017-02-13 18:48:26 +08:00
|
|
|
both remotes and check them against each other on the fly. This can
|
|
|
|
be useful for remotes that don't support hashes or if you really want
|
|
|
|
to check all the data.
|
2021-07-07 23:34:16 +08:00
|
|
|
|
|
|
|
If you supply the |--checkfile HASH| flag with a valid hash name,
|
|
|
|
the |source:path| must point to a text file in the SUM format.
|
2020-05-25 15:50:20 +08:00
|
|
|
`, "|", "`") + FlagsHelp,
|
2023-07-11 01:34:10 +08:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"groups": "Filter,Listing,Check",
|
|
|
|
},
|
2021-07-07 23:34:16 +08:00
|
|
|
RunE: func(command *cobra.Command, args []string) error {
|
2016-08-06 00:12:27 +08:00
|
|
|
cmd.CheckArgs(2, 2, command, args)
|
2021-07-07 23:34:16 +08:00
|
|
|
var (
|
|
|
|
fsrc, fdst fs.Fs
|
|
|
|
hashType hash.Type
|
|
|
|
fsum fs.Fs
|
|
|
|
sumFile string
|
|
|
|
)
|
|
|
|
if checkFileHashType != "" {
|
|
|
|
if err := hashType.Set(checkFileHashType); err != nil {
|
|
|
|
fmt.Println(hash.HelpString(0))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fsum, sumFile, fsrc = cmd.NewFsSrcFileDst(args)
|
|
|
|
} else {
|
|
|
|
fsrc, fdst = cmd.NewFsSrcDst(args)
|
|
|
|
}
|
|
|
|
|
2020-06-24 18:23:34 +08:00
|
|
|
cmd.Run(false, true, command, func() error {
|
2020-03-09 18:54:41 +08:00
|
|
|
opt, close, err := GetCheckOpt(fsrc, fdst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer close()
|
2021-07-07 23:34:16 +08:00
|
|
|
|
|
|
|
if checkFileHashType != "" {
|
|
|
|
return operations.CheckSum(context.Background(), fsrc, fsum, sumFile, hashType, opt, download)
|
|
|
|
}
|
|
|
|
|
2017-02-13 18:48:26 +08:00
|
|
|
if download {
|
2020-03-09 18:54:41 +08:00
|
|
|
return operations.CheckDownload(context.Background(), opt)
|
2017-02-13 18:48:26 +08:00
|
|
|
}
|
2021-05-18 23:20:17 +08:00
|
|
|
hashType := fsrc.Hashes().Overlap(fdst.Hashes()).GetOne()
|
|
|
|
if hashType == hash.None {
|
|
|
|
fs.Errorf(nil, "No common hash found - not using a hash for checks")
|
|
|
|
} else {
|
|
|
|
fs.Infof(nil, "Using %v for hash comparisons", hashType)
|
|
|
|
}
|
2020-03-09 18:54:41 +08:00
|
|
|
return operations.Check(context.Background(), opt)
|
2016-08-06 00:12:27 +08:00
|
|
|
})
|
2021-07-07 23:34:16 +08:00
|
|
|
return nil
|
2016-08-06 00:12:27 +08:00
|
|
|
},
|
|
|
|
}
|