This repository has been archived by the owner on Sep 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 506
/
pull.go
153 lines (138 loc) · 3.55 KB
/
pull.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package drive
import (
"fmt"
"io"
"os"
"path/filepath"
"sync"
)
const (
maxNumOfConcPullTasks = 4
)
// Pull from remote if remote path exists and in a god context. If path is a
// directory, it recursively pulls from the remote if there are remote changes.
// It doesn't check if there are remote changes if isForce is set.
func (g *Commands) Pull() (err error) {
var r, l *File
if r, err = g.rem.FindByPath(g.opts.Path); err != nil {
return
}
absPath := g.context.AbsPathOf(g.opts.Path)
localinfo, _ := os.Stat(absPath)
if localinfo != nil {
l = NewLocalFile(absPath, localinfo)
}
var cl []*Change
fmt.Println("Resolving...")
if cl, err = g.resolveChangeListRecv(false, g.opts.Path, r, l); err != nil {
return
}
if ok := printChangeList(cl, g.opts.IsNoPrompt); ok {
return g.playPullChangeList(cl)
}
return
}
func (g *Commands) playPullChangeList(cl []*Change) (err error) {
var next []*Change
g.taskStart(len(cl))
for {
if len(cl) > maxNumOfConcPullTasks {
next, cl = cl[:maxNumOfConcPullTasks], cl[maxNumOfConcPullTasks:len(cl)]
} else {
next, cl = cl, []*Change{}
}
if len(next) == 0 {
break
}
var wg sync.WaitGroup
wg.Add(len(next))
// play the changes
// TODO: add timeouts
for _, c := range next {
switch c.Op() {
case OpMod:
go g.localMod(&wg, c)
case OpAdd:
go g.localAdd(&wg, c)
case OpDelete:
go g.localDelete(&wg, c)
}
}
wg.Wait()
}
g.taskFinish()
return err
}
func (g *Commands) localMod(wg *sync.WaitGroup, change *Change) (err error) {
defer g.taskDone()
defer wg.Done()
destAbsPath := g.context.AbsPathOf(change.Path)
if change.Src.BlobAt != "" {
// download and replace
if err = g.download(change); err != nil {
return
}
}
return os.Chtimes(destAbsPath, change.Src.ModTime, change.Src.ModTime)
}
func (g *Commands) localAdd(wg *sync.WaitGroup, change *Change) (err error) {
defer g.taskDone()
defer wg.Done()
destAbsPath := g.context.AbsPathOf(change.Path)
// make parent's dir if not exists
os.MkdirAll(filepath.Dir(destAbsPath), os.ModeDir|0755)
if change.Src.IsDir {
return os.Mkdir(destAbsPath, os.ModeDir|0755)
}
if change.Src.BlobAt != "" {
// download and create
if err = g.download(change); err != nil {
return
}
}
return os.Chtimes(destAbsPath, change.Src.ModTime, change.Src.ModTime)
}
func (g *Commands) localDelete(wg *sync.WaitGroup, change *Change) (err error) {
defer g.taskDone()
defer wg.Done()
return os.RemoveAll(change.Dest.BlobAt)
}
func (g *Commands) download(change *Change) (err error) {
destAbsPath := g.context.AbsPathOf(change.Path)
var fo *os.File
fo, err = os.Create(destAbsPath)
if err != nil {
return
}
// close fo on exit and check for its returned error
defer func() {
if err := fo.Close(); err != nil {
return
}
}()
var blob io.ReadCloser
defer func() {
if blob != nil {
blob.Close()
}
}()
blob, err = g.rem.Download(change.Src.Id)
if err != nil {
return err
}
_, err = io.Copy(fo, blob)
return
}