From 31f6a5efacf6b266fa2ad6c15387d4dc26cc6d89 Mon Sep 17 00:00:00 2001 From: Jani Nikula Date: Sun, 3 Mar 2013 11:32:32 +0200 Subject: [PATCH] [PATCH v2] config: do not overwrite symlinks when saving config file --- 2c/f9112f02a637762c8425d845f632fc4a0d7bf3 | 124 ++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 2c/f9112f02a637762c8425d845f632fc4a0d7bf3 diff --git a/2c/f9112f02a637762c8425d845f632fc4a0d7bf3 b/2c/f9112f02a637762c8425d845f632fc4a0d7bf3 new file mode 100644 index 000000000..17ac02a07 --- /dev/null +++ b/2c/f9112f02a637762c8425d845f632fc4a0d7bf3 @@ -0,0 +1,124 @@ +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 4360A431FAF + for ; Sun, 3 Mar 2013 01:32:45 -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 iZjLYD11ZVP3 for ; + Sun, 3 Mar 2013 01:32:44 -0800 (PST) +Received: from mail-la0-f51.google.com (mail-la0-f51.google.com + [209.85.215.51]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id 87174431FAE + for ; Sun, 3 Mar 2013 01:32:44 -0800 (PST) +Received: by mail-la0-f51.google.com with SMTP id fo13so4049532lab.24 + for ; Sun, 03 Mar 2013 01:32:42 -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=O4pjaPTphCdGks3y1eAP+6VJy3Rqk+UeD0jM57SOYp4=; + b=CduZ2S+99l1nyLuo7HG0I2DE/BcpLQnDBeHpaqrQTYaan/yctpEWB1n1lbfHH6lDW4 + 5S9nHJ9QbhP8itd96jH94XrzNWAjIWOrU5CG4j7JlQ8qWlZKPs3+J98jD3hX0HmjUm+S + /4K9D4Z814weoKWqx/oXVplYAS6CaDCnbpct4MUeeUfFtXREiFWnQRCX5I6pR56EMn6s + NAJRokQ+8uI3Lcc9QWQGr5J9kqHs0UCBqapU5OmVgEB7xFT6KSg0GIhelyJ0qfyZHneF + 9IGRG/tHyGBt5W1g4b1Rp4JhUVRYBw9S/8ZMi7GiY5xDpaOQqrNt4wKU5185zwAzmXnV + psag== +X-Received: by 10.112.8.231 with SMTP id u7mr2914562lba.45.1362303161462; + Sun, 03 Mar 2013 01:32:41 -0800 (PST) +Received: from localhost (dsl-hkibrasgw4-50df51-27.dhcp.inet.fi. + [80.223.81.27]) + by mx.google.com with ESMTPS id t17sm9740402lam.9.2013.03.03.01.32.38 + (version=TLSv1.2 cipher=RC4-SHA bits=128/128); + Sun, 03 Mar 2013 01:32:40 -0800 (PST) +From: Jani Nikula +To: notmuch@notmuchmail.org +Subject: [PATCH v2] config: do not overwrite symlinks when saving config file +Date: Sun, 3 Mar 2013 11:32:32 +0200 +Message-Id: <1362303152-25793-1-git-send-email-jani@nikula.org> +X-Mailer: git-send-email 1.7.10.4 +In-Reply-To: +References: +X-Gm-Message-State: + ALoCoQkkn+8iap9zIYBZW60LShZ1PbFBe4q4l6SP+oBSsRL9JHl9R81Oeu5btiiXLIHw7sglyYhO +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 09:32:45 -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 ++++++++++++++++++++---- + 1 file changed, 20 insertions(+), 4 deletions(-) + +diff --git a/notmuch-config.c b/notmuch-config.c +index b5c2066..89bc143 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)) { ++ 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 + -- 2.26.2