FFmpegfs Fuse Multi Media Filesystem 2.16
diskio.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017-2024 Norbert Schlia (nschlia@oblivion-software.de)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17 *
18 * On Debian systems, the complete text of the GNU General Public License
19 * Version 3 can be found in `/usr/share/common-licenses/GPL-3'.
20 */
21
32#include "diskio.h"
33#include "logging.h"
34
35#include <cerrno>
36
37DiskIO::DiskIO()
38 : m_fpi(nullptr)
39{
40
41}
42
43DiskIO::~DiskIO()
44{
45 pvt_close();
46}
47
49{
50 return VIRTUALTYPE::DISK;
51}
52
53size_t DiskIO::bufsize() const
54{
55 return (100 /* KB */ * 1024);
56}
57
59{
61
62 Logging::debug(virtualfile->m_origfile, "Opening the input file.");
63
64 m_fpi = fopen(virtualfile->m_origfile.c_str(), "rb");
65
66 if (m_fpi != nullptr)
67 {
68 return 0;
69 }
70 else
71 {
72 return errno;
73 }
74}
75
76size_t DiskIO::readio(void * data, size_t size)
77{
78 return fread(data, 1, size, m_fpi);
79}
80
81int DiskIO::error() const
82{
83 return ferror(m_fpi);
84}
85
86int64_t DiskIO::duration() const
87{
88 return AV_NOPTS_VALUE; // not applicable
89}
90
91size_t DiskIO::size() const
92{
93 if (m_fpi == nullptr)
94 {
95 errno = EINVAL;
96 return 0;
97 }
98
99 struct stat stbuf;
100 fstat(fileno(m_fpi), &stbuf);
101 return static_cast<size_t>(stbuf.st_size);
102}
103
104size_t DiskIO::tell() const
105{
106 return static_cast<size_t>(ftell(m_fpi));
107}
108
109int DiskIO::seek(int64_t offset, int whence)
110{
111 return fseek(m_fpi, static_cast<long int>(offset), whence);
112}
113
114bool DiskIO::eof() const
115{
116 return feof(m_fpi) ? true : false;
117}
118
120{
121 pvt_close();
122}
123
125{
126 FILE *fpi = m_fpi;
127 if (fpi != nullptr)
128 {
129 m_fpi = nullptr;
130 fclose(fpi);
131 }
132}
virtual int openio(LPVIRTUALFILE virtualfile) override
Open a file.
Definition: diskio.cc:58
virtual int seek(int64_t offset, int whence) override
Seek to position in file.
Definition: diskio.cc:109
virtual int64_t duration() const override
Get the duration of the file, in AV_TIME_BASE fractional seconds.
Definition: diskio.cc:86
virtual VIRTUALTYPE type() const override
Get type of the virtual file.
Definition: diskio.cc:48
virtual bool eof() const override
Check if at end of file.
Definition: diskio.cc:114
virtual int error() const override
Get last error.
Definition: diskio.cc:81
virtual size_t bufsize() const override
Get the ideal buffer size.
Definition: diskio.cc:53
virtual void closeio() override
Close virtual file.
Definition: diskio.cc:119
virtual size_t readio(void *data, size_t size) override
Read data from file.
Definition: diskio.cc:76
void pvt_close()
Close virtual file. Non-virtual version to be safely called from constructor/destructor.
Definition: diskio.cc:124
FILE * m_fpi
File pointer to source media.
Definition: diskio.h:125
virtual size_t size() const override
Get the file size.
Definition: diskio.cc:91
virtual size_t tell() const override
Get current read position.
Definition: diskio.cc:104
LPVIRTUALFILE virtualfile()
Get virtual file object.
Definition: fileio.cc:113
void set_virtualfile(LPVIRTUALFILE virtualfile)
Set the virtual file object.
Definition: fileio.cc:96
static void debug(const T filename, const std::string &format_string, Args &&...args)
Write debug level log entry.
Definition: logging.h:182
Direct disk I/O.
VIRTUALTYPE
Virtual file types enum.
Definition: fileio.h:92
@ DISK
Regular disk file to transcode.
Provide various log facilities to stderr, disk or syslog.
Virtual file definition.
Definition: fileio.h:123
std::string m_origfile
Sanitised name and path of original file.
Definition: fileio.h:152