2013-01-15 00:38:18 +01:00
|
|
|
// Drive interface
|
2013-06-27 21:13:07 +02:00
|
|
|
package drive
|
2013-01-15 00:38:18 +01:00
|
|
|
|
|
|
|
// FIXME need to deal with some corner cases
|
|
|
|
// * multiple files with the same name
|
|
|
|
// * files can be in multiple directories
|
|
|
|
// * can have directory loops
|
2013-01-20 12:56:56 +01:00
|
|
|
// * files with / in name
|
2013-01-15 00:38:18 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2014-12-12 21:02:08 +01:00
|
|
|
"google.golang.org/api/drive/v2"
|
2015-02-02 18:29:08 +01:00
|
|
|
"google.golang.org/api/googleapi"
|
2014-12-12 21:02:08 +01:00
|
|
|
|
2014-03-15 17:06:11 +01:00
|
|
|
"github.com/ncw/rclone/fs"
|
2014-07-13 18:53:11 +02:00
|
|
|
"github.com/ncw/rclone/googleauth"
|
2014-03-27 17:55:29 +01:00
|
|
|
"github.com/ogier/pflag"
|
2014-03-15 17:06:11 +01:00
|
|
|
)
|
2013-06-29 13:15:31 +02:00
|
|
|
|
2014-03-16 15:01:17 +01:00
|
|
|
// Constants
|
|
|
|
const (
|
|
|
|
rcloneClientId = "202264815644.apps.googleusercontent.com"
|
|
|
|
rcloneClientSecret = "X4Z3ca8xfWDb1Voo-F9a7ZxJ"
|
|
|
|
driveFolderType = "application/vnd.google-apps.folder"
|
2014-07-29 18:50:07 +02:00
|
|
|
timeFormatIn = time.RFC3339
|
|
|
|
timeFormatOut = "2006-01-02T15:04:05.000000000Z07:00"
|
2015-02-02 18:29:08 +01:00
|
|
|
minSleep = 10 * time.Millisecond
|
|
|
|
maxSleep = 2 * time.Second
|
|
|
|
decayConstant = 2 // bigger for slower decay, exponential
|
2014-03-16 15:01:17 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Globals
|
|
|
|
var (
|
|
|
|
// Flags
|
2014-03-27 17:55:29 +01:00
|
|
|
driveFullList = pflag.BoolP("drive-full-list", "", true, "Use a full listing for directory list. More data but usually quicker.")
|
2014-07-13 18:53:11 +02:00
|
|
|
// Description of how to auth for this app
|
|
|
|
driveAuth = &googleauth.Auth{
|
|
|
|
Scope: "https://www.googleapis.com/auth/drive",
|
|
|
|
DefaultClientId: rcloneClientId,
|
|
|
|
DefaultClientSecret: rcloneClientSecret,
|
|
|
|
}
|
2014-03-16 15:01:17 +01:00
|
|
|
)
|
|
|
|
|
2013-06-29 13:15:31 +02:00
|
|
|
// Register with Fs
|
|
|
|
func init() {
|
2014-03-15 17:06:11 +01:00
|
|
|
fs.Register(&fs.FsInfo{
|
2014-07-13 18:53:11 +02:00
|
|
|
Name: "drive",
|
|
|
|
NewFs: NewFs,
|
|
|
|
Config: func(name string) {
|
|
|
|
driveAuth.Config(name)
|
|
|
|
},
|
2014-03-15 17:06:11 +01:00
|
|
|
Options: []fs.Option{{
|
|
|
|
Name: "client_id",
|
2014-03-16 15:01:17 +01:00
|
|
|
Help: "Google Application Client Id - leave blank to use rclone's.",
|
2014-03-15 17:06:11 +01:00
|
|
|
}, {
|
|
|
|
Name: "client_secret",
|
2014-03-16 15:01:17 +01:00
|
|
|
Help: "Google Application Client Secret - leave blank to use rclone's.",
|
2014-03-15 17:06:11 +01:00
|
|
|
}},
|
|
|
|
})
|
2013-06-29 13:15:31 +02:00
|
|
|
}
|
|
|
|
|
2013-01-15 00:38:18 +01:00
|
|
|
// FsDrive represents a remote drive server
|
|
|
|
type FsDrive struct {
|
2014-05-05 20:52:52 +02:00
|
|
|
svc *drive.Service // the connection to the drive server
|
|
|
|
root string // the path we are working on
|
|
|
|
client *http.Client // authorized client
|
|
|
|
about *drive.About // information about the drive, including the root
|
|
|
|
rootId string // Id of the root directory
|
|
|
|
foundRoot bool // Whether we have found the root or not
|
|
|
|
findRootLock sync.Mutex // Protect findRoot from concurrent use
|
2015-02-28 16:35:54 +01:00
|
|
|
dirCache *dirCache // Map of directory path to directory id
|
2014-05-05 20:52:52 +02:00
|
|
|
findDirLock sync.Mutex // Protect findDir from concurrent use
|
2015-02-02 18:29:08 +01:00
|
|
|
pacer chan struct{} // To pace the operations
|
|
|
|
sleepTime time.Duration // Time to sleep for each transaction
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// FsObjectDrive describes a drive object
|
|
|
|
type FsObjectDrive struct {
|
2013-01-19 11:11:55 +01:00
|
|
|
drive *FsDrive // what this object is part of
|
|
|
|
remote string // The remote path
|
|
|
|
id string // Drive Id of this object
|
|
|
|
url string // Download URL of this object
|
|
|
|
md5sum string // md5sum of the object
|
|
|
|
bytes int64 // size of the object
|
|
|
|
modifiedDate string // RFC3339 time it was last modified
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
|
|
|
|
2013-01-23 22:19:26 +01:00
|
|
|
// dirCache caches paths to directory Ids and vice versa
|
|
|
|
type dirCache struct {
|
2013-01-15 00:38:18 +01:00
|
|
|
sync.RWMutex
|
2013-01-23 22:19:26 +01:00
|
|
|
cache map[string]string
|
|
|
|
invCache map[string]string
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make a new locked map
|
2015-02-28 16:35:54 +01:00
|
|
|
func newDirCache() *dirCache {
|
|
|
|
d := &dirCache{}
|
2013-01-23 22:19:26 +01:00
|
|
|
d.Flush()
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets an Id given a path
|
|
|
|
func (m *dirCache) Get(path string) (id string, ok bool) {
|
|
|
|
m.RLock()
|
|
|
|
id, ok = m.cache[path]
|
|
|
|
m.RUnlock()
|
|
|
|
return
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
|
|
|
|
2013-01-23 22:19:26 +01:00
|
|
|
// GetInv gets a path given an Id
|
|
|
|
func (m *dirCache) GetInv(path string) (id string, ok bool) {
|
2013-01-15 00:38:18 +01:00
|
|
|
m.RLock()
|
2013-01-23 22:19:26 +01:00
|
|
|
id, ok = m.invCache[path]
|
2013-01-15 00:38:18 +01:00
|
|
|
m.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-01-23 22:19:26 +01:00
|
|
|
// Put a path, id into the map
|
|
|
|
func (m *dirCache) Put(path, id string) {
|
2013-01-15 00:38:18 +01:00
|
|
|
m.Lock()
|
2013-01-23 22:19:26 +01:00
|
|
|
m.cache[path] = id
|
|
|
|
m.invCache[id] = path
|
2013-01-15 00:38:18 +01:00
|
|
|
m.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush the map of all data
|
2013-01-23 22:19:26 +01:00
|
|
|
func (m *dirCache) Flush() {
|
2013-01-15 00:38:18 +01:00
|
|
|
m.Lock()
|
|
|
|
m.cache = make(map[string]string)
|
2013-01-23 22:19:26 +01:00
|
|
|
m.invCache = make(map[string]string)
|
2013-01-15 00:38:18 +01:00
|
|
|
m.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
// String converts this FsDrive to a string
|
|
|
|
func (f *FsDrive) String() string {
|
|
|
|
return fmt.Sprintf("Google drive root '%s'", f.root)
|
|
|
|
}
|
|
|
|
|
2015-02-19 20:12:23 +01:00
|
|
|
// Start a call to the drive API
|
|
|
|
//
|
|
|
|
// This must be called as a pair with endCall
|
|
|
|
//
|
|
|
|
// This waits for the pacer token
|
|
|
|
func (f *FsDrive) beginCall() {
|
2015-02-02 18:29:08 +01:00
|
|
|
// pacer starts with a token in and whenever we take one out
|
|
|
|
// XXX ms later we put another in. We could do this with a
|
|
|
|
// Ticker more accurately, but then we'd have to work out how
|
|
|
|
// not to run it when it wasn't needed
|
|
|
|
<-f.pacer
|
|
|
|
|
|
|
|
// Restart the timer
|
|
|
|
go func(t time.Duration) {
|
|
|
|
// fs.Debug(f, "New sleep for %v at %v", t, time.Now())
|
|
|
|
time.Sleep(t)
|
|
|
|
f.pacer <- struct{}{}
|
|
|
|
}(f.sleepTime)
|
|
|
|
}
|
|
|
|
|
2015-02-19 20:12:23 +01:00
|
|
|
// End a call to the drive API
|
|
|
|
//
|
2015-02-02 18:29:08 +01:00
|
|
|
// Refresh the pace given an error that was returned. It returns a
|
|
|
|
// boolean as to whether the operation should be retried.
|
|
|
|
//
|
|
|
|
// See https://developers.google.com/drive/web/handle-errors
|
|
|
|
// http://stackoverflow.com/questions/18529524/403-rate-limit-after-only-1-insert-per-second
|
2015-02-19 20:12:23 +01:00
|
|
|
func (f *FsDrive) endCall(err error) bool {
|
2015-02-02 18:29:08 +01:00
|
|
|
again := false
|
|
|
|
oldSleepTime := f.sleepTime
|
|
|
|
if err == nil {
|
|
|
|
f.sleepTime = (f.sleepTime<<decayConstant - f.sleepTime) >> decayConstant
|
|
|
|
if f.sleepTime < minSleep {
|
|
|
|
f.sleepTime = minSleep
|
|
|
|
}
|
|
|
|
if f.sleepTime != oldSleepTime {
|
|
|
|
fs.Debug(f, "Reducing sleep to %v", f.sleepTime)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fs.Debug(f, "Error recived: %v", err)
|
|
|
|
if gerr, ok := err.(*googleapi.Error); ok {
|
|
|
|
if len(gerr.Errors) > 0 {
|
|
|
|
reason := gerr.Errors[0].Reason
|
|
|
|
if reason == "rateLimitExceeded" || reason == "userRateLimitExceeded" {
|
|
|
|
f.sleepTime *= 2
|
|
|
|
if f.sleepTime > maxSleep {
|
|
|
|
f.sleepTime = maxSleep
|
|
|
|
}
|
|
|
|
if f.sleepTime != oldSleepTime {
|
|
|
|
fs.Debug(f, "Rate limited, increasing sleep to %v", f.sleepTime)
|
|
|
|
}
|
|
|
|
again = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return again
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pace the remote operations to not exceed Google's limits and retry
|
|
|
|
// on 403 rate limit exceeded
|
|
|
|
//
|
|
|
|
// This calls fn, expecting it to place its error in perr
|
2015-02-19 20:12:23 +01:00
|
|
|
func (f *FsDrive) call(perr *error, fn func()) {
|
2015-02-02 18:29:08 +01:00
|
|
|
for {
|
2015-02-19 20:12:23 +01:00
|
|
|
f.beginCall()
|
2015-02-02 18:29:08 +01:00
|
|
|
fn()
|
2015-02-19 20:12:23 +01:00
|
|
|
if !f.endCall(*perr) {
|
2015-02-02 18:29:08 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-15 00:38:18 +01:00
|
|
|
// parseParse parses a drive 'url'
|
|
|
|
func parseDrivePath(path string) (root string, err error) {
|
2014-03-27 18:49:36 +01:00
|
|
|
root = strings.Trim(path, "/")
|
2013-01-15 00:38:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-01-20 12:56:56 +01:00
|
|
|
// User function to process a File item from listAll
|
|
|
|
//
|
|
|
|
// Should return true to finish processing
|
|
|
|
type listAllFn func(*drive.File) bool
|
|
|
|
|
|
|
|
// Lists the directory required calling the user function on each item found
|
|
|
|
//
|
|
|
|
// If the user fn ever returns true then it early exits with found = true
|
2013-01-15 00:38:18 +01:00
|
|
|
//
|
|
|
|
// Search params: https://developers.google.com/drive/search-parameters
|
2013-01-20 12:56:56 +01:00
|
|
|
func (f *FsDrive) listAll(dirId string, title string, directoriesOnly bool, filesOnly bool, fn listAllFn) (found bool, err error) {
|
2013-01-23 22:19:26 +01:00
|
|
|
query := fmt.Sprintf("trashed=false")
|
|
|
|
if dirId != "" {
|
|
|
|
query += fmt.Sprintf(" and '%s' in parents", dirId)
|
|
|
|
}
|
2013-01-15 00:38:18 +01:00
|
|
|
if title != "" {
|
|
|
|
// Escaping the backslash isn't documented but seems to work
|
|
|
|
title = strings.Replace(title, `\`, `\\`, -1)
|
|
|
|
title = strings.Replace(title, `'`, `\'`, -1)
|
|
|
|
query += fmt.Sprintf(" and title='%s'", title)
|
|
|
|
}
|
|
|
|
if directoriesOnly {
|
|
|
|
query += fmt.Sprintf(" and mimeType='%s'", driveFolderType)
|
|
|
|
}
|
|
|
|
if filesOnly {
|
|
|
|
query += fmt.Sprintf(" and mimeType!='%s'", driveFolderType)
|
|
|
|
}
|
2013-01-23 22:19:26 +01:00
|
|
|
// fmt.Printf("listAll Query = %q\n", query)
|
|
|
|
list := f.svc.Files.List().Q(query).MaxResults(1000)
|
2013-01-20 12:56:56 +01:00
|
|
|
OUTER:
|
2013-01-15 00:38:18 +01:00
|
|
|
for {
|
2015-02-02 18:29:08 +01:00
|
|
|
var files *drive.FileList
|
2015-02-19 20:12:23 +01:00
|
|
|
f.call(&err, func() {
|
2015-02-02 18:29:08 +01:00
|
|
|
files, err = list.Do()
|
|
|
|
})
|
2013-01-15 00:38:18 +01:00
|
|
|
if err != nil {
|
2013-01-20 12:56:56 +01:00
|
|
|
return false, fmt.Errorf("Couldn't list directory: %s", err)
|
|
|
|
}
|
|
|
|
for _, item := range files.Items {
|
|
|
|
if fn(item) {
|
|
|
|
found = true
|
|
|
|
break OUTER
|
|
|
|
}
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
|
|
|
if files.NextPageToken == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
list.PageToken(files.NextPageToken)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-16 15:01:17 +01:00
|
|
|
// NewFs contstructs an FsDrive from the path, container:path
|
|
|
|
func NewFs(name, path string) (fs.Fs, error) {
|
2014-07-13 18:53:11 +02:00
|
|
|
t, err := driveAuth.NewTransport(name)
|
2013-01-15 00:38:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-03-16 15:01:17 +01:00
|
|
|
root, err := parseDrivePath(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
2014-07-13 18:53:11 +02:00
|
|
|
|
2014-05-05 20:52:52 +02:00
|
|
|
f := &FsDrive{
|
2015-02-02 18:29:08 +01:00
|
|
|
root: root,
|
|
|
|
dirCache: newDirCache(),
|
|
|
|
pacer: make(chan struct{}, 1),
|
|
|
|
sleepTime: minSleep,
|
2014-05-05 20:52:52 +02:00
|
|
|
}
|
2013-01-15 00:38:18 +01:00
|
|
|
|
2015-02-02 18:29:08 +01:00
|
|
|
// Put the first pacing token in
|
|
|
|
f.pacer <- struct{}{}
|
|
|
|
|
2013-01-15 00:38:18 +01:00
|
|
|
// Create a new authorized Drive client.
|
|
|
|
f.client = t.Client()
|
|
|
|
f.svc, err = drive.New(f.client)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Couldn't create Drive client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read About so we know the root path
|
2015-02-19 20:12:23 +01:00
|
|
|
f.call(&err, func() {
|
2015-02-02 18:29:08 +01:00
|
|
|
f.about, err = f.svc.About.Get().Do()
|
|
|
|
})
|
2013-01-15 00:38:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Couldn't read info about Drive: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-07-29 00:34:05 +02:00
|
|
|
// Find the Id of the true root and clear everything
|
|
|
|
f.resetRoot()
|
2014-05-05 20:52:52 +02:00
|
|
|
// Find the current root
|
|
|
|
err = f.findRoot(false)
|
|
|
|
if err != nil {
|
|
|
|
// Assume it is a file
|
|
|
|
newRoot, remote := splitPath(root)
|
|
|
|
newF := *f
|
|
|
|
newF.root = newRoot
|
|
|
|
// Make new Fs which is the parent
|
|
|
|
err = newF.findRoot(false)
|
|
|
|
if err != nil {
|
|
|
|
// No root so return old f
|
|
|
|
return f, nil
|
|
|
|
}
|
2014-07-29 18:50:07 +02:00
|
|
|
obj, err := newF.newFsObjectWithInfoErr(remote, nil)
|
2014-05-05 20:52:52 +02:00
|
|
|
if err != nil {
|
|
|
|
// File doesn't exist so return old f
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
// return a Fs Limited to this object
|
|
|
|
return fs.NewLimited(&newF, obj), nil
|
|
|
|
}
|
2013-01-23 22:19:26 +01:00
|
|
|
// fmt.Printf("Root id %s", f.rootId)
|
2013-01-15 00:38:18 +01:00
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an FsObject from a path
|
2014-07-29 18:50:07 +02:00
|
|
|
func (f *FsDrive) newFsObjectWithInfoErr(remote string, info *drive.File) (fs.Object, error) {
|
2013-01-15 00:38:18 +01:00
|
|
|
fs := &FsObjectDrive{
|
|
|
|
drive: f,
|
|
|
|
remote: remote,
|
|
|
|
}
|
|
|
|
if info != nil {
|
2013-01-19 11:11:55 +01:00
|
|
|
fs.setMetaData(info)
|
2013-01-15 00:38:18 +01:00
|
|
|
} else {
|
|
|
|
err := fs.readMetaData() // reads info and meta, returning an error
|
|
|
|
if err != nil {
|
2013-06-28 09:57:32 +02:00
|
|
|
// logged already fs.Debug("Failed to read info: %s", err)
|
2014-05-05 20:52:52 +02:00
|
|
|
return nil, err
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
|
|
|
}
|
2014-05-05 20:52:52 +02:00
|
|
|
return fs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an FsObject from a path
|
|
|
|
//
|
|
|
|
// May return nil if an error occurred
|
2014-07-29 18:50:07 +02:00
|
|
|
func (f *FsDrive) newFsObjectWithInfo(remote string, info *drive.File) fs.Object {
|
|
|
|
fs, _ := f.newFsObjectWithInfoErr(remote, info)
|
2014-05-05 20:52:52 +02:00
|
|
|
// Errors have already been logged
|
2013-01-15 00:38:18 +01:00
|
|
|
return fs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an FsObject from a path
|
|
|
|
//
|
|
|
|
// May return nil if an error occurred
|
2013-06-28 09:57:32 +02:00
|
|
|
func (f *FsDrive) NewFsObject(remote string) fs.Object {
|
2014-07-29 18:50:07 +02:00
|
|
|
return f.newFsObjectWithInfo(remote, nil)
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Path should be directory path either "" or "path/"
|
2013-01-23 22:19:26 +01:00
|
|
|
//
|
|
|
|
// List the directory using a recursive list from the root
|
|
|
|
//
|
|
|
|
// This fetches the minimum amount of stuff but does more API calls
|
|
|
|
// which makes it slow
|
2013-06-28 09:57:32 +02:00
|
|
|
func (f *FsDrive) listDirRecursive(dirId string, path string, out fs.ObjectsChan) error {
|
2013-01-20 12:56:56 +01:00
|
|
|
var subError error
|
2013-01-15 00:38:18 +01:00
|
|
|
// Make the API request
|
2013-01-20 12:56:56 +01:00
|
|
|
_, err := f.listAll(dirId, "", false, false, func(item *drive.File) bool {
|
2013-01-15 00:38:18 +01:00
|
|
|
// Recurse on directories
|
|
|
|
// FIXME should do this in parallel
|
|
|
|
// use a wg to sync then collect error
|
|
|
|
if item.MimeType == driveFolderType {
|
2013-01-23 22:19:26 +01:00
|
|
|
subError = f.listDirRecursive(item.Id, path+item.Title+"/", out)
|
2013-01-20 12:56:56 +01:00
|
|
|
if subError != nil {
|
|
|
|
return true
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If item has no MD5 sum it isn't stored on drive, so ignore it
|
2013-01-20 12:56:56 +01:00
|
|
|
if item.Md5Checksum != "" {
|
2014-07-29 18:50:07 +02:00
|
|
|
if fs := f.newFsObjectWithInfo(path+item.Title, item); fs != nil {
|
2013-01-20 12:56:56 +01:00
|
|
|
out <- fs
|
|
|
|
}
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
|
|
|
}
|
2013-01-20 12:56:56 +01:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if subError != nil {
|
|
|
|
return subError
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-01-23 22:19:26 +01:00
|
|
|
// Path should be directory path either "" or "path/"
|
|
|
|
//
|
|
|
|
// List the directory using a full listing and filtering out unwanted
|
|
|
|
// items
|
|
|
|
//
|
|
|
|
// This is fast in terms of number of API calls, but slow in terms of
|
|
|
|
// fetching more data than it needs
|
2013-06-28 09:57:32 +02:00
|
|
|
func (f *FsDrive) listDirFull(dirId string, path string, out fs.ObjectsChan) error {
|
2013-01-23 22:19:26 +01:00
|
|
|
// Orphans waiting for their parent
|
|
|
|
orphans := make(map[string][]*drive.File)
|
|
|
|
|
|
|
|
var outputItem func(*drive.File, string) // forward def for recursive fn
|
|
|
|
|
|
|
|
// Output an item or directory
|
|
|
|
outputItem = func(item *drive.File, directory string) {
|
|
|
|
// fmt.Printf("found %q %q parent %q dir %q ok %s\n", item.Title, item.Id, parentId, directory, ok)
|
|
|
|
path := item.Title
|
|
|
|
if directory != "" {
|
|
|
|
path = directory + "/" + path
|
|
|
|
}
|
|
|
|
if item.MimeType == driveFolderType {
|
|
|
|
// Put the directory into the dircache
|
|
|
|
f.dirCache.Put(path, item.Id)
|
|
|
|
// fmt.Printf("directory %s %s %s\n", path, item.Title, item.Id)
|
|
|
|
// Collect the orphans if any
|
|
|
|
for _, orphan := range orphans[item.Id] {
|
|
|
|
// fmt.Printf("rescuing orphan %s %s %s\n", path, orphan.Title, orphan.Id)
|
|
|
|
outputItem(orphan, path)
|
|
|
|
}
|
|
|
|
delete(orphans, item.Id)
|
|
|
|
} else {
|
|
|
|
// fmt.Printf("file %s %s %s\n", path, item.Title, item.Id)
|
|
|
|
// If item has no MD5 sum it isn't stored on drive, so ignore it
|
|
|
|
if item.Md5Checksum != "" {
|
2014-07-29 18:50:07 +02:00
|
|
|
if fs := f.newFsObjectWithInfo(path, item); fs != nil {
|
2013-01-23 22:19:26 +01:00
|
|
|
out <- fs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the API request
|
|
|
|
_, err := f.listAll("", "", false, false, func(item *drive.File) bool {
|
|
|
|
if len(item.Parents) == 0 {
|
|
|
|
// fmt.Printf("no parents %s %s: %#v\n", item.Title, item.Id, item)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
parentId := item.Parents[0].Id
|
|
|
|
directory, ok := f.dirCache.GetInv(parentId)
|
|
|
|
if !ok {
|
|
|
|
// Haven't found the parent yet so add to orphans
|
|
|
|
// fmt.Printf("orphan[%s] %s %s\n", parentId, item.Title, item.Id)
|
|
|
|
orphans[parentId] = append(orphans[parentId], item)
|
|
|
|
} else {
|
|
|
|
outputItem(item, directory)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(orphans) > 0 {
|
|
|
|
// fmt.Printf("Orphans!!!! %v", orphans)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-01-15 00:38:18 +01:00
|
|
|
// Splits a path into directory, leaf
|
|
|
|
//
|
|
|
|
// Path shouldn't start or end with a /
|
|
|
|
//
|
|
|
|
// If there are no slashes then directory will be "" and leaf = path
|
|
|
|
func splitPath(path string) (directory, leaf string) {
|
|
|
|
lastSlash := strings.LastIndex(path, "/")
|
|
|
|
if lastSlash >= 0 {
|
|
|
|
directory = path[:lastSlash]
|
|
|
|
leaf = path[lastSlash+1:]
|
|
|
|
} else {
|
|
|
|
directory = ""
|
|
|
|
leaf = path
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finds the directory passed in returning the directory Id starting from pathId
|
|
|
|
//
|
|
|
|
// Path shouldn't start or end with a /
|
|
|
|
//
|
|
|
|
// If create is set it will make the directory if not found
|
|
|
|
//
|
|
|
|
// Algorithm:
|
|
|
|
// Look in the cache for the path, if found return the pathId
|
|
|
|
// If not found strip the last path off the path and recurse
|
|
|
|
// Now have a parent directory id, so look in the parent for self and return it
|
|
|
|
func (f *FsDrive) findDir(path string, create bool) (pathId string, err error) {
|
|
|
|
pathId = f._findDirInCache(path)
|
|
|
|
if pathId != "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f.findDirLock.Lock()
|
|
|
|
defer f.findDirLock.Unlock()
|
|
|
|
return f._findDir(path, create)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for the root and in the cache - safe to call without the findDirLock
|
|
|
|
func (f *FsDrive) _findDirInCache(path string) string {
|
|
|
|
// fmt.Println("Finding",path,"create",create,"cache",cache)
|
|
|
|
// If it is the root, then return it
|
|
|
|
if path == "" {
|
|
|
|
// fmt.Println("Root")
|
|
|
|
return f.rootId
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it is in the cache then return it
|
|
|
|
pathId, ok := f.dirCache.Get(path)
|
|
|
|
if ok {
|
|
|
|
// fmt.Println("Cache hit on", path)
|
|
|
|
return pathId
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlocked findDir - must have findDirLock
|
|
|
|
func (f *FsDrive) _findDir(path string, create bool) (pathId string, err error) {
|
|
|
|
pathId = f._findDirInCache(path)
|
|
|
|
if pathId != "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split the path into directory, leaf
|
|
|
|
directory, leaf := splitPath(path)
|
|
|
|
|
|
|
|
// Recurse and find pathId for directory
|
|
|
|
pathId, err = f._findDir(directory, create)
|
|
|
|
if err != nil {
|
|
|
|
return pathId, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the leaf in pathId
|
2013-01-20 12:56:56 +01:00
|
|
|
found, err := f.listAll(pathId, leaf, true, false, func(item *drive.File) bool {
|
|
|
|
if item.Title == leaf {
|
|
|
|
pathId = item.Id
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
2013-01-15 00:38:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return pathId, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not found create the directory if required or return an error
|
|
|
|
if !found {
|
|
|
|
if create {
|
|
|
|
// fmt.Println("Making", path)
|
|
|
|
// Define the metadata for the directory we are going to create.
|
2015-02-02 18:29:08 +01:00
|
|
|
createInfo := &drive.File{
|
2013-01-15 00:38:18 +01:00
|
|
|
Title: leaf,
|
|
|
|
Description: leaf,
|
|
|
|
MimeType: driveFolderType,
|
|
|
|
Parents: []*drive.ParentReference{{Id: pathId}},
|
|
|
|
}
|
2015-02-02 18:29:08 +01:00
|
|
|
var info *drive.File
|
2015-02-19 20:12:23 +01:00
|
|
|
f.call(&err, func() {
|
2015-02-02 18:29:08 +01:00
|
|
|
info, err = f.svc.Files.Insert(createInfo).Do()
|
|
|
|
})
|
2013-01-15 00:38:18 +01:00
|
|
|
if err != nil {
|
2014-07-29 00:34:05 +02:00
|
|
|
return pathId, fmt.Errorf("Failed to make directory: %v", err)
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
|
|
|
pathId = info.Id
|
|
|
|
} else {
|
|
|
|
return pathId, fmt.Errorf("Couldn't find directory: %q", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the directory in the cache
|
|
|
|
f.dirCache.Put(path, pathId)
|
|
|
|
|
|
|
|
// fmt.Println("Dir", path, "is", pathId)
|
|
|
|
return pathId, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finds the root directory if not already found
|
|
|
|
//
|
|
|
|
// Resets the root directory
|
|
|
|
//
|
|
|
|
// If create is set it will make the directory if not found
|
|
|
|
func (f *FsDrive) findRoot(create bool) error {
|
2014-05-05 20:52:52 +02:00
|
|
|
f.findRootLock.Lock()
|
|
|
|
defer f.findRootLock.Unlock()
|
|
|
|
if f.foundRoot {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
rootId, err := f.findDir(f.root, create)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.rootId = rootId
|
|
|
|
f.dirCache.Flush()
|
|
|
|
// Put the root directory in
|
|
|
|
f.dirCache.Put("", f.rootId)
|
|
|
|
f.foundRoot = true
|
|
|
|
return nil
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
|
|
|
|
2014-07-29 00:34:05 +02:00
|
|
|
// Resets the root directory to the absolute root and clears the dirCache
|
|
|
|
func (f *FsDrive) resetRoot() {
|
|
|
|
f.findRootLock.Lock()
|
|
|
|
defer f.findRootLock.Unlock()
|
|
|
|
f.foundRoot = false
|
|
|
|
f.dirCache.Flush()
|
|
|
|
|
|
|
|
// Put the true root in
|
|
|
|
f.rootId = f.about.RootFolderId
|
|
|
|
|
|
|
|
// Put the root directory in
|
|
|
|
f.dirCache.Put("", f.rootId)
|
|
|
|
}
|
|
|
|
|
2013-01-15 00:38:18 +01:00
|
|
|
// Walk the path returning a channel of FsObjects
|
2013-06-28 09:57:32 +02:00
|
|
|
func (f *FsDrive) List() fs.ObjectsChan {
|
|
|
|
out := make(fs.ObjectsChan, fs.Config.Checkers)
|
2013-01-15 00:38:18 +01:00
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
err := f.findRoot(false)
|
|
|
|
if err != nil {
|
2013-06-27 21:13:07 +02:00
|
|
|
fs.Stats.Error()
|
2015-02-27 16:04:18 +01:00
|
|
|
fs.Log(f, "Couldn't find root: %s", err)
|
2013-01-15 00:38:18 +01:00
|
|
|
} else {
|
2015-02-04 22:29:51 +01:00
|
|
|
if f.root == "" && *driveFullList {
|
2013-01-23 22:19:26 +01:00
|
|
|
err = f.listDirFull(f.rootId, "", out)
|
|
|
|
} else {
|
|
|
|
err = f.listDirRecursive(f.rootId, "", out)
|
|
|
|
}
|
2013-01-15 00:38:18 +01:00
|
|
|
if err != nil {
|
2013-06-27 21:13:07 +02:00
|
|
|
fs.Stats.Error()
|
2015-02-27 16:04:18 +01:00
|
|
|
fs.Log(f, "List failed: %s", err)
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2013-01-23 23:43:20 +01:00
|
|
|
// Walk the path returning a channel of FsObjects
|
2013-06-28 09:57:32 +02:00
|
|
|
func (f *FsDrive) ListDir() fs.DirChan {
|
|
|
|
out := make(fs.DirChan, fs.Config.Checkers)
|
2013-01-23 23:43:20 +01:00
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
err := f.findRoot(false)
|
|
|
|
if err != nil {
|
2013-06-27 21:13:07 +02:00
|
|
|
fs.Stats.Error()
|
2015-02-27 16:04:18 +01:00
|
|
|
fs.Log(f, "Couldn't find root: %s", err)
|
2013-01-23 23:43:20 +01:00
|
|
|
} else {
|
|
|
|
_, err := f.listAll(f.rootId, "", true, false, func(item *drive.File) bool {
|
2013-06-28 09:57:32 +02:00
|
|
|
dir := &fs.Dir{
|
2013-01-23 23:43:20 +01:00
|
|
|
Name: item.Title,
|
|
|
|
Bytes: -1,
|
|
|
|
Count: -1,
|
|
|
|
}
|
2014-07-29 18:50:07 +02:00
|
|
|
dir.When, _ = time.Parse(timeFormatIn, item.ModifiedDate)
|
2013-01-23 23:43:20 +01:00
|
|
|
out <- dir
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
if err != nil {
|
2013-06-27 21:13:07 +02:00
|
|
|
fs.Stats.Error()
|
2015-02-27 16:04:18 +01:00
|
|
|
fs.Log(f, "ListDir failed: %s", err)
|
2013-01-23 23:43:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2014-04-18 18:04:21 +02:00
|
|
|
// Put the object
|
2013-01-15 00:38:18 +01:00
|
|
|
//
|
2014-04-18 18:46:57 +02:00
|
|
|
// This assumes that the object doesn't not already exists - if you
|
|
|
|
// call it when it does exist then it will create a duplicate. Call
|
|
|
|
// object.Update() in this case.
|
|
|
|
//
|
2013-01-15 00:38:18 +01:00
|
|
|
// Copy the reader in to the new object which is returned
|
|
|
|
//
|
2014-04-18 18:04:21 +02:00
|
|
|
// The new object may have been created if an error is returned
|
2013-06-28 09:57:32 +02:00
|
|
|
func (f *FsDrive) Put(in io.Reader, remote string, modTime time.Time, size int64) (fs.Object, error) {
|
2013-01-15 00:38:18 +01:00
|
|
|
// Temporary FsObject under construction
|
2014-04-18 18:46:57 +02:00
|
|
|
o := &FsObjectDrive{drive: f, remote: remote}
|
|
|
|
|
|
|
|
directory, leaf := splitPath(o.remote)
|
|
|
|
directoryId, err := f.findDir(directory, true)
|
|
|
|
if err != nil {
|
|
|
|
return o, fmt.Errorf("Couldn't find or make directory: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define the metadata for the file we are going to create.
|
2015-02-02 18:29:08 +01:00
|
|
|
createInfo := &drive.File{
|
2014-04-18 18:46:57 +02:00
|
|
|
Title: leaf,
|
|
|
|
Description: leaf,
|
|
|
|
Parents: []*drive.ParentReference{{Id: directoryId}},
|
2015-03-01 13:38:31 +01:00
|
|
|
MimeType: fs.MimeType(o),
|
|
|
|
ModifiedDate: modTime.Format(timeFormatOut),
|
2014-04-18 18:46:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make the API request to upload metadata and file data.
|
2014-12-23 12:03:34 +01:00
|
|
|
in = &fs.SeekWrapper{In: in, Size: size}
|
2015-02-02 18:29:08 +01:00
|
|
|
var info *drive.File
|
|
|
|
// Don't retry, return a retry error instead
|
2015-02-19 20:12:23 +01:00
|
|
|
f.beginCall()
|
2015-02-02 18:29:08 +01:00
|
|
|
info, err = f.svc.Files.Insert(createInfo).Media(in).Do()
|
2015-02-19 20:12:23 +01:00
|
|
|
if f.endCall(err) {
|
2015-02-02 18:29:08 +01:00
|
|
|
return o, fs.RetryErrorf("Upload failed - retry: %s", err)
|
|
|
|
}
|
2014-04-18 18:46:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return o, fmt.Errorf("Upload failed: %s", err)
|
|
|
|
}
|
|
|
|
o.setMetaData(info)
|
|
|
|
return o, nil
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates the container if it doesn't exist
|
|
|
|
func (f *FsDrive) Mkdir() error {
|
|
|
|
return f.findRoot(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rmdir deletes the container
|
|
|
|
//
|
|
|
|
// Returns an error if it isn't empty
|
|
|
|
func (f *FsDrive) Rmdir() error {
|
|
|
|
err := f.findRoot(false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-02 18:29:08 +01:00
|
|
|
var children *drive.ChildList
|
2015-02-19 20:12:23 +01:00
|
|
|
f.call(&err, func() {
|
2015-02-02 18:29:08 +01:00
|
|
|
children, err = f.svc.Children.List(f.rootId).MaxResults(10).Do()
|
|
|
|
})
|
2013-01-15 00:38:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(children.Items) > 0 {
|
|
|
|
return fmt.Errorf("Directory not empty: %#v", children.Items)
|
|
|
|
}
|
2013-01-18 18:01:47 +01:00
|
|
|
// Delete the directory if it isn't the root
|
|
|
|
if f.root != "" {
|
2015-02-19 20:12:23 +01:00
|
|
|
f.call(&err, func() {
|
2015-02-02 18:29:08 +01:00
|
|
|
err = f.svc.Files.Delete(f.rootId).Do()
|
|
|
|
})
|
2013-01-18 18:01:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-07-29 00:34:05 +02:00
|
|
|
f.resetRoot()
|
2013-01-18 18:01:47 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-01-19 00:21:02 +01:00
|
|
|
// Return the precision
|
|
|
|
func (fs *FsDrive) Precision() time.Duration {
|
|
|
|
return time.Millisecond
|
|
|
|
}
|
|
|
|
|
2013-01-18 18:01:47 +01:00
|
|
|
// Purge deletes all the files and the container
|
|
|
|
//
|
2014-07-13 10:30:14 +02:00
|
|
|
// Optional interface: Only implement this if you have a way of
|
|
|
|
// deleting all the files quicker than just running Remove() on the
|
|
|
|
// result of List()
|
2013-01-18 18:01:47 +01:00
|
|
|
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
|
|
|
|
}
|
2015-02-19 20:12:23 +01:00
|
|
|
f.call(&err, func() {
|
2015-02-02 18:29:08 +01:00
|
|
|
err = f.svc.Files.Delete(f.rootId).Do()
|
|
|
|
})
|
2014-07-29 00:34:05 +02:00
|
|
|
f.resetRoot()
|
2013-01-15 00:38:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2014-03-28 18:56:04 +01:00
|
|
|
// Return the parent Fs
|
|
|
|
func (o *FsObjectDrive) Fs() fs.Fs {
|
|
|
|
return o.drive
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a string version
|
|
|
|
func (o *FsObjectDrive) String() string {
|
|
|
|
if o == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
return o.remote
|
|
|
|
}
|
|
|
|
|
2013-01-15 00:38:18 +01:00
|
|
|
// Return the remote path
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectDrive) Remote() string {
|
|
|
|
return o.remote
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Md5sum returns the Md5sum of an object returning a lowercase hex string
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectDrive) Md5sum() (string, error) {
|
|
|
|
return o.md5sum, nil
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of an object in bytes
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectDrive) Size() int64 {
|
|
|
|
return o.bytes
|
2013-01-19 11:11:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// setMetaData sets the fs data from a drive.File
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectDrive) setMetaData(info *drive.File) {
|
|
|
|
o.id = info.Id
|
|
|
|
o.url = info.DownloadUrl
|
|
|
|
o.md5sum = strings.ToLower(info.Md5Checksum)
|
|
|
|
o.bytes = info.FileSize
|
|
|
|
o.modifiedDate = info.ModifiedDate
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// readMetaData gets the info if it hasn't already been fetched
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectDrive) readMetaData() (err error) {
|
|
|
|
if o.id != "" {
|
2013-01-15 00:38:18 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-27 21:13:07 +02:00
|
|
|
directory, leaf := splitPath(o.remote)
|
|
|
|
directoryId, err := o.drive.findDir(directory, false)
|
2013-01-15 00:38:18 +01:00
|
|
|
if err != nil {
|
2013-06-28 09:57:32 +02:00
|
|
|
fs.Debug(o, "Couldn't find directory: %s", err)
|
2013-01-15 00:38:18 +01:00
|
|
|
return fmt.Errorf("Couldn't find directory: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-06-27 21:13:07 +02:00
|
|
|
found, err := o.drive.listAll(directoryId, leaf, false, true, func(item *drive.File) bool {
|
2013-01-20 12:56:56 +01:00
|
|
|
if item.Title == leaf {
|
2013-06-27 21:13:07 +02:00
|
|
|
o.setMetaData(item)
|
2013-01-20 12:56:56 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
2013-01-15 00:38:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-01-20 12:56:56 +01:00
|
|
|
if !found {
|
2013-06-28 09:57:32 +02:00
|
|
|
fs.Debug(o, "Couldn't find object")
|
2013-01-20 12:56:56 +01:00
|
|
|
return fmt.Errorf("Couldn't find object")
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
2013-01-20 12:56:56 +01:00
|
|
|
return nil
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ModTime returns the modification time of the object
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// It attempts to read the objects mtime and if that isn't present the
|
|
|
|
// LastModified returned in the http headers
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectDrive) ModTime() time.Time {
|
|
|
|
err := o.readMetaData()
|
2013-01-15 00:38:18 +01:00
|
|
|
if err != nil {
|
2013-06-28 09:57:32 +02:00
|
|
|
fs.Log(o, "Failed to read metadata: %s", err)
|
2013-01-15 00:38:18 +01:00
|
|
|
return time.Now()
|
|
|
|
}
|
2014-07-29 18:50:07 +02:00
|
|
|
modTime, err := time.Parse(timeFormatIn, o.modifiedDate)
|
2013-01-15 00:38:18 +01:00
|
|
|
if err != nil {
|
2013-06-28 09:57:32 +02:00
|
|
|
fs.Log(o, "Failed to read mtime from object: %s", err)
|
2013-01-15 00:38:18 +01:00
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
return modTime
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the modification time of the local fs object
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectDrive) SetModTime(modTime time.Time) {
|
|
|
|
err := o.readMetaData()
|
2013-01-15 00:38:18 +01:00
|
|
|
if err != nil {
|
2013-06-27 21:13:07 +02:00
|
|
|
fs.Stats.Error()
|
2013-06-28 09:57:32 +02:00
|
|
|
fs.Log(o, "Failed to read metadata: %s", err)
|
2013-01-15 00:38:18 +01:00
|
|
|
return
|
|
|
|
}
|
2013-01-19 11:11:55 +01:00
|
|
|
// New metadata
|
2015-02-02 18:29:08 +01:00
|
|
|
updateInfo := &drive.File{
|
2014-07-29 18:50:07 +02:00
|
|
|
ModifiedDate: modTime.Format(timeFormatOut),
|
2013-01-19 11:11:55 +01:00
|
|
|
}
|
2013-01-15 00:38:18 +01:00
|
|
|
// Set modified date
|
2015-02-02 18:29:08 +01:00
|
|
|
var info *drive.File
|
2015-02-19 20:12:23 +01:00
|
|
|
o.drive.call(&err, func() {
|
2015-02-02 18:29:08 +01:00
|
|
|
info, err = o.drive.svc.Files.Update(o.id, updateInfo).SetModifiedDate(true).Do()
|
|
|
|
})
|
2013-01-15 00:38:18 +01:00
|
|
|
if err != nil {
|
2013-06-27 21:13:07 +02:00
|
|
|
fs.Stats.Error()
|
2013-06-28 09:57:32 +02:00
|
|
|
fs.Log(o, "Failed to update remote mtime: %s", err)
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
2015-01-05 00:19:59 +01:00
|
|
|
// Update info from read data
|
|
|
|
o.setMetaData(info)
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Is this object storable
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectDrive) Storable() bool {
|
2013-01-15 00:38:18 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open an object for read
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectDrive) Open() (in io.ReadCloser, err error) {
|
2014-07-15 12:15:48 +02:00
|
|
|
req, err := http.NewRequest("GET", o.url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-07-13 19:58:06 +02:00
|
|
|
req.Header.Set("User-Agent", fs.UserAgent)
|
2015-02-02 18:29:08 +01:00
|
|
|
var res *http.Response
|
2015-02-19 20:12:23 +01:00
|
|
|
o.drive.call(&err, func() {
|
2015-02-02 18:29:08 +01:00
|
|
|
res, err = o.drive.client.Do(req)
|
|
|
|
})
|
2013-01-15 00:38:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if res.StatusCode != 200 {
|
2014-07-25 19:19:49 +02:00
|
|
|
_ = res.Body.Close() // ignore error
|
2013-01-15 00:38:18 +01:00
|
|
|
return nil, fmt.Errorf("Bad response: %d: %s", res.StatusCode, res.Status)
|
|
|
|
}
|
|
|
|
return res.Body, nil
|
|
|
|
}
|
|
|
|
|
2014-04-18 18:46:57 +02:00
|
|
|
// Update the already existing object
|
2014-04-18 18:04:21 +02:00
|
|
|
//
|
|
|
|
// Copy the reader into the object updating modTime and size
|
|
|
|
//
|
|
|
|
// The new object may have been created if an error is returned
|
|
|
|
func (o *FsObjectDrive) Update(in io.Reader, modTime time.Time, size int64) error {
|
2015-02-02 18:29:08 +01:00
|
|
|
updateInfo := &drive.File{
|
2014-04-18 18:46:57 +02:00
|
|
|
Id: o.id,
|
2014-07-29 18:50:07 +02:00
|
|
|
ModifiedDate: modTime.Format(timeFormatOut),
|
2014-04-18 18:04:21 +02:00
|
|
|
}
|
|
|
|
|
2014-04-18 18:46:57 +02:00
|
|
|
// Make the API request to upload metadata and file data.
|
2014-12-23 12:03:34 +01:00
|
|
|
in = &fs.SeekWrapper{In: in, Size: size}
|
2015-02-02 18:29:08 +01:00
|
|
|
var err error
|
|
|
|
var info *drive.File
|
|
|
|
// Don't retry, return a retry error instead
|
2015-02-19 20:12:23 +01:00
|
|
|
o.drive.beginCall()
|
2015-02-02 18:29:08 +01:00
|
|
|
info, err = o.drive.svc.Files.Update(updateInfo.Id, updateInfo).SetModifiedDate(true).Media(in).Do()
|
2015-02-19 20:12:23 +01:00
|
|
|
if o.drive.endCall(err) {
|
2015-02-02 18:29:08 +01:00
|
|
|
return fs.RetryErrorf("Update failed - retry: %s", err)
|
|
|
|
}
|
2014-04-18 18:04:21 +02:00
|
|
|
if err != nil {
|
2014-04-18 18:46:57 +02:00
|
|
|
return fmt.Errorf("Update failed: %s", err)
|
2014-04-18 18:04:21 +02:00
|
|
|
}
|
|
|
|
o.setMetaData(info)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-01-15 00:38:18 +01:00
|
|
|
// Remove an object
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectDrive) Remove() error {
|
2015-02-02 18:29:08 +01:00
|
|
|
var err error
|
2015-02-19 20:12:23 +01:00
|
|
|
o.drive.call(&err, func() {
|
2015-02-02 18:29:08 +01:00
|
|
|
err = o.drive.svc.Files.Delete(o.id).Do()
|
|
|
|
})
|
|
|
|
return err
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the interfaces are satisfied
|
2013-06-27 21:13:07 +02:00
|
|
|
var _ fs.Fs = &FsDrive{}
|
|
|
|
var _ fs.Purger = &FsDrive{}
|
2013-06-28 09:57:32 +02:00
|
|
|
var _ fs.Object = &FsObjectDrive{}
|