]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
global: use i_close_fd{,_path}() instead of open-coding them
authorJosef 'Jeff' Sipek <jeff.sipek@dovecot.fi>
Tue, 19 Sep 2017 12:54:35 +0000 (15:54 +0300)
committerTimo Sirainen <tss@dovecot.fi>
Tue, 3 Oct 2017 07:14:05 +0000 (10:14 +0300)
30 files changed:
src/auth/auth-master-connection.c
src/auth/auth-postfix-connection.c
src/auth/db-checkpassword.c
src/auth/db-passwd-file.c
src/doveadm/server-connection.c
src/lib-auth/auth-master.c
src/lib-dict/dict-cdb.c
src/lib-dict/dict-file.c
src/lib-dns/dns-lookup.c
src/lib-imap-client/imapc-connection.c
src/lib-index/mailbox-log.c
src/lib-master/master-login.c
src/lib-master/master-service.c
src/lib-stats/stats-connection.c
src/lib-storage/index/imapc/imapc-mail-fetch.c
src/lib-storage/index/imapc/imapc-mail.c
src/lib-storage/index/imapc/imapc-save.c
src/lib-storage/index/imapc/imapc-storage.c
src/lib-storage/index/pop3c/pop3c-client.c
src/lib/ioloop-notify-inotify.c
src/lmtp/client.c
src/login-common/login-proxy-state.c
src/plugins/fts-squat/squat-trie.c
src/plugins/fts-squat/squat-uidlist.c
src/plugins/mail-filter/istream-ext-filter.c
src/plugins/mail-filter/ostream-ext-filter.c
src/plugins/quota/quota-fs.c
src/plugins/quota/quota-maildir.c
src/plugins/replication/replication-plugin.c
src/util/tcpwrap.c

index 7f3d7f36f60b14822855f2b2104486b5b55b7ad5..0f0c9eee9f00cdd40f4294ef383450bf15999207 100644 (file)
@@ -787,11 +787,7 @@ void auth_master_connection_destroy(struct auth_master_connection **_conn)
        if (conn->output != NULL)
                o_stream_close(conn->output);
        io_remove(&conn->io);
-       if (conn->fd != -1) {
-               if (close(conn->fd) < 0)
-                       i_error("close(%s): %m", conn->path);
-               conn->fd = -1;
-       }
+       i_close_fd_path(&conn->fd, conn->path);
 
        master_service_client_connection_destroyed(master_service);
        auth_master_connection_unref(&conn);
index 9ab0adeab7028b9c654443f5fd043d98847027db..0abe498b4a43ee146299edc4f363dd3ba2c9ed81 100644 (file)
@@ -201,11 +201,7 @@ auth_postfix_connection_destroy(struct auth_postfix_connection **_conn)
        if (conn->output != NULL)
                o_stream_close(conn->output);
        io_remove(&conn->io);
-       if (conn->fd != -1) {
-               if (close(conn->fd) < 0)
-                       i_error("close(%s): %m", conn->path);
-               conn->fd = -1;
-       }
+       i_close_fd_path(&conn->fd, conn->path);
 
        master_service_client_connection_destroyed(master_service);
        auth_postfix_connection_unref(&conn);
index 1f2ebb03ae42873337119e5f77de33783c11b842..9d89a9d7871f5722c3507e73c4f05eb5d58e7ff5 100644 (file)
@@ -66,15 +66,8 @@ static void checkpassword_request_close(struct chkpw_auth_request *request)
        io_remove(&request->io_in);
        io_remove(&request->io_out);
 
-       if (request->fd_in != -1) {
-               if (close(request->fd_in) < 0)
-                       i_error("checkpassword: close() failed: %m");
-               request->fd_in = -1;
-       }
-       if (request->fd_out != -1) {
-               if (close(request->fd_out) < 0)
-                       i_error("checkpassword: close() failed: %m");
-       }
+       i_close_fd(&request->fd_in);
+       i_close_fd(&request->fd_out);
 }
 
 static void checkpassword_request_free(struct chkpw_auth_request **_request)
index 8aa3abb2ea2c1d83b62a91e1f4f3921c4b7b358b..ff6bebfc80e16b60f1098f187571e28eb6208913 100644 (file)
@@ -234,11 +234,7 @@ static int passwd_file_open(struct passwd_file *pw, bool startup,
 
 static void passwd_file_close(struct passwd_file *pw)
 {
-       if (pw->fd != -1) {
-               if (close(pw->fd) < 0)
-                       i_error("passwd-file %s: close() failed: %m", pw->path);
-               pw->fd = -1;
-       }
+       i_close_fd_path(&pw->fd, pw->path);
 
        if (hash_table_is_created(pw->users))
                hash_table_destroy(&pw->users);
index 2c8039572823618a8176f11b9cd5cafcb14911bb..c22d60c93ec96fc7d7a242f95f67d2a8b27bc90d 100644 (file)
@@ -559,10 +559,7 @@ void server_connection_destroy(struct server_connection **_conn)
        if (conn->ssl_iostream != NULL)
                ssl_iostream_unref(&conn->ssl_iostream);
        io_remove(&conn->io);
-       if (conn->fd != -1) {
-               if (close(conn->fd) < 0)
-                       i_error("close(server) failed: %m");
-       }
+       i_close_fd(&conn->fd);
        pool_unref(&conn->pool);
 }
 
index 85b0790e34b44a956a9c9559152a7486590c959c..b4f3a30e421ebcc6aec43c329a0a4a56aa631898 100644 (file)
@@ -83,11 +83,7 @@ auth_master_init(const char *auth_socket_path, enum auth_master_flags flags)
 static void auth_connection_close(struct auth_master_connection *conn)
 {
        timeout_remove(&conn->to);
-       if (conn->fd != -1) {
-               if (close(conn->fd) < 0)
-                       i_error("close(%s) failed: %m", conn->auth_socket_path);
-               conn->fd = -1;
-       }
+       i_close_fd_path(&conn->fd, conn->auth_socket_path);
 
        conn->sent_handshake = FALSE;
        conn->handshaked = FALSE;
index 1c23a784c735369c451a81deea963c3d3f527ba2..cb66cb0f4305817f6398be8b93e9dfc4fc2311f5 100644 (file)
@@ -76,10 +76,7 @@ static void cdb_dict_deinit(struct dict *_dict)
        /* we can safely deinit unallocated cdb */
        cdb_free(&dict->cdb);
 
-       if (dict->fd != -1) {
-               if (close(dict->fd) < 0)
-                       i_error("close(%s) failed: %m", dict->path);
-       }
+       i_close_fd_path(&dict->fd, dict->path);
 
        i_free(dict->path);
        i_free(dict);
index a35a9c480e831bec906ea7030665a70250fffbe5..6c16f1d435626f2cba7d52553fde7e1ba2eff0b0 100644 (file)
@@ -96,10 +96,7 @@ static void file_dict_deinit(struct dict *_dict)
 {
        struct file_dict *dict = (struct file_dict *)_dict;
 
-       if (dict->fd != -1) {
-               if (close(dict->fd) < 0)
-                       i_error("close(%s) failed: %m", dict->path);
-       }
+       i_close_fd_path(&dict->fd, dict->path);
        hash_table_destroy(&dict->hash);
        pool_unref(&dict->hash_pool);
        i_free(dict->path);
@@ -149,10 +146,7 @@ static int file_dict_open_latest(struct file_dict *dict, const char **error_r)
        if (!file_dict_need_refresh(dict))
                return 0;
 
-       if (dict->fd != -1) {
-               if (close(dict->fd) < 0)
-                       i_error("close(%s) failed: %m", dict->path);
-       }
+       i_close_fd_path(&dict->fd, dict->path);
 
        open_type = dict->lock_method == FILE_LOCK_METHOD_DOTLOCK ?
                O_RDONLY : O_RDWR;
index abe83a6cc8bcebfd929111585284939295fe7b97..ed6376ab287556df355dcc53546a7a3e84c33e1c 100644 (file)
@@ -62,11 +62,7 @@ static void dns_client_disconnect(struct dns_client *client, const char *error)
        timeout_remove(&client->to_idle);
        io_remove(&client->io);
        i_stream_destroy(&client->input);
-       if (client->fd != -1) {
-               if (close(client->fd) < 0)
-                       i_error("close(%s) failed: %m", client->path);
-               client->fd = -1;
-       }
+       i_close_fd_path(&client->fd, client->path);
 
        i_zero(&result);
        result.ret = EAI_FAIL;
index dde95b66bc1fe20f1ecd5467186fa6ba17fcca9b..c3a9fe6f9c01ad1a3a4bc0a3310a1097ce03513a 100644 (file)
@@ -412,10 +412,7 @@ static void imapc_connection_lfiles_free(struct imapc_connection *conn)
 static void
 imapc_connection_literal_reset(struct imapc_connection_literal *literal)
 {
-       if (literal->fd != -1) {
-               if (close(literal->fd) < 0)
-                       i_error("close(%s) failed: %m", literal->temp_path);
-       }
+       i_close_fd_path(&literal->fd, literal->temp_path);
        i_free_and_null(literal->temp_path);
 
        i_zero(literal);
index 3df958ccaf6259842ab3a475e6ea23a2e17d285e..90496757b8a141810836c1a47699109ae2157116 100644 (file)
@@ -72,11 +72,7 @@ void mailbox_log_free(struct mailbox_log **_log)
 
 static void mailbox_log_close(struct mailbox_log *log)
 {
-       if (log->fd != -1) {
-               if (close(log->fd) < 0)
-                       i_error("close(%s) failed: %m", log->filepath);
-               log->fd = -1;
-       }
+       i_close_fd_path(&log->fd, log->filepath);
 }
 
 void mailbox_log_set_permissions(struct mailbox_log *log, mode_t mode,
@@ -199,11 +195,7 @@ int mailbox_log_append(struct mailbox_log *log,
 
 static bool mailbox_log_iter_open_next(struct mailbox_log_iter *iter)
 {
-       if (iter->fd != -1) {
-               if (close(iter->fd) < 0)
-                       i_error("close(%s) failed: %m", iter->filepath);
-               iter->fd = -1;
-       }
+       i_close_fd_path(&iter->fd, iter->filepath);
        if (iter->filepath == NULL)
                iter->filepath = iter->log->filepath2;
        else if (iter->filepath == iter->log->filepath2)
@@ -284,10 +276,7 @@ int mailbox_log_iter_deinit(struct mailbox_log_iter **_iter)
 
        *_iter = NULL;
 
-       if (iter->fd != -1) {
-               if (close(iter->fd) < 0)
-                       i_error("close(%s) failed: %m", iter->filepath);
-       }
+       i_close_fd_path(&iter->fd, iter->filepath);
        i_free(iter);
        return ret;
 }
index 60e8bfcbd9a1afd20751bc34823a3f20151943f7..ac0181d126c5e0d58fc94748136116ec840b0723 100644 (file)
@@ -416,10 +416,7 @@ static void master_login_conn_input(struct master_login_connection *conn)
                        master_login_conn_close(conn);
                        master_login_conn_unref(&conn);
                }
-               if (client_fd != -1) {
-                       if (close(client_fd) < 0)
-                               i_error("close(fd_read client) failed: %m");
-               }
+               i_close_fd(&client_fd);
                return;
        }
        fd_close_on_exec(client_fd, TRUE);
index 4c669074e280ef1d8b102b75b0c91a4a3ac95e33..d1e77071199c688431ad4c74a55bdf9a72ae134e 100644 (file)
@@ -932,11 +932,7 @@ static void master_service_refresh_login_state(struct master_service *service)
 
 void master_service_close_config_fd(struct master_service *service)
 {
-       if (service->config_fd != -1) {
-               if (close(service->config_fd) < 0)
-                       i_error("close(master config fd) failed: %m");
-               service->config_fd = -1;
-       }
+       i_close_fd(&service->config_fd);
 }
 
 void master_service_deinit(struct master_service **_service)
index 459e7c7827a6fdf28f082ebda37161eb22e2e3c1..15ce2ff6f4f18f42b941229ea88e5477bcbe0f38 100644 (file)
@@ -61,10 +61,7 @@ void stats_connection_unref(struct stats_connection **_conn)
                return;
 
        *_conn = NULL;
-       if (conn->fd != -1) {
-               if (close(conn->fd) < 0)
-                       i_error("close(%s) failed: %m", conn->path);
-       }
+       i_close_fd_path(&conn->fd, conn->path);
        i_free(conn->path);
        i_free(conn);
 }
index a86660355e23f96ef44d5fd9dfed125c5c4fd88a..7c1abe822a7b5fd938ccf46d5102cdfe5a1d40ec 100644 (file)
@@ -625,11 +625,7 @@ imapc_fetch_stream(struct imapc_mail *mail,
                        hdr_stream = i_stream_create_fd_autoclose(&mail->fd, 0);
                }
                index_mail_close_streams(imail);
-               if (mail->fd != -1) {
-                       if (close(mail->fd) < 0)
-                               i_error("close(imapc mail) failed: %m");
-                       mail->fd = -1;
-               }
+               i_close_fd(&mail->fd);
        } else {
                if (!have_header) {
                        /* BODY.PEEK[TEXT] received - we can't currently handle
index 7059f2e8d5e31f40d28088b1da900ef086e02f3e..d422eda014751f6f6fbb0cd1e2163188ba635e66 100644 (file)
@@ -449,11 +449,7 @@ static void imapc_mail_close(struct mail *_mail)
                        mail->body = NULL;
                }
        }
-       if (mail->fd != -1) {
-               if (close(mail->fd) < 0)
-                       i_error("close(imapc mail) failed: %m");
-               mail->fd = -1;
-       }
+       i_close_fd(&mail->fd);
        buffer_free(&mail->body);
        mail->header_fetched = FALSE;
        mail->body_fetched = FALSE;
index 7d98a3305c1e7ff776276d5104393df8fb271f6e..4876d0c856af6cd549c0fc2817d34e8f7148c24d 100644 (file)
@@ -291,11 +291,7 @@ int imapc_save_finish(struct mail_save_context *_ctx)
 
        o_stream_unref(&_ctx->data.output);
        i_stream_unref(&ctx->input);
-       if (ctx->fd != -1) {
-               if (close(ctx->fd) < 0)
-                       i_error("close(%s) failed: %m", ctx->temp_path);
-               ctx->fd = -1;
-       }
+       i_close_fd_path(&ctx->fd, ctx->temp_path);
        i_free(ctx->temp_path);
        index_save_context_free(_ctx);
        return ctx->failed ? -1 : 0;
index d7332f657a2f50dcc92620508e6b85918065483a..98585f90dc0c2a5a2bf62891419a5de6af52277f 100644 (file)
@@ -763,11 +763,7 @@ static int imapc_mailbox_open(struct mailbox *box)
 
 void imapc_mail_cache_free(struct imapc_mail_cache *cache)
 {
-       if (cache->fd != -1) {
-               if (close(cache->fd) < 0)
-                       i_error("close(imapc cached mail) failed: %m");
-               cache->fd = -1;
-       }
+       i_close_fd(&cache->fd);
        buffer_free(&cache->buf);
        cache->uid = 0;
 }
index b51c622e42eae62a3eb9cf54e268bb33188ab28e..0197626478d2d5d87e3664d64ca519d348d7468d 100644 (file)
@@ -205,11 +205,7 @@ static void pop3c_client_disconnect(struct pop3c_client *client)
        o_stream_destroy(&client->output);
        if (client->ssl_iostream != NULL)
                ssl_iostream_unref(&client->ssl_iostream);
-       if (client->fd != -1) {
-               if (close(client->fd) < 0)
-                       i_error("close(pop3c) failed: %m");
-               client->fd = -1;
-       }
+       i_close_fd(&client->fd);
        while (array_count(&client->commands) > 0)
                pop3c_client_async_callback_disconnected(client);
        client_login_callback(client, POP3C_COMMAND_STATE_DISCONNECTED,
index 7cc93fbed4968d35efc2b3b650beef61568bfe02..1cef6ceac56fd71f72a303baf4b1ad5a03cd3f11 100644 (file)
@@ -206,11 +206,7 @@ void io_loop_notify_handler_deinit(struct ioloop *ioloop)
                io_remove(&_io);
        }
 
-       if (ctx->inotify_fd != -1) {
-               if (close(ctx->inotify_fd) < 0)
-                       i_error("close(inotify) failed: %m");
-               ctx->inotify_fd = -1;
-       }
+       i_close_fd(&ctx->inotify_fd);
        i_free(ctx);
 }
 
index 5a47bb4d1fc158b82cb69dea1eb70ca64af97dc6..0d14e708a7b7ca0a38ca6f610d5a282d79f644c9 100644 (file)
@@ -381,10 +381,7 @@ void client_state_reset(struct client *client, const char *state_name)
 
        buffer_free(&client->state.mail_data);
        o_stream_unref(&client->state.mail_data_output);
-       if (client->state.mail_data_fd != -1) {
-               if (close(client->state.mail_data_fd) < 0)
-                       i_error("close(mail data fd) failed: %m");
-       }
+       i_close_fd(&client->state.mail_data_fd);
 
        i_zero(&client->state);
        p_clear(client->state_pool);
index 05509ed35685c869d28749c9f786dcb0d821ef87..9815e33d89b873f18b154db802f2fd9fc57b8682 100644 (file)
@@ -56,11 +56,7 @@ struct login_proxy_state *login_proxy_state_init(const char *notify_path)
 
 static void login_proxy_state_close(struct login_proxy_state *state)
 {
-       if (state->notify_fd != -1) {
-               if (close(state->notify_fd) < 0)
-                       i_error("close(%s) failed: %m", state->notify_path);
-               state->notify_fd = -1;
-       }
+       i_close_fd_path(&state->notify_fd, state->notify_path);
 }
 
 void login_proxy_state_deinit(struct login_proxy_state **_state)
index 8d5b5964c3d41a765fdbdf1378306ce8aac228c7..6188021d721942bb134d120a8362249385d78c18 100644 (file)
@@ -163,11 +163,7 @@ static void squat_trie_close_fd(struct squat_trie *trie)
                trie->mmap_base = NULL;
                trie->mmap_size = 0;
        }
-       if (trie->fd != -1) {
-               if (close(trie->fd) < 0)
-                       i_error("close(%s) failed: %m", trie->path);
-               trie->fd = -1;
-       }
+       i_close_fd_path(&trie->fd, trie->path);
 }
 
 static void squat_trie_close(struct squat_trie *trie)
index 34a0b8303313dc88824816df6846fef4be40f304..5027f705c7f3747b3bc598fdfc0ad6fa57323c52 100644 (file)
@@ -557,11 +557,7 @@ static void squat_uidlist_close(struct squat_uidlist *uidlist)
                file_lock_free(&uidlist->file_lock);
        if (uidlist->dotlock != NULL)
                file_dotlock_delete(&uidlist->dotlock);
-       if (uidlist->fd != -1) {
-               if (close(uidlist->fd) < 0)
-                       i_error("close(%s) failed: %m", uidlist->path);
-               uidlist->fd = -1;
-       }
+       i_close_fd_path(&uidlist->fd, uidlist->path);
        uidlist->corrupted = FALSE;
 }
 
index 00a92ab8260a6cf38be2f750ac6016ee86655b5f..682fbca5afe78d78042fed876352b53b16044069 100644 (file)
@@ -29,11 +29,7 @@ i_stream_mail_filter_close(struct iostream_private *stream, bool close_parent)
 
        i_stream_destroy(&mstream->ext_in);
        o_stream_destroy(&mstream->ext_out);
-       if (mstream->fd != -1) {
-               if (close(mstream->fd) < 0)
-                       i_error("ext-filter: close() failed: %m");
-               mstream->fd = -1;
-       }
+       i_close_fd(&mstream->fd);
        if (close_parent)
                i_stream_close(mstream->istream.parent);
 }
index 9eccca6bcd3c7bc45130819738b24562c363897c..9d3c91dc9c1e2de6c79e2ddcd0316b7a6da9eb37 100644 (file)
@@ -28,11 +28,7 @@ o_stream_mail_filter_close(struct iostream_private *stream, bool close_parent)
 
        i_stream_destroy(&mstream->ext_in);
        o_stream_destroy(&mstream->ext_out);
-       if (mstream->fd != -1) {
-               if (close(mstream->fd) < 0)
-                       i_error("ext-filter: close() failed: %m");
-               mstream->fd = -1;
-       }
+       i_close_fd(&mstream->fd);
        if (close_parent)
                o_stream_close(mstream->ostream.parent);
 }
index b21884a3f2ffa1b021103b9f8ed43558d1945d15..ce9a46237d13afdaf69b16d5ca4848bf43336723 100644 (file)
@@ -138,10 +138,7 @@ static void fs_quota_mountpoint_free(struct fs_quota_mountpoint *mount)
                return;
 
 #ifdef FS_QUOTA_SOLARIS
-       if (mount->fd != -1) {
-               if (close(mount->fd) < 0)
-                       i_error("close(%s) failed: %m", mount->path);
-       }
+       i_close_fd_path(&mount->fd, mount->path);
        i_free(mount->path);
 #endif
 
index 37b404f748f9cfc60590efafd7286ee213bbad0e..a971be0566b14908a7f2189bd599160d0d51fcaf 100644 (file)
@@ -526,10 +526,7 @@ static int maildirsize_parse(struct maildir_quota_root *root,
 
 static int maildirsize_open(struct maildir_quota_root *root)
 {
-       if (root->fd != -1) {
-               if (close(root->fd) < 0)
-                       i_error("close(%s) failed: %m", root->maildirsize_path);
-       }
+       i_close_fd_path(&root->fd, root->maildirsize_path);
 
        root->fd = nfs_safe_open(root->maildirsize_path, O_RDWR | O_APPEND);
        if (root->fd == -1) {
index 2cde5408c52e32cd120ca7ed64dd8beb4ca4de23..aa61285e52fa34c3ed6cd51a71cbc28c8435f449 100644 (file)
@@ -378,11 +378,7 @@ void replication_plugin_init(struct module *module)
 
 void replication_plugin_deinit(void)
 {
-       if (fifo_fd != -1) {
-               if (close(fifo_fd) < 0)
-                       i_error("close(%s) failed: %m", fifo_path);
-               fifo_fd = -1;
-       }
+       i_close_fd_path(&fifo_fd, fifo_path);
        i_free_and_null(fifo_path);
 
        mail_storage_hooks_remove(&replication_mail_storage_hooks);
index a0503179c58758d9b5359cf51ce1dedd1d99152e..5ee291822b8803f4f92bc6713495e90a9786d125 100644 (file)
@@ -58,10 +58,7 @@ static void tcpwrap_client_input(struct tcpwrap_client *client)
                i_error("Invalid input from client");
        }
 
-       if (check_fd != -1) {
-               if (close(check_fd) < 0)
-                       i_error("close(fdread fd) failed: %m");
-       }
+       i_close_fd(&check_fd);
        tcpwrap_client_destroy(&client);
 }