1 // HTTPRepository.cxx -- plain HTTP TerraSync remote client
3 // Copyright (C) 20126 James Turner <zakalawe@mac.com>
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "HTTPRepository.hxx"
21 #include <simgear_config.h>
35 #include "simgear/debug/logstream.hxx"
36 #include "simgear/misc/strutils.hxx"
37 #include <simgear/misc/sg_dir.hxx>
38 #include <simgear/io/HTTPClient.hxx>
39 #include <simgear/io/sg_file.hxx>
40 #include <simgear/misc/sgstream.hxx>
41 #include <simgear/structure/exception.hxx>
42 #include <simgear/timing/timestamp.hxx>
44 #include <simgear/misc/sg_hash.hxx>
51 class HTTPRepoGetRequest : public HTTP::Request
54 HTTPRepoGetRequest(HTTPDirectory* d, const std::string& u) :
60 virtual void cancel();
62 size_t contentSize() const
67 void setContentSize(size_t sz)
72 HTTPDirectory* _directory;
76 typedef SGSharedPtr<HTTPRepoGetRequest> RepoRequestPtr;
90 typedef std::vector<HashCacheEntry> HashCache;
97 AbstractRepository::ResultCode error;
100 typedef std::vector<Failure> FailureList;
101 FailureList failures;
103 HTTPRepoPrivate(HTTPRepository* parent) :
104 hashCacheDirty(false),
107 status(AbstractRepository::REPO_NO_ERROR),
113 HTTPRepository* p; // link back to outer
118 AbstractRepository::ResultCode status;
119 HTTPDirectory* rootDir;
120 size_t totalDownloaded;
122 HTTP::Request_ptr updateFile(HTTPDirectory* dir, const std::string& name,
124 HTTP::Request_ptr updateDir(HTTPDirectory* dir, const std::string& hash,
127 std::string hashForPath(const SGPath& p);
128 void updatedFileContents(const SGPath& p, const std::string& newHash);
129 void parseHashCache();
130 std::string computeHashForPath(const SGPath& p);
131 void writeHashCache();
133 void failedToGetRootIndex(AbstractRepository::ResultCode st);
134 void failedToUpdateChild(const SGPath& relativePath,
135 AbstractRepository::ResultCode fileStatus);
137 typedef std::vector<RepoRequestPtr> RequestVector;
138 RequestVector queuedRequests,
141 void makeRequest(RepoRequestPtr req);
142 void finishedRequest(const RepoRequestPtr& req);
144 HTTPDirectory* getOrCreateDirectory(const std::string& path);
145 bool deleteDirectory(const std::string& path);
147 typedef std::vector<HTTPDirectory*> DirectoryVector;
148 DirectoryVector directories;
162 ChildInfo(Type ty, const std::string & nameData, const std::string & hashData) :
170 ChildInfo(const ChildInfo& other) :
174 sizeInBytes(other.sizeInBytes)
177 void setSize(const std::string & sizeData)
179 sizeInBytes = ::strtol(sizeData.c_str(), NULL, 10);
182 bool operator<(const ChildInfo& other) const
184 return name < other.name;
188 std::string name, hash;
192 typedef std::vector<ChildInfo> ChildInfoList;
193 ChildInfoList children;
196 HTTPDirectory(HTTPRepoPrivate* repo, const std::string& path) :
202 SGPath p(absolutePath());
205 // already exists on disk
206 parseDirIndex(children);
207 std::sort(children.begin(), children.end());
208 } catch (sg_exception& ) {
209 // parsing cache failed
215 HTTPRepoPrivate* repository() const
220 std::string url() const
222 if (_relativePath.str().empty()) {
223 return _repository->baseUrl;
226 return _repository->baseUrl + "/" + _relativePath.str();
229 void dirIndexUpdated(const std::string& hash)
231 SGPath fpath(_relativePath);
232 fpath.append(".dirindex");
233 _repository->updatedFileContents(fpath, hash);
236 parseDirIndex(children);
237 std::sort(children.begin(), children.end());
240 void failedToUpdate(AbstractRepository::ResultCode status)
242 if (_relativePath.isNull()) {
244 _repository->failedToGetRootIndex(status);
246 _repository->failedToUpdateChild(_relativePath, status);
250 void updateChildrenBasedOnHash()
252 //SG_LOG(SG_TERRASYNC, SG_DEBUG, "updated children for:" << relativePath());
254 string_list indexNames = indexChildren(),
255 toBeUpdated, orphans;
256 simgear::Dir d(absolutePath());
257 PathList fsChildren = d.children(0);
258 PathList::const_iterator it = fsChildren.begin();
261 for (; it != fsChildren.end(); ++it) {
262 ChildInfo info(it->isDir() ? ChildInfo::DirectoryType : ChildInfo::FileType,
264 std::string hash = hashForChild(info);
266 ChildInfoList::iterator c = findIndexChild(it->file());
267 if (c == children.end()) {
268 SG_LOG(SG_TERRASYNC, SG_DEBUG, "is orphan '" << it->file() << "'" );
269 orphans.push_back(it->file());
270 } else if (c->hash != hash) {
271 SG_LOG(SG_TERRASYNC, SG_DEBUG, "hash mismatch'" << it->file() );
272 // file exists, but hash mismatch, schedule update
274 SG_LOG(SG_TERRASYNC, SG_DEBUG, "file exists but hash is wrong for:" << it->file() );
275 SG_LOG(SG_TERRASYNC, SG_DEBUG, "on disk:" << hash << " vs in info:" << c->hash);
278 toBeUpdated.push_back(it->file() );
280 // file exists and hash is valid. If it's a directory,
281 // perform a recursive check.
282 SG_LOG(SG_TERRASYNC, SG_DEBUG, "file exists hash is good:" << it->file() );
283 if (c->type == ChildInfo::DirectoryType) {
284 SGPath p(relativePath());
285 p.append(it->file());
286 HTTPDirectory* childDir = _repository->getOrCreateDirectory(p.str());
287 childDir->updateChildrenBasedOnHash();
291 // remove existing file system children from the index list,
292 // so we can detect new children
293 // https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Erase-Remove
294 indexNames.erase(std::remove(indexNames.begin(), indexNames.end(), it->file()), indexNames.end());
295 } // of real children iteration
297 // all remaining names in indexChilden are new children
298 toBeUpdated.insert(toBeUpdated.end(), indexNames.begin(), indexNames.end());
300 removeOrphans(orphans);
301 scheduleUpdates(toBeUpdated);
304 void removeOrphans(const string_list& orphans)
306 string_list::const_iterator it;
307 for (it = orphans.begin(); it != orphans.end(); ++it) {
312 string_list indexChildren() const
315 r.reserve(children.size());
316 ChildInfoList::const_iterator it;
317 for (it=children.begin(); it != children.end(); ++it) {
318 r.push_back(it->name);
323 void scheduleUpdates(const string_list& names)
325 string_list::const_iterator it;
326 for (it = names.begin(); it != names.end(); ++it) {
327 ChildInfoList::iterator cit = findIndexChild(*it);
328 if (cit == children.end()) {
329 SG_LOG(SG_TERRASYNC, SG_WARN, "scheduleUpdate, unknown child:" << *it);
333 SG_LOG(SG_TERRASYNC,SG_DEBUG, "scheduling update for " << *it );
334 if (cit->type == ChildInfo::FileType) {
335 _repository->updateFile(this, *it, cit->sizeInBytes);
337 SGPath p(relativePath());
339 HTTPDirectory* childDir = _repository->getOrCreateDirectory(p.str());
340 _repository->updateDir(childDir, cit->hash, cit->sizeInBytes);
345 SGPath absolutePath() const
347 SGPath r(_repository->basePath);
348 r.append(_relativePath.str());
352 SGPath relativePath() const
354 return _relativePath;
357 void didUpdateFile(const std::string& file, const std::string& hash, size_t sz)
359 // check hash matches what we expected
360 ChildInfoList::iterator it = findIndexChild(file);
361 if (it == children.end()) {
362 SG_LOG(SG_TERRASYNC, SG_WARN, "updated file but not found in dir:" << _relativePath << " " << file);
364 SGPath fpath(_relativePath);
367 if (it->hash != hash) {
368 _repository->failedToUpdateChild(_relativePath, AbstractRepository::REPO_ERROR_CHECKSUM);
370 _repository->updatedFileContents(fpath, hash);
371 _repository->totalDownloaded += sz;
372 //SG_LOG(SG_TERRASYNC, SG_INFO, "did update:" << fpath);
374 } // of found in child list
377 void didFailToUpdateFile(const std::string& file,
378 AbstractRepository::ResultCode status)
380 SGPath fpath(_relativePath);
382 _repository->failedToUpdateChild(fpath, status);
388 ChildWithName(const std::string& n) : name(n) {}
391 bool operator()(const ChildInfo& info) const
392 { return info.name == name; }
395 ChildInfoList::iterator findIndexChild(const std::string& name)
397 return std::find_if(children.begin(), children.end(), ChildWithName(name));
400 bool parseDirIndex(ChildInfoList& children)
402 SGPath p(absolutePath());
403 p.append(".dirindex");
408 std::ifstream indexStream( p.c_str(), std::ios::in );
410 if ( !indexStream.is_open() ) {
411 throw sg_io_exception("cannot open dirIndex file", p);
414 while (!indexStream.eof() ) {
416 std::getline( indexStream, line );
417 line = simgear::strutils::strip(line);
419 // skip blank line or comment beginning with '#'
420 if( line.empty() || line[0] == '#' )
423 string_list tokens = simgear::strutils::split( line, ":" );
425 std::string typeData = tokens[0];
427 if( typeData == "version" ) {
428 if( tokens.size() < 2 ) {
429 SG_LOG(SG_TERRASYNC, SG_WARN, "malformed .dirindex file: missing version number in line '" << line << "'" );
432 if( tokens[1] != "1" ) {
433 SG_LOG(SG_TERRASYNC, SG_WARN, "invalid .dirindex file: wrong version number '" << tokens[1] << "' (expected 1)" );
436 continue; // version is good, continue
439 if( typeData == "path" ) {
440 continue; // ignore path, next line
443 if( tokens.size() < 3 ) {
444 SG_LOG(SG_TERRASYNC, SG_WARN, "malformed .dirindex file: not enough tokens in line '" << line << "' (ignoring line)" );
448 if (typeData != "f" && typeData != "d" ) {
449 SG_LOG(SG_TERRASYNC, SG_WARN, "malformed .dirindex file: invalid type in line '" << line << "', expected 'd' or 'f', (ignoring line)" );
452 children.push_back(ChildInfo(typeData == "f" ? ChildInfo::FileType : ChildInfo::DirectoryType, tokens[1], tokens[2]));
454 if (tokens.size() > 3) {
455 children.back().setSize(tokens[3]);
462 void removeChild(const std::string& name)
464 SGPath p(absolutePath());
468 SGPath fpath(_relativePath);
472 ok = _repository->deleteDirectory(fpath.str());
474 // remove the hash cache entry
475 _repository->updatedFileContents(fpath, std::string());
480 SG_LOG(SG_TERRASYNC, SG_WARN, "removal failed for:" << p);
481 throw sg_io_exception("Failed to remove existing file/dir:", p);
485 std::string hashForChild(const ChildInfo& child) const
487 SGPath p(absolutePath());
488 p.append(child.name);
489 if (child.type == ChildInfo::DirectoryType) {
490 p.append(".dirindex");
492 return _repository->hashForPath(p);
495 HTTPRepoPrivate* _repository;
496 SGPath _relativePath; // in URL and file-system space
501 HTTPRepository::HTTPRepository(const SGPath& base, HTTP::Client *cl) :
502 _d(new HTTPRepoPrivate(this))
506 _d->rootDir = new HTTPDirectory(_d.get(), "");
507 _d->parseHashCache();
510 HTTPRepository::~HTTPRepository()
514 void HTTPRepository::setBaseUrl(const std::string &url)
519 std::string HTTPRepository::baseUrl() const
524 HTTP::Client* HTTPRepository::http() const
529 SGPath HTTPRepository::fsBase() const
534 void HTTPRepository::update()
536 if (_d->isUpdating) {
540 _d->status = REPO_NO_ERROR;
541 _d->isUpdating = true;
542 _d->failures.clear();
543 _d->updateDir(_d->rootDir, std::string(), 0);
546 bool HTTPRepository::isDoingSync() const
548 if (_d->status != REPO_NO_ERROR) {
552 return _d->isUpdating;
555 size_t HTTPRepository::bytesToDownload() const
559 HTTPRepoPrivate::RequestVector::const_iterator r;
560 for (r = _d->queuedRequests.begin(); r != _d->queuedRequests.end(); ++r) {
561 result += (*r)->contentSize();
564 for (r = _d->activeRequests.begin(); r != _d->activeRequests.end(); ++r) {
565 result += (*r)->contentSize() - (*r)->responseBytesReceived();
571 size_t HTTPRepository::bytesDownloaded() const
573 size_t result = _d->totalDownloaded;
575 HTTPRepoPrivate::RequestVector::const_iterator r;
576 for (r = _d->activeRequests.begin(); r != _d->activeRequests.end(); ++r) {
577 result += (*r)->responseBytesReceived();
583 AbstractRepository::ResultCode
584 HTTPRepository::failure() const
586 if ((_d->status == REPO_NO_ERROR) && !_d->failures.empty()) {
587 return REPO_PARTIAL_UPDATE;
593 void HTTPRepoGetRequest::cancel()
595 _directory->repository()->http->cancelRequest(this, "Reposiotry cancelled");
599 class FileGetRequest : public HTTPRepoGetRequest
602 FileGetRequest(HTTPDirectory* d, const std::string& file) :
603 HTTPRepoGetRequest(d, makeUrl(d, file)),
606 pathInRepo = _directory->absolutePath();
607 pathInRepo.append(fileName);
608 //SG_LOG(SG_TERRASYNC, SG_INFO, "will GET file " << url());
612 virtual void gotBodyData(const char* s, int n)
615 file.reset(new SGBinaryFile(pathInRepo.str()));
616 if (!file->open(SG_IO_OUT)) {
617 SG_LOG(SG_TERRASYNC, SG_WARN, "unable to create file " << pathInRepo);
618 _directory->repository()->http->cancelRequest(this, "Unable to create output file");
621 sha1_init(&hashContext);
624 sha1_write(&hashContext, s, n);
628 virtual void onDone()
631 if (responseCode() == 200) {
632 std::string hash = strutils::encodeHex(sha1_result(&hashContext), HASH_LENGTH);
633 _directory->didUpdateFile(fileName, hash, contentSize());
634 SG_LOG(SG_TERRASYNC, SG_DEBUG, "got file " << fileName << " in " << _directory->absolutePath());
635 } else if (responseCode() == 404) {
636 SG_LOG(SG_TERRASYNC, SG_WARN, "terrasync file not found on server: " << fileName << " for " << _directory->absolutePath());
637 _directory->didFailToUpdateFile(fileName, AbstractRepository::REPO_ERROR_FILE_NOT_FOUND);
639 SG_LOG(SG_TERRASYNC, SG_WARN, "terrasync file download error on server: " << fileName << " for " << _directory->absolutePath() << ": " << responseCode() );
640 _directory->didFailToUpdateFile(fileName, AbstractRepository::REPO_ERROR_HTTP);
643 _directory->repository()->finishedRequest(this);
646 virtual void onFail()
649 if (pathInRepo.exists()) {
654 _directory->didFailToUpdateFile(fileName, AbstractRepository::REPO_ERROR_SOCKET);
655 _directory->repository()->finishedRequest(this);
659 static std::string makeUrl(HTTPDirectory* d, const std::string& file)
661 return d->url() + "/" + file;
664 std::string fileName; // if empty, we're getting the directory itself
666 simgear::sha1nfo hashContext;
667 std::auto_ptr<SGBinaryFile> file;
670 class DirGetRequest : public HTTPRepoGetRequest
673 DirGetRequest(HTTPDirectory* d, const std::string& targetHash) :
674 HTTPRepoGetRequest(d, makeUrl(d)),
676 _targetHash(targetHash)
678 sha1_init(&hashContext);
679 //SG_LOG(SG_TERRASYNC, SG_INFO, "will GET dir " << url());
687 bool isRootDir() const
693 virtual void gotBodyData(const char* s, int n)
695 body += std::string(s, n);
696 sha1_write(&hashContext, s, n);
699 virtual void onDone()
701 if (responseCode() == 200) {
702 std::string hash = strutils::encodeHex(sha1_result(&hashContext), HASH_LENGTH);
703 if (!_targetHash.empty() && (hash != _targetHash)) {
704 _directory->failedToUpdate(AbstractRepository::REPO_ERROR_CHECKSUM);
705 _directory->repository()->finishedRequest(this);
709 std::string curHash = _directory->repository()->hashForPath(path());
710 if (hash != curHash) {
711 simgear::Dir d(_directory->absolutePath());
713 if (!d.create(0700)) {
714 throw sg_io_exception("Unable to create directory", d.path());
718 // dir index data has changed, so write to disk and update
719 // the hash accordingly
720 std::ofstream of(pathInRepo().c_str(), std::ios::trunc | std::ios::out);
722 throw sg_io_exception("Failed to open directory index file for writing", pathInRepo().c_str());
725 of.write(body.data(), body.size());
727 _directory->dirIndexUpdated(hash);
729 //SG_LOG(SG_TERRASYNC, SG_INFO, "updated dir index " << _directory->absolutePath());
732 _directory->repository()->totalDownloaded += contentSize();
735 // either way we've confirmed the index is valid so update
739 _directory->updateChildrenBasedOnHash();
740 SG_LOG(SG_TERRASYNC, SG_INFO, "after update of:" << _directory->absolutePath() << " child update took:" << st.elapsedMSec());
741 } catch (sg_exception& ) {
742 _directory->failedToUpdate(AbstractRepository::REPO_ERROR_IO);
744 } else if (responseCode() == 404) {
745 _directory->failedToUpdate(AbstractRepository::REPO_ERROR_FILE_NOT_FOUND);
747 _directory->failedToUpdate(AbstractRepository::REPO_ERROR_HTTP);
750 _directory->repository()->finishedRequest(this);
753 virtual void onFail()
756 _directory->failedToUpdate(AbstractRepository::REPO_ERROR_SOCKET);
757 _directory->repository()->finishedRequest(this);
761 static std::string makeUrl(HTTPDirectory* d)
763 return d->url() + "/.dirindex";
766 SGPath pathInRepo() const
768 SGPath p(_directory->absolutePath());
769 p.append(".dirindex");
773 simgear::sha1nfo hashContext;
775 bool _isRootDir; ///< is this the repository root?
776 std::string _targetHash;
779 HTTPRepoPrivate::~HTTPRepoPrivate()
781 DirectoryVector::iterator it;
782 for (it=directories.begin(); it != directories.end(); ++it) {
786 RequestVector::iterator r;
787 for (r=activeRequests.begin(); r != activeRequests.end(); ++r) {
792 HTTP::Request_ptr HTTPRepoPrivate::updateFile(HTTPDirectory* dir, const std::string& name, size_t sz)
794 RepoRequestPtr r(new FileGetRequest(dir, name));
795 r->setContentSize(sz);
800 HTTP::Request_ptr HTTPRepoPrivate::updateDir(HTTPDirectory* dir, const std::string& hash, size_t sz)
802 RepoRequestPtr r(new DirGetRequest(dir, hash));
803 r->setContentSize(sz);
809 class HashEntryWithPath
812 HashEntryWithPath(const std::string& p) : path(p) {}
813 bool operator()(const HTTPRepoPrivate::HashCacheEntry& entry) const
814 { return entry.filePath == path; }
819 std::string HTTPRepoPrivate::hashForPath(const SGPath& p)
821 HashCache::iterator it = std::find_if(hashes.begin(), hashes.end(), HashEntryWithPath(p.str()));
822 if (it != hashes.end()) {
823 // ensure data on disk hasn't changed.
824 // we could also use the file type here if we were paranoid
825 if ((p.sizeInBytes() == it->lengthBytes) && (p.modTime() == it->modTime)) {
829 // entry in the cache, but it's stale so remove and fall through
833 std::string hash = computeHashForPath(p);
834 updatedFileContents(p, hash);
838 std::string HTTPRepoPrivate::computeHashForPath(const SGPath& p)
841 return std::string();
844 char* buf = static_cast<char*>(malloc(1024 * 1024));
846 SGBinaryFile f(p.str());
847 if (!f.open(SG_IO_IN)) {
848 throw sg_io_exception("Couldn't open file for compute hash", p);
850 while ((readLen = f.read(buf, 1024 * 1024)) > 0) {
851 sha1_write(&info, buf, readLen);
856 std::string hashBytes((char*) sha1_result(&info), HASH_LENGTH);
857 return strutils::encodeHex(hashBytes);
860 void HTTPRepoPrivate::updatedFileContents(const SGPath& p, const std::string& newHash)
862 // remove the existing entry
863 HashCache::iterator it = std::find_if(hashes.begin(), hashes.end(), HashEntryWithPath(p.str()));
864 if (it != hashes.end()) {
866 hashCacheDirty = true;
869 if (newHash.empty()) {
870 return; // we're done
873 // use a cloned SGPath and reset its caching to force one stat() call
875 p2.set_cached(false);
878 HashCacheEntry entry;
879 entry.filePath = p.str();
880 entry.hashHex = newHash;
881 entry.modTime = p2.modTime();
882 entry.lengthBytes = p2.sizeInBytes();
883 hashes.push_back(entry);
885 hashCacheDirty = true;
888 void HTTPRepoPrivate::writeHashCache()
890 if (!hashCacheDirty) {
894 SGPath cachePath = basePath;
895 cachePath.append(".hashes");
897 std::ofstream stream(cachePath.c_str(),std::ios::out | std::ios::trunc);
898 HashCache::const_iterator it;
899 for (it = hashes.begin(); it != hashes.end(); ++it) {
900 stream << it->filePath << ":" << it->modTime << ":"
901 << it->lengthBytes << ":" << it->hashHex << "\n";
904 hashCacheDirty = false;
907 void HTTPRepoPrivate::parseHashCache()
910 SGPath cachePath = basePath;
911 cachePath.append(".hashes");
912 if (!cachePath.exists()) {
916 std::ifstream stream(cachePath.c_str(), std::ios::in);
918 while (!stream.eof()) {
920 std::getline(stream,line);
921 line = simgear::strutils::strip(line);
922 if( line.empty() || line[0] == '#' )
925 string_list tokens = simgear::strutils::split( line, ":" );
926 if( tokens.size() < 4 ) {
927 SG_LOG(SG_TERRASYNC, SG_WARN, "invalid entry in '" << cachePath.str() << "': '" << line << "' (ignoring line)");
930 const std::string nameData = simgear::strutils::strip(tokens[0]);
931 const std::string timeData = simgear::strutils::strip(tokens[1]);
932 const std::string sizeData = simgear::strutils::strip(tokens[2]);
933 const std::string hashData = simgear::strutils::strip(tokens[3]);
935 if (nameData.empty() || timeData.empty() || sizeData.empty() || hashData.empty() ) {
936 SG_LOG(SG_TERRASYNC, SG_WARN, "invalid entry in '" << cachePath.str() << "': '" << line << "' (ignoring line)");
940 HashCacheEntry entry;
941 entry.filePath = nameData;
942 entry.hashHex = hashData;
943 entry.modTime = strtol(timeData.c_str(), NULL, 10);
944 entry.lengthBytes = strtol(sizeData.c_str(), NULL, 10);
945 hashes.push_back(entry);
949 class DirectoryWithPath
952 DirectoryWithPath(const std::string& p) : path(p) {}
953 bool operator()(const HTTPDirectory* entry) const
954 { return entry->relativePath().str() == path; }
959 HTTPDirectory* HTTPRepoPrivate::getOrCreateDirectory(const std::string& path)
961 DirectoryWithPath p(path);
962 DirectoryVector::iterator it = std::find_if(directories.begin(), directories.end(), p);
963 if (it != directories.end()) {
967 HTTPDirectory* d = new HTTPDirectory(this, path);
968 directories.push_back(d);
972 bool HTTPRepoPrivate::deleteDirectory(const std::string& path)
974 DirectoryWithPath p(path);
975 DirectoryVector::iterator it = std::find_if(directories.begin(), directories.end(), p);
976 if (it != directories.end()) {
977 HTTPDirectory* d = *it;
978 directories.erase(it);
979 Dir dir(d->absolutePath());
980 bool result = dir.remove(true);
983 // update the hash cache too
984 updatedFileContents(path, std::string());
992 void HTTPRepoPrivate::makeRequest(RepoRequestPtr req)
994 if (activeRequests.size() > 4) {
995 queuedRequests.push_back(req);
997 activeRequests.push_back(req);
998 http->makeRequest(req);
1002 void HTTPRepoPrivate::finishedRequest(const RepoRequestPtr& req)
1004 RequestVector::iterator it = std::find(activeRequests.begin(), activeRequests.end(), req);
1005 if (it == activeRequests.end()) {
1006 throw sg_exception("lost request somehow", req->url());
1008 activeRequests.erase(it);
1010 if (!queuedRequests.empty()) {
1011 RepoRequestPtr rr = queuedRequests.front();
1012 queuedRequests.erase(queuedRequests.begin());
1013 activeRequests.push_back(rr);
1014 http->makeRequest(rr);
1019 if (activeRequests.empty() && queuedRequests.empty()) {
1024 void HTTPRepoPrivate::failedToGetRootIndex(AbstractRepository::ResultCode st)
1026 SG_LOG(SG_TERRASYNC, SG_WARN, "Failed to get root of repo:" << baseUrl);
1030 void HTTPRepoPrivate::failedToUpdateChild(const SGPath& relativePath,
1031 AbstractRepository::ResultCode fileStatus)
1034 f.path = relativePath;
1035 f.error = fileStatus;
1036 failures.push_back(f);
1038 SG_LOG(SG_TERRASYNC, SG_WARN, "failed to update entry:" << relativePath << " code:" << fileStatus);
1043 } // of namespace simgear