Skip to content

Commit 468c9b5

Browse files
committed
mt2exm: move functions from genimport.cpp to mt2exm.cpp
Extract functions only used by mt2exm from genimport.cpp and move them to mt2exm.cpp. This lightens the size of other utilities like eml2mt a bit.
1 parent 4941ba5 commit 468c9b5

File tree

4 files changed

+190
-191
lines changed

4 files changed

+190
-191
lines changed

Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ gromox_mkpublic_LDADD = ${fmt_LIBS} ${libHX_LIBS} ${mysql_LIBS} ${libssl_LIBS} $
222222
gromox_kdb2mt_SOURCES = tools/genimport.cpp tools/genimport.hpp tools/kdb2mt.cpp
223223
gromox_kdb2mt_LDADD = ${fmt_LIBS} ${libHX_LIBS} ${jsoncpp_LIBS} ${mysql_LIBS} ${libpff_LIBS} ${zlib_LIBS} libgromox_common.la libgromox_exrpc.la libgromox_mapi.la
224224
gromox_mt2exm_SOURCES = tools/genimport.cpp tools/genimport.hpp tools/mt2exm.cpp
225-
gromox_mt2exm_LDADD = ${libHX_LIBS} ${mysql_LIBS} libgromox_common.la libgromox_exrpc.la libgromox_mapi.la libgxs_mysql_adaptor.la libgxs_ruleproc.la
225+
gromox_mt2exm_LDADD = ${fmt_LIBS} ${libHX_LIBS} ${mysql_LIBS} libgromox_common.la libgromox_exrpc.la libgromox_mapi.la libgxs_mysql_adaptor.la libgxs_ruleproc.la
226226
gromox_oxm2mt_SOURCES = tools/genimport.cpp tools/genimport.hpp tools/oxm2mt.cpp
227227
gromox_oxm2mt_LDADD = ${libHX_LIBS} ${fmt_LIBS} ${iconv_LIBS} ${mysql_LIBS} ${libolecf_LIBS} libgromox_common.la libgromox_exrpc.la libgromox_mapi.la
228228
gromox_pff2mt_SOURCES = tools/genimport.cpp tools/genimport.hpp tools/pff2mt.cpp

tools/genimport.cpp

Lines changed: 1 addition & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
#include <unordered_map>
1919
#include <utility>
2020
#include <vector>
21-
#include <fmt/format.h>
22-
#include <json/value.h>
2321
#include <libHX/endian.h>
2422
#include <libHX/io.h>
2523
#include <libHX/scope.hpp>
@@ -29,7 +27,6 @@
2927
#include <gromox/exmdb_rpc.hpp>
3028
#include <gromox/ext_buffer.hpp>
3129
#include <gromox/fileio.h>
32-
#include <gromox/json.hpp>
3330
#include <gromox/list_file.hpp>
3431
#include <gromox/mapidefs.h>
3532
#include <gromox/paths.h>
@@ -51,7 +48,7 @@ const char *g_storedir;
5148
unsigned int g_user_id, g_wet_run = 1;
5249
unsigned int g_public_folder, g_verbose_create;
5350
static thread_local alloc_context g_alloc_mgr;
54-
static ec_error_t (*exmdb_local_rules_execute)(const char *, const char *, const char *, eid_t, eid_t, unsigned int);
51+
ec_error_t (*exmdb_local_rules_execute)(const char *, const char *, const char *, eid_t, eid_t, unsigned int);
5552

5653
YError::YError(const std::string &s) : m_str(s)
5754
{}
@@ -240,66 +237,6 @@ int exm_set_change_keys(TPROPVAL_ARRAY *props, eid_t change_num,
240237
return 0;
241238
}
242239

243-
/**
244-
* @o_excl: Enforce that we are the first to create the folder, just like
245-
* open(2)'s %O_EXCL flag.
246-
*/
247-
int exm_create_folder(uint64_t parent_fld, TPROPVAL_ARRAY *props, bool o_excl,
248-
uint64_t *new_fld_id)
249-
{
250-
uint64_t change_num = 0;
251-
if (!exmdb_client->allocate_cn(g_storedir, &change_num)) {
252-
fprintf(stderr, "exm: allocate_cn(fld) RPC failed\n");
253-
return -EIO;
254-
}
255-
if (!props->has(PR_LAST_MODIFICATION_TIME)) {
256-
auto last_time = rop_util_current_nttime();
257-
auto ret = props->set(PR_LAST_MODIFICATION_TIME, &last_time);
258-
if (ret == ecServerOOM)
259-
return -ENOMEM;
260-
else if (ret != ecSuccess)
261-
return -EIO;
262-
}
263-
auto err = props->set(PidTagParentFolderId, &parent_fld);
264-
if (err == ecServerOOM)
265-
return -ENOMEM;
266-
else if (err != ecSuccess)
267-
return -EIO;
268-
auto ret = exm_set_change_keys(props, change_num);
269-
if (ret != 0) {
270-
fprintf(stderr, "exm: tpropval: %s\n", strerror(-ret));
271-
return ret;
272-
}
273-
auto dn = props->get<const char>(PR_DISPLAY_NAME);
274-
if (!o_excl && dn != nullptr) {
275-
if (!exmdb_client->get_folder_by_name(g_storedir,
276-
parent_fld, dn, new_fld_id)) {
277-
fprintf(stderr, "exm: get_folder_by_name \"%s\" RPC/network failed\n", dn);
278-
return -EIO;
279-
}
280-
if (*new_fld_id != 0)
281-
return 0;
282-
}
283-
if (dn == nullptr)
284-
dn = "";
285-
if (!exmdb_client->create_folder(g_storedir, CP_ACP, props, new_fld_id, &err)) {
286-
fprintf(stderr, "exm: create_folder_by_properties \"%s\" RPC failed\n", dn);
287-
return -EIO;
288-
} else if (err != ecSuccess) {
289-
fprintf(stderr, "exm: create_folder_by_properties \"%s\" RPC failed: %s\n",
290-
dn, mapi_strerror(err));
291-
return -EIO;
292-
} else if (*new_fld_id == 0) {
293-
fprintf(stderr, "exm: Could not create folder \"%s\". "
294-
"Either it already existed or some there was some other unspecified problem.\n", dn);
295-
return -EEXIST;
296-
} else if (g_verbose_create) {
297-
fprintf(stderr, "exm: Created folder \"%s\" (fid=0x%llx)\n", dn,
298-
LLU{rop_util_get_gc_value(*new_fld_id)});
299-
}
300-
return 0;
301-
}
302-
303240
int exm_permissions(eid_t fid, const std::vector<PERMISSION_DATA> &perms)
304241
{
305242
if (perms.size() == 0)
@@ -312,129 +249,6 @@ int exm_permissions(eid_t fid, const std::vector<PERMISSION_DATA> &perms)
312249
return 0;
313250
}
314251

315-
int exm_deliver_msg(const char *target, MESSAGE_CONTENT *ct,
316-
const std::string &im_repr, Json::Value &&digest, unsigned int mode)
317-
{
318-
auto ts = rop_util_current_nttime();
319-
auto ret = ct->proplist.set(PR_MESSAGE_DELIVERY_TIME, &ts);
320-
if (ret != ecSuccess)
321-
return ece2nerrno(ret);
322-
uint64_t folder_id = 0, msg_id = 0;
323-
uint32_t r32 = 0;
324-
if (mode & DELIVERY_TWOSTEP)
325-
mode &= ~(DELIVERY_DO_RULES | DELIVERY_DO_NOTIF);
326-
uint64_t change_num = 0;
327-
if (!exmdb_client->allocate_cn(g_storedir, &change_num)) {
328-
fprintf(stderr, "exm: allocate_cn(msg)[delivery] RPC failed\n");
329-
return -EIO;
330-
}
331-
auto iret = exm_set_change_keys(&ct->proplist, change_num);
332-
if (iret != 0) {
333-
fprintf(stderr, "exm: tpropval: %s\n", strerror(-iret));
334-
return iret;
335-
}
336-
auto midstr = fmt::format("{}.cn{}", time(nullptr), rop_util_get_gc_value(change_num));
337-
digest["file"] = midstr;
338-
if (!exmdb_client->imapfile_write(g_storedir, "eml",
339-
midstr.c_str(), im_repr.c_str())) {
340-
fprintf(stderr, "exm: imapfile_write RPC failed\n");
341-
return -EIO;
342-
}
343-
auto djson = json_to_str(digest);
344-
if (!exmdb_client->deliver_message(g_storedir, ENVELOPE_FROM_NULL,
345-
target, CP_ACP, mode, ct, djson.c_str(), &folder_id, &msg_id, &r32)) {
346-
fprintf(stderr, "exm: deliver_message RPC failed: code %u\n",
347-
r32);
348-
return -EIO;
349-
}
350-
351-
auto dm_status = static_cast<deliver_message_result>(r32);
352-
switch (dm_status) {
353-
case deliver_message_result::result_ok:
354-
if (g_verbose_create)
355-
fprintf(stderr, "Created/delivered new message 0x%llx:0x%llx\n",
356-
LLU{rop_util_get_gc_value(folder_id)},
357-
LLU{rop_util_get_gc_value(msg_id)});
358-
break;
359-
case deliver_message_result::result_error:
360-
fprintf(stderr, "Message rejected - unspecified reason\n");
361-
return EXIT_FAILURE;
362-
case deliver_message_result::mailbox_full_bysize:
363-
fprintf(stderr, "Message rejected - mailbox has reached quota limit");
364-
return EXIT_FAILURE;
365-
case deliver_message_result::mailbox_full_bymsg:
366-
fprintf(stderr, "Message rejected - mailbox has reached maximum message count (cf. exmdb_provider.cfg:max_store_message_count)");
367-
return EXIT_FAILURE;
368-
case deliver_message_result::partial_completion:
369-
fprintf(stderr, "Partial completion - The server could not save all of the message (wrong permissions/disk full/...)\n");
370-
return EXIT_FAILURE;
371-
}
372-
if (!(mode & DELIVERY_TWOSTEP))
373-
return EXIT_SUCCESS;
374-
if (exmdb_local_rules_execute == nullptr) {
375-
fprintf(stderr, "Programmer's error: libgxs_ruleproc.so was not activated, cannot perform rule processing");
376-
return EXIT_FAILURE;
377-
}
378-
fprintf(stderr, "Exercising TWOSTEP ruleprocessor:\n");
379-
if (msg_id == 0) {
380-
fprintf(stderr, "deliver_message RPC did not give us a message_id -- not executing any rules.\n");
381-
return EXIT_SUCCESS;
382-
}
383-
auto err = exmdb_local_rules_execute(g_storedir, ENVELOPE_FROM_NULL,
384-
target, folder_id, msg_id, mode);
385-
if (err != ecSuccess) {
386-
fprintf(stderr, "Rule execution not successful: %s\n", mapi_strerror(err));
387-
return EXIT_FAILURE;
388-
}
389-
return EXIT_SUCCESS;
390-
}
391-
392-
int exm_create_msg(uint64_t parent_fld, MESSAGE_CONTENT *ctnt,
393-
const std::string &im_repr, Json::Value &&digest)
394-
{
395-
uint64_t msg_id = 0, change_num = 0;
396-
if (!exmdb_client->allocate_message_id(g_storedir, parent_fld, &msg_id)) {
397-
fprintf(stderr, "exm: allocate_message_id RPC failed (timeout?)\n");
398-
return -EIO;
399-
} else if (!exmdb_client->allocate_cn(g_storedir, &change_num)) {
400-
fprintf(stderr, "exm: allocate_cn(msg) RPC failed\n");
401-
return -EIO;
402-
}
403-
ec_error_t ret;
404-
if ((ret = ctnt->proplist.set(PidTagMid, &msg_id)) != ecSuccess) {
405-
fprintf(stderr, "exm: tpropval: %s\n", mapi_strerror(ret));
406-
return ece2nerrno(ret);
407-
}
408-
auto iret = exm_set_change_keys(&ctnt->proplist, change_num);
409-
if (iret != 0) {
410-
fprintf(stderr, "exm: tpropval: %s\n", strerror(-iret));
411-
return iret;
412-
}
413-
auto midstr = fmt::format("{}.cn{}", time(nullptr), rop_util_get_gc_value(change_num));
414-
digest["file"] = midstr;
415-
if (!exmdb_client->imapfile_write(g_storedir, "eml",
416-
midstr.c_str(), im_repr.c_str())) {
417-
fprintf(stderr, "exm: imapfile_write RPC failed\n");
418-
return -EIO;
419-
}
420-
auto djson = json_to_str(digest);
421-
422-
uint64_t outmid = 0, outcn = 0;
423-
if (!exmdb_client->write_message(g_storedir, CP_UTF8, parent_fld,
424-
ctnt, djson.c_str(), &outmid, &outcn, &ret)) {
425-
fprintf(stderr, "exm: write_message RPC failed\n");
426-
return -EIO;
427-
} else if (ret != ecSuccess) {
428-
fprintf(stderr, "exm: write_message: %s\n", mapi_strerror(ret));
429-
return -EIO;
430-
} else if (g_verbose_create) {
431-
fprintf(stderr, "Created new message 0x%llx:0x%llx\n",
432-
LLU{rop_util_get_gc_value(parent_fld)},
433-
LLU{rop_util_get_gc_value(outmid)});
434-
}
435-
return 0;
436-
}
437-
438252
static std::string sql_escape(MYSQL *sqh, const char *in)
439253
{
440254
std::string out;

tools/genimport.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ extern std::string g_dstuser, g_storedir_s;
9292
extern const char *g_storedir;
9393
extern unsigned int g_user_id, g_wet_run;
9494
extern unsigned int g_public_folder, g_verbose_create;
95+
extern ec_error_t (*exmdb_local_rules_execute)(const char *, const char *, const char *, eid_t, eid_t, unsigned int);
9596

9697
extern void gi_dump_folder_map(const gi_folder_map_t &);
9798
extern void gi_dump_name_map(const gi_name_map &);
@@ -101,10 +102,7 @@ extern void gi_name_map_read(const void *, size_t, gi_name_map &);
101102
extern void gi_name_map_write(const gi_name_map &);
102103
extern gromox::propid_t gi_resolve_namedprop(const PROPERTY_XNAME &);
103104
extern int exm_set_change_keys(TPROPVAL_ARRAY *props, eid_t cn, const BINARY *oldpcl = nullptr);
104-
extern int exm_create_folder(uint64_t parent_fld, TPROPVAL_ARRAY *props, bool o_excl, uint64_t *new_fld_id);
105105
extern int exm_permissions(eid_t, const std::vector<PERMISSION_DATA> &);
106-
extern int exm_deliver_msg(const char *target, MESSAGE_CONTENT *, const std::string &, Json::Value &&, unsigned int flags = 0);
107-
extern int exm_create_msg(uint64_t parent_fld, MESSAGE_CONTENT *, const std::string &, Json::Value &&);
108106
extern int gi_setup_from_user(const char *);
109107
extern int gi_setup_from_dir(const char *);
110108
extern int gi_startup_client(unsigned int maxconn = 1);

0 commit comments

Comments
 (0)