sort-into-month-folder 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/perl -w
  2. # Sort dumped consensuses, statuses, descriptors etc into per-month folders.
  3. # Copyright (c) 2006, 2007, 2008 Peter Palfrader
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. use strict;
  23. use File::Find;
  24. use File::Basename;
  25. use File::stat;
  26. use Time::Local;
  27. my $cutofftime;
  28. sub wanted() {
  29. return unless -f;
  30. my $mtime = stat($_)->mtime;
  31. return if $mtime >= $cutofftime;
  32. my (undef,undef,undef,undef,$mon,$year,undef,undef,undef) = gmtime $mtime;
  33. my $bn = basename $_;
  34. my $dn = dirname $_;
  35. my @path = split /\//, $dn;
  36. $path[0] .= sprintf 's-%4d-%02d', 1900+$year, $mon+1;
  37. $dn = join '/', @path;
  38. if (! -d $dn) {
  39. my $p = '.';
  40. for my $component (@path) {
  41. $p .= '/'.$component;
  42. if (! -d $p) {
  43. mkdir $p or die ("Cannot mkdir $p: $!\n");
  44. };
  45. };
  46. };
  47. print "$_ -> $dn/$bn\n";
  48. rename $_, $dn.'/'.$bn or die ("Cannot rename $_ to $dn/$bn: $!\n");
  49. };
  50. my (undef,undef,undef,undef,$mon,$year,undef,undef,undef) = gmtime(time - 5*24*3600);
  51. $cutofftime = timegm(0,0,0,1,$mon,$year);
  52. find( {
  53. wanted => \&wanted,
  54. no_chdir => 1
  55. },
  56. 'server-descriptor');
  57. find( {
  58. wanted => \&wanted,
  59. no_chdir => 1
  60. },
  61. 'extra-info');