Skip to content

Commit

Permalink
fix(archive): custom create archive function which preserves symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuri committed Jun 13, 2021
1 parent 992b2f3 commit da8cefb
Showing 1 changed file with 64 additions and 7 deletions.
71 changes: 64 additions & 7 deletions worker/cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package cache

import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
"strings"

api "github.com/bleenco/abstruse/pb"
"github.com/bleenco/abstruse/pkg/fs"
"github.com/mholt/archiver/v3"
)

func SaveCache(job *api.Job, dir string) (string, error) {
Expand All @@ -21,15 +23,70 @@ func SaveCache(job *api.Job, dir string) (string, error) {
}
}

cwd, err := os.Getwd()
return out, createArchive(job.GetCache(), out)
}

func createArchive(folders []string, outPath string) error {
out, err := os.Create(outPath)
if err != nil {
return out, err
return err
}
defer os.Chdir(cwd)
defer out.Close()

gw := gzip.NewWriter(out)
defer gw.Close()

tw := tar.NewWriter(gw)
defer tw.Close()

for _, folder := range folders {
folder = filepath.Join(filepath.Dir(outPath), folder)
if err := filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}

if err != nil {
return err
}

link := ""
if info.Mode()&os.ModeSymlink != 0 {
link, err = os.Readlink(path)
if err != nil {
return err
}
}

if err := os.Chdir(dir); err != nil {
return out, err
header, err := tar.FileInfoHeader(info, link)
if err != nil {
return err
}

header.Name = strings.TrimPrefix(path, fmt.Sprintf("%s/", filepath.Dir(outPath)))

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

switch header.Typeflag {
case tar.TypeLink, tar.TypeSymlink, tar.TypeChar, tar.TypeBlock, tar.TypeDir, tar.TypeFifo:
default:
file, err := os.Open(path)
if err != nil {
return err
}

if _, err := io.Copy(tw, file); err != nil {
return err
}
}

return nil
}); err != nil {
return err
}
}

return out, archiver.Archive(job.GetCache(), out)
return nil
}

0 comments on commit da8cefb

Please sign in to comment.