-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathFileStream_Unix.cpp
282 lines (221 loc) · 6.23 KB
/
FileStream_Unix.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "io/FileStream.h"
#include "Util.h"
#include "util/Log.h"
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
//----------------------------------------------------------
bool FileStream::Open( const char* path, FileMode mode, FileAccess access, FileFlags flags )
{
return Open( path, *this, mode, access, flags );
}
void* fallocate_in_background(void* rawfd) {
int fd = *(int*)rawfd;
pthread_detach(pthread_self());
Log::Line("Fallocating files...");
struct timeval begin, end;
gettimeofday(&begin, 0);
off_t fallocate_len = 108447924224; // 101 GiB
int res = fallocate(fd, 0, 0, fallocate_len);
gettimeofday(&end, 0);
if (res < 0) {
Log::Line("Fallocate failed, errno %d", errno);
} else {
long seconds = end.tv_sec - begin.tv_sec;
long microseconds = end.tv_usec - begin.tv_usec;
double elapsed = seconds + microseconds*1e-6;
Log::Line("Fallocate succeeded in %.2f seconds.", elapsed);
}
pthread_exit(nullptr);
}
//----------------------------------------------------------
bool FileStream::Open( const char* path, FileStream& file, FileMode mode, FileAccess access, FileFlags flags )
{
if( path == nullptr )
return false;
if( file._fd >= 0 )
return false;
if( access == FileAccess::None )
access = FileAccess::Read;
mode_t fmode = 0;
int fdFlags = access == FileAccess::Read ? O_RDONLY :
access == FileAccess::Write ? O_WRONLY : O_RDWR;
fdFlags |= mode == FileMode::Create ? O_CREAT :
mode == FileMode::Append ? O_APPEND : 0;
#if PLATFORM_IS_LINUX
if( IsFlagSet( flags, FileFlags::NoBuffering ) )
fdFlags |= O_DIRECT | O_SYNC;
if( IsFlagSet( flags, FileFlags::LargeFile ) )
fdFlags |= O_LARGEFILE;
#endif
if( mode == FileMode::Create )
fmode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
int fd = open( path, fdFlags, fmode );
if( fd < 0 )
return false;
pthread_t t;
pthread_create(&t, NULL, fallocate_in_background, &fd);
#if PLATFORM_IS_MACOS
if( IsFlagSet( flags, FileFlags::NoBuffering ) )
{
int r = fcntl( fd, F_NOCACHE, 1 );
if( r == -1 )
{
file._error = errno;
close( fd );
return false;
}
}
#endif
// Get the block size (useful when using O_DIRECT)
int blockSize = 0;
{
struct stat fs;
int r = fstat( fd, &fs );
if( r == 0 )
{
blockSize = (size_t)fs.st_blksize;
}
else
{
file._error = errno;
close( fd );
return false;
}
ASSERT( blockSize > 0 );
}
file._fd = fd;
file._blockSize = (size_t)blockSize;
file._writePosition = 0;
file._readPosition = 0;
file._access = access;
file._flags = flags;
file._error = 0;
return true;
}
//-----------------------------------------------------------
void FileStream::Close()
{
if( _fd <= 0 )
return;
#if _DEBUG
int r =
#endif
close( _fd );
ASSERT( !r );
_fd = -1;
_writePosition = 0;
_readPosition = 0;
_access = FileAccess::None;
_error = 0;
_blockSize = 0;
}
//-----------------------------------------------------------
ssize_t FileStream::Read( void* buffer, size_t size )
{
ASSERT( buffer );
if( buffer == nullptr )
return -1;
if( ! IsFlagSet( _access, FileAccess::Read ) )
return -1;
if( _fd < 0 )
return -1;
if( size < 1 )
return 0;
const ssize_t sizeRead = read( _fd, buffer, size );
if( sizeRead >= 0 )
_readPosition += (size_t)sizeRead;
else
_error = errno;
return sizeRead;
}
//-----------------------------------------------------------
ssize_t FileStream::Write( const void* buffer, size_t size )
{
ASSERT( buffer );
ASSERT( size );
ASSERT( _fd );
if( buffer == nullptr )
return -1;
if( ! IsFlagSet( _access, FileAccess::Write ) )
return -1;
if( _fd < 0 )
return -1;
if( size < 1 )
return 0;
// Note that this can return less than size if size > SSIZE_MAX
ssize_t written = write( _fd, buffer, size );
if( written >= 0 )
_writePosition += (size_t)written;
else
_error = errno;
return written;
}
//----------------------------------------------------------
bool FileStream::Reserve( ssize_t size )
{
#if PLATFORM_IS_LINUX
int r = posix_fallocate( _fd, 0, (off_t)size );
if( r != 0 )
{
_error = errno;
return false;
}
#else
// #TODO: Use F_PREALLOCATE on macOS
// #SEE: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/fcntl.2.html
return false;
#endif
return true;
}
//----------------------------------------------------------
bool FileStream::Seek( int64 offset, SeekOrigin origin )
{
if( !IsOpen() )
return false;
int whence;
switch( origin )
{
case SeekOrigin::Begin : whence = SEEK_SET; break;
case SeekOrigin::Current: whence = SEEK_CUR; break;
case SeekOrigin::End : whence = SEEK_END; break;
default: return false;
}
off_t r = lseek( _fd, (off_t)offset, whence );
if( r == -1 )
{
_error = errno;
return false;
}
return true;
}
//-----------------------------------------------------------
bool FileStream::Flush()
{
if( !IsOpen() )
return false;
int r = fsync( _fd );
if( r )
{
_error = errno;
return false;
}
return true;
}
//-----------------------------------------------------------
bool FileStream::IsOpen() const
{
return _fd >= 0;
}
//-----------------------------------------------------------
bool FileStream::Exists( const char* path )
{
struct stat fileStat;
if( lstat( path, &fileStat ) == 0 )
{
// #TODO: Check if it is a folder...
return true;
}
return false;
}