b2: cleanup old file versions - fixes #462

This commit is contained in:
Nick Craig-Wood 2016-07-02 17:03:08 +01:00
parent 0a43ff9c13
commit 018fe80bcb
2 changed files with 38 additions and 11 deletions

View File

@ -749,10 +749,12 @@ func (f *Fs) deleteByID(ID, Name string) error {
return nil
}
// Purge deletes all the files and directories
// purge deletes all the files and directories
//
// if oldOnly is true then it deletes only non current files.
//
// Implemented here so we can make sure we delete old versions.
func (f *Fs) Purge() error {
func (f *Fs) purge(oldOnly bool) error {
var errReturn error
var checkErrMutex sync.Mutex
var checkErr = func(err error) {
@ -774,24 +776,46 @@ func (f *Fs) Purge() error {
go func() {
defer wg.Done()
for object := range toBeDeleted {
fs.Stats.Transferring(object.Name)
checkErr(f.deleteByID(object.ID, object.Name))
fs.Stats.DoneTransferring(object.Name)
}
}()
}
last := ""
checkErr(f.list("", fs.MaxLevel, "", 0, true, func(remote string, object *api.File, isDirectory bool) error {
if !isDirectory {
fs.Stats.Checking(remote)
if oldOnly && last != remote {
fs.Debug(remote, "Not deleting current version (id %q) %q", object.ID, object.Action)
} else {
fs.Debug(remote, "Deleting (id %q)", object.ID)
toBeDeleted <- object
}
last = remote
fs.Stats.DoneChecking(remote)
}
return nil
}))
close(toBeDeleted)
wg.Wait()
if !oldOnly {
checkErr(f.Rmdir())
}
return errReturn
}
// Purge deletes all the files and directories including the old versions.
func (f *Fs) Purge() error {
return f.purge(false)
}
// CleanUp deletes all the hidden files.
func (f *Fs) CleanUp() error {
return f.purge(true)
}
// Hashes returns the supported hash sets.
func (f *Fs) Hashes() fs.HashSet {
return fs.HashSet(fs.HashSHA1)
@ -1259,5 +1283,6 @@ func (o *Object) Remove() error {
var (
_ fs.Fs = &Fs{}
_ fs.Purger = &Fs{}
_ fs.CleanUpper = &Fs{}
_ fs.Object = &Object{}
)

View File

@ -119,9 +119,11 @@ available.
The old versions of files are visible in the B2 web interface, but not
via rclone yet.
Rclone doesn't provide any way of managing old versions (downloading
them or deleting them) at the moment. When you `purge` a bucket, all
the old versions will be deleted.
If you wish to remove all the old versions then you can use the
`rclone cleanup remote:bucket` command which will delete all the old
versions of files, leaving the current ones intact.
When you `purge` a bucket, all the old versions will be deleted also.
### Transfers ###