]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/Tools.pm
tools: add fchownat syscall
[pve-common.git] / src / PVE / Tools.pm
index ff01ebd88ca74db036db5dc70725b49236332fce..550da09a609ddcf39f7999cc384bb312515192ed 100644 (file)
@@ -85,6 +85,8 @@ use constant {CLONE_NEWNS   => 0x00020000,
 use constant {O_PATH    => 0x00200000,
               O_TMPFILE => 0x00410000}; # This includes O_DIRECTORY
 
+use constant {AT_EMPTY_PATH => 0x1000};
+
 sub run_with_timeout {
     my ($timeout, $code, @param) = @_;
 
@@ -1556,6 +1558,11 @@ sub mkdirat($$$) {
     return syscall(PVE::Syscall::mkdirat, $dirfd, $name, $mode) == 0;
 }
 
+sub fchownat($$$$$) {
+    my ($dirfd, $pathname, $owner, $group, $flags) = @_;
+    return syscall(PVE::Syscall::fchownat, $dirfd, $pathname, $owner, $group, $flags) == 0;
+}
+
 my $salt_starter = time();
 
 sub encrypt_pw {
@@ -1654,18 +1661,26 @@ sub dev_t_minor($) {
 sub array_intersect {
     my ($arrays) = @_;
 
-    return [] if @$arrays == 0;
-    return $arrays->[0] if @$arrays == 1;
+    if (!ref($arrays->[0])) {
+       $arrays = [ grep { ref($_) eq 'ARRAY' } @_ ];
+    }
+
+    return [] if scalar(@$arrays) == 0;
+    return $arrays->[0] if scalar(@$arrays) == 1;
 
     my $array_unique = sub {
        my %seen = ();
        return grep { ! $seen{ $_ }++ } @_;
     };
 
-    my $return_arr;
-    @$return_arr = $array_unique->(@{$arrays->[0]});
+    # base idea is to get all unique members from the first array, then
+    # check the common elements with the next (uniquely made) one, only keep
+    # those. Repeat for every array and at the end we only have those left
+    # which exist in all arrays
+    my $return_arr = [ $array_unique->(@{$arrays->[0]}) ];
     for my $i (1 .. $#$arrays) {
        my %count = ();
+       # $return_arr is already unique, explicit at before the loop, implicit below.
        foreach my $element (@$return_arr, $array_unique->(@{$arrays->[$i]})) {
            $count{$element}++;
        }
@@ -1673,6 +1688,7 @@ sub array_intersect {
        foreach my $element (keys %count) {
            push @$return_arr, $element if $count{$element} > 1;
        }
+       last if scalar(@$return_arr) == 0; # empty intersection, early exit
     }
 
     return $return_arr;