Implement Purger interface and drive.Purge

This commit is contained in:
Nick Craig-Wood 2013-01-18 17:01:47 +00:00
parent a0a789ff0f
commit 6e732f3dc0
4 changed files with 55 additions and 10 deletions

10
fs.go
View File

@ -31,10 +31,18 @@ type FsObject interface {
Size() int64 Size() int64
Open() (io.ReadCloser, error) Open() (io.ReadCloser, error)
Storable() bool Storable() bool
// Exists() bool
Remove() error Remove() error
} }
// Optional interfaces
type Purger interface {
// Purge all files in the root and the root directory
//
// Implement this if you have a way of deleting all the files
// quicker than just running Remove() on the result of List()
Purge() error
}
type FsObjectsChan chan FsObject type FsObjectsChan chan FsObject
type FsObjects []FsObject type FsObjects []FsObject

View File

@ -1,9 +1,6 @@
// Drive interface // Drive interface
package main package main
// FIXME could make a purge which deleted everything in the container
// easily - rmdir without the checks!
// FIXME list containers equivalent should list directories? // FIXME list containers equivalent should list directories?
// FIXME drive times only accurate to 1 ms (3 decimal places) // FIXME drive times only accurate to 1 ms (3 decimal places)
@ -505,9 +502,6 @@ func (f *FsDrive) Mkdir() error {
// //
// Returns an error if it isn't empty // Returns an error if it isn't empty
func (f *FsDrive) Rmdir() error { func (f *FsDrive) Rmdir() error {
if f.root == "" {
return fmt.Errorf("Can't delete root directory")
}
err := f.findRoot(false) err := f.findRoot(false)
if err != nil { if err != nil {
return err return err
@ -519,6 +513,27 @@ func (f *FsDrive) Rmdir() error {
if len(children.Items) > 0 { if len(children.Items) > 0 {
return fmt.Errorf("Directory not empty: %#v", children.Items) return fmt.Errorf("Directory not empty: %#v", children.Items)
} }
// Delete the directory if it isn't the root
if f.root != "" {
err = f.svc.Files.Delete(f.rootId).Do()
if err != nil {
return err
}
}
return nil
}
// Purge deletes all the files and the container
//
// Returns an error if it isn't empty
func (f *FsDrive) Purge() error {
if f.root == "" {
return fmt.Errorf("Can't purge root directory")
}
err := f.findRoot(false)
if err != nil {
return err
}
err = f.svc.Files.Delete(f.rootId).Do() err = f.svc.Files.Delete(f.rootId).Do()
if err != nil { if err != nil {
return err return err
@ -635,4 +650,5 @@ func (fs *FsObjectDrive) Remove() error {
// Check the interfaces are satisfied // Check the interfaces are satisfied
var _ Fs = &FsDrive{} var _ Fs = &FsDrive{}
var _ Purger = &FsDrive{}
var _ FsObject = &FsObjectDrive{} var _ FsObject = &FsObjectDrive{}

View File

@ -1,4 +1,5 @@
Todo Todo
* Make a test suite which can run on all the given types of fs
* Copy should use the sync code as it is more efficient at directory listing * Copy should use the sync code as it is more efficient at directory listing
* Drive needs a modify window of 1ms * Drive needs a modify window of 1ms
* Factor fses into own packages * Factor fses into own packages
@ -46,6 +47,7 @@ Ideas
* Google drive: https://developers.google.com/drive/ * Google drive: https://developers.google.com/drive/
* rsync over ssh * rsync over ssh
* dropbox: https://github.com/nickoneill/go-dropbox (no MD5s) * dropbox: https://github.com/nickoneill/go-dropbox (no MD5s)
* grive seems to have its secrets in the source code which would make things easier!
@ -58,3 +60,14 @@ s3
* https://forums.aws.amazon.com/message.jspa?messageID=214062 * https://forums.aws.amazon.com/message.jspa?messageID=214062
* Otherwise can set metadata * Otherwise can set metadata
* Returns etag and last modified in bucket list * Returns etag and last modified in bucket list
Bugs
local & drive need to delete directories
2013/01/18 16:31:32 Waiting for deletions to finish
2013/01/18 16:31:32 z3: FIXME Skipping directory
2013/01/18 16:31:32 z3/x: Deleted
2013/01/18 16:31:32 Deleting path
2013/01/18 16:31:32 Rmdir failed: remove z3: directory not empty

View File

@ -354,9 +354,17 @@ func rmdir(fdst, fsrc Fs) {
// //
// FIXME doesn't delete local directories // FIXME doesn't delete local directories
func purge(fdst, fsrc Fs) { func purge(fdst, fsrc Fs) {
DeleteFiles(fdst.List()) if f, ok := fdst.(Purger); ok {
log.Printf("Deleting path") err := f.Purge()
rmdir(fdst, fsrc) if err != nil {
stats.Error()
log.Fatalf("Purge failed: %s", err)
}
} else {
DeleteFiles(fdst.List())
log.Printf("Deleting path")
rmdir(fdst, fsrc)
}
} }
type Command struct { type Command struct {