Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 2E47C431FAF for ; Sun, 3 Mar 2013 06:18:36 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.7 X-Spam-Level: X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id WvNfCAKmOkhO for ; Sun, 3 Mar 2013 06:18:35 -0800 (PST) Received: from mail-la0-f46.google.com (mail-la0-f46.google.com [209.85.215.46]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 7FA6F431FAE for ; Sun, 3 Mar 2013 06:18:35 -0800 (PST) Received: by mail-la0-f46.google.com with SMTP id fq12so4158221lab.5 for ; Sun, 03 Mar 2013 06:18:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references:x-gm-message-state; bh=nvE8ta7V/iP2jnsQO+ImB6cAYABZeSb4fxsVxp3FQgE=; b=XZXIfzQq0l2B2qkBZf5kr+fLUckiJQPq1MVaWc6n5iY3e+T8qB/rukIJZXPxtkdciZ c+eZe6svJgM3h6pNqW9c201B7lStCvpY1JVW/KDAPooHeqRYfzwhNC6eOTYn2mE/rWPM RQj/oKQDi9hlKBdc7xrH6gV5a+KwymfhJ+tyx0FBzXZjpxb9x2AF5oD/54qGp28fVs6j vyhzqrR7NmPryvPNZtMC0OWUQ6P5ucPYjOL4S2wW/c/ljUuRNCVmHoKzCmVgxA1lBZHT XmO/1cY44tK3kBpiA71r8TFRZFoNRnbNMKijVDEhpvOLy4G64Wrz6XRQpBSUv0rcbUoo pW2g== X-Received: by 10.152.144.202 with SMTP id so10mr15105316lab.9.1362320312590; Sun, 03 Mar 2013 06:18:32 -0800 (PST) Received: from localhost (dsl-hkibrasgw4-50df51-27.dhcp.inet.fi. [80.223.81.27]) by mx.google.com with ESMTPS id e11sm6239987lbd.11.2013.03.03.06.18.30 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Sun, 03 Mar 2013 06:18:31 -0800 (PST) From: Jani Nikula To: notmuch@notmuchmail.org Subject: [PATCH v3] config: do not overwrite symlinks when saving config file Date: Sun, 3 Mar 2013 16:18:30 +0200 Message-Id: <1362320310-10930-1-git-send-email-jani@nikula.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: References: X-Gm-Message-State: ALoCoQmFQkaZjEVYT5sxtPw1G+ssEArBrDgMjG+Vs6m1GRjhAH/sWBVV3LlaGUVu7TJ1jLemPcq0 Cc: Tomi Ollila X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 03 Mar 2013 14:18:36 -0000 Use realpath to canonicalize the config path before writing. Previously 'notmuch setup' and 'notmuch config set' overwrote the config file even if it was a symbolic link. --- v3: compared to v2, as suggested by Tomi on IRC: - if (strcmp (filename, config->filename)) { + if (strcmp (filename, config->filename) != 0) { --- notmuch-config.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/notmuch-config.c b/notmuch-config.c index b5c2066..8adb404 100644 --- a/notmuch-config.c +++ b/notmuch-config.c @@ -461,7 +461,7 @@ int notmuch_config_save (notmuch_config_t *config) { size_t length; - char *data; + char *data, *filename; GError *error = NULL; data = g_key_file_to_data (config->key_file, &length, NULL); @@ -470,14 +470,30 @@ notmuch_config_save (notmuch_config_t *config) return 1; } - if (! g_file_set_contents (config->filename, data, length, &error)) { - fprintf (stderr, "Error saving configuration to %s: %s\n", - config->filename, error->message); + /* Try not to overwrite symlinks. */ + filename = realpath (config->filename, NULL); + if (! filename) { + fprintf (stderr, "Error canonicalizing %s: %s\n", config->filename, + strerror (errno)); + g_free (data); + return 1; + } + + if (! g_file_set_contents (filename, data, length, &error)) { + if (strcmp (filename, config->filename) != 0) { + fprintf (stderr, "Error saving configuration to %s (-> %s): %s\n", + config->filename, filename, error->message); + } else { + fprintf (stderr, "Error saving configuration to %s: %s\n", + filename, error->message); + } g_error_free (error); + free (filename); g_free (data); return 1; } + free (filename); g_free (data); return 0; } -- 1.7.10.4