From patchwork Thu Oct 15 04:04:31 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dongsheng Yang X-Patchwork-Id: 530475 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2001:1868:205::9]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id BFDAA1402B6 for ; Thu, 15 Oct 2015 15:14:12 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZmZtu-000304-Ik; Thu, 15 Oct 2015 04:12:22 +0000 Received: from [59.151.112.132] (helo=heian.cn.fujitsu.com) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZmZth-0002pj-Ha for linux-mtd@lists.infradead.org; Thu, 15 Oct 2015 04:12:11 +0000 X-IronPort-AV: E=Sophos;i="5.15,520,1432569600"; d="scan'208";a="101857427" Received: from bogon (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 15 Oct 2015 12:14:10 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (localhost.localdomain [127.0.0.1]) by edo.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id t9F4BHeT010893; Thu, 15 Oct 2015 12:11:17 +0800 Received: from yds-PC.g08.fujitsu.local (10.167.226.66) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.181.6; Thu, 15 Oct 2015 12:11:44 +0800 From: Dongsheng Yang To: , , , , Subject: [PATCH v2 08/27] ubifs: move more functions into io lib Date: Thu, 15 Oct 2015 12:04:31 +0800 Message-ID: <1444881890-4012-9-git-send-email-yangds.fnst@cn.fujitsu.com> X-Mailer: git-send-email 1.8.4.2 In-Reply-To: <1444881890-4012-1-git-send-email-yangds.fnst@cn.fujitsu.com> References: <1444881890-4012-1-git-send-email-yangds.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.66] X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20151014_211210_498576_8399B59E X-CRM114-Status: GOOD ( 19.25 ) X-Spam-Score: -1.1 (-) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-1.1 points) pts rule name description ---- ---------------------- -------------------------------------------------- -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.8 RDNS_NONE Delivered to internal network by a host with no rDNS X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Dongsheng Yang Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Move some common functions in mkfs.ubifs.c to io.c to let others can use them. Signed-off-by: Dongsheng Yang --- ubifs-utils/include/io.h | 4 ++ ubifs-utils/lib/io.c | 101 +++++++++++++++++++++++++++++++++++ ubifs-utils/mkfs.ubifs/mkfs.ubifs.c | 102 +----------------------------------- 3 files changed, 107 insertions(+), 100 deletions(-) diff --git a/ubifs-utils/include/io.h b/ubifs-utils/include/io.h index e24d0c6..920645d 100644 --- a/ubifs-utils/include/io.h +++ b/ubifs-utils/include/io.h @@ -10,6 +10,10 @@ extern int out_fd; extern int out_ubi; extern libubi_t ubi; +extern char *output; int write_leb(struct ubifs_info *c, int lnum, int len, void *buf); +int close_target(void); +int open_target(struct ubifs_info *c, int yes); +int open_ubi(struct ubifs_info *c, const char *node); #endif diff --git a/ubifs-utils/lib/io.c b/ubifs-utils/lib/io.c index 7aba0a6..9817d2a 100644 --- a/ubifs-utils/lib/io.c +++ b/ubifs-utils/lib/io.c @@ -5,6 +5,107 @@ int out_fd; int out_ubi; libubi_t ubi; +char *output; + +/** + * check_volume_empty - check if the UBI volume is empty. + * + * This function checks if the UBI volume is empty by looking if its LEBs are + * mapped or not. + * + * Returns %0 in case of success, %1 is the volume is not empty, + * and a negative error code in case of failure. + */ +static int check_volume_empty(struct ubifs_info *c) +{ + int lnum, err; + + for (lnum = 0; lnum < c->vi.rsvd_lebs; lnum++) { + err = ubi_is_mapped(out_fd, lnum); + if (err < 0) + return err; + if (err == 1) + return 1; + } + return 0; +} + +/** + * open_ubi - open the UBI volume. + * @node: name of the UBI volume character device to fetch information about + * + * Returns %0 in case of success and %-1 in case of failure + */ +int open_ubi(struct ubifs_info *c, const char *node) +{ + struct stat st; + + if (stat(node, &st) || !S_ISCHR(st.st_mode)) + return -1; + + ubi = libubi_open(); + if (!ubi) + return -1; + if (ubi_get_vol_info(ubi, node, &c->vi)) + return -1; + if (ubi_get_dev_info1(ubi, c->vi.dev_num, &c->di)) + return -1; + return 0; +} + +/** + * open_target - open the output target. + * @yes: always ansure yes + * + * Open the output target. The target can be an UBI volume + * or a file. + * + * Returns %0 in case of success and %-1 in case of failure. + */ +int open_target(struct ubifs_info *c, int yes) +{ + if (out_ubi) { + out_fd = open(output, O_RDWR | O_EXCL); + + if (out_fd == -1) + return sys_err_msg("cannot open the UBI volume '%s'", + output); + if (ubi_set_property(out_fd, UBI_VOL_PROP_DIRECT_WRITE, 1)) + return sys_err_msg("ubi_set_property failed"); + + if (!yes && check_volume_empty(c)) { + if (!prompt("UBI volume is not empty. Format anyways?", false)) + return err_msg("UBI volume is not empty"); + } + } else { + out_fd = open(output, O_CREAT | O_RDWR | O_TRUNC, + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); + if (out_fd == -1) + return sys_err_msg("cannot create output file '%s'", + output); + } + return 0; +} + +/** + * close_target - close the output target. + * + * Close the output target. If the target was an UBI + * volume, also close libubi. + * + * Returns %0 in case of success and %-1 in case of failure. + */ +int close_target(void) +{ + if (ubi) + libubi_close(ubi); + if (out_fd >= 0 && close(out_fd) == -1) + return sys_err_msg("cannot close the target '%s'", output); + if (output) + free(output); + return 0; +} + /** * write_leb - copy the image of a LEB to the output target. diff --git a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c index 9795282..b19646f 100644 --- a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c +++ b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c @@ -106,7 +106,6 @@ int yes; static char *root; static int root_len; static struct stat root_st; -static char *output; static int squash_owner; /* The 'head' (position) which nodes are written */ @@ -446,28 +445,6 @@ static long long get_bytes(const char *str) return bytes; } -/** - * open_ubi - open the UBI volume. - * @node: name of the UBI volume character device to fetch information about - * - * Returns %0 in case of success and %-1 in case of failure - */ -static int open_ubi(const char *node) -{ - struct stat st; - - if (stat(node, &st) || !S_ISCHR(st.st_mode)) - return -1; - - ubi = libubi_open(); - if (!ubi) - return -1; - if (ubi_get_vol_info(ubi, node, &c->vi)) - return -1; - if (ubi_get_dev_info1(ubi, c->vi.dev_num, &c->di)) - return -1; - return 0; -} static int get_options(int argc, char**argv) { @@ -629,7 +606,7 @@ static int get_options(int argc, char**argv) if (!output) return err_msg("not output device or file specified"); - out_ubi = !open_ubi(output); + out_ubi = !open_ubi(c, output); if (out_ubi) { c->min_io_size = c->di.min_io_size; @@ -2038,81 +2015,6 @@ static int write_orphan_area(void) return 0; } -/** - * check_volume_empty - check if the UBI volume is empty. - * - * This function checks if the UBI volume is empty by looking if its LEBs are - * mapped or not. - * - * Returns %0 in case of success, %1 is the volume is not empty, - * and a negative error code in case of failure. - */ -static int check_volume_empty(void) -{ - int lnum, err; - - for (lnum = 0; lnum < c->vi.rsvd_lebs; lnum++) { - err = ubi_is_mapped(out_fd, lnum); - if (err < 0) - return err; - if (err == 1) - return 1; - } - return 0; -} - -/** - * open_target - open the output target. - * - * Open the output target. The target can be an UBI volume - * or a file. - * - * Returns %0 in case of success and %-1 in case of failure. - */ -static int open_target(void) -{ - if (out_ubi) { - out_fd = open(output, O_RDWR | O_EXCL); - - if (out_fd == -1) - return sys_err_msg("cannot open the UBI volume '%s'", - output); - if (ubi_set_property(out_fd, UBI_VOL_PROP_DIRECT_WRITE, 1)) - return sys_err_msg("ubi_set_property failed"); - - if (!yes && check_volume_empty()) { - if (!prompt("UBI volume is not empty. Format anyways?", false)) - return err_msg("UBI volume is not empty"); - } - } else { - out_fd = open(output, O_CREAT | O_RDWR | O_TRUNC, - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); - if (out_fd == -1) - return sys_err_msg("cannot create output file '%s'", - output); - } - return 0; -} - - -/** - * close_target - close the output target. - * - * Close the output target. If the target was an UBI - * volume, also close libubi. - * - * Returns %0 in case of success and %-1 in case of failure. - */ -static int close_target(void) -{ - if (ubi) - libubi_close(ubi); - if (out_fd >= 0 && close(out_fd) == -1) - return sys_err_msg("cannot close the target '%s'", output); - if (output) - free(output); - return 0; -} /** * init - initialize things. @@ -2279,7 +2181,7 @@ int main(int argc, char *argv[]) if (err) return err; - err = open_target(); + err = open_target(c, yes); if (err) return err;