Skip to content

Commit 90bb99a

Browse files
committed
use go-safetemp to safely allocate temporary directories that don't
exist
1 parent a0f4648 commit 90bb99a

File tree

4 files changed

+18
-23
lines changed

4 files changed

+18
-23
lines changed

client.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"strings"
1818

1919
urlhelper "github.com/hashicorp/go-getter/helper/url"
20+
"github.com/hashicorp/go-safetemp"
2021
)
2122

2223
// Client is a client for downloading things.
@@ -100,17 +101,14 @@ func (c *Client) Get() error {
100101
dst := c.Dst
101102
src, subDir := SourceDirSubdir(src)
102103
if subDir != "" {
103-
tmpDir, err := ioutil.TempDir("", "tf")
104+
td, tdcloser, err := safetemp.Dir("", "getter")
104105
if err != nil {
105106
return err
106107
}
107-
if err := os.RemoveAll(tmpDir); err != nil {
108-
return err
109-
}
110-
defer os.RemoveAll(tmpDir)
108+
defer tdcloser.Close()
111109

112110
realDst = dst
113-
dst = tmpDir
111+
dst = td
114112
}
115113

116114
u, err := urlhelper.Parse(src)

get_git.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212

1313
urlhelper "github.com/hashicorp/go-getter/helper/url"
14+
"github.com/hashicorp/go-safetemp"
1415
"github.com/hashicorp/go-version"
1516
)
1617

@@ -105,13 +106,11 @@ func (g *GitGetter) Get(dst string, u *url.URL) error {
105106
// GetFile for Git doesn't support updating at this time. It will download
106107
// the file every time.
107108
func (g *GitGetter) GetFile(dst string, u *url.URL) error {
108-
td, err := ioutil.TempDir("", "getter-git")
109+
td, tdcloser, err := safetemp.Dir("", "getter")
109110
if err != nil {
110111
return err
111112
}
112-
if err := os.RemoveAll(td); err != nil {
113-
return err
114-
}
113+
defer tdcloser.Close()
115114

116115
// Get the filename, and strip the filename from the URL so we can
117116
// just get the repository directly.

get_hg.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package getter
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"net/url"
76
"os"
87
"os/exec"
98
"path/filepath"
109
"runtime"
1110

1211
urlhelper "github.com/hashicorp/go-getter/helper/url"
12+
"github.com/hashicorp/go-safetemp"
1313
)
1414

1515
// HgGetter is a Getter implementation that will download a module from
@@ -64,13 +64,13 @@ func (g *HgGetter) Get(dst string, u *url.URL) error {
6464
// GetFile for Hg doesn't support updating at this time. It will download
6565
// the file every time.
6666
func (g *HgGetter) GetFile(dst string, u *url.URL) error {
67-
td, err := ioutil.TempDir("", "getter-hg")
67+
// Create a temporary directory to store the full source. This has to be
68+
// a non-existent directory.
69+
td, tdcloser, err := safetemp.Dir("", "getter")
6870
if err != nil {
6971
return err
7072
}
71-
if err := os.RemoveAll(td); err != nil {
72-
return err
73-
}
73+
defer tdcloser.Close()
7474

7575
// Get the filename, and strip the filename from the URL so we can
7676
// just get the repository directly.

get_http.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"encoding/xml"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"net/http"
98
"net/url"
109
"os"
1110
"path/filepath"
1211
"strings"
12+
13+
"github.com/hashicorp/go-safetemp"
1314
)
1415

1516
// HttpGetter is a Getter implementation that will download from an HTTP
@@ -149,16 +150,13 @@ func (g *HttpGetter) GetFile(dst string, u *url.URL) error {
149150
// getSubdir downloads the source into the destination, but with
150151
// the proper subdir.
151152
func (g *HttpGetter) getSubdir(dst, source, subDir string) error {
152-
// Create a temporary directory to store the full source
153-
td, err := ioutil.TempDir("", "tf")
153+
// Create a temporary directory to store the full source. This has to be
154+
// a non-existent directory.
155+
td, tdcloser, err := safetemp.Dir("", "getter")
154156
if err != nil {
155157
return err
156158
}
157-
defer os.RemoveAll(td)
158-
159-
// We have to create a subdirectory that doesn't exist for the file
160-
// getter to work.
161-
td = filepath.Join(td, "data")
159+
defer tdcloser.Close()
162160

163161
// Download that into the given directory
164162
if err := Get(td, source); err != nil {

0 commit comments

Comments
 (0)