2018-02-01 14:13:24 +01:00
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
2019-01-14 17:12:39 +01:00
|
|
|
"fmt"
|
2018-02-01 14:13:24 +01:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2018-05-02 18:01:39 +02:00
|
|
|
"sync"
|
2019-01-14 17:12:39 +01:00
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
2018-02-01 14:13:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// stringSet holds a set of strings
|
2018-05-02 18:01:39 +02:00
|
|
|
type stringSet struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
items map[string]struct{}
|
2019-01-14 17:12:39 +01:00
|
|
|
name string
|
2018-05-02 18:01:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// newStringSet creates a new empty string set of capacity size
|
2019-01-14 17:12:39 +01:00
|
|
|
func newStringSet(size int, name string) *stringSet {
|
2018-05-02 18:01:39 +02:00
|
|
|
return &stringSet{
|
|
|
|
items: make(map[string]struct{}, size),
|
2019-01-14 17:12:39 +01:00
|
|
|
name: name,
|
2018-05-02 18:01:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add adds remote to the set
|
|
|
|
func (ss *stringSet) add(remote string) {
|
|
|
|
ss.mu.Lock()
|
|
|
|
ss.items[remote] = struct{}{}
|
|
|
|
ss.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// del removes remote from the set
|
|
|
|
func (ss *stringSet) del(remote string) {
|
|
|
|
ss.mu.Lock()
|
|
|
|
delete(ss.items, remote)
|
|
|
|
ss.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// empty returns whether the set has any items
|
|
|
|
func (ss *stringSet) empty() bool {
|
|
|
|
ss.mu.RLock()
|
|
|
|
defer ss.mu.RUnlock()
|
|
|
|
return len(ss.items) == 0
|
|
|
|
}
|
2018-02-01 14:13:24 +01:00
|
|
|
|
2018-08-28 12:17:05 +02:00
|
|
|
// count returns the number of items in the set
|
|
|
|
func (ss *stringSet) count() int {
|
|
|
|
ss.mu.RLock()
|
|
|
|
defer ss.mu.RUnlock()
|
|
|
|
return len(ss.items)
|
|
|
|
}
|
|
|
|
|
2018-02-01 14:13:24 +01:00
|
|
|
// Strings returns all the strings in the stringSet
|
2018-05-02 18:01:39 +02:00
|
|
|
func (ss *stringSet) Strings() []string {
|
|
|
|
ss.mu.RLock()
|
|
|
|
defer ss.mu.RUnlock()
|
|
|
|
strings := make([]string, 0, len(ss.items))
|
|
|
|
for name := range ss.items {
|
2018-02-01 14:13:24 +01:00
|
|
|
var out string
|
|
|
|
if acc := Stats.inProgress.get(name); acc != nil {
|
|
|
|
out = acc.String()
|
|
|
|
} else {
|
2019-01-14 17:12:39 +01:00
|
|
|
out = fmt.Sprintf("%*s: %s",
|
|
|
|
fs.Config.StatsFileNameLength,
|
|
|
|
shortenName(name, fs.Config.StatsFileNameLength),
|
|
|
|
ss.name,
|
|
|
|
)
|
2018-02-01 14:13:24 +01:00
|
|
|
}
|
|
|
|
strings = append(strings, " * "+out)
|
|
|
|
}
|
|
|
|
sorted := sort.StringSlice(strings)
|
|
|
|
sorted.Sort()
|
|
|
|
return sorted
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns all the file names in the stringSet joined by newline
|
2018-05-02 18:01:39 +02:00
|
|
|
func (ss *stringSet) String() string {
|
2018-02-01 14:13:24 +01:00
|
|
|
return strings.Join(ss.Strings(), "\n")
|
|
|
|
}
|
2018-08-28 12:17:05 +02:00
|
|
|
|
|
|
|
// progress returns total bytes read as well as the size.
|
|
|
|
func (ss *stringSet) progress() (totalBytes, totalSize int64) {
|
|
|
|
ss.mu.RLock()
|
|
|
|
defer ss.mu.RUnlock()
|
|
|
|
for name := range ss.items {
|
|
|
|
if acc := Stats.inProgress.get(name); acc != nil {
|
|
|
|
bytes, size := acc.progress()
|
|
|
|
if size >= 0 && bytes >= 0 {
|
|
|
|
totalBytes += bytes
|
|
|
|
totalSize += size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return totalBytes, totalSize
|
|
|
|
}
|