Debian Codegraph @

fat/directory.go

raw symbols SCIP
1 package fat
2
3 import (
4 "fmt"
5 "strings"
6 "time"
7
8 "github.com/mitchellh/go-fs"
9 )
10
11 // Directory implements fs.Directory and is used to interface with
12 // a directory on a FAT filesystem.
13 type Directory struct {
14 device fs.BlockDevice
15 dirCluster *DirectoryCluster
16 fat *FAT
17 }
18
19 // DirectoryEntry implements fs.DirectoryEntry and represents a single
20 // file/folder within a directory in a FAT filesystem. Note that there may be
21 // more than one underlying directory entry data structure on the disk to
22 // account for long filenames.
23 type DirectoryEntry struct {
24 dir *Directory
25 lfnEntries []*DirectoryClusterEntry
26 entry *DirectoryClusterEntry
27
28 name string
29 }
30
31 // DecodeDirectoryEntry takes a list of entries, decodes the next full
32 // DirectoryEntry, and returns the newly created entry, the remaining
33 // entries, and an error, if there was one.
34 func DecodeDirectoryEntry(d *Directory, entries []*DirectoryClusterEntry) (*DirectoryEntry, []*DirectoryClusterEntry, error) {
35 var lfnEntries []*DirectoryClusterEntry
36 var entry *DirectoryClusterEntry
37 var name string
38
39 // Skip all the deleted entries
40 for len(entries) > 0 && entries[0].deleted {
41 entries = entries[1:]
42 }
43
44 // Skip the volume ID
45 if len(entries) > 0 && entries[0].IsVolumeId() {
46 entries = entries[1:]
47 }
48
49 if len(entries) == 0 {
50 return nil, entries, nil
51 }
52
53 // We have a long entry, so we have to traverse to the point where
54 // we're done. Also, calculate out the name and such.
55 if entries[0].IsLong() {
56 lfnEntries := make([]*DirectoryClusterEntry, 0, 3)
57 for entries[0].IsLong() {
58 lfnEntries = append(lfnEntries, entries[0])
59 entries = entries[1:]
60 }
61
62 var nameBytes []rune
63 nameBytes = make([]rune, 13*len(lfnEntries))
64 for i := len(lfnEntries) - 1; i >= 0; i-- {
65 for _, char := range lfnEntries[i].longName {
66 nameBytes = append(nameBytes, char)
67 }
68 }
69
70 name = string(nameBytes)
71 }
72
73 // Get the short entry
74 entry = entries[0]
75 entries = entries[1:]
76
77 // If the short entry is deleted, ignore everything
78 if entry.deleted {
79 return nil, entries, nil
80 }
81
82 if name == "" {
83 name = strings.TrimSpace(entry.name)
84 ext := strings.TrimSpace(entry.ext)
85 if ext != "" {
86 name = fmt.Sprintf("%s.%s", name, ext)
87 }
88 }
89
90 result := &DirectoryEntry{
91 dir: d,
92 lfnEntries: lfnEntries,
93 entry: entry,
94 name: name,
95 }
96
97 return result, entries, nil
98 }
99
100 func (d *DirectoryEntry) Dir() (fs.Directory, error) {
101 if !d.IsDir() {
102 panic("not a directory")
103 }
104
105 dirCluster, err := DecodeDirectoryCluster(
106 d.entry.cluster, d.dir.device, d.dir.fat)
107 if err != nil {
108 return nil, err
109 }
110
111 result := &Directory{
112 device: d.dir.device,
113 dirCluster: dirCluster,
114 fat: d.dir.fat,
115 }
116
117 return result, nil
118 }
119
120 func (d *DirectoryEntry) File() (fs.File, error) {
121 if d.IsDir() {
122 panic("not a file")
123 }
124
125 result := &File{
126 chain: &ClusterChain{
127 device: d.dir.device,
128 fat: d.dir.fat,
129 startCluster: d.entry.cluster,
130 },
131 dir: d.dir,
132 entry: d.entry,
133 }
134
135 return result, nil
136 }
137
138 func (d *DirectoryEntry) IsDir() bool {
139 return (d.entry.attr & AttrDirectory) == AttrDirectory
140 }
141
142 func (d *DirectoryEntry) Name() string {
143 return d.name
144 }
145
146 func (d *DirectoryEntry) ShortName() string {
147 if d.entry.name == "." || d.entry.name == ".." {
148 return d.entry.name
149 }
150
151 return fmt.Sprintf("%s.%s", d.entry.name, d.entry.ext)
152 }
153
154 func (d *Directory) AddDirectory(name string) (fs.DirectoryEntry, error) {
155 entry, err := d.addEntry(name, AttrDirectory)
156 if err != nil {
157 return nil, err
158 }
159
160 // Create the new directory cluster
161 newDirCluster := NewDirectoryCluster(
162 entry.entry.cluster, d.dirCluster.startCluster, entry.entry.createTime)
163
164 if err := newDirCluster.WriteToDevice(d.device, d.fat); err != nil {
165 return nil, err
166 }
167
168 return entry, nil
169 }
170
171 func (d *Directory) AddFile(name string) (fs.DirectoryEntry, error) {
172 entry, err := d.addEntry(name, DirectoryAttr(0))
173 if err != nil {
174 return nil, err
175 }
176
177 return entry, nil
178 }
179
180 func (d *Directory) Entries() []fs.DirectoryEntry {
181 entries := d.dirCluster.entries
182 result := make([]fs.DirectoryEntry, 0, len(entries)/2)
183 for len(entries) > 0 {
184 var entry *DirectoryEntry
185 entry, entries, _ = DecodeDirectoryEntry(d, entries)
186 if entry != nil {
187 result = append(result, entry)
188 }
189 }
190
191 return result
192 }
193
194 func (d *Directory) Entry(name string) fs.DirectoryEntry {
195 name = strings.ToUpper(name)
196
197 for _, entry := range d.Entries() {
198 if strings.ToUpper(entry.Name()) == name {
199 return entry
200 }
201 }
202
203 return nil
204 }
205
206 func (d *Directory) addEntry(name string, attr DirectoryAttr) (*DirectoryEntry, error) {
207 name = strings.TrimSpace(name)
208
209 entries := d.Entries()
210 usedNames := make([]string, 0, len(entries))
211 for _, entry := range entries {
212 if strings.ToUpper(entry.Name()) == strings.ToUpper(name) {
213 return nil, fmt.Errorf("name already exists: %s", name)
214 }
215
216 // Add it to the list of used names
217 dirEntry := entry.(*DirectoryEntry)
218 usedNames = append(usedNames, dirEntry.ShortName())
219 }
220
221 shortName, err := generateShortName(name, usedNames)
222 if err != nil {
223 return nil, err
224 }
225
226 var lfnEntries []*DirectoryClusterEntry
227 if shortName != strings.ToUpper(name) {
228 lfnEntries, err = NewLongDirectoryClusterEntry(name, shortName)
229 if err != nil {
230 return nil, err
231 }
232 }
233
234 // Allocate space for a cluster
235 startCluster, err := d.fat.AllocChain()
236 if err != nil {
237 return nil, err
238 }
239
240 createTime := time.Now()
241
242 // Create the entry for the short name
243 shortParts := strings.Split(shortName, ".")
244 if len(shortParts) == 1 {
245 shortParts = append(shortParts, "")
246 }
247
248 shortEntry := new(DirectoryClusterEntry)
249 shortEntry.attr = attr
250 shortEntry.name = shortParts[0]
251 shortEntry.ext = shortParts[1]
252 shortEntry.cluster = startCluster
253 shortEntry.accessTime = createTime
254 shortEntry.createTime = createTime
255 shortEntry.writeTime = createTime
256
257 // Write the new FAT out
258 if err := d.fat.WriteToDevice(d.device); err != nil {
259 return nil, err
260 }
261
262 // Write the entries out in this directory
263 if lfnEntries != nil {
264 d.dirCluster.entries = append(d.dirCluster.entries, lfnEntries...)
265 }
266 d.dirCluster.entries = append(d.dirCluster.entries, shortEntry)
267
268 if err := d.dirCluster.WriteToDevice(d.device, d.fat); err != nil {
269 return nil, err
270 }
271
272 newEntry := &DirectoryEntry{
273 dir: d,
274 lfnEntries: lfnEntries,
275 entry: shortEntry,
276 }
277
278 return newEntry, nil
279 }