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
0d0ccacd
Commit
0d0ccacd
authored
Jul 30, 2014
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fs/OutputStream: new infrastructure for writing to files
parent
c8858f85
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
503 additions
and
0 deletions
+503
-0
Makefile.am
Makefile.am
+4
-0
BufferedOutputStream.cxx
src/fs/output/BufferedOutputStream.cxx
+143
-0
BufferedOutputStream.hxx
src/fs/output/BufferedOutputStream.hxx
+71
-0
FileOutputStream.cxx
src/fs/output/FileOutputStream.cxx
+134
-0
FileOutputStream.hxx
src/fs/output/FileOutputStream.hxx
+68
-0
OutputStream.hxx
src/fs/output/OutputStream.hxx
+38
-0
StdioOutputStream.hxx
src/fs/output/StdioOutputStream.hxx
+45
-0
No files found.
Makefile.am
View file @
0d0ccacd
...
...
@@ -503,6 +503,10 @@ endif
# File system library
libfs_a_SOURCES
=
\
src/fs/output/OutputStream.hxx
\
src/fs/output/StdoutOutputStream.hxx
\
src/fs/output/FileOutputStream.cxx src/fs/output/FileOutputStream.hxx
\
src/fs/output/BufferedOutputStream.cxx src/fs/output/BufferedOutputStream.hxx
\
src/fs/Domain.cxx src/fs/Domain.hxx
\
src/fs/Limits.hxx
\
src/fs/Traits.cxx src/fs/Traits.hxx
\
...
...
src/fs/output/BufferedOutputStream.cxx
0 → 100644
View file @
0d0ccacd
/*
* 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 "BufferedOutputStream.hxx"
#include "OutputStream.hxx"
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
bool
BufferedOutputStream
::
AppendToBuffer
(
const
void
*
data
,
size_t
size
)
{
auto
r
=
buffer
.
Write
();
if
(
r
.
size
<
size
)
return
false
;
memcpy
(
r
.
data
,
data
,
size
);
buffer
.
Append
(
size
);
return
true
;
}
bool
BufferedOutputStream
::
Write
(
const
void
*
data
,
size_t
size
)
{
if
(
gcc_unlikely
(
last_error
.
IsDefined
()))
return
false
;
if
(
AppendToBuffer
(
data
,
size
))
return
true
;
if
(
!
Flush
())
return
false
;
if
(
AppendToBuffer
(
data
,
size
))
return
true
;
return
os
.
Write
(
data
,
size
,
last_error
);
}
bool
BufferedOutputStream
::
Write
(
const
char
*
p
)
{
return
Write
(
p
,
strlen
(
p
));
}
bool
BufferedOutputStream
::
Format
(
const
char
*
fmt
,
...)
{
if
(
gcc_unlikely
(
last_error
.
IsDefined
()))
return
false
;
auto
r
=
buffer
.
Write
();
if
(
r
.
IsEmpty
())
{
if
(
!
Flush
())
return
false
;
r
=
buffer
.
Write
();
}
/* format into the buffer */
va_list
ap
;
va_start
(
ap
,
fmt
);
size_t
size
=
vsnprintf
(
r
.
data
,
r
.
size
,
fmt
,
ap
);
va_end
(
ap
);
if
(
gcc_unlikely
(
size
>=
r
.
size
))
{
/* buffer was not large enough; flush it and try
again */
if
(
!
Flush
())
return
false
;
r
=
buffer
.
Write
();
if
(
gcc_unlikely
(
size
>=
r
.
size
))
{
/* still not enough space: grow the buffer and
try again */
r
.
size
=
size
+
1
;
r
.
data
=
buffer
.
Write
(
r
.
size
);
}
/* format into the new buffer */
va_start
(
ap
,
fmt
);
size
=
vsnprintf
(
r
.
data
,
r
.
size
,
fmt
,
ap
);
va_end
(
ap
);
/* this time, it must fit */
assert
(
size
<
r
.
size
);
}
buffer
.
Append
(
size
);
return
true
;
}
bool
BufferedOutputStream
::
Flush
()
{
if
(
!
Check
())
return
false
;
auto
r
=
buffer
.
Read
();
if
(
r
.
IsEmpty
())
return
true
;
bool
success
=
os
.
Write
(
r
.
data
,
r
.
size
,
last_error
);
if
(
gcc_likely
(
success
))
buffer
.
Consume
(
r
.
size
);
return
success
;
}
bool
BufferedOutputStream
::
Flush
(
Error
&
error
)
{
if
(
!
Check
(
error
))
return
false
;
auto
r
=
buffer
.
Read
();
if
(
r
.
IsEmpty
())
return
true
;
bool
success
=
os
.
Write
(
r
.
data
,
r
.
size
,
error
);
if
(
gcc_likely
(
success
))
buffer
.
Consume
(
r
.
size
);
return
success
;
}
src/fs/output/BufferedOutputStream.hxx
0 → 100644
View file @
0d0ccacd
/*
* 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.
*/
#ifndef MPD_BUFFERED_OUTPUT_STREAM_HXX
#define MPD_BUFFERED_OUTPUT_STREAM_HXX
#include "check.h"
#include "Compiler.h"
#include "util/DynamicFifoBuffer.hxx"
#include "util/Error.hxx"
#include <stddef.h>
class
OutputStream
;
class
Error
;
class
BufferedOutputStream
{
OutputStream
&
os
;
DynamicFifoBuffer
<
char
>
buffer
;
Error
last_error
;
public
:
BufferedOutputStream
(
OutputStream
&
_os
)
:
os
(
_os
),
buffer
(
32768
)
{}
bool
Write
(
const
void
*
data
,
size_t
size
);
bool
Write
(
const
char
*
p
);
gcc_printf
(
2
,
3
)
bool
Format
(
const
char
*
fmt
,
...);
gcc_pure
bool
Check
()
const
{
return
!
last_error
.
IsDefined
();
}
bool
Check
(
Error
&
error
)
const
{
if
(
last_error
.
IsDefined
())
{
error
.
Set
(
last_error
);
return
false
;
}
else
return
true
;
}
bool
Flush
();
bool
Flush
(
Error
&
error
);
private
:
bool
AppendToBuffer
(
const
void
*
data
,
size_t
size
);
};
#endif
src/fs/output/FileOutputStream.cxx
0 → 100644
View file @
0d0ccacd
/*
* 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 "FileOutputStream.hxx"
#include "fs/FileSystem.hxx"
#include "util/Error.hxx"
#ifdef WIN32
FileOutputStream
::
FileOutputStream
(
Path
_path
,
Error
&
error
)
:
path
(
_path
),
handle
(
CreateFile
(
path
.
c_str
(),
GENERIC_WRITE
,
0
,
nullptr
,
TRUNCATE_EXISTING
,
FILE_ATTRIBUTE_NORMAL
|
FILE_FLAG_WRITE_THROUGH
,
nullptr
))
{
if
(
handle
==
INVALID_HANDLE_VALUE
)
error
.
FormatLastError
(
"Failed to create %s"
,
path
.
c_str
());
}
bool
FileOutputStream
::
Write
(
const
void
*
data
,
size_t
size
,
Error
&
error
)
{
assert
(
IsDefined
());
DWORD
nbytes
;
if
(
!
WriteFile
(
handle
,
data
,
size
,
&
nbytes
,
nullptr
))
{
error
.
FormatLastError
(
"Failed to write to %s"
,
path
.
c_str
());
return
false
;
}
if
(
size_t
(
nbytes
)
!=
size
)
{
error
.
FormatLastError
(
ERROR_DISK_FULL
,
"Failed to write to %s"
,
path
.
c_str
());
return
false
;
}
return
true
;
}
bool
FileOutputStream
::
Commit
(
gcc_unused
Error
&
error
)
{
assert
(
IsDefined
());
CloseHandle
(
handle
);
return
true
;
}
void
FileOutputStream
::
Cancel
()
{
assert
(
IsDefined
());
CloseHandle
(
handle
);
RemoveFile
(
path
);
}
#else
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
FileOutputStream
::
FileOutputStream
(
Path
_path
,
Error
&
error
)
:
path
(
_path
),
fd
(
open
(
path
.
c_str
(),
O_WRONLY
|
O_CLOEXEC
|
O_CREAT
|
O_TRUNC
|
O_NOCTTY
,
0666
))
{
if
(
fd
<
0
)
error
.
FormatErrno
(
"Failed to create %s"
,
path
.
c_str
());
}
bool
FileOutputStream
::
Write
(
const
void
*
data
,
size_t
size
,
Error
&
error
)
{
assert
(
IsDefined
());
ssize_t
nbytes
=
write
(
fd
,
data
,
size
);
if
(
nbytes
<
0
)
{
error
.
FormatErrno
(
"Failed to write to %s"
,
path
.
c_str
());
return
false
;
}
else
if
((
size_t
)
nbytes
<
size
)
{
error
.
FormatErrno
(
ENOSPC
,
"Failed to write to %s"
,
path
.
c_str
());
return
false
;
}
return
true
;
}
bool
FileOutputStream
::
Commit
(
Error
&
error
)
{
assert
(
IsDefined
());
bool
success
=
close
(
fd
)
==
0
;
fd
=
-
1
;
if
(
!
success
)
error
.
FormatErrno
(
"Failed to commit %s"
,
path
.
c_str
());
return
success
;
}
void
FileOutputStream
::
Cancel
()
{
assert
(
IsDefined
());
close
(
fd
);
fd
=
-
1
;
RemoveFile
(
path
);
}
#endif
src/fs/output/FileOutputStream.hxx
0 → 100644
View file @
0d0ccacd
/*
* 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.
*/
#ifndef MPD_FILE_OUTPUT_STREAM_HXX
#define MPD_FILE_OUTPUT_STREAM_HXX
#include "check.h"
#include "OutputStream.hxx"
#include "fs/AllocatedPath.hxx"
#include <assert.h>
#ifdef WIN32
#include <windows.h>
#endif
class
Path
;
class
FileOutputStream
final
:
public
OutputStream
{
AllocatedPath
path
;
#ifdef WIN32
HANDLE
handle
;
#else
int
fd
;
#endif
public
:
FileOutputStream
(
Path
_path
,
Error
&
error
);
~
FileOutputStream
()
{
if
(
IsDefined
())
Cancel
();
}
bool
IsDefined
()
const
{
#ifdef WIN32
return
handle
!=
INVALID_HANDLE_VALUE
;
#else
return
fd
>=
0
;
#endif
}
bool
Commit
(
Error
&
error
);
void
Cancel
();
/* virtual methods from class OutputStream */
bool
Write
(
const
void
*
data
,
size_t
size
,
Error
&
error
)
override
;
};
#endif
src/fs/output/OutputStream.hxx
0 → 100644
View file @
0d0ccacd
/*
* 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.
*/
#ifndef MPD_OUTPUT_STREAM_HXX
#define MPD_OUTPUT_STREAM_HXX
#include "check.h"
#include "Compiler.h"
#include <stddef.h>
class
Error
;
class
OutputStream
{
public
:
OutputStream
()
=
default
;
OutputStream
(
const
OutputStream
&
)
=
delete
;
virtual
bool
Write
(
const
void
*
data
,
size_t
size
,
Error
&
error
)
=
0
;
};
#endif
src/fs/output/StdioOutputStream.hxx
0 → 100644
View file @
0d0ccacd
/*
* 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.
*/
#ifndef MPD_STDIO_OUTPUT_STREAM_HXX
#define MPD_STDIO_OUTPUT_STREAM_HXX
#include "check.h"
#include "OutputStream.hxx"
#include "fs/AllocatedPath.hxx"
#include <stdio.h>
class
StdioOutputStream
final
:
public
OutputStream
{
FILE
*
const
file
;
public
:
StdioOutputStream
(
FILE
*
_file
)
:
file
(
_file
)
{}
/* virtual methods from class OutputStream */
bool
Write
(
const
void
*
data
,
size_t
size
,
gcc_unused
Error
&
error
)
override
{
fwrite
(
data
,
1
,
size
,
file
);
/* this class is debug-only and ignores errors */
return
true
;
}
};
#endif
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