[v3,1/5] util: refactor fwrite
Message ID | 1500318318-18852-1-git-send-email-jonatan.schlag@ipfire.org |
---|---|
State | Superseded |
Headers |
Return-Path: <network-bounces@lists.ipfire.org> Received: from mail01.ipfire.org (unknown [172.28.1.200]) by web02.ipfire.org (Postfix) with ESMTP id A12386159A for <patchwork@ipfire.org>; Mon, 17 Jul 2017 21:05:24 +0200 (CEST) Received: from mail01.ipfire.org (localhost [IPv6:::1]) by mail01.ipfire.org (Postfix) with ESMTP id 6898027C8; Mon, 17 Jul 2017 21:05:24 +0200 (CEST) Received: from ipfire.localdomain (dslb-088-073-208-102.088.073.pools.vodafone-ip.de [88.73.208.102]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) by mail01.ipfire.org (Postfix) with ESMTPSA id 852DB2596; Mon, 17 Jul 2017 21:05:22 +0200 (CEST) From: Jonatan Schlag <jonatan.schlag@ipfire.org> To: network@lists.ipfire.org Subject: [PATCH v3 1/5] util: refactor fwrite Date: Mon, 17 Jul 2017 21:05:14 +0200 Message-Id: <1500318318-18852-1-git-send-email-jonatan.schlag@ipfire.org> X-Mailer: git-send-email 2.6.3 X-BeenThere: network@lists.ipfire.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List for the network package <network.lists.ipfire.org> List-Unsubscribe: <http://lists.ipfire.org/mailman/options/network>, <mailto:network-request@lists.ipfire.org?subject=unsubscribe> List-Archive: <http://lists.ipfire.org/pipermail/network/> List-Post: <mailto:network@lists.ipfire.org> List-Help: <mailto:network-request@lists.ipfire.org?subject=help> List-Subscribe: <http://lists.ipfire.org/mailman/listinfo/network>, <mailto:network-request@lists.ipfire.org?subject=subscribe> Errors-To: network-bounces@lists.ipfire.org Sender: "network" <network-bounces@lists.ipfire.org> |
Message
Jonatan Schlag
July 18, 2017, 5:05 a.m. UTC
This functions now:
- return an error when the destination is a directory
- creates the destination if the destination not exist
Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
---
src/functions/functions.util | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)
Comments
Hi, I find that this makes the function unnecessarily complicated. It would be enough to keep it as it is and add the check if $file is a directory. If you would prefer to have a different error message when ever writing to the file fails, you can implement it as such: if ! print "%s" "$@" >> ${file}; then error "Could not write to file: ..." return ${EXIT_ERROR} fi That at least saves a stat call which would be quite good since the functions writing the configuration files make quite a lot of use of this function. -Michael On Mon, 2017-07-17 at 21:05 +0200, Jonatan Schlag wrote: > This functions now: > - return an error when the destination is a directory > - creates the destination if the destination not exist > > Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org> > --- > src/functions/functions.util | 15 ++++++++++++--- > 1 file changed, 12 insertions(+), 3 deletions(-) > > diff --git a/src/functions/functions.util > b/src/functions/functions.util > index 381208c..dd223f9 100644 > --- a/src/functions/functions.util > +++ b/src/functions/functions.util > @@ -179,12 +179,21 @@ fwrite() { > assert isset file > shift > > - if [ ! -w "${file}" ]; then > - log ERROR "${file}: No such file" > + if [ -d "${file}" ]; then > + log ERROR "${file} is a directory" > return ${EXIT_ERROR} > fi > > - print "%s" "$@" >> ${file} 2>/dev/null > + if [ -f "${file}" ]; then > + if [ -w "${file}" ]; then > + print "%s" "$@" >> ${file} 2>/dev/null > + else > + log ERROR "${file} is not writeable" > + return ${EXIT_ERROR} > + fi > + else > + print "%s" "$@" >> ${file} 2>/dev/null > + fi > } > > make_parent_dir() {