Permalink
Browse files

added class & functions for memory mapped files

  • Loading branch information...
1 parent 94383e0 commit 75c67dad8d9861f5c3f64a9260af4f1692306a7a @nestal nestal committed Jul 16, 2012
@@ -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" ) ;
@@ -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
@@ -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
@@ -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 ) ;
+}
+
+off_t StdioFile::Tell() const
{
assert( IsOpened() ) ;
- return ::lseek( m_fd, offset, whence ) ;
+ return LSeek( m_fd, 0, SEEK_CUR ) ;
}
-long StdioFile::Tell() const
+u64_t StdioFile::Size() const
{
assert( IsOpened() ) ;
- return ::lseek( m_fd, 0, SEEK_CUR ) ;
+
+ 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
@@ -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
@@ -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 ;
+}

0 comments on commit 75c67da

Please sign in to comment.