E-MailRelay
gfilestore.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 gfilestore.h
19///
20
21#ifndef G_SMTP_FILE_STORE_H
22#define G_SMTP_FILE_STORE_H
23
24#include "gdef.h"
25#include "gmessagestore.h"
26#include "gdatetime.h"
27#include "gexception.h"
28#include "gprocess.h"
29#include "gslot.h"
30#include "groot.h"
31#include "gpath.h"
32#include <fstream>
33#include <memory>
34#include <string>
35
36namespace GSmtp
37{
38 class FileStore ;
39 class FileReader ;
40 class FileWriter ;
41 class DirectoryReader ;
42}
43
44//| \class GSmtp::FileStore
45/// A concrete implementation of the MessageStore interface dealing
46/// in paired flat files.
47///
48/// The implementation puts separate envelope and content files
49/// in the spool directory. The content file is written first.
50/// The presence of a matching envelope file is used to indicate
51/// that the content file is valid and that it has been commited
52/// to the care of the SMTP system for delivery.
53///
54/// Passes out unique sequence numbers, filesystem paths and
55/// i/o streams to NewMessageImp.
56///
58{
59public:
60 G_EXCEPTION( InvalidDirectory , "invalid spool directory" ) ;
61 G_EXCEPTION( GetError , "error reading specific message" ) ;
62
63 FileStore( const G::Path & dir , unsigned long max_size , bool test_for_eight_bit ) ;
64 ///< Constructor. Throws an exception if the storage directory
65 ///< is invalid.
66
68 ///< Hands out a new message id.
69
70 std::unique_ptr<std::ofstream> stream( const G::Path & path ) ;
71 ///< Returns a stream to the given content.
72
73 G::Path contentPath( const MessageId & ) const ;
74 ///< Returns the path for a content file.
75
76 G::Path envelopePath( const MessageId & , const char * modifier = "" ) const ;
77 ///< Returns the path for an envelope file.
78
79 bool empty() const override ;
80 ///< Override from GSmtp::MessageStore.
81
82 std::string location( const MessageId & ) const override ;
83 ///< Override from GSmtp::MessageStore.
84
85 std::unique_ptr<StoredMessage> get( const MessageId & ) override ;
86 ///< Override from GSmtp::MessageStore.
87
88 std::shared_ptr<MessageStore::Iterator> iterator( bool lock ) override ;
89 ///< Override from GSmtp::MessageStore.
90
91 std::shared_ptr<MessageStore::Iterator> failures() override ;
92 ///< Override from GSmtp::MessageStore.
93
94 std::unique_ptr<NewMessage> newMessage( const std::string & from ,
95 const std::string & from_auth_in , const std::string & from_auth_out ) override ;
96 ///< Override from GSmtp::MessageStore.
97
98 static std::string x() ;
99 ///< Returns the prefix for envelope header lines.
100
101 static std::string format( int generation = 0 ) ;
102 ///< Returns an identifier for the storage format implemented
103 ///< by this class, or some older generation of it (eg. -1).
104
105 static bool knownFormat( const std::string & format ) ;
106 ///< Returns true if the storage format string is
107 ///< recognised and supported for reading.
108
109 void updated() override ;
110 ///< Override from GSmtp::MessageStore.
111
113 ///< Override from GSmtp::MessageStore.
114
116 ///< Override from GSmtp::MessageStore.
117
118private: // overrides
119 void rescan() override ; // Override from GSmtp::MessageStore.
120 void unfailAll() override ; // Override from GSmtp::MessageStore.
121
122public:
123 ~FileStore() override = default ;
124 FileStore( const FileStore & ) = delete ;
125 FileStore( FileStore && ) = delete ;
126 void operator=( const FileStore & ) = delete ;
127 void operator=( FileStore && ) = delete ;
128
129private:
130 static void checkPath( const G::Path & dir ) ;
131 G::Path fullPath( const std::string & filename ) const ;
132 std::string filePrefix( unsigned long seq ) const ;
133 std::string getline( std::istream & ) const ;
134 std::string value( const std::string & ) const ;
135 std::shared_ptr<MessageStore::Iterator> iteratorImp( bool ) ;
136 void unfailAllImp() ;
137 static const std::string & crlf() ;
138 bool emptyCore() const ;
139 void clearAll() ;
140
141private:
142 unsigned long m_seq ;
143 G::Path m_dir ;
144 unsigned long m_max_size ;
145 bool m_test_for_eight_bit ;
146 unsigned long m_pid_modifier ;
147 G::Slot::Signal<> m_update_signal ;
148 G::Slot::Signal<> m_rescan_signal ;
149} ;
150
151//| \class GSmtp::FileReader
152/// Used by GSmtp::FileStore, GSmtp::NewFile and GSmtp::StoredFile to
153/// claim read permissions for reading a file.
154/// \see G::Root
155///
157{
158public:
160 ///< Default constructor. Switches identity for
161 ///< reading a file.
162
164 ///< Destructor. Switches identity back.
165
166public:
167 FileReader( const FileReader & ) = delete ;
168 FileReader( FileReader && ) = delete ;
169 void operator=( const FileReader & ) = delete ;
170 void operator=( FileReader && ) = delete ;
171} ;
172
173//| \class GSmtp::DirectoryReader
174/// Used by GSmtp::FileStore, GSmtp::NewFile and GSmtp::StoredFile to
175/// claim read permissions for reading a directory.
176/// \see G::Root
177///
179{
180public:
182 ///< Default constructor. Switches identity for
183 ///< reading a directory.
184
186 ///< Destructor. Switches identity back.
187
188public:
189 DirectoryReader( const DirectoryReader & ) = delete ;
190 DirectoryReader( DirectoryReader && ) = delete ;
191 void operator=( const DirectoryReader & ) = delete ;
192 void operator=( DirectoryReader && ) = delete ;
193} ;
194
195//| \class GSmtp::FileWriter
196/// Used by GSmtp::FileStore, GSmtp::NewFile and GSmtp::StoredFile to
197/// claim write permissions.
198/// \see G::Root
199///
201{
202public:
203 FileWriter() ;
204 ///< Default constructor. Switches identity for
205 ///< writing a file.
206
208 ///< Destructor. Switches identity back.
209
210public:
211 FileWriter( const FileWriter & ) = delete ;
212 FileWriter( FileWriter && ) = delete ;
213 void operator=( const FileWriter & ) = delete ;
214 void operator=( FileWriter && ) = delete ;
215} ;
216
217#endif
Used by GSmtp::FileStore, GSmtp::NewFile and GSmtp::StoredFile to claim read permissions for reading ...
Definition: gfilestore.h:179
~DirectoryReader()
Destructor. Switches identity back.
DirectoryReader()
Default constructor.
Used by GSmtp::FileStore, GSmtp::NewFile and GSmtp::StoredFile to claim read permissions for reading ...
Definition: gfilestore.h:157
FileReader()
Default constructor.
~FileReader()
Destructor. Switches identity back.
A concrete implementation of the MessageStore interface dealing in paired flat files.
Definition: gfilestore.h:58
G::Slot::Signal & messageStoreRescanSignal() override
Override from GSmtp::MessageStore.
Definition: gfilestore.cpp:285
FileStore(const G::Path &dir, unsigned long max_size, bool test_for_eight_bit)
Constructor.
Definition: gfilestore.cpp:107
std::unique_ptr< StoredMessage > get(const MessageId &) override
Override from GSmtp::MessageStore.
Definition: gfilestore.cpp:248
std::shared_ptr< MessageStore::Iterator > failures() override
Override from GSmtp::MessageStore.
Definition: gfilestore.cpp:243
static bool knownFormat(const std::string &format)
Returns true if the storage format string is recognised and supported for reading.
Definition: gfilestore.cpp:145
void updated() override
Override from GSmtp::MessageStore.
Definition: gfilestore.cpp:274
std::unique_ptr< std::ofstream > stream(const G::Path &path)
Returns a stream to the given content.
Definition: gfilestore.cpp:190
G::Path contentPath(const MessageId &) const
Returns the path for a content file.
Definition: gfilestore.cpp:200
std::shared_ptr< MessageStore::Iterator > iterator(bool lock) override
Override from GSmtp::MessageStore.
Definition: gfilestore.cpp:233
std::unique_ptr< NewMessage > newMessage(const std::string &from, const std::string &from_auth_in, const std::string &from_auth_out) override
Override from GSmtp::MessageStore.
Definition: gfilestore.cpp:267
static std::string x()
Returns the prefix for envelope header lines.
Definition: gfilestore.cpp:129
G::Path envelopePath(const MessageId &, const char *modifier="") const
Returns the path for an envelope file.
Definition: gfilestore.cpp:205
static std::string format(int generation=0)
Returns an identifier for the storage format implemented by this class, or some older generation of i...
Definition: gfilestore.cpp:134
bool empty() const override
Override from GSmtp::MessageStore.
Definition: gfilestore.cpp:224
std::string location(const MessageId &) const override
Override from GSmtp::MessageStore.
Definition: gfilestore.cpp:185
G::Slot::Signal & messageStoreUpdateSignal() override
Override from GSmtp::MessageStore.
Definition: gfilestore.cpp:280
MessageId newId()
Hands out a new message id.
Definition: gfilestore.cpp:211
Used by GSmtp::FileStore, GSmtp::NewFile and GSmtp::StoredFile to claim write permissions.
Definition: gfilestore.h:201
~FileWriter()
Destructor. Switches identity back.
FileWriter()
Default constructor.
Definition: gfilestore.cpp:345
A somewhat opaque identifer for a MessageStore message.
Definition: gmessagestore.h:43
A class which allows SMTP messages to be stored and retrieved.
Definition: gmessagestore.h:73
A Path object represents a file system path.
Definition: gpath.h:72
Used to temporarily modify the process umask.
Definition: gprocess.h:158
A class which acquires the process's special privileges on construction and releases them on destruct...
Definition: groot.h:52
SMTP and message-store classes.
Definition: gadminserver.h:39
A slot holder, with connect() and emit() methods.
Definition: gslot.h:157