| 1 | # | ||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||
| 3 | # | ||
| 4 | # vim: set ts=3 sw=3 tw=0: | ||
| 5 | # vim: set expandtab: | ||
| 6 | |||
| 7 | package Rex::Endpoint::HTTP::Interface::Fs::Base; | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my $that = shift; | ||
| 14 | my $proto = ref($that) || $that; | ||
| 15 | my $self = { @_ }; | ||
| 16 | |||
| 17 | bless($self, $proto); | ||
| 18 | |||
| 19 | return $self; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub ls { | ||
| 23 | my ($self, $path) = @_; | ||
| 24 | |||
| 25 | my @ret; | ||
| 26 | opendir(my $dh, $path) or die($!); | ||
| 27 | while(my $entry = readdir($dh)) { | ||
| 28 | next if($entry eq "." || $entry eq ".."); | ||
| 29 | push(@ret, $entry); | ||
| 30 | } | ||
| 31 | closedir($dh); | ||
| 32 | |||
| 33 | return @ret; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub is_dir { | ||
| 37 | my ($self, $path) = @_; | ||
| 38 | |||
| 39 | if(-d $path) { | ||
| 40 | return 1; | ||
| 41 | } | ||
| 42 | else { | ||
| 43 | return 0; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | sub is_file { | ||
| 48 | my ($self, $file) = @_; | ||
| 49 | |||
| 50 | if(-f $file) { | ||
| 51 | return 1; | ||
| 52 | } | ||
| 53 | else { | ||
| 54 | return 0; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | sub unlink { | ||
| 59 | my ($self, $path) = @_; | ||
| 60 | |||
| 61 | CORE::unlink($path) or die($!); | ||
| 62 | |||
| 63 | return 1; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub mkdir { | ||
| 67 | my ($self, $path) = @_; | ||
| 68 | |||
| 69 | CORE::mkdir($path) or die($!); | ||
| 70 | |||
| 71 | return 1; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub stat { | ||
| 75 | my ($self, $path) = @_; | ||
| 76 | |||
| 77 | if(my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, | ||
| 78 | $atime, $mtime, $ctime, $blksize, $blocks) = CORE::stat($path)) { | ||
| 79 | |||
| 80 | my %ret; | ||
| 81 | |||
| 82 | $ret{'mode'} = sprintf("%04o", $mode & 07777); | ||
| 83 | $ret{'size'} = $size; | ||
| 84 | $ret{'uid'} = $uid; | ||
| 85 | $ret{'gid'} = $gid; | ||
| 86 | $ret{'atime'} = $atime; | ||
| 87 | $ret{'mtime'} = $mtime; | ||
| 88 | |||
| 89 | return \%ret; | ||
| 90 | } | ||
| 91 | |||
| 92 | die("Not found"); | ||
| 93 | } | ||
| 94 | |||
| 95 | sub is_readable { | ||
| 96 | my ($self, $path) = @_; | ||
| 97 | |||
| 98 | if(-r $path) { | ||
| 99 | return 1; | ||
| 100 | } | ||
| 101 | |||
| 102 | return 0; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub is_writable { | ||
| 106 | my ($self, $path) = @_; | ||
| 107 | |||
| 108 | if(-w $path) { | ||
| 109 | return 1; | ||
| 110 | } | ||
| 111 | |||
| 112 | return 0; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub readlink { | ||
| 116 | my ($self, $path) = @_; | ||
| 117 | |||
| 118 | my $link = CORE::readlink($path) or die($!); | ||
| 119 | |||
| 120 | return $link; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub rename { | ||
| 124 | my ($self, $old, $new) = @_; | ||
| 125 | |||
| 126 | CORE::rename($old, $new) or die($!); | ||
| 127 | |||
| 128 | return 1; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub glob { | ||
| 132 | my ($self, $glob) = @_; | ||
| 133 | |||
| 134 | my @ret = CORE::glob($glob); | ||
| 135 | |||
| 136 | return @ret; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub upload { | ||
| 140 | my ($self, $path, $upload) = @_; | ||
| 141 | |||
| 142 | open(my $fh, ">", $path) or die($!); | ||
| 143 | print $fh $upload->slurp; | ||
| 144 | close($fh); | ||
| 145 | |||
| 146 | return 1; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub download { | ||
| 150 | my ($self, $path) = @_; | ||
| 151 | |||
| 152 | if(! -f $path) { | ||
| 153 | die("File not found."); | ||
| 154 | } | ||
| 155 | |||
| 156 | my $content = eval { local(@ARGV, $/) = ($path); <>; }; | ||
| 157 | |||
| 158 | return $content; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub ln { | ||
| 162 | my ($self, $from, $to) = @_; | ||
| 163 | |||
| 164 | if(-f $to) { | ||
| 165 | CORE::unlink($to) or die($!); | ||
| 166 | } | ||
| 167 | |||
| 168 | CORE::symlink($from, $to) or die($!); | ||
| 169 | |||
| 170 | return 1; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub rmdir { | ||
| 174 | my ($self, $path) = @_; | ||
| 175 | |||
| 176 | system("rm -rf " . $path); | ||
| 177 | |||
| 178 | if($? == 0) { | ||
| 179 | return 1; | ||
| 180 | } | ||
| 181 | |||
| 182 | die("Error deleting directory."); | ||
| 183 | } | ||
| 184 | |||
| 185 | sub chown { | ||
| 186 | my ($self, $user, $file, $options) = @_; | ||
| 187 | |||
| 188 | my $recursive = ""; | ||
| 189 | if(exists $options->{"recursive"} && $options->{"recursive"} == 1) { | ||
| 190 | $recursive = " -R "; | ||
| 191 | } | ||
| 192 | |||
| 193 | system("chown $recursive $user $file"); | ||
| 194 | |||
| 195 | if($? == 0) { | ||
| 196 | return 1; | ||
| 197 | } | ||
| 198 | |||
| 199 | die("Error changing ownership of file"); | ||
| 200 | } | ||
| 201 | |||
| 202 | sub chgrp { | ||
| 203 | my ($self, $group, $file, $options) = @_; | ||
| 204 | |||
| 205 | my $recursive = ""; | ||
| 206 | if(exists $options->{"recursive"} && $options->{"recursive"} == 1) { | ||
| 207 | $recursive = " -R "; | ||
| 208 | } | ||
| 209 | |||
| 210 | system("chgrp $recursive $group $file"); | ||
| 211 | |||
| 212 | if($? == 0) { | ||
| 213 | return 1; | ||
| 214 | } | ||
| 215 | |||
| 216 | die("Error chaning group ownership of file"); | ||
| 217 | } | ||
| 218 | |||
| 219 | sub chmod { | ||
| 220 | my ($self, $mode, $file, $options) = @_; | ||
| 221 | |||
| 222 | my $recursive = ""; | ||
| 223 | if(exists $options->{"recursive"} && $options->{"recursive"} == 1) { | ||
| 224 | $recursive = " -R "; | ||
| 225 | } | ||
| 226 | |||
| 227 | system("chmod $recursive $mode $file"); | ||
| 228 | |||
| 229 | if($? == 0) { | ||
| 230 | return 1; | ||
| 231 | } | ||
| 232 | |||
| 233 | die("Error changing file permission"); | ||
| 234 | } | ||
| 235 | |||
| 236 | sub cp { | ||
| 237 | my ($self, $source, $dest) = @_; | ||
| 238 | |||
| 239 | system("cp -R $source $dest"); | ||
| 240 | |||
| 241 | if($? == 0) { | ||
| 242 | return 1; | ||
| 243 | } | ||
| 244 | |||
| 245 | die("Error copying file"); | ||
| 246 | } | ||
| 247 | |||
| 248 | 1; |