Skip to content

Commit

Permalink
Add symlinks to slug tarball
Browse files Browse the repository at this point in the history
This change adds support for symlinks in a builds final slug archive,
adding the relevant tar headers.
  • Loading branch information
CGA1123 committed Sep 3, 2021
1 parent 2113665 commit e9dd77b
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ type tarball struct {

// targz will walk srcDirPath recursively and write the correspoding G-Zipped Tar
// Archive to the given writers.
//
// TODO: symlinks?
func targz(srcDirPath, dstDirPath string) (*tarball, error) {
f, err := os.Create(dstDirPath)
if err != nil {
Expand All @@ -172,26 +170,42 @@ func targz(srcDirPath, dstDirPath string) (*tarball, error) {
return fmt.Errorf("file moved or removed while building tarball: %w", err)
}

if !info.Mode().IsRegular() {
link := ""
isSymlink := false

if (info.Mode() & fs.ModeSymlink) != 0 {
l, err := os.Readlink(file)
if err != nil {
return fmt.Errorf("failed to readlink: %w", err)
}

link = l
isSymlink = true
}

if !(info.Mode().IsRegular() || isSymlink) {
return nil
}

header, err := tar.FileInfoHeader(info, d.Name())
header, err := tar.FileInfoHeader(info, link)
if err != nil {
return err
}

header.Name = strings.TrimPrefix(strings.TrimPrefix(file, srcDirPath), string(filepath.Separator))

// Heroku requires GNU Tar format (at least for slugs, maybe not for build sources?)
//
// https://devcenter.heroku.com/articles/platform-api-deploying-slugs#create-slug-archive
header.Format = tar.FormatGNU
header.Name = strings.TrimPrefix(strings.TrimPrefix(file, srcDirPath), string(filepath.Separator))

if err := tw.WriteHeader(header); err != nil {
return err
}

if isSymlink {
return nil
}

f, err := os.Open(file)
if err != nil {
return err
Expand Down

0 comments on commit e9dd77b

Please sign in to comment.