Skip to content

Commit

Permalink
Reviews.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricematrat committed Jul 24, 2018
1 parent 6cfdd8c commit 6dc8592
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions internal/charmstore/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ func (r *multiReadSeekCloser) Close() error {
// If no such file was found, it returns an error
// with a params.ErrNotFound cause.
//
// If the file is a symlink, it returns an error ErrRedirect that holds the path
// that the symbolic link refers to.
//
// If the file is actually a directory in the blob, it returns
// an error with a params.ErrForbidden cause.
func (s *Store) OpenBlobFile(blob *Blob, filePath string) (io.ReadCloser, int64, error) {
Expand All @@ -137,25 +140,28 @@ func (s *Store) OpenBlobFile(blob *Blob, filePath string) (io.ReadCloser, int64,
if err != nil {
return nil, 0, errgo.Notef(err, "unable to read file %q", filePath)
}
if file.Mode()&os.ModeSymlink != 0 {
defer content.Close()
url, err := ioutil.ReadAll(content)
if err != nil {
return nil, 0, errgo.Notef(err, "cannot read archive data for symlink %s", file.Name)
}
return nil, 0, ErrRedirect{URL: string(url)}
if file.Mode()&os.ModeSymlink == 0 {
return content, fileInfo.Size(), nil
}
defer content.Close()
url, err := ioutil.ReadAll(content)
if err != nil {
return nil, 0, errgo.Notef(err, "cannot read archive data for symlink %s", file.Name)
}
return content, fileInfo.Size(), nil
return nil, 0, &ErrRedirect{Path: string(url)}
}
return nil, 0, errgo.WithCausef(nil, params.ErrNotFound, "file %q not found in the archive", filePath)
}

// ErrRedirect holds the error returned by OpenBlobFile when a symbolic link
// is opened. The Path field holds the name of the file that the symbolic link
// refers to.
type ErrRedirect struct {
URL string
Path string
}

func (e ErrRedirect) Error() string {
return fmt.Sprintf("redirect to %v", e.URL)
return fmt.Sprintf("file is a symbolic link to %q", e.Path)
}

// OpenCachedBlobFile opens a file from the given entity's archive blob.
Expand Down

0 comments on commit 6dc8592

Please sign in to comment.