]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
fs-posix: Use container_of()
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Wed, 4 Dec 2019 17:39:10 +0000 (19:39 +0200)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Wed, 11 Dec 2019 10:12:30 +0000 (10:12 +0000)
src/lib-fs/fs-posix.c

index fd8dc0543eab991cc376e1bc486d734b8876ed30..747affaaab5a69c65b31ceaa5961126410afb2a2 100644 (file)
@@ -77,7 +77,7 @@ static struct fs *fs_posix_alloc(void)
 static int
 fs_posix_init(struct fs *_fs, const char *args, const struct fs_settings *set)
 {
-       struct posix_fs *fs = (struct posix_fs *)_fs;
+       struct posix_fs *fs = container_of(_fs, struct posix_fs, fs);
        const char *const *tmp;
 
        fs->temp_file_prefix = set->temp_file_prefix != NULL ?
@@ -130,7 +130,7 @@ fs_posix_init(struct fs *_fs, const char *args, const struct fs_settings *set)
 
 static void fs_posix_deinit(struct fs *_fs)
 {
-       struct posix_fs *fs = (struct posix_fs *)_fs;
+       struct posix_fs *fs = container_of(_fs, struct posix_fs, fs);
 
        i_free(fs->temp_file_prefix);
        i_free(fs->root_path);
@@ -140,7 +140,7 @@ static void fs_posix_deinit(struct fs *_fs)
 
 static enum fs_properties fs_posix_get_properties(struct fs *_fs)
 {
-       struct posix_fs *fs = (struct posix_fs *)_fs;
+       struct posix_fs *fs = container_of(_fs, struct posix_fs, fs);
        enum fs_properties props =
                FS_PROPERTY_LOCKS | FS_PROPERTY_FASTCOPY | FS_PROPERTY_RENAME |
                FS_PROPERTY_STAT | FS_PROPERTY_ITER | FS_PROPERTY_RELIABLEITER;
@@ -242,7 +242,7 @@ static int fs_posix_rmdir_parents(struct posix_fs *fs, const char *path)
 
 static int fs_posix_create(struct posix_fs_file *file)
 {
-       struct posix_fs *fs = (struct posix_fs *)file->file.fs;
+       struct posix_fs *fs = container_of(file->file.fs, struct posix_fs, fs);
        string_t *str = t_str_new(256);
        const char *slash;
        unsigned int try_count = 0;
@@ -280,7 +280,7 @@ static int fs_posix_create(struct posix_fs_file *file)
 
 static int fs_posix_open(struct posix_fs_file *file)
 {
-       struct posix_fs *fs = (struct posix_fs *)file->file.fs;
+       struct posix_fs *fs = container_of(file->file.fs, struct posix_fs, fs);
        const char *path = file->full_path;
 
        i_assert(file->fd == -1);
@@ -319,8 +319,9 @@ static void
 fs_posix_file_init(struct fs_file *_file, const char *path,
                   enum fs_open_mode mode, enum fs_open_flags flags)
 {
-       struct posix_fs_file *file = (struct posix_fs_file *)_file;
-       struct posix_fs *fs = (struct posix_fs *)_file->fs;
+       struct posix_fs_file *file =
+               container_of(_file, struct posix_fs_file, file);
+       struct posix_fs *fs = container_of(_file->fs, struct posix_fs, fs);
        guid_128_t guid;
        size_t path_len = strlen(path);
 
@@ -346,7 +347,8 @@ fs_posix_file_init(struct fs_file *_file, const char *path,
 
 static void fs_posix_file_close(struct fs_file *_file)
 {
-       struct posix_fs_file *file = (struct posix_fs_file *)_file;
+       struct posix_fs_file *file =
+               container_of(_file, struct posix_fs_file, file);
 
        if (file->fd != -1 && file->file.output == NULL) {
                if (close(file->fd) < 0) {
@@ -359,7 +361,8 @@ static void fs_posix_file_close(struct fs_file *_file)
 
 static void fs_posix_file_deinit(struct fs_file *_file)
 {
-       struct posix_fs_file *file = (struct posix_fs_file *)_file;
+       struct posix_fs_file *file =
+               container_of(_file, struct posix_fs_file, file);
 
        i_assert(_file->output == NULL);
 
@@ -400,7 +403,8 @@ static int fs_posix_open_for_read(struct posix_fs_file *file)
 
 static bool fs_posix_prefetch(struct fs_file *_file, uoff_t length ATTR_UNUSED)
 {
-       struct posix_fs_file *file = (struct posix_fs_file *)_file;
+       struct posix_fs_file *file =
+               container_of(_file, struct posix_fs_file, file);
 
        if (fs_posix_open_for_read(file) < 0)
                return TRUE;
@@ -417,7 +421,8 @@ static bool fs_posix_prefetch(struct fs_file *_file, uoff_t length ATTR_UNUSED)
 
 static ssize_t fs_posix_read(struct fs_file *_file, void *buf, size_t size)
 {
-       struct posix_fs_file *file = (struct posix_fs_file *)_file;
+       struct posix_fs_file *file =
+               container_of(_file, struct posix_fs_file, file);
        ssize_t ret;
 
        if (fs_posix_open_for_read(file) < 0)
@@ -442,7 +447,8 @@ static ssize_t fs_posix_read(struct fs_file *_file, void *buf, size_t size)
 static struct istream *
 fs_posix_read_stream(struct fs_file *_file, size_t max_buffer_size)
 {
-       struct posix_fs_file *file = (struct posix_fs_file *)_file;
+       struct posix_fs_file *file =
+               container_of(_file, struct posix_fs_file, file);
        struct istream *input;
        int fd_dup;
 
@@ -463,7 +469,7 @@ fs_posix_read_stream(struct fs_file *_file, size_t max_buffer_size)
 
 static void fs_posix_write_rename_if_needed(struct posix_fs_file *file)
 {
-       struct posix_fs *fs = (struct posix_fs *)file->file.fs;
+       struct posix_fs *fs = container_of(file->file.fs, struct posix_fs, fs);
        const char *new_fname;
 
        new_fname = fs_metadata_find(&file->file.metadata, FS_METADATA_WRITE_FNAME);
@@ -480,7 +486,7 @@ static void fs_posix_write_rename_if_needed(struct posix_fs_file *file)
 
 static int fs_posix_write_finish_link(struct posix_fs_file *file)
 {
-       struct posix_fs *fs = (struct posix_fs *)file->file.fs;
+       struct posix_fs *fs = container_of(file->file.fs, struct posix_fs, fs);
        unsigned int try_count = 0;
        int ret;
 
@@ -501,7 +507,7 @@ static int fs_posix_write_finish_link(struct posix_fs_file *file)
 
 static int fs_posix_write_finish(struct posix_fs_file *file)
 {
-       struct posix_fs *fs = (struct posix_fs *)file->file.fs;
+       struct posix_fs *fs = container_of(file->file.fs, struct posix_fs, fs);
        unsigned int try_count = 0;
        int ret, old_errno;
 
@@ -573,7 +579,8 @@ static int fs_posix_write_finish(struct posix_fs_file *file)
 
 static int fs_posix_write(struct fs_file *_file, const void *data, size_t size)
 {
-       struct posix_fs_file *file = (struct posix_fs_file *)_file;
+       struct posix_fs_file *file =
+               container_of(_file, struct posix_fs_file, file);
        ssize_t ret;
 
        if (file->fd == -1) {
@@ -609,7 +616,8 @@ static int fs_posix_write(struct fs_file *_file, const void *data, size_t size)
 
 static void fs_posix_write_stream(struct fs_file *_file)
 {
-       struct posix_fs_file *file = (struct posix_fs_file *)_file;
+       struct posix_fs_file *file =
+               container_of(_file, struct posix_fs_file, file);
 
        i_assert(_file->output == NULL);
 
@@ -629,7 +637,8 @@ static void fs_posix_write_stream(struct fs_file *_file)
 
 static int fs_posix_write_stream_finish(struct fs_file *_file, bool success)
 {
-       struct posix_fs_file *file = (struct posix_fs_file *)_file;
+       struct posix_fs_file *file =
+               container_of(_file, struct posix_fs_file, file);
        int ret = success ? 0 : -1;
 
        o_stream_destroy(&_file->output);
@@ -657,8 +666,9 @@ static int fs_posix_write_stream_finish(struct fs_file *_file, bool success)
 static int
 fs_posix_lock(struct fs_file *_file, unsigned int secs, struct fs_lock **lock_r)
 {
-       struct posix_fs_file *file = (struct posix_fs_file *)_file;
-       struct posix_fs *fs = (struct posix_fs *)_file->fs;
+       struct posix_fs_file *file =
+               container_of(_file, struct posix_fs_file, file);
+       struct posix_fs *fs = container_of(_file->fs, struct posix_fs, fs);
        struct dotlock_settings dotlock_set;
        struct posix_fs_lock fs_lock, *ret_lock;
        int ret = -1;
@@ -715,7 +725,8 @@ fs_posix_lock(struct fs_file *_file, unsigned int secs, struct fs_lock **lock_r)
 
 static void fs_posix_unlock(struct fs_lock *_lock)
 {
-       struct posix_fs_lock *lock = (struct posix_fs_lock *)_lock;
+       struct posix_fs_lock *lock =
+               container_of(_lock, struct posix_fs_lock, lock);
 
        if (lock->file_lock != NULL)
                file_unlock(&lock->file_lock);
@@ -726,7 +737,8 @@ static void fs_posix_unlock(struct fs_lock *_lock)
 
 static int fs_posix_exists(struct fs_file *_file)
 {
-       struct posix_fs_file *file = (struct posix_fs_file *)_file;
+       struct posix_fs_file *file =
+               container_of(_file, struct posix_fs_file, file);
        struct stat st;
 
        if (stat(file->full_path, &st) < 0) {
@@ -742,7 +754,8 @@ static int fs_posix_exists(struct fs_file *_file)
 
 static int fs_posix_stat(struct fs_file *_file, struct stat *st_r)
 {
-       struct posix_fs_file *file = (struct posix_fs_file *)_file;
+       struct posix_fs_file *file =
+               container_of(_file, struct posix_fs_file, file);
 
        /* in case output != NULL it means that we're still writing to the file
           and fs_stat() shouldn't stat the unfinished file. this is done by
@@ -763,9 +776,11 @@ static int fs_posix_stat(struct fs_file *_file, struct stat *st_r)
 
 static int fs_posix_copy(struct fs_file *_src, struct fs_file *_dest)
 {
-       struct posix_fs_file *src = (struct posix_fs_file *)_src;
-       struct posix_fs_file *dest = (struct posix_fs_file *)_dest;
-       struct posix_fs *fs = (struct posix_fs *)_src->fs;
+       struct posix_fs_file *src =
+               container_of(_src, struct posix_fs_file, file);
+       struct posix_fs_file *dest =
+               container_of(_dest, struct posix_fs_file, file);
+       struct posix_fs *fs = container_of(_src->fs, struct posix_fs, fs);
        unsigned int try_count = 0;
        int ret;
 
@@ -793,9 +808,11 @@ static int fs_posix_copy(struct fs_file *_src, struct fs_file *_dest)
 
 static int fs_posix_rename(struct fs_file *_src, struct fs_file *_dest)
 {
-       struct posix_fs *fs = (struct posix_fs *)_src->fs;
-       struct posix_fs_file *src = (struct posix_fs_file *)_src;
-       struct posix_fs_file *dest = (struct posix_fs_file *)_dest;
+       struct posix_fs *fs = container_of(_src->fs, struct posix_fs, fs);
+       struct posix_fs_file *src =
+               container_of(_src, struct posix_fs_file, file);
+       struct posix_fs_file *dest =
+               container_of(_dest, struct posix_fs_file, file);
        unsigned int try_count = 0;
        int ret;
 
@@ -817,8 +834,9 @@ static int fs_posix_rename(struct fs_file *_src, struct fs_file *_dest)
 
 static int fs_posix_delete(struct fs_file *_file)
 {
-       struct posix_fs_file *file = (struct posix_fs_file *)_file;
-       struct posix_fs *fs = (struct posix_fs *)_file->fs;
+       struct posix_fs_file *file =
+               container_of(_file, struct posix_fs_file, file);
+       struct posix_fs *fs = container_of(_file->fs, struct posix_fs, fs);
 
        if (unlink(file->full_path) < 0) {
                if (!UNLINK_EISDIR(errno)) {
@@ -846,8 +864,9 @@ static void
 fs_posix_iter_init(struct fs_iter *_iter, const char *path,
                   enum fs_iter_flags flags ATTR_UNUSED)
 {
-       struct posix_fs_iter *iter = (struct posix_fs_iter *)_iter;
-       struct posix_fs *fs = (struct posix_fs *)_iter->fs;
+       struct posix_fs_iter *iter =
+               container_of(_iter, struct posix_fs_iter, iter);
+       struct posix_fs *fs = container_of(_iter->fs, struct posix_fs, fs);
 
        iter->path = fs->path_prefix == NULL ? i_strdup(path) :
                i_strconcat(fs->path_prefix, path, NULL);
@@ -883,8 +902,9 @@ static bool fs_posix_iter_want(struct posix_fs_iter *iter, const char *fname)
 
 static const char *fs_posix_iter_next(struct fs_iter *_iter)
 {
-       struct posix_fs_iter *iter = (struct posix_fs_iter *)_iter;
-       struct posix_fs *fs = (struct posix_fs *)_iter->fs;
+       struct posix_fs_iter *iter =
+               container_of(_iter, struct posix_fs_iter, iter);
+       struct posix_fs *fs = container_of(_iter->fs, struct posix_fs, fs);
        struct dirent *d;
 
        if (iter->dir == NULL)
@@ -927,7 +947,8 @@ static const char *fs_posix_iter_next(struct fs_iter *_iter)
 
 static int fs_posix_iter_deinit(struct fs_iter *_iter)
 {
-       struct posix_fs_iter *iter = (struct posix_fs_iter *)_iter;
+       struct posix_fs_iter *iter =
+               container_of(_iter, struct posix_fs_iter, iter);
        int ret = 0;
 
        if (iter->dir != NULL && closedir(iter->dir) < 0 && iter->err == 0) {