added class & functions for memory mapped files

pull/40/head
Nestal Wan 2012-07-17 01:08:34 +08:00
parent 94383e0030
commit 75c67dad8d
6 changed files with 214 additions and 8 deletions

View File

@ -589,9 +589,11 @@ bool Resource::Upload(
std::size_t count = 0 ;
while ( (count = file.Read( buf, sizeof(buf) )) > 0 )
data.append( buf, count ) ;
assert( file.Size() == data.size() ) ;
std::ostringstream xcontent_len ;
xcontent_len << "X-Upload-Content-Length: " << data.size() ;
xcontent_len << "X-Upload-Content-Length: " << file.Size() ;
http::Header hdr( auth ) ;
hdr.Add( "Content-Type: application/atom+xml" ) ;

View File

@ -0,0 +1,46 @@
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2012 Wan Wai Ho
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 version 2
of the License.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "MemMap.hh"
#include "StdioFile.hh"
namespace gr {
MemMap::MemMap( StdioFile& file, off_t offset, std::size_t length ) :
m_addr ( file.Map( offset, length ) ),
m_length( length )
{
}
MemMap::~MemMap()
{
StdioFile::UnMap( m_addr, m_length ) ;
}
void* MemMap::Addr() const
{
return m_addr ;
}
std::size_t MemMap::Length() const
{
return m_length ;
}
} // end of namespace

View File

@ -0,0 +1,47 @@
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2012 Wan Wai Ho
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 version 2
of the License.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#pragma once
#include "Exception.hh"
#include "Types.hh"
#include <cstddef>
namespace gr {
class StdioFile ;
class MemMap
{
public :
struct Error : virtual Exception {} ;
public :
MemMap( StdioFile& file, off_t offset, std::size_t length ) ;
~MemMap() ;
void* Addr() const ;
std::size_t Length() const ;
private :
void *m_addr ;
std::size_t m_length ;
} ;
} // end of namespace

View File

@ -30,10 +30,47 @@
#include <boost/exception/errinfo_file_open_mode.hpp>
#include <boost/exception/info.hpp>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
// local functions
namespace {
off_t LSeek( int fd, off_t offset, int whence )
{
assert( fd >= 0 ) ;
off_t r = ::lseek( fd, offset, whence ) ;
if ( r == static_cast<off_t>(-1) )
{
BOOST_THROW_EXCEPTION(
gr::StdioFile::Error()
<< boost::errinfo_api_function("lseek")
<< boost::errinfo_errno(errno)
) ;
}
return r ;
}
struct stat FStat( int fd )
{
struct stat s = {} ;
if ( ::fstat( fd, &s ) != 0 )
{
BOOST_THROW_EXCEPTION(
gr::StdioFile::Error()
<< boost::errinfo_api_function("fstat")
<< boost::errinfo_errno(errno)
) ;
}
return s ;
}
} // end of local functions
namespace gr {
StdioFile::StdioFile( ) : m_fd( -1 )
@ -127,16 +164,26 @@ std::size_t StdioFile::Write( const void *ptr, std::size_t size )
return count ;
}
long StdioFile::Seek( long offset, int whence )
off_t StdioFile::Seek( off_t offset, int whence )
{
assert( IsOpened() ) ;
return ::lseek( m_fd, offset, whence ) ;
return LSeek( m_fd, offset, whence ) ;
}
long StdioFile::Tell() const
off_t StdioFile::Tell() const
{
assert( IsOpened() ) ;
return ::lseek( m_fd, 0, SEEK_CUR ) ;
return LSeek( m_fd, 0, SEEK_CUR ) ;
}
u64_t StdioFile::Size() const
{
assert( IsOpened() ) ;
struct stat s = FStat(m_fd) ;
assert( s.st_size >= 0 ) ;
return static_cast<uint64_t>( s.st_size ) ;
}
void StdioFile::Chmod( int mode )
@ -153,4 +200,32 @@ void StdioFile::Chmod( int mode )
}
}
void* StdioFile::Map( off_t offset, std::size_t length )
{
assert( IsOpened() ) ;
void *addr = ::mmap( 0, length, PROT_READ, MAP_PRIVATE, m_fd, offset ) ;
if ( addr == reinterpret_cast<void*>( -1 ) )
{
BOOST_THROW_EXCEPTION(
Error()
<< boost::errinfo_api_function("mmap")
<< boost::errinfo_errno(errno)
) ;
}
return addr ;
}
void StdioFile::UnMap( void *addr, std::size_t length )
{
if ( ::munmap( addr, length ) != 0 )
{
BOOST_THROW_EXCEPTION(
Error()
<< boost::errinfo_api_function("munmap")
<< boost::errinfo_errno(errno)
) ;
}
}
} // end of namespace

View File

@ -21,6 +21,7 @@
#include "Exception.hh"
#include "FileSystem.hh"
#include "Types.hh"
#include <string>
@ -45,11 +46,15 @@ public :
std::size_t Read( void *ptr, std::size_t size ) ;
std::size_t Write( const void *ptr, std::size_t size ) ;
long Seek( long offset, int whence ) ;
long Tell() const ;
off_t Seek( off_t offset, int whence ) ;
off_t Tell() const ;
u64_t Size() const ;
void Chmod( int mode ) ;
void* Map( off_t offset, std::size_t length ) ;
static void UnMap( void *addr, std::size_t length ) ;
private :
void Open( const fs::path& path, int flags, int mode ) ;

View File

@ -0,0 +1,31 @@
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2012 Wan Wai Ho
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 version 2
of the License.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#pragma once
#include <sys/types.h>
// import types into the Grive namespace
namespace gr
{
using ::off_t ;
// should use boost/cstdint
typedef unsigned long long u64_t ;
}