torture_gen_layout.pl 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env perl
  2. #
  3. # Copyright (C) by Daniel Molkentin <danimo@owncloud.com>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. # for more details.
  14. #
  15. use strict;
  16. use Data::Random::WordList;
  17. ############################################################################
  18. # Which extensions to randomly assign
  19. my @exts = ('txt', 'pdf', 'html', 'docx', 'xlsx', 'pptx', 'odt', 'ods', 'odp');
  20. # Maximum depth of the target structure
  21. my $depth = 4;
  22. # Maximum amount of subfolders within a folder
  23. my $max_subfolder = 10;
  24. # Maximum amount of files within a folder
  25. my $max_files_per_folder = 100;
  26. # Maximum file size
  27. my $max_file_size = 1024**2;
  28. ############################################################################
  29. sub gen_entries($)
  30. {
  31. my ($count) = @_;
  32. my $wl = new Data::Random::WordList( wordlist => '/usr/share/dict/words' );
  33. my @rand_words = $wl->get_words($count);
  34. foreach(@rand_words) {
  35. $_ =~ s/\'//g;
  36. }
  37. $wl->close();
  38. return @rand_words;
  39. }
  40. sub create_subdir($)
  41. {
  42. my ($depth) = @_;
  43. $depth--;
  44. my %dir_tree = ( );
  45. my @dirs = gen_entries(int(rand($max_subfolders)));
  46. my @files = gen_entries(int(rand($max_files_per_folder)));
  47. foreach my $file(@files) {
  48. $dir_tree{$file} = int(rand($max_file_size));
  49. }
  50. if ($depth > 0) {
  51. foreach my $dir(@dirs) {
  52. $dir_tree{$dir} = create_subdir($depth);
  53. }
  54. }
  55. return \%dir_tree;
  56. }
  57. sub create_dir_listing(@)
  58. {
  59. my ($tree, $prefix) = @_;
  60. foreach my $key(keys %$tree) {
  61. my $entry = $tree->{$key};
  62. #print "$entry:".scalar $entry.":".ref $entry."\n";
  63. if (ref $entry eq "HASH") {
  64. create_dir_listing($tree->{$key}, "$prefix/$key");
  65. } else {
  66. my $ext = @exts[rand @exts];
  67. print "$prefix/$key.$ext:$entry\n";
  68. }
  69. }
  70. }
  71. srand();
  72. my $dir = create_subdir($depth);
  73. create_dir_listing($dir, '.');