src/File.cpp
author jbm@humility
Mon Sep 28 23:39:08 2009 -0700 (2009-09-28)
changeset 8 21a6c17f4e3e
parent 2 851de24ecb61
child 9 629f61203db1
permissions -rw-r--r--
jbm: it compiles... but bombs in unit tests
jens@0
     1
/*
jens@0
     2
 *  File.cpp
jens@0
     3
 *  Ottoman
jens@0
     4
 *
jens@0
     5
 *  Created by Jens Alfke on 8/20/09.
jens@0
     6
 *  Copyright 2009 Jens Alfke. All rights reserved.
jens@0
     7
 *  BSD-Licensed: See the file "LICENSE.txt" for details.
jens@0
     8
 */
jens@0
     9
jens@0
    10
#include "File.h"
jens@0
    11
#include "MemoryMap.h"
jens@0
    12
jbm@8
    13
#include <sys/file.h>
jens@0
    14
#include <assert.h>
jens@0
    15
#include <errno.h>
jens@0
    16
#include <fcntl.h>
jens@0
    17
#include <stdio.h>
jens@0
    18
#include <sys/stat.h>
jens@0
    19
#include <sys/uio.h>
jens@0
    20
#include <unistd.h>
jens@0
    21
jens@0
    22
namespace Mooseyard {
jens@0
    23
    
jens@0
    24
    File::File (const char *filename, bool writeable, bool create)  throw(Error)
jens@0
    25
        :_fd(_check( ::open(filename, writeable ?(create ?O_RDWR|O_CREAT :O_RDWR) :O_RDONLY, 0644) )),
jens@0
    26
         _memoryMap(NULL),
jens@0
    27
         _locked(0)
jens@0
    28
    { }
jens@0
    29
jbm@8
    30
    File::File (const char *filename, int oflag, bool locked)  throw(Error)
jbm@8
    31
#if BSD
jbm@8
    32
        :_fd(_check( ::open(filename, oflag | (locked ? O_EXLOCK : 0), 0644) )),
jbm@8
    33
#else
jens@0
    34
        :_fd(_check( ::open(filename, oflag, 0644) )),
jbm@8
    35
#endif
jens@0
    36
         _memoryMap(NULL),
jbm@8
    37
         _locked( locked ? 1 : 0)
jbm@8
    38
    {
jbm@8
    39
#if !BSDISH
jbm@8
    40
      _check( ::flock(_fd, LOCK_EX) );
jbm@8
    41
#endif
jbm@8
    42
    }
jens@0
    43
jens@0
    44
    File::~File() {
jens@0
    45
        delete _memoryMap;
jens@0
    46
        _unlock();
jens@0
    47
        ::close(_fd);
jens@0
    48
    }
jens@0
    49
    
jens@0
    50
    
jens@0
    51
    off_t File::length() const  throw(Error) {
jens@0
    52
        struct stat s;
jens@0
    53
        _check( ::fstat(_fd,&s) );
jens@0
    54
        return s.st_size;
jens@0
    55
    }
jens@0
    56
     
jens@0
    57
    void File::setLength (off_t length)  throw(Error) {
jens@0
    58
        _check( ftruncate(_fd,length) );
jens@0
    59
    }
jens@0
    60
jens@0
    61
    off_t File::position() const  throw(Error) {
jens@0
    62
        return _check( lseek(_fd,0,SEEK_CUR) );
jens@0
    63
    }
jens@0
    64
    
jens@0
    65
    void File::setPosition (off_t pos)  throw(Error) {
jens@0
    66
        _check( lseek(_fd,pos,SEEK_SET) );
jens@0
    67
    }
jens@0
    68
    
jens@0
    69
    off_t File::setPositionToEnd (off_t bytesBefore)  throw(Error) {
jens@0
    70
        return _check( lseek(_fd,-bytesBefore,SEEK_END) );
jens@0
    71
    }
jens@0
    72
    
jens@0
    73
    void File::read (void *dst, size_t size)  throw(Error) {
jens@0
    74
        _checkRead( ::read(_fd, dst, size), size );
jens@0
    75
    }
jens@0
    76
    
jens@0
    77
    size_t File::write (const void *src, size_t size)  throw(Error) {
jens@0
    78
        return _check( ::write(_fd, src, size) );
jens@0
    79
    }
jens@0
    80
    
jens@0
    81
    
jens@0
    82
    void File::readFrom (off_t where, void *dst, size_t size)  throw(Error) {
jens@0
    83
        _checkRead( ::pread(_fd, dst, size, where), size );
jens@0
    84
    }
jens@0
    85
    
jens@0
    86
    void File::writeTo (off_t where, const void *src, size_t size)  throw(Error) {
jens@0
    87
        _check( ::pwrite(_fd, src, size, where) );
jens@0
    88
    }
jens@0
    89
    
jens@0
    90
    size_t File::writeMultiple (Blob blobs[], int count) throw(Error) {
jens@0
    91
        struct iovec vec[count];
jens@0
    92
        for (int i=0; i<count; i++) {
jens@0
    93
            vec[i].iov_base = (void*) blobs[i].bytes;
jens@0
    94
            vec[i].iov_len = blobs[i].length;
jens@0
    95
        }
jens@0
    96
        return _check( ::writev(_fd, vec, count) );
jens@0
    97
    }
jens@0
    98
    
jens@0
    99
    size_t File::writePadding (int alignment) {
jens@2
   100
        int pad = alignment - (int)(position() & (alignment-1));
jens@0
   101
        if (pad == alignment)
jens@0
   102
            return 0;
jens@0
   103
        uint8_t zero[pad];
jens@0
   104
        memset(zero, 0, pad);
jens@0
   105
        return write(zero, pad);
jens@0
   106
    }
jens@0
   107
    
jens@0
   108
    
jens@0
   109
    void File::flush()  throw(Error) {
jens@0
   110
        _check( ::fsync(_fd) );
jens@0
   111
    }
jens@0
   112
    
jens@0
   113
    void File::flushDisk()  throw(Error) {
jbm@8
   114
#if OSX
jens@0
   115
        _check( ::fcntl(_fd,F_FULLFSYNC) );
jbm@8
   116
#else
jbm@8
   117
        _check( fsync(_fd) );
jbm@8
   118
#endif
jens@0
   119
    }
jens@0
   120
    
jens@0
   121
    bool File::hasPath (const char *path) const  throw(Error) {
jens@0
   122
        struct stat myStat, pathStat;
jens@0
   123
        _check( ::fstat(_fd, &myStat) );
jens@0
   124
        if ( ::stat(path, &pathStat) != 0 ) {
jens@0
   125
            if (errno == ENOENT)
jens@0
   126
                return false;
jens@0
   127
            _check(errno);
jens@0
   128
        }
jens@0
   129
        // Compare my inode number with that of the file at path:
jens@0
   130
        return myStat.st_ino == pathStat.st_ino;
jens@0
   131
    }
jens@0
   132
    
jens@0
   133
    
jens@0
   134
    void File::unlink (const char *filename)  throw(Error) {
jens@0
   135
        _check( ::unlink(filename) );
jens@0
   136
    }
jens@0
   137
    
jens@0
   138
    void File::rename (const char *srcFilename, const char *dstFilename)  throw(Error) {
jens@0
   139
        _check( ::rename(srcFilename, dstFilename) );
jens@0
   140
    }
jens@0
   141
jens@0
   142
    
jens@0
   143
#pragma mark -
jens@0
   144
#pragma mark UTILITIES:
jens@0
   145
    
jens@0
   146
    int File::_check (int result)  throw(Error) {
jens@0
   147
        if (result >= 0)
jens@0
   148
            return result;
jens@0
   149
        //printf("*** File::_check: Error %i: %s\n", errno, strerror(errno));
jens@0
   150
        throw Error(errno, strerror(errno));
jens@0
   151
    }
jens@0
   152
    
jens@0
   153
    void File::_checkRead (ssize_t result, size_t expectedSize)  throw(Error) {
jens@0
   154
        if ((size_t)result < expectedSize) {
jens@0
   155
            if (result < 0)
jens@0
   156
                throw Error(errno, strerror(errno));
jens@0
   157
            else
jens@0
   158
                throw Error(kEOF, "unexpected EOF");
jens@0
   159
        }
jens@0
   160
    }
jens@0
   161
    
jens@0
   162
    File::Error::Error(const char *m)
jens@0
   163
        :code(ERANGE), 
jens@0
   164
         message(m) 
jens@0
   165
    { }
jens@0
   166
    
jens@0
   167
    
jens@0
   168
#pragma mark -
jens@0
   169
#pragma mark LOCKS:
jens@0
   170
    
jens@0
   171
    bool File::_lock (bool block) {
jens@0
   172
        if (_locked) {
jens@0
   173
            _locked++;
jens@0
   174
        } else {
jens@0
   175
            int mode = LOCK_EX;
jens@0
   176
            if (block)
jens@0
   177
                mode |= LOCK_NB;
jens@0
   178
            if (::flock(_fd, mode) == 0)
jens@0
   179
                _locked = 1;
jens@0
   180
            else if (errno != EWOULDBLOCK)          // may be returned in LOCK_UN mode
jens@0
   181
                _check(-1);
jens@0
   182
        }
jens@0
   183
        return _locked > 0;
jens@0
   184
    }
jens@0
   185
    
jens@0
   186
    void File::_unlock() {
jens@0
   187
        if (_locked > 0) {
jens@0
   188
            _locked--;
jens@0
   189
            ::flock(_fd, LOCK_UN);
jens@0
   190
        }
jens@0
   191
    }
jens@0
   192
    
jens@0
   193
    File::Lock::Lock (File *file, bool block)  throw(Error)
jens@0
   194
        :_file(file),
jens@0
   195
         _locked( _file->_lock(block) )
jens@0
   196
    { }
jens@0
   197
    
jens@0
   198
    File::Lock::~Lock() {
jens@0
   199
        if (_locked)
jens@0
   200
            _file->_unlock();
jens@0
   201
    }
jens@0
   202
    
jens@0
   203
    bool File::Lock::retry() {
jens@0
   204
        if (!_locked)
jens@0
   205
            _locked = _file->_lock(false);
jens@0
   206
        return _locked;
jens@0
   207
    }
jens@0
   208
    
jens@0
   209
    
jens@0
   210
#pragma mark -
jens@0
   211
#pragma mark MEMORY MAP:
jens@0
   212
    
jens@0
   213
    MemoryMap* File::map() {
jens@0
   214
        if (!_memoryMap)
jens@0
   215
            _memoryMap = new MemoryMap(this);
jens@0
   216
        return _memoryMap;
jens@0
   217
    }
jens@0
   218
    
jens@0
   219
    void File::mapRegion (off_t position, size_t length) {
jens@0
   220
        return map()->mapRegion(position,length);
jens@0
   221
    }
jens@0
   222
    
jens@0
   223
    const void* File::mappedPosition (off_t position) const {
jens@0
   224
        return map()->mappedPosition(position);
jens@0
   225
    }
jens@0
   226
jens@0
   227
}