2014-02-06 07:29:26 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2014 The Music Player Daemon Project
|
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "NfsInputPlugin.hxx"
|
2014-03-15 15:29:10 +01:00
|
|
|
#include "../AsyncInputStream.hxx"
|
2014-02-06 07:29:26 +01:00
|
|
|
#include "../InputPlugin.hxx"
|
2014-02-08 14:24:00 +01:00
|
|
|
#include "lib/nfs/Domain.hxx"
|
2014-03-15 15:29:10 +01:00
|
|
|
#include "lib/nfs/Glue.hxx"
|
|
|
|
#include "lib/nfs/FileReader.hxx"
|
|
|
|
#include "util/HugeAllocator.hxx"
|
2014-02-06 07:29:26 +01:00
|
|
|
#include "util/StringUtil.hxx"
|
|
|
|
#include "util/Error.hxx"
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <nfsc/libnfs.h>
|
|
|
|
}
|
|
|
|
|
2014-04-23 12:53:19 +02:00
|
|
|
#include <string.h>
|
2014-02-06 07:29:26 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2014-03-15 15:29:10 +01:00
|
|
|
/**
|
|
|
|
* Do not buffer more than this number of bytes. It should be a
|
|
|
|
* reasonable limit that doesn't make low-end machines suffer too
|
|
|
|
* much, but doesn't cause stuttering on high-latency lines.
|
|
|
|
*/
|
|
|
|
static const size_t NFS_MAX_BUFFERED = 512 * 1024;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resume the stream at this number of bytes after it has been paused.
|
|
|
|
*/
|
|
|
|
static const size_t NFS_RESUME_AT = 384 * 1024;
|
|
|
|
|
|
|
|
class NfsInputStream final : public AsyncInputStream, NfsFileReader {
|
|
|
|
uint64_t next_offset;
|
2014-02-06 07:29:26 +01:00
|
|
|
|
|
|
|
public:
|
2014-05-11 16:02:57 +02:00
|
|
|
NfsInputStream(const char *_uri,
|
|
|
|
Mutex &_mutex, Cond &_cond,
|
2014-03-15 15:29:10 +01:00
|
|
|
void *_buffer)
|
|
|
|
:AsyncInputStream(_uri, _mutex, _cond,
|
|
|
|
_buffer, NFS_MAX_BUFFERED,
|
|
|
|
NFS_RESUME_AT) {}
|
2014-02-06 07:29:26 +01:00
|
|
|
|
2014-03-15 15:29:10 +01:00
|
|
|
virtual ~NfsInputStream() {
|
|
|
|
DeferClose();
|
2014-02-06 07:29:26 +01:00
|
|
|
}
|
|
|
|
|
2014-03-15 15:29:10 +01:00
|
|
|
bool Open(Error &error) {
|
|
|
|
assert(!IsReady());
|
2014-05-11 17:14:49 +02:00
|
|
|
|
2014-03-15 15:29:10 +01:00
|
|
|
return NfsFileReader::Open(GetURI(), error);
|
2014-02-06 07:29:26 +01:00
|
|
|
}
|
|
|
|
|
2014-03-15 15:29:10 +01:00
|
|
|
private:
|
|
|
|
bool DoRead();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/* virtual methods from AsyncInputStream */
|
|
|
|
virtual void DoResume() override;
|
|
|
|
virtual void DoSeek(offset_type new_offset) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
/* virtual methods from NfsFileReader */
|
|
|
|
void OnNfsFileOpen(uint64_t size) override;
|
|
|
|
void OnNfsFileRead(const void *data, size_t size) override;
|
|
|
|
void OnNfsFileError(Error &&error) override;
|
2014-05-11 17:14:49 +02:00
|
|
|
};
|
2014-02-06 07:29:26 +01:00
|
|
|
|
2014-03-15 15:29:10 +01:00
|
|
|
bool
|
|
|
|
NfsInputStream::DoRead()
|
2014-05-11 17:14:49 +02:00
|
|
|
{
|
2014-03-15 15:29:10 +01:00
|
|
|
assert(NfsFileReader::IsIdle());
|
|
|
|
|
|
|
|
int64_t remaining = size - next_offset;
|
|
|
|
if (remaining <= 0)
|
|
|
|
return true;
|
|
|
|
|
2014-06-21 14:06:31 +02:00
|
|
|
const size_t buffer_space = GetBufferSpace();
|
|
|
|
if (buffer_space == 0) {
|
2014-03-15 15:29:10 +01:00
|
|
|
Pause();
|
|
|
|
return true;
|
2014-02-06 07:29:26 +01:00
|
|
|
}
|
|
|
|
|
2014-06-21 14:06:31 +02:00
|
|
|
size_t nbytes = std::min<size_t>(std::min<uint64_t>(remaining, 32768),
|
|
|
|
buffer_space);
|
2014-05-11 17:14:49 +02:00
|
|
|
|
2014-03-15 15:29:10 +01:00
|
|
|
mutex.unlock();
|
|
|
|
Error error;
|
|
|
|
bool success = NfsFileReader::Read(next_offset, nbytes, error);
|
|
|
|
mutex.lock();
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
PostponeError(std::move(error));
|
2014-05-11 17:14:49 +02:00
|
|
|
return false;
|
2014-02-06 07:29:26 +01:00
|
|
|
}
|
2014-05-11 17:14:49 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-06 07:29:26 +01:00
|
|
|
|
2014-03-15 15:29:10 +01:00
|
|
|
void
|
|
|
|
NfsInputStream::DoResume()
|
|
|
|
{
|
|
|
|
assert(NfsFileReader::IsIdle());
|
|
|
|
|
|
|
|
DoRead();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NfsInputStream::DoSeek(offset_type new_offset)
|
|
|
|
{
|
|
|
|
mutex.unlock();
|
|
|
|
NfsFileReader::CancelRead();
|
|
|
|
mutex.lock();
|
|
|
|
|
|
|
|
next_offset = offset = new_offset;
|
|
|
|
SeekDone();
|
|
|
|
DoRead();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NfsInputStream::OnNfsFileOpen(uint64_t _size)
|
|
|
|
{
|
|
|
|
const ScopeLock protect(mutex);
|
|
|
|
|
|
|
|
size = _size;
|
|
|
|
seekable = true;
|
|
|
|
next_offset = 0;
|
|
|
|
SetReady();
|
|
|
|
DoRead();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NfsInputStream::OnNfsFileRead(const void *data, size_t data_size)
|
|
|
|
{
|
|
|
|
const ScopeLock protect(mutex);
|
|
|
|
assert(!IsBufferFull());
|
|
|
|
assert(IsBufferFull() == (GetBufferSpace() == 0));
|
|
|
|
AppendToBuffer(data, data_size);
|
|
|
|
|
|
|
|
next_offset += data_size;
|
|
|
|
|
|
|
|
DoRead();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NfsInputStream::OnNfsFileError(Error &&error)
|
|
|
|
{
|
|
|
|
const ScopeLock protect(mutex);
|
|
|
|
postponed_error = std::move(error);
|
|
|
|
|
|
|
|
if (IsSeekPending())
|
|
|
|
SeekDone();
|
|
|
|
else if (!IsReady())
|
|
|
|
SetReady();
|
|
|
|
}
|
|
|
|
|
2014-02-06 07:29:26 +01:00
|
|
|
/*
|
|
|
|
* InputPlugin methods
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-03-15 15:29:10 +01:00
|
|
|
static InputPlugin::InitResult
|
|
|
|
input_nfs_init(const config_param &, Error &)
|
|
|
|
{
|
|
|
|
nfs_init();
|
|
|
|
return InputPlugin::InitResult::SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
input_nfs_finish()
|
|
|
|
{
|
|
|
|
nfs_finish();
|
|
|
|
}
|
|
|
|
|
2014-02-06 07:29:26 +01:00
|
|
|
static InputStream *
|
|
|
|
input_nfs_open(const char *uri,
|
|
|
|
Mutex &mutex, Cond &cond,
|
|
|
|
Error &error)
|
|
|
|
{
|
|
|
|
if (!StringStartsWith(uri, "nfs://"))
|
|
|
|
return nullptr;
|
|
|
|
|
2014-03-15 15:29:10 +01:00
|
|
|
void *buffer = HugeAllocate(NFS_MAX_BUFFERED);
|
|
|
|
if (buffer == nullptr) {
|
|
|
|
error.Set(nfs_domain, "Out of memory");
|
2014-02-06 07:29:26 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-03-15 15:29:10 +01:00
|
|
|
NfsInputStream *is = new NfsInputStream(uri, mutex, cond, buffer);
|
|
|
|
if (!is->Open(error)) {
|
|
|
|
delete is;
|
2014-02-06 07:29:26 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-03-15 15:29:10 +01:00
|
|
|
return is;
|
2014-02-06 07:29:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const InputPlugin input_plugin_nfs = {
|
|
|
|
"nfs",
|
2014-03-15 15:29:10 +01:00
|
|
|
input_nfs_init,
|
|
|
|
input_nfs_finish,
|
2014-02-06 07:29:26 +01:00
|
|
|
input_nfs_open,
|
|
|
|
};
|