doveadm-fs.c revision e3734a6606e292b6673a506dfcd7e0964b7ba451
76b43e4417bab52e913da39b5f5bc2a130d3f149Timo Sirainen/* Copyright (c) 2013-2014 Dovecot authors, see the included COPYING file */
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainenstatic void fs_cmd_help(doveadm_command_t *cmd);
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainenstatic void cmd_fs_delete(int argc, char *argv[]);
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainenstatic struct fs *
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainencmd_fs_init(int *argc, char **argv[], int own_arg_count, doveadm_command_t *cmd)
a0c453a8edaec90fb0d945c874de0b1845bc7d7eTimo Sirainen ssl_set.ca_dir = doveadm_settings->ssl_client_ca_dir;
a0c453a8edaec90fb0d945c874de0b1845bc7d7eTimo Sirainen ssl_set.ca_file = doveadm_settings->ssl_client_ca_file;
0fd246126fece57712566c725d6353f255f5fcfaTimo Sirainen if (fs_init((*argv)[1], (*argv)[2], &fs_set, &fs, &error) < 0)
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainen const unsigned char *data;
85a4ae7e8df7ea45a7665828e5edf48a5fc85730Timo Sirainen fs = cmd_fs_init(&argc, &argv, 1, cmd_fs_get);
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen file = fs_file_init(fs, argv[0], FS_OPEN_MODE_READONLY);
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen while ((ret = i_stream_read_data(input, &data, &size, 0)) > 0) {
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainen i_error("%s doesn't exist", fs_file_path(file));
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainen i_error("read(%s) failed: %m", fs_file_path(file));
38a4c09de37bc2ebdc38427a2b958c46dfdcffb1Timo Sirainen fs = cmd_fs_init(&argc, &argv, 2, cmd_fs_put);
0f55802e8fdd95ae4290da6da077819209b71f70Timo Sirainen file = fs_file_init(fs, dest_path, FS_OPEN_MODE_REPLACE);
38a4c09de37bc2ebdc38427a2b958c46dfdcffb1Timo Sirainen input = i_stream_create_file(src_path, IO_BLOCK_SIZE);
38a4c09de37bc2ebdc38427a2b958c46dfdcffb1Timo Sirainen if ((ret = o_stream_send_istream(output, input)) < 0) {
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen if (fs_write_stream_finish(file, &output) < 0) {
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen i_error("fs_write_stream_finish() failed: %s",
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainenstatic void cmd_fs_copy(int argc, char *argv[])
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen fs = cmd_fs_init(&argc, &argv, 2, cmd_fs_copy);
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainen src_file = fs_file_init(fs, src_path, FS_OPEN_MODE_READONLY);
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen dest_file = fs_file_init(fs, dest_path, FS_OPEN_MODE_REPLACE);
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainenstatic void cmd_fs_stat(int argc, char *argv[])
38a4c09de37bc2ebdc38427a2b958c46dfdcffb1Timo Sirainen fs = cmd_fs_init(&argc, &argv, 1, cmd_fs_stat);
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen file = fs_file_init(fs, argv[0], FS_OPEN_MODE_READONLY);
38a4c09de37bc2ebdc38427a2b958c46dfdcffb1Timo Sirainen i_error("%s doesn't exist", fs_file_path(file));
0f55802e8fdd95ae4290da6da077819209b71f70Timo Sirainen fs_file_path(file), fs_file_last_error(file));
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainenstatic void cmd_fs_metadata(int argc, char *argv[])
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen const struct fs_metadata *m;
c37e5edd83ff696d396131f7147ef971cf678911Timo Sirainen fs = cmd_fs_init(&argc, &argv, 1, cmd_fs_metadata);
c37e5edd83ff696d396131f7147ef971cf678911Timo Sirainen file = fs_file_init(fs, argv[0], FS_OPEN_MODE_READONLY);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen i_error("%s doesn't exist", fs_file_path(file));
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen fs_file_path(file), fs_file_last_error(file));
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainenstatic bool cmd_fs_delete_ctx_run(struct fs_delete_ctx *ctx)
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainen unsigned int i;
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainencmd_fs_delete_dir_recursive(struct fs *fs, unsigned int async_count,
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen unsigned int i;
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen ctx.files = t_new(struct fs_file *, ctx.files_count);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen /* delete subdirs first. all fs backends can't handle recursive
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen lookups, so save the list first. */
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen iter = fs_iter_init(fs, path, FS_ITER_FLAG_DIRS);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen while ((fname = fs_iter_next(iter)) != NULL) {
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen /* delete files. again because we're doing this asynchronously finish
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen the iteration first. */
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen if ((fs_get_properties(fs) & FS_PROPERTY_DIRECTORIES) != 0) {
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen /* we need to explicitly delete also the directories */
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen while ((fname = fs_iter_next(iter)) != NULL) {
af3f857bb3166ed99595e11a9d18e5b5cc670e1aTimo Sirainen i_error("fs_wait_async() failed: %s", fs_last_error(fs));
af3f857bb3166ed99595e11a9d18e5b5cc670e1aTimo Sirainen while (doveadm_exit_code == 0 && cmd_fs_delete_ctx_run(&ctx)) {
af3f857bb3166ed99595e11a9d18e5b5cc670e1aTimo Sirainen i_error("fs_wait_async() failed: %s", fs_last_error(fs));
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainencmd_fs_delete_recursive(int argc, char *argv[], unsigned int async_count)
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen fs = cmd_fs_init(&argc, &argv, 1, cmd_fs_delete);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen cmd_fs_delete_dir_recursive(fs, async_count, argv[0]);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainenstatic void cmd_fs_delete(int argc, char *argv[])
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen unsigned int async_count = 0;
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen cmd_fs_delete_recursive(argc, argv, async_count);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen fs = cmd_fs_init(&argc, &argv, 1, cmd_fs_delete);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen file = fs_file_init(fs, argv[0], FS_OPEN_MODE_READONLY);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen i_error("%s doesn't exist", fs_file_path(file));
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen fs_file_path(file), fs_file_last_error(file));
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainenstatic void cmd_fs_iter_full(int argc, char *argv[], enum fs_iter_flags flags,
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainenstatic void cmd_fs_iter(int argc, char *argv[])
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainenstatic void cmd_fs_iter_dirs(int argc, char *argv[])
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen cmd_fs_iter_full(argc, argv, FS_ITER_FLAG_DIRS, cmd_fs_iter_dirs);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen { cmd_fs_get, "fs get", "<fs-driver> <fs-args> <path>" },
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen { cmd_fs_put, "fs put", "<fs-driver> <fs-args> <input path> <path>" },
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen { cmd_fs_copy, "fs copy", "<fs-driver> <fs-args> <source path> <dest path>" },
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen { cmd_fs_stat, "fs stat", "<fs-driver> <fs-args> <path>" },
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen { cmd_fs_metadata, "fs metadata", "<fs-driver> <fs-args> <path>" },
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen { cmd_fs_delete, "fs delete", "[-R] [-n <count>] <fs-driver> <fs-args> <path>" },
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen { cmd_fs_iter, "fs iter", "<fs-driver> <fs-args> <path>" },
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen { cmd_fs_iter_dirs, "fs iter-dirs", "<fs-driver> <fs-args> <path>" },
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainenstatic void fs_cmd_help(doveadm_command_t *cmd)
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen unsigned int i;
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen for (i = 0; i < N_ELEMENTS(doveadm_cmd_fs); i++) {
d0ef8bc2b961a68dd0f75662c2160bd296b9476bTimo Sirainen unsigned int i;
d0ef8bc2b961a68dd0f75662c2160bd296b9476bTimo Sirainen for (i = 0; i < N_ELEMENTS(doveadm_cmd_fs); i++)