/
nativefile.cpp
256 lines (217 loc) · 6.81 KB
/
nativefile.cpp
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* The Doomsday Engine Project -- libcore
*
* Copyright © 2009-2021 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 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 Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/nativefile.h"
#include "de/directoryfeed.h"
#include "de/guard.h"
#include "de/math.h"
#include "de/waitable.h"
#include <the_Foundation/file.h>
#include <the_Foundation/thread.h> // debug prints
#include <iostream>
#include <deque>
#include <atomic>
namespace de {
static std::atomic_int s_openFileCount;
DE_PIMPL(NativeFile)
{
NativePath nativePath; // Path of the native file in the OS file system.
iFile * file = nullptr; // NOTE: One NativeFile shouldn't be accessed by multiple
// threads simultaneously (each read/write mutexed).
Impl(Public *i)
: Base(i)
{}
~Impl()
{
DE_ASSERT(!file);
}
iFile *getFile()
{
if (!file)
{
// We need to keep track of how many native files are open at once, because
// operating systems have a hard limit on this. We shouldn't open and close
// a file for each read/write, because that's very slow, but we shouldn't keep
// files open for a long time either. Note that files may be accessed
// from multiple threads, although usually not simultanously.
s_openFileCount++;
// std::cout << s_openFileCount << " File opened: " << this << " (" << nativePath.asText() << ") "
// << "thread:" << current_Thread() << std::endl;
assert(s_openFileCount < 50);
file = new_File(nativePath.toString());
const bool isWrite = self().mode().testFlag(Write);
// Open with Append mode so that missing files will get created.
// Seek position will be updated anyway when something is written to the file.
if (!open_File(file, isWrite ? append_FileMode | readWrite_FileMode : readOnly_FileMode))
{
iReleasePtr(&file);
if (isWrite)
{
throw OutputError("NativeFile::getFile",
Stringf("Failed to write (%s)", strerror(errno)));
}
else
{
throw InputError("NativeFile::getFile", "Failed to read " + nativePath);
}
}
}
return file;
}
void closeFile()
{
if (file)
{
s_openFileCount--;
// std::cout << s_openFileCount << " File closed: " << this << std::endl;
iReleasePtr(&file);
}
}
};
NativeFile::NativeFile(const String &name, const NativePath &nativePath)
: ByteArrayFile(name), d(new Impl(this))
{
d->nativePath = nativePath;
}
NativeFile::~NativeFile()
{
DE_GUARD(this);
DE_NOTIFY(Deletion, i) i->fileBeingDeleted(*this);
audienceForDeletion().clear();
close();
deindex();
}
String NativeFile::describe() const
{
DE_GUARD(this);
return Stringf("\"%s\"", d->nativePath.pretty().c_str());
}
Block NativeFile::metaId() const
{
// Special exception: application's persistent data store will be rewritten on every
// run so there's no point in caching it.
if (name() == DE_STR("persist.pack"))
{
return Block();
}
return Block(File::metaId() + d->nativePath.toUtf8()).md5Hash();
}
void NativeFile::close()
{
release();
}
void NativeFile::release() const
{
DE_GUARD(this);
d->closeFile();
DE_ASSERT(!d->file);
}
const NativePath &NativeFile::nativePath() const
{
DE_GUARD(this);
return d->nativePath;
}
void NativeFile::clear()
{
DE_GUARD(this);
File::clear(); // checks for write access
d->closeFile();
if (remove(d->nativePath.toString().c_str()))
{
if (errno != ENOENT)
{
throw OutputError("NativeFile::clear",
"Failed to clear " + d->nativePath +
Stringf(" (%s)", strerror(errno)));
}
}
Status st = status();
st.size = 0;
st.modifiedAt = Time();
setStatus(st);
}
NativeFile::Size NativeFile::size() const
{
DE_GUARD(this);
return status().size;
}
void NativeFile::get(Offset at, Byte *values, Size count) const
{
DE_GUARD(this);
if (at + count > size())
{
d->closeFile();
/// @throw IByteArray::OffsetError The region specified for reading extends
/// beyond the bounds of the file.
throw OffsetError("NativeFile::get", description() + ": cannot read past end of file " +
Stringf("(%zu[+%zu] > %zu)", at, count, size()));
}
d->getFile();
if (pos_File(d->file) != at)
{
seek_File(d->file, at);
}
readData_File(d->file, count, values);
// Close the native input file after the full contents have been read.
if (at + count == size())
{
d->closeFile();
}
}
void NativeFile::set(Offset at, const Byte *values, Size count)
{
DE_GUARD(this);
if (at > size())
{
/// @throw IByteArray::OffsetError @a at specified a position beyond the
/// end of the file.
throw OffsetError("NativeFile::set", description() + ": cannot write past end of file");
}
d->getFile();
if (pos_File(d->file) != at)
{
seek_File(d->file, at);
}
if (writeData_File(d->file, values, count) != count)
{
throw OutputError("NativeFile::set",
description() + Stringf(": error writing to file (%s)", strerror(errno)));
}
// Update status.
Status st = status();
st.size = max(st.size, at + count);
st.modifiedAt = Time();
setStatus(st);
}
NativeFile *NativeFile::newStandalone(const NativePath &nativePath)
{
std::unique_ptr<NativeFile> file(new NativeFile(nativePath.fileName(), nativePath));
if (nativePath.exists())
{
// Sync status with the real status.
file->setStatus(DirectoryFeed::fileStatus(nativePath));
}
return file.release();
}
void NativeFile::setMode(const Flags &newMode)
{
DE_GUARD(this);
close();
File::setMode(newMode);
}
} // namespace de