1 /*
2  * Copyright 2014-2019 Max Kellermann <max.kellermann@gmail.com>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
20  * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27  * OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "FileReader.hxx"
31 #include "fs/FileInfo.hxx"
32 #include "system/Error.hxx"
33 #include "io/Open.hxx"
34 
35 #include <cassert>
36 
37 #ifdef _WIN32
38 
FileReader(Path _path)39 FileReader::FileReader(Path _path)
40 	:path(_path),
41 	 handle(CreateFile(path.c_str(), GENERIC_READ, FILE_SHARE_READ,
42 			   nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
43 			   nullptr))
44 {
45 	if (handle == INVALID_HANDLE_VALUE)
46 		throw FormatLastError("Failed to open %s", path.ToUTF8().c_str());
47 }
48 
49 FileInfo
GetFileInfo() const50 FileReader::GetFileInfo() const
51 {
52 	assert(IsDefined());
53 
54 	return FileInfo(path);
55 }
56 
57 size_t
Read(void * data,size_t size)58 FileReader::Read(void *data, size_t size)
59 {
60 	assert(IsDefined());
61 
62 	DWORD nbytes;
63 	if (!ReadFile(handle, data, size, &nbytes, nullptr))
64 		throw FormatLastError("Failed to read from %s",
65 				      path.ToUTF8().c_str());
66 
67 	return nbytes;
68 }
69 
70 void
Seek(off_t offset)71 FileReader::Seek(off_t offset)
72 {
73 	assert(IsDefined());
74 
75 	auto result = SetFilePointer(handle, offset, nullptr, FILE_BEGIN);
76 	if (result == INVALID_SET_FILE_POINTER)
77 		throw MakeLastError("Failed to seek");
78 }
79 
80 void
Skip(off_t offset)81 FileReader::Skip(off_t offset)
82 {
83 	assert(IsDefined());
84 
85 	auto result = SetFilePointer(handle, offset, nullptr, FILE_CURRENT);
86 	if (result == INVALID_SET_FILE_POINTER)
87 		throw MakeLastError("Failed to seek");
88 }
89 
90 void
Close()91 FileReader::Close() noexcept
92 {
93 	assert(IsDefined());
94 
95 	CloseHandle(handle);
96 }
97 
98 #else
99 
FileReader(Path _path)100 FileReader::FileReader(Path _path)
101 	:path(_path), fd(OpenReadOnly(path.c_str()))
102 {
103 }
104 
105 FileInfo
GetFileInfo() const106 FileReader::GetFileInfo() const
107 {
108 	assert(IsDefined());
109 
110 	FileInfo info;
111 	const bool success = fstat(fd.Get(), &info.st) == 0;
112 	if (!success)
113 		throw FormatErrno("Failed to access %s",
114 				  path.ToUTF8().c_str());
115 
116 	return info;
117 }
118 
119 size_t
Read(void * data,size_t size)120 FileReader::Read(void *data, size_t size)
121 {
122 	assert(IsDefined());
123 
124 	ssize_t nbytes = fd.Read(data, size);
125 	if (nbytes < 0)
126 		throw FormatErrno("Failed to read from %s", path.ToUTF8().c_str());
127 
128 	return nbytes;
129 }
130 
131 void
Seek(off_t offset)132 FileReader::Seek(off_t offset)
133 {
134 	assert(IsDefined());
135 
136 	auto result = fd.Seek(offset);
137 	const bool success = result >= 0;
138 	if (!success)
139 		throw MakeErrno("Failed to seek");
140 }
141 
142 void
Skip(off_t offset)143 FileReader::Skip(off_t offset)
144 {
145 	assert(IsDefined());
146 
147 	auto result = fd.Skip(offset);
148 	const bool success = result >= 0;
149 	if (!success)
150 		throw MakeErrno("Failed to seek");
151 }
152 
153 void
Close()154 FileReader::Close() noexcept
155 {
156 	assert(IsDefined());
157 
158 	fd.Close();
159 }
160 
161 #endif
162