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