E-MailRelay
gpopstore.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gpopstore.h
19 ///
20 
21 #ifndef G_POP_STORE_H
22 #define G_POP_STORE_H
23 
24 #include "gdef.h"
25 #include "gpop.h"
26 #include "gpath.h"
27 #include "gexception.h"
28 #include <string>
29 #include <iostream>
30 #include <set>
31 #include <list>
32 
33 namespace GPop
34 {
35  class Store ;
36  class StoreLock ;
37  class StoreLockEntry ;
38 }
39 
40 /// \class GPop::Store
41 /// A message store. Unlike the SMTP message store the POP message
42 /// store allows content files to be in the envelope file's parent
43 /// directory.
44 ///
46 {
47 public:
48  G_EXCEPTION( InvalidDirectory , "invalid spool directory" ) ;
49 
50  Store( G::Path spool_dir , bool by_name , bool allow_delete ) ;
51  ///< Constructor.
52 
53  G::Path dir() const ;
54  ///< Returns the spool directory path.
55 
56  bool allowDelete() const ;
57  ///< Returns true if files can be deleted.
58 
59  bool byName() const ;
60  ///< Returns true if the spool directory is affected
61  ///< by the user name.
62 
63 private:
64  Store( const Store & ) ;
65  void operator=( const Store & ) ;
66  static void checkPath( G::Path , bool , bool ) ;
67  static bool valid( G::Path , bool ) ;
68 
69 private:
70  G::Path m_path ;
71  bool m_by_name ;
72  bool m_allow_delete ;
73 } ;
74 
75 /// \class GPop::StoreLockEntry
76 /// Represents a file in the GPop::Store.
77 /// \see GPop::StoreLock
78 ///
80 {
81 public:
82  int id ;
83  typedef unsigned long Size ;
84  Size size ;
85  std::string uidl ;
86  StoreLockEntry( int id_ , Size size_ , const std::string & uidl_ ) :
87  id(id_) ,
88  size(size_) ,
89  uidl(uidl_)
90  {
91  }
92 } ;
93 
94 /// \class GPop::StoreLock
95 /// Represents an exclusive lock on the message store.
96 /// \see RFC-1939
97 ///
99 {
100 public:
101  G_EXCEPTION( CannotDelete , "cannot delete message file" ) ;
102  G_EXCEPTION( CannotRead , "cannot read message file" ) ;
103  typedef StoreLockEntry::Size Size ;
104  typedef StoreLockEntry Entry ;
105  typedef std::list<Entry> List ;
106  typedef void (*Fn)(std::ostream&,const std::string&) ;
107 
108  explicit StoreLock( Store & store ) ;
109  ///< Constructor. Keeps the reference.
110  ///<
111  ///< Postcondition: !locked()
112 
113  void lock( const std::string & user ) ;
114  ///< Initialisation.
115  ///<
116  ///< Precondition: !user.empty() && !locked()
117  ///< Postcondition: locked()
118 
119  bool locked() const ;
120  ///< Returns true if locked.
121 
122  ~StoreLock() ;
123  ///< Destructor.
124 
125  Size messageCount() const ;
126  ///< Returns the store's message count.
127 
128  Size totalByteCount() const ;
129  ///< Returns the store's total byte count.
130 
131  Size byteCount( int id ) const ;
132  ///< Returns a message size.
133 
134  std::string uidl( int id ) const ;
135  ///< Returns a message's unique id.
136 
137  bool valid( int id ) const ;
138  ///< Validates a message number.
139 
140  List list( int id = -1 ) const ;
141  ///< Lists messages in the store.
142 
143  unique_ptr<std::istream> get( int id ) const ;
144  ///< Retrieves the message content.
145 
146  void remove( int ) ;
147  ///< Marks the message for removal.
148 
149  void rollback() ;
150  ///< Rolls back remove()als but retains the lock.
151  ///<
152  ///< Precondition: locked()
153  ///< Postcondition: locked() [sic]
154 
155  void commit() ;
156  ///< Commits remove()als.
157  ///< Postcondition: !locked()
158 
159 private:
160  struct File /// A private implementation class used by GPop::StoreLock.
161  {
162  std::string name ; // content name
163  StoreLockEntry::Size size ;
164  explicit File( const G::Path & ) ;
165  File( const std::string & name_ , const std::string & size_string ) ;
166  bool operator<( const File & ) const ;
167  static StoreLockEntry::Size toSize( const std::string & s ) ;
168  } ;
169  typedef std::set<File> Set ;
170 
171 private:
172  StoreLock( const StoreLock & ) ; // not implemented
173  void operator=( const StoreLock & ) ; // not implemented
174  Set::iterator find( const std::string & ) ;
175  Set::const_iterator find( int id ) const ;
176  Set::iterator find( int id ) ;
177  void doCommit( Store & ) const ;
178  G::Path path( int id ) const ;
179  G::Path path( const std::string & , bool fallback ) const ;
180  std::string envelopeName( const std::string & ) const ;
181  std::string contentName( const std::string & ) const ;
182  G::Path contentPath( const std::string & ) const ;
183  G::Path contentPath( const File & ) const ;
184  G::Path envelopePath( const File & ) const ;
185  void deleteFile( const G::Path & , bool & ) const ;
186  bool unlinked( Store & , const File & ) const ;
187 
188 private:
189  Store * m_store ;
190  G::Path m_dir ;
191  std::string m_user ;
192  Set m_initial ;
193  Set m_current ;
194  Set m_deleted ;
195 } ;
196 
197 #endif
G::Path dir() const
Returns the spool directory path.
Definition: gpopstore.cpp:77
void rollback()
Rolls back remove()als but retains the lock.
Definition: gpopstore.cpp:437
bool locked() const
Returns true if locked.
Definition: gpopstore.cpp:224
Store(G::Path spool_dir, bool by_name, bool allow_delete)
Constructor.
Size messageCount() const
Returns the store's message count.
Definition: gpopstore.cpp:233
Represents a file in the GPop::Store.
Definition: gpopstore.h:79
bool allowDelete() const
Returns true if files can be deleted.
Definition: gpopstore.cpp:82
A message store.
Definition: gpopstore.h:45
Size totalByteCount() const
Returns the store's total byte count.
Definition: gpopstore.cpp:239
Represents an exclusive lock on the message store.
Definition: gpopstore.h:98
~StoreLock()
Destructor.
Definition: gpopstore.cpp:229
void lock(const std::string &user)
Initialisation.
Definition: gpopstore.cpp:183
Size byteCount(int id) const
Returns a message size.
Definition: gpopstore.cpp:281
void commit()
Commits remove()als.
Definition: gpopstore.cpp:333
bool byName() const
Returns true if the spool directory is affected by the user name.
Definition: gpopstore.cpp:87
bool valid(int id) const
Validates a message number.
Definition: gpopstore.cpp:248
POP3 classes.
Definition: gpopauth.h:28
StoreLock(Store &store)
Constructor.
Definition: gpopstore.cpp:178
A Path object represents a file system path.
Definition: gpath.h:72
List list(int id=-1) const
Lists messages in the store.
Definition: gpopstore.cpp:287
std::string uidl(int id) const
Returns a message's unique id.
Definition: gpopstore.cpp:377