39 std::size_t max_size ) :
48 m_env.from_auth_in = smtp_info.auth ;
49 m_env.from_auth_out = from_auth_out ;
51 m_env.utf8_mailboxes = smtp_info.utf8address ;
54 G_LOG(
"GStore::NewFile: new content file [" << cpath().basename() <<
"]" ) ;
69void GStore::NewFile::cleanup()
74 G_DEBUG(
"GStore::NewFile::cleanup: deleting envelope [" << epath(State::New).basename() <<
"]" ) ;
75 FileOp::remove( epath(State::New) ) ;
77 G_DEBUG(
"GStore::NewFile::cleanup: deleting content [" << cpath().basename() <<
"]" ) ;
78 FileOp::remove( cpath() ) ;
84void GStore::NewFile::prepare(
const std::string & session_auth_id ,
85 const std::string & peer_socket_address ,
const std::string & peer_certificate )
88 G_ASSERT( m_content !=
nullptr ) ;
90 if( m_content->fail() )
91 throw FileError(
"cannot write content file " + cpath().str() ) ;
95 m_env.authentication = session_auth_id ;
96 m_env.client_socket_address = peer_socket_address ;
97 m_env.client_certificate = peer_certificate ;
98 saveEnvelope( m_env , epath(State::New) ) ;
100 static_cast<MessageStore&
>(m_store).updated() ;
103void GStore::NewFile::commit(
bool throw_on_error )
106 m_saved = FileOp::rename( epath(State::New) , epath(State::Normal) ) ;
107 if( !m_saved && throw_on_error )
108 throw FileError(
"cannot rename envelope file to " + epath(State::Normal).str() ) ;
109 static_cast<MessageStore&
>(m_store).updated() ;
112void GStore::NewFile::addTo(
const std::string & to ,
bool local ,
bool utf8address )
116 m_env.to_local.push_back( to ) ;
120 m_env.to_remote.push_back( to ) ;
122 m_env.utf8_mailboxes = true ;
126GStore::NewMessage::Status GStore::NewFile::addContent(
const char * data , std::size_t data_size )
128 std::size_t old_size = m_size ;
129 std::size_t new_size = m_size + data_size ;
130 if( new_size < m_size )
131 new_size = std::numeric_limits<std::size_t>::max() ;
136 if( m_max_size && new_size >= m_max_size )
137 data_size = std::max(m_max_size,old_size) - old_size ;
141 std::ostream & stream = *m_content ;
142 stream.write( data , data_size ) ;
145 if( m_content->fail() )
146 return NewMessage::Status::Error ;
147 else if( m_max_size && m_size >= m_max_size )
148 return NewMessage::Status::TooBig ;
150 return NewMessage::Status::Ok ;
153std::size_t GStore::NewFile::contentSize()
const
159void GStore::NewFile::saveEnvelope( Envelope & env ,
const G::Path & path )
161 G_LOG(
"GStore::NewFile: new envelope file [" << path.
basename() <<
"]" ) ;
165 envelope_stream->close() ;
166 if( envelope_stream->fail() )
167 throw FileError(
"cannot write envelope file" , path.
str() ) ;
175std::string GStore::NewFile::location()
const
177 return cpath().str() ;
180G::Path GStore::NewFile::cpath()
const
182 return m_store.contentPath( m_id ) ;
185G::Path GStore::NewFile::epath( State state )
const
187 return state == State::Normal ?
188 m_store.envelopePath(m_id) :
189 G::Path( m_store.envelopePath(m_id).str() +
".new" ) ;
static std::size_t write(std::ostream &, const Envelope &)
Writes an envelope to a seekable stream.
static MessageStore::BodyType parseSmtpBodyType(const std::string &, MessageStore::BodyType default_=MessageStore::BodyType::Unknown)
Parses an SMTP MAIL-FROM BODY= parameter.
A concrete implementation of the MessageStore interface dealing in paired flat files.
static std::unique_ptr< std::ofstream > stream(const G::Path &path)
Opens an output stream to a message file using the appropriate effective userid and umask.
A somewhat opaque identifer for a GStore::MessageStore message id.
A class which allows SMTP messages to be stored and retrieved.
NewFile(FileStore &store, const std::string &from, const MessageStore::SmtpInfo &, const std::string &from_auth_out, std::size_t max_size)
Constructor.
~NewFile() override
Destructor.
A Path object represents a file system path.
std::string basename() const
Returns the rightmost part of the path, ignoring "." parts.
std::string str() const
Returns the path string.
Information on the SMTP options used when submitted.