@@ -69,7 +69,7 @@ func NewStorage(basePath string, hostname string, timeout time.Duration) (*Stora
6969 }, nil
7070}
7171
72- // ArtifactFor returns an artifact for the given Kubernetes object
72+ // ArtifactFor returns an artifact for the v1alpha1.Source.
7373func (s * Storage ) ArtifactFor (kind string , metadata metav1.Object , fileName , revision string ) sourcev1.Artifact {
7474 path := sourcev1 .ArtifactPath (kind , metadata .GetNamespace (), metadata .GetName (), fileName )
7575 localPath := filepath .Join (s .BasePath , path )
@@ -83,29 +83,30 @@ func (s *Storage) ArtifactFor(kind string, metadata metav1.Object, fileName, rev
8383 }
8484}
8585
86- // MkdirAll calls os.MkdirAll for the given artifact base dir
86+ // MkdirAll calls os.MkdirAll for the given v1alpha1.Artifact base dir.
8787func (s * Storage ) MkdirAll (artifact sourcev1.Artifact ) error {
88- dir := filepath .Dir (artifact . Path )
88+ dir := filepath .Dir (s . LocalPath ( artifact ) )
8989 return os .MkdirAll (dir , 0777 )
9090}
9191
92- // RemoveAll calls os.RemoveAll for the given artifact base dir
92+ // RemoveAll calls os.RemoveAll for the given v1alpha1.Artifact base dir.
9393func (s * Storage ) RemoveAll (artifact sourcev1.Artifact ) error {
94- dir := filepath .Dir (artifact . Path )
94+ dir := filepath .Dir (s . LocalPath ( artifact ) )
9595 return os .RemoveAll (dir )
9696}
9797
9898// RemoveAllButCurrent removes all files for the given artifact base dir excluding the current one
9999func (s * Storage ) RemoveAllButCurrent (artifact sourcev1.Artifact ) error {
100- dir := filepath .Dir (artifact .Path )
100+ localPath := s .LocalPath (artifact )
101+ dir := filepath .Dir (localPath )
101102 var errors []string
102103 _ = filepath .Walk (dir , func (path string , info os.FileInfo , err error ) error {
103104 if err != nil {
104105 errors = append (errors , err .Error ())
105106 return nil
106107 }
107108
108- if path != artifact . Path && ! info .IsDir () && info .Mode ()& os .ModeSymlink != os .ModeSymlink {
109+ if path != localPath && ! info .IsDir () && info .Mode ()& os .ModeSymlink != os .ModeSymlink {
109110 if err := os .Remove (path ); err != nil {
110111 errors = append (errors , info .Name ())
111112 }
@@ -122,7 +123,7 @@ func (s *Storage) RemoveAllButCurrent(artifact sourcev1.Artifact) error {
122123// ArtifactExist returns a boolean indicating whether the artifact exists in storage and is a
123124// regular file.
124125func (s * Storage ) ArtifactExist (artifact sourcev1.Artifact ) bool {
125- fi , err := os .Lstat (artifact . Path )
126+ fi , err := os .Lstat (s . LocalPath ( artifact ) )
126127 if err != nil {
127128 return false
128129 }
@@ -143,7 +144,7 @@ func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, spec sourcev1.
143144
144145 matcher := gitignore .NewMatcher (ps )
145146
146- gzFile , err := os .Create (artifact . Path )
147+ gzFile , err := os .Create (s . LocalPath ( artifact ) )
147148 if err != nil {
148149 return err
149150 }
@@ -204,28 +205,30 @@ func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, spec sourcev1.
204205
205206// WriteFile writes the given bytes to the artifact path if the checksum differs
206207func (s * Storage ) WriteFile (artifact sourcev1.Artifact , data []byte ) error {
208+ localPath := s .LocalPath (artifact )
207209 sum := s .Checksum (data )
208- if file , err := os .Stat (artifact . Path ); ! os .IsNotExist (err ) && ! file .IsDir () {
209- if fb , err := ioutil .ReadFile (artifact . Path ); err == nil && sum == s .Checksum (fb ) {
210+ if file , err := os .Stat (localPath ); ! os .IsNotExist (err ) && ! file .IsDir () {
211+ if fb , err := ioutil .ReadFile (localPath ); err == nil && sum == s .Checksum (fb ) {
210212 return nil
211213 }
212214 }
213215
214- return ioutil .WriteFile (artifact . Path , data , 0644 )
216+ return ioutil .WriteFile (localPath , data , 0644 )
215217}
216218
217219// Symlink creates or updates a symbolic link for the given artifact
218- // and returns the URL for the symlink
220+ // and returns the URL for the symlink.
219221func (s * Storage ) Symlink (artifact sourcev1.Artifact , linkName string ) (string , error ) {
220- dir := filepath .Dir (artifact .Path )
222+ localPath := s .LocalPath (artifact )
223+ dir := filepath .Dir (localPath )
221224 link := filepath .Join (dir , linkName )
222225 tmpLink := link + ".tmp"
223226
224227 if err := os .Remove (tmpLink ); err != nil && ! os .IsNotExist (err ) {
225228 return "" , err
226229 }
227230
228- if err := os .Symlink (artifact . Path , tmpLink ); err != nil {
231+ if err := os .Symlink (localPath , tmpLink ); err != nil {
229232 return "" , err
230233 }
231234
@@ -245,11 +248,20 @@ func (s *Storage) Checksum(b []byte) string {
245248
246249// Lock creates a file lock for the given artifact
247250func (s * Storage ) Lock (artifact sourcev1.Artifact ) (unlock func (), err error ) {
248- lockFile := artifact . Path + ".lock"
251+ lockFile := s . LocalPath ( artifact ) + ".lock"
249252 mutex := lockedfile .MutexAt (lockFile )
250253 return mutex .Lock ()
251254}
252255
256+ // LocalPath returns the local path of the given artifact (that is: relative to
257+ // the Storage.BasePath).
258+ func (s * Storage ) LocalPath (artifact sourcev1.Artifact ) string {
259+ if artifact .Path == "" {
260+ return ""
261+ }
262+ return filepath .Join (s .BasePath , artifact .Path )
263+ }
264+
253265func getPatterns (reader io.Reader , path []string ) []gitignore.Pattern {
254266 var ps []gitignore.Pattern
255267 scanner := bufio .NewScanner (reader )
0 commit comments