2015-09-22 17:47:16 +00:00
|
|
|
// Package drive interfaces with the Google Drive object storage system
|
2013-06-27 19:13:07 +00:00
|
|
|
package drive
|
2013-01-14 23:38:18 +00: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 11:56:56 +00:00
|
|
|
// * files with / in name
|
2013-01-14 23:38:18 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-08-18 07:55:09 +00:00
|
|
|
"log"
|
2013-01-14 23:38:18 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2015-09-17 12:31:10 +00:00
|
|
|
"sync"
|
2013-01-14 23:38:18 +00:00
|
|
|
"time"
|
|
|
|
|
2015-08-18 07:55:09 +00:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/oauth2/google"
|
2014-12-12 20:02:08 +00:00
|
|
|
"google.golang.org/api/drive/v2"
|
2015-02-02 17:29:08 +00:00
|
|
|
"google.golang.org/api/googleapi"
|
2014-12-12 20:02:08 +00:00
|
|
|
|
2015-09-03 20:25:55 +00:00
|
|
|
"github.com/ncw/rclone/dircache"
|
2014-03-15 16:06:11 +00:00
|
|
|
"github.com/ncw/rclone/fs"
|
2015-08-18 07:55:09 +00:00
|
|
|
"github.com/ncw/rclone/oauthutil"
|
2015-09-11 18:18:41 +00:00
|
|
|
"github.com/ncw/rclone/pacer"
|
2015-08-29 17:14:24 +00:00
|
|
|
"github.com/spf13/pflag"
|
2014-03-15 16:06:11 +00:00
|
|
|
)
|
2013-06-29 11:15:31 +00:00
|
|
|
|
2014-03-16 14:01:17 +00:00
|
|
|
// Constants
|
|
|
|
const (
|
2015-08-18 07:55:09 +00:00
|
|
|
rcloneClientID = "202264815644.apps.googleusercontent.com"
|
2015-09-01 21:33:34 +00:00
|
|
|
rcloneClientSecret = "8p/yms3OlNXE9OTDl/HLypf9gdiJ5cT3"
|
2014-03-16 14:01:17 +00:00
|
|
|
driveFolderType = "application/vnd.google-apps.folder"
|
2014-07-29 16:50:07 +00:00
|
|
|
timeFormatIn = time.RFC3339
|
|
|
|
timeFormatOut = "2006-01-02T15:04:05.000000000Z07:00"
|
2015-02-02 17:29:08 +00:00
|
|
|
minSleep = 10 * time.Millisecond
|
|
|
|
maxSleep = 2 * time.Second
|
|
|
|
decayConstant = 2 // bigger for slower decay, exponential
|
2014-03-16 14:01:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Globals
|
|
|
|
var (
|
|
|
|
// Flags
|
2014-03-27 16:55:29 +00:00
|
|
|
driveFullList = pflag.BoolP("drive-full-list", "", true, "Use a full listing for directory list. More data but usually quicker.")
|
2015-08-16 13:49:58 +00:00
|
|
|
driveUseTrash = pflag.BoolP("drive-use-trash", "", false, "Send files to the trash instead of deleting permanently.")
|
2015-03-14 17:55:38 +00:00
|
|
|
// chunkSize is the size of the chunks created during a resumable upload and should be a power of two.
|
|
|
|
// 1<<18 is the minimum size supported by the Google uploader, and there is no maximum.
|
|
|
|
chunkSize = fs.SizeSuffix(256 * 1024)
|
|
|
|
driveUploadCutoff = chunkSize
|
2014-07-13 16:53:11 +00:00
|
|
|
// Description of how to auth for this app
|
2015-08-18 07:55:09 +00:00
|
|
|
driveConfig = &oauth2.Config{
|
|
|
|
Scopes: []string{"https://www.googleapis.com/auth/drive"},
|
|
|
|
Endpoint: google.Endpoint,
|
|
|
|
ClientID: rcloneClientID,
|
2015-09-01 21:33:34 +00:00
|
|
|
ClientSecret: fs.Reveal(rcloneClientSecret),
|
2015-08-18 07:55:09 +00:00
|
|
|
RedirectURL: oauthutil.TitleBarRedirectURL,
|
2014-07-13 16:53:11 +00:00
|
|
|
}
|
2014-03-16 14:01:17 +00:00
|
|
|
)
|
|
|
|
|
2013-06-29 11:15:31 +00:00
|
|
|
// Register with Fs
|
|
|
|
func init() {
|
2015-09-22 17:47:16 +00:00
|
|
|
fs.Register(&fs.Info{
|
2014-07-13 16:53:11 +00:00
|
|
|
Name: "drive",
|
|
|
|
NewFs: NewFs,
|
|
|
|
Config: func(name string) {
|
2015-08-18 07:55:09 +00:00
|
|
|
err := oauthutil.Config(name, driveConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to configure token: %v", err)
|
|
|
|
}
|
2014-07-13 16:53:11 +00:00
|
|
|
},
|
2014-03-15 16:06:11 +00:00
|
|
|
Options: []fs.Option{{
|
2015-09-16 19:08:40 +00:00
|
|
|
Name: oauthutil.ConfigClientID,
|
2015-10-03 13:23:12 +00:00
|
|
|
Help: "Google Application Client Id - leave blank normally.",
|
2014-03-15 16:06:11 +00:00
|
|
|
}, {
|
2015-09-16 19:08:40 +00:00
|
|
|
Name: oauthutil.ConfigClientSecret,
|
2015-10-03 13:23:12 +00:00
|
|
|
Help: "Google Application Client Secret - leave blank normally.",
|
2014-03-15 16:06:11 +00:00
|
|
|
}},
|
|
|
|
})
|
2015-03-14 17:55:38 +00:00
|
|
|
pflag.VarP(&driveUploadCutoff, "drive-upload-cutoff", "", "Cutoff for switching to chunked upload")
|
|
|
|
pflag.VarP(&chunkSize, "drive-chunk-size", "", "Upload chunk size. Must a power of 2 >= 256k.")
|
2013-06-29 11:15:31 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 23:38:18 +00:00
|
|
|
// FsDrive represents a remote drive server
|
|
|
|
type FsDrive struct {
|
2015-09-11 18:18:41 +00:00
|
|
|
name string // name of this remote
|
|
|
|
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
|
|
|
|
dirCache *dircache.DirCache // Map of directory path to directory id
|
|
|
|
pacer *pacer.Pacer // To pace the API calls
|
2013-01-14 23:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FsObjectDrive describes a drive object
|
|
|
|
type FsObjectDrive struct {
|
2013-01-19 10:11:55 +00: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-14 23:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Name of the remote (as passed into NewFs)
|
2015-08-22 15:53:11 +00:00
|
|
|
func (f *FsDrive) Name() string {
|
|
|
|
return f.name
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Root of the remote (as passed into NewFs)
|
2015-09-01 19:45:27 +00:00
|
|
|
func (f *FsDrive) Root() string {
|
|
|
|
return f.root
|
|
|
|
}
|
|
|
|
|
2013-01-14 23:38:18 +00:00
|
|
|
// String converts this FsDrive to a string
|
|
|
|
func (f *FsDrive) String() string {
|
|
|
|
return fmt.Sprintf("Google drive root '%s'", f.root)
|
|
|
|
}
|
|
|
|
|
2015-09-11 18:18:41 +00:00
|
|
|
// shouldRetry determines whehter a given err rates being retried
|
|
|
|
func shouldRetry(err error) (again bool, errOut error) {
|
|
|
|
again = false
|
|
|
|
errOut = err
|
|
|
|
if err != nil {
|
2015-03-02 09:05:23 +00:00
|
|
|
// Check for net error Timeout()
|
|
|
|
if x, ok := err.(interface {
|
|
|
|
Timeout() bool
|
|
|
|
}); ok && x.Timeout() {
|
|
|
|
again = true
|
|
|
|
}
|
|
|
|
// Check for net error Temporary()
|
|
|
|
if x, ok := err.(interface {
|
|
|
|
Temporary() bool
|
|
|
|
}); ok && x.Temporary() {
|
|
|
|
again = true
|
|
|
|
}
|
|
|
|
switch gerr := err.(type) {
|
|
|
|
case *googleapi.Error:
|
|
|
|
if gerr.Code >= 500 && gerr.Code < 600 {
|
|
|
|
// All 5xx errors should be retried
|
|
|
|
again = true
|
|
|
|
} else if len(gerr.Errors) > 0 {
|
2015-02-02 17:29:08 +00:00
|
|
|
reason := gerr.Errors[0].Reason
|
|
|
|
if reason == "rateLimitExceeded" || reason == "userRateLimitExceeded" {
|
|
|
|
again = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-11 18:18:41 +00:00
|
|
|
return again, err
|
2015-02-02 17:29:08 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 23:38:18 +00:00
|
|
|
// parseParse parses a drive 'url'
|
|
|
|
func parseDrivePath(path string) (root string, err error) {
|
2014-03-27 17:49:36 +00:00
|
|
|
root = strings.Trim(path, "/")
|
2013-01-14 23:38:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-01-20 11:56:56 +00: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-14 23:38:18 +00:00
|
|
|
//
|
|
|
|
// Search params: https://developers.google.com/drive/search-parameters
|
2015-09-22 17:47:16 +00:00
|
|
|
func (f *FsDrive) listAll(dirID string, title string, directoriesOnly bool, filesOnly bool, fn listAllFn) (found bool, err error) {
|
2013-01-23 21:19:26 +00:00
|
|
|
query := fmt.Sprintf("trashed=false")
|
2015-09-22 17:47:16 +00:00
|
|
|
if dirID != "" {
|
|
|
|
query += fmt.Sprintf(" and '%s' in parents", dirID)
|
2013-01-23 21:19:26 +00:00
|
|
|
}
|
2013-01-14 23:38:18 +00: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 21:19:26 +00:00
|
|
|
// fmt.Printf("listAll Query = %q\n", query)
|
|
|
|
list := f.svc.Files.List().Q(query).MaxResults(1000)
|
2013-01-20 11:56:56 +00:00
|
|
|
OUTER:
|
2013-01-14 23:38:18 +00:00
|
|
|
for {
|
2015-02-02 17:29:08 +00:00
|
|
|
var files *drive.FileList
|
2015-09-11 18:18:41 +00:00
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2015-02-02 17:29:08 +00:00
|
|
|
files, err = list.Do()
|
2015-09-11 18:18:41 +00:00
|
|
|
return shouldRetry(err)
|
2015-02-02 17:29:08 +00:00
|
|
|
})
|
2013-01-14 23:38:18 +00:00
|
|
|
if err != nil {
|
2013-01-20 11:56:56 +00: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-14 23:38:18 +00:00
|
|
|
}
|
|
|
|
if files.NextPageToken == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
list.PageToken(files.NextPageToken)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-14 17:55:38 +00:00
|
|
|
// Returns true of x is a power of 2 or zero
|
|
|
|
func isPowerOfTwo(x int64) bool {
|
|
|
|
switch {
|
|
|
|
case x == 0:
|
|
|
|
return true
|
|
|
|
case x < 0:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return (x & (x - 1)) == 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-16 14:01:17 +00:00
|
|
|
// NewFs contstructs an FsDrive from the path, container:path
|
|
|
|
func NewFs(name, path string) (fs.Fs, error) {
|
2015-03-14 17:55:38 +00:00
|
|
|
if !isPowerOfTwo(int64(chunkSize)) {
|
|
|
|
return nil, fmt.Errorf("drive: chunk size %v isn't a power of two", chunkSize)
|
|
|
|
}
|
|
|
|
if chunkSize < 256*1024 {
|
|
|
|
return nil, fmt.Errorf("drive: chunk size can't be less than 256k - was %v", chunkSize)
|
|
|
|
}
|
|
|
|
|
2015-08-18 07:55:09 +00:00
|
|
|
oAuthClient, err := oauthutil.NewClient(name, driveConfig)
|
2013-01-14 23:38:18 +00:00
|
|
|
if err != nil {
|
2015-08-18 07:55:09 +00:00
|
|
|
log.Fatalf("Failed to configure drive: %v", err)
|
2013-01-14 23:38:18 +00:00
|
|
|
}
|
|
|
|
|
2014-03-16 14:01:17 +00:00
|
|
|
root, err := parseDrivePath(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-01-14 23:38:18 +00:00
|
|
|
}
|
2014-07-13 16:53:11 +00:00
|
|
|
|
2014-05-05 18:52:52 +00:00
|
|
|
f := &FsDrive{
|
2015-09-11 18:18:41 +00:00
|
|
|
name: name,
|
|
|
|
root: root,
|
|
|
|
pacer: pacer.New().SetMinSleep(minSleep).SetMaxSleep(maxSleep).SetDecayConstant(decayConstant),
|
2014-05-05 18:52:52 +00:00
|
|
|
}
|
2013-01-14 23:38:18 +00:00
|
|
|
|
|
|
|
// Create a new authorized Drive client.
|
2015-08-18 07:55:09 +00:00
|
|
|
f.client = oAuthClient
|
2013-01-14 23:38:18 +00:00
|
|
|
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-09-11 18:18:41 +00:00
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2015-02-02 17:29:08 +00:00
|
|
|
f.about, err = f.svc.About.Get().Do()
|
2015-09-11 18:18:41 +00:00
|
|
|
return shouldRetry(err)
|
2015-02-02 17:29:08 +00:00
|
|
|
})
|
2013-01-14 23:38:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Couldn't read info about Drive: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-09-03 20:25:55 +00:00
|
|
|
f.dirCache = dircache.New(root, f.about.RootFolderId, f)
|
|
|
|
|
2014-05-05 18:52:52 +00:00
|
|
|
// Find the current root
|
2015-09-03 20:25:55 +00:00
|
|
|
err = f.dirCache.FindRoot(false)
|
2014-05-05 18:52:52 +00:00
|
|
|
if err != nil {
|
|
|
|
// Assume it is a file
|
2015-09-03 20:25:55 +00:00
|
|
|
newRoot, remote := dircache.SplitPath(root)
|
2014-05-05 18:52:52 +00:00
|
|
|
newF := *f
|
2015-09-03 20:25:55 +00:00
|
|
|
newF.dirCache = dircache.New(newRoot, f.about.RootFolderId, &newF)
|
2014-05-05 18:52:52 +00:00
|
|
|
newF.root = newRoot
|
|
|
|
// Make new Fs which is the parent
|
2015-09-03 20:25:55 +00:00
|
|
|
err = newF.dirCache.FindRoot(false)
|
2014-05-05 18:52:52 +00:00
|
|
|
if err != nil {
|
|
|
|
// No root so return old f
|
|
|
|
return f, nil
|
|
|
|
}
|
2014-07-29 16:50:07 +00:00
|
|
|
obj, err := newF.newFsObjectWithInfoErr(remote, nil)
|
2014-05-05 18:52:52 +00: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
|
|
|
|
}
|
2015-09-03 20:25:55 +00:00
|
|
|
// fmt.Printf("Root id %s", f.dirCache.RootID())
|
2013-01-14 23:38:18 +00:00
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an FsObject from a path
|
2014-07-29 16:50:07 +00:00
|
|
|
func (f *FsDrive) newFsObjectWithInfoErr(remote string, info *drive.File) (fs.Object, error) {
|
2013-01-14 23:38:18 +00:00
|
|
|
fs := &FsObjectDrive{
|
|
|
|
drive: f,
|
|
|
|
remote: remote,
|
|
|
|
}
|
|
|
|
if info != nil {
|
2013-01-19 10:11:55 +00:00
|
|
|
fs.setMetaData(info)
|
2013-01-14 23:38:18 +00:00
|
|
|
} else {
|
|
|
|
err := fs.readMetaData() // reads info and meta, returning an error
|
|
|
|
if err != nil {
|
2013-06-28 07:57:32 +00:00
|
|
|
// logged already fs.Debug("Failed to read info: %s", err)
|
2014-05-05 18:52:52 +00:00
|
|
|
return nil, err
|
2013-01-14 23:38:18 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-05 18:52:52 +00:00
|
|
|
return fs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an FsObject from a path
|
|
|
|
//
|
|
|
|
// May return nil if an error occurred
|
2014-07-29 16:50:07 +00:00
|
|
|
func (f *FsDrive) newFsObjectWithInfo(remote string, info *drive.File) fs.Object {
|
|
|
|
fs, _ := f.newFsObjectWithInfoErr(remote, info)
|
2014-05-05 18:52:52 +00:00
|
|
|
// Errors have already been logged
|
2013-01-14 23:38:18 +00:00
|
|
|
return fs
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// NewFsObject returns an FsObject from a path
|
2013-01-14 23:38:18 +00:00
|
|
|
//
|
|
|
|
// May return nil if an error occurred
|
2013-06-28 07:57:32 +00:00
|
|
|
func (f *FsDrive) NewFsObject(remote string) fs.Object {
|
2014-07-29 16:50:07 +00:00
|
|
|
return f.newFsObjectWithInfo(remote, nil)
|
2013-01-14 23:38:18 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// FindLeaf finds a directory of name leaf in the folder with ID pathID
|
|
|
|
func (f *FsDrive) FindLeaf(pathID, leaf string) (pathIDOut string, found bool, err error) {
|
|
|
|
// Find the leaf in pathID
|
|
|
|
found, err = f.listAll(pathID, leaf, true, false, func(item *drive.File) bool {
|
2015-09-03 20:25:55 +00:00
|
|
|
if item.Title == leaf {
|
2015-09-22 17:47:16 +00:00
|
|
|
pathIDOut = item.Id
|
2015-09-03 20:25:55 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
2015-09-22 17:47:16 +00:00
|
|
|
return pathIDOut, found, err
|
2015-09-03 20:25:55 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// CreateDir makes a directory with pathID as parent and name leaf
|
|
|
|
func (f *FsDrive) CreateDir(pathID, leaf string) (newID string, err error) {
|
2015-09-03 20:25:55 +00:00
|
|
|
// fmt.Println("Making", path)
|
|
|
|
// Define the metadata for the directory we are going to create.
|
|
|
|
createInfo := &drive.File{
|
|
|
|
Title: leaf,
|
|
|
|
Description: leaf,
|
|
|
|
MimeType: driveFolderType,
|
2015-09-22 17:47:16 +00:00
|
|
|
Parents: []*drive.ParentReference{{Id: pathID}},
|
2015-09-03 20:25:55 +00:00
|
|
|
}
|
|
|
|
var info *drive.File
|
2015-09-11 18:18:41 +00:00
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2015-09-03 20:25:55 +00:00
|
|
|
info, err = f.svc.Files.Insert(createInfo).Do()
|
2015-09-11 18:18:41 +00:00
|
|
|
return shouldRetry(err)
|
2015-09-03 20:25:55 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return info.Id, nil
|
|
|
|
}
|
|
|
|
|
2013-01-14 23:38:18 +00:00
|
|
|
// Path should be directory path either "" or "path/"
|
2013-01-23 21:19:26 +00: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
|
2015-09-22 17:47:16 +00:00
|
|
|
func (f *FsDrive) listDirRecursive(dirID string, path string, out fs.ObjectsChan) error {
|
2013-01-20 11:56:56 +00:00
|
|
|
var subError error
|
2013-01-14 23:38:18 +00:00
|
|
|
// Make the API request
|
2015-09-17 12:31:10 +00:00
|
|
|
var wg sync.WaitGroup
|
2015-09-22 17:47:16 +00:00
|
|
|
_, err := f.listAll(dirID, "", false, false, func(item *drive.File) bool {
|
2013-01-14 23:38:18 +00:00
|
|
|
// Recurse on directories
|
|
|
|
if item.MimeType == driveFolderType {
|
2015-09-17 12:31:10 +00:00
|
|
|
wg.Add(1)
|
|
|
|
folder := path + item.Title + "/"
|
|
|
|
fs.Debug(f, "Reading %s", folder)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
err := f.listDirRecursive(item.Id, folder, out)
|
|
|
|
if err != nil {
|
|
|
|
subError = err
|
|
|
|
fs.ErrorLog(f, "Error reading %s:%s", folder, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
2013-01-14 23:38:18 +00:00
|
|
|
} else {
|
|
|
|
// If item has no MD5 sum it isn't stored on drive, so ignore it
|
2013-01-20 11:56:56 +00:00
|
|
|
if item.Md5Checksum != "" {
|
2014-07-29 16:50:07 +00:00
|
|
|
if fs := f.newFsObjectWithInfo(path+item.Title, item); fs != nil {
|
2013-01-20 11:56:56 +00:00
|
|
|
out <- fs
|
|
|
|
}
|
2013-01-14 23:38:18 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-20 11:56:56 +00:00
|
|
|
return false
|
|
|
|
})
|
2015-09-17 12:31:10 +00:00
|
|
|
wg.Wait()
|
|
|
|
fs.Debug(f, "Finished reading %s", path)
|
2013-01-20 11:56:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if subError != nil {
|
|
|
|
return subError
|
2013-01-14 23:38:18 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-01-23 21:19:26 +00: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
|
2015-09-22 17:47:16 +00:00
|
|
|
func (f *FsDrive) listDirFull(dirID string, path string, out fs.ObjectsChan) error {
|
2013-01-23 21:19:26 +00: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 16:50:07 +00:00
|
|
|
if fs := f.newFsObjectWithInfo(path, item); fs != nil {
|
2013-01-23 21:19:26 +00: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
|
|
|
|
}
|
2015-09-22 17:47:16 +00:00
|
|
|
parentID := item.Parents[0].Id
|
|
|
|
directory, ok := f.dirCache.GetInv(parentID)
|
2013-01-23 21:19:26 +00:00
|
|
|
if !ok {
|
|
|
|
// Haven't found the parent yet so add to orphans
|
2015-09-22 17:47:16 +00:00
|
|
|
// fmt.Printf("orphan[%s] %s %s\n", parentID, item.Title, item.Id)
|
|
|
|
orphans[parentID] = append(orphans[parentID], item)
|
2013-01-23 21:19:26 +00:00
|
|
|
} else {
|
|
|
|
outputItem(item, directory)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(orphans) > 0 {
|
|
|
|
// fmt.Printf("Orphans!!!! %v", orphans)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// List walks the path returning a channel of FsObjects
|
2013-06-28 07:57:32 +00:00
|
|
|
func (f *FsDrive) List() fs.ObjectsChan {
|
|
|
|
out := make(fs.ObjectsChan, fs.Config.Checkers)
|
2013-01-14 23:38:18 +00:00
|
|
|
go func() {
|
|
|
|
defer close(out)
|
2015-09-03 20:25:55 +00:00
|
|
|
err := f.dirCache.FindRoot(false)
|
2013-01-14 23:38:18 +00:00
|
|
|
if err != nil {
|
2013-06-27 19:13:07 +00:00
|
|
|
fs.Stats.Error()
|
2015-08-08 19:10:31 +00:00
|
|
|
fs.ErrorLog(f, "Couldn't find root: %s", err)
|
2013-01-14 23:38:18 +00:00
|
|
|
} else {
|
2015-02-04 21:29:51 +00:00
|
|
|
if f.root == "" && *driveFullList {
|
2015-09-03 20:25:55 +00:00
|
|
|
err = f.listDirFull(f.dirCache.RootID(), "", out)
|
2013-01-23 21:19:26 +00:00
|
|
|
} else {
|
2015-09-03 20:25:55 +00:00
|
|
|
err = f.listDirRecursive(f.dirCache.RootID(), "", out)
|
2013-01-23 21:19:26 +00:00
|
|
|
}
|
2013-01-14 23:38:18 +00:00
|
|
|
if err != nil {
|
2013-06-27 19:13:07 +00:00
|
|
|
fs.Stats.Error()
|
2015-08-08 19:10:31 +00:00
|
|
|
fs.ErrorLog(f, "List failed: %s", err)
|
2013-01-14 23:38:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// ListDir walks the path returning a channel of directories
|
2013-06-28 07:57:32 +00:00
|
|
|
func (f *FsDrive) ListDir() fs.DirChan {
|
|
|
|
out := make(fs.DirChan, fs.Config.Checkers)
|
2013-01-23 22:43:20 +00:00
|
|
|
go func() {
|
|
|
|
defer close(out)
|
2015-09-03 20:25:55 +00:00
|
|
|
err := f.dirCache.FindRoot(false)
|
2013-01-23 22:43:20 +00:00
|
|
|
if err != nil {
|
2013-06-27 19:13:07 +00:00
|
|
|
fs.Stats.Error()
|
2015-08-08 19:10:31 +00:00
|
|
|
fs.ErrorLog(f, "Couldn't find root: %s", err)
|
2013-01-23 22:43:20 +00:00
|
|
|
} else {
|
2015-09-03 20:25:55 +00:00
|
|
|
_, err := f.listAll(f.dirCache.RootID(), "", true, false, func(item *drive.File) bool {
|
2013-06-28 07:57:32 +00:00
|
|
|
dir := &fs.Dir{
|
2013-01-23 22:43:20 +00:00
|
|
|
Name: item.Title,
|
|
|
|
Bytes: -1,
|
|
|
|
Count: -1,
|
|
|
|
}
|
2014-07-29 16:50:07 +00:00
|
|
|
dir.When, _ = time.Parse(timeFormatIn, item.ModifiedDate)
|
2013-01-23 22:43:20 +00:00
|
|
|
out <- dir
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
if err != nil {
|
2013-06-27 19:13:07 +00:00
|
|
|
fs.Stats.Error()
|
2015-08-08 19:10:31 +00:00
|
|
|
fs.ErrorLog(f, "ListDir failed: %s", err)
|
2013-01-23 22:43:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2015-02-14 18:48:08 +00:00
|
|
|
// Creates a drive.File info from the parameters passed in and a half
|
|
|
|
// finished FsObjectDrive which must have setMetaData called on it
|
2013-01-14 23:38:18 +00:00
|
|
|
//
|
2015-02-14 18:48:08 +00:00
|
|
|
// Used to create new objects
|
|
|
|
func (f *FsDrive) createFileInfo(remote string, modTime time.Time, size int64) (*FsObjectDrive, *drive.File, error) {
|
2013-01-14 23:38:18 +00:00
|
|
|
// Temporary FsObject under construction
|
2015-02-14 18:48:08 +00:00
|
|
|
o := &FsObjectDrive{
|
|
|
|
drive: f,
|
|
|
|
remote: remote,
|
|
|
|
bytes: size,
|
|
|
|
}
|
2014-04-18 16:46:57 +00:00
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
leaf, directoryID, err := f.dirCache.FindPath(remote, true)
|
2014-04-18 16:46:57 +00:00
|
|
|
if err != nil {
|
2015-09-03 20:25:55 +00:00
|
|
|
return nil, nil, err
|
2014-04-18 16:46:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Define the metadata for the file we are going to create.
|
2015-02-02 17:29:08 +00:00
|
|
|
createInfo := &drive.File{
|
2014-04-18 16:46:57 +00:00
|
|
|
Title: leaf,
|
|
|
|
Description: leaf,
|
2015-09-22 17:47:16 +00:00
|
|
|
Parents: []*drive.ParentReference{{Id: directoryID}},
|
2015-03-01 12:38:31 +00:00
|
|
|
MimeType: fs.MimeType(o),
|
|
|
|
ModifiedDate: modTime.Format(timeFormatOut),
|
2014-04-18 16:46:57 +00:00
|
|
|
}
|
2015-02-14 18:48:08 +00:00
|
|
|
return o, createInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put the object
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// Copy the reader in to the new object which is returned
|
|
|
|
//
|
|
|
|
// The new object may have been created if an error is returned
|
|
|
|
func (f *FsDrive) Put(in io.Reader, remote string, modTime time.Time, size int64) (fs.Object, error) {
|
|
|
|
o, createInfo, err := f.createFileInfo(remote, modTime, size)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-04-18 16:46:57 +00:00
|
|
|
|
2015-02-02 17:29:08 +00:00
|
|
|
var info *drive.File
|
2015-03-14 17:55:38 +00:00
|
|
|
if size == 0 || size < int64(driveUploadCutoff) {
|
2015-03-02 09:05:23 +00:00
|
|
|
// Make the API request to upload metadata and file data.
|
|
|
|
// Don't retry, return a retry error instead
|
2015-09-11 18:18:41 +00:00
|
|
|
err = f.pacer.CallNoRetry(func() (bool, error) {
|
|
|
|
info, err = f.svc.Files.Insert(createInfo).Media(in).Do()
|
|
|
|
return shouldRetry(err)
|
|
|
|
})
|
2015-03-02 09:05:23 +00:00
|
|
|
if err != nil {
|
2015-09-11 18:18:41 +00:00
|
|
|
return o, err
|
2015-03-02 09:05:23 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Upload the file in chunks
|
|
|
|
info, err = f.Upload(in, size, createInfo.MimeType, createInfo, remote)
|
|
|
|
if err != nil {
|
|
|
|
return o, err
|
|
|
|
}
|
2014-04-18 16:46:57 +00:00
|
|
|
}
|
|
|
|
o.setMetaData(info)
|
|
|
|
return o, nil
|
2013-01-14 23:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates the container if it doesn't exist
|
|
|
|
func (f *FsDrive) Mkdir() error {
|
2015-09-03 20:25:55 +00:00
|
|
|
return f.dirCache.FindRoot(true)
|
2013-01-14 23:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rmdir deletes the container
|
|
|
|
//
|
|
|
|
// Returns an error if it isn't empty
|
|
|
|
func (f *FsDrive) Rmdir() error {
|
2015-09-03 20:25:55 +00:00
|
|
|
err := f.dirCache.FindRoot(false)
|
2013-01-14 23:38:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-02 17:29:08 +00:00
|
|
|
var children *drive.ChildList
|
2015-09-11 18:18:41 +00:00
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2015-09-03 20:25:55 +00:00
|
|
|
children, err = f.svc.Children.List(f.dirCache.RootID()).MaxResults(10).Do()
|
2015-09-11 18:18:41 +00:00
|
|
|
return shouldRetry(err)
|
2015-02-02 17:29:08 +00:00
|
|
|
})
|
2013-01-14 23:38:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(children.Items) > 0 {
|
|
|
|
return fmt.Errorf("Directory not empty: %#v", children.Items)
|
|
|
|
}
|
2013-01-18 17:01:47 +00:00
|
|
|
// Delete the directory if it isn't the root
|
|
|
|
if f.root != "" {
|
2015-09-11 18:18:41 +00:00
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2015-08-16 13:49:58 +00:00
|
|
|
if *driveUseTrash {
|
2015-09-03 20:25:55 +00:00
|
|
|
_, err = f.svc.Files.Trash(f.dirCache.RootID()).Do()
|
2015-08-16 13:49:58 +00:00
|
|
|
} else {
|
2015-09-03 20:25:55 +00:00
|
|
|
err = f.svc.Files.Delete(f.dirCache.RootID()).Do()
|
2015-08-16 13:49:58 +00:00
|
|
|
}
|
2015-09-11 18:18:41 +00:00
|
|
|
return shouldRetry(err)
|
2015-02-02 17:29:08 +00:00
|
|
|
})
|
2013-01-18 17:01:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-09-03 20:25:55 +00:00
|
|
|
f.dirCache.ResetRoot()
|
2013-01-18 17:01:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Precision of the object storage system
|
|
|
|
func (f *FsDrive) Precision() time.Duration {
|
2013-01-18 23:21:02 +00:00
|
|
|
return time.Millisecond
|
|
|
|
}
|
|
|
|
|
2015-02-14 18:48:08 +00:00
|
|
|
// Copy src to this remote using server side copy operations.
|
|
|
|
//
|
|
|
|
// This is stored with the remote path given
|
|
|
|
//
|
|
|
|
// It returns the destination Object and a possible error
|
|
|
|
//
|
|
|
|
// Will only be called if src.Fs().Name() == f.Name()
|
|
|
|
//
|
|
|
|
// If it isn't possible then return fs.ErrorCantCopy
|
|
|
|
func (f *FsDrive) Copy(src fs.Object, remote string) (fs.Object, error) {
|
|
|
|
srcObj, ok := src.(*FsObjectDrive)
|
|
|
|
if !ok {
|
|
|
|
fs.Debug(src, "Can't copy - not same remote type")
|
|
|
|
return nil, fs.ErrorCantCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
o, createInfo, err := f.createFileInfo(remote, srcObj.ModTime(), srcObj.bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var info *drive.File
|
2015-09-11 18:18:41 +00:00
|
|
|
err = o.drive.pacer.Call(func() (bool, error) {
|
2015-02-14 18:48:08 +00:00
|
|
|
info, err = o.drive.svc.Files.Copy(srcObj.id, createInfo).Do()
|
2015-09-11 18:18:41 +00:00
|
|
|
return shouldRetry(err)
|
2015-02-14 18:48:08 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
o.setMetaData(info)
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
|
2013-01-18 17:01:47 +00:00
|
|
|
// Purge deletes all the files and the container
|
|
|
|
//
|
2014-07-13 08:30:14 +00: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 17:01:47 +00:00
|
|
|
func (f *FsDrive) Purge() error {
|
|
|
|
if f.root == "" {
|
|
|
|
return fmt.Errorf("Can't purge root directory")
|
|
|
|
}
|
2015-09-03 20:25:55 +00:00
|
|
|
err := f.dirCache.FindRoot(false)
|
2013-01-18 17:01:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-11 18:18:41 +00:00
|
|
|
err = f.pacer.Call(func() (bool, error) {
|
2015-08-16 13:49:58 +00:00
|
|
|
if *driveUseTrash {
|
2015-09-03 20:25:55 +00:00
|
|
|
_, err = f.svc.Files.Trash(f.dirCache.RootID()).Do()
|
2015-08-16 13:49:58 +00:00
|
|
|
} else {
|
2015-09-03 20:25:55 +00:00
|
|
|
err = f.svc.Files.Delete(f.dirCache.RootID()).Do()
|
2015-08-16 13:49:58 +00:00
|
|
|
}
|
2015-09-11 18:18:41 +00:00
|
|
|
return shouldRetry(err)
|
2015-02-02 17:29:08 +00:00
|
|
|
})
|
2015-09-03 20:25:55 +00:00
|
|
|
f.dirCache.ResetRoot()
|
2013-01-14 23:38:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-31 20:05:51 +00:00
|
|
|
// Move src to this remote using server side move operations.
|
|
|
|
//
|
|
|
|
// This is stored with the remote path given
|
|
|
|
//
|
|
|
|
// It returns the destination Object and a possible error
|
|
|
|
//
|
|
|
|
// Will only be called if src.Fs().Name() == f.Name()
|
|
|
|
//
|
|
|
|
// If it isn't possible then return fs.ErrorCantMove
|
2015-09-22 17:47:16 +00:00
|
|
|
func (f *FsDrive) Move(src fs.Object, remote string) (fs.Object, error) {
|
2015-08-31 20:05:51 +00:00
|
|
|
srcObj, ok := src.(*FsObjectDrive)
|
|
|
|
if !ok {
|
|
|
|
fs.Debug(src, "Can't move - not same remote type")
|
|
|
|
return nil, fs.ErrorCantMove
|
|
|
|
}
|
|
|
|
|
|
|
|
// Temporary FsObject under construction
|
2015-09-22 17:47:16 +00:00
|
|
|
dstObj, dstInfo, err := f.createFileInfo(remote, srcObj.ModTime(), srcObj.bytes)
|
2015-08-31 20:05:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the move
|
2015-09-22 17:47:16 +00:00
|
|
|
info, err := f.svc.Files.Patch(srcObj.id, dstInfo).SetModifiedDate(true).Do()
|
2015-08-31 20:05:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dstObj.setMetaData(info)
|
|
|
|
return dstObj, nil
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// DirMove moves src directory to this remote using server side move
|
|
|
|
// operations.
|
2015-08-31 20:05:51 +00:00
|
|
|
//
|
|
|
|
// Will only be called if src.Fs().Name() == f.Name()
|
|
|
|
//
|
|
|
|
// If it isn't possible then return fs.ErrorCantDirMove
|
|
|
|
//
|
|
|
|
// If destination exists then return fs.ErrorDirExists
|
2015-09-22 17:47:16 +00:00
|
|
|
func (f *FsDrive) DirMove(src fs.Fs) error {
|
2015-08-31 20:05:51 +00:00
|
|
|
srcFs, ok := src.(*FsDrive)
|
|
|
|
if !ok {
|
|
|
|
fs.Debug(srcFs, "Can't move directory - not same remote type")
|
|
|
|
return fs.ErrorCantDirMove
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if destination exists
|
2015-09-22 17:47:16 +00:00
|
|
|
f.dirCache.ResetRoot()
|
|
|
|
err := f.dirCache.FindRoot(false)
|
2015-08-31 20:05:51 +00:00
|
|
|
if err == nil {
|
|
|
|
return fs.ErrorDirExists
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find ID of parent
|
2015-09-22 17:47:16 +00:00
|
|
|
leaf, directoryID, err := f.dirCache.FindPath(f.root, true)
|
2015-08-31 20:05:51 +00:00
|
|
|
if err != nil {
|
2015-09-03 20:25:55 +00:00
|
|
|
return err
|
2015-08-31 20:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do the move
|
|
|
|
patch := drive.File{
|
|
|
|
Title: leaf,
|
2015-09-22 17:47:16 +00:00
|
|
|
Parents: []*drive.ParentReference{{Id: directoryID}},
|
2015-08-31 20:05:51 +00:00
|
|
|
}
|
2015-09-22 17:47:16 +00:00
|
|
|
_, err = f.svc.Files.Patch(srcFs.dirCache.RootID(), &patch).Do()
|
2015-08-31 20:05:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-03 20:25:55 +00:00
|
|
|
srcFs.dirCache.ResetRoot()
|
2015-08-31 20:05:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-01-14 23:38:18 +00:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Fs returns the parent Fs
|
2014-03-28 17:56:04 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Remote returns the remote path
|
2013-06-27 19:13:07 +00:00
|
|
|
func (o *FsObjectDrive) Remote() string {
|
|
|
|
return o.remote
|
2013-01-14 23:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Md5sum returns the Md5sum of an object returning a lowercase hex string
|
2013-06-27 19:13:07 +00:00
|
|
|
func (o *FsObjectDrive) Md5sum() (string, error) {
|
|
|
|
return o.md5sum, nil
|
2013-01-14 23:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of an object in bytes
|
2013-06-27 19:13:07 +00:00
|
|
|
func (o *FsObjectDrive) Size() int64 {
|
|
|
|
return o.bytes
|
2013-01-19 10:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// setMetaData sets the fs data from a drive.File
|
2013-06-27 19:13:07 +00: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-14 23:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// readMetaData gets the info if it hasn't already been fetched
|
2013-06-27 19:13:07 +00:00
|
|
|
func (o *FsObjectDrive) readMetaData() (err error) {
|
|
|
|
if o.id != "" {
|
2013-01-14 23:38:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
leaf, directoryID, err := o.drive.dirCache.FindPath(o.remote, false)
|
2013-01-14 23:38:18 +00:00
|
|
|
if err != nil {
|
2015-09-03 20:25:55 +00:00
|
|
|
return err
|
2013-01-14 23:38:18 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
found, err := o.drive.listAll(directoryID, leaf, false, true, func(item *drive.File) bool {
|
2013-01-20 11:56:56 +00:00
|
|
|
if item.Title == leaf {
|
2013-06-27 19:13:07 +00:00
|
|
|
o.setMetaData(item)
|
2013-01-20 11:56:56 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
2013-01-14 23:38:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-01-20 11:56:56 +00:00
|
|
|
if !found {
|
2013-06-28 07:57:32 +00:00
|
|
|
fs.Debug(o, "Couldn't find object")
|
2013-01-20 11:56:56 +00:00
|
|
|
return fmt.Errorf("Couldn't find object")
|
2013-01-14 23:38:18 +00:00
|
|
|
}
|
2013-01-20 11:56:56 +00:00
|
|
|
return nil
|
2013-01-14 23:38:18 +00: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 19:13:07 +00:00
|
|
|
func (o *FsObjectDrive) ModTime() time.Time {
|
|
|
|
err := o.readMetaData()
|
2013-01-14 23:38:18 +00:00
|
|
|
if err != nil {
|
2013-06-28 07:57:32 +00:00
|
|
|
fs.Log(o, "Failed to read metadata: %s", err)
|
2013-01-14 23:38:18 +00:00
|
|
|
return time.Now()
|
|
|
|
}
|
2014-07-29 16:50:07 +00:00
|
|
|
modTime, err := time.Parse(timeFormatIn, o.modifiedDate)
|
2013-01-14 23:38:18 +00:00
|
|
|
if err != nil {
|
2013-06-28 07:57:32 +00:00
|
|
|
fs.Log(o, "Failed to read mtime from object: %s", err)
|
2013-01-14 23:38:18 +00:00
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
return modTime
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// SetModTime sets the modification time of the drive fs object
|
2013-06-27 19:13:07 +00:00
|
|
|
func (o *FsObjectDrive) SetModTime(modTime time.Time) {
|
|
|
|
err := o.readMetaData()
|
2013-01-14 23:38:18 +00:00
|
|
|
if err != nil {
|
2013-06-27 19:13:07 +00:00
|
|
|
fs.Stats.Error()
|
2015-08-08 19:10:31 +00:00
|
|
|
fs.ErrorLog(o, "Failed to read metadata: %s", err)
|
2013-01-14 23:38:18 +00:00
|
|
|
return
|
|
|
|
}
|
2013-01-19 10:11:55 +00:00
|
|
|
// New metadata
|
2015-02-02 17:29:08 +00:00
|
|
|
updateInfo := &drive.File{
|
2014-07-29 16:50:07 +00:00
|
|
|
ModifiedDate: modTime.Format(timeFormatOut),
|
2013-01-19 10:11:55 +00:00
|
|
|
}
|
2013-01-14 23:38:18 +00:00
|
|
|
// Set modified date
|
2015-02-02 17:29:08 +00:00
|
|
|
var info *drive.File
|
2015-09-11 18:18:41 +00:00
|
|
|
err = o.drive.pacer.Call(func() (bool, error) {
|
2015-02-02 17:29:08 +00:00
|
|
|
info, err = o.drive.svc.Files.Update(o.id, updateInfo).SetModifiedDate(true).Do()
|
2015-09-11 18:18:41 +00:00
|
|
|
return shouldRetry(err)
|
2015-02-02 17:29:08 +00:00
|
|
|
})
|
2013-01-14 23:38:18 +00:00
|
|
|
if err != nil {
|
2013-06-27 19:13:07 +00:00
|
|
|
fs.Stats.Error()
|
2015-08-08 19:10:31 +00:00
|
|
|
fs.ErrorLog(o, "Failed to update remote mtime: %s", err)
|
2015-03-02 09:25:33 +00:00
|
|
|
return
|
2013-01-14 23:38:18 +00:00
|
|
|
}
|
2015-01-04 23:19:59 +00:00
|
|
|
// Update info from read data
|
|
|
|
o.setMetaData(info)
|
2013-01-14 23:38:18 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Storable returns a boolean as to whether this object is storable
|
2013-06-27 19:13:07 +00:00
|
|
|
func (o *FsObjectDrive) Storable() bool {
|
2013-01-14 23:38:18 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open an object for read
|
2013-06-27 19:13:07 +00:00
|
|
|
func (o *FsObjectDrive) Open() (in io.ReadCloser, err error) {
|
2015-08-16 13:11:21 +00:00
|
|
|
if o.url == "" {
|
|
|
|
return nil, fmt.Errorf("Forbidden to download - check sharing permission")
|
|
|
|
}
|
2014-07-15 10:15:48 +00:00
|
|
|
req, err := http.NewRequest("GET", o.url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-07-13 17:58:06 +00:00
|
|
|
req.Header.Set("User-Agent", fs.UserAgent)
|
2015-02-02 17:29:08 +00:00
|
|
|
var res *http.Response
|
2015-09-11 18:18:41 +00:00
|
|
|
err = o.drive.pacer.Call(func() (bool, error) {
|
2015-02-02 17:29:08 +00:00
|
|
|
res, err = o.drive.client.Do(req)
|
2015-09-11 18:18:41 +00:00
|
|
|
return shouldRetry(err)
|
2015-02-02 17:29:08 +00:00
|
|
|
})
|
2013-01-14 23:38:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if res.StatusCode != 200 {
|
2014-07-25 17:19:49 +00:00
|
|
|
_ = res.Body.Close() // ignore error
|
2013-01-14 23:38:18 +00:00
|
|
|
return nil, fmt.Errorf("Bad response: %d: %s", res.StatusCode, res.Status)
|
|
|
|
}
|
|
|
|
return res.Body, nil
|
|
|
|
}
|
|
|
|
|
2014-04-18 16:46:57 +00:00
|
|
|
// Update the already existing object
|
2014-04-18 16:04:21 +00: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 17:29:08 +00:00
|
|
|
updateInfo := &drive.File{
|
2014-04-18 16:46:57 +00:00
|
|
|
Id: o.id,
|
2014-07-29 16:50:07 +00:00
|
|
|
ModifiedDate: modTime.Format(timeFormatOut),
|
2014-04-18 16:04:21 +00:00
|
|
|
}
|
|
|
|
|
2014-04-18 16:46:57 +00:00
|
|
|
// Make the API request to upload metadata and file data.
|
2015-02-02 17:29:08 +00:00
|
|
|
var err error
|
|
|
|
var info *drive.File
|
2015-03-14 17:55:38 +00:00
|
|
|
if size == 0 || size < int64(driveUploadCutoff) {
|
2015-03-02 09:05:23 +00:00
|
|
|
// Don't retry, return a retry error instead
|
2015-09-11 18:18:41 +00:00
|
|
|
err = o.drive.pacer.CallNoRetry(func() (bool, error) {
|
|
|
|
info, err = o.drive.svc.Files.Update(updateInfo.Id, updateInfo).SetModifiedDate(true).Media(in).Do()
|
|
|
|
return shouldRetry(err)
|
|
|
|
})
|
2015-03-02 09:05:23 +00:00
|
|
|
if err != nil {
|
2015-09-11 18:18:41 +00:00
|
|
|
return err
|
2015-03-02 09:05:23 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Upload the file in chunks
|
|
|
|
info, err = o.drive.Upload(in, size, fs.MimeType(o), updateInfo, o.remote)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-18 16:04:21 +00:00
|
|
|
}
|
|
|
|
o.setMetaData(info)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-01-14 23:38:18 +00:00
|
|
|
// Remove an object
|
2013-06-27 19:13:07 +00:00
|
|
|
func (o *FsObjectDrive) Remove() error {
|
2015-02-02 17:29:08 +00:00
|
|
|
var err error
|
2015-09-11 18:18:41 +00:00
|
|
|
err = o.drive.pacer.Call(func() (bool, error) {
|
2015-08-16 13:49:58 +00:00
|
|
|
if *driveUseTrash {
|
|
|
|
_, err = o.drive.svc.Files.Trash(o.id).Do()
|
|
|
|
} else {
|
|
|
|
err = o.drive.svc.Files.Delete(o.id).Do()
|
|
|
|
}
|
2015-09-11 18:18:41 +00:00
|
|
|
return shouldRetry(err)
|
2015-02-02 17:29:08 +00:00
|
|
|
})
|
|
|
|
return err
|
2013-01-14 23:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the interfaces are satisfied
|
2015-08-31 20:05:51 +00:00
|
|
|
var (
|
|
|
|
_ fs.Fs = (*FsDrive)(nil)
|
|
|
|
_ fs.Purger = (*FsDrive)(nil)
|
|
|
|
_ fs.Copier = (*FsDrive)(nil)
|
|
|
|
_ fs.Mover = (*FsDrive)(nil)
|
|
|
|
_ fs.DirMover = (*FsDrive)(nil)
|
|
|
|
_ fs.Object = (*FsObjectDrive)(nil)
|
|
|
|
)
|