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
|
/*
** PJL C++ Library
** file_vector.c
**
** Copyright (C) 1998 Paul J. Lucas
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
// standard
#include <fcntl.h> /* for open(2), O_RDONLY, ... */
#include <sys/mman.h> /* for mmap(2) */
#include <sys/stat.h> /* for stat(2) */
#include <unistd.h> /* for close(2) */
// local
#include "file_vector.h"
#ifndef PJL_NO_NAMESPACES
using namespace std;
#endif
//*****************************************************************************
//
// SYNOPSIS
//
void file_vector_base::close()
//
// DESCRIPTION
//
// Munmap and closes a file previously opened and mmapped by
// file_vector_base::open().
//
// SEE ALSO
//
// close(2), file_vector_base::open(3), munmap(2)
//
//*****************************************************************************
{
if ( addr_ )
::munmap( STATIC_CAST( char* )( addr_ ), size_ );
if ( fd_ )
::close( fd_ );
init();
}
//*****************************************************************************
//
// SYNOPSIS
//
void file_vector_base::init()
//
// DESCRIPTION
//
// Initialize an instance of file_vector_base.
//
//*****************************************************************************
{
size_ = 0;
fd_ = 0;
addr_ = 0;
error_ = 0;
}
//*****************************************************************************
//
// SYNOPSIS
//
bool file_vector_base::open( char const *path, ios::open_mode mode )
//
// DESCRIPTION
//
// Open and mmap a file.
//
// RETURN VALUE
//
// Returns true only if the file was mmapped successfully.
//
// SEE ALSO
//
// mmap(2), open(2), stat(2)
//
//*****************************************************************************
{
close();
struct stat stat_buf;
if ( ::stat( path, &stat_buf ) == -1 ) {
error_ = 1;
return false;
}
int flags = 0;
if ( mode & ios::in ) flags |= O_RDONLY;
if ( mode & ios::out ) flags |= O_WRONLY;
if ( ( fd_ = ::open( path, flags ) ) == -1 ) {
fd_ = 0;
error_ = 2;
return false;
}
int prot = PROT_NONE;
if ( mode & ios::in ) prot |= PROT_READ;
if ( mode & ios::out ) prot |= PROT_WRITE;
addr_ = ::mmap( 0, stat_buf.st_size, prot, MAP_SHARED, fd_, 0 );
if ( addr_ == REINTERPRET_CAST( caddr_t )( -1 ) ) {
addr_ = 0;
error_ = 3;
return false;
}
size_ = stat_buf.st_size;
return true;
}
|