MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

FileOutputStream.hxx
Go to the documentation of this file.
1 /*
2 * Copyright 2003-2017 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MPD_FILE_OUTPUT_STREAM_HXX
21 #define MPD_FILE_OUTPUT_STREAM_HXX
22
23 #include "check.h"
24 #include "OutputStream.hxx"
25 #include "fs/AllocatedPath.hxx"
26 #include "Compiler.h"
27
28 #ifndef WIN32
29 #include "system/FileDescriptor.hxx"
30 #endif
31
32 #include <assert.h>
33 #include <stdint.h>
34
35 #ifdef WIN32
36 #include <windows.h>
37 #endif
38
39 class Path;
40
41 class FileOutputStream final : public OutputStream {
42 const AllocatedPath path;
43
44 #ifdef WIN32
45 HANDLE handle = INVALID_HANDLE_VALUE;
46 #else
47 FileDescriptor fd = FileDescriptor::Undefined();
48 #endif
49
50 #ifdef HAVE_LINKAT
51
55 bool is_tmpfile = false;
56 #endif
57
58 public:
59 enum class Mode : uint8_t {
65 CREATE,
66
72 CREATE_VISIBLE,
73
78 APPEND_EXISTING,
79
84 APPEND_OR_CREATE,
85 };
86
87 private:
88 Mode mode;
89
90 public:
91 explicit FileOutputStream(Path _path, Mode _mode=Mode::CREATE);
92
93 ~FileOutputStream() {
94 if (IsDefined())
95 Cancel();
96 }
97
98 public:
99 Path GetPath() const {
100 return path;
101 }
102
103 gcc_pure
104 uint64_t Tell() const;
105
106 /* virtual methods from class OutputStream */
107 void Write(const void *data, size_t size) override;
108
109 void Commit();
110 void Cancel();
111
112 private:
113 void OpenCreate(bool visible);
114 void OpenAppend(bool create);
115
116 bool Close() {
117 assert(IsDefined());
118
119 #ifdef WIN32
120 CloseHandle(handle);
121 handle = INVALID_HANDLE_VALUE;
122 return true;
123 #else
124 return fd.Close();
125 #endif
126 }
127
128 #ifdef WIN32
129 bool SeekEOF() {
130 return SetFilePointer(handle, 0, nullptr,
131 FILE_END) != 0xffffffff;
132 }
133 #endif
134
135 bool IsDefined() const {
136 #ifdef WIN32
137 return handle != INVALID_HANDLE_VALUE;
138 #else
139 return fd.IsDefined();
140 #endif
141 }
142 };
143
144 #endif
FileOutputStream::Mode::CREATE
Create a new file, or replace an existing file.
FileOutputStream::FileOutputStream
FileOutputStream(Path _path, Mode _mode=Mode::CREATE)
check.h
FileOutputStream::Mode
Mode
Definition: FileOutputStream.hxx:59
FileOutputStream::GetPath
Path GetPath() const
Definition: FileOutputStream.hxx:99
FileOutputStream::Mode::APPEND_OR_CREATE
Like #APPEND_EXISTING, but create the file if it does not exist.
FileDescriptor::IsDefined
constexpr bool IsDefined() const
Definition: FileDescriptor.hxx:61
FileOutputStream::Commit
void Commit()
AllocatedPath
A path name in the native file system character set.
Definition: AllocatedPath.hxx:38
OutputStream.hxx
FileOutputStream::Tell
gcc_pure uint64_t Tell() const
FileOutputStream
Definition: FileOutputStream.hxx:41
FileOutputStream::Cancel
void Cancel()
FileOutputStream::Mode::CREATE_VISIBLE
Like #CREATE, but no attempt is made to hide file contents during the transaction (e...
FileOutputStream::Mode::APPEND_EXISTING
Append to a file that already exists.
Path
A path name in the native file system character set.
Definition: Path.hxx:39
Compiler.h
FileOutputStream::~FileOutputStream
~FileOutputStream()
Definition: FileOutputStream.hxx:93
AllocatedPath.hxx
OutputStream
Definition: OutputStream.hxx:27
FileDescriptor::Undefined
static constexpr FileDescriptor Undefined()
Definition: FileDescriptor.hxx:89
FileDescriptor::Close
bool Close()
Close the file descriptor.
Definition: FileDescriptor.hxx:136
FileOutputStream::Write
void Write(const void *data, size_t size) override
Throws std::exception on error.
FileDescriptor
An OO wrapper for a UNIX file descriptor.
Definition: FileDescriptor.hxx:49
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
FileDescriptor.hxx