Skip to content

Commit

Permalink
use go-safetemp to safely allocate temporary directories that don't
Browse files Browse the repository at this point in the history
exist
  • Loading branch information
mitchellh committed Mar 27, 2018
1 parent a0f4648 commit 90bb99a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
10 changes: 4 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"strings"

urlhelper "github.com/hashicorp/go-getter/helper/url"
"github.com/hashicorp/go-safetemp"
)

// Client is a client for downloading things.
Expand Down Expand Up @@ -100,17 +101,14 @@ func (c *Client) Get() error {
dst := c.Dst
src, subDir := SourceDirSubdir(src)
if subDir != "" {
tmpDir, err := ioutil.TempDir("", "tf")
td, tdcloser, err := safetemp.Dir("", "getter")
if err != nil {
return err
}
if err := os.RemoveAll(tmpDir); err != nil {
return err
}
defer os.RemoveAll(tmpDir)
defer tdcloser.Close()

realDst = dst
dst = tmpDir
dst = td
}

u, err := urlhelper.Parse(src)
Expand Down
7 changes: 3 additions & 4 deletions get_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

urlhelper "github.com/hashicorp/go-getter/helper/url"
"github.com/hashicorp/go-safetemp"
"github.com/hashicorp/go-version"
)

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

// Get the filename, and strip the filename from the URL so we can
// just get the repository directly.
Expand Down
10 changes: 5 additions & 5 deletions get_hg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package getter

import (
"fmt"
"io/ioutil"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"

urlhelper "github.com/hashicorp/go-getter/helper/url"
"github.com/hashicorp/go-safetemp"
)

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

// Get the filename, and strip the filename from the URL so we can
// just get the repository directly.
Expand Down
14 changes: 6 additions & 8 deletions get_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"

"github.com/hashicorp/go-safetemp"
)

// HttpGetter is a Getter implementation that will download from an HTTP
Expand Down Expand Up @@ -149,16 +150,13 @@ func (g *HttpGetter) GetFile(dst string, u *url.URL) error {
// getSubdir downloads the source into the destination, but with
// the proper subdir.
func (g *HttpGetter) getSubdir(dst, source, subDir string) error {
// Create a temporary directory to store the full source
td, err := ioutil.TempDir("", "tf")
// Create a temporary directory to store the full source. This has to be
// a non-existent directory.
td, tdcloser, err := safetemp.Dir("", "getter")
if err != nil {
return err
}
defer os.RemoveAll(td)

// We have to create a subdirectory that doesn't exist for the file
// getter to work.
td = filepath.Join(td, "data")
defer tdcloser.Close()

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

0 comments on commit 90bb99a

Please sign in to comment.