File Coverage

blib/lib/FusionInventory/Agent/Task/Inventory/MacOS/Drives.pm
Criterion Covered Total %
statement 12 57 21.0
branch 0 14 0.0
condition 0 6 0.0
subroutine 4 8 50.0
pod 0 2 0.0
total 16 87 18.3


line stmt bran cond sub pod time code
1             package FusionInventory::Agent::Task::Inventory::MacOS::Drives;
2              
3 1     1   64580028 use strict;
  1         2  
  1         55  
4 1     1   7 use warnings;
  1         6  
  1         65  
5              
6 1     1   436 use FusionInventory::Agent::Tools;
  1         3  
  1         136  
7 1     1   402 use FusionInventory::Agent::Tools::Unix;
  1         2  
  1         521  
8              
9             sub isEnabled {
10 0     0 0   my (%params) = @_;
11 0 0         return 0 if $params{no_category}->{drive};
12 0           return 1;
13             }
14              
15             sub doInventory {
16 0     0 0   my (%params) = @_;
17              
18 0           my $inventory = $params{inventory};
19 0           my $logger = $params{logger};
20              
21             # get filesystem types
22             my @types =
23 0           grep { ! /^(?:fdesc|devfs|procfs|linprocfs|linsysfs|tmpfs|fdescfs)$/ }
  0            
24             getFilesystemsTypesFromMount(logger => $logger);
25              
26             # get filesystems for each type
27 0           my @filesystems;
28 0           foreach my $type (@types) {
29 0           push @filesystems, getFilesystemsFromDf(
30             logger => $logger,
31             command => "df -P -k -t $type",
32             type => $type,
33             );
34             }
35              
36 0           my %filesystems = map { $_->{VOLUMN} => $_ } @filesystems;
  0            
37              
38 0           foreach my $partition (_getPartitions()) {
39 0           my $device = "/dev/$partition";
40              
41 0           my $info = _getPartitionInfo(partition => $partition);
42              
43 0           my $filesystem = $filesystems{$device};
44 0 0         next unless $filesystem;
45              
46 0 0         if ($info->{'Total Size'} =~ /^([.\d]+ \s \S+)/x) {
47 0           $filesystem->{TOTAL} = getCanonicalSize($1);
48             }
49             $filesystem->{SERIAL} = $info->{'Volume UUID'} ||
50 0   0       $info->{'UUID'};
51             $filesystem->{FILESYSTEM} = $info->{'File System'} ||
52 0   0       $info->{'Partition Type'};
53 0           $filesystem->{LABEL} = $info->{'Volume Name'};
54             }
55              
56             # add filesystems to the inventory
57 0           foreach my $key (keys %filesystems) {
58             $inventory->addEntry(
59             section => 'DRIVES',
60 0           entry => $filesystems{$key}
61             );
62             }
63             }
64              
65             sub _getPartitions {
66 0     0     my (%params) = @_;
67              
68 0           my $command = "diskutil list";
69 0           my $handle = getFileHandle(command => $command, %params);
70 0 0         return unless $handle;
71              
72 0           my @devices;
73 0           while (my $line = <$handle>) {
74             # partition identifiers look like disk0s1
75 0 0         next unless $line =~ /(disk \d+ s \d+)$/x;
76 0           push @devices, $1;
77             }
78 0           close $handle;
79              
80 0           return @devices;
81             }
82              
83             sub _getPartitionInfo {
84 0     0     my (%params) = @_;
85              
86 0           my $command = "diskutil info $params{partition}";
87 0           my $handle = getFileHandle(command => $command, %params);
88 0 0         return unless $handle;
89              
90 0           my $info;
91 0           while (my $line = <$handle>) {
92 0 0         next unless $line =~ /(\S[^:]+) : \s+ (\S.*\S)/x;
93 0           $info->{$1} = $2;
94             }
95 0           close $handle;
96              
97 0           return $info;
98             }
99              
100             1;