Skip to content

Commit

Permalink
Checksum computation only if !ignoreChecksum and size varies.
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Odeke committed Jan 21, 2015
1 parent 7764051 commit c281591
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 97 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ To pull specific files or directories, pass in one or more paths:
$ drive pull photos/img001.png docs
```

Note: To ignore checksum verification during a pull:

```shell
$ drive pull -ignore-checksum
```

#### Exporting Docs

By default, the `pull` command will export Google Docs documents as PDF files. To specify other formats, use the `-export` option:
Expand Down Expand Up @@ -126,6 +132,13 @@ The `push` command uploads data to Google Drive to mirror data stored locally.

Like `pull`, you can run it without any arguments to push all of the files from the current path, or you can pass in one or more paths to push specific files or directories.

Note: To ignore checksum verification during a push:

```shell
$ drive push -ignore-checksum
```


To get Google Drive to convert a file to its native Google Docs format

```shell
Expand Down
65 changes: 37 additions & 28 deletions cmd/drive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,14 @@ func (cmd *listCmd) Run(args []string) {
}

type pullCmd struct {
exportsDir *string
export *string
force *bool
hidden *bool
noPrompt *bool
noClobber *bool
recursive *bool
exportsDir *string
export *string
force *bool
hidden *bool
noPrompt *bool
noClobber *bool
recursive *bool
ignoreChecksum *bool
}

func (cmd *pullCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {
Expand All @@ -214,6 +215,7 @@ func (cmd *pullCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {
cmd.noPrompt = fs.Bool("no-prompt", false, "shows no prompt before applying the pull action")
cmd.hidden = fs.Bool("hidden", false, "allows pulling of hidden paths")
cmd.force = fs.Bool("force", false, "forces a pull even if no changes present")
cmd.ignoreChecksum = fs.Bool(drive.CLIOptionIgnoreChecksum, false, drive.DescIgnoreChecksum)
cmd.exportsDir = fs.String("export-dir", "", "directory to place exports")

return fs
Expand All @@ -235,15 +237,16 @@ func (cmd *pullCmd) Run(args []string) {
exports := nonEmptyStrings(strings.Split(*cmd.export, ","))

exitWithError(drive.New(context, &drive.Options{
Exports: uniqOrderedStr(exports),
ExportsDir: strings.Trim(*cmd.exportsDir, " "),
Force: *cmd.force,
Hidden: *cmd.hidden,
NoPrompt: *cmd.noPrompt,
NoClobber: *cmd.noClobber,
Path: path,
Recursive: *cmd.recursive,
Sources: sources,
Exports: uniqOrderedStr(exports),
ExportsDir: strings.Trim(*cmd.exportsDir, " "),
Force: *cmd.force,
Hidden: *cmd.hidden,
IgnoreChecksum: *cmd.ignoreChecksum,
NoPrompt: *cmd.noPrompt,
NoClobber: *cmd.noClobber,
Path: path,
Recursive: *cmd.recursive,
Sources: sources,
}).Pull())
}

Expand All @@ -259,7 +262,8 @@ type pushCmd struct {
convert *bool
// ocr when set indicates that Optical Character Recognition should be
// attempted on .[gif, jpg, pdf, png] uploads
ocr *bool
ocr *bool
ignoreChecksum *bool
}

func (cmd *pushCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {
Expand All @@ -270,6 +274,7 @@ func (cmd *pushCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {
cmd.force = fs.Bool("force", false, "forces a push even if no changes present")
cmd.mountedPush = fs.Bool("m", false, "allows pushing of mounted paths")
cmd.convert = fs.Bool("convert", false, "toggles conversion of the file to its appropriate Google Doc format")
cmd.ignoreChecksum = fs.Bool(drive.CLIOptionIgnoreChecksum, false, drive.DescIgnoreChecksum)
cmd.ocr = fs.Bool("ocr", false, "if true, attempt OCR on gif, jpg, pdf and png uploads")
return fs
}
Expand Down Expand Up @@ -322,12 +327,13 @@ func (cmd *pushCmd) createPushOptions() *drive.Options {
}

return &drive.Options{
Force: *cmd.force,
Hidden: *cmd.hidden,
NoClobber: *cmd.noClobber,
NoPrompt: *cmd.noPrompt,
TypeMask: mask,
Recursive: *cmd.recursive,
Force: *cmd.force,
Hidden: *cmd.hidden,
NoClobber: *cmd.noClobber,
NoPrompt: *cmd.noPrompt,
TypeMask: mask,
IgnoreChecksum: *cmd.ignoreChecksum,
Recursive: *cmd.recursive,
}
}

Expand Down Expand Up @@ -404,21 +410,24 @@ func (cmd *aboutCmd) Run(args []string) {
}

type diffCmd struct {
hidden *bool
hidden *bool
ignoreChecksum *bool
}

func (cmd *diffCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {
cmd.hidden = fs.Bool("hidden", false, "allows pulling of hidden paths")
cmd.ignoreChecksum = fs.Bool(drive.CLIOptionIgnoreChecksum, false, drive.DescIgnoreChecksum)
return fs
}

func (cmd *diffCmd) Run(args []string) {
sources, context, path := preprocessArgs(args)
exitWithError(drive.New(context, &drive.Options{
Recursive: true,
Path: path,
Hidden: *cmd.hidden,
Sources: sources,
Recursive: true,
Path: path,
Hidden: *cmd.hidden,
Sources: sources,
IgnoreChecksum: *cmd.ignoreChecksum,
}).Diff())
}

Expand Down
2 changes: 1 addition & 1 deletion src/about.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
drive "github.com/google/google-api-go-client/drive/v2"
)

const Version = "0.0.6"
const Version = "0.0.7"

const (
Barely = iota
Expand Down
7 changes: 6 additions & 1 deletion src/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ func (g *Commands) clearMountPoints() {
}
}

func (g *Commands) differ(a, b *File) bool {
return fileDifferences(a, b, g.opts.IgnoreChecksum) == DifferNone
}

func (g *Commands) resolveChangeListRecv(
isPush bool, d, p string, r *File, l *File) (cl []*Change, err error) {
var change *Change
Expand All @@ -135,7 +139,7 @@ func (g *Commands) resolveChangeListRecv(
if !g.opts.Force && hasExportLinks(r) {
// The case when we have files that don't provide the download urls
// but exportable links, we just need to check that mod times are the same.
mask := fileDifferences(r, l)
mask := fileDifferences(r, l, g.opts.IgnoreChecksum)
if !dirTypeDiffers(mask) && !modTimeDiffers(mask) {
return cl, nil
}
Expand All @@ -145,6 +149,7 @@ func (g *Commands) resolveChangeListRecv(

change.Force = g.opts.Force
change.NoClobber = g.opts.NoClobber
change.IgnoreChecksum = g.opts.IgnoreChecksum

if change.Op() != OpNone {
cl = append(cl, change)
Expand Down
3 changes: 3 additions & 0 deletions src/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ type Options struct {
Force bool
// Hidden discovers hidden paths if set
Hidden bool
// IgnoreChecksum when set avoids the step
// of comparing checksums as a final check.
IgnoreChecksum bool
// Allows listing of content in trash
InTrash bool
Meta *map[string][]string
Expand Down
6 changes: 5 additions & 1 deletion src/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ func (g *Commands) perDiff(change *Change, diffProgPath, cwd string) (err error)
change.Path, l.Size)
}

if sameFileTillChecksum(r, l) {
mask := fileDifferences(r, l, g.opts.IgnoreChecksum)
if modTimeDiffers(mask) {
fmt.Printf("* %-15s %-40s\n* %-15s %-40s\n",
"remote:", toUTCString(r.ModTime), "local:", toUTCString(l.ModTime))
} else if mask == DifferNone {
// No output when "no changes found"
return nil
}
Expand Down
54 changes: 33 additions & 21 deletions src/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,45 @@ const (
)

const (
DescAbout = "print out information about your Google drive"
DescAll = "print out the entire help section"
DescDiff = "compares local files with their remote equivalent"
DescEmptyTrash = "permanently cleans out your trash"
DescFeatures = "returns information about the features of your drive"
DescHelp = "Get help for a topic"
DescInit = "initializes a directory and authenticates user"
DescList = "lists the contents of remote path"
DescQuota = "prints out information related to your quota space"
DescPublish = "publishes a file and prints its publicly available url"
DescPull = "pulls remote changes from Google Drive"
DescPush = "push local changes to Google Drive"
DescShare = "share files with specific emails giving the specified users specifies roles and permissions"
DescTouch = "updates a remote file's modification time to that currently on the server"
DescTrash = "moves files to trash"
DescUnshare = "revoke a user's access to a file"
DescUntrash = "restores files from trash to their original locations"
DescUnpublish = "revokes public access to a file"
DescVersion = "prints the version"
DescAccountType = "\n\t* anyone.\n\t* user.\n\t* domain.\n\t* group"
DescRoles = "\n\t* owner.\n\t* reader.\n\t* writer.\n\t* commenter."
DescAbout = "print out information about your Google drive"
DescAll = "print out the entire help section"
DescDiff = "compares local files with their remote equivalent"
DescEmptyTrash = "permanently cleans out your trash"
DescFeatures = "returns information about the features of your drive"
DescHelp = "Get help for a topic"
DescInit = "initializes a directory and authenticates user"
DescList = "lists the contents of remote path"
DescQuota = "prints out information related to your quota space"
DescPublish = "publishes a file and prints its publicly available url"
DescPull = "pulls remote changes from Google Drive"
DescPush = "push local changes to Google Drive"
DescShare = "share files with specific emails giving the specified users specifies roles and permissions"
DescTouch = "updates a remote file's modification time to that currently on the server"
DescTrash = "moves files to trash"
DescUnshare = "revoke a user's access to a file"
DescUntrash = "restores files from trash to their original locations"
DescUnpublish = "revokes public access to a file"
DescVersion = "prints the version"
DescAccountType = "\n\t* anyone.\n\t* user.\n\t* domain.\n\t* group"
DescRoles = "\n\t* owner.\n\t* reader.\n\t* writer.\n\t* commenter."
DescIgnoreChecksum = "avoids computation of checksums as a final check" +
"in case for example you are low on bandwidth"
)

const (
CLIOptionIgnoreChecksum = "ignore-checksum"
)

var skipChecksumNote = fmt.Sprintf(
"\nNote: You can skip checksum verification by using `-%s`", CLIOptionIgnoreChecksum)

var docMap = map[string][]string{
AboutKey: []string{
DescAbout,
},
DiffKey: []string{
DescDiff, "Accepts multiple remote paths for line by line comparison",
skipChecksumNote,
},
EmptyTrashKey: []string{
DescEmptyTrash,
Expand All @@ -85,12 +95,14 @@ var docMap = map[string][]string{
PullKey: []string{
DescPull, "Downloads content from the remote drive or modifies",
" local content to match that on your Google Drive",
skipChecksumNote,
},
PushKey: []string{
DescPush, "Uploads content to your Google Drive from your local path",
"Push comes in a couple of flavors",
"\t* Ordinary push: `drive push path1 path2 path3`",
"\t* Mounted push: `drive push -m path1 [path2 path3] drive_context_path`",
skipChecksumNote,
},
ListKey: []string{
DescList,
Expand Down
2 changes: 1 addition & 1 deletion src/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (g *Commands) localMod(wg *sync.WaitGroup, change *Change, exports []string

// Simple heuristic to avoid downloading all the
// content yet it could just be a modTime difference
mask := fileDifferences(change.Src, change.Dest)
mask := fileDifferences(change.Src, change.Dest, change.IgnoreChecksum)
if checksumDiffers(mask) {
// download and replace
if err = g.download(change, exports); err != nil {
Expand Down
10 changes: 9 additions & 1 deletion src/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,15 @@ func (g *Commands) remoteMod(change *Change) (err error) {
return
}

_, err = g.rem.UpsertByComparison(parent.Id, absPath, change.Src, change.Dest, g.opts.TypeMask)
args := upsertOpt{
parentId: parent.Id,
fsAbsPath: absPath,
src: change.Src,
dest: change.Dest,
mask: g.opts.TypeMask,
ignoreChecksum: g.opts.IgnoreChecksum,
}
_, err = g.rem.UpsertByComparison(&args)
return err
}

Expand Down
Loading

0 comments on commit c281591

Please sign in to comment.