Skip to content

Commit d9814e9

Browse files
jwalterjengelh
authored andcommitted
mbop: implement mbop_fprintf as verbose option for mbop
References: GXL-145
1 parent f5199db commit d9814e9

File tree

7 files changed

+36
-28
lines changed

7 files changed

+36
-28
lines changed

tools/mbop.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@ extern void command_overview();
2727
extern int cmd_parser(int, char **);
2828

2929
extern char *g_arg_username, *g_arg_userdir;
30-
extern unsigned int g_continuous_mode;
30+
extern unsigned int g_continuous_mode, g_verbose_mode, g_command_num;
3131

32+
template <typename... Args> int mbop_fprintf(Args &&...args) {
33+
if (g_verbose_mode)
34+
fprintf(stderr, "%s [cmd %d]: ", g_storedir, g_command_num);
35+
return fprintf(std::forward<Args>(args)...);
36+
}
3237
}
3338

3439
extern void mbop_help_cb(const struct HXoptcb *);

tools/mbop_delmsg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ int main(int argc, char **argv)
5454
while (*++argv != nullptr) {
5555
eid_t eid = gi_lookup_eid_by_name(g_storedir, *argv);
5656
if (eid == 0) {
57-
fprintf(stderr, "Not recognized/found: \"%s\"\n", *argv);
57+
global::mbop_fprintf(stderr, "Not recognized/found: \"%s\"\n", *argv);
5858
return EXIT_FAILURE;
5959
}
6060
eids.push_back(eid);

tools/mbop_emptyfld.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static int generic_del(eid_t fid, const std::vector<uint64_t> &chosen)
4646
ea.pids = deconst(chosen.data());
4747
if (!exmdb_client->delete_messages(g_storedir, CP_ACP, nullptr, fid,
4848
&ea, false, &partial_complete)) {
49-
fprintf(stderr, "fid 0x%llx delete_messages failed\n", LLU{rop_util_get_gc_value(fid)});
49+
global::mbop_fprintf(stderr, "fid 0x%llx delete_messages failed\n", LLU{rop_util_get_gc_value(fid)});
5050
return EXIT_FAILURE;
5151
}
5252
return EXIT_SUCCESS;
@@ -64,7 +64,7 @@ static int select_mids_by_time(eid_t fid, unsigned int tbl_flags,
6464
RESTRICTION rst_e = {RES_AND, {deconst(&rst_d)}};
6565
if (!exmdb_client->load_content_table(g_storedir, CP_ACP, fid, nullptr,
6666
tbl_flags, &rst_e, nullptr, &table_id, &row_count)) {
67-
fprintf(stderr, "fid 0x%llx load_content_table failed\n", LLU{rop_util_get_gc_value(fid)});
67+
global::mbop_fprintf(stderr, "fid 0x%llx load_content_table failed\n", LLU{rop_util_get_gc_value(fid)});
6868
return EXIT_FAILURE;
6969
}
7070
auto cl_0 = HX::make_scope_exit([&]() { exmdb_client->unload_table(g_storedir, table_id); });
@@ -73,7 +73,7 @@ static int select_mids_by_time(eid_t fid, unsigned int tbl_flags,
7373
tarray_set rowset{};
7474
if (!exmdb_client->query_table(g_storedir, nullptr, CP_ACP, table_id,
7575
&mtaghdr, 0, row_count, &rowset)) {
76-
fprintf(stderr, "fid 0x%llx query_table failed\n", LLU{rop_util_get_gc_value(fid)});
76+
global::mbop_fprintf(stderr, "fid 0x%llx query_table failed\n", LLU{rop_util_get_gc_value(fid)});
7777
return EXIT_FAILURE;
7878
}
7979
for (const auto &row : rowset) {
@@ -119,7 +119,7 @@ static int do_hierarchy(eid_t fid, uint32_t depth)
119119
uint32_t table_id = 0, row_count = 0;
120120
if (!exmdb_client->load_hierarchy_table(g_storedir, fid,
121121
nullptr, 0, nullptr, &table_id, &row_count)) {
122-
fprintf(stderr, "fid 0x%llx load_content_table failed\n", LLU{rop_util_get_gc_value(fid)});
122+
global::mbop_fprintf(stderr, "fid 0x%llx load_content_table failed\n", LLU{rop_util_get_gc_value(fid)});
123123
return EXIT_FAILURE;
124124
}
125125
auto cl_1 = HX::make_scope_exit([=]() { exmdb_client->unload_table(g_storedir, table_id); });
@@ -128,7 +128,7 @@ static int do_hierarchy(eid_t fid, uint32_t depth)
128128
tarray_set rowset{};
129129
if (!exmdb_client->query_table(g_storedir, nullptr, CP_ACP, table_id,
130130
&ftaghdr, 0, row_count, &rowset)) {
131-
fprintf(stderr, "fid 0x%llx query_table failed\n", LLU{rop_util_get_gc_value(fid)});
131+
global::mbop_fprintf(stderr, "fid 0x%llx query_table failed\n", LLU{rop_util_get_gc_value(fid)});
132132
return EXIT_FAILURE;
133133
}
134134
exmdb_client->unload_table(g_storedir, table_id);
@@ -145,7 +145,7 @@ static int do_hierarchy(eid_t fid, uint32_t depth)
145145
static constexpr PROPTAG_ARRAY ftaghdr2 = {std::size(ftags2), deconst(ftags2)};
146146
TPROPVAL_ARRAY props{};
147147
if (!exmdb_client->get_folder_properties(g_storedir, CP_ACP, fid, &ftaghdr2, &props)) {
148-
fprintf(stderr, "fid 0x%llx get_folder_props failed\n", LLU{fid});
148+
global::mbop_fprintf(stderr, "fid 0x%llx get_folder_props failed\n", LLU{fid});
149149
return EXIT_FAILURE;
150150
}
151151
auto p1 = props.get<const uint32_t>(PR_CONTENT_COUNT);
@@ -159,12 +159,12 @@ static int do_hierarchy(eid_t fid, uint32_t depth)
159159
BOOL b_result = false;
160160
if (!exmdb_client->delete_folder(g_storedir, CP_ACP, fid,
161161
g_del_flags & DELETE_HARD_DELETE, &b_result)) {
162-
fprintf(stderr, "fid 0x%llx delete_folder RPC rejected/malformed\n", LLU{rop_util_get_gc_value(fid)});
162+
global::mbop_fprintf(stderr, "fid 0x%llx delete_folder RPC rejected/malformed\n", LLU{rop_util_get_gc_value(fid)});
163163
return EXIT_FAILURE;
164164
} else if (!b_result) {
165-
fprintf(stderr, "fid 0x%llx delete_folder unsuccessful (no permissions etc.)\n", LLU{rop_util_get_gc_value(fid)});
165+
global::mbop_fprintf(stderr, "fid 0x%llx delete_folder unsuccessful (no permissions etc.)\n", LLU{rop_util_get_gc_value(fid)});
166166
} else {
167-
fprintf(stderr, "Folder 0x%llx: deleted due to --delempty\n", LLU{rop_util_get_gc_value(fid)});
167+
global::mbop_fprintf(stderr, "Folder 0x%llx: deleted due to --delempty\n", LLU{rop_util_get_gc_value(fid)});
168168
}
169169
return EXIT_SUCCESS;
170170
}
@@ -176,26 +176,26 @@ int main(int argc, char **argv)
176176
return EXIT_PARAM;
177177
auto cl_0 = HX::make_scope_exit([=]() { HX_zvecfree(argv); });
178178
if (g_del_flags & DEL_FOLDERS && g_recurse) {
179-
fprintf(stderr, "Combining -R and --nuke-folders is unreasonable: when you nuke folders, you cannot recurse into them anymore.\n");
179+
global::mbop_fprintf(stderr, "Combining -R and --nuke-folders is unreasonable: when you nuke folders, you cannot recurse into them anymore.\n");
180180
return EXIT_FAILURE;
181181
} else if (g_delempty && !g_recurse) {
182-
fprintf(stderr, "--delempty requires -R\n");
182+
global::mbop_fprintf(stderr, "--delempty requires -R\n");
183183
return EXIT_FAILURE;
184184
}
185185
if (g_time_str != nullptr) {
186186
char *end = nullptr;
187187
auto t = HX_strtoull_sec(g_time_str, &end);
188188
if (t == ULLONG_MAX && errno == ERANGE) {
189-
fprintf(stderr, "Timespec \"%s\" is too damn big\n", g_time_str);
189+
global::mbop_fprintf(stderr, "Timespec \"%s\" is too damn big\n", g_time_str);
190190
return EXIT_FAILURE;
191191
} else if (end != nullptr && *end != '\0') {
192-
fprintf(stderr, "Timespec \"%s\" not fully understood (error at: \"%s\")\n",
192+
global::mbop_fprintf(stderr, "Timespec \"%s\" not fully understood (error at: \"%s\")\n",
193193
g_time_str, end);
194194
return EXIT_FAILURE;
195195
}
196196
g_cutoff_time = rop_util_unix_to_nttime(time(nullptr) - t);
197197
if (g_del_flags & DEL_FOLDERS) {
198-
fprintf(stderr, "Combining -t and --nuke-folders is unreasonable: when you delete folders, it may delete messages therein which are younger than -t.\n");
198+
global::mbop_fprintf(stderr, "Combining -t and --nuke-folders is unreasonable: when you delete folders, it may delete messages therein which are younger than -t.\n");
199199
return EXIT_FAILURE;
200200
}
201201
}
@@ -205,7 +205,7 @@ int main(int argc, char **argv)
205205
BOOL partial = false;
206206
eid_t eid = gi_lookup_eid_by_name(g_storedir, *argv);
207207
if (eid == 0) {
208-
fprintf(stderr, "Not recognized/found: \"%s\"\n", *argv);
208+
global::mbop_fprintf(stderr, "Not recognized/found: \"%s\"\n", *argv);
209209
return EXIT_FAILURE;
210210
}
211211
if (g_cutoff_time != 0 || g_recurse) {
@@ -220,7 +220,7 @@ int main(int argc, char **argv)
220220
auto ok = exmdb_client->empty_folder(g_storedir, CP_UTF8, nullptr,
221221
eid, g_del_flags, &partial);
222222
if (!ok) {
223-
fprintf(stderr, "empty_folder(%s) failed\n", *argv);
223+
global::mbop_fprintf(stderr, "empty_folder(%s) failed\n", *argv);
224224
ret = EXIT_FAILURE;
225225
}
226226
delcount(eid, &curr_delc, &curr_fldc);

tools/mbop_foreach.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ int main(int argc, char **argv)
110110
HXOPT_RQ_ORDER | HXOPT_USAGEONERR) != HXOPT_ERR_SUCCESS ||
111111
g_exit_after_optparse)
112112
return EXIT_PARAM;
113+
global::g_command_num = 0;
113114
auto cl_0 = HX::make_scope_exit([=]() { HX_zvecfree(argv); });
114115
if (global::g_arg_username != nullptr || global::g_arg_userdir != nullptr) {
115116
fprintf(stderr, "Cannot use -d/-u with foreach.*\n");
@@ -187,6 +188,7 @@ int main(int argc, char **argv)
187188
g_storedir_s = std::move(user.maildir);
188189
g_storedir = g_storedir_s.c_str();
189190
ret = global::cmd_parser(argc, argv);
191+
++global::g_command_num;
190192
if (ret == EXIT_PARAM)
191193
return ret;
192194
else if (ret != EXIT_SUCCESS && !global::g_continuous_mode)

tools/mbop_freebusy.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ static int xmktime(const char *str, time_t *out)
3535
struct tm tm{};
3636
end = strptime(str, "%FT%T", &tm);
3737
if (end != nullptr && *end != '\0') {
38-
fprintf(stderr, "\"%s\" not understood, error at \"%s\". Required format is \"2024-01-01T00:00:00\" [always local system time] or unixtime.\n", g_start_txt, end);
38+
global::mbop_fprintf(stderr, "\"%s\" not understood, error at \"%s\". Required format is \"2024-01-01T00:00:00\" [always local system time] or unixtime.\n", g_start_txt, end);
3939
return -1;
4040
}
4141
tm.tm_wday = -1;
4242
*out = mktime(&tm);
4343
if (*out == -1 && tm.tm_wday == -1) {
44-
fprintf(stderr, "\"%s\" not understood by mktime\n", g_start_txt);
44+
global::mbop_fprintf(stderr, "\"%s\" not understood by mktime\n", g_start_txt);
4545
return -1;
4646
}
4747
return 0;
@@ -60,7 +60,7 @@ int main(int argc, char **argv)
6060
return EXIT_PARAM;
6161
std::vector<freebusy_event> fbout;
6262
if (!get_freebusy(g_requestor, g_storedir, start_time, end_time, fbout)) {
63-
fprintf(stderr, "get_freebusy call not successful\n");
63+
global::mbop_fprintf(stderr, "get_freebusy call not successful\n");
6464
return EXIT_FAILURE;
6565
}
6666
printf("Results (%zu rows):\n", fbout.size());

tools/mbop_locale.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ int main(int argc, char **argv)
3131
return EXIT_PARAM;
3232
auto cl_0a = HX::make_scope_exit([=]() { HX_zvecfree(argv); });
3333
if (g_language == nullptr) {
34-
fprintf(stderr, "You need to specify the -l option\n");
34+
global::mbop_fprintf(stderr, "You need to specify the -l option\n");
3535
return EXIT_PARAM;
3636
}
3737
textmaps_init();
3838
if (!mysql_adaptor_set_user_lang(g_dstuser.c_str(), g_language)) {
39-
fprintf(stderr, "Update of UI language rejected\n");
39+
global::mbop_fprintf(stderr, "Update of UI language rejected\n");
4040
return EXIT_FAILURE;
4141
}
4242

@@ -58,7 +58,7 @@ int main(int argc, char **argv)
5858
TPROPVAL_ARRAY props{};
5959
if (!exmdb_client->get_folder_properties(g_storedir,
6060
CP_ACP, folder_id, &taghdr, &props)) {
61-
fprintf(stderr, "get_folder_props failed\n");
61+
global::mbop_fprintf(stderr, "get_folder_props failed\n");
6262
return EXIT_FAILURE;
6363
}
6464
auto orig_name = props.get<const char>(PR_DISPLAY_NAME);
@@ -69,7 +69,7 @@ int main(int argc, char **argv)
6969
PROBLEM_ARRAY probs{};
7070
if (!exmdb_client->set_folder_properties(g_storedir, CP_ACP,
7171
folder_id, &new_props, &probs)) {
72-
fprintf(stderr, "set_folder_props failed\n");
72+
global::mbop_fprintf(stderr, "set_folder_props failed\n");
7373
return EXIT_FAILURE;
7474
}
7575
}

tools/mbop_main.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ void delcount(eid_t fid, uint32_t *delc, uint32_t *fldc)
6666
namespace global {
6767

6868
char *g_arg_username, *g_arg_userdir;
69-
unsigned int g_continuous_mode;
69+
unsigned int g_continuous_mode, g_verbose_mode, g_command_num;
7070
static constexpr HXoption g_options_table[] = {
7171
{nullptr, 'c', HXTYPE_NONE, &g_continuous_mode, {}, {}, {}, "Do not stop on errors"},
72+
{nullptr, 'v', HXTYPE_NONE, &g_verbose_mode, {}, {}, {}, "Be a little more talkative"},
7273
{nullptr, 'd', HXTYPE_STRING, &g_arg_userdir, nullptr, nullptr, 0, "Directory of the mailbox", "DIR"},
7374
{nullptr, 'u', HXTYPE_STRING, &g_arg_username, nullptr, nullptr, 0, "Username of store to import to", "EMAILADDR"},
7475
MBOP_AUTOHELP,
@@ -169,11 +170,11 @@ static int main(int argc, char **argv)
169170
else if (strcmp(argv[0], "recalc-sizes") == 0)
170171
ok = recalc_sizes(g_storedir);
171172
else {
172-
fprintf(stderr, "Unrecognized subcommand \"%s\"\n", argv[0]);
173+
global::mbop_fprintf(stderr, "Unrecognized subcommand \"%s\"\n", argv[0]);
173174
return EXIT_PARAM;
174175
}
175176
if (!ok) {
176-
fprintf(stderr, "%s: the operation failed\n", argv[0]);
177+
global::mbop_fprintf(stderr, "%s: the operation failed\n", argv[0]);
177178
return EXIT_FAILURE;
178179
}
179180
return EXIT_SUCCESS;

0 commit comments

Comments
 (0)