FileOutputStream.cxx 5.24 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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"
22
#include "system/Error.hxx"
23

24 25 26 27 28
FileOutputStream::FileOutputStream(Path _path, Mode _mode)
	:path(_path), mode(_mode)
{
	switch (mode) {
	case Mode::CREATE:
29 30 31 32 33
		OpenCreate(false);
		break;

	case Mode::CREATE_VISIBLE:
		OpenCreate(true);
34 35 36
		break;

	case Mode::APPEND_EXISTING:
37 38 39 40 41
		OpenAppend(false);
		break;

	case Mode::APPEND_OR_CREATE:
		OpenAppend(true);
42 43 44 45
		break;
	}
}

46 47
#ifdef WIN32

48
inline void
49
FileOutputStream::OpenCreate(gcc_unused bool visible)
50
{
51 52 53 54
	handle = CreateFile(path.c_str(), GENERIC_WRITE, 0, nullptr,
			    CREATE_ALWAYS,
			    FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH,
			    nullptr);
55
	if (!IsDefined())
56
		throw FormatLastError("Failed to create %s",
57 58 59 60
				      path.ToUTF8().c_str());
}

inline void
61
FileOutputStream::OpenAppend(bool create)
62 63
{
	handle = CreateFile(path.c_str(), GENERIC_WRITE, 0, nullptr,
64
			    create ? OPEN_ALWAYS : OPEN_EXISTING,
65 66 67 68 69 70 71 72 73 74 75 76 77
			    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());
	}

78 79
}

80
uint64_t
81
FileOutputStream::Tell() const
82 83 84 85 86 87 88 89 90
{
	LONG high = 0;
	DWORD low = SetFilePointer(handle, 0, &high, FILE_CURRENT);
	if (low == 0xffffffff)
		return 0;

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

91
void
92
FileOutputStream::Write(const void *data, size_t size)
93 94 95 96
{
	assert(IsDefined());

	DWORD nbytes;
97 98 99
	if (!WriteFile(handle, data, size, &nbytes, nullptr))
		throw FormatLastError("Failed to write to %s",
				      GetPath().c_str());
100

101 102 103
	if (size_t(nbytes) != size)
		throw FormatLastError(ERROR_DISK_FULL, "Failed to write to %s",
				      GetPath().c_str());
104 105
}

106 107
void
FileOutputStream::Commit()
108 109 110
{
	assert(IsDefined());

111
	Close();
112 113 114 115 116 117 118
}

void
FileOutputStream::Cancel()
{
	assert(IsDefined());

119
	Close();
120

121
	DeleteFile(GetPath().c_str());
122 123 124 125 126 127 128 129
}

#else

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

130 131 132 133 134 135 136 137 138 139 140
#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.
 */
141 142
static bool
OpenTempFile(FileDescriptor &fd, Path path)
143 144 145
{
	const auto directory = path.GetDirectoryName();
	if (directory.IsNull())
146
		return false;
147

148
	return fd.Open(directory.c_str(), O_TMPFILE|O_WRONLY, 0666);
149 150 151 152
}

#endif /* HAVE_LINKAT */

153
inline void
154
FileOutputStream::OpenCreate(bool visible)
155
{
156 157
#ifdef HAVE_LINKAT
	/* try Linux's O_TMPFILE first */
158
	is_tmpfile = !visible && OpenTempFile(fd, GetPath());
159 160 161
	if (!is_tmpfile) {
#endif
		/* fall back to plain POSIX */
162 163 164
		if (!fd.Open(GetPath().c_str(),
			     O_WRONLY|O_CREAT|O_TRUNC,
			     0666))
165
			throw FormatErrno("Failed to create %s",
166
					  GetPath().c_str());
167 168
#ifdef HAVE_LINKAT
	}
169 170
#else
	(void)visible;
171
#endif
172 173
}

174
inline void
175
FileOutputStream::OpenAppend(bool create)
176
{
177 178 179 180 181
	int flags = O_WRONLY|O_APPEND;
	if (create)
		flags |= O_CREAT;

	if (!fd.Open(path.c_str(), flags))
182 183 184 185
		throw FormatErrno("Failed to append to %s",
				  path.c_str());
}

186
uint64_t
187
FileOutputStream::Tell() const
188 189 190 191
{
	return fd.Tell();
}

192
void
193
FileOutputStream::Write(const void *data, size_t size)
194 195 196
{
	assert(IsDefined());

197
	ssize_t nbytes = fd.Write(data, size);
198 199 200 201 202
	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());
203 204
}

205 206
void
FileOutputStream::Commit()
207 208 209
{
	assert(IsDefined());

210
#ifdef HAVE_LINKAT
211
	if (is_tmpfile) {
212
		unlink(GetPath().c_str());
213 214 215

		/* hard-link the temporary file to the final path */
		char fd_path[64];
216
		snprintf(fd_path, sizeof(fd_path), "/proc/self/fd/%d",
217 218
			 fd.Get());
		if (linkat(AT_FDCWD, fd_path, AT_FDCWD, path.c_str(),
219 220
			   AT_SYMLINK_FOLLOW) < 0)
			throw FormatErrno("Failed to commit %s",
221
					  path.c_str());
222 223 224
	}
#endif

225 226 227
	if (!Close()) {
#ifdef WIN32
		throw FormatLastError("Failed to commit %s",
228
				      path.ToUTF8().c_str());
229
#else
230
		throw FormatErrno("Failed to commit %s", path.c_str());
231 232
#endif
	}
233 234 235 236 237 238 239
}

void
FileOutputStream::Cancel()
{
	assert(IsDefined());

240
	Close();
241

242 243
	switch (mode) {
	case Mode::CREATE:
244
#ifdef HAVE_LINKAT
245
		if (!is_tmpfile)
246
#endif
247 248
			unlink(GetPath().c_str());
		break;
249

250
	case Mode::CREATE_VISIBLE:
251
	case Mode::APPEND_EXISTING:
252
	case Mode::APPEND_OR_CREATE:
253 254
		/* can't roll this back */
		break;
255
	}
256 257 258
}

#endif