39 std::size_t max_size ) :
45 m_env.from_auth_in = smtp_info.auth ;
46 m_env.from_auth_out = from_auth_out ;
48 m_env.utf8_mailboxes =
49 smtp_info.address_style == MessageStore::AddressStyle::Utf8Mailbox ||
50 smtp_info.address_style == MessageStore::AddressStyle::Utf8Both ;
53 G_LOG(
"GStore::NewFile: new content file [" << cpath().basename() <<
"]" ) ;
68void GStore::NewFile::cleanup()
73 G_DEBUG(
"GStore::NewFile::cleanup: deleting envelope [" << epath(State::New).basename() <<
"]" ) ;
74 FileOp::remove( epath(State::New) ) ;
76 G_DEBUG(
"GStore::NewFile::cleanup: deleting content [" << cpath().basename() <<
"]" ) ;
77 FileOp::remove( cpath() ) ;
83void GStore::NewFile::prepare(
const std::string & session_auth_id ,
84 const std::string & peer_socket_address ,
const std::string & peer_certificate )
87 G_ASSERT( m_content !=
nullptr ) ;
89 if( m_content->fail() )
90 throw FileError(
"cannot write content file " + cpath().str() ) ;
94 m_env.authentication = session_auth_id ;
95 m_env.client_socket_address = peer_socket_address ;
96 m_env.client_certificate = peer_certificate ;
97 saveEnvelope( m_env , epath(State::New) ) ;
99 static_cast<MessageStore&
>(m_store).updated() ;
102void GStore::NewFile::commit(
bool throw_on_error )
105 m_saved = FileOp::rename( epath(State::New) , epath(State::Normal) ) ;
106 if( !m_saved && throw_on_error )
107 throw FileError(
"cannot rename envelope file to " + epath(State::Normal).str() ) ;
108 static_cast<MessageStore&
>(m_store).updated() ;
111void GStore::NewFile::addTo(
const std::string & to ,
bool local , MessageStore::AddressStyle address_style )
115 m_env.to_local.push_back( to ) ;
119 m_env.to_remote.push_back( to ) ;
120 if( address_style == MessageStore::AddressStyle::Utf8Mailbox ||
121 address_style == MessageStore::AddressStyle::Utf8Both )
123 m_env.utf8_mailboxes = true ;
128GStore::NewMessage::Status GStore::NewFile::addContent(
const char * data , std::size_t data_size )
130 std::size_t old_size = m_size ;
131 std::size_t new_size = m_size + data_size ;
132 if( new_size < m_size )
133 new_size = std::numeric_limits<std::size_t>::max() ;
138 if( m_max_size && new_size >= m_max_size )
139 data_size = std::max(m_max_size,old_size) - old_size ;
143 std::ostream & stream = *m_content ;
144 stream.write( data , data_size ) ;
147 if( m_content->fail() )
148 return NewMessage::Status::Error ;
149 else if( m_max_size && m_size >= m_max_size )
150 return NewMessage::Status::TooBig ;
152 return NewMessage::Status::Ok ;
155std::size_t GStore::NewFile::contentSize()
const
161void GStore::NewFile::saveEnvelope( Envelope & env ,
const G::Path & path )
163 G_LOG(
"GStore::NewFile: new envelope file [" << path.
basename() <<
"]" ) ;
167 envelope_stream->close() ;
168 if( envelope_stream->fail() )
169 throw FileError(
"cannot write envelope file" , path.
str() ) ;
177std::string GStore::NewFile::location()
const
179 return cpath().str() ;
182G::Path GStore::NewFile::cpath()
const
184 return m_store.contentPath( m_id ) ;
187G::Path GStore::NewFile::epath( State state )
const
189 return state == State::Normal ?
190 m_store.envelopePath(m_id) :
191 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.