]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
doveadm fs: Split functions in preparation for "doveadm mail fs" commands
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Mon, 7 Aug 2023 08:16:44 +0000 (11:16 +0300)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Wed, 12 Feb 2025 10:34:10 +0000 (12:34 +0200)
src/doveadm/Makefile.am
src/doveadm/doveadm-fs.c
src/doveadm/doveadm-fs.h [new file with mode: 0644]

index 294b8e539a3c2f5c234ada062ab8c9e32c78831c..71d8f65a814758caeeeb7c9cc4029167e8fbe4b1 100644 (file)
@@ -170,6 +170,7 @@ pkginc_lib_HEADERS = \
 noinst_HEADERS = \
        client-connection.h \
        client-connection-private.h \
+       doveadm-fs.h \
        doveadm-who.h
 
 test_programs = \
index 3c9e65259b1171685873014cd952a3d32e885145..39c893cad6838492bdf7b05a21493abfb0623434 100644 (file)
@@ -12,6 +12,7 @@
 #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;
@@ -44,20 +44,26 @@ cmd_fs_init(struct doveadm_cmd_context *cctx)
 
 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);
@@ -83,16 +89,9 @@ static void cmd_fs_get(struct doveadm_cmd_context *cctx)
 
 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);
@@ -101,6 +100,21 @@ static void cmd_fs_put(struct doveadm_cmd_context *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);
@@ -146,15 +160,21 @@ static void cmd_fs_put(struct doveadm_cmd_context *cctx)
 
 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) ;
@@ -174,22 +194,27 @@ static void cmd_fs_copy(struct doveadm_cmd_context *cctx)
 
 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));
@@ -209,23 +234,27 @@ static void cmd_fs_stat(struct doveadm_cmd_context *cctx)
 
 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);
@@ -415,39 +444,30 @@ static void cmd_fs_delete_recursive_path(struct doveadm_cmd_context *cctx,
        }
 }
 
-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);
@@ -460,29 +480,31 @@ static void cmd_fs_delete_paths(struct doveadm_cmd_context *cctx,
                        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)
@@ -490,14 +512,23 @@ static void cmd_fs_iter_full(struct doveadm_cmd_context *cctx,
        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);
diff --git a/src/doveadm/doveadm-fs.h b/src/doveadm/doveadm-fs.h
new file mode 100644 (file)
index 0000000..417da3d
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef DOVEADM_FS_H
+#define DOVEADM_FS_H
+
+void doveadm_fs_get(struct doveadm_cmd_context *cctx, const char *path);
+void doveadm_fs_put(struct doveadm_cmd_context *cctx,
+                   const char *src_path, const char *dest_path,
+                   const buffer_t *hash);
+void doveadm_fs_copy(struct doveadm_cmd_context *cctx,
+                    const char *src_path, const char *dest_path);
+void doveadm_fs_stat(struct doveadm_cmd_context *cctx, const char *path);
+void doveadm_fs_metadata(struct doveadm_cmd_context *cctx, const char *path);
+void doveadm_fs_delete_recursive(struct doveadm_cmd_context *cctx,
+                                const char *const *paths,
+                                unsigned int async_count);
+void doveadm_fs_delete_paths(struct doveadm_cmd_context *cctx,
+                            const char *const *paths,
+                            unsigned int async_count);
+void doveadm_fs_iter(struct doveadm_cmd_context *cctx,
+                    enum fs_iter_flags flags, const char *path);
+
+#endif