diff options
| author | Petter Rasmussen | 2016-02-07 22:01:57 +0100 |
|---|---|---|
| committer | Petter Rasmussen | 2016-02-07 22:05:14 +0100 |
| commit | 6199148887e51431f3479cce4f7b6d713cd6ba7f (patch) | |
| tree | 598e965a0eb7a1edcd8ba5954c414b1fc2f35860 /drive | |
| parent | c45e61e6406c188bcad432233a6eb25b4307a536 (diff) | |
| download | gdrive-6199148887e51431f3479cce4f7b6d713cd6ba7f.tar.bz2 | |
Retry create directory on backend error
Diffstat (limited to 'drive')
| -rw-r--r-- | drive/sync_upload.go | 72 |
1 files changed, 49 insertions, 23 deletions
diff --git a/drive/sync_upload.go b/drive/sync_upload.go index c4df1e8..f4efcb0 100644 --- a/drive/sync_upload.go +++ b/drive/sync_upload.go @@ -134,36 +134,36 @@ func (self *Drive) createMissingRemoteDirs(files *syncFiles, args UploadSyncArgs return nil, fmt.Errorf("Could not find remote directory with path '%s'", parentPath) } - dstFile := &drive.File{ - Name: lf.info.Name(), - MimeType: DirectoryMimeType, - Parents: []string{parent.file.Id}, - AppProperties: map[string]string{"syncRootId": args.RootId}, + fmt.Fprintf(args.Out, "[%04d/%04d] Creating directory %s\n", i + 1, missingCount, filepath.Join(files.root.file.Name, lf.relPath)) + + f, err := self.createMissingRemoteDir(createMissingRemoteDirArgs{ + name: lf.info.Name(), + parentId: parent.file.Id, + rootId: args.RootId, + dryRun: args.DryRun, + try: 0, + }) + if err != nil { + return nil, err } - fmt.Fprintf(args.Out, "[%04d/%04d] Creating directory: %s\n", i + 1, missingCount, filepath.Join(files.root.file.Name, lf.relPath)) - - if args.DryRun { - files.remote = append(files.remote, &RemoteFile{ - relPath: lf.relPath, - file: dstFile, - }) - } else { - f, err := self.service.Files.Create(dstFile).Do() - if err != nil { - return nil, fmt.Errorf("Failed to create directory: %s", err) - } - - files.remote = append(files.remote, &RemoteFile{ - relPath: lf.relPath, - file: f, - }) - } + files.remote = append(files.remote, &RemoteFile{ + relPath: lf.relPath, + file: f, + }) } return files, nil } +type createMissingRemoteDirArgs struct { + name string + parentId string + rootId string + dryRun bool + try int +} + func (self *Drive) uploadMissingFiles(files *syncFiles, args UploadSyncArgs) error { missingFiles := files.filterMissingRemoteFiles() missingCount := len(missingFiles) @@ -245,6 +245,32 @@ func (self *Drive) deleteExtraneousRemoteFiles(files *syncFiles, args UploadSync return nil } +func (self *Drive) createMissingRemoteDir(args createMissingRemoteDirArgs) (*drive.File, error) { + dstFile := &drive.File{ + Name: args.name, + MimeType: DirectoryMimeType, + Parents: []string{args.parentId}, + AppProperties: map[string]string{"syncRootId": args.rootId}, + } + + if args.dryRun { + return dstFile, nil + } + + f, err := self.service.Files.Create(dstFile).Do() + if err != nil { + if isBackendError(err) && args.try < MaxBackendErrorRetries { + exponentialBackoffSleep(args.try) + args.try++ + self.createMissingRemoteDir(args) + } else { + return nil, fmt.Errorf("Failed to create directory: %s", err) + } + } + + return f, nil +} + func (self *Drive) uploadMissingFile(parentId string, lf *LocalFile, args UploadSyncArgs, try int) error { srcFile, err := os.Open(lf.absPath) if err != nil { |
