forked from andreaskoch/allmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem.go
211 lines (165 loc) · 4.06 KB
/
filesystem.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2015 Andreas Koch. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fsutil
import (
"bufio"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
)
// readLine returns a single line (without the ending \n)
// from the input buffered reader.
// An error is returned iff there is an error with the
// buffered reader.
func readLine(bufferedReader *bufio.Reader) (string, error) {
var (
isPrefix bool = true
err error = nil
line, ln []byte
)
for isPrefix && err == nil {
line, isPrefix, err = bufferedReader.ReadLine()
ln = append(ln, line...)
}
return string(ln), err
}
// Get all lines of a given file
func GetLines(inFile io.Reader) []string {
lines := make([]string, 0, 10)
bufferedReader := bufio.NewReader(inFile)
line, err := readLine(bufferedReader)
for err == nil {
lines = append(lines, line)
line, err = readLine(bufferedReader)
}
return lines
}
func CreateDirectory(path string) bool {
err := os.MkdirAll(path, 0700)
return err == nil
}
func OpenFile(filepath string) (*os.File, error) {
if !FileExists(filepath) {
return nil, fmt.Errorf("The file %q does not exist.", filepath)
}
return os.OpenFile(filepath, os.O_RDWR, 0644)
}
func CreateFile(filePath string) (success bool, err error) {
// make sure the parent directory exists
directory := filepath.Dir(filePath)
if !DirectoryExists(directory) {
if !CreateDirectory(directory) {
return false, fmt.Errorf("Cannot create the directory for the given file %q.", filePath)
}
}
// create the file
if _, err := os.Create(filePath); err != nil {
return false, fmt.Errorf("Could not create file %q. Error: ", filePath, err)
}
return true, nil
}
func PathExists(path string) bool {
if strings.TrimSpace(path) == "" {
return false
}
if _, err := os.Stat(path); err != nil && os.IsNotExist(err) {
return false
}
return true
}
func FileExists(path string) bool {
if strings.TrimSpace(path) == "" {
return false
}
file, err := os.Stat(path)
if err != nil && os.IsNotExist(err) {
return false
}
return !file.IsDir()
}
func DirectoryExists(path string) bool {
if strings.TrimSpace(path) == "" {
return false
}
file, err := os.Stat(path)
if err != nil && os.IsNotExist(err) {
return false
}
return file.IsDir()
}
func IsFile(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return false, err
}
return fileInfo.IsDir() == false, nil
}
func IsDirectory(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return false, err
}
return fileInfo.IsDir(), nil
}
// Gets the current working directory in which this application is being executed.
func GetWorkingDirectory() string {
workingDirectory, err := os.Getwd()
if err != nil {
return "."
}
return workingDirectory
}
func GetSubDirectories(path string) []string {
directories := make([]string, 0)
if ok, _ := IsDirectory(path); !ok {
fmt.Errorf("%q is not a directory.\n", path)
return directories
}
directoryEntries, err := ioutil.ReadDir(path)
if err != nil {
fmt.Errorf("Cannot read directory %q.\n", path)
return directories
}
for _, entry := range directoryEntries {
if !entry.IsDir() {
continue // skip files
}
subDirectory := filepath.Join(path, entry.Name())
directories = append(directories, subDirectory)
}
return directories
}
func GetModificationTime(path string) (time.Time, error) {
info, err := os.Stat(path)
if err != nil {
var t time.Time
return t, err
}
return info.ModTime(), nil
}
// GetTempDirectory returns the path to a new temparory directory.
func GetTempDirectory() string {
randomString := rand_str()
tempDir := filepath.Join(os.TempDir(), randomString)
if !CreateDirectory(tempDir) {
panic(fmt.Sprintf("Cannot create temp directory %q.", tempDir))
}
return tempDir
}
func rand_str() string {
size := 8
rb := make([]byte, size)
_, err := rand.Read(rb)
if err != nil {
panic(err)
}
rs := base64.URLEncoding.EncodeToString(rb)
return rs
}