@@ -69,9 +69,8 @@ func NewStorage(basePath string, hostname string, timeout time.Duration) (*Stora
6969 }, nil
7070}
7171
72- // ArtifactFor returns an artifact for the given Kubernetes object
73- func (s * Storage ) ArtifactFor (kind string , metadata metav1.Object , fileName , revision string ) sourcev1.Artifact {
74- kind = strings .ToLower (kind )
72+ // ArtifactFor returns an artifact for the v1alpha1.Source.
73+ func (s * Storage ) ArtifactFor (kind string , metadata metav1.Object , fileName , revision , checksum string ) sourcev1.Artifact {
7574 path := sourcev1 .ArtifactPath (kind , metadata .GetNamespace (), metadata .GetName (), fileName )
7675 localPath := filepath .Join (s .BasePath , path )
7776 url := fmt .Sprintf ("http://%s/%s" , s .Hostname , path )
@@ -80,33 +79,35 @@ func (s *Storage) ArtifactFor(kind string, metadata metav1.Object, fileName, rev
8079 Path : localPath ,
8180 URL : url ,
8281 Revision : revision ,
82+ Checksum : checksum ,
8383 LastUpdateTime : metav1 .Now (),
8484 }
8585}
8686
87- // MkdirAll calls os.MkdirAll for the given artifact base dir
87+ // MkdirAll calls os.MkdirAll for the given v1alpha1.Artifact base dir.
8888func (s * Storage ) MkdirAll (artifact sourcev1.Artifact ) error {
89- dir := filepath .Dir (artifact . Path )
89+ dir := filepath .Dir (s . LocalPath ( artifact ) )
9090 return os .MkdirAll (dir , 0777 )
9191}
9292
93- // RemoveAll calls os.RemoveAll for the given artifact base dir
93+ // RemoveAll calls os.RemoveAll for the given v1alpha1.Artifact base dir.
9494func (s * Storage ) RemoveAll (artifact sourcev1.Artifact ) error {
95- dir := filepath .Dir (artifact . Path )
95+ dir := filepath .Dir (s . LocalPath ( artifact ) )
9696 return os .RemoveAll (dir )
9797}
9898
9999// RemoveAllButCurrent removes all files for the given artifact base dir excluding the current one
100100func (s * Storage ) RemoveAllButCurrent (artifact sourcev1.Artifact ) error {
101- dir := filepath .Dir (artifact .Path )
101+ localPath := s .LocalPath (artifact )
102+ dir := filepath .Dir (localPath )
102103 var errors []string
103104 _ = filepath .Walk (dir , func (path string , info os.FileInfo , err error ) error {
104105 if err != nil {
105106 errors = append (errors , err .Error ())
106107 return nil
107108 }
108109
109- if path != artifact . Path && ! info .IsDir () && info .Mode ()& os .ModeSymlink != os .ModeSymlink {
110+ if path != localPath && ! info .IsDir () && info .Mode ()& os .ModeSymlink != os .ModeSymlink {
110111 if err := os .Remove (path ); err != nil {
111112 errors = append (errors , info .Name ())
112113 }
@@ -123,7 +124,7 @@ func (s *Storage) RemoveAllButCurrent(artifact sourcev1.Artifact) error {
123124// ArtifactExist returns a boolean indicating whether the artifact exists in storage and is a
124125// regular file.
125126func (s * Storage ) ArtifactExist (artifact sourcev1.Artifact ) bool {
126- fi , err := os .Lstat (artifact . Path )
127+ fi , err := os .Lstat (s . LocalPath ( artifact ) )
127128 if err != nil {
128129 return false
129130 }
@@ -144,7 +145,7 @@ func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, spec sourcev1.
144145
145146 matcher := gitignore .NewMatcher (ps )
146147
147- gzFile , err := os .Create (artifact . Path )
148+ gzFile , err := os .Create (s . LocalPath ( artifact ) )
148149 if err != nil {
149150 return err
150151 }
@@ -205,28 +206,30 @@ func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, spec sourcev1.
205206
206207// WriteFile writes the given bytes to the artifact path if the checksum differs
207208func (s * Storage ) WriteFile (artifact sourcev1.Artifact , data []byte ) error {
209+ localPath := s .LocalPath (artifact )
208210 sum := s .Checksum (data )
209- if file , err := os .Stat (artifact . Path ); ! os .IsNotExist (err ) && ! file .IsDir () {
210- if fb , err := ioutil .ReadFile (artifact . Path ); err == nil && sum == s .Checksum (fb ) {
211+ if file , err := os .Stat (localPath ); ! os .IsNotExist (err ) && ! file .IsDir () {
212+ if fb , err := ioutil .ReadFile (localPath ); err == nil && sum == s .Checksum (fb ) {
211213 return nil
212214 }
213215 }
214216
215- return ioutil .WriteFile (artifact . Path , data , 0644 )
217+ return ioutil .WriteFile (localPath , data , 0644 )
216218}
217219
218220// Symlink creates or updates a symbolic link for the given artifact
219- // and returns the URL for the symlink
221+ // and returns the URL for the symlink.
220222func (s * Storage ) Symlink (artifact sourcev1.Artifact , linkName string ) (string , error ) {
221- dir := filepath .Dir (artifact .Path )
223+ localPath := s .LocalPath (artifact )
224+ dir := filepath .Dir (localPath )
222225 link := filepath .Join (dir , linkName )
223226 tmpLink := link + ".tmp"
224227
225228 if err := os .Remove (tmpLink ); err != nil && ! os .IsNotExist (err ) {
226229 return "" , err
227230 }
228231
229- if err := os .Symlink (artifact . Path , tmpLink ); err != nil {
232+ if err := os .Symlink (localPath , tmpLink ); err != nil {
230233 return "" , err
231234 }
232235
@@ -246,11 +249,20 @@ func (s *Storage) Checksum(b []byte) string {
246249
247250// Lock creates a file lock for the given artifact
248251func (s * Storage ) Lock (artifact sourcev1.Artifact ) (unlock func (), err error ) {
249- lockFile := artifact . Path + ".lock"
252+ lockFile := s . LocalPath ( artifact ) + ".lock"
250253 mutex := lockedfile .MutexAt (lockFile )
251254 return mutex .Lock ()
252255}
253256
257+ // LocalPath returns the local path of the given artifact (that is: relative to
258+ // the Storage.BasePath).
259+ func (s * Storage ) LocalPath (artifact sourcev1.Artifact ) string {
260+ if artifact .Path == "" {
261+ return ""
262+ }
263+ return filepath .Join (s .BasePath , artifact .Path )
264+ }
265+
254266func getPatterns (reader io.Reader , path []string ) []gitignore.Pattern {
255267 var ps []gitignore.Pattern
256268 scanner := bufio .NewScanner (reader )
0 commit comments