Skip to content

Commit

Permalink
Merge remote branch 'origin/2.2.x' into 2.2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Gonéri Le Bouder committed May 14, 2012
2 parents 889ec84 + 86e390b commit c17bfdc
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/FusionInventory/Agent/HTTP/Client/Fusion.pm
Expand Up @@ -29,11 +29,11 @@ sub _prepareVal {
return '' unless length($val);

# forbid to long argument.
while (length(uri_escape($val)) > 1500) {
while (length(URI::Escape::uri_escape_utf8($val)) > 1500) {
$val =~ s/^.{5}//;
}

return uri_escape($val);
return URI::Escape::uri_escape_utf8($val);
}

sub send { ## no critic (ProhibitBuiltinHomonyms)
Expand Down
9 changes: 8 additions & 1 deletion lib/FusionInventory/Agent/Task/Inventory.pm
Expand Up @@ -89,7 +89,14 @@ sub run {
$self->{deviceid} .
$extension;

if (open my $handle, '>', $file) {
my $handle;
if (Win32::Unicode::File->require) {
$handle = Win32::Unicode::File->new('w', $file);
} else {
open($handle, '>', $file);
}

if ($handle) {
$self->_printInventory(
inventory => $inventory,
handle => $handle,
Expand Down
35 changes: 29 additions & 6 deletions lib/FusionInventory/Agent/Task/Inventory/Input/Generic/Screen.pm
Expand Up @@ -7,13 +7,15 @@ use English qw(-no_match_vars);
use MIME::Base64;
use UNIVERSAL::require;

use File::Find;
use FusionInventory::Agent::Tools;
use FusionInventory::Agent::Tools::Screen;

sub isEnabled {

return
$OSNAME eq 'MSWin32' ||
-d '/sys' ||
canRun('monitor-get-edid-using-vbe') ||
canRun('monitor-get-edid') ||
canRun('get-edid');
Expand Down Expand Up @@ -42,8 +44,8 @@ sub doInventory {
}
$screen->{BASE64} = encode_base64($screen->{edid});

delete $screen->{edid};
}
delete $screen->{edid};

$inventory->addEntry(
section => 'MONITORS',
Expand Down Expand Up @@ -113,18 +115,39 @@ sub _getScreensFromWindows {

sub _getScreensFromUnix {

my @screens;

my $wanted = sub {
return unless $File::Find::name =~ m{(/edid)$};
open my $t, "<$File::Find::name";
my $edid = <$t>;
close $t;

push @screens, { edid => $edid } if $edid;
};

if (-d '/sys') {
File::Find::find($wanted, '/sys');
}

return @screens if @screens;

my $edid =
getFirstLine(command => 'monitor-get-edid-using-vbe') ||
getFirstLine(command => 'monitor-get-edid');
push @screens, { edid => $edid };

if (!$edid) {
foreach (1..5) { # Sometime get-edid return an empty string...
$edid = getFirstLine(command => 'get-edid');
last if $edid;
return @screens if @screens;

foreach (1..5) { # Sometime get-edid return an empty string...
$edid = getFirstLine(command => 'get-edid');
if ($edid) {
push @screens, { edid => $edid };
last;
}
}

return ( { edid => $edid } );
return @screens;
}

sub _getScreens {
Expand Down
11 changes: 9 additions & 2 deletions lib/FusionInventory/Agent/Task/Inventory/Input/Win32/OS.pm
Expand Up @@ -98,9 +98,16 @@ sub doInventory {
$inventory->setHardware({
MEMORY => $object->{TotalPhysicalMemory},
WORKGROUP => $object->{Domain} || $object->{Workgroup},
WINOWNER => $object->{PrimaryOwnerName},
NAME => $object->{Name},
WINOWNER => $object->{PrimaryOwnerName}
});

if (!$name) {
$inventory->setHardware({
NAME => $object->{Name},
});
}


}

foreach my $object (getWmiObjects(
Expand Down
2 changes: 1 addition & 1 deletion lib/FusionInventory/Agent/Tools.pm
Expand Up @@ -185,7 +185,7 @@ sub getSanitizedString {
$string =~ s/[[:cntrl:]]//g;

# encode to utf-8 if needed
if ($string !~ m/\A(
if (!Encode::is_utf8($string) && $string !~ m/\A(
[\x09\x0A\x0D\x20-\x7E] # ASCII
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
Expand Down
2 changes: 1 addition & 1 deletion lib/FusionInventory/Agent/Tools/Hostname.pm
Expand Up @@ -35,7 +35,7 @@ sub getHostname {

# GetComputerNameExW returns the string in UTF16, we have to change it
# to UTF8
return encode("UTF-8", substr(decode("UCS-2le", $buffer), 0, ord $n));
return substr(decode("UCS-2le", $buffer), 0, ord $n);
}

1;
Expand Down
58 changes: 58 additions & 0 deletions lib/FusionInventory/Agent/Tools/Win32.pm
Expand Up @@ -18,6 +18,10 @@ use Win32::TieRegistry (
qw/KEY_READ/
);

use File::Temp ();
use File::Temp qw/ :seekable /;
use Win32::Job;

Win32::OLE->Option(CP => Win32::OLE::CP_UTF8);

use FusionInventory::Agent::Tools;
Expand All @@ -33,6 +37,7 @@ our @EXPORT = qw(
getRegistryKey
getWmiObjects
getLocalCodepage
runCommand
);

sub is64bit {
Expand Down Expand Up @@ -150,6 +155,39 @@ sub _getRegistryKey {
return $key;
}

sub runCommand {
my (%params) = @_;


my $command = $params{command};
my $timeout = $params{timeout} || 3600*2;

my $job = Win32::Job->new;

my $buff = File::Temp->new();
my $void = File::Temp->new();


my $args = { stdout => $buff, no_window => 1 };
$args->{stderr} = $params{no_stderr}?$void:$buff;

$job->spawn($ENV{SYSTEMROOT}.'/system32/cmd.exe', "cmd.exe /c $command", $args);

$job->run($timeout);

$buff->seek(0, SEEK_SET);

my $exitcode;

my ($status) = $job->status();
foreach my $pid (%$status) {
$exitcode = $status->{$pid}{exitcode};
last;
}

return ($exitcode, $buff);
}

1;
__END__
Expand Down Expand Up @@ -216,3 +254,23 @@ E.g: HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVersion
=item logger
=back
=head2 runCommand(%params)
Returns a command in a Win32 Process
=over
=item command the command to run
=item timeout a time in second, default is 3600*2
=item no_stderr ignore STDERR output, default is false
=back
Return an array
=item exitcode the error code, 293 means a timeout occurred
=item fd a file descriptor on the output
6 changes: 6 additions & 0 deletions t/fake/windows/Win32/Job.pm
@@ -0,0 +1,6 @@
package win32::Job;

use strict;
use warnings;

1;
44 changes: 44 additions & 0 deletions t/tools/win32.t
@@ -0,0 +1,44 @@
#!/usr/bin/perl

use strict;
use warnings;

use English qw(-no_match_vars);

use Test::More;

BEGIN {
# use mock modules for non-available ones
push @INC, 't/fake/windows' if $OSNAME ne 'MSWin32';
}

use FusionInventory::Agent::Tools::Win32;

if ($OSNAME ne 'MSWin32') {
plan skip_all => 'depend on Win32';
} else {
plan tests => 5;
}
my ($code, $fd) = runCommand(command => "perl -V");
ok($code eq 0, "perl -V returns 0");
my $seemOk;
foreach (<$fd>) {
$seemOk=1 if /Summary of my perl5/;
}
ok($seemOk eq 1, "perl -V output looks good");
($code, $fd) = runCommand(
timeout => 1,
command => "perl -e\"sleep 10\""
);
ok($code eq 293, "timeout=1: timeout catched");
my $command = "perl -BAD";
($code, $fd) = runCommand(
command => $command,
no_stderr => 1
);
ok(!defined(<$fd>), "no_stderr=1: don't catch STDERR output");
($code, $fd) = runCommand(
command => $command,
no_stderr => 0
);
ok(defined(<$fd>), "no_stderr=0: catch STDERR output");

0 comments on commit c17bfdc

Please sign in to comment.