diff --git a/lib/conf.pm b/lib/conf.pm index d7dbadd..2844881 100644 --- a/lib/conf.pm +++ b/lib/conf.pm @@ -2,9 +2,10 @@ package conf; use strict; use warnings "all"; -use diagnostics; use vars qw/$VERSION/; -use JSON::PP; +use Fcntl qw(O_WRONLY O_CREAT O_TRUNC); +use JSON::XS; +use utf8; use Exporter qw(import); our @EXPORT = qw(loadConf saveConf); @@ -13,10 +14,21 @@ $VERSION = "1.0"; sub loadConf() { my $c = "data/myapi.json"; - my $sep = $/; - $/ = ''; - open (C, $c) or die "No conf at $c\n"; - my $json = ; + open (C, "<", $c) or die "[FATA] No conf at $c: $!\n"; + my $len = (stat($c))[7]; + my $json; + my $readlen = read(C, $json, $len); + + unless ($readlen) { + close C; + die "[FATA] Unable to read $c: $!\n"; + } + + if ($readlen != $len) { + close C; + die "[FATA] File $c is $len bytes on disk, but we read only $readlen bytes\n"; + } + close C; return decode_json($json); } @@ -24,14 +36,29 @@ sub loadConf() { sub saveConf($) { my $c = shift; my $file = "data/myapi.json"; - my $j = JSON::PP->new->pretty->canonical->indent_length(4); + my $j = JSON::XS->new->pretty->canonical->indent(1); my $json = $j->encode($c); + $j = undef; undef $j; + use bytes; + my $len = length($json); + no bytes; # TODO: make it transactional - open (C, ">", $file) or die "Unable to open $file\n"; - print C $json; + sysopen (C, $file, O_WRONLY|O_CREAT|O_TRUNC) or die "[FATA] Unable to open $file: $!\n"; + binmode C, ':utf8'; + + my $written = syswrite(C, $json, $len); + + unless (defined($written)) { + die "[FATA] Unable to write to $file: $!"; + } + + unless ($written != $len) { + die "[FATA] We wrote $written bytes to $file, bu buffer length id $len bytes\n"; + } + close C; } 1; -# vim: set ft=perl noet ai ts=4 sw=4 sts=4: \ No newline at end of file +# vim: set ft=perl noet ai ts=4 sw=4 sts=4: