forked from andreasbuhr/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwritable_file.cpp
More file actions
119 lines (107 loc) · 2.39 KB
/
Copy pathwritable_file.cpp
File metadata and controls
119 lines (107 loc) · 2.39 KB
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
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/writable_file.hpp>
#include <system_error>
#if CPPCORO_OS_WINNT
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h>
void cppcoro::writable_file::set_size(
std::uint64_t fileSize)
{
LARGE_INTEGER position;
position.QuadPart = fileSize;
BOOL ok = ::SetFilePointerEx(m_fileHandle.handle(), position, nullptr, FILE_BEGIN);
if (!ok)
{
DWORD errorCode = ::GetLastError();
throw std::system_error
{
static_cast<int>(errorCode),
std::system_category(),
"error setting file size: SetFilePointerEx"
};
}
ok = ::SetEndOfFile(m_fileHandle.handle());
if (!ok)
{
DWORD errorCode = ::GetLastError();
throw std::system_error
{
static_cast<int>(errorCode),
std::system_category(),
"error setting file size: SetEndOfFile"
};
}
}
cppcoro::file_write_operation cppcoro::writable_file::write(
std::uint64_t offset,
const void* buffer,
std::size_t byteCount) noexcept
{
return file_write_operation{
m_fileHandle.handle(),
offset,
buffer,
byteCount
};
}
cppcoro::file_write_operation_cancellable cppcoro::writable_file::write(
std::uint64_t offset,
const void* buffer,
std::size_t byteCount,
cancellation_token ct) noexcept
{
return file_write_operation_cancellable{
m_fileHandle.handle(),
offset,
buffer,
byteCount,
std::move(ct)
};
}
#elif CPPCORO_OS_LINUX
#include <unistd.h>
void cppcoro::writable_file::set_size(
std::uint64_t fileSize)
{
if (ftruncate64(m_fileData.fd.fd(), fileSize) < 0)
{
throw std::system_error
{
errno,
std::system_category(),
"error setting file size: ftruncate64"
};
}
}
cppcoro::file_write_operation cppcoro::writable_file::write(
std::uint64_t offset,
const void* buffer,
std::size_t byteCount) noexcept
{
return file_write_operation(
m_fileData.fd.fd(),
m_fileData.mq,
offset,
buffer,
byteCount);
}
cppcoro::file_write_operation_cancellable cppcoro::writable_file::write(
std::uint64_t offset,
const void* buffer,
std::size_t byteCount,
cancellation_token ct) noexcept
{
return file_write_operation_cancellable(
m_fileData.fd.fd(),
m_fileData.mq,
offset,
buffer,
byteCount,
std::move(ct));
}
#endif