aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/fastos/unix_file.cpp
blob: e8c30468a344025013ea5919edb28a55d59678b3 (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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
******************************************************************************
* @author  Oivind H. Danielsen
* @date    Creation date: 2000-01-18
* @file
* Implementation of FastOS_UNIX_File methods.
*****************************************************************************/

#include "file.h"
#include <sstream>
#include <cassert>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/mman.h>
#ifdef __linux__
#include <sys/vfs.h>
#else
#include <sys/mount.h>
#endif
#ifdef __APPLE__
#include <libproc.h>
#include <sys/proc_info.h>
#endif
#include "file_rw_ops.h"

using fastos::File_RW_Ops;

int FastOS_UNIX_File::GetLastOSError() {
    return errno;
}

ssize_t
FastOS_UNIX_File::Read(void *buffer, size_t len)
{
    return File_RW_Ops::read(_filedes, buffer, len);
}


ssize_t
FastOS_UNIX_File::Write2(const void *buffer, size_t len)
{
    return File_RW_Ops::write(_filedes, buffer, len);
}

bool
FastOS_UNIX_File::SetPosition(int64_t desiredPosition)
{
    int64_t position = lseek(_filedes, desiredPosition, SEEK_SET);

    return (position == desiredPosition);
}


int64_t
FastOS_UNIX_File::GetPosition()
{
    return lseek(_filedes, 0, SEEK_CUR);
}

void FastOS_UNIX_File::ReadBuf(void *buffer, size_t length,
                               int64_t readOffset)
{
    ssize_t readResult;

    readResult = File_RW_Ops::pread(_filedes, buffer, length, readOffset);
    if (static_cast<size_t>(readResult) != length) {
        std::string errorString = readResult != -1 ?
                                  std::string("short read") :
                                  FastOS_FileInterface::getLastErrorString();
        std::ostringstream os;
        os << "Fatal: Reading " << length << " bytes, got " << readResult << " from '"
           << GetFileName() << "' failed: " << errorString;
        throw std::runtime_error(os.str());
    }
}

bool
FastOS_UNIX_File::Stat(const char *filename, FastOS_StatInfo *statInfo)
{
    bool rc = false;

    struct stat stbuf{};
    int lstatres;

    do {
        lstatres = lstat(filename, &stbuf);
    } while (lstatres == -1 && errno == EINTR);
    if (lstatres == 0) {
        statInfo->_error = FastOS_StatInfo::Ok;
        statInfo->_isRegular = S_ISREG(stbuf.st_mode);
        statInfo->_isDirectory = S_ISDIR(stbuf.st_mode);
        statInfo->_size = static_cast<int64_t>(stbuf.st_size);
        uint64_t modTimeNS = stbuf.st_mtime * 1000 * 1000 * 1000lu;
#ifdef __linux__
        modTimeNS += stbuf.st_mtim.tv_nsec;
#elif defined(__APPLE__)
        modTimeNS += stbuf.st_mtimespec.tv_nsec;
#endif
        statInfo->_modifiedTime = vespalib::system_time(std::chrono::nanoseconds(modTimeNS));
        rc = true;
    } else {
        if (errno == ENOENT) {
            statInfo->_error = FastOS_StatInfo::FileNotFound;
        } else {
            statInfo->_error = FastOS_StatInfo::Unknown;
        }
    }

    return rc;
}

bool FastOS_UNIX_File::SetCurrentDirectory (const char *pathName) { return (chdir(pathName) == 0); }


int FastOS_UNIX_File::GetMaximumFilenameLength (const char *pathName)
{
    return pathconf(pathName, _PC_NAME_MAX);
}

int FastOS_UNIX_File::GetMaximumPathLength(const char *pathName)
{
    return pathconf(pathName, _PC_PATH_MAX);
}

std::string
FastOS_UNIX_File::getCurrentDirectory()
{
    std::string res;
    int maxPathLen = FastOS_File::GetMaximumPathLength(".");
    if (maxPathLen == -1) {
        maxPathLen = 16384;
    } else if (maxPathLen < 512) {
        maxPathLen = 512;
    }

    char *currentDir = new char [maxPathLen + 1];

    if (getcwd(currentDir, maxPathLen) != nullptr) {
        res = currentDir;
    }
    delete [] currentDir;

    return res;
}


unsigned int
FastOS_UNIX_File::CalcAccessFlags(unsigned int openFlags)
{
    unsigned int accessFlags=0;

    if ((openFlags & (FASTOS_FILE_OPEN_READ | FASTOS_FILE_OPEN_DIRECTIO)) != 0) {
        if ((openFlags & FASTOS_FILE_OPEN_WRITE) != 0) {
            // Open for reading and writing
            accessFlags = O_RDWR;
        } else {
            // Open for reading only
            accessFlags = O_RDONLY;
        }
    } else {
        // Open for writing only
        accessFlags = O_WRONLY;
    }

    if (((openFlags & FASTOS_FILE_OPEN_EXISTING) == 0) && ((openFlags & FASTOS_FILE_OPEN_WRITE) != 0)) {
        // Create file if it does not exist
        accessFlags |= O_CREAT;
    }

#if defined(O_SYNC)
    if ((openFlags & FASTOS_FILE_OPEN_SYNCWRITES) != 0)
        accessFlags |= O_SYNC;
#elif defined(O_FSYNC)
    if ((openFlags & FASTOS_FILE_OPEN_SYNCWRITES) != 0)
        accessFlags |= O_FSYNC;
#endif

#ifdef __linux__
    if ((openFlags & FASTOS_FILE_OPEN_DIRECTIO) != 0) {
        accessFlags |= O_DIRECT;
    }
#endif

    if ((openFlags & FASTOS_FILE_OPEN_TRUNCATE) != 0) {
        // Truncate file on open
        accessFlags |= O_TRUNC;
    }
    return accessFlags;
}

#ifdef __linux__
constexpr int ALWAYS_SUPPORTED_MMAP_FLAGS = ~MAP_HUGETLB;
#else
constexpr int ALWAYS_SUPPORTED_MMAP_FLAGS = ~0;
#endif

bool
FastOS_UNIX_File::Open(unsigned int openFlags, const char *filename)
{
    bool rc = false;
    assert(_filedes == -1);

    if ((openFlags & FASTOS_FILE_OPEN_STDFLAGS) != 0) {
        FILE *file;

        switch(openFlags & FASTOS_FILE_OPEN_STDFLAGS) {

        case FASTOS_FILE_OPEN_STDOUT:
            file = stdout;
            SetFileName("stdout");
            break;

        case FASTOS_FILE_OPEN_STDERR:
            file = stderr;
            SetFileName("stderr");
            break;

        default:
            fprintf(stderr, "Invalid open-flags %08X\n", openFlags);
            abort();
        }

#ifdef __linux__
        _filedes = file->_fileno;
#else
        _filedes = fileno(file);
#endif
        _openFlags = openFlags;
        rc = true;
    } else {
        if (filename != nullptr) {
            SetFileName(filename);
        }
        unsigned int accessFlags = CalcAccessFlags(openFlags);

        _filedes = open(_filename.c_str(), accessFlags, 0664);

        rc = (_filedes != -1);

        if (rc) {
            _openFlags = openFlags;
            if (_mmapEnabled) {
                int64_t filesize = GetSize();
                auto mlen = static_cast<size_t>(filesize);
                if ((static_cast<int64_t>(mlen) == filesize) && (mlen > 0)) {
                    void *mbase = mmap(nullptr, mlen, PROT_READ, MAP_SHARED | _mmapFlags, _filedes, 0);
                    if (mbase == MAP_FAILED) {
                        mbase = mmap(nullptr, mlen, PROT_READ, MAP_SHARED | (_mmapFlags & ALWAYS_SUPPORTED_MMAP_FLAGS), _filedes, 0);
                    }
                    if (mbase != MAP_FAILED) {
#ifdef __linux__
                        int fadviseOptions = getFAdviseOptions();
                        int eCode(0);
                        if (POSIX_FADV_RANDOM == fadviseOptions) {
                            eCode = posix_madvise(mbase, mlen, POSIX_MADV_RANDOM);
                        } else if (POSIX_FADV_SEQUENTIAL == fadviseOptions) {
                            eCode = posix_madvise(mbase, mlen, POSIX_MADV_SEQUENTIAL);
                        }
                        if (eCode != 0) {
                            fprintf(stderr, "Failed: posix_madvise(%p, %ld, %d) = %d\n", mbase, mlen, fadviseOptions, eCode);
                        }
#endif
                        _mmapbase = mbase;
                        _mmaplen = mlen;
                    } else {
                        close(_filedes);
                        _filedes = -1;
                        std::ostringstream os;
                        os << "mmap of file '" << GetFileName() << "' with flags '" << std::hex << (MAP_SHARED | _mmapFlags) << std::dec
                           << "' failed with error :'" << getErrorString(GetLastOSError()) << "'";
                        throw std::runtime_error(os.str());
                    }
                }
            }
        }

    }

    return rc;
}

void FastOS_UNIX_File::dropFromCache() const
{
#ifdef __linux__
    posix_fadvise(_filedes, 0, 0, POSIX_FADV_DONTNEED);
#endif
}


bool
FastOS_UNIX_File::Close()
{
    bool ok = true;

    if (_filedes >= 0) {
        if ((_openFlags & FASTOS_FILE_OPEN_STDFLAGS) != 0) {
            ok = true;
        } else {
            do {
                ok = (close(_filedes) == 0);
            } while (!ok && errno == EINTR);
        }

        if (_mmapbase != nullptr) {
            madvise(_mmapbase, _mmaplen, MADV_DONTNEED);
            munmap(static_cast<char *>(_mmapbase), _mmaplen);
            _mmapbase = nullptr;
            _mmaplen = 0;
        }

        _filedes = -1;
    }

    _openFlags = 0;

    return ok;
}


int64_t
FastOS_UNIX_File::GetSize()
{
    int64_t fileSize=-1;
    struct stat stbuf{};

    assert(IsOpened());

    int res = fstat(_filedes, &stbuf);

    if (res == 0) {
        fileSize = stbuf.st_size;
    }

    return fileSize;
}

bool
FastOS_UNIX_File::Delete(const char *name)
{
    return (unlink(name) == 0);
}


bool
FastOS_UNIX_File::Delete()
{
    assert( ! IsOpened());

    return (unlink(_filename.c_str()) == 0);
}

bool FastOS_UNIX_File::Rename (const char *currentFileName, const char *newFileName)
{
    bool rc = false;

    // Enforce documentation. If the destination file exists,
    // fail Rename.
    FastOS_StatInfo statInfo{};
    if (!FastOS_File::Stat(newFileName, &statInfo)) {
        rc = (rename(currentFileName, newFileName) == 0);
    } else {
        errno = EEXIST;
    }
    return rc;
}

bool
FastOS_UNIX_File::Sync()
{
    assert(IsOpened());

    return (fsync(_filedes) == 0);
}


bool
FastOS_UNIX_File::SetSize(int64_t newSize)
{
    bool rc = false;

    if (ftruncate(_filedes, static_cast<off_t>(newSize)) == 0) {
        rc = SetPosition(newSize);
    }

    return rc;
}


FastOS_File::Error
FastOS_UNIX_File::TranslateError (const int osError)
{
    switch(osError) {
    case ENOENT:     return ERR_NOENT;      // No such file or directory
    case ENOMEM:     return ERR_NOMEM;      // Not enough memory
    case EACCES:     return ERR_ACCES;      // Permission denied
    case EEXIST:     return ERR_EXIST;      // File exists
    case EINVAL:     return ERR_INVAL;      // Invalid argument
    case ENOSPC:     return ERR_NOSPC;      // No space left on device
    case EINTR:      return ERR_INTR;       // interrupt
    case EAGAIN:     return ERR_AGAIN;      // Resource unavailable, try again
    case EBUSY:      return ERR_BUSY;       // Device or resource busy
    case EIO:        return ERR_IO;         // I/O error
    case EPERM:      return ERR_PERM;       // Not owner
    case ENODEV:     return ERR_NODEV;      // No such device
    case ENXIO:      return ERR_NXIO;       // Device not configured
    default:         break;
    }

    if (osError == ENFILE)
        return ERR_NFILE;

    if (osError == EMFILE)
        return ERR_MFILE;

    return ERR_UNKNOWN;
}


std::string
FastOS_UNIX_File::getErrorString(const int osError)
{
    std::error_code ec(osError, std::system_category());
    return ec.message();
}


int64_t FastOS_UNIX_File::GetFreeDiskSpace (const char *path)
{
    struct statfs statBuf{};
    int statVal = statfs(path, &statBuf);
    if (statVal == 0) {
        return int64_t(statBuf.f_bavail) * int64_t(statBuf.f_bsize);
    }

    return -1;
}

int
FastOS_UNIX_File::count_open_files()
{
#ifdef __APPLE__
    int buffer_size = proc_pidinfo(getpid(), PROC_PIDLISTFDS, 0, nullptr, 0);
    return buffer_size / sizeof(proc_fdinfo);
#else
    return 0;
#endif
}

FastOS_UNIX_DirectoryScan::FastOS_UNIX_DirectoryScan(const char *searchPath)
    : FastOS_DirectoryScanInterface(searchPath),
      _statRun(false),
      _isDirectory(false),
      _isRegular(false),
      _statName(nullptr),
      _statFilenameP(nullptr),
      _dir(nullptr),
      _dp(nullptr)
{
    _dir = opendir(searchPath);

    const int minimumLength = 512 + 1;
    const int defaultLength = 16384;

    int maxNameLength = FastOS_File::GetMaximumFilenameLength(searchPath);
    int maxPathLength = FastOS_File::GetMaximumPathLength(searchPath);
    int nameLength = maxNameLength + 1 + maxPathLength;

    if ((maxNameLength == -1) ||
       (maxPathLength == -1) ||
       (nameLength < minimumLength))
    {
        nameLength = defaultLength;
    }

    _statName = new char [nameLength + 1];  // Include null

    strcpy(_statName, searchPath);
    strcat(_statName, "/");

    _statFilenameP = &_statName[strlen(_statName)];
}


FastOS_UNIX_DirectoryScan::~FastOS_UNIX_DirectoryScan()
{
    if (_dir != nullptr) {
        closedir(_dir);
        _dir = nullptr;
    }
    delete [] _statName;
}


bool
FastOS_UNIX_DirectoryScan::ReadNext()
{
    _statRun = false;

    if (_dir != nullptr) {
        _dp = readdir(_dir);
        return (_dp != nullptr);
    }

    return false;
}


void
FastOS_UNIX_DirectoryScan::DoStat()
{
    struct stat stbuf{};

    assert(_dp != nullptr);

    strcpy(_statFilenameP, _dp->d_name);

    if (lstat(_statName, &stbuf) == 0) {
        _isRegular = S_ISREG(stbuf.st_mode);
        _isDirectory = S_ISDIR(stbuf.st_mode);
    } else {
        printf("lstat failed for [%s]\n", _dp->d_name);
        _isRegular = false;
        _isDirectory = false;
    }

    _statRun = true;
}


bool
FastOS_UNIX_DirectoryScan::IsDirectory()
{
    if (!_statRun) {
        DoStat();
    }

    return _isDirectory;
}


bool
FastOS_UNIX_DirectoryScan::IsRegular()
{
    if (!_statRun) {
        DoStat();
    }

    return _isRegular;
}


const char *
FastOS_UNIX_DirectoryScan::GetName()
{
    assert(_dp != nullptr);

    return static_cast<const char *>(_dp->d_name);
}