sync.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Rufs - Remote Union File System
  3. *
  4. * Copyright 2017 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package term
  11. import (
  12. "fmt"
  13. "devt.de/krotik/common/bitutil"
  14. )
  15. /*
  16. cmdSync Make sure dst has the same files and directories as src.
  17. */
  18. func cmdSync(tt *TreeTerm, arg ...string) (string, error) {
  19. var res string
  20. lenArg := len(arg)
  21. err := fmt.Errorf("sync requires a source and a destination directory")
  22. if lenArg > 1 {
  23. src := tt.parsePathParam(arg[0])
  24. dst := tt.parsePathParam(arg[1])
  25. updFunc := func(op, srcFile, dstFile string, writtenBytes, totalBytes, currentFile, totalFiles int64) {
  26. if writtenBytes > 0 {
  27. tt.WriteStatus(fmt.Sprintf("%v (%v/%v) writing: %v -> %v %v / %v", op,
  28. currentFile, totalFiles, srcFile, dstFile,
  29. bitutil.ByteSizeString(writtenBytes, false),
  30. bitutil.ByteSizeString(totalBytes, false)))
  31. } else {
  32. tt.ClearStatus()
  33. fmt.Fprint(tt.out, fmt.Sprintln(fmt.Sprintf("%v (%v/%v) %v -> %v", op,
  34. currentFile, totalFiles, srcFile, dstFile)))
  35. }
  36. }
  37. if err = tt.tree.Sync(src, dst, true, updFunc); err == nil {
  38. res = "Done"
  39. }
  40. }
  41. return res, err
  42. }