xref: /curl/tests/directories.pm (revision efbaa612)
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
25package directories;
26
27use strict;
28use warnings;
29
30BEGIN {
31    use base qw(Exporter);
32
33    our @EXPORT = qw(
34        ftp_contentlist
35        wildcard_filesize
36        wildcard_getfile
37    );
38}
39
40
41my %file_chmod1 = (
42  'name'      => 'chmod1',
43  'content'   => "This file should have permissions 444\n",
44  'perm'      => 'r--r--r--',
45  'time'      => 'Jan 11 10:00',
46  'dostime'   => '01-11-10  10:00AM',
47);
48
49my %file_chmod2 = (
50  'name'      => 'chmod2',
51  'content'   => "This file should have permissions 666\n",
52  'perm'      => 'rw-rw-rw-',
53  'time'      => 'Feb  1  8:00',
54  'dostime'   => '02-01-10  08:00AM',
55);
56
57my %file_chmod3 = (
58  'name'      => 'chmod3',
59  'content'   => "This file should have permissions 777\n",
60  'perm'      => 'rwxrwxrwx',
61  'time'      => 'Feb  1  8:00',
62  'dostime'   => '02-01-10  08:00AM',
63);
64
65my %file_chmod4 = (
66  'type'      => 'd',
67  'name'      => 'chmod4',
68  'content'   => "This file should have permissions 001\n",
69  'perm'      => '--S--S--t',
70  'time'      => 'May  4  4:31',
71  'dostime'   => '05-04-10  04:31AM'
72);
73
74my %file_chmod5 = (
75  'type'      => 'd',
76  'name'      => 'chmod5',
77  'content'   => "This file should have permissions 110\n",
78  'perm'      => '--s--s--T',
79  'time'      => 'May  4  4:31',
80  'dostime'   => '05-04-10  04:31AM'
81);
82
83my %link_link = (
84  'type'      => 'l',
85  'name'      => 'link -> file.txt',
86  'size'      => '8',
87  'perm'      => 'rwxrwxrwx',
88  'time'      => 'Jan  6  4:42'
89);
90
91my %link_link_absolute = (
92  'type'      => 'l',
93  'name'      => 'link_absolute -> /data/ftp/file.txt',
94  'size'      => '15',
95  'perm'      => 'rwxrwxrwx',
96  'time'      => 'Jan  6  4:45'
97);
98
99my %dir_dot = (
100  'type'      => "d",
101  'name'      => ".",
102  'hlink'     => "4",
103  'time'      => "Apr 27  5:12",
104  'size'      => "20480",
105  'dostime'   => "04-27-10  05:12AM",
106  'perm'      => "rwxrwxrwx"
107);
108
109my %dir_ddot = (
110  'type'      => "d",
111  'name'      => "..",
112  'hlink'     => "4",
113  'size'      => "20480",
114  'time'      => "Apr 23  3:12",
115  'dostime'   => "04-23-10  03:12AM",
116  'perm'      => "rwxrwxrwx"
117);
118
119my %dir_weirddir_txt = (
120  'type'      => "d",
121  'name'      => "weirddir.txt",
122  'hlink'     => "2",
123  'size'      => "4096",
124  'time'      => "Apr 23  3:12",
125  'dostime'   => "04-23-10  03:12AM",
126  'perm'      => "rwxr-xrwx"
127);
128
129my %dir_UNIX = (
130  'type'      => "d",
131  'name'      => "UNIX",
132  'hlink'     => "11",
133  'size'      => "4096",
134  'time'      => "Nov 01  2008",
135  'dostime'   => "11-01-08  11:11AM",
136  'perm'      => "rwx--x--x"
137);
138
139my %dir_DOS = (
140  'type'      => "d",
141  'name'      => "DOS",
142  'hlink'     => "11",
143  'size'      => "4096",
144  'time'      => "Nov 01  2008",
145  'dostime'   => "11-01-08  11:11AM",
146  'perm'      => "rwx--x--x"
147);
148
149my %dir_dot_NeXT = (
150  'type'      => "d",
151  'name'      => ".NeXT",
152  'hlink'     => "4",
153  'size'      => "4096",
154  'time'      => "Jan 23  2:05",
155  'dostime'   => "01-23-05  02:05AM",
156  'perm'      => "rwxrwxrwx"
157);
158
159my %file_empty_file_dat = (
160  'name'      => "empty_file.dat",
161  'content'   => "",
162  'perm'      => "rw-r--r--",
163  'time'      => "Apr 27 11:01",
164  'dostime'   => "04-27-10  11:01AM"
165);
166
167my %file_file_txt = (
168  'name'      => "file.txt",
169  'content'   => "This is content of file \"file.txt\"\n",
170  'time'      => "Apr 27 11:01",
171  'dostime'   => "04-27-10  11:01AM",
172  'perm'      => "rw-r--r--"
173);
174
175my %file_someothertext_txt = (
176  'name'      => "someothertext.txt",
177  'content'   => "Some junk ;-) This file does not really exist.\n",
178  'time'      => "Apr 27 11:01",
179  'dostime'   => "04-27-10  11:01AM",
180  'perm'      => "rw-r--r--"
181);
182
183my %lists = (
184  '/fully_simulated/' => {
185    'files'   => [ \%dir_dot, \%dir_ddot, \%dir_DOS, \%dir_UNIX ],
186    'eol'     => "\r\n",
187    'type'    => "unix"
188  },
189  '/fully_simulated/UNIX/' => {
190    'files'   => [ \%dir_dot, \%dir_ddot,
191                   \%file_chmod1, \%file_chmod2, \%file_chmod3, \%file_chmod4, \%file_chmod5,
192                   \%file_empty_file_dat, \%file_file_txt,
193                   \%link_link, \%link_link_absolute, \%dir_dot_NeXT,
194                   \%file_someothertext_txt, \%dir_weirddir_txt ],
195    'eol'     => "\r\n",
196    'type'    => 'unix'
197  },
198  '/fully_simulated/DOS/' => {
199    'files'   => [ \%dir_dot, \%dir_ddot,
200                   \%file_chmod1, \%file_chmod2, \%file_chmod3, \%file_chmod4, \%file_chmod5,
201                   \%file_empty_file_dat, \%file_file_txt,
202                   \%dir_dot_NeXT, \%file_someothertext_txt, \%dir_weirddir_txt ],
203    'eol'     => "\r\n",
204    'type'    => 'dos'
205  }
206);
207
208sub ftp_createcontent {
209  my ($list) = $_[0];
210
211  my $type = $$list{'type'};
212  my $eol  = $$list{'eol'};
213  my $list_ref = $$list{'files'};
214
215  my @diroutput;
216  my @contentlist;
217  if($type eq "unix") {
218    for(@$list_ref) {
219      my %file = %$_;
220      my $line = "";
221      my $ftype  = $file{'type'}  ? $file{'type'}  : "-";
222      my $fperm  = $file{'perm'}  ? $file{'perm'}  : "rwxr-xr-x";
223      my $fuser  = $file{'user'}  ? sprintf("%15s", $file{'user'})   : "ftp-default";
224      my $fgroup = $file{'group'} ? sprintf("%15s", $file{'group'})  : "ftp-default";
225      my $fsize = "";
226      if(exists($file{'type'}) && $file{'type'} eq "d") {
227        $fsize = $file{'size'} ? sprintf("%7s", $file{'size'}) : sprintf("%7d", 4096);
228      }
229      else {
230        $fsize = sprintf("%7d", exists($file{'content'}) ? length $file{'content'} : 0);
231      }
232      my $fhlink = $file{'hlink'} ? sprintf("%4d",  $file{'hlink'})  : "   1";
233      my $ftime  = $file{'time'}  ? sprintf("%10s", $file{'time'})   : "Jan 9  1933";
234      push(@contentlist, "$ftype$fperm $fhlink $fuser $fgroup $fsize $ftime $file{'name'}$eol");
235    }
236
237    return @contentlist;
238  }
239  elsif($type =~ /^dos$/) {
240    for(@$list_ref) {
241      my %file = %$_;
242      my $line = "";
243      my $time = $file{'dostime'} ? $file{'dostime'} : "06-25-97  09:12AM";
244      my $size_or_dir;
245      if(exists($file{'type'}) && $file{'type'} =~ /^d$/) {
246        $size_or_dir = "      <DIR>         ";
247      }
248      else {
249        $size_or_dir = sprintf("%20d", length $file{'content'});
250      }
251      push(@contentlist, "$time $size_or_dir $file{'name'}$eol");
252    }
253    return @contentlist;
254  }
255}
256
257sub wildcard_filesize {
258  my ($list_type, $file) = @_;
259  my $list = $lists{$list_type};
260  if($list) {
261    my $files = $list->{'files'};
262    for(@$files) {
263      my %f = %$_;
264      if ($f{'name'} eq $file) {
265        if($f{'content'}) {
266          return length $f{'content'};
267        }
268        elsif ($f{'type'} ne "d"){
269          return 0;
270        }
271        else {
272          return -1;
273        }
274      }
275    }
276  }
277  return -1;
278}
279
280sub wildcard_getfile {
281  my ($list_type, $file) = @_;
282  my $list = $lists{$list_type};
283  if($list) {
284    my $files = $list->{'files'};
285    for(@$files) {
286      my %f = %$_;
287      if ($f{'name'} eq $file) {
288        if($f{'content'}) {
289          return (length $f{'content'}, $f{'content'});
290        }
291        elsif (!exists($f{'type'}) or $f{'type'} ne "d"){
292          return (0, "");
293        }
294        else {
295          return (-1, 0);
296        }
297      }
298    }
299  }
300  return (-1, 0);
301}
302
303sub ftp_contentlist {
304  my $listname = $_[0];
305  my $list = $lists{$listname};
306  return ftp_createcontent($list);
307}
308