#include "settings.h"
#include "fs-api.h"
#include "doveadm.h"
+#include "doveadm-fs.h"
#include "doveadm-print.h"
#include <stdio.h>
static void fs_cmd_help(struct doveadm_cmd_context *cctx) ATTR_NORETURN;
static void cmd_fs_delete(struct doveadm_cmd_context *cctx);
-static struct fs *
-cmd_fs_init(struct doveadm_cmd_context *cctx)
+static struct fs *doveadm_fs_init(struct doveadm_cmd_context *cctx)
{
struct fs_parameters fs_param;
struct fs *fs;
const char *filter_name, *error;
if (!doveadm_cmd_param_str(cctx, "filter-name", &filter_name))
- fs_cmd_help(cctx);
+ help_ver2(cctx->cmd);
i_zero(&fs_param);
fs_param.temp_dir = doveadm_settings->mail_temp_dir;
static void cmd_fs_get(struct doveadm_cmd_context *cctx)
{
- struct fs *fs;
- struct fs_file *file;
- struct istream *input;
const char *path;
- const unsigned char *data;
- size_t size;
- ssize_t ret;
doveadm_print_init(DOVEADM_PRINT_TYPE_PAGER);
doveadm_print_header("content", "content", DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE);
- fs = cmd_fs_init(cctx);
if (!doveadm_cmd_param_str(cctx, "path", &path))
fs_cmd_help(cctx);
+ doveadm_fs_get(cctx, path);
+}
+
+void doveadm_fs_get(struct doveadm_cmd_context *cctx, const char *path)
+{
+ struct fs *fs;
+ struct fs_file *file;
+ struct istream *input;
+ const unsigned char *data;
+ size_t size;
+ ssize_t ret;
+
+ fs = doveadm_fs_init(cctx);
file = fs_file_init(fs, path, FS_OPEN_MODE_READONLY);
input = fs_read_stream(file, IO_BLOCK_SIZE);
static void cmd_fs_put(struct doveadm_cmd_context *cctx)
{
- struct fs *fs;
- enum fs_properties props;
const char *hash_str, *src_path, *dest_path;
- const char *const *metas;
- struct fs_file *file;
- struct istream *input;
- struct ostream *output;
buffer_t *hash = NULL;
- fs = cmd_fs_init(cctx);
if (!doveadm_cmd_param_str(cctx, "input-path", &src_path) ||
!doveadm_cmd_param_str(cctx, "path", &dest_path))
fs_cmd_help(cctx);
if (hex_to_binary(optarg, hash) < 0)
i_fatal("Invalid -h parameter: Hash not in hex");
}
+ doveadm_fs_put(cctx, src_path, dest_path, hash);
+}
+
+void doveadm_fs_put(struct doveadm_cmd_context *cctx,
+ const char *src_path, const char *dest_path,
+ const buffer_t *hash)
+{
+ struct fs *fs;
+ enum fs_properties props;
+ const char *const *metas;
+ struct fs_file *file;
+ struct istream *input;
+ struct ostream *output;
+
+ fs = doveadm_fs_init(cctx);
file = fs_file_init(fs, dest_path, FS_OPEN_MODE_REPLACE);
props = fs_get_properties(fs);
static void cmd_fs_copy(struct doveadm_cmd_context *cctx)
{
- struct fs *fs;
- struct fs_file *src_file, *dest_file;
const char *src_path, *dest_path;
- fs = cmd_fs_init(cctx);
if (!doveadm_cmd_param_str(cctx, "source-path", &src_path) ||
!doveadm_cmd_param_str(cctx, "destination-path", &dest_path))
fs_cmd_help(cctx);
+ doveadm_fs_copy(cctx, src_path, dest_path);
+}
+
+void doveadm_fs_copy(struct doveadm_cmd_context *cctx,
+ const char *src_path, const char *dest_path)
+{
+ struct fs *fs;
+ struct fs_file *src_file, *dest_file;
+ fs = doveadm_fs_init(cctx);
src_file = fs_file_init(fs, src_path, FS_OPEN_MODE_READONLY);
dest_file = fs_file_init(fs, dest_path, FS_OPEN_MODE_REPLACE);
if (fs_copy(src_file, dest_file) == 0) ;
static void cmd_fs_stat(struct doveadm_cmd_context *cctx)
{
- struct fs *fs;
- struct fs_file *file;
- struct stat st;
const char *path;
- fs = cmd_fs_init(cctx);
- if (!doveadm_cmd_param_str(cctx, "path", &path))
- fs_cmd_help(cctx);
-
- file = fs_file_init(fs, path, FS_OPEN_MODE_READONLY);
-
doveadm_print_init(DOVEADM_PRINT_TYPE_FORMATTED);
doveadm_print_formatted_set_format("%{path} size=%{size}");
doveadm_print_header_simple("path");
doveadm_print_header("size", "size", DOVEADM_PRINT_HEADER_FLAG_NUMBER);
+ if (!doveadm_cmd_param_str(cctx, "path", &path))
+ fs_cmd_help(cctx);
+ doveadm_fs_stat(cctx, path);
+}
+
+void doveadm_fs_stat(struct doveadm_cmd_context *cctx, const char *path)
+{
+ struct fs *fs;
+ struct fs_file *file;
+ struct stat st;
+
+ fs = doveadm_fs_init(cctx);
+ file = fs_file_init(fs, path, FS_OPEN_MODE_READONLY);
+
if (fs_stat(file, &st) == 0) {
doveadm_print(fs_file_path(file));
doveadm_print(dec2str(st.st_size));
static void cmd_fs_metadata(struct doveadm_cmd_context *cctx)
{
- struct fs *fs;
- struct fs_file *file;
- const struct fs_metadata *m;
- const ARRAY_TYPE(fs_metadata) *metadata;
const char *path;
- fs = cmd_fs_init(cctx);
- if (!doveadm_cmd_param_str(cctx, "path", &path))
- fs_cmd_help(cctx);
-
- file = fs_file_init(fs, path, FS_OPEN_MODE_READONLY);
-
doveadm_print_init(DOVEADM_PRINT_TYPE_FORMATTED);
doveadm_print_formatted_set_format("%{key}=%{value}\n");
doveadm_print_header_simple("key");
doveadm_print_header_simple("value");
+ if (!doveadm_cmd_param_str(cctx, "path", &path))
+ fs_cmd_help(cctx);
+ doveadm_fs_metadata(cctx, path);
+}
+
+void doveadm_fs_metadata(struct doveadm_cmd_context *cctx, const char *path)
+{
+ struct fs *fs;
+ struct fs_file *file;
+ const struct fs_metadata *m;
+ const ARRAY_TYPE(fs_metadata) *metadata;
+
+ fs = doveadm_fs_init(cctx);
+ file = fs_file_init(fs, path, FS_OPEN_MODE_READONLY);
if (fs_get_metadata(file, &metadata) == 0) {
array_foreach(metadata, m) {
doveadm_print(m->key);
}
}
-static void
-cmd_fs_delete_recursive(struct doveadm_cmd_context *cctx,
- unsigned int async_count)
+void doveadm_fs_delete_recursive(struct doveadm_cmd_context *cctx,
+ const char *const *paths,
+ unsigned int async_count)
{
struct fs *fs;
- const char *const *paths;
unsigned int i;
- fs = cmd_fs_init(cctx);
- if (!doveadm_cmd_param_array(cctx, "path", &paths))
- fs_cmd_help(cctx);
-
+ fs = doveadm_fs_init(cctx);
for (i = 0; paths[i] != NULL; i++)
cmd_fs_delete_recursive_path(cctx, fs, paths[i], async_count);
fs_deinit(&fs);
}
-static void cmd_fs_delete_paths(struct doveadm_cmd_context *cctx,
- unsigned int async_count)
+void doveadm_fs_delete_paths(struct doveadm_cmd_context *cctx,
+ const char *const *paths,
+ unsigned int async_count)
{
- struct fs *fs;
struct fs_delete_ctx ctx;
- const char *const *paths;
unsigned int i;
int ret;
- fs = cmd_fs_init(cctx);
- if (!doveadm_cmd_param_array(cctx, "path", &paths))
- fs_cmd_help(cctx);
-
i_zero(&ctx);
ctx.cctx = cctx;
- ctx.fs = fs;
+ ctx.fs = doveadm_fs_init(cctx);
ctx.path_prefix = "";
ctx.files_count = I_MAX(async_count, 1);
ctx.files = t_new(struct fs_file *, ctx.files_count);
break;
}
doveadm_fs_delete_async_finish(&ctx);
- fs_deinit(&fs);
+ fs_deinit(&ctx.fs);
}
static void cmd_fs_delete(struct doveadm_cmd_context *cctx)
{
+ const char *const *paths;
bool recursive = FALSE;
int64_t async_count = 0;
(void)doveadm_cmd_param_bool(cctx, "recursive", &recursive);
(void)doveadm_cmd_param_int64(cctx, "max-parallel", &async_count);
+ if (!doveadm_cmd_param_array(cctx, "path", &paths))
+ fs_cmd_help(cctx);
+
if (recursive)
- cmd_fs_delete_recursive(cctx, async_count);
+ doveadm_fs_delete_recursive(cctx, paths, async_count);
else
- cmd_fs_delete_paths(cctx, async_count);
+ doveadm_fs_delete_paths(cctx, paths, async_count);
}
static void cmd_fs_iter_full(struct doveadm_cmd_context *cctx,
enum fs_iter_flags flags)
{
- struct fs *fs;
- struct fs_iter *iter;
- const char *path, *fname, *error;
+ const char *path;
bool b;
if (doveadm_cmd_param_bool(cctx, "no-cache", &b) && b)
if (doveadm_cmd_param_bool(cctx, "object-ids", &b) && b)
flags |= FS_ITER_FLAG_OBJECTIDS;
- fs = cmd_fs_init(cctx);
if (!doveadm_cmd_param_str(cctx, "path", &path))
fs_cmd_help(cctx);
doveadm_print_init(DOVEADM_PRINT_TYPE_FORMATTED);
doveadm_print_formatted_set_format("%{path}\n");
doveadm_print_header_simple("path");
+ doveadm_fs_iter(cctx, flags, path);
+}
+
+void doveadm_fs_iter(struct doveadm_cmd_context *cctx,
+ enum fs_iter_flags flags, const char *path)
+{
+ struct fs *fs;
+ struct fs_iter *iter;
+ const char *fname, *error;
+ fs = doveadm_fs_init(cctx);
iter = fs_iter_init(fs, path, flags);
while ((fname = fs_iter_next(iter)) != NULL) {
doveadm_print(fname);