Skip to content

Commit

Permalink
#183: cleanup.
Browse files Browse the repository at this point in the history
- split rbox_mail_storage_try_copy into smaller functions.
- fixed: some cpplint issues.
  • Loading branch information
jrse committed Aug 9, 2018
1 parent 5064dc6 commit 1d5cb92
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 147 deletions.
3 changes: 2 additions & 1 deletion src/librmb/tools/rmb/rmb-commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@

#ifndef SRC_LIBRMB_TOOLS_RMB_RMB_COMMANDS_H_
#define SRC_LIBRMB_TOOLS_RMB_RMB_COMMANDS_H_
#include <stdlib.h>

#include <iostream>
#include <map>
#include <string>
#include <stdlib.h>
#include <vector>
#include <sstream>
#include <iterator>
Expand Down
70 changes: 32 additions & 38 deletions src/storage-rbox/doveadm-rbox-plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*/
#include <algorithm>
#include <list>
#include <map>
#include <string>
#include <vector>

extern "C" {
#include "lib.h"
#include "module-dir.h"
Expand All @@ -27,11 +33,9 @@ extern "C" {
#include "mail-search.h"
#include "mail-search-build.h"
#include "doveadm-cmd.h"
#include "doveadm-mail.h"
#include "istream.h"
#include "doveadm-print.h"
}
#include <algorithm>

#include "tools/rmb/rmb-commands.h"
#include "rados-cluster.h"
Expand All @@ -45,7 +49,8 @@ extern "C" {
#include "rbox-save.h"
#include "rbox-storage.hpp"

int check_namespace_mailboxes(struct mail_namespace *ns, std::vector<librmb::RadosMailObject *> &mail_objects);
int check_namespace_mailboxes(const struct mail_namespace *ns,
const std::vector<librmb::RadosMailObject *> &mail_objects);

class RboxDoveadmPlugin {
public:
Expand All @@ -54,6 +59,7 @@ class RboxDoveadmPlugin {
this->storage = new librmb::RadosStorageImpl(cluster);
this->config = new librmb::RadosDovecotCephCfgImpl(&storage->get_io_ctx());
}

~RboxDoveadmPlugin() {
if (config != nullptr) {
delete config;
Expand All @@ -67,18 +73,16 @@ class RboxDoveadmPlugin {
delete cluster;
}
}

int open_connection() {
int ret = -1;
if (config == nullptr) {
return -1;
}
ret = storage->open_connection(config->get_pool_name(), config->get_rados_cluster_name(),
config->get_rados_username());
return ret;
return (config == nullptr) ? -1
: storage->open_connection(config->get_pool_name(), config->get_rados_cluster_name(),
config->get_rados_username());
}
int read_plugin_configuration(struct mail_user *user) {

void read_plugin_configuration(struct mail_user *user) {
if (user == NULL) {
return 0;
return;
}
std::map<std::string, std::string> *map = config->get_config();
for (std::map<std::string, std::string>::iterator it = map->begin(); it != map->end(); ++it) {
Expand All @@ -90,9 +94,9 @@ class RboxDoveadmPlugin {
}
}
config->set_config_valid(true);
return 0;
}
int read_doveadm_plugin_configuration() {

void read_doveadm_plugin_configuration() {
std::map<std::string, std::string> *map = config->get_config();
for (std::map<std::string, std::string>::iterator it = map->begin(); it != map->end(); ++it) {
std::string setting = it->first;
Expand All @@ -103,7 +107,6 @@ class RboxDoveadmPlugin {
}
}
config->set_config_valid(true);
return 0;
}

public:
Expand All @@ -113,23 +116,21 @@ class RboxDoveadmPlugin {
};

static int open_connection_load_config(RboxDoveadmPlugin *plugin) {
int ret = -1;

plugin->read_doveadm_plugin_configuration();
int open = plugin->open_connection();
if (open < 0) {
i_error("Error opening rados connection. Errorcode: %d", open);
return 0;
}
librmb::RadosCephConfig *cfg = (static_cast<librmb::RadosDovecotCephCfgImpl *>(plugin->config))->get_rados_ceph_cfg();
ret = cfg->load_cfg();
int ret = cfg->load_cfg();
if (ret < 0) {
i_error("Error accessing configuration. Errorcode: %d", ret);
return ret;
}
return ret;
}
static int cmd_rmb_config(std::map<std::string, std::string> &opts) {
static int cmd_rmb_config(const std::map<std::string, std::string> &opts) {
RboxDoveadmPlugin plugin;
plugin.read_doveadm_plugin_configuration();
int open = open_connection_load_config(&plugin);
Expand Down Expand Up @@ -213,12 +214,11 @@ static int cmd_rmb_ls_run(struct doveadm_mail_cmd_context *ctx, struct mail_user

ctx->exit_code = cmd_rmb_search_run(opts, user, false, parser, mail_objects, false);

// TODO: check for mails without reference
auto it_mail = std::find_if(mail_objects.begin(), mail_objects.end(),
[](librmb::RadosMailObject *n) -> bool { return n->is_index_ref() == false; });

if (it_mail != mail_objects.end()) {
std::cout << "There are unreference objects " << std::endl;
std::cout << "There are unreferenced objects " << std::endl;
}

for (auto mo : mail_objects) {
Expand All @@ -231,7 +231,6 @@ static int cmd_rmb_ls_run(struct doveadm_mail_cmd_context *ctx, struct mail_user
return 0;
}
static int cmd_rmb_ls_mb_run(struct doveadm_mail_cmd_context *ctx, struct mail_user *user) {
int ret = -1;
std::map<std::string, std::string> opts;
opts["ls"] = "mb";
opts["sort"] = "uid";
Expand All @@ -243,7 +242,7 @@ static int cmd_rmb_ls_mb_run(struct doveadm_mail_cmd_context *ctx, struct mail_u
i_error("invalid ls search query");
ctx->exit_code = -1;
}
return ret;
return 0;
}

static int cmd_rmb_get_run(struct doveadm_mail_cmd_context *ctx, struct mail_user *user) {
Expand Down Expand Up @@ -531,7 +530,7 @@ static int doveadm_rmb_mail_next_user(struct doveadm_mail_cmd_context *ctx,
#else
mail_storage_service_user_free(&cur_service_user);
#endif
return ret;
return ret;
}
for (std::list<librmb::RadosSaveLogEntry>::iterator it = entries->begin(); it != entries->end(); ++it) {
// do something here!
Expand Down Expand Up @@ -600,8 +599,8 @@ static int cmd_rmb_revert_log_run(struct doveadm_mail_cmd_context *ctx, struct m
return 0;
}

static int iterate_mailbox(struct mail_namespace *ns, const struct mailbox_info *info,
std::vector<librmb::RadosMailObject *> &mail_objects) {
static int iterate_mailbox(const struct mail_namespace *ns, const struct mailbox_info *info,
const std::vector<librmb::RadosMailObject *> &mail_objects) {
int ret = 0;
struct mailbox_transaction_context *mailbox_transaction;
struct mail_search_context *search_ctx;
Expand Down Expand Up @@ -674,7 +673,8 @@ static int iterate_mailbox(struct mail_namespace *ns, const struct mailbox_info
return ret;
}

int check_namespace_mailboxes(struct mail_namespace *ns, std::vector<librmb::RadosMailObject *> &mail_objects) {
int check_namespace_mailboxes(const struct mail_namespace *ns,
const std::vector<librmb::RadosMailObject *> &mail_objects) {
struct mailbox_list_iterate_context *iter;
const struct mailbox_info *info;
int ret = 0;
Expand Down Expand Up @@ -714,7 +714,6 @@ static int cmd_rmb_check_indices_run(struct doveadm_mail_cmd_context *ctx, struc
check_namespace_mailboxes(ns, mail_objects);
}

// TODO: check for mails with
auto it_mail = std::find_if(mail_objects.begin(), mail_objects.end(),
[](librmb::RadosMailObject *m) { return m->is_index_ref() == false; });

Expand Down Expand Up @@ -755,8 +754,8 @@ static int cmd_rmb_check_indices_run(struct doveadm_mail_cmd_context *ctx, struc
std::cout << "mail object: " << mo->get_oid().c_str()
<< " deleted: " << (plugin.storage->delete_mail(mo) < 0 ? " FALSE " : " TRUE") << std::endl;
ctx->exit_code = 2;
}
delete mo;
}
delete mo;
}
delete ms;

Expand Down Expand Up @@ -850,7 +849,6 @@ static int cmd_mailbox_delete_run(struct doveadm_mail_cmd_context *_ctx, struct
return 0;
}


static int cmd_rmb_mailbox_delete_run(struct doveadm_mail_cmd_context *ctx, struct mail_user *user) {
int ret = cmd_mailbox_delete_run(ctx, user);
if (ret == 0 && ctx->exit_code == 0) {
Expand Down Expand Up @@ -929,7 +927,7 @@ static void cmd_rmb_mailbox_delete_init(struct doveadm_mail_cmd_context *_ctx AT
const char *name;
unsigned int i;

if (args[0] == NULL){
if (args[0] == NULL) {
doveadm_mail_help_name("rmb mailbox delete");
}
doveadm_mailbox_args_check(args);
Expand Down Expand Up @@ -991,8 +989,6 @@ struct doveadm_mail_cmd_context *cmd_rmb_revert_log_alloc(void) {
return ctx;
}



static bool cmd_check_indices_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c) {
struct check_indices_cmd_context *ctx = (struct check_indices_cmd_context *)_ctx;

Expand Down Expand Up @@ -1041,8 +1037,6 @@ static bool cmd_mailbox_delete_parse_arg(struct doveadm_mail_cmd_context *_ctx,
return TRUE;
}



struct doveadm_mail_cmd_context *cmd_rmb_mailbox_delete_alloc(void) {
struct delete_cmd_context *ctx;
ctx = doveadm_mail_cmd_alloc(struct delete_cmd_context);
Expand All @@ -1069,15 +1063,15 @@ int cmd_rmb_config_create(int argc, char *argv[]) {
int open = plugin.open_connection();
if (open < 0) {
i_error("Error opening rados connection. Errorcode: %d", open);
return -1;
return -1;
}
librmb::RadosCephConfig *cfg = (static_cast<librmb::RadosDovecotCephCfgImpl *>(plugin.config))->get_rados_ceph_cfg();
int ret = cfg->load_cfg();
if (ret < 0) {
ret = cfg->save_cfg();
if (ret < 0) {
i_error("error creating configuration %d", ret);
return -1;
return -1;
}
std::cout << "config created" << std::endl;
} else {
Expand Down
2 changes: 0 additions & 2 deletions src/storage-rbox/istream-bufferlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ struct istream *i_stream_create_from_bufferlist(librados::bufferlist *data, cons
stream->buffer = reinterpret_cast<const unsigned char *>(data->c_str());
stream->pos = size;
stream->max_buffer_size = (size_t)-1;
;

stream->read = i_stream_data_read;
stream->seek = i_stream_data_seek; // use default
Expand All @@ -58,4 +57,3 @@ struct istream *i_stream_create_from_bufferlist(librados::bufferlist *data, cons
i_stream_set_name(&stream->istream, "(buffer)");
return &stream->istream;
}

1 change: 1 addition & 0 deletions src/storage-rbox/ostream-bufferlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*/
#include <string>

extern "C" {
#include "lib.h"
Expand Down
Loading

0 comments on commit 1d5cb92

Please sign in to comment.