summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/filecheck.pm
blob: 01d4909612d289d905905d303af41853ef903a56 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::filecheck;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 3.00;
  6. my %units=( #{{{ # size in bytes
  7. B => 1,
  8. byte => 1,
  9. KB => 2 ** 10,
  10. kilobyte => 2 ** 10,
  11. K => 2 ** 10,
  12. KB => 2 ** 10,
  13. kilobyte => 2 ** 10,
  14. M => 2 ** 20,
  15. MB => 2 ** 20,
  16. megabyte => 2 ** 20,
  17. G => 2 ** 30,
  18. GB => 2 ** 30,
  19. gigabyte => 2 ** 30,
  20. T => 2 ** 40,
  21. TB => 2 ** 40,
  22. terabyte => 2 ** 40,
  23. P => 2 ** 50,
  24. PB => 2 ** 50,
  25. petabyte => 2 ** 50,
  26. E => 2 ** 60,
  27. EB => 2 ** 60,
  28. exabyte => 2 ** 60,
  29. Z => 2 ** 70,
  30. ZB => 2 ** 70,
  31. zettabyte => 2 ** 70,
  32. Y => 2 ** 80,
  33. YB => 2 ** 80,
  34. yottabyte => 2 ** 80,
  35. # ikiwiki, if you find you need larger data quantities, either modify
  36. # yourself to add them, or travel back in time to 2008 and kill me.
  37. # -- Joey
  38. );
  39. sub parsesize ($) {
  40. my $size=shift;
  41. no warnings;
  42. my $base=$size+0; # force to number
  43. use warnings;
  44. foreach my $unit (sort keys %units) {
  45. if ($size=~/[0-9\s]\Q$unit\E$/i) {
  46. return $base * $units{$unit};
  47. }
  48. }
  49. return $base;
  50. }
  51. # This is provided for other plugins that want to convert back the other way.
  52. sub humansize ($) {
  53. my $size=shift;
  54. foreach my $unit (reverse sort { $units{$a} <=> $units{$b} || $b cmp $a } keys %units) {
  55. if ($size / $units{$unit} > 0.25) {
  56. return (int($size / $units{$unit} * 10)/10).$unit;
  57. }
  58. }
  59. return $size; # near zero, or negative
  60. }
  61. package IkiWiki::PageSpec;
  62. sub match_maxsize ($$;@) {
  63. my $page=shift;
  64. my $maxsize=eval{IkiWiki::Plugin::filecheck::parsesize(shift)};
  65. if ($@) {
  66. return IkiWiki::ErrorReason->new("unable to parse maxsize (or number too large)");
  67. }
  68. my %params=@_;
  69. my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
  70. if (! defined $file) {
  71. return IkiWiki::ErrorReason->new("no file specified");
  72. }
  73. if (-s $file > $maxsize) {
  74. return IkiWiki::FailReason->new("file too large (".(-s $file)." > $maxsize)");
  75. }
  76. else {
  77. return IkiWiki::SuccessReason->new("file not too large");
  78. }
  79. }
  80. sub match_minsize ($$;@) {
  81. my $page=shift;
  82. my $minsize=eval{IkiWiki::Plugin::filecheck::parsesize(shift)};
  83. if ($@) {
  84. return IkiWiki::ErrorReason->new("unable to parse minsize (or number too large)");
  85. }
  86. my %params=@_;
  87. my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
  88. if (! defined $file) {
  89. return IkiWiki::ErrorReason->new("no file specified");
  90. }
  91. if (-s $file < $minsize) {
  92. return IkiWiki::FailReason->new("file too small");
  93. }
  94. else {
  95. return IkiWiki::SuccessReason->new("file not too small");
  96. }
  97. }
  98. sub match_mimetype ($$;@) {
  99. my $page=shift;
  100. my $wanted=shift;
  101. my %params=@_;
  102. my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
  103. if (! defined $file) {
  104. return IkiWiki::ErrorReason->new("no file specified");
  105. }
  106. # Use ::magic to get the mime type, the idea is to only trust
  107. # data obtained by examining the actual file contents.
  108. eval q{use File::MimeInfo::Magic};
  109. if ($@) {
  110. return IkiWiki::ErrorReason->new("failed to load File::MimeInfo::Magic ($@); cannot check MIME type");
  111. }
  112. my $mimetype=File::MimeInfo::Magic::magic($file);
  113. if (! defined $mimetype) {
  114. $mimetype=File::MimeInfo::Magic::default($file);
  115. if (! defined $mimetype) {
  116. $mimetype="unknown";
  117. }
  118. }
  119. my $regexp=IkiWiki::glob2re($wanted);
  120. if ($mimetype!~/^$regexp$/i) {
  121. return IkiWiki::FailReason->new("file MIME type is $mimetype, not $wanted");
  122. }
  123. else {
  124. return IkiWiki::SuccessReason->new("file MIME type is $mimetype");
  125. }
  126. }
  127. sub match_virusfree ($$;@) {
  128. my $page=shift;
  129. my $wanted=shift;
  130. my %params=@_;
  131. my $file=exists $params{file} ? $params{file} : $IkiWiki::pagesources{$page};
  132. if (! defined $file) {
  133. return IkiWiki::ErrorReason->new("no file specified");
  134. }
  135. if (! exists $IkiWiki::config{virus_checker} ||
  136. ! length $IkiWiki::config{virus_checker}) {
  137. return IkiWiki::ErrorReason->new("no virus_checker configured");
  138. }
  139. # The file needs to be fed into the virus checker on stdin,
  140. # because the file is not world-readable, and if clamdscan is
  141. # used, clamd would fail to read it.
  142. eval q{use IPC::Open2};
  143. error($@) if $@;
  144. open (IN, "<", $file) || return IkiWiki::ErrorReason->new("failed to read file");
  145. binmode(IN);
  146. my $sigpipe=0;
  147. $SIG{PIPE} = sub { $sigpipe=1 };
  148. my $pid=open2(\*CHECKER_OUT, "<&IN", $IkiWiki::config{virus_checker});
  149. my $reason=<CHECKER_OUT>;
  150. chomp $reason;
  151. 1 while (<CHECKER_OUT>);
  152. close(CHECKER_OUT);
  153. waitpid $pid, 0;
  154. $SIG{PIPE}="DEFAULT";
  155. if ($sigpipe || $?) {
  156. if (! length $reason) {
  157. $reason="virus checker $IkiWiki::config{virus_checker}; failed with no output";
  158. }
  159. return IkiWiki::FailReason->new("file seems to contain a virus ($reason)");
  160. }
  161. else {
  162. return IkiWiki::SuccessReason->new("file seems virusfree ($reason)");
  163. }
  164. }
  165. sub match_ispage ($$;@) {
  166. my $filename=shift;
  167. if (defined IkiWiki::pagetype($filename)) {
  168. return IkiWiki::SuccessReason->new("file is a wiki page");
  169. }
  170. else {
  171. return IkiWiki::FailReason->new("file is not a wiki page");
  172. }
  173. }