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
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/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()
|
|
|
|
}
|
|
|
|
|
2019-07-18 12:13:54 +02:00
|
|
|
// merge adds items from another set
|
|
|
|
func (ss *stringSet) merge(m *stringSet) {
|
|
|
|
ss.mu.Lock()
|
|
|
|
m.mu.Lock()
|
|
|
|
for item := range m.items {
|
|
|
|
ss.items[item] = struct{}{}
|
|
|
|
}
|
|
|
|
m.mu.Unlock()
|
|
|
|
ss.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-05-02 18:01:39 +02:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2019-11-19 14:22:33 +01:00
|
|
|
// String returns string representation of set items excluding any in
|
|
|
|
// exclude (if set).
|
|
|
|
func (ss *stringSet) String(progress *inProgress, exclude *stringSet) string {
|
2018-05-02 18:01:39 +02:00
|
|
|
ss.mu.RLock()
|
|
|
|
defer ss.mu.RUnlock()
|
2019-07-18 12:13:54 +02:00
|
|
|
strngs := make([]string, 0, len(ss.items))
|
2018-05-02 18:01:39 +02:00
|
|
|
for name := range ss.items {
|
2019-11-19 14:22:33 +01:00
|
|
|
if exclude != nil {
|
|
|
|
exclude.mu.RLock()
|
|
|
|
_, found := exclude.items[name]
|
|
|
|
exclude.mu.RUnlock()
|
|
|
|
if found {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2018-02-01 14:13:24 +01:00
|
|
|
var out string
|
2019-07-18 12:13:54 +02:00
|
|
|
if acc := progress.get(name); acc != nil {
|
2018-02-01 14:13:24 +01:00
|
|
|
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
|
|
|
}
|
2019-07-18 12:13:54 +02:00
|
|
|
strngs = append(strngs, " * "+out)
|
2018-02-01 14:13:24 +01:00
|
|
|
}
|
2019-07-18 12:13:54 +02:00
|
|
|
sorted := sort.StringSlice(strngs)
|
2018-02-01 14:13:24 +01:00
|
|
|
sorted.Sort()
|
2019-07-18 12:13:54 +02:00
|
|
|
return strings.Join(sorted, "\n")
|
2018-02-01 14:13:24 +01:00
|
|
|
}
|
2018-08-28 12:17:05 +02:00
|
|
|
|
|
|
|
// progress returns total bytes read as well as the size.
|
2019-07-18 12:13:54 +02:00
|
|
|
func (ss *stringSet) progress(stats *StatsInfo) (totalBytes, totalSize int64) {
|
2018-08-28 12:17:05 +02:00
|
|
|
ss.mu.RLock()
|
|
|
|
defer ss.mu.RUnlock()
|
|
|
|
for name := range ss.items {
|
2019-07-18 12:13:54 +02:00
|
|
|
if acc := stats.inProgress.get(name); acc != nil {
|
2018-08-28 12:17:05 +02:00
|
|
|
bytes, size := acc.progress()
|
|
|
|
if size >= 0 && bytes >= 0 {
|
|
|
|
totalBytes += bytes
|
|
|
|
totalSize += size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return totalBytes, totalSize
|
|
|
|
}
|