-
Notifications
You must be signed in to change notification settings - Fork 5
/
file.go
418 lines (359 loc) · 10.2 KB
/
file.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
// 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 (
"errors"
"fmt"
"github.com/dankomiocevic/mulifs/musicmgr"
"github.com/dankomiocevic/mulifs/playlistmgr"
"github.com/dankomiocevic/mulifs/store"
"github.com/golang/glog"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"golang.org/x/net/context"
)
// File defines a file in the filesystem structure.
// files can be Songs or .description files.
// Songs are actual songs in the Music Library and
// .description files detail more information about the
// Directory they are located in.
type File struct {
artist string
album string
song string
name string
mPoint string
}
/** This function is used to do nothing to the file
* but to update the Touch time.
*/
func DelayedVoid(f File) error {
return nil
}
func (f *File) Attr(ctx context.Context, a *fuse.Attr) error {
glog.Infof("Entering file Attr with name: %s, Artist: %s and Album: %s.\n", f.name, f.artist, f.album)
if f.name[0] == '.' {
if f.name == ".description" {
descriptionJson, err := store.GetDescription(f.artist, f.album, f.name)
if err != nil {
return err
}
a.Size = uint64(len(descriptionJson))
a.Mode = 0444
if config_params.uid != 0 {
a.Uid = uint32(config_params.uid)
}
if config_params.gid != 0 {
a.Gid = uint32(config_params.gid)
}
} else {
return fuse.EPERM
}
} else {
var songPath string
var err error
if f.artist == "drop" {
songPath, err = store.GetDropFilePath(f.name, f.mPoint)
PushFileItem(*f, nil)
} else if f.artist == "playlists" {
songPath, err = store.GetPlaylistFilePath(f.album, f.name, f.mPoint)
PushFileItem(*f, nil)
} else {
songPath, err = store.GetFilePath(f.artist, f.album, f.name)
}
if err != nil {
glog.Infof("Error getting song path: %s\n", err)
return err
}
r, err := os.Open(songPath)
if err != nil {
glog.Infof("Error opening file: %s\n", err)
return err
}
defer r.Close()
fi, err := r.Stat()
if err != nil {
glog.Infof("Error getting file status: %s\n", err)
return err
}
a.Size = uint64(fi.Size())
a.Mode = 0777
if config_params.uid != 0 {
a.Uid = uint32(config_params.uid)
}
if config_params.gid != 0 {
a.Gid = uint32(config_params.gid)
}
}
return nil
}
var _ = fs.NodeOpener(&File{})
func (f *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
glog.Infof("Entered Open with file name: %s.\n", f.name)
if f.name == ".description" {
return &FileHandle{r: nil, f: f}, nil
}
if f.name[0] == '.' {
return nil, fuse.EPERM
}
if runtime.GOOS == "darwin" {
resp.Flags |= fuse.OpenDirectIO
}
if req.Flags.IsReadOnly() {
glog.Info("Open: File requested is read only.\n")
}
if req.Flags.IsReadWrite() {
glog.Info("Open: File requested is read write.\n")
}
if req.Flags.IsWriteOnly() {
glog.Info("Open: File requested is write only.\n")
}
var err error
var songPath string
if f.artist == "drop" {
songPath, err = store.GetDropFilePath(f.name, f.mPoint)
PushFileItem(*f, DelayedVoid)
} else if f.artist == "playlists" {
songPath, err = store.GetPlaylistFilePath(f.album, f.name, f.mPoint)
PushFileItem(*f, DelayedVoid)
} else {
songPath, err = store.GetFilePath(f.artist, f.album, f.name)
}
if err != nil {
glog.Error(err)
return nil, err
}
r, err := os.OpenFile(songPath, int(req.Flags), 0666)
if err != nil {
return nil, err
}
return &FileHandle{r: r, f: f}, nil
}
type FileHandle struct {
r *os.File
f *File
}
var _ fs.Handle = (*FileHandle)(nil)
// DelayedHandlePlaylistSong handles a dropped file
// inside a playlist and is called by the background
// dispatcher after some time has passed.
func DelayedHandlePlaylistSong(f File) error {
rootPoint := f.mPoint
if rootPoint[len(rootPoint)-1] != '/' {
rootPoint = rootPoint + "/"
}
path := rootPoint + "playlists/" + f.album + "/" + f.name
extension := filepath.Ext(f.name)
if extension != ".mp3" {
os.Remove(path)
return errors.New("File is not an mp3.")
}
src, err := os.Stat(path)
if err != nil || src.IsDir() {
return errors.New("File not found.")
}
err, tags := musicmgr.GetMp3Tags(path)
if err != nil {
os.Remove(path)
return err
}
artist := store.GetCompatibleString(tags.Artist)
album := store.GetCompatibleString(tags.Album)
title := tags.Title
if strings.HasSuffix(title, ".mp3") {
title = title[:len(title)-len(".mp3")]
}
title = store.GetCompatibleString(title) + ".mp3"
newPath, err := store.GetFilePath(artist, album, title)
if err == nil {
var playlistFile playlistmgr.PlaylistFile
playlistFile.Title = title
playlistFile.Artist = artist
playlistFile.Album = album
playlistFile.Path = newPath
err = store.AddFileToPlaylist(playlistFile, f.album)
} else {
err = store.HandleDrop(path, rootPoint)
if err == nil {
newPath, err = store.GetFilePath(artist, album, title)
if err == nil {
var playlistFile playlistmgr.PlaylistFile
playlistFile.Title = title
playlistFile.Artist = artist
playlistFile.Album = album
playlistFile.Path = newPath
err = store.AddFileToPlaylist(playlistFile, f.album)
}
}
}
os.Remove(path)
if err == nil {
err = store.RegeneratePlaylistFile(f.album, rootPoint)
}
return err
}
// DelayedHandleDrop handles a dropped file
// but is called by the background dispatcher
// after some time has passed.
func DelayedHandleDrop(f File) error {
// Get the dropped file path.
rootPoint := f.mPoint
if rootPoint[len(rootPoint)-1] != '/' {
rootPoint = rootPoint + "/"
}
path := rootPoint + "drop/" + f.name
err := store.HandleDrop(path, rootPoint)
fmt.Printf("DelayedHandleDrop: %s\n", path)
if err != nil {
glog.Error(err)
return err
}
return nil
}
var _ fs.HandleReleaser = (*FileHandle)(nil)
func (fh *FileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
if fh.r == nil {
if fh.f.name == ".description" {
glog.Infof("Entered Release: .description file\n")
return nil
}
if fh.f.name[0] == '.' {
return fuse.EPERM
}
}
if fh.r == nil {
glog.Info("Release: There is no file handler.\n")
return fuse.EIO
}
glog.Infof("Releasing the file: %s\n", fh.r.Name())
if fh.f != nil && fh.f.artist == "drop" {
glog.Infof("Entered Release dropping the song: %s\n", fh.f.name)
ret_val := fh.r.Close()
PushFileItem(*fh.f, DelayedHandleDrop)
return ret_val
}
if fh.f != nil && fh.f.artist == "playlists" {
glog.Infof("Entered Release with playlist song: %s\n", fh.f.name)
ret_val := fh.r.Close()
PushFileItem(*fh.f, DelayedHandlePlaylistSong)
return ret_val
}
// This is not an music file or this is a strange situation.
if fh.f == nil || len(fh.f.artist) < 1 || len(fh.f.album) < 1 {
glog.Info("Entered Release: Artist or Album not set.\n")
return fh.r.Close()
}
glog.Infof("Entered Release: Artist: %s, Album: %s, Song: %s\n", fh.f.artist, fh.f.album, fh.f.name)
ret_val := fh.r.Close()
extension := filepath.Ext(fh.f.name)
songPath, err := store.GetFilePath(fh.f.artist, fh.f.album, fh.f.name)
if err != nil {
return err
}
if extension == ".mp3" {
//TODO: Use the correct artist and album
musicmgr.SetMp3Tags(fh.f.artist, fh.f.album, fh.f.song, songPath)
}
return ret_val
}
var _ = fs.HandleReader(&FileHandle{})
func (fh *FileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
glog.Infof("Entered Read.\n")
//TODO: Check if we need to add something here for playlists and drop directories.
if fh.r == nil {
if fh.f.name == ".description" {
glog.Info("Reading description file\n")
if len(fh.f.artist) < 1 {
return fuse.ENOENT
}
_, err := store.GetArtistPath(fh.f.artist)
if err != nil {
return err
}
if len(fh.f.album) > 1 {
_, err = store.GetAlbumPath(fh.f.artist, fh.f.album)
if err != nil {
return err
}
}
descBytes, err := store.GetDescription(fh.f.artist, fh.f.album, fh.f.name)
if err != nil {
return err
}
resp.Data = []byte(descBytes)
return nil
}
glog.Info("There is no file handler.\n")
return fuse.EIO
}
glog.Infof("Reading file: %s.\n", fh.r.Name())
if _, err := fh.r.Seek(req.Offset, 0); err != nil {
return err
}
buf := make([]byte, req.Size)
n, err := fh.r.Read(buf)
resp.Data = buf[:n]
if err != nil && err != io.EOF {
glog.Error(err)
return err
}
return nil
}
var _ = fs.HandleWriter(&FileHandle{})
func (fh *FileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
glog.Infof("Entered Write\n")
//TODO: Check if we need to add something here for playlists and drop directories.
if fh.r == nil {
if fh.f.name == ".description" {
glog.Errorf("Not allowed to write description file.\n")
//TODO: Allow to write description
return nil
}
return fuse.EIO
}
glog.Infof("Writing file: %s.\n", fh.r.Name())
if _, err := fh.r.Seek(req.Offset, 0); err != nil {
return err
}
n, err := fh.r.Write(req.Data)
resp.Size = n
return err
}
var _ = fs.HandleFlusher(&FileHandle{})
func (fh *FileHandle) Flush(ctx context.Context, req *fuse.FlushRequest) error {
if fh.f != nil {
glog.Infof("Entered Flush with Song: %s, Artist: %s and Album: %s\n", fh.f.name, fh.f.artist, fh.f.album)
}
if fh.r == nil {
glog.Infof("There is no file handler.\n")
return fuse.EIO
}
glog.Infof("Entered Flush with path: %s\n", fh.r.Name())
fh.r.Sync()
return nil
}
var _ = fs.NodeSetattrer(&File{})
func (f *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
glog.Infof("Entered SetAttr with Song: %s, Artist: %s and Album: %s\n", f.name, f.artist, f.album)
if req.Valid.Size() {
glog.Infof("New size: %d\n", int(req.Size))
}
return nil
}