2012-11-18 18:32:31 +01:00
|
|
|
// Sync files and directories to and from swift
|
|
|
|
//
|
|
|
|
// Nick Craig-Wood <nick@craig-wood.com>
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2012-11-28 12:17:31 +01:00
|
|
|
"crypto/md5"
|
2012-11-18 18:32:31 +01:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2012-11-20 23:40:48 +01:00
|
|
|
"github.com/ncw/swift"
|
2012-11-28 12:17:31 +01:00
|
|
|
"io"
|
2012-11-18 18:32:31 +01:00
|
|
|
"log"
|
|
|
|
"os"
|
2012-11-30 00:07:29 +01:00
|
|
|
"path"
|
2012-11-20 23:40:48 +01:00
|
|
|
"path/filepath"
|
2012-11-29 23:13:58 +01:00
|
|
|
"runtime"
|
2012-11-18 18:32:31 +01:00
|
|
|
"runtime/pprof"
|
2012-11-28 12:17:31 +01:00
|
|
|
"strings"
|
2012-11-29 23:13:58 +01:00
|
|
|
"sync"
|
2012-12-01 11:53:48 +01:00
|
|
|
"time"
|
2012-11-18 18:32:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Globals
|
|
|
|
var (
|
|
|
|
// Flags
|
|
|
|
cpuprofile = flag.String("cpuprofile", "", "Write cpu profile to file")
|
2012-12-24 01:03:18 +01:00
|
|
|
snet = flag.Bool("snet", false, "Use internal service network") // FIXME not implemented
|
|
|
|
verbose = flag.Bool("verbose", false, "Print lots more stuff")
|
|
|
|
quiet = flag.Bool("quiet", false, "Print as little stuff as possible")
|
2012-11-18 18:32:31 +01:00
|
|
|
// FIXME make these part of swift so we get a standard set of flags?
|
2012-11-29 23:13:58 +01:00
|
|
|
authUrl = flag.String("auth", os.Getenv("ST_AUTH"), "Auth URL for server. Defaults to environment var ST_AUTH.")
|
|
|
|
userName = flag.String("user", os.Getenv("ST_USER"), "User name. Defaults to environment var ST_USER.")
|
|
|
|
apiKey = flag.String("key", os.Getenv("ST_KEY"), "API key (password). Defaults to environment var ST_KEY.")
|
|
|
|
checkers = flag.Int("checkers", 8, "Number of checkers to run in parallel.")
|
2012-12-23 10:32:33 +01:00
|
|
|
transfers = flag.Int("transfers", 4, "Number of file transfers to run in parallel.")
|
2012-11-18 18:32:31 +01:00
|
|
|
)
|
|
|
|
|
2012-12-01 11:53:48 +01:00
|
|
|
// A filesystem like object which can either be a remote object or a
|
|
|
|
// local file/directory or both
|
2012-11-20 23:40:48 +01:00
|
|
|
type FsObject struct {
|
2012-12-01 11:53:48 +01:00
|
|
|
remote string // The remote path
|
|
|
|
path string // The local path
|
|
|
|
info os.FileInfo // Interface for file info
|
2012-11-20 23:40:48 +01:00
|
|
|
}
|
|
|
|
|
2012-11-29 23:13:58 +01:00
|
|
|
type FsObjectsChan chan *FsObject
|
|
|
|
|
2012-11-28 12:38:14 +01:00
|
|
|
type FsObjects []FsObject
|
2012-11-20 23:40:48 +01:00
|
|
|
|
2012-11-28 18:12:22 +01:00
|
|
|
// Write debuging output for this FsObject
|
2012-11-29 00:40:09 +01:00
|
|
|
func (fs *FsObject) Debugf(text string, args ...interface{}) {
|
2012-11-28 18:12:22 +01:00
|
|
|
out := fmt.Sprintf(text, args...)
|
2012-12-01 11:53:48 +01:00
|
|
|
log.Printf("%s: %s", fs.remote, out)
|
2012-11-28 18:12:22 +01:00
|
|
|
}
|
|
|
|
|
2012-11-28 12:17:31 +01:00
|
|
|
// md5sum calculates the md5sum of a file returning a lowercase hex string
|
2012-11-28 18:12:22 +01:00
|
|
|
func (fs *FsObject) md5sum() (string, error) {
|
|
|
|
in, err := os.Open(fs.path)
|
2012-11-28 12:17:31 +01:00
|
|
|
if err != nil {
|
2012-11-28 18:12:22 +01:00
|
|
|
fs.Debugf("Failed to open: %s", err)
|
2012-11-28 12:17:31 +01:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer in.Close() // FIXME ignoring error
|
|
|
|
hash := md5.New()
|
|
|
|
_, err = io.Copy(hash, in)
|
|
|
|
if err != nil {
|
2012-11-28 18:12:22 +01:00
|
|
|
fs.Debugf("Failed to read: %s", err)
|
2012-11-28 12:17:31 +01:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%x", hash.Sum(nil)), nil
|
|
|
|
}
|
|
|
|
|
2012-12-01 11:53:48 +01:00
|
|
|
// Sets the modification time of the local fs object
|
|
|
|
func (fs *FsObject) SetModTime(modTime time.Time) {
|
2012-12-01 13:05:17 +01:00
|
|
|
err := Chtimes(fs.path, modTime, modTime)
|
2012-12-01 11:53:48 +01:00
|
|
|
if err != nil {
|
|
|
|
fs.Debugf("Failed to set mtime on file: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks to see if the remote and local objects are equal by looking
|
|
|
|
// at size, mtime and MD5SUM
|
2012-11-27 23:28:46 +01:00
|
|
|
//
|
2012-12-01 11:53:48 +01:00
|
|
|
// If the remote and local size are different then it is considered to
|
|
|
|
// be not equal.
|
2012-11-28 12:17:31 +01:00
|
|
|
//
|
2012-11-28 18:24:00 +01:00
|
|
|
// If the size is the same and the mtime is the same then it is
|
2012-12-01 11:53:48 +01:00
|
|
|
// considered to be equal. This is the heuristic rsync uses when
|
2012-11-28 18:24:00 +01:00
|
|
|
// not using --checksum.
|
|
|
|
//
|
|
|
|
// If the size is the same and and mtime is different or unreadable
|
|
|
|
// and the MD5SUM is the same then the file is considered to be
|
2012-12-01 11:53:48 +01:00
|
|
|
// equal. In this case the mtime on the object is updated. If
|
|
|
|
// upload is set then the remote object is changed otherwise the local
|
|
|
|
// object.
|
2012-11-28 18:24:00 +01:00
|
|
|
//
|
2012-12-01 11:53:48 +01:00
|
|
|
// Otherwise the file is considered to be not equal including if there
|
|
|
|
// were errors reading info.
|
|
|
|
func (fs *FsObject) Equal(c *swift.Connection, container string, upload bool) bool {
|
|
|
|
// FIXME could pass in an Object here if we have one which
|
|
|
|
// will mean we could do the Size and Hash checks without a
|
|
|
|
// remote call if we wanted
|
|
|
|
obj, h, err := c.Object(container, fs.remote)
|
2012-11-27 23:28:46 +01:00
|
|
|
if err != nil {
|
2012-11-28 18:12:22 +01:00
|
|
|
fs.Debugf("Failed to read info: %s", err)
|
2012-12-01 11:53:48 +01:00
|
|
|
return false
|
2012-11-27 23:28:46 +01:00
|
|
|
}
|
|
|
|
if obj.Bytes != fs.info.Size() {
|
2012-11-28 18:12:22 +01:00
|
|
|
fs.Debugf("Sizes differ")
|
2012-12-01 11:53:48 +01:00
|
|
|
return false
|
2012-11-27 23:28:46 +01:00
|
|
|
}
|
2012-11-28 18:24:00 +01:00
|
|
|
|
|
|
|
// Size the same so check the mtime
|
2012-11-27 23:28:46 +01:00
|
|
|
m := h.ObjectMetadata()
|
2012-12-01 11:53:48 +01:00
|
|
|
remoteModTime, err := m.GetModTime()
|
2012-11-27 23:28:46 +01:00
|
|
|
if err != nil {
|
2012-11-28 18:12:22 +01:00
|
|
|
fs.Debugf("Failed to read mtime: %s", err)
|
2012-12-01 11:53:48 +01:00
|
|
|
} else if !remoteModTime.Equal(fs.info.ModTime()) {
|
2012-11-28 18:24:00 +01:00
|
|
|
fs.Debugf("Modification times differ")
|
|
|
|
} else {
|
2012-12-01 11:53:48 +01:00
|
|
|
fs.Debugf("Size and modification time the same")
|
|
|
|
return true
|
2012-11-29 00:40:09 +01:00
|
|
|
}
|
2012-11-28 18:24:00 +01:00
|
|
|
|
|
|
|
// mtime is unreadable or different but size is the same so
|
|
|
|
// check the MD5SUM
|
|
|
|
localMd5, err := fs.md5sum()
|
|
|
|
if err != nil {
|
|
|
|
fs.Debugf("Failed to calculate md5: %s", err)
|
2012-12-01 11:53:48 +01:00
|
|
|
return false
|
2012-11-27 23:28:46 +01:00
|
|
|
}
|
2012-11-28 18:24:00 +01:00
|
|
|
// fs.Debugf("Local MD5 %s", localMd5)
|
|
|
|
// fs.Debugf("Remote MD5 %s", obj.Hash)
|
|
|
|
if localMd5 != strings.ToLower(obj.Hash) {
|
|
|
|
fs.Debugf("Md5sums differ")
|
2012-12-01 11:53:48 +01:00
|
|
|
return false
|
2012-11-27 23:28:46 +01:00
|
|
|
}
|
2012-11-28 18:24:00 +01:00
|
|
|
|
2012-12-01 11:53:48 +01:00
|
|
|
// Size and MD5 the same but mtime different so update the
|
|
|
|
// mtime of the local or remote object here
|
|
|
|
if upload {
|
|
|
|
m.SetModTime(fs.info.ModTime())
|
|
|
|
err = c.ObjectUpdate(container, fs.remote, m.ObjectHeaders())
|
|
|
|
if err != nil {
|
|
|
|
fs.Debugf("Failed to update remote mtime: %s", err)
|
|
|
|
}
|
|
|
|
fs.Debugf("Updated mtime of remote object")
|
|
|
|
} else {
|
|
|
|
fmt.Printf("metadata %q, remoteModTime = %s\n", m, remoteModTime)
|
|
|
|
fs.SetModTime(remoteModTime)
|
|
|
|
fs.Debugf("Updated mtime of local object")
|
2012-11-28 18:42:12 +01:00
|
|
|
}
|
2012-11-29 00:40:09 +01:00
|
|
|
|
2012-12-01 11:53:48 +01:00
|
|
|
fs.Debugf("Size and MD5SUM of local and remote objects identical")
|
|
|
|
return true
|
2012-11-27 23:28:46 +01:00
|
|
|
}
|
|
|
|
|
2012-11-29 23:13:58 +01:00
|
|
|
// Is this object storable
|
2012-12-01 11:53:48 +01:00
|
|
|
func (fs *FsObject) storable() bool {
|
2012-11-20 23:40:48 +01:00
|
|
|
mode := fs.info.Mode()
|
|
|
|
if mode&(os.ModeSymlink|os.ModeNamedPipe|os.ModeSocket|os.ModeDevice) != 0 {
|
2012-11-28 18:12:22 +01:00
|
|
|
fs.Debugf("Can't transfer non file/directory")
|
2012-11-29 23:13:58 +01:00
|
|
|
return false
|
2012-11-20 23:40:48 +01:00
|
|
|
} else if mode&os.ModeDir != 0 {
|
|
|
|
// Debug?
|
2012-11-28 18:12:22 +01:00
|
|
|
fs.Debugf("FIXME Skipping directory")
|
2012-11-29 23:13:58 +01:00
|
|
|
return false
|
2012-11-20 23:40:48 +01:00
|
|
|
}
|
2012-11-29 23:13:58 +01:00
|
|
|
return true
|
|
|
|
}
|
2012-11-20 23:40:48 +01:00
|
|
|
|
2012-11-29 23:13:58 +01:00
|
|
|
// Puts the FsObject into the container
|
|
|
|
func (fs *FsObject) put(c *swift.Connection, container string) {
|
|
|
|
// FIXME content type
|
|
|
|
in, err := os.Open(fs.path)
|
|
|
|
if err != nil {
|
|
|
|
fs.Debugf("Failed to open: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
m := swift.Metadata{}
|
|
|
|
m.SetModTime(fs.info.ModTime())
|
2012-12-01 11:53:48 +01:00
|
|
|
_, err = c.ObjectPut(container, fs.remote, in, true, "", "", m.ObjectHeaders())
|
2012-11-29 23:13:58 +01:00
|
|
|
if err != nil {
|
|
|
|
fs.Debugf("Failed to upload: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fs.Debugf("Uploaded")
|
2012-11-20 23:40:48 +01:00
|
|
|
}
|
|
|
|
|
2012-12-23 10:32:33 +01:00
|
|
|
// Stat a FsObject into info
|
2012-12-01 11:53:48 +01:00
|
|
|
func (fs *FsObject) lstat() error {
|
|
|
|
info, err := os.Lstat(fs.path)
|
|
|
|
fs.info = info
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2012-11-30 00:07:29 +01:00
|
|
|
// Return an FsObject from a path
|
|
|
|
//
|
|
|
|
// May return nil if an error occurred
|
|
|
|
func NewFsObject(root, path string) *FsObject {
|
2012-12-01 11:53:48 +01:00
|
|
|
remote, err := filepath.Rel(root, path)
|
2012-11-30 00:07:29 +01:00
|
|
|
if err != nil {
|
2012-12-01 11:53:48 +01:00
|
|
|
log.Printf("Failed to get relative path %s: %s", path, err)
|
2012-11-30 00:07:29 +01:00
|
|
|
return nil
|
|
|
|
}
|
2012-12-01 11:53:48 +01:00
|
|
|
if remote == "." {
|
|
|
|
remote = ""
|
|
|
|
}
|
|
|
|
fs := &FsObject{remote: remote, path: path}
|
|
|
|
err = fs.lstat()
|
2012-11-30 00:07:29 +01:00
|
|
|
if err != nil {
|
2012-12-01 11:53:48 +01:00
|
|
|
log.Printf("Failed to stat %s: %s", path, err)
|
2012-11-30 00:07:29 +01:00
|
|
|
return nil
|
|
|
|
}
|
2012-12-01 11:53:48 +01:00
|
|
|
return fs
|
2012-11-30 00:07:29 +01:00
|
|
|
}
|
|
|
|
|
2012-11-29 23:13:58 +01:00
|
|
|
// Walk the path returning a channel of FsObjects
|
2012-11-20 23:40:48 +01:00
|
|
|
//
|
|
|
|
// FIXME ignore symlinks?
|
|
|
|
// FIXME what about hardlinks / etc
|
2012-11-29 23:13:58 +01:00
|
|
|
func walk(root string) FsObjectsChan {
|
|
|
|
out := make(FsObjectsChan, *checkers)
|
|
|
|
go func() {
|
|
|
|
err := filepath.Walk(root, func(path string, f os.FileInfo, err error) error {
|
2012-11-20 23:40:48 +01:00
|
|
|
if err != nil {
|
2012-11-29 23:13:58 +01:00
|
|
|
log.Printf("Failed to open directory: %s: %s", path, err)
|
|
|
|
} else {
|
2012-11-30 00:07:29 +01:00
|
|
|
if fs := NewFsObject(root, path); fs != nil {
|
|
|
|
out <- fs
|
2012-11-29 23:13:58 +01:00
|
|
|
}
|
2012-11-20 23:40:48 +01:00
|
|
|
}
|
2012-11-29 23:13:58 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to open directory: %s: %s", root, err)
|
2012-11-20 23:40:48 +01:00
|
|
|
}
|
2012-11-29 23:13:58 +01:00
|
|
|
close(out)
|
|
|
|
}()
|
|
|
|
return out
|
2012-11-20 23:40:48 +01:00
|
|
|
}
|
|
|
|
|
2012-11-29 23:13:58 +01:00
|
|
|
// Read FsObjects on in and write them to out if they need uploading
|
|
|
|
//
|
|
|
|
// FIXME potentially doing lots of MD5SUMS at once
|
2012-12-23 10:32:33 +01:00
|
|
|
func checker(c *swift.Connection, container string, in, out FsObjectsChan, upload bool, wg *sync.WaitGroup) {
|
2012-11-29 23:13:58 +01:00
|
|
|
defer wg.Done()
|
|
|
|
for fs := range in {
|
2012-12-23 10:32:33 +01:00
|
|
|
if !upload {
|
|
|
|
_ = fs.lstat()
|
|
|
|
if fs.info == nil {
|
|
|
|
fs.Debugf("Couldn't find local file - download")
|
|
|
|
out <- fs
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-29 23:13:58 +01:00
|
|
|
// Check to see if can store this
|
2012-12-01 11:53:48 +01:00
|
|
|
if !fs.storable() {
|
2012-11-29 23:13:58 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Check to see if changed or not
|
2012-12-23 10:32:33 +01:00
|
|
|
if fs.Equal(c, container, upload) {
|
2012-11-29 23:13:58 +01:00
|
|
|
fs.Debugf("Unchanged skipping")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
out <- fs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read FsObjects on in and upload them
|
|
|
|
func uploader(c *swift.Connection, container string, in FsObjectsChan, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
for fs := range in {
|
2012-11-20 23:40:48 +01:00
|
|
|
fs.put(c, container)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-29 23:13:58 +01:00
|
|
|
// Syncs a directory into a container
|
2012-12-04 00:58:17 +01:00
|
|
|
func upload(c *swift.Connection, args []string) {
|
|
|
|
root, container := args[0], args[1]
|
2012-12-04 01:09:22 +01:00
|
|
|
mkdir(c, []string{container})
|
2012-11-29 23:13:58 +01:00
|
|
|
to_be_checked := walk(root)
|
2012-12-23 10:32:33 +01:00
|
|
|
to_be_uploaded := make(FsObjectsChan, *transfers)
|
2012-11-29 23:13:58 +01:00
|
|
|
|
|
|
|
var checkerWg sync.WaitGroup
|
|
|
|
checkerWg.Add(*checkers)
|
|
|
|
for i := 0; i < *checkers; i++ {
|
2012-12-23 10:32:33 +01:00
|
|
|
go checker(c, container, to_be_checked, to_be_uploaded, true, &checkerWg)
|
2012-11-29 23:13:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var uploaderWg sync.WaitGroup
|
2012-12-23 10:32:33 +01:00
|
|
|
uploaderWg.Add(*transfers)
|
|
|
|
for i := 0; i < *transfers; i++ {
|
2012-11-29 23:13:58 +01:00
|
|
|
go uploader(c, container, to_be_uploaded, &uploaderWg)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Waiting for checks to finish")
|
|
|
|
checkerWg.Wait()
|
|
|
|
close(to_be_uploaded)
|
|
|
|
log.Printf("Waiting for uploads to finish")
|
|
|
|
uploaderWg.Wait()
|
|
|
|
}
|
|
|
|
|
2012-11-30 00:07:29 +01:00
|
|
|
// Get an object to the filepath making directories if necessary
|
2012-12-01 11:53:48 +01:00
|
|
|
func (fs *FsObject) get(c *swift.Connection, container string) {
|
|
|
|
log.Printf("Download %s to %s", fs.remote, fs.path)
|
2012-11-30 00:07:29 +01:00
|
|
|
|
2012-12-01 11:53:48 +01:00
|
|
|
dir := path.Dir(fs.path)
|
2012-11-30 00:07:29 +01:00
|
|
|
err := os.MkdirAll(dir, 0770)
|
|
|
|
if err != nil {
|
2012-12-01 11:53:48 +01:00
|
|
|
fs.Debugf("Couldn't make directory: %s", err)
|
2012-11-30 00:07:29 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-01 11:53:48 +01:00
|
|
|
out, err := os.Create(fs.path)
|
2012-11-30 00:07:29 +01:00
|
|
|
if err != nil {
|
2012-12-01 11:53:48 +01:00
|
|
|
fs.Debugf("Failed to open: %s", err)
|
2012-11-30 00:07:29 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-01 11:53:48 +01:00
|
|
|
h, getErr := c.ObjectGet(container, fs.remote, out, true, nil)
|
2012-12-01 09:18:58 +01:00
|
|
|
if getErr != nil {
|
2012-12-01 11:53:48 +01:00
|
|
|
fs.Debugf("Failed to download: %s", getErr)
|
2012-11-30 00:07:29 +01:00
|
|
|
}
|
|
|
|
|
2012-12-01 09:18:58 +01:00
|
|
|
closeErr := out.Close()
|
|
|
|
if closeErr != nil {
|
2012-12-01 11:53:48 +01:00
|
|
|
fs.Debugf("Error closing: %s", closeErr)
|
2012-11-30 00:07:29 +01:00
|
|
|
}
|
|
|
|
|
2012-12-01 09:18:58 +01:00
|
|
|
if getErr != nil || closeErr != nil {
|
2012-12-01 11:53:48 +01:00
|
|
|
fs.Debugf("Removing failed download")
|
|
|
|
err = os.Remove(fs.path)
|
2012-11-30 00:07:29 +01:00
|
|
|
if err != nil {
|
2012-12-01 11:53:48 +01:00
|
|
|
fs.Debugf("Failed to remove failed download: %s", err)
|
2012-11-30 00:07:29 +01:00
|
|
|
}
|
2012-12-01 09:18:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the mtime
|
|
|
|
modTime, err := h.ObjectMetadata().GetModTime()
|
|
|
|
if err != nil {
|
2012-12-01 11:53:48 +01:00
|
|
|
fs.Debugf("Failed to read mtime from object: %s", err)
|
2012-12-01 09:18:58 +01:00
|
|
|
} else {
|
2012-12-01 11:53:48 +01:00
|
|
|
fs.SetModTime(modTime)
|
2012-11-30 00:07:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-23 10:32:33 +01:00
|
|
|
// Read FsObjects on in and download them
|
|
|
|
func downloader(c *swift.Connection, container string, in FsObjectsChan, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
for fs := range in {
|
|
|
|
fs.get(c, container)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-30 00:07:29 +01:00
|
|
|
// Syncs a container into a directory
|
|
|
|
//
|
|
|
|
// FIXME need optional stat in FsObject and to be able to make FsObjects from ObjectsAll
|
2012-12-04 01:09:22 +01:00
|
|
|
//
|
|
|
|
// FIXME should download and stat many at once
|
2012-12-04 00:58:17 +01:00
|
|
|
func download(c *swift.Connection, args []string) {
|
|
|
|
container, root := args[0], args[1]
|
2012-11-30 00:07:29 +01:00
|
|
|
// FIXME this would be nice running into a channel!
|
|
|
|
objects, err := c.ObjectsAll(container, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't read container %q: %s", container, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.MkdirAll(root, 0770)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't make directory %q: %s", root, err)
|
|
|
|
}
|
|
|
|
|
2012-12-23 10:32:33 +01:00
|
|
|
to_be_checked := make(FsObjectsChan, *checkers)
|
|
|
|
to_be_downloaded := make(FsObjectsChan, *transfers)
|
|
|
|
|
|
|
|
var checkerWg sync.WaitGroup
|
|
|
|
checkerWg.Add(*checkers)
|
|
|
|
for i := 0; i < *checkers; i++ {
|
|
|
|
go checker(c, container, to_be_checked, to_be_downloaded, false, &checkerWg)
|
|
|
|
}
|
|
|
|
|
|
|
|
var downloaderWg sync.WaitGroup
|
|
|
|
downloaderWg.Add(*transfers)
|
|
|
|
for i := 0; i < *transfers; i++ {
|
|
|
|
go downloader(c, container, to_be_downloaded, &downloaderWg)
|
|
|
|
}
|
|
|
|
|
2012-11-30 00:07:29 +01:00
|
|
|
for i := range objects {
|
|
|
|
object := &objects[i]
|
|
|
|
filepath := path.Join(root, object.Name)
|
2012-12-23 10:32:33 +01:00
|
|
|
to_be_checked <- &FsObject{remote: object.Name, path: filepath}
|
2012-11-30 00:07:29 +01:00
|
|
|
}
|
2012-12-23 10:32:33 +01:00
|
|
|
close(to_be_checked)
|
|
|
|
|
|
|
|
log.Printf("Waiting for checks to finish")
|
|
|
|
checkerWg.Wait()
|
|
|
|
close(to_be_downloaded)
|
|
|
|
log.Printf("Waiting for downloads to finish")
|
|
|
|
downloaderWg.Wait()
|
|
|
|
|
2012-11-30 00:07:29 +01:00
|
|
|
}
|
|
|
|
|
2012-11-20 23:40:48 +01:00
|
|
|
// Lists the containers
|
|
|
|
func listContainers(c *swift.Connection) {
|
2012-11-29 00:40:09 +01:00
|
|
|
containers, err := c.ContainersAll(nil)
|
2012-11-20 23:40:48 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't list containers: %s", err)
|
|
|
|
}
|
|
|
|
for _, container := range containers {
|
|
|
|
fmt.Printf("%9d %12d %s\n", container.Count, container.Bytes, container.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lists files in a container
|
2012-12-04 00:58:17 +01:00
|
|
|
func list(c *swift.Connection, args []string) {
|
|
|
|
if len(args) == 0 {
|
|
|
|
listContainers(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
container := args[0]
|
2012-11-29 00:40:09 +01:00
|
|
|
//objects, err := c.ObjectsAll(container, &swift.ObjectsOpts{Prefix: "", Delimiter: '/'})
|
|
|
|
objects, err := c.ObjectsAll(container, nil)
|
2012-11-20 23:40:48 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't read container %q: %s", container, err)
|
|
|
|
}
|
|
|
|
for _, object := range objects {
|
|
|
|
if object.PseudoDirectory {
|
|
|
|
fmt.Printf("%9s %19s %s\n", "Directory", "-", object.Name)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%9d %19s %s\n", object.Bytes, object.LastModified.Format("2006-01-02 15:04:05"), object.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Makes a container
|
2012-12-04 00:58:17 +01:00
|
|
|
func mkdir(c *swift.Connection, args []string) {
|
|
|
|
container := args[0]
|
2012-11-20 23:40:48 +01:00
|
|
|
err := c.ContainerCreate(container, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't create container %q: %s", container, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes a container
|
2012-12-04 00:58:17 +01:00
|
|
|
func rmdir(c *swift.Connection, args []string) {
|
|
|
|
container := args[0]
|
2012-11-20 23:40:48 +01:00
|
|
|
err := c.ContainerDelete(container)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't delete container %q: %s", container, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-04 01:09:22 +01:00
|
|
|
// Removes a container and all of its contents
|
|
|
|
//
|
|
|
|
// FIXME should make FsObjects and use debugging
|
|
|
|
func purge(c *swift.Connection, args []string) {
|
|
|
|
container := args[0]
|
|
|
|
objects, err := c.ObjectsAll(container, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't read container %q: %s", container, err)
|
|
|
|
}
|
|
|
|
|
2012-12-23 10:49:49 +01:00
|
|
|
to_be_deleted := make(chan *swift.Object, *transfers)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(*transfers)
|
|
|
|
for i := 0; i < *transfers; i++ {
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for object := range to_be_deleted {
|
|
|
|
err := c.ObjectDelete(container, object.Name)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("%s: Couldn't delete: %s\n", object.Name, err)
|
|
|
|
} else {
|
|
|
|
log.Printf("%s: Deleted\n", object.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2012-12-04 01:09:22 +01:00
|
|
|
for i := range objects {
|
2012-12-23 10:49:49 +01:00
|
|
|
to_be_deleted <- &objects[i]
|
2012-12-04 01:09:22 +01:00
|
|
|
}
|
2012-12-23 10:49:49 +01:00
|
|
|
close(to_be_deleted)
|
|
|
|
|
|
|
|
log.Printf("Waiting for deletions to finish")
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
log.Printf("Deleting container")
|
2012-12-04 01:09:22 +01:00
|
|
|
rmdir(c, args)
|
|
|
|
}
|
|
|
|
|
2012-12-04 00:58:17 +01:00
|
|
|
type Command struct {
|
|
|
|
name string
|
|
|
|
help string
|
|
|
|
run func(*swift.Connection, []string)
|
|
|
|
minArgs, maxArgs int
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkArgs checks there are enough arguments and prints a message if not
|
|
|
|
func (cmd *Command) checkArgs(args []string) {
|
|
|
|
if len(args) < cmd.minArgs {
|
|
|
|
syntaxError()
|
|
|
|
fmt.Fprintf(os.Stderr, "Command %s needs %d arguments mininum\n", cmd.name, cmd.minArgs)
|
|
|
|
os.Exit(1)
|
|
|
|
} else if len(args) > cmd.maxArgs {
|
|
|
|
syntaxError()
|
|
|
|
fmt.Fprintf(os.Stderr, "Command %s needs %d arguments maximum\n", cmd.name, cmd.maxArgs)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var Commands = []Command{
|
|
|
|
{
|
|
|
|
"upload",
|
|
|
|
`<directory> <container>
|
|
|
|
Upload the local directory to the remote container. Doesn't
|
|
|
|
upload unchanged files, testing first by modification time
|
|
|
|
then by MD5SUM
|
|
|
|
`,
|
|
|
|
upload,
|
|
|
|
2, 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"download",
|
|
|
|
`<container> <directory>
|
|
|
|
Download the container to the local directory. Doesn't
|
|
|
|
download unchanged files
|
|
|
|
`,
|
|
|
|
download,
|
|
|
|
2, 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ls",
|
|
|
|
`[<container>]
|
|
|
|
List the containers if no parameter supplied or the contents
|
|
|
|
of <container>
|
|
|
|
`,
|
|
|
|
list,
|
|
|
|
0, 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"mkdir",
|
|
|
|
`<container>
|
|
|
|
Make the container if it doesn't already exist
|
|
|
|
`,
|
|
|
|
mkdir,
|
|
|
|
1, 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"rmdir",
|
|
|
|
`<container>
|
|
|
|
Remove the container. Note that you can't remove a container with
|
|
|
|
objects in - use rm for that
|
|
|
|
`,
|
|
|
|
rmdir,
|
|
|
|
1, 1,
|
|
|
|
},
|
2012-12-04 01:09:22 +01:00
|
|
|
{
|
|
|
|
"purge",
|
|
|
|
`<container>
|
|
|
|
Remove the container and all of the contents.
|
|
|
|
`,
|
|
|
|
purge,
|
|
|
|
1, 1,
|
|
|
|
},
|
2012-12-04 00:58:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// syntaxError prints the syntax
|
|
|
|
func syntaxError() {
|
|
|
|
fmt.Fprintf(os.Stderr, `Sync files and directories to and from swift
|
|
|
|
|
|
|
|
Syntax: [options] subcommand <parameters> <parameters...>
|
|
|
|
|
|
|
|
Subcommands:
|
|
|
|
`)
|
|
|
|
for i := range Commands {
|
|
|
|
cmd := &Commands[i]
|
|
|
|
fmt.Fprintf(os.Stderr, " %s: %s\n", cmd.name, cmd.help)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "Options:\n")
|
|
|
|
flag.PrintDefaults()
|
|
|
|
fmt.Fprintf(os.Stderr, `
|
|
|
|
It is only necessary to use a unique prefix of the subcommand, eg 'up' for 'upload'.
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit with the message
|
|
|
|
func fatal(message string, args ...interface{}) {
|
|
|
|
syntaxError()
|
|
|
|
fmt.Fprintf(os.Stderr, message, args...)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2012-11-18 18:32:31 +01:00
|
|
|
func main() {
|
|
|
|
flag.Usage = syntaxError
|
|
|
|
flag.Parse()
|
2012-11-20 23:40:48 +01:00
|
|
|
args := flag.Args()
|
2012-11-29 23:13:58 +01:00
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
2012-11-18 18:32:31 +01:00
|
|
|
|
|
|
|
// Setup profiling if desired
|
|
|
|
if *cpuprofile != "" {
|
|
|
|
f, err := os.Create(*cpuprofile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
}
|
|
|
|
|
2012-11-20 23:40:48 +01:00
|
|
|
fmt.Println(args)
|
|
|
|
if len(args) < 1 {
|
|
|
|
fatal("No command supplied\n")
|
|
|
|
}
|
2012-11-18 18:32:31 +01:00
|
|
|
|
|
|
|
if *userName == "" {
|
2012-11-20 23:40:48 +01:00
|
|
|
log.Fatal("Need -user or environmental variable ST_USER")
|
2012-11-18 18:32:31 +01:00
|
|
|
}
|
|
|
|
if *apiKey == "" {
|
2012-11-20 23:40:48 +01:00
|
|
|
log.Fatal("Need -key or environmental variable ST_KEY")
|
2012-11-18 18:32:31 +01:00
|
|
|
}
|
|
|
|
if *authUrl == "" {
|
2012-11-20 23:40:48 +01:00
|
|
|
log.Fatal("Need -auth or environmental variable ST_AUTH")
|
2012-11-18 18:32:31 +01:00
|
|
|
}
|
2012-11-20 23:40:48 +01:00
|
|
|
c := &swift.Connection{
|
2012-11-18 18:32:31 +01:00
|
|
|
UserName: *userName,
|
|
|
|
ApiKey: *apiKey,
|
|
|
|
AuthUrl: *authUrl,
|
|
|
|
}
|
|
|
|
err := c.Authenticate()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to authenticate", err)
|
|
|
|
}
|
|
|
|
|
2012-12-04 00:58:17 +01:00
|
|
|
cmd := strings.ToLower(args[0])
|
2012-11-20 23:40:48 +01:00
|
|
|
args = args[1:]
|
|
|
|
|
2012-12-04 00:58:17 +01:00
|
|
|
// Find the command doing a prefix match
|
|
|
|
var found *Command
|
|
|
|
for i := range Commands {
|
|
|
|
command := &Commands[i]
|
|
|
|
// exact command name found - use that
|
|
|
|
if command.name == cmd {
|
|
|
|
found = command
|
|
|
|
break
|
|
|
|
} else if strings.HasPrefix(command.name, cmd) {
|
|
|
|
if found != nil {
|
|
|
|
log.Fatalf("Not unique - matches multiple commands %q", cmd)
|
|
|
|
}
|
|
|
|
found = command
|
2012-11-20 23:40:48 +01:00
|
|
|
}
|
|
|
|
}
|
2012-12-04 00:58:17 +01:00
|
|
|
if found == nil {
|
|
|
|
log.Fatalf("Unknown command %q", cmd)
|
|
|
|
}
|
|
|
|
found.checkArgs(args)
|
|
|
|
found.run(c, args)
|
2012-11-18 18:32:31 +01:00
|
|
|
}
|