2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2018-10-31 17:54:59 +01:00
|
|
|
* Copyright 2003-2018 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-05-18 04:46:36 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2004-05-18 04:46:36 +02:00
|
|
|
*/
|
|
|
|
|
2013-01-21 10:49:42 +01:00
|
|
|
#include "FileInputPlugin.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "../InputStream.hxx"
|
|
|
|
#include "../InputPlugin.hxx"
|
2014-10-02 18:38:33 +02:00
|
|
|
#include "fs/Path.hxx"
|
2015-03-03 18:10:02 +01:00
|
|
|
#include "fs/FileInfo.hxx"
|
|
|
|
#include "fs/io/FileReader.hxx"
|
2015-03-03 17:03:21 +01:00
|
|
|
#include "system/FileDescriptor.hxx"
|
2016-09-09 15:37:06 +02:00
|
|
|
#include "util/RuntimeError.hxx"
|
2008-10-28 20:41:24 +01:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
2015-03-03 21:17:47 +01:00
|
|
|
#include <fcntl.h>
|
2004-05-18 04:46:36 +02:00
|
|
|
|
2014-10-02 19:14:59 +02:00
|
|
|
class FileInputStream final : public InputStream {
|
2015-03-03 18:10:02 +01:00
|
|
|
FileReader reader;
|
2013-01-21 10:49:42 +01:00
|
|
|
|
2014-10-02 19:14:59 +02:00
|
|
|
public:
|
2015-03-03 18:10:02 +01:00
|
|
|
FileInputStream(const char *path, FileReader &&_reader, off_t _size,
|
2018-06-22 19:37:18 +02:00
|
|
|
Mutex &_mutex)
|
|
|
|
:InputStream(path, _mutex),
|
2015-03-03 18:10:02 +01:00
|
|
|
reader(std::move(_reader)) {
|
2014-05-11 16:02:57 +02:00
|
|
|
size = _size;
|
|
|
|
seekable = true;
|
|
|
|
SetReady();
|
2013-01-21 10:49:42 +01:00
|
|
|
}
|
|
|
|
|
2014-05-11 17:14:49 +02:00
|
|
|
/* virtual methods from InputStream */
|
|
|
|
|
2017-05-08 14:44:49 +02:00
|
|
|
bool IsEOF() noexcept override {
|
2014-05-11 17:14:49 +02:00
|
|
|
return GetOffset() >= GetSize();
|
|
|
|
}
|
|
|
|
|
2016-09-09 18:47:42 +02:00
|
|
|
size_t Read(void *ptr, size_t size) override;
|
|
|
|
void Seek(offset_type offset) override;
|
2009-12-30 23:27:37 +01:00
|
|
|
};
|
|
|
|
|
2016-09-09 15:37:06 +02:00
|
|
|
InputStreamPtr
|
2018-06-22 19:37:18 +02:00
|
|
|
OpenFileInputStream(Path path, Mutex &mutex)
|
2016-09-09 15:37:06 +02:00
|
|
|
{
|
2015-12-16 11:05:33 +01:00
|
|
|
FileReader reader(path);
|
2004-05-18 04:46:36 +02:00
|
|
|
|
2015-12-16 11:05:33 +01:00
|
|
|
const FileInfo info = reader.GetFileInfo();
|
2008-10-29 21:11:20 +01:00
|
|
|
|
2016-09-09 15:37:06 +02:00
|
|
|
if (!info.IsRegular())
|
|
|
|
throw FormatRuntimeError("Not a regular file: %s",
|
|
|
|
path.c_str());
|
2008-10-29 21:11:28 +01:00
|
|
|
|
2007-01-19 02:40:02 +01:00
|
|
|
#ifdef POSIX_FADV_SEQUENTIAL
|
2015-03-03 18:10:02 +01:00
|
|
|
posix_fadvise(reader.GetFD().Get(), (off_t)0, info.GetSize(),
|
|
|
|
POSIX_FADV_SEQUENTIAL);
|
2007-01-19 02:40:02 +01:00
|
|
|
#endif
|
|
|
|
|
2018-07-18 16:30:46 +02:00
|
|
|
return std::make_unique<FileInputStream>(path.ToUTF8Throw().c_str(),
|
2017-12-26 20:05:22 +01:00
|
|
|
std::move(reader), info.GetSize(),
|
2018-06-22 19:37:18 +02:00
|
|
|
mutex);
|
2014-10-02 18:38:33 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 18:47:42 +02:00
|
|
|
void
|
|
|
|
FileInputStream::Seek(offset_type new_offset)
|
|
|
|
{
|
2017-11-13 17:08:00 +01:00
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
reader.Seek((off_t)new_offset);
|
|
|
|
}
|
|
|
|
|
2015-03-03 18:10:02 +01:00
|
|
|
offset = new_offset;
|
2004-05-18 04:46:36 +02:00
|
|
|
}
|
|
|
|
|
2014-05-11 17:14:49 +02:00
|
|
|
size_t
|
2016-09-09 18:47:42 +02:00
|
|
|
FileInputStream::Read(void *ptr, size_t read_size)
|
|
|
|
{
|
2017-11-13 17:08:00 +01:00
|
|
|
size_t nbytes;
|
|
|
|
|
|
|
|
{
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
nbytes = reader.Read(ptr, read_size);
|
|
|
|
}
|
|
|
|
|
2014-05-11 17:14:49 +02:00
|
|
|
offset += nbytes;
|
2015-12-16 11:05:33 +01:00
|
|
|
return nbytes;
|
2004-05-18 04:46:36 +02:00
|
|
|
}
|