E-MailRelay
gpopstore.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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 "gpath.h"
26#include "gexception.h"
27#include <string>
28#include <iostream>
29#include <set>
30#include <list>
31
32namespace GPop
33{
34 class Store ;
35 class StoreLock ;
36 class StoreLockEntry ;
37}
38
39//| \class GPop::Store
40/// A message store. Unlike the SMTP message store the POP message
41/// store allows content files to be in the envelope file's parent
42/// directory.
43///
45{
46public:
47 G_EXCEPTION( InvalidDirectory , "invalid spool directory" ) ;
48
49 Store( const G::Path & spool_dir , bool by_name , bool allow_delete ) ;
50 ///< Constructor.
51
52 G::Path dir() const ;
53 ///< Returns the spool directory path.
54
55 bool allowDelete() const ;
56 ///< Returns true if files can be deleted.
57
58 bool byName() const ;
59 ///< Returns true if the spool directory is affected
60 ///< by the user name.
61
62public:
63 ~Store() = default ;
64 Store( const Store & ) = delete ;
65 Store( Store && ) = delete ;
66 void operator=( const Store & ) = delete ;
67 void operator=( Store && ) = delete ;
68
69private:
70 static void checkPath( const G::Path & , bool , bool ) ;
71 static bool valid( const G::Path & , bool ) ;
72
73private:
74 G::Path m_path ;
75 bool m_by_name ;
76 bool m_allow_delete ;
77} ;
78
79//| \class GPop::StoreLockEntry
80/// Represents a file in the GPop::Store.
81/// \see GPop::StoreLock
82///
84{
85public:
86 int id ;
87 using Size = unsigned long ;
88 Size size ;
89 std::string uidl ;
90 StoreLockEntry( int id_ , Size size_ , const std::string & uidl_ ) :
91 id(id_) ,
92 size(size_) ,
93 uidl(uidl_)
94 {
95 }
96} ;
97
98//| \class GPop::StoreLock
99/// Represents an exclusive lock on the message store.
100/// \see RFC-1939
101///
103{
104public:
105 G_EXCEPTION( CannotDelete , "cannot delete message file" ) ;
106 G_EXCEPTION( CannotRead , "cannot read message file" ) ;
107 using Size = StoreLockEntry::Size ;
108 using Entry = StoreLockEntry ;
109 using List = std::list<Entry> ;
110 using Fn = void (*)(std::ostream &, const std::string &) ;
111
112 explicit StoreLock( Store & store ) ;
113 ///< Constructor. Keeps the reference.
114 ///<
115 ///< Postcondition: !locked()
116
117 void lock( const std::string & user ) ;
118 ///< Initialisation.
119 ///<
120 ///< Precondition: !user.empty() && !locked()
121 ///< Postcondition: locked()
122
123 bool locked() const ;
124 ///< Returns true if locked.
125
127 ///< Destructor.
128
129 Size messageCount() const ;
130 ///< Returns the store's message count.
131
132 Size totalByteCount() const ;
133 ///< Returns the store's total byte count.
134
135 Size byteCount( int id ) const ;
136 ///< Returns a message size.
137
138 std::string uidl( int id ) const ;
139 ///< Returns a message's unique id.
140
141 bool valid( int id ) const ;
142 ///< Validates a message number.
143
144 List list( int id = -1 ) const ;
145 ///< Lists messages in the store.
146
147 std::unique_ptr<std::istream> get( int id ) const ;
148 ///< Retrieves the message content.
149
150 void remove( int ) ;
151 ///< Marks the message for removal.
152
153 void rollback() ;
154 ///< Rolls back remove()als but retains the lock.
155 ///<
156 ///< Precondition: locked()
157 ///< Postcondition: locked() [sic]
158
159 void commit() ;
160 ///< Commits remove()als.
161 ///< Postcondition: !locked()
162
163private:
164 struct File /// A private implementation class used by GPop::StoreLock.
165 {
166 std::string name ; // content name
167 StoreLockEntry::Size size ;
168 explicit File( const G::Path & ) ;
169 File( const std::string & name_ , const std::string & size_string ) ;
170 bool operator<( const File & ) const ;
171 static StoreLockEntry::Size toSize( const std::string & s ) ;
172 } ;
173 using Set = std::set<File> ;
174
175public:
176 StoreLock( const StoreLock & ) = delete ;
177 StoreLock( StoreLock && ) = delete ;
178 void operator=( const StoreLock & ) = delete ;
179 void operator=( StoreLock && ) = delete ;
180
181private:
182 Set::iterator find( const std::string & ) ;
183 Set::const_iterator find( int id ) const ;
184 Set::iterator find( int id ) ;
185 void doCommit( Store & ) const ;
186 G::Path path( int id ) const ;
187 G::Path path( const std::string & , bool fallback ) const ;
188 std::string envelopeName( const std::string & ) const ;
189 std::string contentName( const std::string & ) const ;
190 G::Path contentPath( const std::string & ) const ;
191 G::Path contentPath( const File & ) const ;
192 G::Path envelopePath( const File & ) const ;
193 void deleteFile( const G::Path & , bool & ) const ;
194 bool unlinked( Store & , const File & ) const ;
195
196private:
197 Store * m_store ;
198 G::Path m_dir ;
199 std::string m_user ;
200 Set m_initial ;
201 Set m_current ;
202 Set m_deleted ;
203} ;
204
205#endif
Represents a file in the GPop::Store.
Definition: gpopstore.h:84
Represents an exclusive lock on the message store.
Definition: gpopstore.h:103
Size byteCount(int id) const
Returns a message size.
Definition: gpopstore.cpp:279
bool valid(int id) const
Validates a message number.
Definition: gpopstore.cpp:246
std::string uidl(int id) const
Returns a message's unique id.
Definition: gpopstore.cpp:376
std::unique_ptr< std::istream > get(int id) const
Retrieves the message content.
Definition: gpopstore.cpp:298
List list(int id=-1) const
Lists messages in the store.
Definition: gpopstore.cpp:285
void rollback()
Rolls back remove()als but retains the lock.
Definition: gpopstore.cpp:436
void commit()
Commits remove()als.
Definition: gpopstore.cpp:332
~StoreLock()
Destructor.
void lock(const std::string &user)
Initialisation.
Definition: gpopstore.cpp:182
StoreLock(Store &store)
Constructor.
Definition: gpopstore.cpp:177
Size messageCount() const
Returns the store's message count.
Definition: gpopstore.cpp:231
void remove(int)
Marks the message for removal.
Definition: gpopstore.cpp:318
Size totalByteCount() const
Returns the store's total byte count.
Definition: gpopstore.cpp:237
bool locked() const
Returns true if locked.
Definition: gpopstore.cpp:223
A message store.
Definition: gpopstore.h:45
bool allowDelete() const
Returns true if files can be deleted.
Definition: gpopstore.cpp:81
bool byName() const
Returns true if the spool directory is affected by the user name.
Definition: gpopstore.cpp:86
G::Path dir() const
Returns the spool directory path.
Definition: gpopstore.cpp:76
Store(const G::Path &spool_dir, bool by_name, bool allow_delete)
Constructor.
Definition: gpopstore.cpp:68
A Path object represents a file system path.
Definition: gpath.h:72
POP3 classes.
Definition: gpopserver.h:37