bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainen#include "lib.h"
edfd009103881de283e146609f691281aba150aeTimo Sirainen#include "fs-api-private.h"
be6fab2b1eded6b57d6688c6aa5a7c784f943865Aki Tuomi#include "ostream.h"
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenstruct wrapper_fs_iter {
edfd009103881de283e146609f691281aba150aeTimo Sirainen struct fs_iter iter;
edfd009103881de283e146609f691281aba150aeTimo Sirainen struct fs_iter *parent;
edfd009103881de283e146609f691281aba150aeTimo Sirainen};
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenenum fs_properties fs_wrapper_get_properties(struct fs *fs)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen return fs_get_properties(fs->parent);
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenvoid fs_wrapper_file_close(struct fs_file *file)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen fs_file_close(file->parent);
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenconst char *fs_wrapper_file_get_path(struct fs_file *file)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen return fs_file_path(file->parent);
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenvoid fs_wrapper_set_async_callback(struct fs_file *file,
edfd009103881de283e146609f691281aba150aeTimo Sirainen fs_file_async_callback_t *callback,
edfd009103881de283e146609f691281aba150aeTimo Sirainen void *context)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen fs_file_set_async_callback(file->parent, callback, context);
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenvoid fs_wrapper_wait_async(struct fs *fs)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen fs_wait_async(fs->parent);
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenvoid fs_wrapper_set_metadata(struct fs_file *file, const char *key,
edfd009103881de283e146609f691281aba150aeTimo Sirainen const char *value)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen fs_set_metadata(file->parent, key, value);
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenint fs_wrapper_get_metadata(struct fs_file *file,
edfd009103881de283e146609f691281aba150aeTimo Sirainen const ARRAY_TYPE(fs_metadata) **metadata_r)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen return fs_get_metadata(file->parent, metadata_r);
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenbool fs_wrapper_prefetch(struct fs_file *file, uoff_t length)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen return fs_prefetch(file->parent, length);
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenssize_t fs_wrapper_read(struct fs_file *file, void *buf, size_t size)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen return fs_read(file->parent, buf, size);
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenstruct istream *
edfd009103881de283e146609f691281aba150aeTimo Sirainenfs_wrapper_read_stream(struct fs_file *file, size_t max_buffer_size)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen return fs_read_stream(file->parent, max_buffer_size);
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenint fs_wrapper_write(struct fs_file *file, const void *data, size_t size)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen return fs_write(file->parent, data, size);
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenvoid fs_wrapper_write_stream(struct fs_file *file)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen i_assert(file->output == NULL);
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainen file->output = fs_write_stream(file->parent);
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenint fs_wrapper_write_stream_finish(struct fs_file *file, bool success)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
0cf4f39f69167d08b23c720570e0bfbc27e3ac8eTimo Sirainen if (file->output == NULL)
0cf4f39f69167d08b23c720570e0bfbc27e3ac8eTimo Sirainen return fs_write_stream_finish_async(file->parent);
0cf4f39f69167d08b23c720570e0bfbc27e3ac8eTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainen if (!success) {
b8f4e3aa58caa7ee16cf21a21f92521bf05cbdc4Aki Tuomi fs_write_stream_abort_parent(file, &file->output);
edfd009103881de283e146609f691281aba150aeTimo Sirainen return -1;
edfd009103881de283e146609f691281aba150aeTimo Sirainen }
0cf4f39f69167d08b23c720570e0bfbc27e3ac8eTimo Sirainen return fs_write_stream_finish(file->parent, &file->output);
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenint fs_wrapper_lock(struct fs_file *file, unsigned int secs,
edfd009103881de283e146609f691281aba150aeTimo Sirainen struct fs_lock **lock_r)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen return fs_lock(file->parent, secs, lock_r);
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenvoid fs_wrapper_unlock(struct fs_lock *_lock ATTR_UNUSED)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen i_unreached();
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenint fs_wrapper_exists(struct fs_file *file)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen return fs_exists(file->parent);
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenint fs_wrapper_stat(struct fs_file *file, struct stat *st_r)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen return fs_stat(file->parent, st_r);
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
a63cd84128875485e40ed804dbf0b0945526989cTimo Sirainenint fs_wrapper_get_nlinks(struct fs_file *file, nlink_t *nlinks_r)
a63cd84128875485e40ed804dbf0b0945526989cTimo Sirainen{
a63cd84128875485e40ed804dbf0b0945526989cTimo Sirainen return fs_get_nlinks(file->parent, nlinks_r);
a63cd84128875485e40ed804dbf0b0945526989cTimo Sirainen}
a63cd84128875485e40ed804dbf0b0945526989cTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenint fs_wrapper_copy(struct fs_file *src, struct fs_file *dest)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen if (src != NULL)
edfd009103881de283e146609f691281aba150aeTimo Sirainen return fs_copy(src->parent, dest->parent);
edfd009103881de283e146609f691281aba150aeTimo Sirainen else
edfd009103881de283e146609f691281aba150aeTimo Sirainen return fs_copy_finish_async(dest->parent);
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenint fs_wrapper_rename(struct fs_file *src, struct fs_file *dest)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen return fs_rename(src->parent, dest->parent);
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenint fs_wrapper_delete(struct fs_file *file)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen return fs_delete(file->parent);
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
4d71d712b42285afbd2a997f3408142ef0d5f899Timo Sirainenstruct fs_iter *fs_wrapper_iter_alloc(void)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
4d71d712b42285afbd2a997f3408142ef0d5f899Timo Sirainen struct wrapper_fs_iter *iter = i_new(struct wrapper_fs_iter, 1);
4d71d712b42285afbd2a997f3408142ef0d5f899Timo Sirainen return &iter->iter;
4d71d712b42285afbd2a997f3408142ef0d5f899Timo Sirainen}
4d71d712b42285afbd2a997f3408142ef0d5f899Timo Sirainen
4d71d712b42285afbd2a997f3408142ef0d5f899Timo Sirainenvoid fs_wrapper_iter_init(struct fs_iter *_iter, const char *path,
4d71d712b42285afbd2a997f3408142ef0d5f899Timo Sirainen enum fs_iter_flags flags)
4d71d712b42285afbd2a997f3408142ef0d5f899Timo Sirainen{
4d71d712b42285afbd2a997f3408142ef0d5f899Timo Sirainen struct wrapper_fs_iter *iter = (struct wrapper_fs_iter *)_iter;
edfd009103881de283e146609f691281aba150aeTimo Sirainen
6d1218e68ce883735ffde9d7907e626ab81b9fb5Timo Sirainen iter->parent = fs_iter_init_parent(_iter, path, flags);
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenconst char *fs_wrapper_iter_next(struct fs_iter *_iter)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen struct wrapper_fs_iter *iter = (struct wrapper_fs_iter *)_iter;
edfd009103881de283e146609f691281aba150aeTimo Sirainen const char *fname;
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainen iter->parent->async_callback = _iter->async_callback;
edfd009103881de283e146609f691281aba150aeTimo Sirainen iter->parent->async_context = _iter->async_context;
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainen fname = fs_iter_next(iter->parent);
edfd009103881de283e146609f691281aba150aeTimo Sirainen _iter->async_have_more = iter->parent->async_have_more;
edfd009103881de283e146609f691281aba150aeTimo Sirainen return fname;
edfd009103881de283e146609f691281aba150aeTimo Sirainen}
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainenint fs_wrapper_iter_deinit(struct fs_iter *_iter)
edfd009103881de283e146609f691281aba150aeTimo Sirainen{
edfd009103881de283e146609f691281aba150aeTimo Sirainen struct wrapper_fs_iter *iter = (struct wrapper_fs_iter *)_iter;
edfd009103881de283e146609f691281aba150aeTimo Sirainen int ret;
edfd009103881de283e146609f691281aba150aeTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainen ret = fs_iter_deinit(&iter->parent);
edfd009103881de283e146609f691281aba150aeTimo Sirainen i_free(iter);
edfd009103881de283e146609f691281aba150aeTimo Sirainen return ret;
edfd009103881de283e146609f691281aba150aeTimo Sirainen}