public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Filip Schauer <f.schauer@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH common v2 1/2] add get_device_stat helper subroutine
Date: Mon, 15 Apr 2024 15:17:01 +0200	[thread overview]
Message-ID: <20240415131702.94922-2-f.schauer@proxmox.com> (raw)
In-Reply-To: <20240415131702.94922-1-f.schauer@proxmox.com>

The get_device_stat subroutine gets a device path, validates it, and
returns the file mode and the device identifier.

Signed-off-by: Filip Schauer <f.schauer@proxmox.com>
---
 src/PVE/Tools.pm | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
index 766c809..d1b53e5 100644
--- a/src/PVE/Tools.pm
+++ b/src/PVE/Tools.pm
@@ -7,7 +7,8 @@ use Date::Format qw(time2str);
 use Digest::MD5;
 use Digest::SHA;
 use Encode;
-use Fcntl qw(:DEFAULT :flock);
+use Errno qw(ENOENT);
+use Fcntl qw(:DEFAULT :flock :mode);
 use File::Basename;
 use File::Path qw(make_path);
 use Filesys::Df (); # don't overwrite our df()
@@ -1853,6 +1854,21 @@ sub dev_t_minor($) {
     return (($dev_t >> 12) & 0xfff00) | ($dev_t & 0xff);
 }
 
+sub get_device_stat {
+    my ($path) = @_;
+
+    die "Path is not defined\n" if !defined($path);
+
+    my ($mode, $rdev) = (stat($path))[2, 6];
+    die "Device $path does not exist\n" if $! == ENOENT;
+    die "Error accessing device $path\n"
+	if (!defined($mode) || !defined($rdev));
+    die "$path is not a device\n"
+	if (!S_ISBLK($mode) && !S_ISCHR($mode));
+
+    return ($mode, $rdev);
+}
+
 # Given an array of array refs [ \[a b c], \[a b b], \[e b a] ]
 # Returns the intersection of elements as a single array [a b]
 sub array_intersect {
-- 
2.39.2





  reply	other threads:[~2024-04-15 13:17 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-15 13:17 [pve-devel] [PATCH common/container v2 0/2] fix invalid device passthrough being added to config Filip Schauer
2024-04-15 13:17 ` Filip Schauer [this message]
2024-04-15 13:54   ` [pve-devel] [PATCH common v2 1/2] add get_device_stat helper subroutine Fiona Ebner
2024-04-16  9:28     ` Filip Schauer
2024-04-15 13:17 ` [pve-devel] [PATCH container v2 2/2] fix invalid device passthrough being added to config Filip Schauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240415131702.94922-2-f.schauer@proxmox.com \
    --to=f.schauer@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal