-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfiles.go
More file actions
41 lines (32 loc) · 734 Bytes
/
files.go
File metadata and controls
41 lines (32 loc) · 734 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package saml
import (
"crypto/sha1"
"fmt"
"os"
)
// WorkDir is a temporary directory for files. We need to write keys to disk in
// order for xmlsec1 to pick them and use them.
var WorkDir = "/tmp"
func writeFile(buf []byte) (string, error) {
destDir := WorkDir
if err := os.MkdirAll(destDir, 0700); err != nil {
return "", err
}
hash := sha1.Sum(buf)
fileName := destDir + "/" + fmt.Sprintf("%x.tmp", hash)
if stat, err := os.Stat(fileName); err == nil {
if !stat.IsDir() {
// Path exists and is a file.
return fileName, nil
}
}
fp, err := os.Create(fileName)
if err != nil {
return "", err
}
defer fp.Close()
if _, err := fp.Write(buf); err != nil {
return "", err
}
return fileName, nil
}