Skip to content

Commit d400f6b

Browse files
authored
Merge pull request #63 from brianneville/plugin-tmp-name
containerz/server: hide temp files and add timestamp
2 parents a71d099 + d190477 commit d400f6b

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

server/deploy.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"io"
2121
"os"
2222
"path/filepath"
23+
"strings"
24+
"time"
2325

2426
"golang.org/x/sys/unix"
2527
"google.golang.org/grpc/codes"
@@ -31,6 +33,9 @@ import (
3133
cpb "github.com/openconfig/gnoi/containerz"
3234
)
3335

36+
// tmpFilePrefix is a prefix used in the naming of temp files written by moveFile
37+
const tmpFilePrefix = ".tmp-"
38+
3439
// pluginLocation is the location where plugins are expected to be written to.
3540
var pluginLocation = "/plugins"
3641

@@ -212,7 +217,13 @@ func moveFile(sourcePath, destPath string) error {
212217
if err != nil {
213218
return fmt.Errorf("unable to open source file: %s", err)
214219
}
215-
outputTmp, err := os.CreateTemp(filepath.Dir(destPath), "tmp-")
220+
if baseName := filepath.Base(destPath); strings.HasPrefix(baseName, tmpFilePrefix) {
221+
return fmt.Errorf("cannot write file %q with prefix %q,"+
222+
" please use another name without this prefix",
223+
baseName, tmpFilePrefix)
224+
}
225+
outputTmp, err := os.CreateTemp(filepath.Dir(destPath),
226+
fmt.Sprintf("%s%d-", tmpFilePrefix, time.Now().UnixNano()))
216227
if err != nil {
217228
inputFile.Close()
218229
return fmt.Errorf("unable to create a temp file with error %s", err)

0 commit comments

Comments
 (0)