From 80cf052591d40f65238b0aca0620a0cb691e7365 Mon Sep 17 00:00:00 2001 From: Jani Nikula Date: Sun, 7 Apr 2013 20:15:03 +0300 Subject: [PATCH] [PATCH 2/3] cli: config: do not overwrite symlinks when saving config file --- f7/594d5168d25e91601421ee598cfe8b38cfd91e | 142 ++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 f7/594d5168d25e91601421ee598cfe8b38cfd91e diff --git a/f7/594d5168d25e91601421ee598cfe8b38cfd91e b/f7/594d5168d25e91601421ee598cfe8b38cfd91e new file mode 100644 index 000000000..55bf09bea --- /dev/null +++ b/f7/594d5168d25e91601421ee598cfe8b38cfd91e @@ -0,0 +1,142 @@ +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 CC4B1431FD5 + for ; Sun, 7 Apr 2013 10:15:21 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" +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 LM63a-k5T2xq for ; + Sun, 7 Apr 2013 10:15:20 -0700 (PDT) +Received: from mail-la0-f41.google.com (mail-la0-f41.google.com + [209.85.215.41]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id 30E34431FCB + for ; Sun, 7 Apr 2013 10:15:20 -0700 (PDT) +Received: by mail-la0-f41.google.com with SMTP id eg20so665140lab.28 + for ; Sun, 07 Apr 2013 10:15:17 -0700 (PDT) +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:in-reply-to:references:x-gm-message-state; + bh=7DB9c1D52BfgAp/3fsKGTtVlx0hpWx7idOBaEE9dhRQ=; + b=DcXnJmTL1nIFQ3+Tr5myVBxKMgLqhOBTq5GMOBilCBBOwxDLaHnqGKQOO0/Pjzcg+I + dfeQONhmBrBGUCdrq2BnfohQzUNWcA6CmZzO6Mm34VZeabZ55gJa60HO+Bbje86aUbyx + FgDbgyPbgRlF7WAw0qCpeQlDzX6uv1T8LIM36smRhe/y7Whyimh0iIPsTM6v96fHF7bT + 8RcQUkGMTyItoirOjaqLiaQcK8Xzg8FlGEoQyQY2a+zsPgGQTSxicyiYb+LZqu9pYICT + O0SEkG6rOODDQcRbYQzd+BnYLOBb9WCCBYDirLBWtL1tKrP2ImpZmJYfw7GhZNTLs9O+ + QZfg== +X-Received: by 10.112.154.170 with SMTP id vp10mr5937697lbb.10.1365354917336; + Sun, 07 Apr 2013 10:15:17 -0700 (PDT) +Received: from localhost (dsl-hkibrasgw4-50df51-27.dhcp.inet.fi. + [80.223.81.27]) + by mx.google.com with ESMTPS id c7sm9250623lbe.6.2013.04.07.10.15.15 + (version=TLSv1.2 cipher=RC4-SHA bits=128/128); + Sun, 07 Apr 2013 10:15:16 -0700 (PDT) +From: Jani Nikula +To: notmuch@notmuchmail.org +Subject: [PATCH 2/3] cli: config: do not overwrite symlinks when saving config + file +Date: Sun, 7 Apr 2013 20:15:03 +0300 +Message-Id: + <5502b2718a124b074b14c1835320e8b825a79045.1365354320.git.jani@nikula.org> +X-Mailer: git-send-email 1.7.10.4 +In-Reply-To: +References: +In-Reply-To: +References: +X-Gm-Message-State: + ALoCoQmyo412e5C/Ju0/PFulYZ6RYWpN/rYa4fL7F9BIa3WAPey42sIADzWIRe0LsDpai4m/5asT +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, 07 Apr 2013 17:15:22 -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. +--- + notmuch-config.c | 24 ++++++++++++++++++++---- + test/config | 1 - + 2 files changed, 20 insertions(+), 5 deletions(-) + +diff --git a/notmuch-config.c b/notmuch-config.c +index 66a1cdf..d9c2eb3 100644 +--- a/notmuch-config.c ++++ b/notmuch-config.c +@@ -444,7 +444,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); +@@ -453,14 +453,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; + } +diff --git a/test/config b/test/config +index 344eced..ca4cf33 100755 +--- a/test/config ++++ b/test/config +@@ -78,7 +78,6 @@ test_expect_equal "$(notmuch --config=alt-config-link config get user.name)" \ + "Link Name" + + test_begin_subtest "Writing config file through symlink follows symlink" +-test_subtest_known_broken + test_expect_equal "$(readlink alt-config-link)" "alt-config" + + test_done +-- +1.7.10.4 + -- 2.26.2