changeset 26725:f5c592fad74f

fs-posix: Use container_of()
author Timo Sirainen <timo.sirainen@open-xchange.com>
date Wed, 04 Dec 2019 19:39:10 +0200
parents f9c167f27742
children e3129a5210e4
files src/lib-fs/fs-posix.c
diffstat 1 files changed, 57 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-fs/fs-posix.c	Wed Dec 04 12:30:26 2019 -0500
+++ b/src/lib-fs/fs-posix.c	Wed Dec 04 19:39:10 2019 +0200
@@ -77,7 +77,7 @@
 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 @@
 
 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 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_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_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 @@
 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 @@
 
 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_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 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 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 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 @@
 
 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 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(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(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 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 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_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 @@
 
 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 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_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_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_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_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 @@
 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 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 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) {