-
Notifications
You must be signed in to change notification settings - Fork 0
/
filenames.go
162 lines (140 loc) · 3.93 KB
/
filenames.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
package json_configs
import (
"fmt"
"os"
"path/filepath"
)
type FileDetail struct {
Name string
DistinctName string
FullPath string
PathComponents []string
}
const MAX_ITERATIONS = 100
// Create Parsed{} struct for each filename, validating and formatting names
func DistinctFilenames(filenames []string, errList *[]string) (fileDetails []FileDetail) {
var err error
var file FileDetail
var items, newitems, remainitems []string
var filename, fullpath, dir, name string
var i int
var ok, distinct bool
// Map to determine distinct names
usednames := make(map[string][]string)
// Map to make sure full-path names are unique
fullnameMap := make(map[string]FileDetail)
for _, filename = range filenames {
// Make sure each filename is a valid file
fullpath, err = ValidateFile(filename)
if err != nil {
*errList = append(*errList, fmt.Sprintf("%v", err))
continue
}
// Check for duplicates
file, ok = fullnameMap[fullpath]
if ok {
*errList = append(*errList, fmt.Sprintf("file is duplicate of %s, skipping [%s]", file.Name, filename))
continue
}
// Split base from dir to create initial FileDetail
dir, name = filepath.Split(fullpath)
if len(name) == 0 {
*errList = append(*errList, fmt.Sprintf("invalid file [%s]", err, filename))
continue
}
file = FileDetail{
Name: filename,
DistinctName: name,
FullPath: fullpath,
}
// Make list of file directory components in absolute path
for i = 0; i < MAX_ITERATIONS; i++ {
dir = filepath.Dir(dir)
name = filepath.Base(dir)
file.PathComponents = append(file.PathComponents, name)
if len(dir) == 1 {
break
}
}
fullnameMap[fullpath] = file
// List base names used, to see if names are distinct
items, ok = usednames[file.DistinctName]
if ok {
items = append(items, fullpath)
} else {
items = []string{fullpath}
}
usednames[file.DistinctName] = items
}
for i = 0; i < MAX_ITERATIONS; i++ {
distinct = true
// Generate distinct names across all files
for name, items = range usednames {
// If multiple items, we don't have distinct filenames
if len(items) > 1 {
distinct = false
for _, fullpath = range items {
file, ok = fullnameMap[fullpath]
if !ok {
fmt.Printf("internal error processing file %s\n", fullpath)
continue
}
if len(file.PathComponents) > 0 {
// prepend distinct name with first path component from list
file.DistinctName = filepath.Join(file.PathComponents[0], file.DistinctName)
file.PathComponents = file.PathComponents[1:]
// store new distinct name
newitems, ok = usednames[file.DistinctName]
if ok {
newitems = append(newitems, fullpath)
} else {
newitems = []string{fullpath}
}
usednames[file.DistinctName] = newitems
// Update results
fullnameMap[fullpath] = file
} else {
// exhausted path components, so distinct name remains unchanged
remainitems = append(remainitems, fullpath)
}
}
if len(remainitems) > 0 {
usednames[name] = remainitems
} else {
delete(usednames, name)
}
}
}
// Having checked across all files, each name used only once
if distinct {
break
}
}
// Copy results from fullnameMap
for _, file = range fullnameMap {
fileDetails = append(fileDetails, file)
}
return
}
func ValidateFile(file string) (fullpath string, err error) {
var dirInfo os.FileInfo
fullpath, err = filepath.Abs(file)
if err != nil {
err = fmt.Errorf("%v [%s]", err, file)
return
}
dirInfo, err = os.Stat(fullpath)
if os.IsNotExist(err) {
err = fmt.Errorf("invalid, no such file [%s]", file)
return
} else if os.IsPermission(err) {
err = fmt.Errorf("invalid, permission denied [%s]", file)
return
} else if err != nil {
err = fmt.Errorf("invalid, %v [%s]", err, file)
return
} else if dirInfo.IsDir() {
err = fmt.Errorf("invalid, filename is a directory [%s]", file)
}
return
}