This repository has been archived by the owner on Nov 20, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
subDir.go
297 lines (241 loc) · 7.21 KB
/
subDir.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package main
import (
"fmt"
"log"
"os"
"strings"
"syscall"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/mdlayher/goset"
)
// SubDir represents a directory in the filesystem
type SubDir struct {
ID int64
Root bool
}
// Attr retrives the attributes for this SubDir
func (SubDir) Attr() fuse.Attr {
return fuse.Attr{
Mode: os.ModeDir | 0555,
}
}
// Create does nothing, because subfs is read-only
func (SubDir) Create(req *fuse.CreateRequest, res *fuse.CreateResponse, intr fs.Intr) (fs.Node, fs.Handle, fuse.Error) {
return nil, nil, fuse.Errno(syscall.EROFS)
}
// Fsync does nothing, because subfs is read-only
func (SubDir) Fsync(req *fuse.FsyncRequest, intr fs.Intr) fuse.Error {
return fuse.Errno(syscall.EROFS)
}
// Link does nothing, because subfs is read-only
func (SubDir) Link(req *fuse.LinkRequest, node fs.Node, intr fs.Intr) (fs.Node, fuse.Error) {
return nil, fuse.Errno(syscall.EROFS)
}
// Lookup scans the current directory for matching files or directories
func (d SubDir) Lookup(name string, intr fs.Intr) (fs.Node, fuse.Error) {
// Lookup directory by name
if dir, ok := nameToDir[name]; ok {
return dir, nil
}
// Lookup file by name
if f, ok := nameToFile[name]; ok {
return f, nil
}
// File not found
return nil, fuse.ENOENT
}
// ReadDir returns a list of directory entries depending on the current path
func (d SubDir) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
// List of directory entries to return
directories := make([]fuse.Dirent, 0)
// If at root of filesystem, fetch indexes
if d.Root {
// If empty, wait for indexes to be available
if len(indexCache) == 0 {
<-indexChan
}
// Get index from cache
index := indexCache
// Iterate indices
for _, i := range index {
// Iterate all artists
for _, a := range i.Artist {
// Map artist's name to directory
nameToDir[a.Name] = SubDir{
ID: a.ID,
Root: false,
}
// Create a directory entry
dir := fuse.Dirent{
Name: a.Name,
Type: fuse.DT_Dir,
}
// Append entry
directories = append(directories, dir)
}
}
return directories, nil
}
// Not at filesystem root, so get this directory's contents
content, err := subsonic.GetMusicDirectory(d.ID)
if err != nil {
log.Printf("subfs: failed to retrieve directory %d: %s", d.ID, err.Error())
return nil, fuse.ENOENT
}
// Check for unique, available cover art IDs
coverArt := set.New()
// List of bad characters which should be replaced in filenames
badChars := []string{"/", "\\"}
// Iterate all returned directories
for _, dir := range content.Directories {
// Check for any characters which may cause trouble with filesystem display
for _, b := range badChars {
dir.Title = strings.Replace(dir.Title, b, "_", -1)
}
// Create a directory entry
entry := fuse.Dirent{
Name: dir.Title,
Type: fuse.DT_Dir,
}
// Add SubDir directory to lookup map
nameToDir[dir.Title] = SubDir{
ID: dir.ID,
Root: false,
}
// Check for cover art
coverArt.Add(dir.CoverArt)
// Append to list
directories = append(directories, entry)
}
// Iterate all returned audio
for _, a := range content.Audio {
// Check for lossless and lossy transcode
transcodes := []struct {
suffix string
size int64
}{
{a.Suffix, a.Size},
{a.TranscodedSuffix, 0},
}
for _, t := range transcodes {
// If suffix is empty (source is lossy), skip this file
if t.suffix == "" {
continue
}
// Mark file as lossless by default
lossless := true
// If size is empty (transcode to lossy), estimate it and mark as lossy
if t.size == 0 {
lossless = false
// Since we have no idea what Subsonic's transcoding settings are, we will estimate
// using MP3 CBR 320 as our benchmark, being that it will likely over-estimate
// Thanks: http://www.jeffreysward.com/editorials/mp3size.htm
t.size = ((a.DurationRaw * 320) / 8) * 1024
}
// Predefined audio filename format
audioFormat := fmt.Sprintf("%02d - %s - %s.%s", a.Track, a.Artist, a.Title, t.suffix)
// Check for any characters which may cause trouble with filesystem display
for _, b := range badChars {
audioFormat = strings.Replace(audioFormat, b, "_", -1)
}
// Create a directory entry
dir := fuse.Dirent{
Name: audioFormat,
Type: fuse.DT_File,
}
// Add SubFile file to lookup map
nameToFile[dir.Name] = SubFile{
ID: a.ID,
Created: a.Created,
FileName: audioFormat,
IsVideo: false,
Lossless: lossless,
Size: t.size,
}
// Check for cover art
coverArt.Add(a.CoverArt)
// Append to list
directories = append(directories, dir)
}
}
// Iterate all returned video
for _, v := range content.Video {
// Predefined video filename format
videoFormat := fmt.Sprintf("%s.%s", v.Title, v.Suffix)
// Check for any characters which may cause trouble with filesystem display
for _, b := range badChars {
videoFormat = strings.Replace(videoFormat, b, "_", -1)
}
// Create a directory entry
dir := fuse.Dirent{
Name: videoFormat,
Type: fuse.DT_File,
}
// Add SubFile file to lookup map
nameToFile[dir.Name] = SubFile{
ID: v.ID,
Created: v.Created,
FileName: videoFormat,
Size: v.Size,
IsVideo: true,
}
// Check for cover art
coverArt.Add(v.CoverArt)
// Append to list
directories = append(directories, dir)
}
// Iterate all cover art
for _, e := range coverArt.Enumerate() {
// Type-hint to int64
c := e.(int64)
coverArtFormat := fmt.Sprintf("%d.jpg", c)
// Create a directory entry
dir := fuse.Dirent{
Name: coverArtFormat,
Type: fuse.DT_File,
}
// Add SubFile file to lookup map
nameToFile[dir.Name] = SubFile{
ID: c,
FileName: coverArtFormat,
IsArt: true,
}
// Append to list
directories = append(directories, dir)
}
// Return all directory entries
return directories, nil
}
// Mkdir does nothing, because subfs is read-only
func (SubDir) Mkdir(req *fuse.MkdirRequest, intr fs.Intr) (fs.Node, fuse.Error) {
return nil, fuse.Errno(syscall.EROFS)
}
// Mknod does nothing, because subfs is read-only
func (SubDir) Mknod(req *fuse.MknodRequest, intr fs.Intr) (fs.Node, fuse.Error) {
return nil, fuse.Errno(syscall.EROFS)
}
// Remove does nothing, because subfs is read-only
func (SubDir) Remove(req *fuse.RemoveRequest, intr fs.Intr) fuse.Error {
return fuse.Errno(syscall.EROFS)
}
// Removexattr does nothing, because subfs is read-only
func (SubDir) Removexattr(req *fuse.RemovexattrRequest, intr fs.Intr) fuse.Error {
return fuse.Errno(syscall.EROFS)
}
// Rename does nothing, because subfs is read-only
func (SubDir) Rename(req *fuse.RenameRequest, node fs.Node, intr fs.Intr) fuse.Error {
return fuse.Errno(syscall.EROFS)
}
// Setattr does nothing, because subfs is read-only
func (SubDir) Setattr(req *fuse.SetattrRequest, res *fuse.SetattrResponse, intr fs.Intr) fuse.Error {
return fuse.Errno(syscall.EROFS)
}
// Setxattr does nothing, because subfs is read-only
func (SubDir) Setxattr(req *fuse.SetxattrRequest, intr fs.Intr) fuse.Error {
return fuse.Errno(syscall.EROFS)
}
// Symlink does nothing, because subfs is read-only
func (SubDir) Symlink(req *fuse.SymlinkRequest, intr fs.Intr) (fs.Node, fuse.Error) {
return nil, fuse.Errno(syscall.EROFS)
}