-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdir.go
625 lines (526 loc) · 13.8 KB
/
dir.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
// Copyright 2016 Danko Miocevic. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Author: Danko Miocevic
package main
import (
"github.com/dankomiocevic/mulifs/store"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/golang/glog"
"golang.org/x/net/context"
)
// Dir struct specifies a Directory in the
// filesystem, the Directories can be Artist or Albums.
// The root Directory that contains all the Artists
// is also a Directory.
type Dir struct {
fs *FS
artist string
album string
mPoint string
}
var _ = fs.Node(&Dir{})
func (d *Dir) Attr(ctx context.Context, a *fuse.Attr) error {
glog.Infof("Entered Attr dir: Artist: %s, Album: %s\n", d.artist, d.album)
a.Mode = os.ModeDir | 0777
if config_params.uid != 0 {
a.Uid = uint32(config_params.uid)
}
if config_params.gid != 0 {
a.Gid = uint32(config_params.gid)
}
a.Size = 4096
return nil
}
var dirDirs = []fuse.Dirent{
{Name: "drop", Type: fuse.DT_Dir},
{Name: "playlists", Type: fuse.DT_Dir},
}
var _ = fs.NodeStringLookuper(&Dir{})
func (d *Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
glog.Infof("Entering Lookup with artist: %s, album: %s and name: %s.\n", d.artist, d.album, name)
if name == ".description" {
return &File{artist: d.artist, album: d.album, song: name, name: name, mPoint: d.mPoint}, nil
}
if name[0] == '.' {
return nil, fuse.EIO
}
if len(d.artist) < 1 {
if name == "drop" {
return &Dir{fs: d.fs, artist: "drop", album: "", mPoint: d.mPoint}, nil
}
if name == "playlists" {
return &Dir{fs: d.fs, artist: "playlists", album: "", mPoint: d.mPoint}, nil
}
_, err := store.GetArtistPath(name)
if err != nil {
glog.Info(err)
return nil, err
}
return &Dir{fs: d.fs, artist: name, album: "", mPoint: d.mPoint}, nil
}
if len(d.album) < 1 && d.artist != "drop" && d.artist != "playlists" {
_, err := store.GetAlbumPath(d.artist, name)
if err != nil {
glog.Info(err)
return nil, err
}
return &Dir{fs: d.fs, artist: d.artist, album: name, mPoint: d.mPoint}, nil
}
var err error
if d.artist == "drop" {
_, err = store.GetDropFilePath(name, d.mPoint)
if err != nil {
glog.Info(err)
return nil, fuse.ENOENT
}
} else if d.artist == "playlists" {
if len(d.album) < 1 {
_, err = store.GetPlaylistPath(name)
if err != nil {
glog.Info(err)
return nil, fuse.ENOENT
}
return &Dir{fs: d.fs, artist: d.artist, album: name, mPoint: d.mPoint}, nil
} else {
_, err = store.GetPlaylistFilePath(d.album, name, d.mPoint)
if err != nil {
glog.Info(err)
return nil, fuse.ENOENT
}
}
} else {
_, err = store.GetFilePath(d.artist, d.album, name)
if err != nil {
glog.Info(err)
return nil, err
}
}
extension := filepath.Ext(name)
songName := name[:len(name)-len(extension)]
return &File{artist: d.artist, album: d.album, song: songName, name: name, mPoint: d.mPoint}, nil
}
var _ = fs.HandleReadDirAller(&Dir{})
func (d *Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
glog.Infof("Entering ReadDirAll\n")
if len(d.artist) < 1 {
a, err := store.ListArtists()
if err != nil {
return nil, fuse.ENOENT
}
for _, v := range dirDirs {
a = append(a, v)
}
return a, nil
}
if d.artist == "drop" {
if len(d.album) > 0 {
return nil, fuse.ENOENT
}
rootPoint := d.mPoint
if rootPoint[len(rootPoint)-1] != '/' {
rootPoint = rootPoint + "/"
}
path := rootPoint + "drop"
// Check if the drop directory exists
src, err := os.Stat(path)
if err != nil {
return nil, nil
}
// Check if it is a directory
if !src.IsDir() {
return nil, nil
}
var a []fuse.Dirent
files, _ := ioutil.ReadDir(path)
for _, f := range files {
var node fuse.Dirent
node.Name = f.Name()
node.Type = fuse.DT_File
a = append(a, node)
}
return a, nil
}
if d.artist == "playlists" {
if len(d.album) < 1 {
a, err := store.ListPlaylists()
if err != nil {
return nil, fuse.ENOENT
}
return a, nil
}
a, err := store.ListPlaylistSongs(d.album, d.mPoint)
if err != nil {
return nil, fuse.ENOENT
}
return a, nil
}
if len(d.album) < 1 {
a, err := store.ListAlbums(d.artist)
if err != nil {
return nil, fuse.ENOENT
}
return a, nil
}
a, err := store.ListSongs(d.artist, d.album)
if err != nil {
return nil, fuse.ENOENT
}
return a, nil
}
var _ = fs.NodeMkdirer(&Dir{})
func (d *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
name := req.Name
glog.Infof("Entering mkdir with name: %s.\n", name)
// Do not allow creating directories starting with dot
if name[0] == '.' {
glog.Info("Names starting with dot are not allowed.")
return nil, fuse.EPERM
}
if d.mPoint[len(d.mPoint)-1] != '/' {
d.mPoint = d.mPoint + "/"
}
if len(d.artist) < 1 {
glog.Info("Creating an Artist.")
ret, err := store.CreateArtist(name)
if err != nil {
glog.Infof("Error creating artist: %s\n", err)
return nil, err
}
path := d.mPoint + ret
err = os.MkdirAll(path, 0777)
if err != nil {
glog.Infof("Error creating artist folder: %s\n", err)
return nil, fuse.EIO
}
return &Dir{fs: d.fs, artist: ret, album: "", mPoint: d.mPoint}, nil
}
if d.artist == "drop" {
return nil, fuse.EIO
}
if d.artist == "playlists" {
if len(d.album) < 1 {
ret, err := store.CreatePlaylist(name, d.mPoint)
if err != nil {
glog.Infof("Error creating playlist: %s\n", err)
return nil, err
}
err = store.RegeneratePlaylistFile(ret, d.mPoint)
if err != nil {
glog.Infof("Error regenerating playlist: %s\n", err)
return nil, err
}
return &Dir{fs: d.fs, artist: "playlists", album: ret, mPoint: d.mPoint}, nil
}
return nil, fuse.EPERM
}
if len(d.album) < 1 {
glog.Infof("Creating album: %s in artist: %s.\n", d.artist, name)
ret, err := store.CreateAlbum(d.artist, name)
if err != nil {
return nil, err
}
path := d.mPoint + d.artist + "/" + ret
err = os.MkdirAll(path, 0777)
if err != nil {
glog.Infof("Error creating artist folder: %s\n", err)
return nil, fuse.EIO
}
return &Dir{fs: d.fs, artist: d.artist, album: ret, mPoint: d.mPoint}, nil
}
return nil, fuse.EIO
}
var _ = fs.NodeCreater(&Dir{})
func (d *Dir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
glog.Infof("Entered Create Dir\n")
if req.Flags.IsReadOnly() {
glog.Info("Create: File requested is read only.\n")
}
if req.Flags.IsReadWrite() {
glog.Info("Create: File requested is read write.\n")
}
if req.Flags.IsWriteOnly() {
glog.Info("Create: File requested is write only.\n")
}
if runtime.GOOS == "darwin" {
resp.Flags |= fuse.OpenDirectIO
}
if d.artist == "drop" {
if len(d.album) > 0 {
glog.Info("Subdirectories are not allowed in drop folder.")
return nil, nil, fuse.EIO
}
rootPoint := d.mPoint
if rootPoint[len(rootPoint)-1] != '/' {
rootPoint = rootPoint + "/"
}
name := req.Name
path := rootPoint + "drop/"
extension := filepath.Ext(name)
if extension != ".mp3" {
glog.Info("Only mp3 files are allowed.")
return nil, nil, fuse.EIO
}
// Check if the drop directory exists
src, err := os.Stat(path)
if err != nil || !src.IsDir() {
err = os.MkdirAll(path, 0777)
if err != nil {
glog.Infof("Cannot create dir: %s\n", err)
return nil, nil, err
}
}
fi, err := os.Create(path + name)
if err != nil {
glog.Infof("Cannot create file: %s\n", err)
return nil, nil, err
}
keyName := name[:len(name)-len(extension)]
f := &File{
artist: d.artist,
album: d.album,
song: keyName,
name: name,
mPoint: d.mPoint,
}
if fi != nil {
glog.Infof("Returning file handle for: %s.\n", fi.Name())
}
return f, &FileHandle{r: fi, f: f}, nil
}
if d.artist == "playlists" {
if len(d.album) < 1 {
glog.Info("Files are not allowed outside playlists.")
return nil, nil, fuse.EIO
}
rootPoint := d.mPoint
if rootPoint[len(rootPoint)-1] != '/' {
rootPoint = rootPoint + "/"
}
name := req.Name
path := rootPoint + "playlists/" + d.album
extension := filepath.Ext(name)
if extension != ".mp3" {
glog.Info("Only mp3 files are allowed.")
return nil, nil, fuse.EIO
}
// Check if the playlist drop directory exists
src, err := os.Stat(path)
if err != nil || !src.IsDir() {
err = os.MkdirAll(path, 0777)
if err != nil {
glog.Infof("Cannot create dir: %s\n", err)
return nil, nil, err
}
}
fi, err := os.Create(path + "/" + name)
if err != nil {
glog.Infof("Cannot create file: %s\n", err)
return nil, nil, err
}
keyName := name[:len(name)-len(extension)]
f := &File{
artist: d.artist,
album: d.album,
song: keyName,
name: name,
mPoint: d.mPoint,
}
if fi != nil {
glog.Infof("Returning file handle for: %s.\n", fi.Name())
}
return f, &FileHandle{r: fi, f: f}, nil
}
if len(d.artist) < 1 || len(d.album) < 1 {
return nil, nil, fuse.EPERM
}
nameRaw := req.Name
if nameRaw[0] == '.' {
glog.Info("Cannot create files starting with dot.")
return nil, nil, fuse.EPERM
}
rootPoint := d.mPoint
if rootPoint[len(rootPoint)-1] != '/' {
rootPoint = rootPoint + "/"
}
path := rootPoint + d.artist + "/" + d.album + "/"
name, err := store.CreateSong(d.artist, d.album, nameRaw, path)
if err != nil {
glog.Info("Error creating song.")
return nil, nil, fuse.EPERM
}
err = os.MkdirAll(path, 0777)
if err != nil {
glog.Info("Cannot create folder.")
return nil, nil, err
}
fi, err := os.Create(path + name)
if err != nil {
glog.Infof("Cannot create file: %s\n", err)
return nil, nil, err
}
extension := filepath.Ext(name)
keyName := name[:len(name)-len(extension)]
f := &File{
artist: d.artist,
album: d.album,
song: keyName,
name: name,
mPoint: d.mPoint,
}
if fi != nil {
glog.Infof("Returning file handle for: %s.\n", fi.Name())
}
return f, &FileHandle{r: fi, f: f}, nil
}
var _ = fs.NodeRemover(&Dir{})
func (d *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
//TODO: Correct this function to work with drop folder.
name := req.Name
glog.Infof("Entered Remove function with Artist: %s, Album: %s and Name: %s.\n", d.artist, d.album, name)
if name == ".description" {
return nil
}
if name[0] == '.' {
return fuse.EIO
}
if req.Dir {
if len(name) < 1 {
return fuse.EIO
}
if len(d.artist) < 1 {
if name == "drop" {
return fuse.EIO
}
if name == "playlists" {
return fuse.EIO
}
err := store.DeleteArtist(name, d.mPoint)
if err != nil {
return fuse.EIO
}
return nil
}
if d.artist == "playlists" {
store.DeletePlaylist(name, d.mPoint)
return nil
}
err := store.DeleteAlbum(d.artist, name, d.mPoint)
if err != nil {
return fuse.EIO
}
return nil
} else {
if len(d.artist) < 1 || len(d.album) < 1 {
return fuse.EIO
}
fullPath, err := store.GetFilePath(d.artist, d.album, name)
if err != nil {
return fuse.EIO
}
if d.artist == "playlists" {
err := store.DeletePlaylistSong(d.album, name, false)
if err != nil {
return fuse.EIO
}
}
err = store.DeleteSong(d.artist, d.album, name, d.mPoint)
if err != nil {
return fuse.EIO
}
//TODO: Check if there are no more files in the folder
// and delete the folder.
err = os.Remove(fullPath)
if err != nil {
return err
}
return nil
}
}
var _ = fs.NodeRenamer(&Dir{})
func (d *Dir) Rename(ctx context.Context, r *fuse.RenameRequest, newDir fs.Node) error {
var newD *Dir
newD = newDir.(*Dir)
glog.Infof("Renaming: OldName: %s, NewName: %s, newDir: %s/%s\n", r.OldName, r.NewName, newD.artist, newD.album)
if d.mPoint[len(d.mPoint)-1] != '/' {
d.mPoint = d.mPoint + "/"
}
path := d.mPoint + d.artist + "/" + d.album + "/" + r.OldName
if r.OldName == ".description" || r.NewName == ".description" {
return fuse.EPERM
}
if r.NewName[0] == '.' || r.OldName[0] == '.' {
glog.Info("Names starting with dot are not allowed.")
return fuse.EPERM
}
if len(d.artist) < 1 {
glog.Info("Changing artist name.")
if len(newD.artist) > 0 {
return fuse.EPERM
}
err := store.MoveArtist(r.OldName, r.NewName, d.mPoint)
return err
}
if d.artist == "drop" {
glog.Info("Cannot rename inside drop folder.")
return fuse.EPERM
}
if d.artist == "playlists" {
glog.Info("Rename inside playlists folder.")
var err error
if len(d.album) < 1 {
glog.Info("Rename playlist name.")
var newName string
if len(newD.album) < 1 {
newName, err = store.RenamePlaylist(r.OldName, r.NewName, d.mPoint)
} else {
newName, err = store.RenamePlaylist(r.OldName, newD.album, d.mPoint)
}
if err != nil {
return fuse.EIO
}
err = store.RegeneratePlaylistFile(newName, d.mPoint)
if err != nil {
return fuse.EIO
}
return nil
}
_, err = store.RenamePlaylistSong(d.album, r.OldName, r.NewName, d.mPoint)
if err != nil {
return fuse.EIO
}
err = store.RegeneratePlaylistFile(d.album, d.mPoint)
if err != nil {
return fuse.EIO
}
return nil
}
if len(d.album) < 1 {
glog.Info("Moving album")
if len(newD.album) > 0 {
return fuse.EPERM
}
err := store.MoveAlbum(d.artist, r.OldName, newD.artist, r.NewName, d.mPoint)
return err
}
_, err := store.MoveSongs(d.artist, d.album, r.OldName, newD.artist, newD.album, r.NewName, path, d.mPoint)
if err != nil {
return fuse.EIO
}
return nil
}