/
tmp.go
165 lines (125 loc) · 4.01 KB
/
tmp.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Package tmp provides methods and structs for working with temporary data
package tmp
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/essentialkaos/ek/v12/fsutil"
"github.com/essentialkaos/ek/v12/rand"
"github.com/essentialkaos/ek/v12/strutil"
)
// ////////////////////////////////////////////////////////////////////////////////// //
// Temp is basic temp struct
type Temp struct {
Dir string
DirPerms os.FileMode
FilePerms os.FileMode
objects []string
}
// ////////////////////////////////////////////////////////////////////////////////// //
// ErrNilTemp is returned if temp struct is nil
var ErrNilTemp = fmt.Errorf("Temp struct is nil")
// ////////////////////////////////////////////////////////////////////////////////// //
// Dir is path to temporary directory
var Dir = os.TempDir()
// DefaultDirPerms is default permissions for directories
var DefaultDirPerms = os.FileMode(0750)
// DefaultFilePerms is default permissions for files
var DefaultFilePerms = os.FileMode(0640)
// ////////////////////////////////////////////////////////////////////////////////// //
// NewTemp creates new Temp structure
func NewTemp(dir ...string) (*Temp, error) {
tempDir := filepath.Clean(Dir)
if len(dir) != 0 {
tempDir = filepath.Clean(dir[0])
}
if !fsutil.IsExist(tempDir) {
return nil, fmt.Errorf("Directory %s does not exist", tempDir)
}
if !fsutil.IsDir(tempDir) {
return nil, fmt.Errorf("%s is not a directory", tempDir)
}
if !fsutil.IsWritable(tempDir) {
return nil, fmt.Errorf("Directory %s is not writable", tempDir)
}
return &Temp{
Dir: tempDir,
DirPerms: DefaultDirPerms,
FilePerms: DefaultFilePerms,
}, nil
}
// ////////////////////////////////////////////////////////////////////////////////// //
// MkDir creates temporary directory
func (t *Temp) MkDir(nameSuffix ...string) (string, error) {
if t == nil {
return "", ErrNilTemp
}
name := strutil.Q(nameSuffix...)
tmpDir := getTempName(t.Dir, name)
err := os.MkdirAll(tmpDir, t.DirPerms)
if err != nil {
return "", err
}
t.objects = append(t.objects, tmpDir)
return tmpDir, err
}
// MkFile creates temporary file
func (t *Temp) MkFile(nameSuffix ...string) (*os.File, string, error) {
if t == nil {
return nil, "", ErrNilTemp
}
name := strutil.Q(nameSuffix...)
tmpFile := getTempName(t.Dir, name)
fd, err := os.OpenFile(tmpFile, os.O_RDWR|os.O_CREATE, t.FilePerms)
if err != nil {
return nil, "", err
}
t.objects = append(t.objects, tmpFile)
return fd, tmpFile, nil
}
// MkName returns name for temporary object (file or directory)
func (t *Temp) MkName(nameSuffix ...string) string {
if t == nil {
return ""
}
name := strutil.Q(nameSuffix...)
tmpObj := getTempName(t.Dir, name)
t.objects = append(t.objects, tmpObj)
return tmpObj
}
// Clean removes all temporary objects (files and directories)
func (t *Temp) Clean() {
if t == nil || t.objects == nil || len(t.objects) == 0 {
return
}
for _, object := range t.objects {
os.RemoveAll(object)
}
}
// ////////////////////////////////////////////////////////////////////////////////// //
// getTempName returns name of temporary file
func getTempName(dir, name string) string {
var result string
for {
if name != "" {
result = filepath.Join(dir, fmt.Sprintf(
"tmp_%d_%s_%s", time.Now().UnixMilli(), rand.String(8), name,
))
} else {
result = filepath.Join(dir, fmt.Sprintf(
"tmp_%d_%s", time.Now().UnixMilli(), rand.String(8),
))
}
if !fsutil.IsExist(result) {
break
}
}
return result
}