state.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IO::Select;
  5. use File::Basename;
  6. use File::Temp qw( tempfile );
  7. use Sys::Hostname;
  8. use IPC::System::Simple qw( system capture );
  9. # Local modules
  10. use Cwd qw(abs_path);
  11. use File::Basename qw(dirname);
  12. use lib dirname(abs_path($0));
  13. use DMX;
  14. # Prototypes
  15. sub mtime($);
  16. # User config
  17. my %DISPLAY_DEVS = ('TV' => 1, 'PROJECTOR' => 1);
  18. my $DISPLAY = undef();
  19. my $STATE_TIMEOUT = 180;
  20. my $EXISTS_TIMEOUT = 900;
  21. my %MON_FILES = ();
  22. # Host-specific config
  23. # This is necessary to avoid contention on shared disks
  24. my $HOST = Sys::Hostname::hostname();
  25. if ($HOST =~ /loki/i) {
  26. # Ensure the media path is available before we get going
  27. my $MEDIA_PATH = undef();
  28. {
  29. my $mount = $ENV{'HOME'} . '/bin/video/mediaPath';
  30. $MEDIA_PATH = capture($mount);
  31. }
  32. if (!$MEDIA_PATH || !-d $MEDIA_PATH) {
  33. die("Unable to access media path\n");
  34. }
  35. # Garage door opener
  36. $MON_FILES{ $MEDIA_PATH . '/DMX/cmd/GARAGE_CMD' } = 'EXISTS-VALUE-CLEAR';
  37. }
  38. # Available state files
  39. {
  40. # Plex
  41. $MON_FILES{'PLEX'} = 'NONE';
  42. $MON_FILES{'GUI'} = 'NONE';
  43. $MON_FILES{'PLAYING'} = 'PLAYING';
  44. # RiffTrax
  45. $MON_FILES{'RIFF'} = 'VALUE-NOUPDATE';
  46. # Motion detection
  47. $MON_FILES{'MOTION'} = 'MTIME';
  48. $MON_FILES{'MOTION_STAIRS'} = 'MTIME-NOUPDATE';
  49. $MON_FILES{'NO_MOTION'} = 'EXISTS-NOUPDATE';
  50. # Projector
  51. $MON_FILES{'PROJECTOR'} = 'STATUS';
  52. $MON_FILES{'PROJECTOR_COLOR'} = 'VALUE-NOUPDATE';
  53. $MON_FILES{'PROJECTOR_INPUT'} = 'VALUE-NOUPDATE';
  54. $MON_FILES{'PROJECTOR_LAMP'} = 'VALUE-NOUPDATE';
  55. # Amplifier
  56. $MON_FILES{'AMPLIFIER'} = 'STATUS';
  57. $MON_FILES{'AMPLIFIER_VOL'} = 'VALUE-NOUPDATE';
  58. $MON_FILES{'AMPLIFIER_MODE'} = 'VALUE-NOUPDATE';
  59. $MON_FILES{'AMPLIFIER_INPUT'} = 'VALUE-NOUPDATE';
  60. $MON_FILES{'AUDIO_AMP'} = 'EXISTS-ON';
  61. $MON_FILES{'STEREO_CMD'} = 'EXISTS-ON';
  62. # A/V Effects
  63. $MON_FILES{'LIGHTS'} = 'EXISTS-TIMEOUT';
  64. $MON_FILES{'BRIGHT'} = 'EXISTS-TIMEOUT';
  65. $MON_FILES{'RAVE'} = 'EXISTS';
  66. $MON_FILES{'EFFECT'} = 'EXISTS';
  67. # Equipment
  68. $MON_FILES{'FAN_CMD'} = 'EXISTS-ON';
  69. # OS State
  70. $MON_FILES{'FRONT_APP'} = 'VALUE-NOUPDATE';
  71. $MON_FILES{'COLOR'} = 'VALUE-NOUPDATE';
  72. $MON_FILES{'AUDIO'} = 'VALUE-NOUPDATE';
  73. $MON_FILES{'AUDIO_STATE'} = 'VALUE-NOUPDATE';
  74. # TV
  75. $MON_FILES{'TV'} = 'STATUS';
  76. $MON_FILES{'TV_VOL'} = 'VALUE';
  77. }
  78. my %EXTRAS = (
  79. 'PLAYING' => {
  80. 'URL' => qr/^file:(.+)$/m,
  81. 'YEAR' => qr/^year:(\d+)/m,
  82. 'SERIES' => qr/^showtitle:(.+)$/m,
  83. 'SEASON' => qr/^season:(\d+)/m,
  84. 'EPISODE' => qr/^episode:(\d+)/m,
  85. 'TITLE' => qr/^title:(.+)$/m,
  86. 'LENGTH' => qr/^duration:(\d+)/m,
  87. 'POSITION' => qr/^time:(\d+(?:\.\d+)?)/m,
  88. 'TYPE' => qr/^type:(.+)$/m,
  89. 'IMAGE' => qr/^fanart:(.+)$/m,
  90. 'THUMB' => qr/^thumbnail:(.+)$/m,
  91. 'ALBUM' => qr/^album:(.+)$/m,
  92. 'ARTIST' => qr/^artist:(.+)$/m,
  93. 'SELECTION' => qr/^selection:(.+)$/m,
  94. 'WINDOW' => qr/^window:(.+)$/m,
  95. 'WINDOWID' => qr/^windowid:(\d+)/m,
  96. }
  97. );
  98. # App config
  99. my $SOCK_TIMEOUT = 5;
  100. my $DATA_DIR = DMX::dataDir();
  101. my $CMD_FILE = 'STATE';
  102. my $MAX_CMD_LEN = 4096;
  103. my $RESET_CMD = $ENV{'HOME'} . '/bin/video/dmx/reset.sh';
  104. my $PUSH_TIMEOUT = 20;
  105. # Debug
  106. my $DEBUG = 0;
  107. if ($ENV{'DEBUG'}) {
  108. $DEBUG = 1;
  109. }
  110. # Command-line arguments
  111. my ($DELAY) = @ARGV;
  112. if (!$DELAY) {
  113. $DELAY = 0.5;
  114. }
  115. # Socket init
  116. my $select = DMX::stateSocket($CMD_FILE);
  117. # Add the extras to the main file list
  118. foreach my $file (keys(%EXTRAS)) {
  119. if (exists($MON_FILES{$file})) {
  120. foreach my $extra (keys(%{ $EXTRAS{$file} })) {
  121. $MON_FILES{ $file . '_' . $extra } = 'NONE';
  122. }
  123. }
  124. }
  125. # Init the file tracking structure
  126. my %files = ();
  127. foreach my $name (keys(%MON_FILES)) {
  128. my %file = (
  129. 'name' => basename($name),
  130. 'type' => $MON_FILES{$name},
  131. 'path' => $DATA_DIR . $name,
  132. 'update' => 0,
  133. 'value' => 0,
  134. 'last' => 0,
  135. );
  136. # Allow absolute paths to override the $DATA_DIR path
  137. if ($name =~ /^\//) {
  138. $file{'path'} = $name;
  139. }
  140. # Record the directory name for folder monitoring
  141. $file{'dir'} = dirname($file{'path'});
  142. # Set the attribute bits, for use in later update handling logic
  143. my %attr = (
  144. 'available' => 0,
  145. 'status' => 0,
  146. 'exists' => 0,
  147. 'value' => 0,
  148. 'clear' => 0,
  149. 'clear_off' => 0,
  150. 'mtime' => 0,
  151. 'playing' => 0,
  152. 'no_update' => 0,
  153. );
  154. if ($file{'type'} =~ /\bSTATUS\b/i) {
  155. $attr{'status'} = 1;
  156. }
  157. if ($file{'type'} =~ /\bEXISTS\b/i) {
  158. $attr{'exists'} = 1;
  159. }
  160. if ($file{'type'} =~ /\bVALUE\b/i) {
  161. $attr{'value'} = 1;
  162. }
  163. if ($file{'type'} =~ /\bCLEAR\b/i) {
  164. $attr{'clear'} = 1;
  165. }
  166. if ($file{'type'} =~ /\bON\b/i) {
  167. $attr{'clear_off'} = 1;
  168. }
  169. if ($file{'type'} =~ /\bTIMEOUT\b/i) {
  170. $attr{'clear_timeout'} = 1;
  171. }
  172. if ($file{'type'} =~ /\bMTIME\b/i) {
  173. $attr{'mtime'} = 1;
  174. }
  175. if ($file{'type'} =~ /\bPLAYING\b/i) {
  176. $attr{'playing'} = 1;
  177. }
  178. if ($file{'type'} =~ /\bNOUPDATE\b/i) {
  179. $attr{'no_update'} = 1;
  180. }
  181. # Cross-match some data types for easy of use
  182. if ($attr{'value'} || $attr{'playing'}) {
  183. $attr{'status'} = 1;
  184. }
  185. if ($attr{'status'} || $attr{'exists'}) {
  186. $attr{'mtime'} = 1;
  187. }
  188. # Push ATTR into the file hash
  189. $file{'attr'} = \%attr;
  190. # Push FILE into the files hash
  191. $files{$name} = \%file;
  192. }
  193. # Delete any existing input files, to ensure our state is reset
  194. foreach my $file (values(%files)) {
  195. if (-e $file->{'path'}) {
  196. if ($DEBUG) {
  197. print STDERR 'Deleting ' . $file->{'name'} . ': ' . $file->{'path'} . "\n";
  198. }
  199. unlink($file->{'path'});
  200. }
  201. }
  202. # Reset all dependents at starts
  203. system($RESET_CMD);
  204. # Subscribers
  205. my @subscribers = ();
  206. # State
  207. my $state = 'INIT';
  208. my $stateLast = $state;
  209. my $status = '';
  210. my $statusLast = $status;
  211. my $updateLast = 0;
  212. my $timeSinceUpdate = 0;
  213. my $pushLast = 0;
  214. # Loop forever
  215. while (1) {
  216. # Provide a method to force updates even when the state does not change
  217. my $update = 0;
  218. # Check for queued commands
  219. my @ready_clients = $select->can_read($DELAY);
  220. foreach my $fh (@ready_clients) {
  221. # Grab the inbound text
  222. my $path = undef();
  223. $fh->recv($path, $MAX_CMD_LEN);
  224. $path =~ s/^\s+//;
  225. $path =~ s/\s+$//;
  226. $path =~ s/\W/_/g;
  227. if ($DEBUG) {
  228. print STDERR 'Got path: ' . $path . "\n";
  229. }
  230. # Open the new socket
  231. my $sub = eval { DMX::clientSock($path); };
  232. if (!$sub) {
  233. print STDERR 'Unable to open socket: ' . $path . ": ${@}\n";
  234. next;
  235. }
  236. # Add the socket to our subscriber list
  237. my %tmp = (
  238. 'path' => $path,
  239. 'socket' => $sub,
  240. );
  241. push(@subscribers, \%tmp);
  242. # Force an update
  243. $update = 1;
  244. }
  245. # Use opendir/readdir in each folder to ensure fresh results
  246. my %folders = ();
  247. foreach my $file (values(%files)) {
  248. if (!$folders{ $file->{'dir'} }) {
  249. my $dh = undef();
  250. opendir($dh, $file->{'dir'})
  251. or die('Unable to open directory: ' . $file->{'dir'});
  252. if ($DEBUG) {
  253. print STDERR 'Refreshed folder: ' . $file->{'dir'} . "\n";
  254. }
  255. my @files = readdir($dh);
  256. closedir($dh);
  257. $folders{ $file->{'dir'} } = 1;
  258. }
  259. }
  260. # Monitor files of all types
  261. foreach my $file (values(%files)) {
  262. # Skip "NONE" files -- they are data stores handled in other actions
  263. if ($file->{'type'} eq 'NONE') {
  264. next;
  265. }
  266. # Always record the previous status and clear the new one
  267. $file->{'last'} = $file->{'value'};
  268. $file->{'value'} = 0;
  269. # Track available/unavailable status in each cycle
  270. {
  271. my $wasAvailable = $file->{'attr'}->{'available'};
  272. $file->{'attr'}->{'available'} = -r $file->{'path'} ? 1 : 0;
  273. if ($wasAvailable != $file->{'attr'}->{'available'}) {
  274. # Reset and extras for this file
  275. if (exists($EXTRAS{ $file->{'name'} })) {
  276. foreach my $extra (keys(%{ $EXTRAS{ $file->{'name'} } })) {
  277. my $name = $file->{'name'} . '_' . $extra;
  278. $files{$name}{'last'} = $files{$name}{'value'};
  279. $files{$name}{'value'} = 0;
  280. }
  281. }
  282. # Determine which display device we have (if any)
  283. if (exists($DISPLAY_DEVS{ $file->{'name'} })) {
  284. $DISPLAY = $file->{'name'};
  285. if ($DEBUG) {
  286. print STDERR 'Selecting display device: ' . $DISPLAY . "\n";
  287. }
  288. }
  289. if ($DEBUG) {
  290. if ($file->{'attr'}->{'available'}) {
  291. print STDERR 'Added file ' . $file->{'name'} . ': ' . $file->{'path'} . "\n";
  292. } else {
  293. print STDERR 'Dropped file ' . $file->{'name'} . ': ' . $file->{'path'} . "\n";
  294. }
  295. }
  296. }
  297. }
  298. # Skip unavailable files
  299. if (!$file->{'attr'}->{'available'}) {
  300. next;
  301. }
  302. # Record the last update for MTIME files
  303. if ($file->{'attr'}->{'mtime'}) {
  304. my $mtime = mtime($file->{'path'});
  305. # All EXISTS files have MTIME set, and mtime returns 0 if the file does not exist, so we can overload this check
  306. if ($file->{'attr'}->{'exists'}) {
  307. if ($mtime > 0) {
  308. $file->{'update'} = $mtime;
  309. $file->{'value'} = 1;
  310. } elsif ($file->{'last'}) {
  311. $file->{'update'} = time();
  312. }
  313. } else {
  314. $file->{'update'} = $mtime;
  315. }
  316. if ($DEBUG) {
  317. print STDERR 'Mtime: ' . $file->{'name'} . ': ' . $file->{'value'} . ':' . $file->{'update'} . "\n";
  318. }
  319. }
  320. # Grab the state from STATUS files
  321. if ($file->{'attr'}->{'status'}) {
  322. my $text = '';
  323. {
  324. if (!open(my $fh, $file->{'path'})) {
  325. warn('Unable to open ' . $file->{'path'} . "\n");
  326. } else {
  327. local $/;
  328. $text = <$fh>;
  329. close($fh);
  330. }
  331. }
  332. # Parse the value for the file
  333. if ($file->{'attr'}->{'value'}) {
  334. $text =~ s/\n$//;
  335. $file->{'value'} = $text;
  336. } elsif ($file->{'attr'}->{'playing'}) {
  337. if ($text =~ /^playing:1/m) {
  338. $file->{'value'} = 1;
  339. }
  340. } else {
  341. if ($text =~ /1/) {
  342. $file->{'value'} = 1;
  343. }
  344. }
  345. if ($DEBUG) {
  346. print STDERR 'Status: ' . $file->{'name'} . ': ' . $file->{'value'} . "\n";
  347. }
  348. # Handle extras, if any
  349. if (exists($EXTRAS{ $file->{'name'} })) {
  350. foreach my $extra (keys(%{ $EXTRAS{ $file->{'name'} } })) {
  351. my $name = $file->{'name'} . '_' . $extra;
  352. $files{$name}{'last'} = $files{$name}{'value'};
  353. $files{$name}{'value'} = 0;
  354. $files{$name}{'update'} = $file->{'update'};
  355. if ($text =~ $EXTRAS{ $file->{'name'} }{$extra}) {
  356. $files{$name}{'value'} = $1;
  357. }
  358. if ($DEBUG) {
  359. print STDERR 'Status (extra): ' . $name . ': ' . $files{$name}{'value'} . "\n";
  360. }
  361. }
  362. }
  363. }
  364. }
  365. # Simulate the old GUI indicator
  366. {
  367. my $gui = 0;
  368. if (exists($files{'PLAYING_WINDOW'}) && $files{'PLAYING_WINDOW'}->{'value'} ne 'Fullscreen video') {
  369. $gui = 1;
  370. }
  371. $files{'GUI'}->{'value'} = $gui;
  372. }
  373. if (exists($files{'PLAYING'})) {
  374. $files{'GUI'}->{'update'} = $files{'PLAYING'}->{'update'};
  375. }
  376. # Ignore the MOTION file if NO_MOTION is set
  377. if (exists($files{'NO_MOTION'}) && $files{'NO_MOTION'}->{'value'}) {
  378. if (exists($files{'MOTION'})) {
  379. $files{'MOTION'}->{'value'} = 0;
  380. $files{'MOTION'}->{'update'} = 0;
  381. }
  382. }
  383. # Set the global update timestamp, excluding files marked NOUPDATE
  384. foreach my $file (values(%files)) {
  385. if ($file->{'update'} > $updateLast && !$file->{'attr'}->{'no_update'}) {
  386. $updateLast = $file->{'update'};
  387. }
  388. }
  389. $timeSinceUpdate = time() - $updateLast;
  390. # Calculate the PLEX state
  391. $files{'PLEX'}->{'last'} = $files{'PLEX'}->{'value'};
  392. $files{'PLEX'}->{'value'} = 0;
  393. if (exists($files{'FRONT_APP'})) {
  394. if ( $files{'FRONT_APP'}->{'value'} =~ 'com.plexapp.plexhometheater'
  395. || $files{'FRONT_APP'}->{'value'} eq 'com.apple.ScreenSaver.Engine')
  396. {
  397. $files{'PLEX'}->{'value'} = 1;
  398. }
  399. }
  400. if ($files{'PLEX'}->{'last'} != $files{'PLEX'}->{'value'}) {
  401. $files{'PLEX'}->{'update'} = time();
  402. }
  403. # Determine some intermediate state data
  404. my $playing = 0;
  405. if (exists($files{'PLAYING'}) && $files{'PLAYING'}->{'value'}) {
  406. $playing = 1;
  407. }
  408. my $video = 0;
  409. if (exists($files{'PLAYING_TYPE'}) && $files{'PLAYING_TYPE'}->{'value'} ne 'audio') {
  410. $video = 1;
  411. } elsif (!exists($files{'PLAYING_TYPE'})) {
  412. $video = 1;
  413. }
  414. # Non-plex apps are always "playing" and "video"
  415. if (exists($files{'PLEX'}) && !$files{'PLEX'}->{'value'}) {
  416. $playing = 1;
  417. $video = 1;
  418. }
  419. # Calculate the new state
  420. $stateLast = $state;
  421. if (!defined($DISPLAY) || !exists($files{$DISPLAY})) {
  422. # If there's no display the state is always PLAY or PAUSE, as indicated by the master
  423. if ($playing) {
  424. $state = 'PLAY';
  425. } else {
  426. $state = 'PAUSE';
  427. }
  428. } elsif ($files{$DISPLAY}->{'value'}) {
  429. # If a display exists and is on:
  430. # PLAY when video is active and playing
  431. # PAUSE any other time (including when audio is active)
  432. if ($playing && $video) {
  433. $state = 'PLAY';
  434. } else {
  435. $state = 'PAUSE';
  436. }
  437. } else {
  438. # If the display exists but is off, check the timeouts to determine MOTION vs. OFF
  439. if ($timeSinceUpdate > $STATE_TIMEOUT) {
  440. $state = 'OFF';
  441. } elsif ($timeSinceUpdate < $STATE_TIMEOUT) {
  442. $state = 'MOTION';
  443. }
  444. }
  445. if ($state eq 'INIT') {
  446. $state = 'OFF';
  447. }
  448. # Clear EXISTS_ON files when the main state is "OFF" (and before we append their status)
  449. foreach my $file (values(%files)) {
  450. if ( $file->{'attr'}->{'clear_off'}
  451. && $file->{'value'}
  452. && $state eq 'OFF')
  453. {
  454. unlink($file->{'path'});
  455. $file->{'status'} = 0;
  456. if ($DEBUG) {
  457. print STDERR 'Clearing exists flag for: ' . $file->{'name'} . "\n";
  458. }
  459. }
  460. }
  461. # Clear EXISTS_TIMEOUT files when the main state is "OFF" and EXISTS_TIMEOUT has expired (and before we append their status)
  462. foreach my $file (values(%files)) {
  463. if ( $file->{'attr'}->{'clear_timeout'}
  464. && $file->{'value'}
  465. && $state eq 'OFF'
  466. && $timeSinceUpdate > $EXISTS_TIMEOUT)
  467. {
  468. unlink($file->{'path'});
  469. $file->{'status'} = 0;
  470. if ($DEBUG) {
  471. print STDERR 'Clearing exists flag for: ' . $file->{'name'} . "\n";
  472. }
  473. }
  474. }
  475. # Calculate the new status
  476. $statusLast = $status;
  477. {
  478. my @statTime = ();
  479. foreach my $file (values(%files)) {
  480. my $text = $file->{'value'};
  481. $text =~ s/\s/ /g;
  482. $text =~ s/\|/-/g;
  483. push(@statTime, $file->{'name'} . '|' . $text . '|' . $file->{'update'});
  484. }
  485. $status = "\n" . join("\n", @statTime);
  486. }
  487. # Clear EXISTS_CLEAR files immediately (but after we append their status)
  488. foreach my $file (values(%files)) {
  489. if ($file->{'attr'}->{'clear'} && $file->{'value'}) {
  490. unlink($file->{'path'});
  491. $file->{'status'} = 0;
  492. if ($DEBUG) {
  493. print STDERR 'Clearing exists flag for: ' . $file->{'name'} . "\n";
  494. }
  495. }
  496. }
  497. # Force updates on a periodic basis
  498. if (time() - $pushLast > $PUSH_TIMEOUT) {
  499. $update = 1;
  500. }
  501. # Update on any status change
  502. foreach my $file (values(%files)) {
  503. if ($file->{'value'} ne $file->{'last'}) {
  504. $update = 1;
  505. last;
  506. }
  507. }
  508. # Force updates on any state change
  509. if ($stateLast ne $state || $statusLast ne $status) {
  510. if ($DEBUG) {
  511. print STDERR 'State change: ' . $stateLast . $statusLast . ' => ' . $state . $status . "\n";
  512. }
  513. $update = 1;
  514. }
  515. # Push the update
  516. if ($update) {
  517. # Note the push
  518. $pushLast = time();
  519. # Send notifications to all subscribers
  520. foreach my $sub (@subscribers) {
  521. # Drop subscribers that are not available
  522. if (!eval { $sub->{'socket'}->send($state . $status) }) {
  523. print STDERR 'Dropping bad socket from subscriber list: ' . $sub->{'path'} . "\n";
  524. my @new_subscribers = ();
  525. foreach my $new (@subscribers) {
  526. if ($new->{'socket'} eq $sub->{'socket'}) {
  527. next;
  528. }
  529. push(@new_subscribers, $new);
  530. }
  531. @subscribers = @new_subscribers;
  532. }
  533. }
  534. # Save the state and value to disk
  535. my ($fh, $tmp) = tempfile($DATA_DIR . 'STATE.XXXXXXXX', 'UNLINK' => 0);
  536. print $fh $state . "\n";
  537. close($fh);
  538. rename($tmp, $DATA_DIR . 'STATE');
  539. }
  540. }
  541. sub mtime($) {
  542. my ($file) = @_;
  543. my $mtime = 0;
  544. if (-r $file) {
  545. (undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), $mtime, undef(), undef(), undef()) = stat($file);
  546. }
  547. return $mtime;
  548. }