Example #1
0
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
}
Example #2
0
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
}
Example #3
0
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
}