Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
57ad3aca
Commit
57ad3aca
authored
Mar 03, 2015
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
input/file: use class FileReader
parent
f677f42b
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
30 deletions
+23
-30
FileReader.hxx
src/fs/io/FileReader.hxx
+6
-0
FileInputPlugin.cxx
src/input/plugins/FileInputPlugin.cxx
+17
-30
No files found.
src/fs/io/FileReader.hxx
View file @
57ad3aca
...
@@ -78,6 +78,12 @@ public:
...
@@ -78,6 +78,12 @@ public:
#endif
#endif
}
}
#ifndef WIN32
FileDescriptor
GetFD
()
const
{
return
fd
;
}
#endif
void
Close
();
void
Close
();
bool
GetFileInfo
(
FileInfo
&
info
,
Error
&
error
)
const
;
bool
GetFileInfo
(
FileInfo
&
info
,
Error
&
error
)
const
;
...
...
src/input/plugins/FileInputPlugin.cxx
View file @
57ad3aca
...
@@ -24,6 +24,8 @@
...
@@ -24,6 +24,8 @@
#include "util/Error.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "util/Domain.hxx"
#include "fs/Path.hxx"
#include "fs/Path.hxx"
#include "fs/FileInfo.hxx"
#include "fs/io/FileReader.hxx"
#include "system/FileDescriptor.hxx"
#include "system/FileDescriptor.hxx"
#include <sys/stat.h>
#include <sys/stat.h>
...
@@ -33,22 +35,18 @@
...
@@ -33,22 +35,18 @@
static
constexpr
Domain
file_domain
(
"file"
);
static
constexpr
Domain
file_domain
(
"file"
);
class
FileInputStream
final
:
public
InputStream
{
class
FileInputStream
final
:
public
InputStream
{
File
Descriptor
fd
;
File
Reader
reader
;
public
:
public
:
FileInputStream
(
const
char
*
path
,
File
Descriptor
_fd
,
off_t
_size
,
FileInputStream
(
const
char
*
path
,
File
Reader
&&
_reader
,
off_t
_size
,
Mutex
&
_mutex
,
Cond
&
_cond
)
Mutex
&
_mutex
,
Cond
&
_cond
)
:
InputStream
(
path
,
_mutex
,
_cond
),
:
InputStream
(
path
,
_mutex
,
_cond
),
fd
(
_fd
)
{
reader
(
std
::
move
(
_reader
)
)
{
size
=
_size
;
size
=
_size
;
seekable
=
true
;
seekable
=
true
;
SetReady
();
SetReady
();
}
}
~
FileInputStream
()
{
fd
.
Close
();
}
/* virtual methods from InputStream */
/* virtual methods from InputStream */
bool
IsEOF
()
override
{
bool
IsEOF
()
override
{
...
@@ -64,33 +62,27 @@ OpenFileInputStream(Path path,
...
@@ -64,33 +62,27 @@ OpenFileInputStream(Path path,
Mutex
&
mutex
,
Cond
&
cond
,
Mutex
&
mutex
,
Cond
&
cond
,
Error
&
error
)
Error
&
error
)
{
{
FileDescriptor
fd
;
FileReader
reader
(
path
,
error
);
if
(
!
fd
.
OpenReadOnly
(
path
.
c_str
()))
{
if
(
!
reader
.
IsDefined
())
error
.
FormatErrno
(
"Failed to open
\"
%s
\"
"
,
path
.
c_str
());
return
nullptr
;
return
nullptr
;
}
struct
stat
st
;
FileInfo
info
;
if
(
fstat
(
fd
.
Get
(),
&
st
)
<
0
)
{
if
(
!
reader
.
GetFileInfo
(
info
,
error
))
error
.
FormatErrno
(
"Failed to stat
\"
%s
\"
"
,
path
.
c_str
());
fd
.
Close
();
return
nullptr
;
return
nullptr
;
}
if
(
!
S_ISREG
(
st
.
st_mode
))
{
if
(
!
info
.
IsRegular
(
))
{
error
.
Format
(
file_domain
,
"Not a regular file: %s"
,
error
.
Format
(
file_domain
,
"Not a regular file: %s"
,
path
.
c_str
());
path
.
c_str
());
fd
.
Close
();
return
nullptr
;
return
nullptr
;
}
}
#ifdef POSIX_FADV_SEQUENTIAL
#ifdef POSIX_FADV_SEQUENTIAL
posix_fadvise
(
fd
.
Get
(),
(
off_t
)
0
,
st
.
st_size
,
POSIX_FADV_SEQUENTIAL
);
posix_fadvise
(
reader
.
GetFD
().
Get
(),
(
off_t
)
0
,
info
.
GetSize
(),
POSIX_FADV_SEQUENTIAL
);
#endif
#endif
return
new
FileInputStream
(
path
.
ToUTF8
().
c_str
(),
return
new
FileInputStream
(
path
.
ToUTF8
().
c_str
(),
fd
,
st
.
st_size
,
std
::
move
(
reader
),
info
.
GetSize
()
,
mutex
,
cond
);
mutex
,
cond
);
}
}
...
@@ -107,24 +99,19 @@ input_file_open(gcc_unused const char *filename,
...
@@ -107,24 +99,19 @@ input_file_open(gcc_unused const char *filename,
bool
bool
FileInputStream
::
Seek
(
offset_type
new_offset
,
Error
&
error
)
FileInputStream
::
Seek
(
offset_type
new_offset
,
Error
&
error
)
{
{
auto
result
=
fd
.
Seek
((
off_t
)
new_offset
);
if
(
!
reader
.
Seek
((
off_t
)
new_offset
,
error
))
if
(
result
<
0
)
{
error
.
SetErrno
(
"Failed to seek"
);
return
false
;
return
false
;
}
offset
=
(
offset_type
)
resul
t
;
offset
=
new_offse
t
;
return
true
;
return
true
;
}
}
size_t
size_t
FileInputStream
::
Read
(
void
*
ptr
,
size_t
read_size
,
Error
&
error
)
FileInputStream
::
Read
(
void
*
ptr
,
size_t
read_size
,
Error
&
error
)
{
{
ssize_t
nbytes
=
fd
.
Read
(
ptr
,
read_size
);
ssize_t
nbytes
=
reader
.
Read
(
ptr
,
read_size
,
error
);
if
(
nbytes
<
0
)
{
if
(
nbytes
<
0
)
error
.
SetErrno
(
"Failed to read"
);
return
0
;
return
0
;
}
offset
+=
nbytes
;
offset
+=
nbytes
;
return
(
size_t
)
nbytes
;
return
(
size_t
)
nbytes
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment