FileOutputStream.cxx 5.02 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 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"
23
#include "system/Error.hxx"
24

25 26
#ifdef WIN32

27
FileOutputStream::FileOutputStream(Path _path)
28
	:BaseFileOutputStream(_path)
29
{
30 31 32 33 34
	SetHandle(CreateFile(_path.c_str(), GENERIC_WRITE, 0, nullptr,
			     CREATE_ALWAYS,
			     FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH,
			     nullptr));
	if (!IsDefined())
35
		throw FormatLastError("Failed to create %s",
36
				      GetPath().ToUTF8().c_str());
37 38
}

39 40 41 42 43 44 45 46 47 48 49
uint64_t
BaseFileOutputStream::Tell() const
{
	LONG high = 0;
	DWORD low = SetFilePointer(handle, 0, &high, FILE_CURRENT);
	if (low == 0xffffffff)
		return 0;

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

50 51
void
BaseFileOutputStream::Write(const void *data, size_t size)
52 53 54 55
{
	assert(IsDefined());

	DWORD nbytes;
56 57 58
	if (!WriteFile(handle, data, size, &nbytes, nullptr))
		throw FormatLastError("Failed to write to %s",
				      GetPath().c_str());
59

60 61 62
	if (size_t(nbytes) != size)
		throw FormatLastError(ERROR_DISK_FULL, "Failed to write to %s",
				      GetPath().c_str());
63 64
}

65 66
void
FileOutputStream::Commit()
67 68 69
{
	assert(IsDefined());

70
	Close();
71 72 73 74 75 76 77
}

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

78 79
	Close();
	RemoveFile(GetPath());
80 81 82 83 84 85 86 87
}

#else

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

88 89 90 91 92 93 94 95 96 97 98
#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.
 */
99 100
static bool
OpenTempFile(FileDescriptor &fd, Path path)
101 102 103
{
	const auto directory = path.GetDirectoryName();
	if (directory.IsNull())
104
		return false;
105

106
	return fd.Open(directory.c_str(), O_TMPFILE|O_WRONLY, 0666);
107 108 109 110
}

#endif /* HAVE_LINKAT */

111
FileOutputStream::FileOutputStream(Path _path)
112
	:BaseFileOutputStream(_path)
113
{
114 115
#ifdef HAVE_LINKAT
	/* try Linux's O_TMPFILE first */
116
	is_tmpfile = OpenTempFile(SetFD(), GetPath());
117 118 119
	if (!is_tmpfile) {
#endif
		/* fall back to plain POSIX */
120 121 122
		if (!SetFD().Open(GetPath().c_str(),
				  O_WRONLY|O_CREAT|O_TRUNC,
				  0666))
123
			throw FormatErrno("Failed to create %s",
124
					  GetPath().c_str());
125 126 127
#ifdef HAVE_LINKAT
	}
#endif
128 129
}

130 131 132 133 134 135
uint64_t
BaseFileOutputStream::Tell() const
{
	return fd.Tell();
}

136 137
void
BaseFileOutputStream::Write(const void *data, size_t size)
138 139 140
{
	assert(IsDefined());

141
	ssize_t nbytes = fd.Write(data, size);
142 143 144 145 146
	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());
147 148
}

149 150
void
FileOutputStream::Commit()
151 152 153
{
	assert(IsDefined());

154 155
#if HAVE_LINKAT
	if (is_tmpfile) {
156
		RemoveFile(GetPath());
157 158 159

		/* hard-link the temporary file to the final path */
		char fd_path[64];
160
		snprintf(fd_path, sizeof(fd_path), "/proc/self/fd/%d",
161 162
			 GetFD().Get());
		if (linkat(AT_FDCWD, fd_path, AT_FDCWD, GetPath().c_str(),
163 164
			   AT_SYMLINK_FOLLOW) < 0)
			throw FormatErrno("Failed to commit %s",
165
					  GetPath().c_str());
166 167 168
	}
#endif

169 170 171 172 173 174 175 176
	if (!Close()) {
#ifdef WIN32
		throw FormatLastError("Failed to commit %s",
				      GetPath().ToUTF8().c_str());
#else
		throw FormatErrno("Failed to commit %s", GetPath().c_str());
#endif
	}
177 178 179 180 181 182 183
}

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

184
	Close();
185

186 187 188
#ifdef HAVE_LINKAT
	if (!is_tmpfile)
#endif
189
		RemoveFile(GetPath());
190 191
}

192
#endif
193

194
AppendFileOutputStream::AppendFileOutputStream(Path _path)
195 196 197
	:BaseFileOutputStream(_path)
{
#ifdef WIN32
198
	SetHandle(CreateFile(GetPath().c_str(), GENERIC_WRITE, 0, nullptr,
199 200
			     OPEN_EXISTING,
			     FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH,
201
			     nullptr));
202
	if (!IsDefined())
203
		throw FormatLastError("Failed to append to %s",
204
				      GetPath().ToUTF8().c_str());
205 206

	if (!SeekEOF()) {
207
		auto code = GetLastError();
208
		Close();
209 210
		throw FormatLastError(code, "Failed seek end-of-file of %s",
				      GetPath().ToUTF8().c_str());
211
	}
212 213 214
#else
	if (!SetFD().Open(GetPath().c_str(),
			  O_WRONLY|O_APPEND))
215
		throw FormatErrno("Failed to append to %s",
216 217 218 219
				  GetPath().c_str());
#endif
}

220 221
void
AppendFileOutputStream::Commit()
222 223 224
{
	assert(IsDefined());

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