Skip to content

Commit

Permalink
Delete extraneous files
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Jan 31, 2016
1 parent 3d98eb0 commit 893e48c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
64 changes: 63 additions & 1 deletion drive/upload_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type UploadSyncArgs struct {
Progress io.Writer
Path string
Parent string
DeleteRemote bool
DeleteExtraneous bool
ChunkSize int64
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions gdrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ func main() {
OmitValue: true,
},
cli.BoolFlag{
Name: "deleteRemote",
Patterns: []string{"--delete-remote"},
Name: "deleteExtraneous",
Patterns: []string{"--delete-extraneous"},
Description: "Delete extraneous files from drive",
OmitValue: true,
},
Expand Down
2 changes: 1 addition & 1 deletion handlers_drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func uploadSyncHandler(ctx cli.Context) {
Progress: progressWriter(args.Bool("noProgress")),
Path: args.String("path"),
Parent: args.String("parent"),
DeleteRemote: args.Bool("deleteRemote"),
DeleteExtraneous: args.Bool("deleteExtraneous"),
ChunkSize: args.Int64("chunksize"),
})
checkErr(err)
Expand Down

0 comments on commit 893e48c

Please sign in to comment.