include stored passphrase in delivery Release_2.1.0-RC6
authorVolker Birk <vb@pep-project.org>
Sat, 04 Jul 2020 23:42:53 +0200
changeset 27011c85ef006f0
parent 269 54b2abbb2d7d
child 271 232f492db882
include stored passphrase in delivery
passphrase_cache.cc
passphrase_cache.hh
     1.1 --- a/passphrase_cache.cc	Sat Jul 04 22:31:01 2020 +0200
     1.2 +++ b/passphrase_cache.cc	Sat Jul 04 23:42:53 2020 +0200
     1.3 @@ -9,12 +9,12 @@
     1.4      { }
     1.5  
     1.6      PassphraseCache::PassphraseCache(size_t max_size, duration timeout) :
     1.7 -            _max_size{max_size}, _timeout{timeout}, _which{_cache.end()}
     1.8 +            _max_size{max_size}, _timeout{timeout}, first_time(true)
     1.9      { }
    1.10  
    1.11      PassphraseCache::PassphraseCache(const PassphraseCache& second) :
    1.12              _cache{second._cache}, _max_size{second._max_size},
    1.13 -            _timeout{second._timeout}, _which{_cache.end()}
    1.14 +            _timeout{second._timeout}, first_time(true)
    1.15      {
    1.16          cleanup();
    1.17      }
    1.18 @@ -31,7 +31,6 @@
    1.19  
    1.20      const char *PassphraseCache::add(const std::string& passphrase)
    1.21      {
    1.22 -        assert(_which == _cache.end()); // never modify while iterating
    1.23          std::lock_guard<std::mutex> lock(_mtx);
    1.24  
    1.25          if (!passphrase.empty()) {
    1.26 @@ -83,29 +82,32 @@
    1.27  
    1.28      void PassphraseCache::cleanup()
    1.29      {
    1.30 -        assert(_which == _cache.end()); // never modify while iterating
    1.31          while (!_cache.empty() && _cache.front().tp < clock::now() - _timeout)
    1.32              _cache.pop_front();
    1.33      }
    1.34  
    1.35      void PassphraseCache::refresh(cache::iterator entry)
    1.36      {
    1.37 -        assert(_which == _cache.end()); // never modify while iterating
    1.38          entry->tp = clock::now();
    1.39          _cache.splice(_cache.end(), _cache, entry);
    1.40      }
    1.41  
    1.42      const char *PassphraseCache::latest_passphrase()
    1.43      {
    1.44 -        std::lock_guard<std::mutex> lock(_mtx);
    1.45 -        
    1.46 +        if (first_time) {
    1.47 +            cleanup();
    1.48 +            _which = _cache.end();
    1.49 +            first_time = false;
    1.50 +            return _stored.c_str();
    1.51 +        }
    1.52 +
    1.53          if (_cache.empty()) {
    1.54 -            _which = _cache.end();
    1.55 +            first_time = true;
    1.56              throw Empty();
    1.57          }
    1.58  
    1.59          if (_which == _cache.begin()) {
    1.60 -            _which = _cache.end();
    1.61 +            first_time = true;
    1.62              throw Exhausted();
    1.63          }
    1.64  
     2.1 --- a/passphrase_cache.hh	Sat Jul 04 22:31:01 2020 +0200
     2.2 +++ b/passphrase_cache.hh	Sat Jul 04 23:42:53 2020 +0200
     2.3 @@ -31,6 +31,7 @@
     2.4          duration _timeout;
     2.5  
     2.6          cache::iterator _which;
     2.7 +        bool first_time;
     2.8  
     2.9      public:
    2.10          struct Empty : public std::underflow_error {