2012-04-26 19:40:38 +04:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2012-05-19 11:41:21 +04:00
|
|
|
#include "Resource.hh"
|
2012-05-12 21:57:15 +04:00
|
|
|
#include "CommonUri.hh"
|
2012-06-10 12:18:21 +04:00
|
|
|
#include "Entry.hh"
|
2012-04-26 19:40:38 +04:00
|
|
|
|
2012-06-03 12:42:51 +04:00
|
|
|
#include "http/Agent.hh"
|
2012-05-19 12:18:33 +04:00
|
|
|
#include "http/Download.hh"
|
2012-06-10 19:11:32 +04:00
|
|
|
#include "http/Header.hh"
|
2012-05-29 21:30:06 +04:00
|
|
|
// #include "http/ResponseLog.hh"
|
2012-05-19 12:18:33 +04:00
|
|
|
#include "http/StringResponse.hh"
|
|
|
|
#include "http/XmlResponse.hh"
|
2012-05-20 13:12:01 +04:00
|
|
|
#include "protocol/Json.hh"
|
2012-05-20 19:16:59 +04:00
|
|
|
#include "util/CArray.hh"
|
2012-05-19 12:18:33 +04:00
|
|
|
#include "util/Crypt.hh"
|
2012-06-03 14:31:02 +04:00
|
|
|
#include "util/log/Log.hh"
|
2012-04-27 06:49:33 +04:00
|
|
|
#include "util/OS.hh"
|
2012-05-20 20:10:29 +04:00
|
|
|
#include "util/StdioFile.hh"
|
2012-05-12 13:38:41 +04:00
|
|
|
#include "xml/Node.hh"
|
|
|
|
#include "xml/NodeSet.hh"
|
2012-06-07 14:26:57 +04:00
|
|
|
#include "xml/String.hh"
|
2012-04-26 19:40:38 +04:00
|
|
|
|
2012-06-10 19:38:27 +04:00
|
|
|
#include <boost/bind.hpp>
|
2012-06-03 12:42:51 +04:00
|
|
|
#include <boost/exception/all.hpp>
|
|
|
|
|
2012-04-26 20:55:10 +04:00
|
|
|
#include <cassert>
|
|
|
|
|
2012-04-26 19:40:38 +04:00
|
|
|
// for debugging
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
namespace gr {
|
|
|
|
|
2012-05-27 20:24:37 +04:00
|
|
|
// hard coded XML file
|
|
|
|
const std::string xml_meta =
|
|
|
|
"<?xml version='1.0' encoding='UTF-8'?>\n"
|
|
|
|
"<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:docs=\"http://schemas.google.com/docs/2007\">"
|
|
|
|
"<category scheme=\"http://schemas.google.com/g/2005#kind\" "
|
|
|
|
"term=\"http://schemas.google.com/docs/2007#%1%\"/>"
|
|
|
|
"<title>%2%</title>"
|
|
|
|
"</entry>" ;
|
|
|
|
|
|
|
|
|
2012-05-20 10:57:25 +04:00
|
|
|
/// default constructor creates the root folder
|
|
|
|
Resource::Resource() :
|
2012-06-10 12:18:21 +04:00
|
|
|
m_name ( "." ),
|
|
|
|
m_kind ( "folder" ),
|
|
|
|
m_id ( "folder:root" ),
|
|
|
|
m_href ( root_href ),
|
|
|
|
m_create ( root_create ),
|
|
|
|
m_parent ( 0 ),
|
|
|
|
m_state ( sync )
|
2012-05-20 10:57:25 +04:00
|
|
|
{
|
|
|
|
}
|
2012-05-31 15:22:18 +04:00
|
|
|
|
2012-05-31 19:37:47 +04:00
|
|
|
Resource::Resource( const std::string& name, const std::string& kind ) :
|
2012-06-10 12:18:21 +04:00
|
|
|
m_name ( name ),
|
|
|
|
m_kind ( kind ),
|
|
|
|
m_parent ( 0 ),
|
|
|
|
m_state ( unknown )
|
2012-04-26 19:40:38 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-06-18 19:44:28 +04:00
|
|
|
void Resource::SetState( State new_state )
|
|
|
|
{
|
|
|
|
// only the new and delete states need to be set recursively
|
|
|
|
assert(
|
|
|
|
new_state == remote_new || new_state == remote_deleted ||
|
|
|
|
new_state == local_new || new_state == local_deleted
|
|
|
|
) ;
|
|
|
|
|
|
|
|
m_state = new_state ;
|
|
|
|
std::for_each( m_child.begin(), m_child.end(),
|
|
|
|
boost::bind( &Resource::SetState, _1, new_state ) ) ;
|
|
|
|
}
|
|
|
|
|
2012-05-31 19:48:30 +04:00
|
|
|
void Resource::FromRemoteFolder( const Entry& remote, const DateTime& last_sync )
|
2012-05-17 20:37:11 +04:00
|
|
|
{
|
2012-05-30 21:17:22 +04:00
|
|
|
fs::path path = Path() ;
|
|
|
|
|
2012-06-03 10:44:09 +04:00
|
|
|
if ( remote.CreateLink().empty() )
|
|
|
|
Log( "folder %1% is read-only", path, log::verbose ) ;
|
2012-06-18 19:44:28 +04:00
|
|
|
|
2012-05-31 19:48:30 +04:00
|
|
|
// already sync
|
|
|
|
if ( fs::is_directory( path ) )
|
2012-05-27 18:49:49 +04:00
|
|
|
{
|
2012-05-31 19:48:30 +04:00
|
|
|
Log( "folder %1% is in sync", path, log::verbose ) ;
|
|
|
|
m_state = sync ;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remote file created after last sync, so remote is newer
|
|
|
|
else if ( remote.MTime() > last_sync )
|
|
|
|
{
|
|
|
|
if ( fs::exists( path ) )
|
2012-05-30 21:17:22 +04:00
|
|
|
{
|
2012-05-31 19:48:30 +04:00
|
|
|
// TODO: handle type change
|
|
|
|
Log( "%1% changed from folder to file", path, log::verbose ) ;
|
2012-05-30 21:17:22 +04:00
|
|
|
m_state = sync ;
|
|
|
|
}
|
2012-05-31 19:48:30 +04:00
|
|
|
else
|
|
|
|
{
|
2012-06-18 19:44:28 +04:00
|
|
|
// make all children as remote_new, if any
|
2012-06-03 10:44:09 +04:00
|
|
|
Log( "folder %1% is created in remote", path, log::verbose ) ;
|
2012-06-18 19:44:28 +04:00
|
|
|
SetState( remote_new ) ;
|
2012-05-31 19:48:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( fs::exists( path ) )
|
2012-05-30 21:17:22 +04:00
|
|
|
{
|
2012-05-31 19:48:30 +04:00
|
|
|
// TODO: handle type chage
|
|
|
|
Log( "%1% changed from file to folder", path, log::verbose ) ;
|
|
|
|
m_state = sync ;
|
2012-05-30 21:17:22 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-31 19:48:30 +04:00
|
|
|
Log( "folder %1% is deleted in local", path, log::verbose ) ;
|
2012-06-18 19:44:28 +04:00
|
|
|
SetState( local_deleted ) ;
|
2012-05-30 21:17:22 +04:00
|
|
|
}
|
2012-05-27 18:49:49 +04:00
|
|
|
}
|
2012-05-31 19:48:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Update the state according to information (i.e. Entry) from remote. This function
|
|
|
|
/// compares the modification time and checksum of both copies and determine which
|
|
|
|
/// one is newer.
|
|
|
|
void Resource::FromRemote( const Entry& remote, const DateTime& last_sync )
|
|
|
|
{
|
|
|
|
// sync folder
|
|
|
|
if ( remote.Kind() == "folder" && IsFolder() )
|
|
|
|
FromRemoteFolder( remote, last_sync ) ;
|
2012-06-03 11:55:32 +04:00
|
|
|
else
|
|
|
|
FromRemoteFile( remote, last_sync ) ;
|
2012-05-27 18:49:49 +04:00
|
|
|
|
2012-06-10 12:18:21 +04:00
|
|
|
AssignIDs( remote ) ;
|
|
|
|
|
2012-06-03 11:55:32 +04:00
|
|
|
assert( m_state != unknown ) ;
|
2012-06-03 12:57:21 +04:00
|
|
|
|
|
|
|
if ( m_state == remote_new || m_state == remote_changed )
|
2012-06-10 12:18:21 +04:00
|
|
|
{
|
|
|
|
m_md5 = remote.MD5() ;
|
|
|
|
m_mtime = remote.MTime() ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::AssignIDs( const Entry& remote )
|
|
|
|
{
|
2012-06-10 17:03:32 +04:00
|
|
|
// the IDs from change feed entries are different
|
|
|
|
if ( !remote.IsChange() )
|
|
|
|
{
|
|
|
|
m_id = remote.ResourceID() ;
|
|
|
|
m_href = remote.SelfHref() ;
|
|
|
|
m_edit = remote.EditLink() ;
|
|
|
|
m_create = remote.CreateLink() ;
|
|
|
|
m_content = remote.ContentSrc() ;
|
|
|
|
m_etag = remote.ETag() ;
|
|
|
|
}
|
2012-06-03 11:55:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::FromRemoteFile( const Entry& remote, const DateTime& last_sync )
|
|
|
|
{
|
|
|
|
assert( m_parent != 0 ) ;
|
|
|
|
|
|
|
|
fs::path path = Path() ;
|
|
|
|
|
|
|
|
// recursively create/delete folder
|
|
|
|
if ( m_parent->m_state == remote_new || m_parent->m_state == remote_deleted ||
|
|
|
|
m_parent->m_state == local_new || m_parent->m_state == local_deleted )
|
|
|
|
{
|
|
|
|
Log( "file %1% parent %2% recursively in %3% (%4%)", path,
|
|
|
|
( m_parent->m_state == remote_new || m_parent->m_state == local_new ) ? "created" : "deleted",
|
2012-06-05 19:08:03 +04:00
|
|
|
( m_parent->m_state == remote_new || m_parent->m_state == remote_deleted ) ? "remote" : "local",
|
2012-06-18 19:44:28 +04:00
|
|
|
m_parent->m_state, log::verbose ) ;
|
2012-06-03 11:55:32 +04:00
|
|
|
|
|
|
|
m_state = m_parent->m_state ;
|
|
|
|
}
|
|
|
|
|
2012-05-30 21:17:22 +04:00
|
|
|
// local not exists
|
|
|
|
else if ( !fs::exists( path ) )
|
|
|
|
{
|
2012-06-03 19:58:28 +04:00
|
|
|
Trace( "file %1% change stamp = %2%", Path(), remote.ChangeStamp() ) ;
|
|
|
|
|
|
|
|
if ( remote.MTime() > last_sync || remote.ChangeStamp() > 0 )
|
2012-05-30 21:17:22 +04:00
|
|
|
{
|
2012-06-05 19:08:03 +04:00
|
|
|
Log( "file %1% is created in remote (change %2%)", path,
|
|
|
|
remote.ChangeStamp(), log::verbose ) ;
|
|
|
|
|
2012-05-30 21:17:22 +04:00
|
|
|
m_state = remote_new ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-31 20:40:07 +04:00
|
|
|
Log( "file %1% is deleted in local", path, log::verbose ) ;
|
2012-05-30 21:17:22 +04:00
|
|
|
m_state = local_deleted ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-31 15:22:18 +04:00
|
|
|
// if checksum is equal, no need to compare the mtime
|
2012-06-10 12:18:21 +04:00
|
|
|
else if ( remote.MD5() == m_md5 )
|
2012-05-31 15:22:18 +04:00
|
|
|
{
|
|
|
|
Log( "file %1% is already in sync", Path(), log::verbose ) ;
|
|
|
|
m_state = sync ;
|
|
|
|
}
|
|
|
|
|
2012-05-20 10:57:25 +04:00
|
|
|
// use mtime to check which one is more recent
|
|
|
|
else
|
|
|
|
{
|
2012-06-10 17:03:32 +04:00
|
|
|
assert( m_state != unknown ) ;
|
2012-05-29 21:30:06 +04:00
|
|
|
|
|
|
|
// if remote is modified
|
2012-06-10 12:18:21 +04:00
|
|
|
if ( remote.MTime() > m_mtime )
|
2012-05-31 15:22:18 +04:00
|
|
|
{
|
|
|
|
Log( "file %1% is changed in remote", path, log::verbose ) ;
|
2012-05-29 21:30:06 +04:00
|
|
|
m_state = remote_changed ;
|
2012-05-31 15:22:18 +04:00
|
|
|
}
|
2012-05-29 21:30:06 +04:00
|
|
|
|
|
|
|
// remote also has the file, so it's not new in local
|
2012-05-30 21:17:22 +04:00
|
|
|
else if ( m_state == local_new || m_state == remote_deleted )
|
2012-05-31 15:22:18 +04:00
|
|
|
{
|
|
|
|
Log( "file %1% is changed in local", path, log::verbose ) ;
|
2012-05-29 21:30:06 +04:00
|
|
|
m_state = local_changed ;
|
2012-05-31 15:22:18 +04:00
|
|
|
}
|
2012-05-31 19:48:30 +04:00
|
|
|
else
|
2012-06-10 12:18:21 +04:00
|
|
|
Trace( "file %1% state is %2%", m_name, m_state ) ;
|
2012-05-20 10:57:25 +04:00
|
|
|
}
|
2012-05-17 20:37:11 +04:00
|
|
|
}
|
|
|
|
|
2012-05-29 21:30:06 +04:00
|
|
|
/// Update the resource with the attributes of local file or directory. This
|
|
|
|
/// function will propulate the fields in m_entry.
|
|
|
|
void Resource::FromLocal( const DateTime& last_sync )
|
2012-05-20 15:28:44 +04:00
|
|
|
{
|
|
|
|
fs::path path = Path() ;
|
2012-05-29 21:30:06 +04:00
|
|
|
assert( fs::exists( path ) ) ;
|
2012-05-27 20:24:37 +04:00
|
|
|
|
2012-05-31 19:37:47 +04:00
|
|
|
// root folder is always in sync
|
|
|
|
if ( !IsRoot() )
|
2012-05-20 15:28:44 +04:00
|
|
|
{
|
2012-06-13 21:42:41 +04:00
|
|
|
m_mtime = os::FileCTime( path ) ;
|
|
|
|
|
|
|
|
// follow parent recursively
|
|
|
|
if ( m_parent->m_state == local_new || m_parent->m_state == local_deleted )
|
|
|
|
m_state = local_new ;
|
|
|
|
|
2012-05-30 21:17:22 +04:00
|
|
|
// if the file is not created after last sync, assume file is
|
|
|
|
// remote_deleted first, it will be updated to sync/remote_changed
|
|
|
|
// in FromRemote()
|
2012-06-13 21:42:41 +04:00
|
|
|
else
|
|
|
|
m_state = ( m_mtime > last_sync ? local_new : remote_deleted ) ;
|
2012-05-27 18:49:49 +04:00
|
|
|
|
2012-06-10 12:18:21 +04:00
|
|
|
m_name = path.filename().string() ;
|
|
|
|
m_kind = fs::is_directory(path) ? "folder" : "file" ;
|
|
|
|
m_md5 = fs::is_directory(path) ? "" : crypt::MD5::Get( path ) ;
|
2012-05-20 19:16:59 +04:00
|
|
|
}
|
2012-05-31 19:37:47 +04:00
|
|
|
|
|
|
|
assert( m_state != unknown ) ;
|
2012-05-20 15:28:44 +04:00
|
|
|
}
|
|
|
|
|
2012-05-19 11:41:21 +04:00
|
|
|
std::string Resource::SelfHref() const
|
2012-04-26 19:40:38 +04:00
|
|
|
{
|
2012-06-10 12:18:21 +04:00
|
|
|
return m_href ;
|
2012-04-26 19:40:38 +04:00
|
|
|
}
|
|
|
|
|
2012-05-19 13:14:04 +04:00
|
|
|
std::string Resource::Name() const
|
2012-04-26 19:40:38 +04:00
|
|
|
{
|
2012-06-10 12:18:21 +04:00
|
|
|
return m_name ;
|
2012-04-26 19:40:38 +04:00
|
|
|
}
|
|
|
|
|
2012-05-19 11:41:21 +04:00
|
|
|
std::string Resource::ResourceID() const
|
2012-05-17 20:37:11 +04:00
|
|
|
{
|
2012-06-10 12:18:21 +04:00
|
|
|
return m_id ;
|
2012-05-17 20:37:11 +04:00
|
|
|
}
|
|
|
|
|
2012-05-19 11:41:21 +04:00
|
|
|
const Resource* Resource::Parent() const
|
2012-04-26 20:55:10 +04:00
|
|
|
{
|
2012-05-24 13:47:26 +04:00
|
|
|
assert( m_parent == 0 || m_parent->IsFolder() ) ;
|
2012-04-26 20:55:10 +04:00
|
|
|
return m_parent ;
|
|
|
|
}
|
|
|
|
|
2012-05-19 11:41:21 +04:00
|
|
|
Resource* Resource::Parent()
|
2012-04-26 19:40:38 +04:00
|
|
|
{
|
2012-05-24 13:47:26 +04:00
|
|
|
assert( m_parent == 0 || m_parent->IsFolder() ) ;
|
2012-04-26 19:40:38 +04:00
|
|
|
return m_parent ;
|
|
|
|
}
|
|
|
|
|
2012-05-19 11:41:21 +04:00
|
|
|
void Resource::AddChild( Resource *child )
|
2012-04-26 19:40:38 +04:00
|
|
|
{
|
2012-04-26 20:55:10 +04:00
|
|
|
assert( child != 0 ) ;
|
2012-05-20 13:12:01 +04:00
|
|
|
assert( child->m_parent == 0 || child->m_parent == this ) ;
|
2012-04-26 20:55:10 +04:00
|
|
|
assert( child != this ) ;
|
|
|
|
|
|
|
|
child->m_parent = this ;
|
2012-04-26 19:40:38 +04:00
|
|
|
m_child.push_back( child ) ;
|
|
|
|
}
|
|
|
|
|
2012-05-19 11:41:21 +04:00
|
|
|
void Resource::Swap( Resource& coll )
|
2012-04-26 19:40:38 +04:00
|
|
|
{
|
2012-06-10 12:18:21 +04:00
|
|
|
m_name.swap( coll.m_name ) ;
|
|
|
|
m_kind.swap( coll.m_kind ) ;
|
|
|
|
m_md5.swap( coll.m_md5 ) ;
|
|
|
|
m_etag.swap( coll.m_etag ) ;
|
|
|
|
m_id.swap( coll.m_id ) ;
|
|
|
|
|
|
|
|
m_href.swap( coll.m_href ) ;
|
|
|
|
m_content.swap( coll.m_content ) ;
|
|
|
|
m_edit.swap( coll.m_edit ) ;
|
|
|
|
m_create.swap( coll.m_create ) ;
|
|
|
|
|
|
|
|
m_mtime.Swap( coll.m_mtime ) ;
|
|
|
|
|
2012-04-26 20:55:10 +04:00
|
|
|
std::swap( m_parent, coll.m_parent ) ;
|
2012-04-26 19:40:38 +04:00
|
|
|
m_child.swap( coll.m_child ) ;
|
2012-05-24 13:47:26 +04:00
|
|
|
std::swap( m_state, coll.m_state ) ;
|
2012-04-26 19:40:38 +04:00
|
|
|
}
|
|
|
|
|
2012-05-19 12:18:33 +04:00
|
|
|
bool Resource::IsFolder() const
|
|
|
|
{
|
2012-06-10 12:18:21 +04:00
|
|
|
return m_kind == "folder" ;
|
2012-05-19 12:18:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fs::path Resource::Path() const
|
2012-04-26 20:55:10 +04:00
|
|
|
{
|
|
|
|
assert( m_parent != this ) ;
|
2012-05-19 14:03:02 +04:00
|
|
|
assert( m_parent == 0 || m_parent->IsFolder() ) ;
|
|
|
|
|
2012-06-10 19:11:32 +04:00
|
|
|
return m_parent != 0 ? (m_parent->Path() / m_name) : m_name ;
|
2012-04-26 20:55:10 +04:00
|
|
|
}
|
|
|
|
|
2012-05-19 11:41:21 +04:00
|
|
|
bool Resource::IsInRootTree() const
|
2012-05-12 21:57:15 +04:00
|
|
|
{
|
2012-05-19 14:03:02 +04:00
|
|
|
assert( m_parent == 0 || m_parent->IsFolder() ) ;
|
2012-05-12 21:57:15 +04:00
|
|
|
return m_parent == 0 ? (SelfHref() == root_href) : m_parent->IsInRootTree() ;
|
|
|
|
}
|
|
|
|
|
2012-05-19 13:14:04 +04:00
|
|
|
Resource* Resource::FindChild( const std::string& name )
|
2012-05-17 20:37:11 +04:00
|
|
|
{
|
2012-05-19 11:41:21 +04:00
|
|
|
for ( std::vector<Resource*>::iterator i = m_child.begin() ; i != m_child.end() ; ++i )
|
2012-05-17 20:37:11 +04:00
|
|
|
{
|
|
|
|
assert( (*i)->m_parent == this ) ;
|
2012-06-10 12:18:21 +04:00
|
|
|
if ( (*i)->m_name == name )
|
2012-05-17 20:37:11 +04:00
|
|
|
return *i ;
|
|
|
|
}
|
|
|
|
return 0 ;
|
|
|
|
}
|
2012-05-24 14:57:37 +04:00
|
|
|
|
2012-05-20 10:57:25 +04:00
|
|
|
// try to change the state to "sync"
|
2012-06-03 12:42:51 +04:00
|
|
|
void Resource::Sync( http::Agent *http, const http::Header& auth )
|
2012-05-19 12:18:33 +04:00
|
|
|
{
|
2012-05-31 19:37:47 +04:00
|
|
|
assert( m_state != unknown ) ;
|
2012-06-10 19:38:27 +04:00
|
|
|
assert( !IsRoot() || m_state == sync ) ; // root folder is already synced
|
2012-05-19 12:18:33 +04:00
|
|
|
|
2012-06-10 19:38:27 +04:00
|
|
|
SyncSelf( http, auth ) ;
|
|
|
|
|
2012-06-10 20:40:56 +04:00
|
|
|
// if myself is deleted, no need to do the childrens
|
|
|
|
if ( m_state != local_deleted && m_state != remote_deleted )
|
|
|
|
std::for_each( m_child.begin(), m_child.end(),
|
|
|
|
boost::bind( &Resource::Sync, _1, http, auth ) ) ;
|
2012-06-10 19:38:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::SyncSelf( http::Agent* http, const http::Header& auth )
|
|
|
|
{
|
2012-06-18 19:17:28 +04:00
|
|
|
assert( !IsRoot() || m_state == sync ) ; // root is always sync
|
|
|
|
assert( IsRoot() || http == 0 || fs::is_directory( m_parent->Path() ) ) ;
|
2012-06-10 20:40:56 +04:00
|
|
|
assert( IsRoot() || m_parent->m_state != remote_deleted ) ;
|
|
|
|
assert( IsRoot() || m_parent->m_state != local_deleted ) ;
|
|
|
|
|
|
|
|
const fs::path path = Path() ;
|
|
|
|
|
2012-05-20 10:57:25 +04:00
|
|
|
switch ( m_state )
|
2012-05-19 12:18:33 +04:00
|
|
|
{
|
2012-05-20 10:57:25 +04:00
|
|
|
case local_new :
|
2012-06-10 20:40:56 +04:00
|
|
|
Log( "sync %1% doesn't exist in server, uploading", path, log::info ) ;
|
2012-05-27 18:49:49 +04:00
|
|
|
|
2012-06-17 12:33:38 +04:00
|
|
|
if ( http != 0 && Create( http, auth ) )
|
2012-05-20 21:18:16 +04:00
|
|
|
m_state = sync ;
|
2012-05-20 19:16:59 +04:00
|
|
|
break ;
|
|
|
|
|
|
|
|
case local_deleted :
|
2012-06-10 20:40:56 +04:00
|
|
|
Log( "sync %1% deleted in local. deleting remote", path, log::info ) ;
|
2012-06-17 12:33:38 +04:00
|
|
|
if ( http != 0 )
|
|
|
|
DeleteRemote( http, auth ) ;
|
2012-05-20 10:57:25 +04:00
|
|
|
break ;
|
|
|
|
|
|
|
|
case local_changed :
|
2012-06-10 20:40:56 +04:00
|
|
|
Log( "sync %1% changed in local. uploading", path, log::info ) ;
|
2012-06-17 12:33:38 +04:00
|
|
|
if ( http != 0 && EditContent( http, auth ) )
|
2012-05-20 10:57:25 +04:00
|
|
|
m_state = sync ;
|
|
|
|
break ;
|
|
|
|
|
|
|
|
case remote_new :
|
2012-06-10 20:40:56 +04:00
|
|
|
Log( "sync %1% created in remote. creating local", path, log::info ) ;
|
2012-06-17 12:33:38 +04:00
|
|
|
if ( http != 0 )
|
|
|
|
{
|
|
|
|
if ( IsFolder() )
|
|
|
|
fs::create_directories( path ) ;
|
|
|
|
else
|
|
|
|
Download( http, path, auth ) ;
|
|
|
|
|
|
|
|
m_state = sync ;
|
|
|
|
}
|
2012-06-10 20:40:56 +04:00
|
|
|
break ;
|
|
|
|
|
2012-05-20 10:57:25 +04:00
|
|
|
case remote_changed :
|
2012-06-10 20:40:56 +04:00
|
|
|
assert( !IsFolder() ) ;
|
|
|
|
Log( "sync %1% changed in remote. downloading", path, log::info ) ;
|
2012-06-17 12:33:38 +04:00
|
|
|
if ( http != 0 )
|
|
|
|
{
|
|
|
|
Download( http, path, auth ) ;
|
|
|
|
m_state = sync ;
|
|
|
|
}
|
2012-05-20 10:57:25 +04:00
|
|
|
break ;
|
|
|
|
|
2012-05-30 21:17:22 +04:00
|
|
|
case remote_deleted :
|
2012-06-10 20:40:56 +04:00
|
|
|
Log( "sync %1% deleted in remote. deleting local", path, log::info ) ;
|
2012-06-17 12:33:38 +04:00
|
|
|
if ( http != 0 )
|
|
|
|
DeleteLocal() ;
|
2012-05-30 21:17:22 +04:00
|
|
|
break ;
|
|
|
|
|
2012-05-20 10:57:25 +04:00
|
|
|
case sync :
|
2012-06-10 20:40:56 +04:00
|
|
|
Log( "sync %1% already in sync", path, log::verbose ) ;
|
2012-05-20 19:16:59 +04:00
|
|
|
break ;
|
2012-06-10 19:38:27 +04:00
|
|
|
|
2012-06-10 20:40:56 +04:00
|
|
|
// shouldn't go here
|
2012-06-10 19:38:27 +04:00
|
|
|
case unknown :
|
|
|
|
assert( false ) ;
|
|
|
|
break ;
|
2012-05-20 19:16:59 +04:00
|
|
|
|
2012-05-20 10:57:25 +04:00
|
|
|
default :
|
|
|
|
break ;
|
2012-05-19 12:18:33 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-31 20:40:07 +04:00
|
|
|
/// this function doesn't really remove the local file. it renames it.
|
|
|
|
void Resource::DeleteLocal()
|
|
|
|
{
|
2012-06-03 17:53:51 +04:00
|
|
|
static const boost::format trash_file( "%1%-%2%" ) ;
|
|
|
|
|
2012-05-31 20:40:07 +04:00
|
|
|
assert( m_parent != 0 ) ;
|
|
|
|
fs::path parent = m_parent->Path() ;
|
2012-06-03 17:53:51 +04:00
|
|
|
fs::path dest = ".trash" / parent / Name() ;
|
2012-05-31 20:40:07 +04:00
|
|
|
|
|
|
|
std::size_t idx = 1 ;
|
|
|
|
while ( fs::exists( dest ) && idx != 0 )
|
2012-06-03 17:53:51 +04:00
|
|
|
dest = ".trash" / parent / (boost::format(trash_file) % Name() % idx++).str() ;
|
2012-05-31 20:40:07 +04:00
|
|
|
|
|
|
|
// wrap around! just remove the file
|
|
|
|
if ( idx == 0 )
|
|
|
|
fs::remove_all( Path() ) ;
|
|
|
|
else
|
2012-06-03 17:53:51 +04:00
|
|
|
{
|
|
|
|
fs::create_directories( dest.parent_path() ) ;
|
2012-05-31 20:40:07 +04:00
|
|
|
fs::rename( Path(), dest ) ;
|
2012-06-03 17:53:51 +04:00
|
|
|
}
|
2012-05-31 20:40:07 +04:00
|
|
|
}
|
|
|
|
|
2012-06-03 12:42:51 +04:00
|
|
|
void Resource::DeleteRemote( http::Agent *http, const http::Header& auth )
|
2012-05-19 12:18:33 +04:00
|
|
|
{
|
2012-06-17 12:33:38 +04:00
|
|
|
assert( http != 0 ) ;
|
2012-05-19 12:18:33 +04:00
|
|
|
http::StringResponse str ;
|
2012-06-03 19:58:28 +04:00
|
|
|
|
2012-05-31 20:40:07 +04:00
|
|
|
try
|
|
|
|
{
|
2012-06-03 19:58:28 +04:00
|
|
|
http::Header hdr( auth ) ;
|
2012-06-10 12:18:21 +04:00
|
|
|
hdr.Add( "If-Match: " + m_etag ) ;
|
2012-06-03 19:58:28 +04:00
|
|
|
|
|
|
|
// doesn't know why, but an update before deleting seems to work always
|
|
|
|
http::XmlResponse xml ;
|
2012-06-10 12:18:21 +04:00
|
|
|
http->Get( m_href, &xml, hdr ) ;
|
|
|
|
AssignIDs( Entry( xml.Response() ) ) ;
|
2012-06-03 12:57:21 +04:00
|
|
|
|
2012-06-10 12:18:21 +04:00
|
|
|
http->Custom( "DELETE", m_href, &str, hdr ) ;
|
2012-05-31 20:40:07 +04:00
|
|
|
}
|
2012-06-03 12:42:51 +04:00
|
|
|
catch ( Exception& e )
|
2012-05-31 20:40:07 +04:00
|
|
|
{
|
2012-06-03 10:44:09 +04:00
|
|
|
// don't rethrow here. there are some cases that I don't know why
|
|
|
|
// the delete will fail.
|
2012-06-03 12:42:51 +04:00
|
|
|
Trace( "Exception %1% %2%",
|
|
|
|
boost::diagnostic_information(e),
|
|
|
|
str.Response() ) ;
|
2012-05-31 20:40:07 +04:00
|
|
|
}
|
2012-05-19 12:18:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-03 12:42:51 +04:00
|
|
|
void Resource::Download( http::Agent* http, const fs::path& file, const http::Header& auth ) const
|
2012-05-19 12:18:33 +04:00
|
|
|
{
|
2012-06-10 19:11:32 +04:00
|
|
|
assert( http != 0 ) ;
|
|
|
|
|
2012-05-19 12:18:33 +04:00
|
|
|
http::Download dl( file.string(), http::Download::NoChecksum() ) ;
|
2012-06-10 12:18:21 +04:00
|
|
|
long r = http->Get( m_content, &dl, auth ) ;
|
2012-05-19 12:18:33 +04:00
|
|
|
if ( r <= 400 )
|
2012-06-03 12:57:21 +04:00
|
|
|
{
|
2012-06-10 12:18:21 +04:00
|
|
|
assert( m_mtime != DateTime() ) ;
|
|
|
|
os::SetFileTime( file, m_mtime ) ;
|
2012-06-03 12:57:21 +04:00
|
|
|
}
|
2012-05-20 10:57:25 +04:00
|
|
|
}
|
|
|
|
|
2012-06-03 12:42:51 +04:00
|
|
|
bool Resource::EditContent( http::Agent* http, const http::Header& auth )
|
2012-05-19 12:18:33 +04:00
|
|
|
{
|
2012-06-10 19:11:32 +04:00
|
|
|
assert( http != 0 ) ;
|
2012-05-27 20:24:37 +04:00
|
|
|
assert( m_parent != 0 ) ;
|
2012-06-10 20:40:56 +04:00
|
|
|
assert( m_parent->m_state == sync ) ;
|
2012-05-27 20:24:37 +04:00
|
|
|
|
2012-05-19 12:18:33 +04:00
|
|
|
// upload link missing means that file is read only
|
2012-06-10 12:18:21 +04:00
|
|
|
if ( m_edit.empty() )
|
2012-05-19 12:18:33 +04:00
|
|
|
{
|
2012-06-10 12:18:21 +04:00
|
|
|
Log( "Cannot upload %1%: file read-only. %2%", m_name, m_state, log::warning ) ;
|
2012-05-19 12:18:33 +04:00
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
2012-06-10 12:18:21 +04:00
|
|
|
return Upload( http, m_edit, auth, false ) ;
|
2012-05-20 21:18:16 +04:00
|
|
|
}
|
|
|
|
|
2012-06-03 12:42:51 +04:00
|
|
|
bool Resource::Create( http::Agent* http, const http::Header& auth )
|
2012-05-20 21:18:16 +04:00
|
|
|
{
|
2012-06-10 19:11:32 +04:00
|
|
|
assert( http != 0 ) ;
|
2012-05-20 21:18:16 +04:00
|
|
|
assert( m_parent != 0 ) ;
|
2012-05-28 20:31:29 +04:00
|
|
|
assert( m_parent->IsFolder() ) ;
|
2012-06-10 20:40:56 +04:00
|
|
|
assert( m_parent->m_state == sync ) ;
|
2012-05-27 20:24:37 +04:00
|
|
|
|
|
|
|
if ( IsFolder() )
|
|
|
|
{
|
|
|
|
std::string uri = feed_base ;
|
|
|
|
if ( !m_parent->IsRoot() )
|
2012-06-10 12:18:21 +04:00
|
|
|
uri += ("/" + http->Escape(m_parent->m_id) + "/contents") ;
|
2012-05-27 20:24:37 +04:00
|
|
|
|
2012-06-17 11:58:27 +04:00
|
|
|
std::string meta = (boost::format( xml_meta )
|
|
|
|
% "folder"
|
|
|
|
% xml::Escape(m_name)
|
|
|
|
).str() ;
|
|
|
|
|
2012-06-03 12:42:51 +04:00
|
|
|
http::Header hdr( auth ) ;
|
|
|
|
hdr.Add( "Content-Type: application/atom+xml" ) ;
|
2012-05-27 20:24:37 +04:00
|
|
|
|
|
|
|
http::XmlResponse xml ;
|
2012-05-28 20:31:29 +04:00
|
|
|
// http::ResponseLog log( "create", ".xml", &xml ) ;
|
2012-05-27 20:24:37 +04:00
|
|
|
http->Post( uri, meta, &xml, hdr ) ;
|
2012-06-10 12:18:21 +04:00
|
|
|
AssignIDs( Entry( xml.Response() ) ) ;
|
2012-05-27 20:24:37 +04:00
|
|
|
|
|
|
|
return true ;
|
|
|
|
}
|
2012-06-10 12:18:21 +04:00
|
|
|
else if ( !m_parent->m_create.empty() )
|
2012-05-27 20:24:37 +04:00
|
|
|
{
|
2012-06-10 12:18:21 +04:00
|
|
|
return Upload( http, m_parent->m_create + "?convert=false", auth, true ) ;
|
2012-05-27 20:24:37 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-10 12:18:21 +04:00
|
|
|
Log( "parent of %1% does not exist: cannot upload", Name(), log::warning ) ;
|
2012-05-27 20:24:37 +04:00
|
|
|
return false ;
|
|
|
|
}
|
2012-05-20 21:18:16 +04:00
|
|
|
}
|
2012-05-19 12:18:33 +04:00
|
|
|
|
2012-06-03 12:42:51 +04:00
|
|
|
bool Resource::Upload( http::Agent* http, const std::string& link, const http::Header& auth, bool post )
|
2012-05-20 21:18:16 +04:00
|
|
|
{
|
2012-06-10 19:11:32 +04:00
|
|
|
assert( http != 0 ) ;
|
|
|
|
|
2012-05-27 10:55:26 +04:00
|
|
|
StdioFile file( Path() ) ;
|
2012-05-19 12:18:33 +04:00
|
|
|
|
2012-05-24 20:55:28 +04:00
|
|
|
// TODO: upload in chunks
|
2012-05-20 20:10:29 +04:00
|
|
|
std::string data ;
|
|
|
|
char buf[4096] ;
|
|
|
|
std::size_t count = 0 ;
|
|
|
|
while ( (count = file.Read( buf, sizeof(buf) )) > 0 )
|
|
|
|
data.append( buf, count ) ;
|
|
|
|
|
2012-05-19 12:18:33 +04:00
|
|
|
std::ostringstream xcontent_len ;
|
|
|
|
xcontent_len << "X-Upload-Content-Length: " << data.size() ;
|
|
|
|
|
2012-06-03 12:42:51 +04:00
|
|
|
http::Header hdr( auth ) ;
|
|
|
|
hdr.Add( "Content-Type: application/atom+xml" ) ;
|
|
|
|
hdr.Add( "X-Upload-Content-Type: application/octet-stream" ) ;
|
|
|
|
hdr.Add( xcontent_len.str() ) ;
|
2012-06-10 12:18:21 +04:00
|
|
|
hdr.Add( "If-Match: " + m_etag ) ;
|
2012-06-03 12:42:51 +04:00
|
|
|
hdr.Add( "Expect:" ) ;
|
2012-05-19 12:18:33 +04:00
|
|
|
|
2012-06-07 14:26:57 +04:00
|
|
|
std::string meta = (boost::format( xml_meta )
|
2012-06-10 12:18:21 +04:00
|
|
|
% m_kind
|
|
|
|
% xml::Escape(m_name)
|
2012-06-07 14:26:57 +04:00
|
|
|
).str() ;
|
2012-05-27 20:24:37 +04:00
|
|
|
|
2012-05-19 12:18:33 +04:00
|
|
|
http::StringResponse str ;
|
2012-05-20 21:18:16 +04:00
|
|
|
if ( post )
|
|
|
|
http->Post( link, meta, &str, hdr ) ;
|
|
|
|
else
|
|
|
|
http->Put( link, meta, &str, hdr ) ;
|
2012-05-19 12:18:33 +04:00
|
|
|
|
2012-06-03 12:42:51 +04:00
|
|
|
http::Header uphdr ;
|
|
|
|
uphdr.Add( "Expect:" ) ;
|
|
|
|
uphdr.Add( "Accept:" ) ;
|
2012-05-19 12:18:33 +04:00
|
|
|
|
2012-05-20 20:10:29 +04:00
|
|
|
// the content upload URL is in the "Location" HTTP header
|
|
|
|
std::string uplink = http->RedirLocation() ;
|
|
|
|
http::XmlResponse xml ;
|
2012-05-19 12:18:33 +04:00
|
|
|
|
2012-05-20 20:10:29 +04:00
|
|
|
http->Put( uplink, data, &xml, uphdr ) ;
|
2012-06-10 12:18:21 +04:00
|
|
|
AssignIDs( Entry( xml.Response() ) ) ;
|
2012-05-19 12:18:33 +04:00
|
|
|
|
|
|
|
return true ;
|
|
|
|
}
|
|
|
|
|
2012-05-20 13:12:01 +04:00
|
|
|
Resource::iterator Resource::begin() const
|
|
|
|
{
|
|
|
|
return m_child.begin() ;
|
|
|
|
}
|
|
|
|
|
|
|
|
Resource::iterator Resource::end() const
|
|
|
|
{
|
|
|
|
return m_child.end() ;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t Resource::size() const
|
|
|
|
{
|
|
|
|
return m_child.size() ;
|
|
|
|
}
|
|
|
|
|
2012-05-20 19:16:59 +04:00
|
|
|
std::ostream& operator<<( std::ostream& os, Resource::State s )
|
|
|
|
{
|
|
|
|
static const char *state[] =
|
|
|
|
{
|
2012-05-30 21:17:22 +04:00
|
|
|
"sync", "local_new", "local_changed", "local_deleted", "remote_new",
|
|
|
|
"remote_changed", "remote_deleted"
|
2012-05-20 19:16:59 +04:00
|
|
|
} ;
|
|
|
|
assert( s >= 0 && s < Count(state) ) ;
|
|
|
|
return os << state[s] ;
|
|
|
|
}
|
|
|
|
|
2012-05-24 13:47:26 +04:00
|
|
|
std::string Resource::StateStr() const
|
|
|
|
{
|
|
|
|
std::ostringstream ss ;
|
|
|
|
ss << m_state ;
|
|
|
|
return ss.str() ;
|
|
|
|
}
|
|
|
|
|
2012-06-10 19:11:32 +04:00
|
|
|
std::string Resource::MD5() const
|
|
|
|
{
|
|
|
|
return m_md5 ;
|
|
|
|
}
|
|
|
|
|
2012-05-27 20:24:37 +04:00
|
|
|
bool Resource::IsRoot() const
|
|
|
|
{
|
|
|
|
return m_parent == 0 ;
|
|
|
|
}
|
|
|
|
|
2012-06-03 11:55:32 +04:00
|
|
|
bool Resource::HasID() const
|
|
|
|
{
|
2012-06-10 12:18:21 +04:00
|
|
|
return !m_href.empty() && !m_id.empty() ;
|
2012-06-03 11:55:32 +04:00
|
|
|
}
|
|
|
|
|
2012-04-26 19:40:38 +04:00
|
|
|
} // end of namespace
|
|
|
|
|
|
|
|
namespace std
|
|
|
|
{
|
2012-05-19 11:41:21 +04:00
|
|
|
void swap( gr::Resource& c1, gr::Resource& c2 )
|
2012-04-26 19:40:38 +04:00
|
|
|
{
|
|
|
|
c1.Swap( c2 ) ;
|
|
|
|
}
|
|
|
|
}
|