Skip to content

Sync/Copy/Move: write list of transferred/deleted files #7282

Closed

Description

@nielash

This is a feature request for the ability to access a complete list of files transferred/deleted by Sync/Copy/Move, as well as their basic attributes and any errors encountered. This is similar to rclone check's ability to output lists for MissingOnDst, MissingOnSrc, etc.

While this would be a nice feature in general for many reasons, I am primarily interested because it's currently the blocking issue for Bisync's biggest bug.

Prior discussion (from #6797)

@nielash:

Just wanted to mention, in case it's related to the work you're planning here, that it would be really helpful for future bisync work to have an easy way to access a complete list of transfers. Similar to accounting.Stats(ctx).Transferred(), but that one is limited to MaxCompletedTransfers (100) and doesn't include modtime or hash. My hope is to use this to avoid having to re-list each fs at the end of a bisync run to find out what happened during the syncCopyMove operations, as re-listing is more expensive and error-prone.

If this maybe already exists and I'm missing it, please let me know 😃 I see how to access the total number of transfers/errors, but we would need a list to reconcile it with the initial directory listings to build the "after" snapshot. If keeping the full transfer list in memory is a concern, we could potentially work with just a list of errored transfers (and infer that anything not on this list was successful and now matches what we passed in.) But this is not entirely foolproof, as it leaves open the possibility that files changed in between the listing and copying.

@ncw:

That isn't quite what is under discussion here, but what I think you want is something like we have for operations.Check where you can pass in io.Writers to have the file names written to.

type CheckOpt struct {
	Fdst, Fsrc   fs.Fs     // fses to check
	Check        checkFn   // function to use for checking
	OneWay       bool      // one way only?
	Combined     io.Writer // a file with file names with leading sigils
	MissingOnSrc io.Writer // files only in the destination
	MissingOnDst io.Writer // files only in the source
	Match        io.Writer // matching files
	Differ       io.Writer // differing files
	Error        io.Writer // files with errors of some kind
}

If you read the docs for rclone check you'll see the bits about outputting file names to files. People have asked for exactly this for sync/copy/move and I think this internal inteface would suit your requirements too.

I should probably change the io.Writer into a func(remote string) error to explicitly say it will be called with a path each time. We'd need a way of signalling high level retries also as the functions potentially get called each high level retry.

@nielash:

Yes, this would be great! (I used this recently in 5ca61ab.) For bisync purposes I think we'd also need the size, modtime, and hash in addition to the list of names (at least to the extent that syncCopyMove already has this info -- and I think it does, as that's how it knows if a file is "corrupted on transfer".)

In addition to the above, it would also be important to know whether each operation was a copy or delete, and whether it had an error. The end goal is to update the initial listing, which uses this format:

// flags <- size -> <- hash -> id <------------ modtime -----------> "<----- remote"
// - 3009805 md5:xxxxxx - 2006-01-02T15:04:05.000000000-0700 "12 - Wait.mp3"
//
// flags: "-" for a file and "d" for a directory (reserved)
// hash: "type:value" or "-" (example: "md5:378840336ab14afa9c6b8d887e68a340")
// id: "-" (reserved)

See also:

How to use GitHub

  • Please use the 👍 reaction to show that you are affected by the same issue.
  • Please don't comment if you have no relevant information to add. It's just extra noise for everyone subscribed to this issue.
  • Subscribe to receive notifications on status change and new comments.

Activity

  1. nielash commented on Sep 9, 2023

    @nielash
    ContributorAuthor

    @ncw I started working on a proof-of-concept for this. If you have a moment, could you let me know if it's generally headed in the right direction? nielash@50bf6a6

  2. ncw commented on Sep 9, 2023

    @ncw
    Member

    I see you have instrumented Copy, Move, Delete so the fundamental operations. This won't get you all of the things here

    	Combined     io.Writer // a file with file names with leading sigils
    	MissingOnSrc io.Writer // files only in the destination
    	MissingOnDst io.Writer // files only in the source
    	Match        io.Writer // matching files
    	Differ       io.Writer // differing files
    	Error        io.Writer // files with errors of some kind
    

    I was thinking to instrument the sync.Sync routine where you know exactly what status each thing is. Check out how operations.Check is instrumented with c.report and c.reportFilename.

    I think there only needs to be one function with a signature like this

    func(sigil rune, src fs.Object, dst fs.Object)
    

    The sigils define exactly what is happening. These could of course be constants which might make the code easier to read.

    = 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
    

    You can then read filenames, modtimes or hashes from the objects. However you must only read them if you are going to need them as reading modtimes or hashes can both be expensive on some backends. If you aren't sure then just store the object until you do know.

    Rather than changing all the function prototypes for everything, the modern go way of passing a function through lots of layers would be to add it to the context.

    So you'd export a function from fs/sync to add a function pointer to the context, say sync.WithLogger and then you'd extract this logger function in sync.newSyncCopyMove and store it in the syncCopyMove structure if found or a no-op function if not so you can call it without checking it for nil. This will make the patch much smaller!

    type LoggerFn func(sigil rune, src fs.Object, dst fs.Object)
    type loggerContextKey struct{}
    var loggerKey = loggerContextKey{}
    func WithLogger(ctx context.Context, logger LoggerFn) context.Context {
        return context.WithValue(ctx, loggerKey, logger)
    }

    Then in the newSyncCopyMove something like this (though thinking ahead this should probably be in operations in operations.GetLogger(ctx) as should the WithLogger and definitions.

        logger, ok := ctx.Value(loggerKey).(LoggerFn)
        if ok {
            s.logger = logger
        } else {
            s.logger = func(sigil rune, src fs.Object, dst fs.Object) { }
        }

    Then to close the circle we can do the same thing in operations.Check and factor the duplicate code into operations.

  3. nielash commented on Sep 10, 2023

    @nielash
    ContributorAuthor

    Thanks so much! Excellent feedback. I will take another stab at it with this in mind.

    I think one challenge this approach will present for Bisync is that the sigils indicate the state before the transfers, not after. For example, * indicates that the file differed on src and dst, but that does not necessarily mean that sync updated the dst -- perhaps we did not attempt to due to --ignore-existing, or perhaps we did attempt to but there was an error. The sigils tell us "here's how the src and dst were different before my changes", whereas what Bisync really wants is "here's a complete list of every change I made on the dest". This could maybe be solved by adding an additional sigil, but I'm not exactly sure where in the code would be the cleanest place to call it from.

    You can then read filenames, modtimes or hashes from the objects. However you must only read them if you are going to need them as reading modtimes or hashes can both be expensive on some backends.

    Totally agree, and that's why I was hoping to record them while copy is already using them, to avoid having to request them twice. For example, we already print hashes to the log here:

    // Verify hashes are the same after transfer - ignoring blank hashes
    if hashType != hash.None {
    // checkHashes has logged and counted errors
    equal, _, srcSum, dstSum, _ := checkHashes(ctx, src, dst, hashType)
    if !equal {
    err = fmt.Errorf("corrupted on transfer: %v hash differ %q vs %q", hashType, srcSum, dstSum)
    fs.Errorf(dst, "%v", err)
    err = fs.CountError(err)
    removeFailedCopy(ctx, dst)
    return newDst, err
    }
    }

    But I realize this may not be possible in some cases -- ex. renames and server-side copies.

    Bisync does always need all modtimes, as it's currently the only way it can detect whether a file changed between bisync runs. If we do have to read modtimes/hashes a second time, what is crucial is that we do it during the sync, not after, to minimize the possibility of the file changing again before we have a chance to read it (for instance, in an hours-long sync). (This is why I'm pretty sure the code for this will have to live in sync or operations, as opposed to bisync, even if it will only be called during bisync.)

    Totally agree about passing it through the context -- much cleaner 🙂

  4. ncw commented on Sep 10, 2023

    @ncw
    Member

    Thanks so much! Excellent feedback. I will take another stab at it with this in mind.

    :-)

    I think one challenge this approach will present for Bisync is that the sigils indicate the state before the transfers, not after. For example, * indicates that the file differed on src and dst, but that does not necessarily mean that sync updated the dst -- perhaps we did not attempt to due to --ignore-existing, or perhaps we did attempt to but there was an error.

    That is a good point. The set of sigils was designed for rclone check which doesn't alter the state. Probably all the existing sigils should have an implied meaning for sync that sync successfully corrected the situation.

    Or maybe the logger should take an error too and if it is not nil we assume that the operation failed?

    Not quite sure how to report --ignore-existing - I'd be tempted to report = as ok and didn't need copying.

    The sigils tell us "here's how the src and dst were different before my changes", whereas what Bisync really wants is "here's a complete list of every change I made on the dest". This could maybe be solved by adding an additional sigil, but I'm not exactly sure where in the code would be the cleanest place to call it from.

    You'll see in the sync code that there is a log or a debug at all these terminal places. There are no logs in the Copy/Move section as Copy and Move themselves make the logs. This is so that every file makes one log.

    You can then read filenames, modtimes or hashes from the objects. However you must only read them if you are going to need them as reading modtimes or hashes can both be expensive on some backends.

    Totally agree, and that's why I was hoping to record them while copy is already using them, to avoid having to request them twice.

    Modtimes and hashes will be cached in the object so if they've been looked up once, then it is cheap to look them up again.

    Bisync does always need all modtimes, as it's currently the only way it can detect whether a file changed between bisync runs. If we do have to read modtimes/hashes a second time, what is crucial is that we do it during the sync, not after, to minimize the possibility of the file changing again before we have a chance to read it (for instance, in an hours-long sync). (This is why I'm pretty sure the code for this will have to live in sync or operations, as opposed to bisync, even if it will only be called during bisync.)

    Hopefully the caching will fix that.

    I can see it is going to be quite tricky getting the correct logging in to sync so I expect we'll have to collaborate on that.

  5. nielash commented on Sep 12, 2023

    @nielash
    ContributorAuthor

    New version based on your feedback: nielash@60fc9c7 Still very much a work-in-progress, but I think it's a lot closer to what you had in mind. I included an example of how rclone sync could use the same flags as rclone check and output the same file lists.

    I wrote a very basic test script to test whether rclone check and rclone sync output exactly the same file lists, and so far it seems that they do, in all the scenarios I tried. But more testing is needed. (Relatedly: is there a better way to do this kind of thing within the actual go testing framework? i.e. run a trusted command with CLI args from a different rclone package, as opposed to trying to recreate it with internal functions.)

    --error is not fully implemented yet. processError() seems like the obvious place to do it, but it doesn't have the fs.Objects in scope, so it might need to be one layer back.

    The Completed stream is also not done -- it's the trickiest part, as we discussed. I will probably need your help with it. I'm also coming around to the idea that it may not be totally necessary, since we can infer that the dst should always end up matching the src, unless there were errors. One concern is high-level retries, if the src could be changing during the course of a long sync. Does it refresh the src object each high-level retry or is the src cached the first time only? If the former, we may want to add some logic to de-dupe the stream in favor of the newest line for each path.

    Also, scratch what I said about the bisync code having to live elsewhere... after I wrote that, it occurred to me that I could pass in a custom LoggerFn 😊

  6. ncw commented on Sep 12, 2023

    @ncw
    Member

    New version based on your feedback: nielash@60fc9c7 Still very much a work-in-progress, but I think it's a lot closer to what you had in mind. I included an example of how rclone sync could use the same flags as rclone check and output the same file lists.

    Yes that is looking very neat now - I like it! I put a few more comments on it.

    I wrote a very basic test script to test whether rclone check and rclone sync output exactly the same file lists, and so far it seems that they do, in all the scenarios I tried. But more testing is needed.

    Very nice.

    (Relatedly: is there a better way to do this kind of thing within the actual go testing framework? i.e. run a trusted command with CLI args from a different rclone package, as opposed to trying to recreate it with internal functions.)

    Yes there is the https://pkg.go.dev/github.com/rogpeppe/[email protected]/testscript package which is for exactly this purpose.

    Though I'm not sure how you make sure you've built an rclone binary first. I guess it could be a separate step in the CI build process.

    Your test script could fairly easily be wrapped up into a separate script, though it might be better to add a logger to the operations and sync tests and check the outputs there were as expected - that would then give you more chance to track down the problems as the individual features get exercised.

    --error is not fully implemented yet. processError() seems like the obvious place to do it, but it doesn't have the fs.Objects in scope, so it might need to be one layer back.

    I think processError is probably too low level as lots of things can hit that (eg directory reading error).

    The Completed stream is also not done -- it's the trickiest part, as we discussed. I will probably need your help with it.

    I think completed probably has to be logged in the sync routine.

    I'm also coming around to the idea that it may not be totally necessary, since we can infer that the dst should always end up matching the src, unless there were errors.

    Yes that should be true!

    One concern is high-level retries, if the src could be changing during the course of a long sync. Does it refresh the src object each high-level retry

    Yes it does

    or is the src cached the first time only? If the former, we may want to add some logic to de-dupe the stream in favor of the newest line for each path.

    High level retries throw a bit of a spanner in the works here... If we were just logging to files what would you expect to see?

    Here are some ideas

    • Just log the first run through
    • Just log the last run through (rewind the stream maybe, though not going to work on stdout)
    • Log a # sigil meaning start of next try (would need to be written to all logs)
    • Attempt to deduplicate the logs
    • Force --retries 1 (this will be how bisync calls the code anyway as internal calls don't have retries).

    Deduplicating the logs (so trying to make them appear as if there was only one run) sounds attractive but I think will be very hard and might use up lots of memory.

    Also, scratch what I said about the bisync code having to live elsewhere... after I wrote that, it occurred to me that I could pass in a custom LoggerFn 😊

    :-)

  7. DarrenPIngram commented on Sep 15, 2023

    @DarrenPIngram

    I cannot add much to the programming side, but I am excited about this feature plan [even though I probably must script some hack to achieve similar over the weekend]. I would just like to highlight, if possible, it would be desirable for any output to be configured and parseable, so one could, for example, act on the transferred data file list with other scripting,

    e.g. /path/to/dog1.txt
    /path/to/dogstories/dog2.txt

    So you could add this to a list, a report, or other action of your choosing [outside of rclone].

  8. ncw commented on Sep 15, 2023

    @ncw
    Member

    @DarrenPIngram the plan is to make the output match that of rclone check which is very easy to parse.

  9. DarrenPIngram commented on Sep 15, 2023

    @DarrenPIngram
  10. 30 remaining items

  11. added 2 commits that reference this issue on Dec 8, 2023
    c5fe96b
    670c5e5
  12. added 2 commits that reference this issue on Jan 20, 2024
    c0968a0
    3a50f35
  13. moved this to Done in bisyncon Aug 20, 2024
  14. marvin-roesch commented on Sep 23, 2024

    @marvin-roesch
    Contributor

    I will happily create a follow-up issue for this, but I was wondering if there were any plans to also support this kind of log output for rclone copy as well. We have a use case where we want to only add onto a destination and perform deletes at a later date based on some additional external information. We would like to maintain a list of files that are on the source, however (and whether they needed to be transferred or not).

    Obviously deletes/--missing-on-src would not need to be supported in this case.

    We could implement this by listing everything in the source first and extracting the transfers from the rclone copy logs, but due to the number of files in our source, it would be much preferable to have this done in a single step. Considering the infrastructure for it already seems to be in place, maybe this is something that can easily be added to rclone copy?

  15. nielash commented on Sep 25, 2024

    @nielash
    ContributorAuthor

    I will happily create a follow-up issue for this, but I was wondering if there were any plans to also support this kind of log output for rclone copy as well.

    Indeed there were!

    // TODO: add same flags to move and copy

    And then we never got around to it, I guess. It would be pretty easy to implement though, as you could copy and paste a lot of the same code from here. Any chance you'd want to give it a go and submit a PR?

    In any case, yes -- please create a new issue for this so it stays on our radar!

  16. marvin-roesch commented on Oct 4, 2024

    @marvin-roesch
    Contributor

    Thanks for the response, @nielash! I have created #8115 and will have a look at implementing this myself to submit as a PR.

  17. added 2 commits that reference this issue on Jul 20, 2026
    9bc5f60
    4a16f91
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions