Browse Source

add file_time helper

Byron Jones 6 years ago
parent
commit
5f5e192ca3
5 changed files with 17 additions and 12 deletions
  1. 3 3
      dev-precommit
  2. 4 4
      lib/LogBot/Config.pm
  3. 6 1
      lib/LogBot/Util.pm
  4. 2 2
      logbot-redirect
  5. 2 2
      logbot-web

+ 3 - 3
dev-precommit

@@ -11,7 +11,7 @@ use Cwd qw( abs_path );
 use File::Basename qw( basename );
 use File::Find qw( find );
 use List::Util qw( any );
-use LogBot::Util qw( run );
+use LogBot::Util qw( file_time run );
 use Mojo::File ();
 use Mojo::Util qw( trim );
 use Perl::Critic ();
@@ -337,7 +337,7 @@ sub is_modified {
     my ($file) = @_;
     _init_modified();
 
-    my $mtime    = (stat($file))[9];
+    my $mtime    = file_time($file);
     my $abs_file = abs_path($file);
     $lastmod->{$abs_file} //= 0;
 
@@ -348,7 +348,7 @@ sub set_modified {
     my ($file) = @_;
     _init_modified();
 
-    $lastmod->{ abs_path($file) } = (stat($file))[9];
+    $lastmod->{ abs_path($file) } = file_time($file);
     _write_modified();
 }
 

+ 4 - 4
lib/LogBot/Config.pm

@@ -9,7 +9,7 @@ use lib "$RealBin/lib";
 
 use File::Basename qw( basename );
 use File::Path qw( make_path );
-use LogBot::Util qw( normalise_channel path_for squash_error timestamp );
+use LogBot::Util qw( file_time normalise_channel path_for squash_error timestamp );
 use Try::Tiny qw( catch try );
 use YAML::Tiny ();
 
@@ -64,7 +64,7 @@ sub load_config {
     $config->{_derived} = {
         file     => $config_file,
         root     => glob(q{'} . $config->{path} . q{'}),         # expand ~
-        time     => (stat($config_file))[9],
+        time     => file_time($config_file),
         web      => $params{web},
         readonly => $params{web},
         is_dev   => substr(basename($config_file), 0, 1) eq '_',
@@ -105,7 +105,7 @@ sub load_config {
 sub reload_config {
     my ($config) = @_;
     my $config_file = $config->{_derived}->{file};
-    return (stat($config_file))[9] == $config->{_derived}->{time}
+    return file_time($config_file) == $config->{_derived}->{time}
         ? $config
         : load_config($config_file, web => $config->{_derived}->{web});
 }
@@ -152,7 +152,7 @@ sub load_all_configs {
 
     my $hash = '';
     foreach my $file (@files) {
-        $hash .= $file . '.' . ((stat($file))[9]);
+        $hash .= $file . '.' . file_time($file);
     }
 
     if ($all_configs_hash ne $hash) {

+ 6 - 1
lib/LogBot/Util.pm

@@ -10,7 +10,7 @@ our @EXPORT_OK = qw(
     logbot_init
     nick_is_bot
     normalise_channel source_to_nick
-    slurp spurt touch
+    slurp spurt touch file_time
     squash_error
     date_string_to_ymd timestamp time_to_ymd time_to_datestr time_to_datetimestr ymd_to_time
     path_for file_for
@@ -154,6 +154,11 @@ sub touch {
     }
 }
 
+sub file_time {
+    my ($file) = @_;
+    return -e $file ? (stat($file))[9] : undef;
+}
+
 sub file_for {
     my ($config, $type, @params) = @_;
 

+ 2 - 2
logbot-redirect

@@ -10,7 +10,7 @@ use lib "$RealBin/lib";
 BEGIN { $ENV{TZ} = 'UTC' }
 
 use LogBot::Config qw( find_config load_config );
-use LogBot::Web::Util qw( rewrite_old_urls );
+use LogBot::Web::Util qw( file_time rewrite_old_urls );
 use Mojo::URL ();
 use Mojolicious::Lite qw( app );
 
@@ -51,7 +51,7 @@ sub redirect {
 my %cache;
 helper static => sub {
     my ($self, $file) = @_;
-    return $cache{static}->{$file} //= '/static/' . $file . '?' . (stat($RealBin . '/web/public/static/' . $file))[9];
+    return $cache{static}->{$file} //= '/static/' . $file . '?' . file_time($RealBin . '/web/public/static/' . $file);
 };
 
 # configure mojo

+ 2 - 2
logbot-web

@@ -20,7 +20,7 @@ use DateTime ();
 use IO::Compress::Gzip qw( gzip );
 use LogBot::Config qw( find_config load_all_configs load_config reload_config );
 use LogBot::MemCache ();
-use LogBot::Util qw( time_to_ymd );
+use LogBot::Util qw( file_time time_to_ymd );
 use LogBot::Web::Util qw( channel_from_param channel_topics linkify nick_colour_init render_init rewrite_old_urls );
 use Mojo::ByteStream ();
 use Mojo::Util qw( dumper );
@@ -253,7 +253,7 @@ my %cache;
 # static file with timestamp
 helper static => sub {
     my ($c, $file) = @_;
-    return $cache{static}->{$file} //= '/static/' . $file . '?' . (stat($RealBin . '/web/public/static/' . $file))[9];
+    return $cache{static}->{$file} //= '/static/' . $file . '?' . file_time($RealBin . '/web/public/static/' . $file);
 };
 
 # inline svg