From patchwork Fri Aug 11 12:30:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Tulak X-Patchwork-Id: 9895677 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id CD1A460236 for ; Fri, 11 Aug 2017 12:30:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BE93D28B85 for ; Fri, 11 Aug 2017 12:30:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B334928B8C; Fri, 11 Aug 2017 12:30:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 41A7C28B85 for ; Fri, 11 Aug 2017 12:30:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752705AbdHKMap (ORCPT ); Fri, 11 Aug 2017 08:30:45 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44291 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752674AbdHKMap (ORCPT ); Fri, 11 Aug 2017 08:30:45 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2CC2B4A6F8 for ; Fri, 11 Aug 2017 12:30:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 2CC2B4A6F8 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=jtulak@redhat.com Received: from jtulak-arch.redhat.com (unknown [10.43.17.156]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3D02363757; Fri, 11 Aug 2017 12:30:44 +0000 (UTC) From: Jan Tulak To: linux-xfs@vger.kernel.org Cc: Jan Tulak Subject: [PATCH 1/6] mkfs: Save raw user input field to the opts struct Date: Fri, 11 Aug 2017 14:30:32 +0200 Message-Id: <20170811123037.15962-2-jtulak@redhat.com> In-Reply-To: <20170811123037.15962-1-jtulak@redhat.com> References: <20170811123037.15962-1-jtulak@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Fri, 11 Aug 2017 12:30:45 +0000 (UTC) Sender: linux-xfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Save exactly what the user gave us for every option. This way, we will never lose the information if we need it to print back an issue. (Just add the infrastructure now, used in the next patches.) Signed-off-by: Jan Tulak --- CHANGE: * added strdup * added boundary checks to set/get functions --- mkfs/xfs_mkfs.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/mkfs/xfs_mkfs.c b/mkfs/xfs_mkfs.c index 7bb6408f..fa0b475c 100644 --- a/mkfs/xfs_mkfs.c +++ b/mkfs/xfs_mkfs.c @@ -107,6 +107,11 @@ unsigned int sectorsize; * sets what is used with simple specifying the subopt (-d file). * A special SUBOPT_NEEDS_VAL can be used to require a user-given * value in any case. + * + * raw_input INTERNAL + * Filled raw string from the user, so we never lose that information e.g. + * to print it back in case of an issue. + * */ struct opt_params { const char name; @@ -122,6 +127,7 @@ struct opt_params { long long minval; long long maxval; long long defaultval; + char *raw_input; } subopt_params[MAX_SUBOPTS]; }; @@ -730,6 +736,69 @@ struct opt_params mopts = { #define WHACK_SIZE (128 * 1024) /* + * Return 0 on success, -ENOMEM if it could not allocate enough memory for + * the string to be saved. + */ +static int +set_conf_raw(struct opt_params *opt, const int subopt, const char *value) +{ + if (subopt < 0 || subopt >= MAX_SUBOPTS) { + fprintf(stderr, + "This is a bug: set_conf_raw called with invalid opt/subopt: %c/%d\n", + opt->name, subopt); + return -EINVAL; + } + if (value == NULL) { + if (opt->subopt_params[subopt].raw_input != NULL) + free(opt->subopt_params[subopt].raw_input); + opt->subopt_params[subopt].raw_input = NULL; + } else { + opt->subopt_params[subopt].raw_input = strdup(value); + if (opt->subopt_params[subopt].raw_input == NULL) + return -ENOMEM; + } + return 0; +} + +/* + * Return 0 on success, -ENOMEM if it could not allocate enough memory for + * the string to be saved into the out pointer. + */ +static int +get_conf_raw(const struct opt_params *opt, const int subopt, char **out) +{ + if (subopt < 0 || subopt >= MAX_SUBOPTS) { + fprintf(stderr, + "This is a bug: get_conf_raw called with invalid opt/subopt: %c/%d\n", + opt->name, subopt); + return -EINVAL; + } + *out = strdup(opt->subopt_params[subopt].raw_input); + if (*out == NULL) + return -ENOMEM; + return 0; + +} + +/* + * Same as get_conf_raw(), except it returns the string through return. + * If any error occurs, return NULL. + */ +static char * +get_conf_raw_safe(const struct opt_params *opt, const int subopt) +{ + char *str; + + str = NULL; + + if (get_conf_raw(opt, subopt, &str) == -ENOMEM) { + fprintf(stderr, "Out of memory!"); + return NULL; + } + return str; +} + +/* * Convert lsu to lsunit for 512 bytes blocks and check validity of the values. */ static void