diff options
| author | Petter Rasmussen | 2016-02-13 17:41:40 +0100 |
|---|---|---|
| committer | Petter Rasmussen | 2016-02-13 17:41:40 +0100 |
| commit | dd623e82015df217a84cffd6592c379aad3b4c29 (patch) | |
| tree | 53c1cc833217c6782f2bd456fd04ad0d4cb0a90f /drive/sync_upload.go | |
| parent | 96d229360a687d9a660fe9bf7e01170fbde16798 (diff) | |
| download | gdrive-dd623e82015df217a84cffd6592c379aad3b4c29.tar.bz2 | |
Ensure that there is enough free space on drive
Diffstat (limited to 'drive/sync_upload.go')
| -rw-r--r-- | drive/sync_upload.go | 45 |
1 files changed, 40 insertions, 5 deletions
diff --git a/drive/sync_upload.go b/drive/sync_upload.go index 2c4a363..a94e507 100644 --- a/drive/sync_upload.go +++ b/drive/sync_upload.go @@ -44,12 +44,18 @@ func (self *Drive) UploadSync(args UploadSyncArgs) error { return err } - // Find changed files + // Find missing and changed files changedFiles := files.filterChangedLocalFiles() + missingFiles := files.filterMissingRemoteFiles() fmt.Fprintf(args.Out, "Found %d local files and %d remote files\n", len(files.local), len(files.remote)) - // Ensure that that we don't overwrite any remote changes + // Ensure that there is enough free space on drive + if ok, msg := self.checkRemoteFreeSpace(missingFiles, changedFiles); !ok { + return fmt.Errorf(msg) + } + + // Ensure that we don't overwrite any remote changes if args.Resolution == NoResolution { err = ensureNoRemoteModifications(changedFiles) if err != nil { @@ -64,7 +70,7 @@ func (self *Drive) UploadSync(args UploadSyncArgs) error { } // Upload missing files - err = self.uploadMissingFiles(files, args) + err = self.uploadMissingFiles(missingFiles, files, args) if err != nil { return err } @@ -177,8 +183,7 @@ type createMissingRemoteDirArgs struct { try int } -func (self *Drive) uploadMissingFiles(files *syncFiles, args UploadSyncArgs) error { - missingFiles := files.filterMissingRemoteFiles() +func (self *Drive) uploadMissingFiles(missingFiles []*LocalFile, files *syncFiles, args UploadSyncArgs) error { missingCount := len(missingFiles) if missingCount > 0 { @@ -433,3 +438,33 @@ func ensureNoRemoteModifications(files []*changedFile) error { formatConflicts(conflicts, buffer) return fmt.Errorf(buffer.String()) } + +func (self *Drive) checkRemoteFreeSpace(missingFiles []*LocalFile, changedFiles []*changedFile) (bool, string) { + about, err := self.service.About.Get().Fields("storageQuota").Do() + if err != nil { + return false, fmt.Sprintf("Failed to determine free space: %s", err) + } + + quota := about.StorageQuota + if quota.Limit == 0 { + return true, "" + } + + freeSpace := quota.Limit - quota.Usage + + var totalSize int64 + + for _, lf := range missingFiles { + totalSize += lf.Size() + } + + for _, cf := range changedFiles { + totalSize += cf.local.Size() + } + + if totalSize > freeSpace { + return false, fmt.Sprintf("Not enough free space, have %s need %s", formatSize(freeSpace, false), formatSize(totalSize, false)) + } + + return true, "" +} |
