]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-fs: metawrap - Use container_of() instead of casts
authorJosef 'Jeff' Sipek <jeff.sipek@open-xchange.com>
Tue, 21 Jul 2020 17:05:34 +0000 (13:05 -0400)
committerjeff.sipek <jeff.sipek@open-xchange.com>
Tue, 4 Aug 2020 22:11:58 +0000 (22:11 +0000)
Being more explicit not only makes the code more explict, it makes it safer.

src/lib-fs/fs-metawrap.c

index 7b3d1b1b997896e0673df02ba38f108f88b92645..e8b39dface07a3c455e491c2bd78926f690ac42c 100644 (file)
@@ -33,6 +33,9 @@ struct metawrap_fs_file {
        bool metadata_changed_since_write;
 };
 
+#define METAWRAP_FS(ptr)       container_of((ptr), struct metawrap_fs, fs)
+#define METAWRAP_FILE(ptr)     container_of((ptr), struct metawrap_fs_file, file)
+
 static struct fs *fs_metawrap_alloc(void)
 {
        struct metawrap_fs *fs;
@@ -46,7 +49,7 @@ static int
 fs_metawrap_init(struct fs *_fs, const char *args,
                 const struct fs_settings *set, const char **error_r)
 {
-       struct metawrap_fs *fs = (struct metawrap_fs *)_fs;
+       struct metawrap_fs *fs = METAWRAP_FS(_fs);
        const char *parent_name, *parent_args;
 
        if (*args == '\0') {
@@ -71,7 +74,7 @@ fs_metawrap_init(struct fs *_fs, const char *args,
 
 static void fs_metawrap_free(struct fs *_fs)
 {
-       struct metawrap_fs *fs = (struct metawrap_fs *)_fs;
+       struct metawrap_fs *fs = METAWRAP_FS(_fs);
 
        fs_deinit(&_fs->parent);
        i_free(fs);
@@ -79,7 +82,7 @@ static void fs_metawrap_free(struct fs *_fs)
 
 static enum fs_properties fs_metawrap_get_properties(struct fs *_fs)
 {
-       const struct metawrap_fs *fs = (const struct metawrap_fs *)_fs;
+       const struct metawrap_fs *fs = METAWRAP_FS(_fs);
        enum fs_properties props;
 
        props = fs_get_properties(_fs->parent);
@@ -103,8 +106,8 @@ static void
 fs_metawrap_file_init(struct fs_file *_file, const char *path,
                      enum fs_open_mode mode, enum fs_open_flags flags)
 {
-       struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
-       struct metawrap_fs *fs = (struct metawrap_fs *)_file->fs;
+       struct metawrap_fs_file *file = METAWRAP_FILE(_file);
+       struct metawrap_fs *fs = METAWRAP_FS(_file->fs);
 
        file->file.path = i_strdup(path);
        file->fs = fs;
@@ -129,7 +132,7 @@ fs_metawrap_file_init(struct fs_file *_file, const char *path,
 
 static void fs_metawrap_file_deinit(struct fs_file *_file)
 {
-       struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
+       struct metawrap_fs_file *file = METAWRAP_FILE(_file);
 
        if (file->super_read != _file->parent)
                fs_file_deinit(&file->super_read);
@@ -141,7 +144,7 @@ static void fs_metawrap_file_deinit(struct fs_file *_file)
 
 static void fs_metawrap_file_close(struct fs_file *_file)
 {
-       struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
+       struct metawrap_fs_file *file = METAWRAP_FILE(_file);
 
        i_stream_unref(&file->input);
        fs_file_close(file->super_read);
@@ -152,7 +155,7 @@ static void
 fs_metawrap_set_metadata(struct fs_file *_file, const char *key,
                         const char *value)
 {
-       struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
+       struct metawrap_fs_file *file = METAWRAP_FILE(_file);
 
        if (!file->fs->wrap_metadata ||
            strcmp(key, FS_METADATA_WRITE_FNAME) == 0)
@@ -168,7 +171,7 @@ fs_metawrap_get_metadata(struct fs_file *_file,
                         enum fs_get_metadata_flags flags,
                         const ARRAY_TYPE(fs_metadata) **metadata_r)
 {
-       struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
+       struct metawrap_fs_file *file = METAWRAP_FILE(_file);
        ssize_t ret;
        char c;
 
@@ -206,7 +209,7 @@ fs_metawrap_get_metadata(struct fs_file *_file,
 
 static bool fs_metawrap_prefetch(struct fs_file *_file, uoff_t length)
 {
-       struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
+       struct metawrap_fs_file *file = METAWRAP_FILE(_file);
 
        if (!file->fs->wrap_metadata)
                return fs_prefetch(_file->parent, length);
@@ -216,7 +219,7 @@ static bool fs_metawrap_prefetch(struct fs_file *_file, uoff_t length)
 
 static ssize_t fs_metawrap_read(struct fs_file *_file, void *buf, size_t size)
 {
-       struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
+       struct metawrap_fs_file *file = METAWRAP_FILE(_file);
 
        if (!file->fs->wrap_metadata)
                return fs_read(_file->parent, buf, size);
@@ -264,7 +267,7 @@ fs_metawrap_read_stream(struct fs_file *_file, size_t max_buffer_size)
 
 static int fs_metawrap_write(struct fs_file *_file, const void *data, size_t size)
 {
-       struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
+       struct metawrap_fs_file *file = METAWRAP_FILE(_file);
 
        if (!file->fs->wrap_metadata)
                return fs_write(_file->parent, data, size);
@@ -359,7 +362,7 @@ fs_metawrap_create_updated_istream(struct metawrap_fs_file *file,
 
 static int fs_metawrap_write_stream_finish(struct fs_file *_file, bool success)
 {
-       struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
+       struct metawrap_fs_file *file = METAWRAP_FILE(_file);
        struct istream *input;
        int ret;
 
@@ -425,7 +428,7 @@ static int fs_metawrap_write_stream_finish(struct fs_file *_file, bool success)
 
 static int fs_metawrap_stat(struct fs_file *_file, struct stat *st_r)
 {
-       struct metawrap_fs_file *file = (struct metawrap_fs_file *)_file;
+       struct metawrap_fs_file *file = METAWRAP_FILE(_file);
        struct istream *input;
        uoff_t input_size;
        ssize_t ret;
@@ -480,7 +483,7 @@ static int fs_metawrap_stat(struct fs_file *_file, struct stat *st_r)
 
 static int fs_metawrap_copy(struct fs_file *_src, struct fs_file *_dest)
 {
-       struct metawrap_fs_file *dest = (struct metawrap_fs_file *)_dest;
+       struct metawrap_fs_file *dest = METAWRAP_FILE(_dest);
 
        if (!dest->fs->wrap_metadata || !_dest->metadata_changed)
                return fs_wrapper_copy(_src, _dest);