-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemp_dir.go
175 lines (155 loc) · 4.57 KB
/
temp_dir.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
166
167
168
169
170
171
172
173
174
175
// Copyright 2017-2019 Vlad Didenko. All rights reserved.
// See the included LICENSE.md file for licensing information
package fst // import "go.didenko.com/fst"
import (
"io/ioutil"
"os"
"path/filepath"
)
// TempInitDir function creates a directory for holding
// temporary files according to platform preferences and
// returns the directory name and a cleanup function.
//
// The returned values are:
//
// 1. a string containing the created temporary directory path
//
// 2. a cleanup function to change back to the old working
// directory and to delete the temporary directory
//
// 3. an error
//
// If there was an error while creating the temporary
// directory, then the returned directory name is empty,
// cleanup funcion is nil, and the temp folder is
// expected to be already removed.
func TempInitDir(f Fatalfable) (string, func()) {
root, err := ioutil.TempDir("", "")
if err != nil {
os.RemoveAll(root)
f.Fatalf("Creating a temp directory: %s", err)
}
return root, func() {
dirs := make([]string, 0)
err := filepath.Walk(
root,
func(fn string, fi os.FileInfo, er error) error {
if fi.IsDir() {
err = os.Chmod(fn, 0700)
if err != nil {
return err
}
dirs = append(dirs, fn)
return nil
}
return os.Remove(fn)
})
if err != nil {
f.Fatalf("In the file cleanup: %s", err)
}
for i := len(dirs) - 1; i >= 0; i-- {
err = os.RemoveAll(dirs[i])
if err != nil {
f.Fatalf("In the directory cleanup: %s", err)
}
}
}
}
// TempInitChdir creates a temporary directory in the same
// fashion as TempInitDir. It also changes into the newly
// created temporary directory and adds returning back
// to the old working directory to the returned cleanup
// function. The returned values are:
//
// 1. a string containing the previous working directory
//
// 2. a cleanup function to change back to the old working
// directory and to delete the temporary directory
//
// 3. an error
func TempInitChdir(f Fatalfable) (string, func()) {
root, cleanup := TempInitDir(f)
wd, err := os.Getwd()
if err != nil {
cleanup()
f.Fatalf("Working directory indetermined: %s", err)
}
err = os.Chdir(root)
if err != nil {
cleanup()
f.Fatalf("Changing to the temporary directory %q: %s", root, err)
}
return wd,
func() {
os.Chdir(wd)
cleanup()
}
}
// TempCloneDir function creates a copy of an existing
// directory with it's content - regular files only - for
// holding temporary test files.
//
// The returned values are:
//
// 1. a string containing the created temporary directory path
//
// 2. a cleanup function to change back to the old working
// directory and to delete the temporary directory
//
// 3. an error
//
// If there was an error while cloning the temporary
// directory, then the returned directory name is empty,
// cleanup funcion is nil, and the temp folder is
// expected to be already removed.
//
// The clone attempts to maintain the basic original Unix
// permissions (9-bit only, from the rxwrwxrwx set).
// If, however, the user does not have read permission
// for a file, or read+execute permission for a directory,
// then the clone process will naturally fail.
func TempCloneDir(f Fatalfable, src string) (string, func()) {
root, cleanup := TempInitDir(f)
TreeCopy(newFatalCleaner(f, cleanup), src, root)
return root, cleanup
}
// TempCloneChdir clones a temporary directory in the same
// fashion as TempCloneDir. It also changes into the newly
// cloned temporary directory and adds returning back
// to the old working directory to the returned cleanup
// function. The returned values are:
//
// 1. a string containing the previous working directory
//
// 2. a cleanup function to change back to the old working
// directory and to delete the temporary directory
//
// 3. an error
func TempCloneChdir(f Fatalfable, src string) (string, func()) {
root, cleanup := TempCloneDir(f, src)
wd, err := os.Getwd()
if err != nil {
cleanup()
f.Fatalf("Working directory indetermined: %s", err)
}
err = os.Chdir(root)
if err != nil {
cleanup()
f.Fatalf("Changing to the temporary directory %q: %s", root, err)
}
return wd,
func() {
os.Chdir(wd)
cleanup()
}
}
// TempCreateChdir is a combination of `TempInitChdir` and
// `TreeCreate` functions. It creates a termporary directory,
// changes into it, populates it fron the provided `config`
// as `TreeCreate` would, and returns the old directory name
// and the cleanup function.
func TempCreateChdir(f Fatalfable, nodes []*Node) (string, func()) {
old, cleanup := TempInitChdir(f)
TreeCreate(newFatalCleaner(f, cleanup), nodes)
return old, cleanup
}