MPD  0.20.6
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
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
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 
73 
79 
85  };
86 
87 private:
88  Mode mode;
89 
90 public:
91  explicit FileOutputStream(Path _path, Mode _mode=Mode::CREATE);
92 
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
Create a new file, or replace an existing file.
FileOutputStream(Path _path, Mode _mode=Mode::CREATE)
Path GetPath() const
Like #APPEND_EXISTING, but create the file if it does not exist.
constexpr bool IsDefined() const
A path name in the native file system character set.
gcc_pure uint64_t Tell() const
Like #CREATE, but no attempt is made to hide file contents during the transaction (e...
Append to a file that already exists.
A path name in the native file system character set.
Definition: Path.hxx:39
static constexpr FileDescriptor Undefined()
bool Close()
Close the file descriptor.
void Write(const void *data, size_t size) override
Throws std::exception on error.
An OO wrapper for a UNIX file descriptor.
#define gcc_pure
Definition: Compiler.h:116