replaced the Path class with fs::path

pull/40/head
Matchman Green 2012-05-17 01:20:02 +08:00
parent 57fb6d3b0e
commit 3a33db68b6
16 changed files with 62 additions and 349 deletions

View File

@ -20,7 +20,6 @@
#include "Collection.hh"
#include "CommonUri.hh"
#include "util/Path.hh"
#include "util/OS.hh"
#include "xml/Node.hh"
#include "xml/NodeSet.hh"
@ -100,10 +99,10 @@ void Collection::Swap( Collection& coll )
m_leaf.swap( coll.m_leaf ) ;
}
void Collection::CreateSubDir( const Path& prefix )
void Collection::CreateSubDir( const fs::path& prefix )
{
Path dir = prefix / m_entry.Title() ;
os::MakeDir( dir ) ;
fs::path dir = prefix / m_entry.Title() ;
fs::create_directories( dir ) ;
for ( std::vector<Collection*>::iterator i = m_child.begin() ; i != m_child.end() ; ++i )
{
@ -118,10 +117,10 @@ void Collection::ForEachFile(
{
}
Path Collection::Dir() const
fs::path Collection::Dir() const
{
assert( m_parent != this ) ;
return m_parent != 0 ? (m_parent->Dir() / m_entry.Title()) : Path() ;
return m_parent != 0 ? (m_parent->Dir() / m_entry.Title()) : fs::current_path() ;
}
bool Collection::IsInRootTree() const

View File

@ -22,13 +22,13 @@
#include "Entry.hh"
#include "util/Exception.hh"
#include "util/Function.hh"
#include "util/FileSystem.hh"
#include <string>
#include <vector>
namespace gr {
class Path ;
class File ;
class Collection
@ -45,7 +45,7 @@ public :
const Collection* Parent() const ;
Collection* Parent() ;
std::string ParentHref() const ;
Path Dir() const ;
fs::path Dir() const ;
bool IsInRootTree() const ;
void AddChild( Collection *child ) ;
@ -54,7 +54,7 @@ public :
void Swap( Collection& coll ) ;
// traversing the tree
void CreateSubDir( const Path& prefix ) ;
void CreateSubDir( const fs::path& prefix ) ;
void ForEachFile(
Function<void(const std::string&)> callback,
const std::string& prefix = "." ) ;

View File

@ -30,7 +30,7 @@
#include "protocol/OAuth2.hh"
#include "util/Destroy.hh"
#include "util/Log.hh"
#include "util/Path.hh"
//#include "util/Path.hh"
#include "xml/Node.hh"
#include "xml/NodeSet.hh"
@ -69,7 +69,7 @@ Drive::Drive( OAuth2& auth ) :
Trace( "change stamp is %1%", change_stamp ) ;
m_state.ChangeStamp( change_stamp ) ;
m_state.Sync( "." ) ;
m_state.Sync( fs::current_path() ) ;
ConstructDirTree( &http ) ;
@ -217,7 +217,7 @@ void Drive::ConstructDirTree( http::Agent *http )
// lastly, iterating from the root, create the directories in the local file system
assert( Root()->Parent() == 0 ) ;
Root()->CreateSubDir( Path() ) ;
Root()->CreateSubDir( fs::current_path() ) ;
}
void Drive::UpdateFile( Entry& entry, Collection& parent, http::Agent *http )
@ -229,6 +229,10 @@ void Drive::UpdateFile( Entry& entry, Collection& parent, http::Agent *http )
m_files.push_back( file ) ;
parent.AddLeaf( file ) ;
Trace( "%1% ID = %2%", file->Path(), file->ResourceID() ) ;
m_state.SetId( file->Path(), file->ResourceID() ) ;
// file->Update( http, m_http_hdr ) ;
}
else

View File

@ -21,7 +21,6 @@
#include "util/Log.hh"
#include "util/OS.hh"
#include "util/Path.hh"
#include "xml/Node.hh"
#include "xml/NodeSet.hh"

View File

@ -64,7 +64,6 @@ public :
void Swap( Entry& e ) ;
private :
void Update( const xml::Node& entry ) ;
private :

View File

@ -28,7 +28,6 @@
#include "protocol/OAuth2.hh"
#include "util/Crypt.hh"
#include "util/DateTime.hh"
#include "util/Path.hh"
#include "util/OS.hh"
#include "util/Log.hh"
#include "xml/Node.hh"
@ -49,10 +48,10 @@ void File::Update( http::Agent *http, const http::Headers& auth )
assert( m_parent != 0 ) ;
bool changed = true ;
Path path = m_parent->Dir() / m_entry.Filename() ;
fs::path path = m_parent->Dir() / m_entry.Filename() ;
// compare checksum first if file exists
std::ifstream ifile( path.Str().c_str(), std::ios::binary | std::ios::in ) ;
std::ifstream ifile( path.string().c_str(), std::ios::binary | std::ios::in ) ;
if ( ifile && m_entry.ServerMD5() == crypt::MD5(ifile.rdbuf()) )
changed = false ;
@ -85,10 +84,10 @@ void File::Delete( http::Agent *http, const http::Headers& auth )
}
void File::Download( http::Agent* http, const Path& file, const http::Headers& auth ) const
void File::Download( http::Agent* http, const fs::path& file, const http::Headers& auth ) const
{
Log( "Downloading %1%", file ) ;
http::Download dl( file.Str(), http::Download::NoChecksum() ) ;
http::Download dl( file.string(), http::Download::NoChecksum() ) ;
long r = http->Get( m_entry.ContentSrc(), &dl, auth ) ;
if ( r <= 400 )
os::SetFileTime( file, m_entry.ServerModified() ) ;
@ -141,8 +140,21 @@ bool File::Upload( http::Agent* http, std::streambuf *file, const http::Headers&
http->Put( uplink, data, &xml, uphdr ) ;
Trace( "Receipted response = %1%", xml.Response() ) ;
m_entry.Update( xml.Response() ) ;
return true ;
}
fs::path File::Path() const
{
assert( m_parent != 0 ) ;
return m_parent->Dir() / m_entry.Filename() ;
}
std::string File::ResourceID() const
{
return m_entry.ResourceID() ;
}
} // end of namespace

View File

@ -21,6 +21,7 @@
#include "Entry.hh"
#include "http/Agent.hh"
#include "util/FileSystem.hh"
#include <iosfwd>
@ -40,10 +41,13 @@ public :
void Update( http::Agent *http, const http::Headers& auth ) ;
void Download( http::Agent* http, const Path& file, const http::Headers& auth ) const ;
void Download( http::Agent* http, const fs::path& file, const http::Headers& auth ) const ;
bool Upload( http::Agent* http, std::streambuf *file, const http::Headers& auth ) ;
void Delete( http::Agent* http, const http::Headers& auth ) ;
fs::path Path() const ;
std::string ResourceID() const ;
private :
Entry m_entry ;
const Collection *m_parent ;

View File

@ -131,6 +131,7 @@ void State::ChangeStamp( const std::string& cs )
void State::Sync( const fs::path& p )
{
Trace( "synchronizing = %1%", p ) ;
for ( fs::directory_iterator i( p ) ; i != fs::directory_iterator() ; ++i )
{
Trace( "file found = %1%", i->path() ) ;
@ -161,11 +162,21 @@ void State::Write( const fs::path& filename ) const
fs << result ;
}
Json State::FileInfo( const boost::filesystem::path& p )
void State::SetId( const fs::path& p, const std::string& id )
{
Json info ;
info.Add( "mtime", Json( fs::last_write_time(p) ) ) ;
return info ;
PathIdx& pidx = m_impl->rs.get<ByPath>() ;
PathIdx::iterator it = pidx.find( p ) ;
if ( it != pidx.end() )
{
Resource r = *it ;
r.id = id ;
pidx.replace( it, r ) ;
}
else
{
Trace( "can't find %1%", p ) ;
}
}
} // end of namespace

View File

@ -34,15 +34,14 @@ public :
void Sync( const fs::path& p ) ;
void SetId( const fs::path& p, const std::string& id ) ;
void Read( const fs::path& filename ) ;
void Write( const fs::path& filename ) const ;
std::string ChangeStamp() const ;
void ChangeStamp( const std::string& cs ) ;
private :
static Json FileInfo( const fs::path& p ) ;
private :
struct Impl ;
std::auto_ptr<Impl> m_impl ;

View File

@ -21,7 +21,6 @@
#include "DateTime.hh"
#include "Exception.hh"
#include "Path.hh"
// boost headers
#include <boost/throw_exception.hpp>
@ -40,19 +39,9 @@
namespace gr { namespace os {
void MakeDir( const Path& dir )
DateTime FileMTime( const fs::path& filename )
{
MakeDir( dir.Str() ) ;
}
void MakeDir( const std::string& dir )
{
mkdir( dir.c_str(), 0700 ) ;
}
DateTime FileMTime( const Path& filename )
{
return FileMTime( filename.Str() ) ;
return FileMTime( filename.string() ) ;
}
DateTime FileMTime( const std::string& filename )
@ -75,9 +64,9 @@ DateTime FileMTime( const std::string& filename )
#endif
}
void SetFileTime( const Path& filename, const DateTime& t )
void SetFileTime( const fs::path& filename, const DateTime& t )
{
return SetFileTime( filename.Str(), t ) ;
return SetFileTime( filename.string(), t ) ;
}
void SetFileTime( const std::string& filename, const DateTime& t )

View File

@ -20,6 +20,7 @@
#pragma once
#include "Exception.hh"
#include "FileSystem.hh"
#include <string>
@ -31,15 +32,12 @@ class Path ;
namespace os
{
struct Error : virtual Exception {} ;
void MakeDir( const std::string& dir ) ;
void MakeDir( const Path& dir ) ;
DateTime FileMTime( const std::string& filename ) ;
DateTime FileMTime( const Path& filename ) ;
DateTime FileMTime( const fs::path& filename ) ;
void SetFileTime( const std::string& filename, const DateTime& t ) ;
void SetFileTime( const Path& filename, const DateTime& t ) ;
void SetFileTime( const fs::path& filename, const DateTime& t ) ;
}
} // end of namespaces

View File

@ -1,135 +0,0 @@
/*
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 "Path.hh"
#include <algorithm>
#include <cassert>
#include <iterator>
#include <sstream>
#include <iostream>
namespace gr {
Path::Path( const std::string& p ) :
m_paths( Separate(p) )
{
}
Path::Path( const Path& path ) :
m_paths( path.m_paths )
{
}
std::string Path::Str() const
{
// this is not the fastest way to implement it, but the easiest
std::ostringstream ss ;
ss << *this ;
return ss.str() ;
}
Path::DirVec Path::Separate( std::string str )
{
std::vector<std::string> result ;
std::size_t n = str.find( '/' ) ;
while ( n != str.npos )
{
std::string dir = str.substr( 0, n ) ;
if ( !dir.empty() )
result.push_back( dir ) ;
str.erase( 0, n+1 ) ;
n = str.find( '/' ) ;
}
result.push_back( str ) ;
return result ;
}
std::ostream& operator<<( std::ostream& os, const Path& path )
{
assert( !path.m_paths.empty() ) ;
if ( path.m_paths.front() != "." )
os << '/' ;
std::copy( path.Begin(), path.End()-1, std::ostream_iterator<std::string>( os, "/" ) ) ;
return os << path.m_paths.back() ;
}
Path& Path::operator/=( const Path& path )
{
std::copy( path.Begin(), path.End(), std::back_inserter( m_paths ) ) ;
return *this ;
}
Path& Path::operator/=( const std::string& path )
{
m_paths.push_back( path ) ;
return *this ;
}
Path Path::operator/( const Path& path ) const
{
Path tmp(*this) ;
tmp /= path ;
return tmp ;
}
Path Path::operator/( const std::string& str ) const
{
Path tmp(*this) ;
tmp /= str ;
return tmp ;
}
bool operator==( const Path& p1, const Path& p2 )
{
return p1.m_paths == p2.m_paths ;
}
bool operator!=( const Path& p1, const Path& p2 )
{
return !( p1 == p2 ) ;
}
Path::iterator Path::Begin()
{
return m_paths.begin() ;
}
Path::iterator Path::End()
{
return m_paths.end() ;
}
Path::const_iterator Path::Begin() const
{
return m_paths.begin() ;
}
Path::const_iterator Path::End() const
{
return m_paths.end() ;
}
} // end of namespace

View File

@ -1,67 +0,0 @@
/*
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 <string>
#include <vector>
namespace gr {
class Path
{
private :
typedef std::vector<std::string> DirVec ;
public :
typedef DirVec::iterator iterator ;
typedef DirVec::const_iterator const_iterator ;
public :
// it will less troublesome to use if we can convert them automatically
Path( const std::string& p = "." ) ;
Path( const Path& path ) ;
std::string Str() const ;
Path operator/( const Path& path ) const ;
Path operator/( const std::string& str ) const ;
Path& operator/=( const Path& path ) ;
Path& operator/=( const std::string& str ) ;
iterator Begin() ;
iterator End() ;
const_iterator Begin() const ;
const_iterator End() const ;
friend std::ostream& operator<<( std::ostream& os, const Path& path ) ;
// support std::string for both arguments also
friend bool operator==( const Path& p1, const Path& p2 ) ;
private :
static DirVec Separate( std::string str ) ;
private :
DirVec m_paths ;
} ;
// this doesn't need to be friend. just call op== and ! it
bool operator!=( const Path& p1, const Path& p2 ) ;
} // end of namespace

View File

@ -26,7 +26,6 @@
#include "util/DateTimeTest.hh"
#include "util/FunctionTest.hh"
#include "util/GdbmTest.hh"
#include "util/PathTest.hh"
#include "util/SignalHandlerTest.hh"
#include "xml/NodeTest.hh"
@ -43,7 +42,6 @@ int main( int argc, char **argv )
runner.addTest( StateTest::suite( ) ) ;
runner.addTest( DateTimeTest::suite( ) ) ;
runner.addTest( FunctionTest::suite( ) ) ;
runner.addTest( PathTest::suite( ) ) ;
runner.addTest( SignalHandlerTest::suite( ) ) ;
runner.addTest( GdbmTest::suite( ) ) ;
runner.addTest( NodeTest::suite( ) ) ;

View File

@ -1,54 +0,0 @@
/*
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 "PathTest.hh"
#include "util/Path.hh"
namespace grut {
using namespace gr ;
PathTest::PathTest( )
{
}
void PathTest::TestStr( )
{
Path relative = Path() / "dir1" / "dir2" ;
CPPUNIT_ASSERT_EQUAL( std::string( "./dir1/dir2" ), relative.Str() ) ;
Path absolute = Path( "usr" ) / "X11R6" / "lib" ;
CPPUNIT_ASSERT_EQUAL( std::string( "/usr/X11R6/lib" ), absolute.Str() ) ;
}
void PathTest::TestSeparate( )
{
Path relative( "./matchman/green///grive/dir" ) ;
Path relative2 = Path() / "matchman" / "green" / "grive" / "dir" ;
CPPUNIT_ASSERT_EQUAL( relative2, relative ) ;
Path absolute( "////usr//local/grive/////dir" ) ;
Path absolute2 = Path("usr") / "local" / "grive" / "dir" ;
CPPUNIT_ASSERT_EQUAL( absolute2, absolute ) ;
}
} // end of namespace grut

View File

@ -1,43 +0,0 @@
/*
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 <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
namespace grut {
class PathTest : public CppUnit::TestFixture
{
public :
PathTest( ) ;
// declare suit function
CPPUNIT_TEST_SUITE( PathTest ) ;
CPPUNIT_TEST( TestStr ) ;
CPPUNIT_TEST( TestSeparate ) ;
CPPUNIT_TEST_SUITE_END();
private :
void TestStr( ) ;
void TestSeparate( ) ;
} ;
} // end of namespace