Skip to content

Commit

Permalink
Update tar write function to handle irregular files
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Sim <isim@redhat.com>
  • Loading branch information
Ivan Sim committed Apr 30, 2021
1 parent 7dc0559 commit 7bfa328
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions core/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,27 +164,11 @@ func compressed(dataDir string, blocks []*promtsdb.Block, writer *io.PipeWriter)
var (
buf = &bytes.Buffer{}
tw = tar.NewWriter(buf)

dirChunksHead = filepath.Join(dataDir, "chunks_head")
dirWAL = filepath.Join(dataDir, "wal")
)

writeHeader := func(path string, info os.FileInfo, typeFlag byte) error {
name := path[len(dataDir)+1:]
header := &tar.Header{
Name: name,
Mode: int64(info.Mode()),
ModTime: info.ModTime(),
Size: info.Size(),
Typeflag: typeFlag,
}

return tw.WriteHeader(header)
}

dirs := []string{
dirChunksHead,
dirWAL,
filepath.Join(dataDir, "chunks_head"),
filepath.Join(dataDir, "wal"),
}
for _, block := range blocks {
dirs = append(dirs, block.Dir())
Expand All @@ -197,7 +181,20 @@ func compressed(dataDir string, blocks []*promtsdb.Block, writer *io.PipeWriter)
return err
}

if err := writeHeader(path, info, tar.TypeReg); err != nil {
var link string
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
if link, err = os.Readlink(path); err != nil {
return err
}
}

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

header.Name = path[len(dataDir)+1:]
if err = tw.WriteHeader(header); err != nil {
return err
}

Expand All @@ -210,7 +207,8 @@ func compressed(dataDir string, blocks []*promtsdb.Block, writer *io.PipeWriter)
return fmt.Errorf("failed to read data file: %w", err)
}

if _, err := tw.Write(data); err != nil {
buf := bytes.NewBuffer(data)
if _, err := io.Copy(tw, buf); err != nil {
return fmt.Errorf("failed to write compressed file: %w", err)
}

Expand Down

0 comments on commit 7bfa328

Please sign in to comment.