directories.pm 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #***************************************************************************
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. #
  10. # This software is licensed as described in the file COPYING, which
  11. # you should have received as part of this distribution. The terms
  12. # are also available at https://curl.se/docs/copyright.html.
  13. #
  14. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. # copies of the Software, and permit persons to whom the Software is
  16. # furnished to do so, under the terms of the COPYING file.
  17. #
  18. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. # KIND, either express or implied.
  20. #
  21. # SPDX-License-Identifier: curl
  22. #
  23. ###########################################################################
  24. package directories;
  25. use strict;
  26. use warnings;
  27. BEGIN {
  28. use base qw(Exporter);
  29. our @EXPORT = qw(
  30. ftp_contentlist
  31. wildcard_filesize
  32. wildcard_getfile
  33. );
  34. }
  35. my %file_chmod1 = (
  36. 'name' => 'chmod1',
  37. 'content' => "This file should have permissions 444\n",
  38. 'perm' => 'r--r--r--',
  39. 'time' => 'Jan 11 10:00',
  40. 'dostime' => '01-11-10 10:00AM',
  41. );
  42. my %file_chmod2 = (
  43. 'name' => 'chmod2',
  44. 'content' => "This file should have permissions 666\n",
  45. 'perm' => 'rw-rw-rw-',
  46. 'time' => 'Feb 1 8:00',
  47. 'dostime' => '02-01-10 08:00AM',
  48. );
  49. my %file_chmod3 = (
  50. 'name' => 'chmod3',
  51. 'content' => "This file should have permissions 777\n",
  52. 'perm' => 'rwxrwxrwx',
  53. 'time' => 'Feb 1 8:00',
  54. 'dostime' => '02-01-10 08:00AM',
  55. );
  56. my %file_chmod4 = (
  57. 'type' => 'd',
  58. 'name' => 'chmod4',
  59. 'content' => "This file should have permissions 001\n",
  60. 'perm' => '--S--S--t',
  61. 'time' => 'May 4 4:31',
  62. 'dostime' => '05-04-10 04:31AM'
  63. );
  64. my %file_chmod5 = (
  65. 'type' => 'd',
  66. 'name' => 'chmod5',
  67. 'content' => "This file should have permissions 110\n",
  68. 'perm' => '--s--s--T',
  69. 'time' => 'May 4 4:31',
  70. 'dostime' => '05-04-10 04:31AM'
  71. );
  72. my %link_link = (
  73. 'type' => 'l',
  74. 'name' => 'link -> file.txt',
  75. 'size' => '8',
  76. 'perm' => 'rwxrwxrwx',
  77. 'time' => 'Jan 6 4:42'
  78. );
  79. my %link_link_absolute = (
  80. 'type' => 'l',
  81. 'name' => 'link_absolute -> /data/ftp/file.txt',
  82. 'size' => '15',
  83. 'perm' => 'rwxrwxrwx',
  84. 'time' => 'Jan 6 4:45'
  85. );
  86. my %dir_dot = (
  87. 'type' => "d",
  88. 'name' => ".",
  89. 'hlink' => "4",
  90. 'time' => "Apr 27 5:12",
  91. 'size' => "20480",
  92. 'dostime' => "04-27-10 05:12AM",
  93. 'perm' => "rwxrwxrwx"
  94. );
  95. my %dir_ddot = (
  96. 'type' => "d",
  97. 'name' => "..",
  98. 'hlink' => "4",
  99. 'size' => "20480",
  100. 'time' => "Apr 23 3:12",
  101. 'dostime' => "04-23-10 03:12AM",
  102. 'perm' => "rwxrwxrwx"
  103. );
  104. my %dir_weirddir_txt = (
  105. 'type' => "d",
  106. 'name' => "weirddir.txt",
  107. 'hlink' => "2",
  108. 'size' => "4096",
  109. 'time' => "Apr 23 3:12",
  110. 'dostime' => "04-23-10 03:12AM",
  111. 'perm' => "rwxr-xrwx"
  112. );
  113. my %dir_UNIX = (
  114. 'type' => "d",
  115. 'name' => "UNIX",
  116. 'hlink' => "11",
  117. 'size' => "4096",
  118. 'time' => "Nov 01 2008",
  119. 'dostime' => "11-01-08 11:11AM",
  120. 'perm' => "rwx--x--x"
  121. );
  122. my %dir_DOS = (
  123. 'type' => "d",
  124. 'name' => "DOS",
  125. 'hlink' => "11",
  126. 'size' => "4096",
  127. 'time' => "Nov 01 2008",
  128. 'dostime' => "11-01-08 11:11AM",
  129. 'perm' => "rwx--x--x"
  130. );
  131. my %dir_dot_NeXT = (
  132. 'type' => "d",
  133. 'name' => ".NeXT",
  134. 'hlink' => "4",
  135. 'size' => "4096",
  136. 'time' => "Jan 23 2:05",
  137. 'dostime' => "01-23-05 02:05AM",
  138. 'perm' => "rwxrwxrwx"
  139. );
  140. my %file_empty_file_dat = (
  141. 'name' => "empty_file.dat",
  142. 'content' => "",
  143. 'perm' => "rw-r--r--",
  144. 'time' => "Apr 27 11:01",
  145. 'dostime' => "04-27-10 11:01AM"
  146. );
  147. my %file_file_txt = (
  148. 'name' => "file.txt",
  149. 'content' => "This is content of file \"file.txt\"\n",
  150. 'time' => "Apr 27 11:01",
  151. 'dostime' => "04-27-10 11:01AM",
  152. 'perm' => "rw-r--r--"
  153. );
  154. my %file_someothertext_txt = (
  155. 'name' => "someothertext.txt",
  156. 'content' => "Some junk ;-) This file does not really exist.\n",
  157. 'time' => "Apr 27 11:01",
  158. 'dostime' => "04-27-10 11:01AM",
  159. 'perm' => "rw-r--r--"
  160. );
  161. my %lists = (
  162. '/fully_simulated/' => {
  163. 'files' => [ \%dir_dot, \%dir_ddot, \%dir_DOS, \%dir_UNIX ],
  164. 'eol' => "\r\n",
  165. 'type' => "unix"
  166. },
  167. '/fully_simulated/UNIX/' => {
  168. 'files' => [ \%dir_dot, \%dir_ddot,
  169. \%file_chmod1, \%file_chmod2, \%file_chmod3, \%file_chmod4, \%file_chmod5,
  170. \%file_empty_file_dat, \%file_file_txt,
  171. \%link_link, \%link_link_absolute, \%dir_dot_NeXT,
  172. \%file_someothertext_txt, \%dir_weirddir_txt ],
  173. 'eol' => "\r\n",
  174. 'type' => 'unix'
  175. },
  176. '/fully_simulated/DOS/' => {
  177. 'files' => [ \%dir_dot, \%dir_ddot,
  178. \%file_chmod1, \%file_chmod2, \%file_chmod3, \%file_chmod4, \%file_chmod5,
  179. \%file_empty_file_dat, \%file_file_txt,
  180. \%dir_dot_NeXT, \%file_someothertext_txt, \%dir_weirddir_txt ],
  181. 'eol' => "\r\n",
  182. 'type' => 'dos'
  183. }
  184. );
  185. sub ftp_createcontent {
  186. my ($list) = $_[0];
  187. my $type = $$list{'type'};
  188. my $eol = $$list{'eol'};
  189. my $list_ref = $$list{'files'};
  190. my @diroutput;
  191. my @contentlist;
  192. if($type eq "unix") {
  193. for(@$list_ref) {
  194. my %file = %$_;
  195. my $line = "";
  196. my $ftype = $file{'type'} ? $file{'type'} : "-";
  197. my $fperm = $file{'perm'} ? $file{'perm'} : "rwxr-xr-x";
  198. my $fuser = $file{'user'} ? sprintf("%15s", $file{'user'}) : "ftp-default";
  199. my $fgroup = $file{'group'} ? sprintf("%15s", $file{'group'}) : "ftp-default";
  200. my $fsize = "";
  201. if(exists($file{'type'}) && $file{'type'} eq "d") {
  202. $fsize = $file{'size'} ? sprintf("%7s", $file{'size'}) : sprintf("%7d", 4096);
  203. }
  204. else {
  205. $fsize = sprintf("%7d", exists($file{'content'}) ? length $file{'content'} : 0);
  206. }
  207. my $fhlink = $file{'hlink'} ? sprintf("%4d", $file{'hlink'}) : " 1";
  208. my $ftime = $file{'time'} ? sprintf("%10s", $file{'time'}) : "Jan 9 1933";
  209. push(@contentlist, "$ftype$fperm $fhlink $fuser $fgroup $fsize $ftime $file{'name'}$eol");
  210. }
  211. return @contentlist;
  212. }
  213. elsif($type =~ /^dos$/) {
  214. for(@$list_ref) {
  215. my %file = %$_;
  216. my $line = "";
  217. my $time = $file{'dostime'} ? $file{'dostime'} : "06-25-97 09:12AM";
  218. my $size_or_dir;
  219. if(exists($file{'type'}) && $file{'type'} =~ /^d$/) {
  220. $size_or_dir = " <DIR> ";
  221. }
  222. else {
  223. $size_or_dir = sprintf("%20d", length $file{'content'});
  224. }
  225. push(@contentlist, "$time $size_or_dir $file{'name'}$eol");
  226. }
  227. return @contentlist;
  228. }
  229. }
  230. sub wildcard_filesize {
  231. my ($list_type, $file) = @_;
  232. my $list = $lists{$list_type};
  233. if($list) {
  234. my $files = $list->{'files'};
  235. for(@$files) {
  236. my %f = %$_;
  237. if ($f{'name'} eq $file) {
  238. if($f{'content'}) {
  239. return length $f{'content'};
  240. }
  241. elsif ($f{'type'} ne "d"){
  242. return 0;
  243. }
  244. else {
  245. return -1;
  246. }
  247. }
  248. }
  249. }
  250. return -1;
  251. }
  252. sub wildcard_getfile {
  253. my ($list_type, $file) = @_;
  254. my $list = $lists{$list_type};
  255. if($list) {
  256. my $files = $list->{'files'};
  257. for(@$files) {
  258. my %f = %$_;
  259. if ($f{'name'} eq $file) {
  260. if($f{'content'}) {
  261. return (length $f{'content'}, $f{'content'});
  262. }
  263. elsif (!exists($f{'type'}) or $f{'type'} ne "d"){
  264. return (0, "");
  265. }
  266. else {
  267. return (-1, 0);
  268. }
  269. }
  270. }
  271. }
  272. return (-1, 0);
  273. }
  274. sub ftp_contentlist {
  275. my $listname = $_[0];
  276. my $list = $lists{$listname};
  277. return ftp_createcontent($list);
  278. }