示例#1
0
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
}
示例#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
}