Skip to content

Commit 19d7281

Browse files
authored
fix: Fix template create with sub-folders on Windows (coder#4548)
On Windows, files in tar archives were stored with Windows path-separators resulting in them being individual files as opposed to contained in a folder. This commit ensures Unix-based paths (slash) are being used inside tar archives. Exmple of previous output: ``` /tmp/provisionerd673501182/images: /tmp/provisionerd673501182/: README.md images images\base.Dockerfile images\java.Dockerfile images\node.Dockerfile main.tf ``` Fixes coder#2815
1 parent 88f7505 commit 19d7281

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

provisionersdk/archive.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ func Tar(directory string, limit int64) ([]byte, error) {
8787
// Don't store tfstate!
8888
return err
8989
}
90-
header.Name = rel
90+
// Use unix paths in the tar archive.
91+
header.Name = filepath.ToSlash(rel)
9192
if err := tarWriter.WriteHeader(header); err != nil {
9293
return err
9394
}
@@ -131,7 +132,7 @@ func Untar(directory string, archive []byte) error {
131132
return err
132133
}
133134
// #nosec
134-
target := filepath.Join(directory, header.Name)
135+
target := filepath.Join(directory, filepath.FromSlash(header.Name))
135136
switch header.Typeflag {
136137
case tar.TypeDir:
137138
if _, err := os.Stat(target); err != nil {

provisionersdk/archive_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package provisionersdk_test
22

33
import (
4-
"fmt"
54
"os"
65
"path/filepath"
76
"testing"
@@ -59,6 +58,7 @@ func TestTar(t *testing.T) {
5958
}}
6059
for _, file := range files {
6160
newDir := dir
61+
file.Name = filepath.FromSlash(file.Name)
6262
if filepath.Base(file.Name) != file.Name {
6363
newDir = filepath.Join(newDir, filepath.Dir(file.Name))
6464
err := os.MkdirAll(newDir, 0755)
@@ -70,7 +70,6 @@ func TestTar(t *testing.T) {
7070
_ = tmpFile.Close()
7171
file.Name, err = filepath.Rel(dir, tmpFile.Name())
7272
require.NoError(t, err)
73-
fmt.Printf("rel")
7473
}
7574
content, err := provisionersdk.Tar(dir, 1024)
7675
require.NoError(t, err)

0 commit comments

Comments
 (0)