aboutsummaryrefslogtreecommitdiffstats
path: root/src/fs/io/FileOutputStream.cxx
blob: 0eff8b5f0babcad81127a30752fcb515f4b3edd0 (plain) (blame)
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
/*
 * Copyright (C) 2003-2014 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 "system/fd_util.h"
#include "util/Error.hxx"

#ifdef WIN32

FileOutputStream::FileOutputStream(Path _path, Error &error)
	:path(_path),
	 handle(CreateFile(path.c_str(), GENERIC_WRITE, 0, nullptr,
			   TRUNCATE_EXISTING,
			   FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH,
			   nullptr))
{
	if (handle == INVALID_HANDLE_VALUE)
		error.FormatLastError("Failed to create %s", path.c_str());
}

bool
FileOutputStream::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.c_str());
		return false;
	}

	if (size_t(nbytes) != size) {
		error.FormatLastError(ERROR_DISK_FULL,
				      "Failed to write to %s", path.c_str());
		return false;
	}

	return true;
}

bool
FileOutputStream::Commit(gcc_unused Error &error)
{
	assert(IsDefined());

	CloseHandle(handle);
	handle = INVALID_HANDLE_VALUE;
	return true;
}

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

	CloseHandle(handle);
	handle = INVALID_HANDLE_VALUE;
	RemoveFile(path);
}

#else

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

FileOutputStream::FileOutputStream(Path _path, Error &error)
	:path(_path),
	 fd(open_cloexec(path.c_str(),
			 O_WRONLY|O_CREAT|O_TRUNC,
			 0666))
{
	if (fd < 0)
		error.FormatErrno("Failed to create %s", path.c_str());
}

bool
FileOutputStream::Write(const void *data, size_t size, Error &error)
{
	assert(IsDefined());

	ssize_t nbytes = write(fd, data, size);
	if (nbytes < 0) {
		error.FormatErrno("Failed to write to %s", path.c_str());
		return false;
	} else if ((size_t)nbytes < size) {
		error.FormatErrno(ENOSPC,
				  "Failed to write to %s", path.c_str());
		return false;
	}

	return true;
}

bool
FileOutputStream::Commit(Error &error)
{
	assert(IsDefined());

	bool success = close(fd) == 0;
	fd = -1;
	if (!success)
		error.FormatErrno("Failed to commit %s", path.c_str());

	return success;
}

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

	close(fd);
	fd = -1;

	RemoveFile(path);
}

#endif