[pve-devel] [PATCH common v2 1/2] add get_device_stat helper subroutine

Filip Schauer f.schauer at proxmox.com
Mon Apr 15 15:17:01 CEST 2024


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 at 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





More information about the pve-devel mailing list