1 // terrasync.cxx -- scenery fetcher
3 // Started by Curtis Olson, November 2002.
5 // Copyright (C) 2002 Curtis L. Olson - http://www.flightgear.org/~curt
6 // Copyright (C) 2008 Alexander R. Perry <alex.perry@ieee.org>
7 // Copyright (C) 2011 Thorsten Brehm <brehmt@gmail.com>
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 # include <simgear_config.h>
29 #include <simgear/compiler.h>
38 #elif defined(_MSC_VER)
40 # ifndef HAVE_SVN_CLIENT_H
46 #include <stdlib.h> // atoi() atof() abs() system()
47 #include <signal.h> // signal()
54 #include <simgear/compiler.h>
56 #include "terrasync.hxx"
57 #include <simgear/bucket/newbucket.hxx>
58 #include <simgear/misc/sg_path.hxx>
59 #include <simgear/misc/strutils.hxx>
60 #include <simgear/threads/SGQueue.hxx>
61 #include <simgear/scene/tgdb/TileCache.hxx>
62 #include <simgear/misc/sg_dir.hxx>
63 #include <OpenThreads/Thread>
65 #ifdef HAVE_SVN_CLIENT_H
66 # ifdef HAVE_LIBSVN_CLIENT_1
67 # include <svn_version.h>
68 # include <svn_auth.h>
69 # include <svn_client.h>
70 # include <svn_cmdline.h>
71 # include <svn_pools.h>
73 # undef HAVE_SVN_CLIENT_H
77 #ifdef HAVE_SVN_CLIENT_H
78 static const svn_version_checklist_t mysvn_checklist[] = {
79 { "svn_subr", svn_subr_version },
80 { "svn_client", svn_client_version },
83 static const bool svn_built_in_available = true;
85 static const bool svn_built_in_available = false;
88 using namespace simgear;
90 const char* rsync_cmd =
91 "rsync --verbose --archive --delete --perms --owner --group";
93 const char* svn_options =
96 typedef map<string,time_t> CompletedTiles;
98 ///////////////////////////////////////////////////////////////////////////////
99 // WaitingTile ////////////////////////////////////////////////////////////////
100 ///////////////////////////////////////////////////////////////////////////////
104 WaitingTile(string dir,bool refresh) :
105 _dir(dir), _refreshScenery(refresh) {}
107 bool _refreshScenery;
110 ///////////////////////////////////////////////////////////////////////////////
111 // SGTerraSync::SvnThread /////////////////////////////////////////////////////
112 ///////////////////////////////////////////////////////////////////////////////
113 class SGTerraSync::SvnThread : public OpenThreads::Thread
117 virtual ~SvnThread( ) { stop(); }
122 bool isIdle() {return waitingTiles.empty();}
123 void request(const WaitingTile& dir) {waitingTiles.push_front(dir);}
124 bool isDirty() { bool r = _is_dirty;_is_dirty = false;return r;}
125 bool hasNewTiles() { return !_freshTiles.empty();}
126 WaitingTile getNewTile() { return _freshTiles.pop_front();}
128 void setSvnServer(string server) { _svn_server = simgear::strutils::strip(server);}
129 void setExtSvnUtility(string svn_util) { _svn_command = simgear::strutils::strip(svn_util);}
130 void setRsyncServer(string server) { _rsync_server = simgear::strutils::strip(server);}
131 void setLocalDir(string dir) { _local_dir = simgear::strutils::strip(dir);}
132 string getLocalDir() { return _local_dir;}
133 void setUseSvn(bool use_svn) { _use_svn = use_svn;}
135 #ifdef HAVE_SVN_CLIENT_H
136 void setUseBuiltin(bool built_in) { _use_built_in = built_in;}
139 volatile bool _active;
140 volatile bool _running;
142 volatile bool _stalled;
143 volatile int _fail_count;
144 volatile int _updated_tile_count;
145 volatile int _success_count;
146 volatile int _consecutive_errors;
150 bool syncTree(const char* dir);
151 bool syncTreeExternal(const char* dir);
153 #ifdef HAVE_SVN_CLIENT_H
154 static int svnClientSetup(void);
155 bool syncTreeInternal(const char* dir);
159 // Things we need for doing subversion checkout - often
160 static apr_pool_t *_svn_pool;
161 static svn_client_ctx_t *_svn_ctx;
162 static svn_opt_revision_t *_svn_rev;
163 static svn_opt_revision_t *_svn_rev_peg;
166 volatile bool _is_dirty;
168 SGBlockingDeque <WaitingTile> waitingTiles;
169 CompletedTiles _completedTiles;
170 SGBlockingDeque <WaitingTile> _freshTiles;
174 string _rsync_server;
178 #ifdef HAVE_SVN_CLIENT_H
179 apr_pool_t* SGTerraSync::SvnThread::_svn_pool = NULL;
180 svn_client_ctx_t* SGTerraSync::SvnThread::_svn_ctx = NULL;
181 svn_opt_revision_t* SGTerraSync::SvnThread::_svn_rev = NULL;
182 svn_opt_revision_t* SGTerraSync::SvnThread::_svn_rev_peg = NULL;
185 SGTerraSync::SvnThread::SvnThread() :
191 _updated_tile_count(0),
193 _consecutive_errors(0),
194 #ifdef HAVE_SVN_CLIENT_H
201 #ifdef HAVE_SVN_CLIENT_H
202 int errCode = SGTerraSync::SvnThread::svnClientSetup();
203 if (errCode != EXIT_SUCCESS)
205 SG_LOG(SG_TERRAIN,SG_ALERT,
206 "Failed to initialize built-in SVN client, error = " << errCode);
211 void SGTerraSync::SvnThread::stop()
213 // drop any pending requests
214 waitingTiles.clear();
219 // set stop flag and wake up the thread with an empty request
221 WaitingTile w("",false);
227 bool SGTerraSync::SvnThread::start()
234 SG_LOG(SG_TERRAIN,SG_ALERT,
235 "Cannot start scenery download. Local cache directory is undefined.");
241 SGPath path(_local_dir);
242 path.append("version");
245 SG_LOG(SG_TERRAIN,SG_ALERT,
246 "Cannot start scenery download. Directory '" << _local_dir <<
247 "' contains the base package. Use a separate directory.");
252 #ifdef HAVE_SVN_CLIENT_H
253 _use_svn |= _use_built_in;
256 if ((_use_svn)&&(_svn_server==""))
258 SG_LOG(SG_TERRAIN,SG_ALERT,
259 "Cannot start scenery download. Subversion scenery server is undefined.");
265 if ((!_use_svn)&&(_rsync_server==""))
267 SG_LOG(SG_TERRAIN,SG_ALERT,
268 "Cannot start scenery download. Rsync scenery server is undefined.");
275 _updated_tile_count = 0;
277 _consecutive_errors = 0;
283 #ifdef HAVE_SVN_CLIENT_H
284 if (_use_svn && _use_built_in)
285 status = "Using built-in SVN support. ";
290 status = "Using external SVN utility '";
291 status += _svn_command;
296 status = "Using RSYNC. ";
299 // not really an alert - but we want to (always) see this message, so user is
300 // aware we're downloading scenery (and using bandwidth).
301 SG_LOG(SG_TERRAIN,SG_ALERT,
302 "Starting automatic scenery download/synchronization. "
304 << "Directory: '" << _local_dir << "'.");
306 OpenThreads::Thread::start();
310 // sync one directory tree
311 bool SGTerraSync::SvnThread::syncTree(const char* dir)
314 SGPath path( _local_dir );
317 rc = path.create_dir( 0755 );
320 SG_LOG(SG_TERRAIN,SG_ALERT,
321 "Cannot create directory '" << dir << "', return code = " << rc );
325 #ifdef HAVE_SVN_CLIENT_H
327 return syncTreeInternal(dir);
331 return syncTreeExternal(dir);
336 #ifdef HAVE_SVN_CLIENT_H
337 bool SGTerraSync::SvnThread::syncTreeInternal(const char* dir)
339 SG_LOG(SG_TERRAIN,SG_DEBUG, "Synchronizing scenery directory " << dir);
342 SG_LOG(SG_TERRAIN,SG_ALERT,
343 "Built-in SVN client failed to initialize.");
348 char dest_base_dir[512];
349 snprintf( command, 512,
350 "%s/%s", _svn_server.c_str(), dir);
351 snprintf( dest_base_dir, 512,
352 "%s/%s", _local_dir.c_str(), dir);
353 svn_error_t *err = NULL;
355 apr_pool_t *subpool = svn_pool_create(_svn_pool);
358 #if (SVN_VER_MINOR >= 5)
359 err = svn_client_checkout3(NULL,
365 0, // ignore-externals = false
366 0, // allow unver obstructions = false
371 err = svn_client_checkout2(NULL,
376 1, // recurse=true - same as svn_depth_infinity for checkout3 above
377 0, // ignore externals = false
382 bool ReturnValue = true;
385 // Report errors from the checkout attempt
386 SG_LOG(SG_TERRAIN,SG_ALERT,
387 "Failed to synchronize directory '" << dir << "', " <<
389 svn_error_clear(err);
391 err = svn_client_cleanup(dest_base_dir,
395 SG_LOG(SG_TERRAIN,SG_ALERT,
396 "SVN repository cleanup successful for '" << dir << "'.");
401 SG_LOG(SG_TERRAIN,SG_DEBUG, "Done with scenery directory " << dir);
403 svn_pool_destroy(subpool);
408 bool SGTerraSync::SvnThread::syncTreeExternal(const char* dir)
414 snprintf( command, 512,
415 "\"%s\" %s %s/%s \"%s/%s\"", _svn_command.c_str(), svn_options,
416 _svn_server.c_str(), dir,
417 _local_dir.c_str(), dir );
419 snprintf( command, 512,
420 "%s %s/%s/ \"%s/%s/\"", rsync_cmd,
421 _rsync_server.c_str(), dir,
422 _local_dir.c_str(), dir );
424 SG_LOG(SG_TERRAIN,SG_DEBUG, "sync command '" << command << "'");
425 rc = system( command );
428 SG_LOG(SG_TERRAIN,SG_ALERT,
429 "Failed to synchronize directory '" << dir << "', " <<
430 "error code= " << rc);
436 void SGTerraSync::SvnThread::run()
441 WaitingTile next = waitingTiles.pop_front();
445 CompletedTiles::iterator ii =
446 _completedTiles.find( next._dir );
447 time_t now = time(0);
448 if ((ii == _completedTiles.end())||
449 ((ii->second + 60*60*24) < now ))
452 if (!syncTree(next._dir.c_str()))
454 _consecutive_errors++;
459 _consecutive_errors = 0;
461 SG_LOG(SG_TERRAIN,SG_INFO,
462 "Successfully synchronized directory '" << next._dir << "'");
463 if (next._refreshScenery)
466 _updated_tile_count++;
467 _freshTiles.push_back(next);
472 _completedTiles[ next._dir ] = now;
475 if (_consecutive_errors >= 5)
487 #ifdef HAVE_SVN_CLIENT_H
488 // Configure our subversion session
489 int SGTerraSync::SvnThread::svnClientSetup(void)
491 // Are we already prepared?
492 if (_svn_pool) return EXIT_SUCCESS;
493 // No, so initialize svn internals generally
496 // there is a segfault when providing an error stream.
497 // Apparently, calling setvbuf with a nul buffer is
498 // not supported under msvc 7.1 ( code inside svn_cmdline_init )
499 if (svn_cmdline_init("terrasync", 0) != EXIT_SUCCESS)
502 if (svn_cmdline_init("terrasync", stderr) != EXIT_SUCCESS)
505 /* Oh no! svn_cmdline_init configures the locale - affecting numeric output
506 * formats (i.e. sprintf("%f", ...)). fgfs relies on "C" locale in many places
507 * (including assumptions on required sprintf buffer sizes). Things go horribly
508 * wrong when the locale is changed to anything else but "C". Might be enough to
509 * revert LC_NUMERIC locale - but we'll do a complete revert for now...*/
510 setlocale(LC_ALL,"C");
513 apr_pool_create(&pool, NULL);
514 svn_error_t *err = NULL;
515 SVN_VERSION_DEFINE(_svn_version);
516 err = svn_ver_check_list(&_svn_version, mysvn_checklist);
518 return svn_cmdline_handle_exit_error(err, pool, "fgfs: ");
519 err = svn_ra_initialize(pool);
521 return svn_cmdline_handle_exit_error(err, pool, "fgfs: ");
522 char *config_dir = NULL;
523 err = svn_config_ensure(config_dir, pool);
525 return svn_cmdline_handle_exit_error(err, pool, "fgfs: ");
526 err = svn_client_create_context(&_svn_ctx, pool);
528 return svn_cmdline_handle_exit_error(err, pool, "fgfs: ");
529 err = svn_config_get_config(&(_svn_ctx->config),
532 return svn_cmdline_handle_exit_error(err, pool, "fgfs: ");
534 cfg = ( svn_config_t*) apr_hash_get(
536 SVN_CONFIG_CATEGORY_CONFIG,
537 APR_HASH_KEY_STRING);
539 return svn_cmdline_handle_exit_error(err, pool, "fgfs: ");
541 svn_auth_baton_t *ab=NULL;
543 #if (SVN_VER_MINOR >= 6)
544 err = svn_cmdline_create_auth_baton (&ab,
545 TRUE, NULL, NULL, config_dir, TRUE, FALSE, cfg,
546 _svn_ctx->cancel_func, _svn_ctx->cancel_baton, pool);
548 err = svn_cmdline_setup_auth_baton(&ab,
549 TRUE, NULL, NULL, config_dir, TRUE, cfg,
550 _svn_ctx->cancel_func, _svn_ctx->cancel_baton, pool);
554 return svn_cmdline_handle_exit_error(err, pool, "fgfs: ");
556 _svn_ctx->auth_baton = ab;
557 #if (SVN_VER_MINOR >= 5)
558 _svn_ctx->conflict_func = NULL;
559 _svn_ctx->conflict_baton = NULL;
562 // Now our magic revisions
563 _svn_rev = (svn_opt_revision_t*) apr_palloc(pool,
564 sizeof(svn_opt_revision_t));
567 _svn_rev_peg = (svn_opt_revision_t*) apr_palloc(pool,
568 sizeof(svn_opt_revision_t));
571 _svn_rev->kind = svn_opt_revision_head;
572 _svn_rev_peg->kind = svn_opt_revision_unspecified;
573 // Success if we got this far
579 ///////////////////////////////////////////////////////////////////////////////
580 // SGTerraSync ////////////////////////////////////////////////////////////////
581 ///////////////////////////////////////////////////////////////////////////////
582 SGTerraSync::SGTerraSync(SGPropertyNode_ptr root) :
586 _terraRoot(root->getNode("/sim/terrasync",true)),
589 _svnThread = new SvnThread();
592 SGTerraSync::~SGTerraSync()
594 _tiedProperties.Untie();
599 void SGTerraSync::init()
601 _refresh_display = _terraRoot->getNode("refresh-display",true);
602 _terraRoot->getNode("built-in-svn-available",true)->setBoolValue(svn_built_in_available);
606 void SGTerraSync::reinit()
608 // do not reinit when enabled and we're already up and running
609 if ((_terraRoot->getBoolValue("enabled",false))&&
610 (_svnThread->_active && _svnThread->_running))
615 if (_terraRoot->getBoolValue("enabled",false))
617 _svnThread->setSvnServer(_terraRoot->getStringValue("svn-server",""));
618 _svnThread->setRsyncServer(_terraRoot->getStringValue("rsync-server",""));
619 _svnThread->setLocalDir(_terraRoot->getStringValue("scenery-dir",""));
621 #ifdef HAVE_SVN_CLIENT_H
622 _svnThread->setUseBuiltin(_terraRoot->getBoolValue("use-built-in-svn",true));
624 _terraRoot->getNode("use-built-in-svn",true)->setBoolValue(false);
626 _svnThread->setUseSvn(_terraRoot->getBoolValue("use-svn",true));
627 _svnThread->setExtSvnUtility(_terraRoot->getStringValue("ext-svn-utility","svn"));
629 if (_svnThread->start())
630 syncAirportsModels();
633 _stalled_node->setBoolValue(_svnThread->_stalled);
638 void SGTerraSync::bind()
640 _tiedProperties.Tie( _terraRoot->getNode("busy", true), (bool*) &_svnThread->_busy );
641 _tiedProperties.Tie( _terraRoot->getNode("active", true), (bool*) &_svnThread->_active );
642 _tiedProperties.Tie( _terraRoot->getNode("update-count", true), (int*) &_svnThread->_success_count );
643 _tiedProperties.Tie( _terraRoot->getNode("error-count", true), (int*) &_svnThread->_fail_count );
644 _tiedProperties.Tie( _terraRoot->getNode("tile-count", true), (int*) &_svnThread->_updated_tile_count );
645 _terraRoot->getNode("busy", true)->setAttribute(SGPropertyNode::WRITE,false);
646 _terraRoot->getNode("active", true)->setAttribute(SGPropertyNode::WRITE,false);
647 _terraRoot->getNode("update-count", true)->setAttribute(SGPropertyNode::WRITE,false);
648 _terraRoot->getNode("error-count", true)->setAttribute(SGPropertyNode::WRITE,false);
649 _terraRoot->getNode("tile-count", true)->setAttribute(SGPropertyNode::WRITE,false);
650 _terraRoot->getNode("use-built-in-svn", true)->setAttribute(SGPropertyNode::USERARCHIVE,false);
651 _terraRoot->getNode("use-svn", true)->setAttribute(SGPropertyNode::USERARCHIVE,false);
652 // stalled is used as a signal handler (to connect listeners triggering GUI pop-ups)
653 _stalled_node = _terraRoot->getNode("stalled", true);
654 _stalled_node->setBoolValue(_svnThread->_stalled);
655 _stalled_node->setAttribute(SGPropertyNode::PRESERVE,true);
658 void SGTerraSync::unbind()
661 _tiedProperties.Untie();
664 void SGTerraSync::update(double)
666 static SGBucket bucket;
667 if (_svnThread->isDirty())
669 if (!_svnThread->_active)
671 if (_svnThread->_stalled)
673 SG_LOG(SG_TERRAIN,SG_ALERT,
674 "Automatic scenery download/synchronization stalled. Too many errors.");
678 // not really an alert - just always show this message
679 SG_LOG(SG_TERRAIN,SG_ALERT,
680 "Automatic scenery download/synchronization has stopped.");
682 _stalled_node->setBoolValue(_svnThread->_stalled);
685 if (!_refresh_display->getBoolValue())
688 while (_svnThread->hasNewTiles())
690 WaitingTile next = _svnThread->getNewTile();
691 if (next._refreshScenery)
693 refreshScenery(_svnThread->getLocalDir(),next._dir);
699 void SGTerraSync::refreshScenery(SGPath path,const string& relativeDir)
701 // find tiles to be refreshed
704 path.append(relativeDir);
707 simgear::Dir dir(path);
708 //TODO need to be smarter here. only update tiles which actually
709 // changed recently. May also be possible to use information from the
710 // built-in SVN client directly (instead of checking directory contents).
711 PathList tileList = dir.children(simgear::Dir::TYPE_FILE, ".stg");
712 for (unsigned int i=0; i<tileList.size(); ++i)
714 // reload scenery tile
715 long index = atoi(tileList[i].file().c_str());
716 _tile_cache->refresh_tile(index);
722 bool SGTerraSync::isIdle() {return _svnThread->isIdle();}
724 void SGTerraSync::setTileCache(TileCache* tile_cache)
726 _tile_cache = tile_cache;
729 void SGTerraSync::syncAirportsModels()
732 for ( synced_other = 'K'; synced_other <= 'Z'; synced_other++ )
735 snprintf( dir, 512, "Airports/%c", synced_other );
736 WaitingTile w(dir,false);
737 _svnThread->request( w );
739 for ( synced_other = 'A'; synced_other <= 'J'; synced_other++ )
742 snprintf( dir, 512, "Airports/%c", synced_other );
743 WaitingTile w(dir,false);
744 _svnThread->request( w );
746 WaitingTile w("Models",false);
747 _svnThread->request( w );
751 void SGTerraSync::syncArea( int lat, int lon )
753 if ( lat < -90 || lat > 90 || lon < -180 || lon > 180 )
756 int baselat, baselon;
759 int base = (int)(lat / 10);
760 if ( lat == base * 10 ) {
763 baselat = (base - 1) * 10;
767 baselat = (int)(lat / 10) * 10;
771 int base = (int)(lon / 10);
772 if ( lon == base * 10 ) {
775 baselon = (base - 1) * 10;
779 baselon = (int)(lon / 10) * 10;
783 const char* terrainobjects[3] = { "Terrain", "Objects", 0 };
785 for (const char** tree = &terrainobjects[0]; *tree; tree++)
788 snprintf( dir, 512, "%s/%c%03d%c%02d/%c%03d%c%02d",
790 EW, abs(baselon), NS, abs(baselat),
791 EW, abs(lon), NS, abs(lat) );
792 WaitingTile w(dir,refresh);
793 _svnThread->request( w );
799 void SGTerraSync::syncAreas( int lat, int lon, int lat_dir, int lon_dir )
801 if ( lat_dir == 0 && lon_dir == 0 ) {
802 // do surrounding 8 1x1 degree areas.
803 for ( int i = lat - 1; i <= lat + 1; ++i ) {
804 for ( int j = lon - 1; j <= lon + 1; ++j ) {
805 if ( i != lat || j != lon ) {
811 if ( lat_dir != 0 ) {
812 syncArea( lat + lat_dir, lon - 1 );
813 syncArea( lat + lat_dir, lon + 1 );
814 syncArea( lat + lat_dir, lon );
816 if ( lon_dir != 0 ) {
817 syncArea( lat - 1, lon + lon_dir );
818 syncArea( lat + 1, lon + lon_dir );
819 syncArea( lat, lon + lon_dir );
823 // do current 1x1 degree area first
824 syncArea( lat, lon );
828 bool SGTerraSync::schedulePosition(int lat, int lon)
830 // Ignore messages where the location does not change
831 if ( lat != last_lat || lon != last_lon )
833 SG_LOG(SG_TERRAIN,SG_DEBUG, "Requesting scenery update for position " <<
835 int lat_dir, lon_dir, dist;
836 if ( last_lat == NOWHERE || last_lon == NOWHERE )
838 lat_dir = lon_dir = 0;
841 dist = lat - last_lat;
844 lat_dir = dist / abs(dist);
850 dist = lon - last_lon;
853 lon_dir = dist / abs(dist);
860 SG_LOG(SG_TERRAIN,SG_DEBUG, "Scenery update for " <<
861 "lat = " << lat << ", lon = " << lon <<
862 ", lat_dir = " << lat_dir << ", " <<
863 "lon_dir = " << lon_dir);
865 syncAreas( lat, lon, lat_dir, lon_dir );