/
Drive.cc
200 lines (156 loc) · 4.94 KB
/
Drive.cc
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
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 "Drive.hh"
#include "CommonUri.hh"
#include "Entry.hh"
#include "Feed.hh"
#include "http/Agent.hh"
#include "http/ResponseLog.hh"
#include "http/XmlResponse.hh"
#include "protocol/Json.hh"
#include "util/Destroy.hh"
#include "util/log/Log.hh"
#include "xml/Node.hh"
#include "xml/NodeSet.hh"
#include <boost/bind.hpp>
// standard C++ library
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <map>
#include <sstream>
// for debugging only
#include <iostream>
namespace gr {
namespace
{
const std::string state_file = ".grive_state" ;
}
Drive::Drive( http::Agent *http, const Json& options ) :
m_http ( http ),
m_state ( state_file, options ),
m_log_xml ( options["log-xml"].Bool() )
{
assert( m_http != 0 ) ;
}
void Drive::FromRemote( const Entry& entry )
{
// entries from change feed does not have the parent HREF,
// so these checkings are done in normal entries only
Resource *parent = m_state.FindByHref( entry.ParentHref() ) ;
if ( parent != 0 && !parent->IsFolder() )
Log( "warning: entry %1% has parent %2% which is not a folder, ignored",
entry.Title(), parent->Name(), log::verbose ) ;
else if ( parent == 0 || !parent->IsInRootTree() )
Log( "file \"%1%\" parent doesn't exist, ignored", entry.Title(), log::verbose ) ;
else
m_state.FromRemote( entry ) ;
}
void Drive::FromChange( const Entry& entry )
{
if ( entry.IsRemoved() )
Log( "file \"%1%\" represents a deletion, ignored", entry.Title(), log::verbose ) ;
// folders go directly
else
m_state.FromRemote( entry ) ;
}
void Drive::SaveState()
{
m_state.Write( state_file ) ;
}
void Drive::SyncFolders( )
{
assert( m_http != 0 ) ;
Log( "Synchronizing folders", log::info ) ;
http::XmlResponse xml ;
m_http->Get( feed_base + "/-/folder?max-results=50&showroot=true", &xml, http::Header() ) ;
Feed feed( xml.Response() ) ;
do
{
// first, get all collections from the query result
for ( Feed::iterator i = feed.begin() ; i != feed.end() ; ++i )
{
Entry e( *i ) ;
if ( e.Kind() == "folder" )
{
if ( e.ParentHrefs().size() != 1 )
Log( "folder \"%1%\" has multiple parents, ignored", e.Title(), log::verbose ) ;
else if ( e.Title().find('/') != std::string::npos )
Log( "folder \"%1%\" contains a slash in its name, ignored", e.Title(), log::verbose ) ;
else
m_state.FromRemote( e ) ;
}
}
} while ( feed.GetNext( m_http, http::Header() ) ) ;
m_state.ResolveEntry() ;
}
void Drive::DetectChanges()
{
Log( "Reading local directories", log::info ) ;
m_state.FromLocal( "." ) ;
long prev_stamp = m_state.ChangeStamp() ;
Trace( "previous change stamp is %1%", prev_stamp ) ;
SyncFolders( ) ;
Log( "Reading remote server file list", log::info ) ;
Feed feed ;
if ( m_log_xml )
feed.EnableLog( "/tmp/file", ".xml" ) ;
feed.Start( m_http, http::Header(), feed_base + "?showfolders=true&showroot=true" ) ;
m_resume_link = feed.Root()["link"].
Find( "@rel", "http://schemas.google.com/g/2005#resumable-create-media" )["@href"] ;
do
{
std::for_each(
feed.begin(), feed.end(),
boost::bind( &Drive::FromRemote, this, _1 ) ) ;
} while ( feed.GetNext( m_http, http::Header() ) ) ;
// pull the changes feed
if ( prev_stamp != -1 )
{
Log( "Detecting changes from last sync", log::info ) ;
Feed changes ;
if ( m_log_xml )
feed.EnableLog( "/tmp/changes", ".xml" ) ;
feed.Start( m_http, http::Header(), ChangesFeed(prev_stamp+1) ) ;
std::for_each(
changes.begin(), changes.end(),
boost::bind( &Drive::FromChange, this, _1 ) ) ;
}
}
void Drive::Update()
{
Log( "Synchronizing files", log::info ) ;
m_state.Sync( m_http ) ;
UpdateChangeStamp( ) ;
}
void Drive::DryRun()
{
Log( "Synchronizing files (dry-run)", log::info ) ;
m_state.Sync( 0 ) ;
}
void Drive::UpdateChangeStamp( )
{
assert( m_http != 0 ) ;
// get changed feed
http::XmlResponse xrsp ;
m_http->Get( ChangesFeed(m_state.ChangeStamp()+1), &xrsp, http::Header() ) ;
// we should go through the changes to see if it was really Grive to made that change
// maybe by recording the updated timestamp and compare it?
m_state.ChangeStamp(
std::atoi(xrsp.Response()["docs:largestChangestamp"]["@value"].front().Value().c_str()) ) ;
}
} // end of namespace