-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirfiles.go
127 lines (115 loc) · 3.37 KB
/
dirfiles.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
package main
import (
"errors"
"os"
"path/filepath"
"strings"
)
//-----------------------------------------------------------------------------
type DirFiles struct {
Path string
fset map[string]struct{}
}
//-----------------------------------------------------------------------------
func MakeDirFiles(path string) (DirFiles, error) {
dir := DirFiles{}
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return dir, errors.New("'" + path + "' does not exists")
} else {
return dir, err
}
}
if !strings.HasSuffix(path, string(filepath.Separator)) {
path += string(filepath.Separator)
}
dir.Path = path
err := dir.Refresh()
if err != nil {
return dir, err
}
return dir, nil
}
//-----------------------------------------------------------------------------
func (dir *DirFiles) Refresh() error {
flist, err := filepath.Glob(dir.Path + "*")
if err != nil {
return err
}
dir.fset = make(map[string]struct{}, len(flist))
for _, fullName := range flist {
base := filepath.Base(fullName)
dir.fset[strings.ToLower(base)] = struct{}{}
}
return nil
}
//-----------------------------------------------------------------------------
func (dir *DirFiles) Count() int {
return len(dir.fset)
}
//-----------------------------------------------------------------------------
func (dir *DirFiles) ListExt(extList ...string) []string {
list := make([]string, 0, len(dir.fset))
for _, ext := range extList {
ext = "." + strings.ToLower(ext)
for fname, _ := range dir.fset {
if filepath.Ext(fname) == ext {
list = append(list, fname)
}
}
}
return list
}
//-----------------------------------------------------------------------------
func (dir *DirFiles) Exists(fname string) bool {
_, found := dir.fset[strings.ToLower(fname)]
return found
}
//-----------------------------------------------------------------------------
// if fname doesn't exists in dir, returns fname without changing
// else tryes to modify name until it become unic
func (dir *DirFiles) GenerateUnicName(fname string) string {
for dir.Exists(fname) {
ext := filepath.Ext(fname)
name := fname[:len(fname)-len(ext)]
if len(name) < 2 {
fname = name + "_0" + ext
continue
}
prefix := name[:len(name)-2]
suffix := name[len(name)-2:]
if suffix[0] == '_' && suffix[1] >= '0' && suffix[1] <= '8' {
fname = prefix + "_" + string(suffix[1]+1) + ext
} else {
fname = name + "_0" + ext
}
}
return fname
}
//-----------------------------------------------------------------------------
// renames file inside dir. Both oldName and newName are short names
// returns newName (may differ from arg)
func (dir *DirFiles) Rename(oldName, newName string) (string, error) {
if !dir.Exists(oldName) {
return "", errors.New("DirFiles.Rename: " +
oldName + " does not exists")
}
newName = dir.GenerateUnicName(newName)
err := os.Rename(dir.Path+oldName, dir.Path+newName)
if err != nil {
return "", err
}
delete(dir.fset, strings.ToLower(oldName))
dir.fset[strings.ToLower(newName)] = struct{}{}
return newName, nil
}
//-----------------------------------------------------------------------------
func (dir *DirFiles) Move(fullFromName, newName string) (string, error) {
newName = dir.GenerateUnicName(newName)
err := os.Rename(fullFromName, dir.Path+newName)
if err != nil {
return "", err
}
dir.fset[strings.ToLower(newName)] = struct{}{}
return newName, nil
}