-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfile.go
56 lines (53 loc) · 1.32 KB
/
file.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
package patrol
import (
"fmt"
"os"
"path/filepath"
)
var (
ERR_PATH_INVALID = fmt.Errorf("Path can NOT be current or parent Directory!")
ERR_DIRECTORY = fmt.Errorf("Path was a Directory")
)
func OpenFile(
path string,
) (
*os.File,
error,
) {
// validate csv path
// path.Clean will return an absolute path with one exception
// it can return "." and ".."
// if either of these values are returned they are useless to us, we can't write to the current or parent directories
path = filepath.Clean(path)
if path == "." ||
path == ".." {
// invalid
return nil, ERR_PATH_INVALID
}
// path is usable
// we need to open our file for writing, we don't care about writing
// we don't care if our file already exists, append if it does!
// we also need to create our file if it does not exist
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
// failed to open
return nil, err
}
// returned file handler must be closed
// if an error occurs we must close this handler
// check if our "file" is a directory
i, err := f.Stat()
if err != nil {
// failed to stat
f.Close()
return nil, err
}
if i.IsDir() {
// this is a directory, we can't use this!
f.Close()
return nil, ERR_DIRECTORY
}
// file is safe to use
// returned file handler MUST be closed
return f, nil
}