This file is indexed.

/usr/share/gocode/src/github.com/mitchellh/go-fs/fat/directory.go is in golang-github-mitchellh-go-fs-dev 0.0~git20150611.0.a34c1b9-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package fat

import (
	"fmt"
	"github.com/mitchellh/go-fs"
	"strings"
	"time"
)

// Directory implements fs.Directory and is used to interface with
// a directory on a FAT filesystem.
type Directory struct {
	device     fs.BlockDevice
	dirCluster *DirectoryCluster
	fat        *FAT
}

// DirectoryEntry implements fs.DirectoryEntry and represents a single
// file/folder within a directory in a FAT filesystem. Note that the
// underlying directory entry data structures on the disk may be more
// than one to accomodate for long filenames.
type DirectoryEntry struct {
	dir        *Directory
	lfnEntries []*DirectoryClusterEntry
	entry      *DirectoryClusterEntry

	name string
}

// DecodeDirectoryEntry takes a list of entries, decodes the next full
// DirectoryEntry, and returns the newly created entry, the remaining
// entries, and an error, if there was one.
func DecodeDirectoryEntry(d *Directory, entries []*DirectoryClusterEntry) (*DirectoryEntry, []*DirectoryClusterEntry, error) {
	var lfnEntries []*DirectoryClusterEntry
	var entry *DirectoryClusterEntry
	var name string

	// Skip all the deleted entries
	for len(entries) > 0 && entries[0].deleted {
		entries = entries[1:]
	}

	// Skip the volume ID
	if len(entries) > 0 && entries[0].IsVolumeId() {
		entries = entries[1:]
	}

	if len(entries) == 0 {
		return nil, entries, nil
	}

	// We have a long entry, so we have to traverse to the point where
	// we're done. Also, calculate out the name and such.
	if entries[0].IsLong() {
		lfnEntries := make([]*DirectoryClusterEntry, 0, 3)
		for entries[0].IsLong() {
			lfnEntries = append(lfnEntries, entries[0])
			entries = entries[1:]
		}

		var nameBytes []rune
		nameBytes = make([]rune, 13*len(lfnEntries))
		for i := len(lfnEntries) - 1; i >= 0; i-- {
			for _, char := range lfnEntries[i].longName {
				nameBytes = append(nameBytes, char)
			}
		}

		name = string(nameBytes)
	}

	// Get the short entry
	entry = entries[0]
	entries = entries[1:]

	// If the short entry is deleted, ignore everything
	if entry.deleted {
		return nil, entries, nil
	}

	if name == "" {
		name = strings.TrimSpace(entry.name)
		ext := strings.TrimSpace(entry.ext)
		if ext != "" {
			name = fmt.Sprintf("%s.%s", name, ext)
		}
	}

	result := &DirectoryEntry{
		dir:        d,
		lfnEntries: lfnEntries,
		entry:      entry,
		name:       name,
	}

	return result, entries, nil
}

func (d *DirectoryEntry) Dir() (fs.Directory, error) {
	if !d.IsDir() {
		panic("not a directory")
	}

	dirCluster, err := DecodeDirectoryCluster(
		d.entry.cluster, d.dir.device, d.dir.fat)
	if err != nil {
		return nil, err
	}

	result := &Directory{
		device:     d.dir.device,
		dirCluster: dirCluster,
		fat:        d.dir.fat,
	}

	return result, nil
}

func (d *DirectoryEntry) File() (fs.File, error) {
	if d.IsDir() {
		panic("not a file")
	}

	result := &File{
		chain: &ClusterChain{
			device:       d.dir.device,
			fat:          d.dir.fat,
			startCluster: d.entry.cluster,
		},
		dir:   d.dir,
		entry: d.entry,
	}

	return result, nil
}

func (d *DirectoryEntry) IsDir() bool {
	return (d.entry.attr & AttrDirectory) == AttrDirectory
}

func (d *DirectoryEntry) Name() string {
	return d.name
}

func (d *DirectoryEntry) ShortName() string {
	if d.entry.name == "." || d.entry.name == ".." {
		return d.entry.name
	}

	return fmt.Sprintf("%s.%s", d.entry.name, d.entry.ext)
}

func (d *Directory) AddDirectory(name string) (fs.DirectoryEntry, error) {
	entry, err := d.addEntry(name, AttrDirectory)
	if err != nil {
		return nil, err
	}

	// Create the new directory cluster
	newDirCluster := NewDirectoryCluster(
		entry.entry.cluster, d.dirCluster.startCluster, entry.entry.createTime)

	if err := newDirCluster.WriteToDevice(d.device, d.fat); err != nil {
		return nil, err
	}

	return entry, nil
}

func (d *Directory) AddFile(name string) (fs.DirectoryEntry, error) {
	entry, err := d.addEntry(name, DirectoryAttr(0))
	if err != nil {
		return nil, err
	}

	return entry, nil
}

func (d *Directory) Entries() []fs.DirectoryEntry {
	entries := d.dirCluster.entries
	result := make([]fs.DirectoryEntry, 0, len(entries)/2)
	for len(entries) > 0 {
		var entry *DirectoryEntry
		entry, entries, _ = DecodeDirectoryEntry(d, entries)
		if entry != nil {
			result = append(result, entry)
		}
	}

	return result
}

func (d *Directory) Entry(name string) fs.DirectoryEntry {
	name = strings.ToUpper(name)

	for _, entry := range d.Entries() {
		if strings.ToUpper(entry.Name()) == name {
			return entry
		}
	}

	return nil
}

func (d *Directory) addEntry(name string, attr DirectoryAttr) (*DirectoryEntry, error) {
	name = strings.TrimSpace(name)

	entries := d.Entries()
	usedNames := make([]string, 0, len(entries))
	for _, entry := range entries {
		if strings.ToUpper(entry.Name()) == strings.ToUpper(name) {
			return nil, fmt.Errorf("name already exists: %s", name)
		}

		// Add it to the list of used names
		dirEntry := entry.(*DirectoryEntry)
		usedNames = append(usedNames, dirEntry.ShortName())
	}

	shortName, err := generateShortName(name, usedNames)
	if err != nil {
		return nil, err
	}

	var lfnEntries []*DirectoryClusterEntry
	if shortName != strings.ToUpper(name) {
		lfnEntries, err = NewLongDirectoryClusterEntry(name, shortName)
		if err != nil {
			return nil, err
		}
	}

	// Allocate space for a cluster
	startCluster, err := d.fat.AllocChain()
	if err != nil {
		return nil, err
	}

	createTime := time.Now()

	// Create the entry for the short name
	shortParts := strings.Split(shortName, ".")
	if len(shortParts) == 1 {
		shortParts = append(shortParts, "")
	}

	shortEntry := new(DirectoryClusterEntry)
	shortEntry.attr = attr
	shortEntry.name = shortParts[0]
	shortEntry.ext = shortParts[1]
	shortEntry.cluster = startCluster
	shortEntry.accessTime = createTime
	shortEntry.createTime = createTime
	shortEntry.writeTime = createTime

	// Write the new FAT out
	if err := d.fat.WriteToDevice(d.device); err != nil {
		return nil, err
	}

	// Write the entries out in this directory
	if lfnEntries != nil {
		d.dirCluster.entries = append(d.dirCluster.entries, lfnEntries...)
	}
	d.dirCluster.entries = append(d.dirCluster.entries, shortEntry)

	if err := d.dirCluster.WriteToDevice(d.device, d.fat); err != nil {
		return nil, err
	}

	newEntry := &DirectoryEntry{
		dir:        d,
		lfnEntries: lfnEntries,
		entry:      shortEntry,
	}

	return newEntry, nil
}