quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

directories.pm (8937B)


      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 
     25 package directories;
     26 
     27 use strict;
     28 use warnings;
     29 
     30 BEGIN {
     31     use base qw(Exporter);
     32 
     33     our @EXPORT = qw(
     34         ftp_contentlist
     35         wildcard_filesize
     36         wildcard_getfile
     37     );
     38 }
     39 
     40 
     41 my %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 
     49 my %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 
     57 my %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 
     65 my %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 
     74 my %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 
     83 my %link_link = (
     84     'type'      => 'l',
     85     'name'      => 'link -> file.txt',
     86     'size'      => '8',
     87     'perm'      => 'rwxrwxrwx',
     88     'time'      => 'Jan  6  4:42'
     89 );
     90 
     91 my %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 
     99 my %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 
    109 my %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 
    119 my %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 
    129 my %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 
    139 my %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 
    149 my %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 
    159 my %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 
    167 my %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 
    175 my %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 
    183 my %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 
    208 sub 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 @contentlist;
    216     if($type eq "unix") {
    217         for(@$list_ref) {
    218             my %file = %$_;
    219             my $line = "";
    220             my $ftype  = $file{'type'}  ? $file{'type'}  : "-";
    221             my $fperm  = $file{'perm'}  ? $file{'perm'}  : "rwxr-xr-x";
    222             my $fuser  = $file{'user'}  ? sprintf("%15s", $file{'user'})   : "ftp-default";
    223             my $fgroup = $file{'group'} ? sprintf("%15s", $file{'group'})  : "ftp-default";
    224             my $fsize = "";
    225             if(exists($file{'type'}) && $file{'type'} eq "d") {
    226                 $fsize = $file{'size'} ? sprintf("%7s", $file{'size'}) : sprintf("%7d", 4096);
    227             }
    228             else {
    229                 $fsize = sprintf("%7d", exists($file{'content'}) ? length $file{'content'} : 0);
    230             }
    231             my $fhlink = $file{'hlink'} ? sprintf("%4d",  $file{'hlink'})  : "   1";
    232             my $ftime  = $file{'time'}  ? sprintf("%10s", $file{'time'})   : "Jan 9  1933";
    233             push(@contentlist, "$ftype$fperm $fhlink $fuser $fgroup $fsize $ftime $file{'name'}$eol");
    234         }
    235 
    236         return @contentlist;
    237     }
    238     elsif($type =~ /^dos$/) {
    239         for(@$list_ref) {
    240             my %file = %$_;
    241             my $line = "";
    242             my $time = $file{'dostime'} ? $file{'dostime'} : "06-25-97  09:12AM";
    243             my $size_or_dir;
    244             if(exists($file{'type'}) && $file{'type'} =~ /^d$/) {
    245                 $size_or_dir = "      <DIR>         ";
    246             }
    247             else {
    248                 $size_or_dir = sprintf("%20d", length $file{'content'});
    249             }
    250             push(@contentlist, "$time $size_or_dir $file{'name'}$eol");
    251         }
    252         return @contentlist;
    253     }
    254 }
    255 
    256 sub wildcard_filesize {
    257     my ($list_type, $file) = @_;
    258     my $list = $lists{$list_type};
    259     if($list) {
    260         my $files = $list->{'files'};
    261         for(@$files) {
    262             my %f = %$_;
    263             if($f{'name'} eq $file) {
    264                 if($f{'content'}) {
    265                     return length $f{'content'};
    266                 }
    267                 elsif($f{'type'} ne "d"){
    268                     return 0;
    269                 }
    270                 else {
    271                     return -1;
    272                 }
    273             }
    274         }
    275     }
    276     return -1;
    277 }
    278 
    279 sub wildcard_getfile {
    280     my ($list_type, $file) = @_;
    281     my $list = $lists{$list_type};
    282     if($list) {
    283         my $files = $list->{'files'};
    284         for(@$files) {
    285             my %f = %$_;
    286             if($f{'name'} eq $file) {
    287                 if($f{'content'}) {
    288                     return (length $f{'content'}, $f{'content'});
    289                 }
    290                 elsif(!exists($f{'type'}) or $f{'type'} ne "d"){
    291                     return (0, "");
    292                 }
    293                 else {
    294                     return (-1, 0);
    295                 }
    296             }
    297         }
    298     }
    299     return (-1, 0);
    300 }
    301 
    302 sub ftp_contentlist {
    303     my $listname = $_[0];
    304     my $list = $lists{$listname};
    305     return ftp_createcontent($list);
    306 }