FileOutputStream.cxx 6.54 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2018 Max Kellermann <max.kellermann@gmail.com>
3
 *
4 5 6
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
7
 *
8 9
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
10
 *
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 29 30
 */

#include "FileOutputStream.hxx"
31
#include "system/Error.hxx"
32
#include "util/StringFormat.hxx"
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#ifdef __linux__
#include <fcntl.h>
#endif

#ifdef __linux__
FileOutputStream::FileOutputStream(FileDescriptor _directory_fd,
				   Path _path, Mode _mode)
	:path(_path),
	 directory_fd(_directory_fd),
	 mode(_mode)
{
	Open();
}
#endif

49
FileOutputStream::FileOutputStream(Path _path, Mode _mode)
50 51 52 53 54
	:path(_path),
#ifdef __linux__
	 directory_fd(AT_FDCWD),
#endif
	 mode(_mode)
55 56 57 58 59 60
{
	Open();
}

inline void
FileOutputStream::Open()
61 62 63
{
	switch (mode) {
	case Mode::CREATE:
64 65 66 67 68
		OpenCreate(false);
		break;

	case Mode::CREATE_VISIBLE:
		OpenCreate(true);
69 70 71
		break;

	case Mode::APPEND_EXISTING:
72 73 74 75 76
		OpenAppend(false);
		break;

	case Mode::APPEND_OR_CREATE:
		OpenAppend(true);
77 78 79 80
		break;
	}
}

81
#ifdef _WIN32
82

83
inline void
84
FileOutputStream::OpenCreate(gcc_unused bool visible)
85
{
86 87 88 89
	handle = CreateFile(path.c_str(), GENERIC_WRITE, 0, nullptr,
			    CREATE_ALWAYS,
			    FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH,
			    nullptr);
90
	if (!IsDefined())
91
		throw FormatLastError("Failed to create %s",
92 93 94 95
				      path.ToUTF8().c_str());
}

inline void
96
FileOutputStream::OpenAppend(bool create)
97 98
{
	handle = CreateFile(path.c_str(), GENERIC_WRITE, 0, nullptr,
99
			    create ? OPEN_ALWAYS : OPEN_EXISTING,
100 101 102 103 104 105 106 107 108 109 110 111 112
			    FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH,
			    nullptr);
	if (!IsDefined())
		throw FormatLastError("Failed to append to %s",
				      path.ToUTF8().c_str());

	if (!SeekEOF()) {
		auto code = GetLastError();
		Close();
		throw FormatLastError(code, "Failed seek end-of-file of %s",
				      path.ToUTF8().c_str());
	}

113 114
}

115
uint64_t
116
FileOutputStream::Tell() const noexcept
117 118 119 120 121 122 123 124 125
{
	LONG high = 0;
	DWORD low = SetFilePointer(handle, 0, &high, FILE_CURRENT);
	if (low == 0xffffffff)
		return 0;

	return uint64_t(high) << 32 | uint64_t(low);
}

126
void
127
FileOutputStream::Write(const void *data, size_t size)
128 129 130 131
{
	assert(IsDefined());

	DWORD nbytes;
132 133 134
	if (!WriteFile(handle, data, size, &nbytes, nullptr))
		throw FormatLastError("Failed to write to %s",
				      GetPath().c_str());
135

136 137 138
	if (size_t(nbytes) != size)
		throw FormatLastError(ERROR_DISK_FULL, "Failed to write to %s",
				      GetPath().c_str());
139 140
}

141 142
void
FileOutputStream::Commit()
143 144 145
{
	assert(IsDefined());

146
	Close();
147 148 149
}

void
150
FileOutputStream::Cancel() noexcept
151 152 153
{
	assert(IsDefined());

154
	Close();
155

156
	DeleteFile(GetPath().c_str());
157 158 159 160 161 162 163 164
}

#else

#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

165
#ifdef HAVE_O_TMPFILE
166 167 168 169 170 171 172 173 174 175
#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.
 */
176
static bool
177 178
OpenTempFile(FileDescriptor directory_fd,
	     FileDescriptor &fd, Path path)
179
{
180 181 182
	if (directory_fd != FileDescriptor(AT_FDCWD))
		return fd.Open(directory_fd, ".", O_TMPFILE|O_WRONLY, 0666);

183 184
	const auto directory = path.GetDirectoryName();
	if (directory.IsNull())
185
		return false;
186

187
	return fd.Open(directory.c_str(), O_TMPFILE|O_WRONLY, 0666);
188 189
}

190
#endif /* HAVE_O_TMPFILE */
191

192
inline void
193
FileOutputStream::OpenCreate(bool visible)
194
{
195
#ifdef HAVE_O_TMPFILE
196
	/* try Linux's O_TMPFILE first */
197
	is_tmpfile = !visible && OpenTempFile(directory_fd, fd, GetPath());
198 199 200
	if (!is_tmpfile) {
#endif
		/* fall back to plain POSIX */
201 202 203 204 205
		if (!fd.Open(
#ifdef __linux__
			     directory_fd,
#endif
			     GetPath().c_str(),
206 207
			     O_WRONLY|O_CREAT|O_TRUNC,
			     0666))
208
			throw FormatErrno("Failed to create %s",
209
					  GetPath().c_str());
210
#ifdef HAVE_O_TMPFILE
211
	}
212 213
#else
	(void)visible;
214
#endif
215 216
}

217
inline void
218
FileOutputStream::OpenAppend(bool create)
219
{
220 221 222 223
	int flags = O_WRONLY|O_APPEND;
	if (create)
		flags |= O_CREAT;

224 225 226 227 228
	if (!fd.Open(
#ifdef __linux__
		     directory_fd,
#endif
		     path.c_str(), flags))
229 230 231 232
		throw FormatErrno("Failed to append to %s",
				  path.c_str());
}

233
uint64_t
234
FileOutputStream::Tell() const noexcept
235 236 237 238
{
	return fd.Tell();
}

239
void
240
FileOutputStream::Write(const void *data, size_t size)
241 242 243
{
	assert(IsDefined());

244
	ssize_t nbytes = fd.Write(data, size);
245 246 247 248 249
	if (nbytes < 0)
		throw FormatErrno("Failed to write to %s", GetPath().c_str());
	else if ((size_t)nbytes < size)
		throw FormatErrno(ENOSPC, "Failed to write to %s",
				  GetPath().c_str());
250 251
}

252 253
void
FileOutputStream::Commit()
254 255 256
{
	assert(IsDefined());

257
#ifdef HAVE_O_TMPFILE
258
	if (is_tmpfile) {
259
		unlinkat(directory_fd.Get(), GetPath().c_str(), 0);
260 261

		/* hard-link the temporary file to the final path */
262 263
		if (linkat(AT_FDCWD,
			   StringFormat<64>("/proc/self/fd/%d", fd.Get()),
264
			   directory_fd.Get(), path.c_str(),
265 266
			   AT_SYMLINK_FOLLOW) < 0)
			throw FormatErrno("Failed to commit %s",
267
					  path.c_str());
268 269 270
	}
#endif

271
	if (!Close()) {
272
#ifdef _WIN32
273
		throw FormatLastError("Failed to commit %s",
274
				      path.ToUTF8().c_str());
275
#else
276
		throw FormatErrno("Failed to commit %s", path.c_str());
277 278
#endif
	}
279 280 281
}

void
282
FileOutputStream::Cancel() noexcept
283 284 285
{
	assert(IsDefined());

286
	Close();
287

288 289
	switch (mode) {
	case Mode::CREATE:
290
#ifdef HAVE_O_TMPFILE
291
		if (!is_tmpfile)
292
#endif
293 294 295 296 297
#ifdef __linux__
			unlinkat(directory_fd.Get(), GetPath().c_str(), 0);
#else
		unlink(GetPath().c_str());
#endif
298
		break;
299

300
	case Mode::CREATE_VISIBLE:
301
	case Mode::APPEND_EXISTING:
302
	case Mode::APPEND_OR_CREATE:
303 304
		/* can't roll this back */
		break;
305
	}
306 307 308
}

#endif