2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 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
|
|
|
*/
|
|
|
|
|
2009-10-11 23:32:22 +02:00
|
|
|
#include "config.h" /* must be first for large file support */
|
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"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
2013-10-17 23:23:25 +02:00
|
|
|
#include "fs/Traits.hxx"
|
2013-08-07 10:31:31 +02:00
|
|
|
#include "system/fd_util.h"
|
2010-05-20 06:59:25 +02:00
|
|
|
#include "open.h"
|
2008-10-28 20:41:24 +01:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2004-05-18 04:46:36 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain file_domain("file");
|
|
|
|
|
2014-05-11 16:02:57 +02:00
|
|
|
struct FileInputStream final : public InputStream {
|
2009-12-30 23:27:37 +01:00
|
|
|
int fd;
|
2013-01-21 10:49:42 +01:00
|
|
|
|
2014-05-11 16:02:57 +02:00
|
|
|
FileInputStream(const char *path, int _fd, off_t _size,
|
|
|
|
Mutex &_mutex, Cond &_cond)
|
2014-05-11 18:25:55 +02:00
|
|
|
:InputStream(path, _mutex, _cond),
|
2013-01-28 20:32:23 +01:00
|
|
|
fd(_fd) {
|
2014-05-11 16:02:57 +02:00
|
|
|
size = _size;
|
|
|
|
seekable = true;
|
|
|
|
SetReady();
|
2013-01-21 10:49:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
~FileInputStream() {
|
|
|
|
close(fd);
|
|
|
|
}
|
2014-05-11 17:14:49 +02:00
|
|
|
|
|
|
|
/* virtual methods from InputStream */
|
|
|
|
|
|
|
|
bool IsEOF() override {
|
|
|
|
return GetOffset() >= GetSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Read(void *ptr, size_t size, Error &error) override;
|
2014-05-22 10:10:16 +02:00
|
|
|
bool Seek(offset_type offset, Error &error) override;
|
2009-12-30 23:27:37 +01:00
|
|
|
};
|
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
static InputStream *
|
2011-09-14 21:46:41 +02:00
|
|
|
input_file_open(const char *filename,
|
2013-01-27 17:20:50 +01:00
|
|
|
Mutex &mutex, Cond &cond,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-28 20:41:24 +01:00
|
|
|
int fd, ret;
|
|
|
|
struct stat st;
|
2004-05-18 04:46:36 +02:00
|
|
|
|
2013-12-04 22:53:43 +01:00
|
|
|
if (!PathTraitsFS::IsAbsolute(filename))
|
2013-01-21 10:49:42 +01:00
|
|
|
return nullptr;
|
2008-10-27 10:10:24 +01:00
|
|
|
|
2010-05-20 06:59:25 +02:00
|
|
|
fd = open_cloexec(filename, O_RDONLY|O_BINARY, 0);
|
2008-10-28 20:41:24 +01:00
|
|
|
if (fd < 0) {
|
2009-11-14 23:53:04 +01:00
|
|
|
if (errno != ENOENT && errno != ENOTDIR)
|
2013-08-10 18:02:44 +02:00
|
|
|
error.FormatErrno("Failed to open \"%s\"",
|
|
|
|
filename);
|
2013-01-21 10:49:42 +01:00
|
|
|
return nullptr;
|
2004-05-18 04:46:36 +02:00
|
|
|
}
|
|
|
|
|
2008-10-28 20:41:24 +01:00
|
|
|
ret = fstat(fd, &st);
|
2008-10-29 21:11:20 +01:00
|
|
|
if (ret < 0) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.FormatErrno("Failed to stat \"%s\"", filename);
|
2008-10-29 21:11:20 +01:00
|
|
|
close(fd);
|
2013-01-21 10:49:42 +01:00
|
|
|
return nullptr;
|
2008-10-29 21:11:20 +01:00
|
|
|
}
|
|
|
|
|
2008-10-29 21:11:28 +01:00
|
|
|
if (!S_ISREG(st.st_mode)) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(file_domain, "Not a regular file: %s", filename);
|
2008-10-29 21:11:28 +01:00
|
|
|
close(fd);
|
2013-01-21 10:49:42 +01:00
|
|
|
return nullptr;
|
2008-10-29 21:11:28 +01:00
|
|
|
}
|
|
|
|
|
2007-01-19 02:40:02 +01:00
|
|
|
#ifdef POSIX_FADV_SEQUENTIAL
|
2009-12-30 23:27:37 +01:00
|
|
|
posix_fadvise(fd, (off_t)0, st.st_size, POSIX_FADV_SEQUENTIAL);
|
2007-01-19 02:40:02 +01:00
|
|
|
#endif
|
|
|
|
|
2014-05-11 16:02:57 +02:00
|
|
|
return new FileInputStream(filename, fd, st.st_size, mutex, cond);
|
2004-05-18 04:46:36 +02:00
|
|
|
}
|
|
|
|
|
2014-05-11 17:14:49 +02:00
|
|
|
bool
|
2014-05-22 10:10:16 +02:00
|
|
|
FileInputStream::Seek(offset_type new_offset, Error &error)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2014-05-22 10:10:16 +02:00
|
|
|
new_offset = (offset_type)lseek(fd, (off_t)new_offset, SEEK_SET);
|
2014-05-11 17:14:49 +02:00
|
|
|
if (new_offset < 0) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.SetErrno("Failed to seek");
|
2008-10-26 20:56:46 +01:00
|
|
|
return false;
|
2004-05-18 04:46:36 +02:00
|
|
|
}
|
|
|
|
|
2014-05-11 17:14:49 +02:00
|
|
|
offset = new_offset;
|
2008-10-26 20:56:46 +01:00
|
|
|
return true;
|
2004-05-18 04:46:36 +02:00
|
|
|
}
|
|
|
|
|
2014-05-11 17:14:49 +02:00
|
|
|
size_t
|
|
|
|
FileInputStream::Read(void *ptr, size_t read_size, Error &error)
|
2004-05-18 04:46:36 +02:00
|
|
|
{
|
2014-05-11 17:14:49 +02:00
|
|
|
ssize_t nbytes = read(fd, ptr, read_size);
|
2008-10-28 20:41:24 +01:00
|
|
|
if (nbytes < 0) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.SetErrno("Failed to read");
|
2008-10-28 20:41:24 +01:00
|
|
|
return 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
2004-05-18 04:46:36 +02:00
|
|
|
|
2014-05-11 17:14:49 +02:00
|
|
|
offset += nbytes;
|
2008-10-28 20:41:24 +01:00
|
|
|
return (size_t)nbytes;
|
2004-05-18 04:46:36 +02:00
|
|
|
}
|
|
|
|
|
2013-10-17 10:20:57 +02:00
|
|
|
const InputPlugin input_plugin_file = {
|
2013-01-21 10:49:42 +01:00
|
|
|
"file",
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
input_file_open,
|
2008-10-26 20:38:44 +01:00
|
|
|
};
|