diff options
| author | Petter Rasmussen | 2016-01-31 12:56:58 +0100 |
|---|---|---|
| committer | Petter Rasmussen | 2016-01-31 12:56:58 +0100 |
| commit | 893e48c864ac601fbde20f1b07331852e26358a7 (patch) | |
| tree | 697ccfb69bd66d00975df631495926d675aa57e0 /drive/upload_sync.go | |
| parent | 3d98eb0ac55a09a92f4dd1e214f246e988874e9f (diff) | |
| download | gdrive-893e48c864ac601fbde20f1b07331852e26358a7.tar.bz2 | |
Delete extraneous files
Diffstat (limited to 'drive/upload_sync.go')
| -rw-r--r-- | drive/upload_sync.go | 64 |
1 files changed, 63 insertions, 1 deletions
diff --git a/drive/upload_sync.go b/drive/upload_sync.go index b000006..763bfcf 100644 --- a/drive/upload_sync.go +++ b/drive/upload_sync.go @@ -17,7 +17,7 @@ type UploadSyncArgs struct { Progress io.Writer Path string Parent string - DeleteRemote bool + DeleteExtraneous bool ChunkSize int64 } @@ -65,6 +65,13 @@ func (self *Drive) UploadSync(args UploadSyncArgs) error { return err } + // Delete extraneous files on drive + if args.DeleteExtraneous { + err = self.deleteExtraneousRemoteFiles(files, args) + if err != nil { + return err + } + } fmt.Fprintf(args.Out, "Sync finished in %s\n", time.Since(started)) return nil @@ -237,6 +244,25 @@ func (self *Drive) updateChangedFiles(files *syncFiles, args UploadSyncArgs) err return nil } +func (self *Drive) deleteExtraneousRemoteFiles(files *syncFiles, args UploadSyncArgs) error { + extraneousFiles := files.filterExtraneousRemoteFiles() + extraneousCount := len(extraneousFiles) + + if extraneousCount > 0 { + fmt.Fprintf(args.Out, "\n%d extraneous file(s) on drive\n", extraneousCount) + } + + for i, rf := range extraneousFiles { + fmt.Fprintf(args.Out, "[%04d/%04d] Deleting %s\n", i + 1, extraneousCount, filepath.Join(files.root.file.Name, rf.relPath)) + err := self.deleteRemoteFile(rf, args) + if err != nil { + return err + } + } + + return nil +} + func (self *Drive) uploadMissingFile(rootId string, lf *localFile, args UploadSyncArgs) error { srcFile, err := os.Open(lf.absPath) if err != nil { @@ -287,6 +313,15 @@ func (self *Drive) updateChangedFile(cf *changedFile, args UploadSyncArgs) error return nil } +func (self *Drive) deleteRemoteFile(rf *remoteFile, args UploadSyncArgs) error { + err := self.service.Files.Delete(rf.file.Id).Do() + if err != nil { + return fmt.Errorf("Failed to delete file: %s", err) + } + + return nil +} + func (self *Drive) prepareRemoteFiles(rootDir *drive.File) ([]*remoteFile, error) { // Find all files which has rootDir as root query := fmt.Sprintf("appProperties has {key='syncRootId' and value='%s'}", rootDir.Id) @@ -488,11 +523,28 @@ func (self *syncFiles) filterChangedLocalFiles() []*changedFile { return files } +func (self *syncFiles) filterExtraneousRemoteFiles() []*remoteFile { + var files []*remoteFile + + for _, rf := range self.remote { + if !self.existsLocal(rf) { + files = append(files, rf) + } + } + + return files +} + func (self *syncFiles) existsRemote(lf *localFile) bool { _, found := self.findRemoteByPath(lf.relPath) return found } +func (self *syncFiles) existsLocal(rf *remoteFile) bool { + _, found := self.findLocalByPath(rf.relPath) + return found +} + func (self *syncFiles) findRemoteByPath(relPath string) (*remoteFile, bool) { if relPath == "." { return self.root, true @@ -507,6 +559,16 @@ func (self *syncFiles) findRemoteByPath(relPath string) (*remoteFile, bool) { return nil, false } +func (self *syncFiles) findLocalByPath(relPath string) (*localFile, bool) { + for _, lf := range self.local { + if relPath == lf.relPath { + return lf, true + } + } + + return nil, false +} + type byPathLength []*localFile func (self byPathLength) Len() int { |
