From patchwork Wed Aug 26 09:04:22 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhaolei X-Patchwork-Id: 7075671 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 6D56AC05AD for ; Wed, 26 Aug 2015 09:06:16 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 8E0F120918 for ; Wed, 26 Aug 2015 09:06:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9C41720916 for ; Wed, 26 Aug 2015 09:06:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756648AbbHZJGH (ORCPT ); Wed, 26 Aug 2015 05:06:07 -0400 Received: from cn.fujitsu.com ([59.151.112.132]:15263 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1756236AbbHZJGD (ORCPT ); Wed, 26 Aug 2015 05:06:03 -0400 X-IronPort-AV: E=Sophos;i="5.15,520,1432569600"; d="scan'208";a="100036005" Received: from unknown (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 26 Aug 2015 17:09: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 t7Q95sJo016061 for ; Wed, 26 Aug 2015 17:05:54 +0800 Received: from localhost.localdomain (10.167.226.114) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server id 14.3.181.6; Wed, 26 Aug 2015 17:06:00 +0800 From: Zhao Lei To: CC: Zhao Lei Subject: [PATCH v2 1/2] btrfs-progs: Introduce open_btrfs_dir wrapper Date: Wed, 26 Aug 2015 17:04:22 +0800 Message-ID: X-Mailer: git-send-email 1.8.5.1 In-Reply-To: References: MIME-Version: 1.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch introduce open_btrfs_dir() to open a dir in btrfs filesystem. It can be used for several tools in btrfs-progs. Signed-off-by: Zhao Lei --- utils.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 1 + 2 files changed, 57 insertions(+) diff --git a/utils.c b/utils.c index 1dfcc2d..eb9a605 100644 --- a/utils.c +++ b/utils.c @@ -36,6 +36,8 @@ #include #include #include +#include +#include #include "kerncompat.h" #include "radix-tree.h" @@ -1081,6 +1083,60 @@ int open_path_or_dev_mnt(const char *path, DIR **dirstream) return fdmnt; } +/* + * Do following check before open_file_or_dir(): + * 1: path is in a btrfs filesystem + * 2: path is a dir + */ +int open_btrfs_dir(const char *path, DIR **dirstream, int output) +{ + struct statfs stfs; + struct stat st; + int ret; + + if (statfs(path, &stfs) != 0) { + if (output) + fprintf(stderr, + "ERROR: can't access '%s', %s\n", + path, strerror(errno)); + return -1; + } + + if (stfs.f_type != BTRFS_SUPER_MAGIC) { + if (output) + fprintf(stderr, + "ERROR: not btrfs filesystem: %s\n", + path); + return -2; + } + + if (stat(path, &st) != 0) { + if (output) + fprintf(stderr, + "ERROR: can't access '%s', %s\n", + path, strerror(errno)); + return -1; + } + + if (!S_ISDIR(st.st_mode)) { + if (output) + fprintf(stderr, + "ERROR: not directory: %s\n", + path); + return -3; + } + + ret = open_file_or_dir(path, dirstream); + if (ret < 0) { + if (output) + fprintf(stderr, + "ERROR: can't access '%s', %s\n", + path, strerror(errno)); + } + + return ret; +} + /* checks if a device is a loop device */ static int is_loop_device (const char* device) { struct stat statbuf; diff --git a/utils.h b/utils.h index 8ec23c9..87c0d08 100644 --- a/utils.h +++ b/utils.h @@ -158,6 +158,7 @@ int is_block_device(const char *file); int is_mount_point(const char *file); int check_arg_type(const char *input); int open_path_or_dev_mnt(const char *path, DIR **dirstream); +int open_btrfs_dir(const char *path, DIR **dirstream, int output); u64 btrfs_device_size(int fd, struct stat *st); /* Helper to always get proper size of the destination string */ #define strncpy_null(dest, src) __strncpy__null(dest, src, sizeof(dest))