nixos/install-grub: don't use bare file handles and 3 argument open
this is not best practice perl since a long time. <!-- ps-id: 225e1b18-348c-412f-8ecd-394f3ba9a32d -->
This commit is contained in:
parent
8d5abcd1bb
commit
33c2472b69
@ -37,7 +37,8 @@ sub readFile {
|
|||||||
my ($fn) = @_;
|
my ($fn) = @_;
|
||||||
# enable slurp mode: read entire file in one go
|
# enable slurp mode: read entire file in one go
|
||||||
local $/ = undef;
|
local $/ = undef;
|
||||||
open my $fh, "<$fn" or return undef;
|
open my $fh, "<", $fn
|
||||||
|
or return;
|
||||||
my $s = <$fh>;
|
my $s = <$fh>;
|
||||||
close $fh;
|
close $fh;
|
||||||
# disable slurp mode
|
# disable slurp mode
|
||||||
@ -48,7 +49,7 @@ sub readFile {
|
|||||||
|
|
||||||
sub writeFile {
|
sub writeFile {
|
||||||
my ($fn, $s) = @_;
|
my ($fn, $s) = @_;
|
||||||
open my $fh, ">$fn" or die "cannot create $fn: $!\n";
|
open my $fh, ">", $fn or die "cannot create $fn: $!\n";
|
||||||
print $fh $s or die "cannot write to $fn: $!\n";
|
print $fh $s or die "cannot write to $fn: $!\n";
|
||||||
close $fh or die "cannot close $fn: $!\n";
|
close $fh or die "cannot close $fn: $!\n";
|
||||||
}
|
}
|
||||||
@ -690,17 +691,17 @@ struct(GrubState => {
|
|||||||
# because it is read line-by-line.
|
# because it is read line-by-line.
|
||||||
sub readGrubState {
|
sub readGrubState {
|
||||||
my $defaultGrubState = GrubState->new(name => "", version => "", efi => "", devices => "", efiMountPoint => "", extraGrubInstallArgs => () );
|
my $defaultGrubState = GrubState->new(name => "", version => "", efi => "", devices => "", efiMountPoint => "", extraGrubInstallArgs => () );
|
||||||
open FILE, "<$bootPath/grub/state" or return $defaultGrubState;
|
open my $fh, "<", "$bootPath/grub/state" or return $defaultGrubState;
|
||||||
local $/ = "\n";
|
local $/ = "\n";
|
||||||
my $name = <FILE>;
|
my $name = <$fh>;
|
||||||
chomp($name);
|
chomp($name);
|
||||||
my $version = <FILE>;
|
my $version = <$fh>;
|
||||||
chomp($version);
|
chomp($version);
|
||||||
my $efi = <FILE>;
|
my $efi = <$fh>;
|
||||||
chomp($efi);
|
chomp($efi);
|
||||||
my $devices = <FILE>;
|
my $devices = <$fh>;
|
||||||
chomp($devices);
|
chomp($devices);
|
||||||
my $efiMountPoint = <FILE>;
|
my $efiMountPoint = <$fh>;
|
||||||
chomp($efiMountPoint);
|
chomp($efiMountPoint);
|
||||||
# Historically, arguments in the state file were one per each line, but that
|
# Historically, arguments in the state file were one per each line, but that
|
||||||
# gets really messy when newlines are involved, structured arguments
|
# gets really messy when newlines are involved, structured arguments
|
||||||
@ -708,7 +709,7 @@ sub readGrubState {
|
|||||||
# when we need to remove a setting in the future. Thus, the 6th line is a JSON
|
# when we need to remove a setting in the future. Thus, the 6th line is a JSON
|
||||||
# object that can store structured data, with named keys, and all new state
|
# object that can store structured data, with named keys, and all new state
|
||||||
# should go in there.
|
# should go in there.
|
||||||
my $jsonStateLine = <FILE>;
|
my $jsonStateLine = <$fh>;
|
||||||
# For historical reasons we do not check the values above for un-definedness
|
# For historical reasons we do not check the values above for un-definedness
|
||||||
# (that is, when the state file has too few lines and EOF is reached),
|
# (that is, when the state file has too few lines and EOF is reached),
|
||||||
# because the above come from the first version of this logic and are thus
|
# because the above come from the first version of this logic and are thus
|
||||||
@ -720,7 +721,7 @@ sub readGrubState {
|
|||||||
}
|
}
|
||||||
my %jsonState = %{decode_json($jsonStateLine)};
|
my %jsonState = %{decode_json($jsonStateLine)};
|
||||||
my @extraGrubInstallArgs = exists($jsonState{'extraGrubInstallArgs'}) ? @{$jsonState{'extraGrubInstallArgs'}} : ();
|
my @extraGrubInstallArgs = exists($jsonState{'extraGrubInstallArgs'}) ? @{$jsonState{'extraGrubInstallArgs'}} : ();
|
||||||
close FILE;
|
close $fh;
|
||||||
my $grubState = GrubState->new(name => $name, version => $version, efi => $efi, devices => $devices, efiMountPoint => $efiMountPoint, extraGrubInstallArgs => \@extraGrubInstallArgs );
|
my $grubState = GrubState->new(name => $name, version => $version, efi => $efi, devices => $devices, efiMountPoint => $efiMountPoint, extraGrubInstallArgs => \@extraGrubInstallArgs );
|
||||||
return $grubState
|
return $grubState
|
||||||
}
|
}
|
||||||
@ -787,18 +788,18 @@ if ($requireNewInstall != 0) {
|
|||||||
my $stateFile = "$bootPath/grub/state";
|
my $stateFile = "$bootPath/grub/state";
|
||||||
my $stateFileTmp = $stateFile . ".tmp";
|
my $stateFileTmp = $stateFile . ".tmp";
|
||||||
|
|
||||||
open FILE, ">$stateFileTmp" or die "cannot create $stateFileTmp: $!\n";
|
open my $fh, ">", "$stateFileTmp" or die "cannot create $stateFileTmp: $!\n";
|
||||||
print FILE get("fullName"), "\n" or die;
|
print $fh get("fullName"), "\n" or die;
|
||||||
print FILE get("fullVersion"), "\n" or die;
|
print $fh get("fullVersion"), "\n" or die;
|
||||||
print FILE $efiTarget, "\n" or die;
|
print $fh $efiTarget, "\n" or die;
|
||||||
print FILE join( ",", @deviceTargets ), "\n" or die;
|
print $fh join( ",", @deviceTargets ), "\n" or die;
|
||||||
print FILE $efiSysMountPoint, "\n" or die;
|
print $fh $efiSysMountPoint, "\n" or die;
|
||||||
my %jsonState = (
|
my %jsonState = (
|
||||||
extraGrubInstallArgs => \@extraGrubInstallArgs
|
extraGrubInstallArgs => \@extraGrubInstallArgs
|
||||||
);
|
);
|
||||||
my $jsonStateLine = encode_json(\%jsonState);
|
my $jsonStateLine = encode_json(\%jsonState);
|
||||||
print FILE $jsonStateLine, "\n" or die;
|
print $fh $jsonStateLine, "\n" or die;
|
||||||
close FILE or die;
|
close $fh or die;
|
||||||
|
|
||||||
# Atomically switch to the new state file
|
# Atomically switch to the new state file
|
||||||
rename $stateFileTmp, $stateFile or die "cannot rename $stateFileTmp to $stateFile: $!\n";
|
rename $stateFileTmp, $stateFile or die "cannot rename $stateFileTmp to $stateFile: $!\n";
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user