public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Fiona Ebner <f.ebner@proxmox.com>
To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>,
	Filip Schauer <f.schauer@proxmox.com>
Subject: Re: [pve-devel] [PATCH common v2 1/2] add get_device_stat helper subroutine
Date: Mon, 15 Apr 2024 15:54:41 +0200	[thread overview]
Message-ID: <c3af7312-6e9d-48f6-9641-67e3efc81efa@proxmox.com> (raw)
In-Reply-To: <20240415131702.94922-2-f.schauer@proxmox.com>

Am 15.04.24 um 15:17 schrieb Filip Schauer:
> 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);

We already have "use POSIX" with various error codes, so should be added
there.

> +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 {

It's not the whole stat, so the name is a bit misleading. Tools.pm is
already very big and mixes different things, so not super happy about
adding new things to it. If it would return the whole stat, it could
still be fine IMHO, because it's more likely to be reusable later.
Alternatively, we can keep this code in pve-container.

> +    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));

Style nit: useless parentheses, post-if can fit on the same line.

> +
> +    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 {




  reply	other threads:[~2024-04-15 13:54 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 ` [pve-devel] [PATCH common v2 1/2] add get_device_stat helper subroutine Filip Schauer
2024-04-15 13:54   ` Fiona Ebner [this message]
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=c3af7312-6e9d-48f6-9641-67e3efc81efa@proxmox.com \
    --to=f.ebner@proxmox.com \
    --cc=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