1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
/*
* Copyright (C) 2003-2015 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"
FileOutputStream *
FileOutputStream::Create(Path path, Error &error)
{
FileOutputStream *f = new FileOutputStream(path, error);
if (!f->IsDefined()) {
delete f;
f = nullptr;
}
return f;
}
#ifdef WIN32
FileOutputStream::FileOutputStream(Path _path, Error &error)
:BaseFileOutputStream(_path)
{
SetHandle(CreateFile(_path.c_str(), GENERIC_WRITE, 0, nullptr,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH,
nullptr));
if (!IsDefined())
error.FormatLastError("Failed to create %s",
GetPath().ToUTF8().c_str());
}
bool
BaseFileOutputStream::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.ToUTF8().c_str());
return false;
}
if (size_t(nbytes) != size) {
error.FormatLastError(ERROR_DISK_FULL,
"Failed to write to %s",
path.ToUTF8().c_str());
return false;
}
return true;
}
bool
FileOutputStream::Commit(gcc_unused Error &error)
{
assert(IsDefined());
Close();
return true;
}
void
FileOutputStream::Cancel()
{
assert(IsDefined());
Close();
RemoveFile(GetPath());
}
#else
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#ifdef HAVE_LINKAT
#ifndef O_TMPFILE
/* supported since Linux 3.11 */
#define __O_TMPFILE 020000000
#define O_TMPFILE (__O_TMPFILE | O_DIRECTORY)
#include <stdio.h>
#endif
/**
* Open a file using Linux's O_TMPFILE for writing the given file.
*/
static bool
OpenTempFile(FileDescriptor &fd, Path path)
{
const auto directory = path.GetDirectoryName();
if (directory.IsNull())
return false;
return fd.Open(directory.c_str(), O_TMPFILE|O_WRONLY, 0666);
}
#endif /* HAVE_LINKAT */
FileOutputStream::FileOutputStream(Path _path, Error &error)
:BaseFileOutputStream(_path)
{
#ifdef HAVE_LINKAT
/* try Linux's O_TMPFILE first */
is_tmpfile = OpenTempFile(SetFD(), GetPath());
if (!is_tmpfile) {
#endif
/* fall back to plain POSIX */
if (!SetFD().Open(GetPath().c_str(),
O_WRONLY|O_CREAT|O_TRUNC,
0666))
error.FormatErrno("Failed to create %s",
GetPath().c_str());
#ifdef HAVE_LINKAT
}
#endif
}
bool
BaseFileOutputStream::Write(const void *data, size_t size, Error &error)
{
assert(IsDefined());
ssize_t nbytes = fd.Write(data, size);
if (nbytes < 0) {
error.FormatErrno("Failed to write to %s", GetPath().c_str());
return false;
} else if ((size_t)nbytes < size) {
error.FormatErrno(ENOSPC,
"Failed to write to %s", GetPath().c_str());
return false;
}
return true;
}
bool
FileOutputStream::Commit(Error &error)
{
assert(IsDefined());
#if HAVE_LINKAT
if (is_tmpfile) {
RemoveFile(GetPath());
/* hard-link the temporary file to the final path */
char fd_path[64];
snprintf(fd_path, sizeof(fd_path), "/proc/self/fd/%d",
GetFD().Get());
if (linkat(AT_FDCWD, fd_path, AT_FDCWD, GetPath().c_str(),
AT_SYMLINK_FOLLOW) < 0) {
error.FormatErrno("Failed to commit %s",
GetPath().c_str());
Close();
return false;
}
}
#endif
bool success = Close();
if (!success)
error.FormatErrno("Failed to commit %s", GetPath().c_str());
return success;
}
void
FileOutputStream::Cancel()
{
assert(IsDefined());
Close();
#ifdef HAVE_LINKAT
if (!is_tmpfile)
#endif
RemoveFile(GetPath());
}
#ifdef WIN32
FileOutputStream::FileOutputStream(Path _path, Error &error)
:path(_path),
handle(CreateFile(path.c_str(), GENERIC_WRITE, 0, nullptr,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH,
nullptr))
{
if (handle == INVALID_HANDLE_VALUE) {
const auto path_utf8 = path.ToUTF8();
error.FormatLastError("Failed to create %s",
path_utf8.c_str());
}
}
#else
AppendFileOutputStream::AppendFileOutputStream(Path _path, Error &error)
:BaseFileOutputStream(_path)
{
#ifdef WIN32
SetHandle(CreateFile(path.c_str(), GENERIC_WRITE, 0, nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH,
nullptr))
if (!IsDefined())
error.FormatLastError("Failed to append to %s",
GetPath().ToUTF8().c_str());
#else
if (!SetFD().Open(GetPath().c_str(),
O_WRONLY|O_APPEND))
error.FormatErrno("Failed to append to %s",
GetPath().c_str());
#endif
}
#endif
bool
AppendFileOutputStream::Commit(gcc_unused Error &error)
{
assert(IsDefined());
#ifdef WIN32
return Close();
#else
bool success = Close();
if (!success)
error.FormatErrno("Failed to commit %s", GetPath().c_str());
return success;
#endif
}
#endif
|