From patchwork Sun Jul 24 11:03:25 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Goffredo Baroncelli X-Patchwork-Id: 9244849 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 E6E37607FD for ; Sun, 24 Jul 2016 11:03:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C1D0B2793B for ; Sun, 24 Jul 2016 11:03:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A27BC27FA8; Sun, 24 Jul 2016 11:03:41 +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.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID 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 8127B2793B for ; Sun, 24 Jul 2016 11:03:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752187AbcGXLDi (ORCPT ); Sun, 24 Jul 2016 07:03:38 -0400 Received: from smtp-31.italiaonline.it ([212.48.25.159]:38225 "EHLO libero.it" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752084AbcGXLDe (ORCPT ); Sun, 24 Jul 2016 07:03:34 -0400 Received: from venice.bhome ([94.38.190.179]) by smtp-31.iol.local with SMTP id RHBzbxdpBQrIyRHC0bdZfP; Sun, 24 Jul 2016 13:03:32 +0200 x-libjamoibt: 1601 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=libero.it; s=s2014; t=1469358212; bh=9VmmpehLxCUBWh5GNU0OR0jfhXvs2F8hd411+8aevtI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=elu64oSbw1DzEdlmHqju21RjDyYvSlarEZXaMk97HcbnnUMeWjxPpyjppFQ+hbop/ WrUkvC4mqr/WpQqAwL07ZrthDRIyhqgLu90NQv0fHHNwwdtZkPaIXE5exDJD8QX2Tl IMm8WcC947DI6o7QzdJUHoGZNEsfGfdZDFNxd0UFJQmMTHvXrVBj5H6+mLTVNtkKnC JyhkhuInAdT5W1+AsyxYZbPJhgzrBbT9MY9M6J8opgEMJH8MjK469PVSKgiSLiWA7c lI5sGu/WuxyGlnG4dB26BAZH3n3KM8XnSO/JuL1RtT2nsCuN4sVbY3Ps5T6giprOnE XbIVQFE9R/HgA== X-CNFS-Analysis: v=2.2 cv=AcnzJDfG c=1 sm=1 tr=0 a=nUeg4taw7nlFxlnmnt3uSQ==:117 a=nUeg4taw7nlFxlnmnt3uSQ==:17 a=NL-WSrys4zUA:10 a=h2K0HY8J6V3-NiqQhFUA:9 From: Goffredo Baroncelli To: linux-btrfs@vger.kernel.org Cc: dsterba@suse.cz, Chris Mason , Goffredo Baroncelli Subject: [PATCH 1/5] Add some helper functions Date: Sun, 24 Jul 2016 13:03:25 +0200 Message-Id: <1469358209-9427-2-git-send-email-kreijack@libero.it> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1469358209-9427-1-git-send-email-kreijack@libero.it> References: <1469358209-9427-1-git-send-email-kreijack@libero.it> X-CMAE-Envelope: MS4wfFq3S5FvfAV2KHxiSl+NQck9EXq2t/ISsYzeE+/q38mOccDChyO0sGauZ/VJSGHad40HUWtYIcYvW9bTERSuCk0acQgF9SiiGqPDHpn/W2T1ILznrzbl bJxwmHNN4knOCLadtFBRHyr9k9Dj+6rMzrl0xTvhpKqqxSijWHXL4JdlXt652l4pLxv++pFLNGfbmHqp9xKlqyZJE28lnTDafU0yki4qLAfqD60a0GJgRuF7 FbWzQYqrAYabQUSZmIlGfOlMcX0W2dYVYxROV53XMew= Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Goffredo Baroncelli Add the following functions: - int is_btrfs_fs(const char *path) -> returns 0 if path is a btrfs filesystem - void check_root_or_exit() -> checks if the user has the root capability or it exits writing an error message - void check_btrfs_or_exit(const char *path) checks if path is a valid btrfs filesystem, otherwise it exits Signed-off-by: Goffredo baroncelli --- utils.c | 41 +++++++++++++++++++++++++++++++++++++++++ utils.h | 14 ++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/utils.c b/utils.c index 578fdb0..b99706c 100644 --- a/utils.c +++ b/utils.c @@ -4131,3 +4131,44 @@ unsigned int rand_range(unsigned int upper) */ return (unsigned int)(jrand48(rand_seed) % upper); } + +/* + * check if path is a btrfs filesystem + */ +int is_btrfs_fs(const char *path) +{ + struct statfs stfs; + + if (statfs(path, &stfs) != 0) { + /* cannot access */ + return -1; + } + + if (stfs.f_type != BTRFS_SUPER_MAGIC) { + /* not a btrfs filesystem */ + return -2; + } + + return 0; +} + +/* + * check if the user is root + */ +void check_root_or_exit() +{ + if (geteuid() == 0) + return; + + error("You need to be root to execute this command"); + exit(100); +} + +void check_btrfs_or_exit(const char *path) +{ + if (!is_btrfs_fs(path)) + return; + + error("'%s' must be a valid btrfs filesystem", path); + exit(100); +} diff --git a/utils.h b/utils.h index 98bfb34..0bd6ecb 100644 --- a/utils.h +++ b/utils.h @@ -399,4 +399,18 @@ unsigned int rand_range(unsigned int upper); /* Also allow setting the seed manually */ void init_rand_seed(u64 seed); +/* return 0 if path is a valid btrfs filesystem */ +int is_btrfs_fs(const char *path); + +/* + * check if the user has the root capability, otherwise it exits printing an + * error message + */ +void check_root_or_exit(); +/* + * check if path is a valid btrfs filesystem, otherwise it exits printing an + * error message + */ +void check_btrfs_or_exit(const char *path); + #endif