| 1 | package Sidef::Types::Glob::File; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use 5.016; | ||
| 5 | |||
| 6 | use parent qw( | ||
| 7 | Sidef::Types::String::String | ||
| 8 | ); | ||
| 9 | |||
| 10 | require Encode; | ||
| 11 | require File::Spec; | ||
| 12 | |||
| 13 | use Sidef::Types::Number::Number; | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my (undef, $file) = @_; | ||
| 17 | if (@_ > 2) { | ||
| 18 | shift(@_); | ||
| 19 | $file = File::Spec->catfile(map { "$_" } @_); | ||
| 20 | } | ||
| 21 | elsif (ref($file) && ref($file) ne 'SCALAR') { | ||
| 22 | $file = "$file"; | ||
| 23 | } | ||
| 24 | bless \$file, __PACKAGE__; | ||
| 25 | } | ||
| 26 | |||
| 27 | *call = \&new; | ||
| 28 | |||
| 29 | sub get_value { ${$_[0]} } | ||
| 30 | sub to_file { $_[0] } | ||
| 31 | |||
| 32 | { | ||
| 33 | no strict 'refs'; | ||
| 34 | require Fcntl; | ||
| 35 | |||
| 36 | my %cache; | ||
| 37 | foreach my $name (@Fcntl::EXPORT, @Fcntl::EXPORT_OK) { | ||
| 38 | ($name =~ /^[A-Z]/ and defined(&{'Fcntl::' . $name})) or next; | ||
| 39 | *{__PACKAGE__ . '::' . $name} = sub { | ||
| 40 | $cache{$name} //= Sidef::Types::Number::Number::_set_int(&{'Fcntl::' . $name}); | ||
| 41 | }; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | sub size { | ||
| 46 | ref($_[0]) || shift(@_); | ||
| 47 | my ($self) = @_; | ||
| 48 | Sidef::Types::Number::Number::_set_int(-s "$self"); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub md5 { | ||
| 52 | ref($_[0]) || shift(@_); | ||
| 53 | state $x = require Digest::MD5; | ||
| 54 | open my $fh, '<:raw', "$_[0]" or return undef; | ||
| 55 | my $o = Digest::MD5->new; | ||
| 56 | $o->addfile($fh); | ||
| 57 | Sidef::Types::String::String->new(scalar $o->hexdigest); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub sha1 { | ||
| 61 | ref($_[0]) || shift(@_); | ||
| 62 | state $x = require Digest::SHA; | ||
| 63 | open my $fh, '<:raw', "$_[0]" or return undef; | ||
| 64 | my $o = Digest::SHA->new(1); | ||
| 65 | $o->addfile($fh); | ||
| 66 | Sidef::Types::String::String->new(scalar $o->hexdigest); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub sha256 { | ||
| 70 | ref($_[0]) || shift(@_); | ||
| 71 | state $x = require Digest::SHA; | ||
| 72 | open my $fh, '<:raw', "$_[0]" or return undef; | ||
| 73 | my $o = Digest::SHA->new(256); | ||
| 74 | $o->addfile($fh); | ||
| 75 | Sidef::Types::String::String->new(scalar $o->hexdigest); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub sha512 { | ||
| 79 | ref($_[0]) || shift(@_); | ||
| 80 | state $x = require Digest::SHA; | ||
| 81 | open my $fh, '<:raw', "$_[0]" or return undef; | ||
| 82 | my $o = Digest::SHA->new(512); | ||
| 83 | $o->addfile($fh); | ||
| 84 | Sidef::Types::String::String->new(scalar $o->hexdigest); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub compare { | ||
| 88 | ref($_[0]) || shift(@_); | ||
| 89 | my ($self, $file) = @_; | ||
| 90 | state $x = require File::Compare; | ||
| 91 | my $cmp = File::Compare::compare("$self", "$file"); | ||
| 92 | |||
| 93 | $cmp < 0 ? Sidef::Types::Number::Number::MONE | ||
| 94 | : $cmp > 0 ? Sidef::Types::Number::Number::ONE | ||
| 95 | : Sidef::Types::Number::Number::ZERO; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub is_newer_than { | ||
| 99 | ref($_[0]) || shift(@_); | ||
| 100 | my ($self, $other) = @_; | ||
| 101 | my $self_mtime = (CORE::stat("$self"))[9] // return undef; | ||
| 102 | my $other_mtime = (CORE::stat("$other"))[9] // return undef; | ||
| 103 | ($self_mtime > $other_mtime) ? Sidef::Types::Bool::Bool::TRUE : Sidef::Types::Bool::Bool::FALSE; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub is_older_than { | ||
| 107 | ref($_[0]) || shift(@_); | ||
| 108 | my ($self, $other) = @_; | ||
| 109 | my $self_mtime = (CORE::stat("$self"))[9] // return undef; | ||
| 110 | my $other_mtime = (CORE::stat("$other"))[9] // return undef; | ||
| 111 | ($self_mtime < $other_mtime) ? Sidef::Types::Bool::Bool::TRUE : Sidef::Types::Bool::Bool::FALSE; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub mktemp { | ||
| 115 | my ($self, %opts) = @_; | ||
| 116 | state $x = require File::Temp; | ||
| 117 | my ($fh, $file) = File::Temp::tempfile(%opts); | ||
| 118 | Sidef::Types::Glob::FileHandle->new($fh, __PACKAGE__->new($file)); | ||
| 119 | } | ||
| 120 | |||
| 121 | *make_tmp = \&mktemp; | ||
| 122 | *make_temp = \&mktemp; | ||
| 123 | |||
| 124 | sub exists { | ||
| 125 | ref($_[0]) || shift(@_); | ||
| 126 | my ($self) = @_; | ||
| 127 | (-e "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 128 | } | ||
| 129 | |||
| 130 | sub is_empty { | ||
| 131 | ref($_[0]) || shift(@_); | ||
| 132 | my ($self) = @_; | ||
| 133 | (-z "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub is_directory { | ||
| 137 | ref($_[0]) || shift(@_); | ||
| 138 | my ($self) = @_; | ||
| 139 | (-d "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 140 | } | ||
| 141 | |||
| 142 | *is_dir = \&is_directory; | ||
| 143 | |||
| 144 | sub is_link { | ||
| 145 | ref($_[0]) || shift(@_); | ||
| 146 | my ($self) = @_; | ||
| 147 | (-l "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 148 | } | ||
| 149 | |||
| 150 | sub readlink { | ||
| 151 | ref($_[0]) || shift(@_); | ||
| 152 | my ($self) = @_; | ||
| 153 | my $link = "$self"; | ||
| 154 | my $class = (-d $link) ? 'Sidef::Types::Glob::Dir' : __PACKAGE__; | ||
| 155 | $class->new(CORE::readlink($link)); | ||
| 156 | } | ||
| 157 | |||
| 158 | *read_link = \&readlink; | ||
| 159 | |||
| 160 | sub is_socket { | ||
| 161 | ref($_[0]) || shift(@_); | ||
| 162 | my ($self) = @_; | ||
| 163 | (-S "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 164 | } | ||
| 165 | |||
| 166 | sub is_block { | ||
| 167 | ref($_[0]) || shift(@_); | ||
| 168 | my ($self) = @_; | ||
| 169 | (-b "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 170 | } | ||
| 171 | |||
| 172 | sub is_char_device { | ||
| 173 | ref($_[0]) || shift(@_); | ||
| 174 | my ($self) = @_; | ||
| 175 | (-c "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 176 | } | ||
| 177 | |||
| 178 | sub is_readable { | ||
| 179 | ref($_[0]) || shift(@_); | ||
| 180 | my ($self) = @_; | ||
| 181 | (-r "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 182 | } | ||
| 183 | |||
| 184 | sub is_writeable { | ||
| 185 | ref($_[0]) || shift(@_); | ||
| 186 | my ($self) = @_; | ||
| 187 | (-w "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 188 | } | ||
| 189 | |||
| 190 | sub has_setuid_bit { | ||
| 191 | ref($_[0]) || shift(@_); | ||
| 192 | my ($self) = @_; | ||
| 193 | (-u "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 194 | } | ||
| 195 | |||
| 196 | sub has_setgid_bit { | ||
| 197 | ref($_[0]) || shift(@_); | ||
| 198 | my ($self) = @_; | ||
| 199 | (-g "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 200 | } | ||
| 201 | |||
| 202 | sub has_sticky_bit { | ||
| 203 | ref($_[0]) || shift(@_); | ||
| 204 | my ($self) = @_; | ||
| 205 | (-k "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 206 | } | ||
| 207 | |||
| 208 | sub modification_time_days_diff { | ||
| 209 | ref($_[0]) || shift(@_); | ||
| 210 | my ($self) = @_; | ||
| 211 | my $days = -M "$self"; | ||
| 212 | defined($days) ? Sidef::Types::Number::Number->new($days) : undef; | ||
| 213 | } | ||
| 214 | |||
| 215 | sub access_time_days_diff { | ||
| 216 | ref($_[0]) || shift(@_); | ||
| 217 | my ($self) = @_; | ||
| 218 | my $days = -A "$self"; | ||
| 219 | defined($days) ? Sidef::Types::Number::Number->new($days) : undef; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub change_time_days_diff { | ||
| 223 | ref($_[0]) || shift(@_); | ||
| 224 | my ($self) = @_; | ||
| 225 | my $days = -C "$self"; | ||
| 226 | defined($days) ? Sidef::Types::Number::Number->new($days) : undef; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub is_executable { | ||
| 230 | ref($_[0]) || shift(@_); | ||
| 231 | my ($self) = @_; | ||
| 232 | (-x "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 233 | } | ||
| 234 | |||
| 235 | sub is_owned { | ||
| 236 | ref($_[0]) || shift(@_); | ||
| 237 | my ($self) = @_; | ||
| 238 | (-o "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 239 | } | ||
| 240 | |||
| 241 | sub is_real_readable { | ||
| 242 | ref($_[0]) || shift(@_); | ||
| 243 | my ($self) = @_; | ||
| 244 | (-R "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 245 | } | ||
| 246 | |||
| 247 | sub is_real_writeable { | ||
| 248 | ref($_[0]) || shift(@_); | ||
| 249 | my ($self) = @_; | ||
| 250 | (-W "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 251 | } | ||
| 252 | |||
| 253 | sub is_real_executable { | ||
| 254 | ref($_[0]) || shift(@_); | ||
| 255 | my ($self) = @_; | ||
| 256 | (-X "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 257 | } | ||
| 258 | |||
| 259 | sub is_real_owned { | ||
| 260 | ref($_[0]) || shift(@_); | ||
| 261 | my ($self) = @_; | ||
| 262 | (-O "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 263 | } | ||
| 264 | |||
| 265 | sub is_binary { | ||
| 266 | ref($_[0]) || shift(@_); | ||
| 267 | my ($self) = @_; | ||
| 268 | (-B "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 269 | } | ||
| 270 | |||
| 271 | sub is_text { | ||
| 272 | ref($_[0]) || shift(@_); | ||
| 273 | my ($self) = @_; | ||
| 274 | (-T "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 275 | } | ||
| 276 | |||
| 277 | sub is_file { | ||
| 278 | ref($_[0]) || shift(@_); | ||
| 279 | my ($self) = @_; | ||
| 280 | (-f "$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 281 | } | ||
| 282 | |||
| 283 | sub name { | ||
| 284 | ref($_[0]) || shift(@_); | ||
| 285 | my ($self) = @_; | ||
| 286 | Sidef::Types::String::String->new("$self"); | ||
| 287 | } | ||
| 288 | |||
| 289 | sub basename { | ||
| 290 | ref($_[0]) || shift(@_); | ||
| 291 | my ($self) = @_; | ||
| 292 | |||
| 293 | state $x = require File::Basename; | ||
| 294 | Sidef::Types::String::String->new(File::Basename::basename("$self")); | ||
| 295 | } | ||
| 296 | |||
| 297 | *base = \&basename; | ||
| 298 | *base_name = \&basename; | ||
| 299 | |||
| 300 | sub dirname { | ||
| 301 | ref($_[0]) || shift(@_); | ||
| 302 | my ($self) = @_; | ||
| 303 | |||
| 304 | state $x = require File::Basename; | ||
| 305 | Sidef::Types::Glob::Dir->new(File::Basename::dirname("$self")); | ||
| 306 | } | ||
| 307 | |||
| 308 | *dir = \&dirname; | ||
| 309 | *dir_name = \&dirname; | ||
| 310 | |||
| 311 | sub extension { | ||
| 312 | ref($_[0]) || shift(@_); | ||
| 313 | my ($self) = @_; | ||
| 314 | my $base = "$self"; | ||
| 315 | $base =~ s{.*/}{}; | ||
| 316 | ($base =~ /\.([^.]+)\z/) ? Sidef::Types::String::String->new($1) : Sidef::Types::String::String->new(''); | ||
| 317 | } | ||
| 318 | |||
| 319 | sub basename_without_ext { | ||
| 320 | ref($_[0]) || shift(@_); | ||
| 321 | my ($self) = @_; | ||
| 322 | my $str = "" . $self->basename; | ||
| 323 | $str =~ s/\.[^.]+\z//; | ||
| 324 | Sidef::Types::String::String->new($str); | ||
| 325 | } | ||
| 326 | |||
| 327 | sub is_absolute { | ||
| 328 | ref($_[0]) || shift(@_); | ||
| 329 | my ($self) = @_; | ||
| 330 | |||
| 331 | File::Spec->file_name_is_absolute("$self") | ||
| 332 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 333 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 334 | } | ||
| 335 | |||
| 336 | *is_abs = \&is_absolute; | ||
| 337 | |||
| 338 | sub abs_name { | ||
| 339 | my $class = ref($_[0]) || shift(@_); | ||
| 340 | my ($self, $base) = @_; | ||
| 341 | $class->new(Encode::decode_utf8(File::Spec->rel2abs(Encode::encode_utf8("$self"), defined($base) ? Encode::encode_utf8("$base") : ()))); | ||
| 342 | } | ||
| 343 | |||
| 344 | *abs = \&abs_name; | ||
| 345 | *absname = \&abs_name; | ||
| 346 | *rel2abs = \&abs_name; | ||
| 347 | |||
| 348 | sub rel_name { | ||
| 349 | my $class = ref($_[0]) || shift(@_); | ||
| 350 | my ($self, $base) = @_; | ||
| 351 | $class->new(Encode::decode_utf8(File::Spec->abs2rel(Encode::encode_utf8("$self"), defined($base) ? Encode::encode_utf8("$base") : ()))); | ||
| 352 | } | ||
| 353 | |||
| 354 | *rel = \&rel_name; | ||
| 355 | *relname = \&rel_name; | ||
| 356 | *abs2rel = \&rel_name; | ||
| 357 | |||
| 358 | sub abs_path { | ||
| 359 | my $class = ref($_[0]) || shift(@_); | ||
| 360 | my ($self) = @_; | ||
| 361 | |||
| 362 | state $x = require Cwd; | ||
| 363 | $class->new(Encode::decode_utf8(Cwd::abs_path("$self"))); | ||
| 364 | } | ||
| 365 | |||
| 366 | *realpath = \&abs_path; | ||
| 367 | |||
| 368 | sub rename { | ||
| 369 | ref($_[0]) || shift(@_); | ||
| 370 | my ($self, $file) = @_; | ||
| 371 | |||
| 372 | CORE::rename("$self", "$file") | ||
| 373 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 374 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 375 | } | ||
| 376 | |||
| 377 | sub move { | ||
| 378 | ref($_[0]) || shift(@_); | ||
| 379 | my ($self, $file) = @_; | ||
| 380 | |||
| 381 | state $x = require File::Copy; | ||
| 382 | File::Copy::move("$self", "$file") | ||
| 383 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 384 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 385 | } | ||
| 386 | |||
| 387 | *mv = \&move; | ||
| 388 | |||
| 389 | sub copy { | ||
| 390 | ref($_[0]) || shift(@_); | ||
| 391 | my ($self, $file) = @_; | ||
| 392 | |||
| 393 | state $x = require File::Copy; | ||
| 394 | File::Copy::copy("$self", "$file") | ||
| 395 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 396 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 397 | } | ||
| 398 | |||
| 399 | *cp = \© | ||
| 400 | |||
| 401 | sub backup { | ||
| 402 | ref($_[0]) || shift(@_); | ||
| 403 | my ($self, $suffix) = @_; | ||
| 404 | $suffix //= '.bak'; | ||
| 405 | my $dest = "$self" . "$suffix"; | ||
| 406 | $self->copy($dest); | ||
| 407 | } | ||
| 408 | |||
| 409 | sub edit { | ||
| 410 | ref($_[0]) || shift(@_); | ||
| 411 | my ($self, $code) = @_; | ||
| 412 | |||
| 413 | my @lines; | ||
| 414 | CORE::open(my $fh, '+<:utf8', "$self") || return (Sidef::Types::Bool::Bool::FALSE); | ||
| 415 | while (defined(my $line = <$fh>)) { | ||
| 416 | push @lines, $code->run(Sidef::Types::String::String->new($line)); | ||
| 417 | } | ||
| 418 | |||
| 419 | CORE::truncate($fh, 0) || return undef; | ||
| 420 | CORE::seek($fh, 0, 0) || return undef; | ||
| 421 | |||
| 422 | do { | ||
| 423 | local $, = q{}; | ||
| 424 | local $\ = q{}; | ||
| 425 | (print $fh @lines) || return undef; | ||
| 426 | CORE::close $fh; | ||
| 427 | } | ||
| 428 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 429 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 430 | } | ||
| 431 | |||
| 432 | sub read { | ||
| 433 | ref($_[0]) || shift(@_); | ||
| 434 | my ($self, $mode) = @_; | ||
| 435 | |||
| 436 | $mode = defined($mode) ? "$mode" : 'utf8'; | ||
| 437 | $mode =~ s/^://; | ||
| 438 | CORE::open(my $fh, "<:$mode", "$self") || return undef; | ||
| 439 | |||
| 440 | local $/; | ||
| 441 | Sidef::Types::String::String->new(scalar <$fh>); | ||
| 442 | } | ||
| 443 | |||
| 444 | sub write { | ||
| 445 | ref($_[0]) || shift(@_); | ||
| 446 | my ($self, $string, $mode) = @_; | ||
| 447 | |||
| 448 | $mode = defined($mode) ? "$mode" : 'utf8'; | ||
| 449 | CORE::open(my $fh, ">:$mode", "$self") || return undef; | ||
| 450 | |||
| 451 | (print $fh "$string") || return undef; | ||
| 452 | |||
| 453 | (CORE::close $fh) | ||
| 454 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 455 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 456 | } | ||
| 457 | |||
| 458 | sub append { | ||
| 459 | ref($_[0]) || shift(@_); | ||
| 460 | my ($self, $string, $mode) = @_; | ||
| 461 | |||
| 462 | $mode = defined($mode) ? "$mode" : 'utf8'; | ||
| 463 | CORE::open(my $fh, ">>:$mode", "$self") || return undef; | ||
| 464 | |||
| 465 | (print $fh "$string") || return undef; | ||
| 466 | |||
| 467 | (CORE::close $fh) | ||
| 468 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 469 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 470 | } | ||
| 471 | |||
| 472 | sub lines { | ||
| 473 | ref($_[0]) || shift(@_); | ||
| 474 | my ($self) = @_; | ||
| 475 | CORE::open(my $fh, '<:utf8', "$self") or return undef; | ||
| 476 | my @lines = map { chomp; Sidef::Types::String::String->new($_) } <$fh>; | ||
| 477 | CORE::close($fh); | ||
| 478 | Sidef::Types::Array::Array->new(\@lines); | ||
| 479 | } | ||
| 480 | |||
| 481 | sub each_line { | ||
| 482 | ref($_[0]) || shift(@_); | ||
| 483 | my ($self, $block) = @_; | ||
| 484 | CORE::open(my $fh, '<:utf8', "$self") or return Sidef::Types::Bool::Bool::FALSE; | ||
| 485 | while (defined(my $line = <$fh>)) { | ||
| 486 | chomp($line); | ||
| 487 | $block->run(Sidef::Types::String::String->new($line)); | ||
| 488 | } | ||
| 489 | CORE::close($fh); | ||
| 490 | Sidef::Types::Bool::Bool::TRUE; | ||
| 491 | } | ||
| 492 | |||
| 493 | sub open { | ||
| 494 | ref($_[0]) || shift(@_); | ||
| 495 | my ($self, $mode, $fh_ref, $err_ref) = @_; | ||
| 496 | |||
| 497 | $mode //= '<:utf8'; | ||
| 498 | $mode = "$mode" if (ref $mode); | ||
| 499 | |||
| 500 | my $success = CORE::open(my $fh, $mode, ref($self) eq __PACKAGE__ ? $$self : "$self"); | ||
| 501 | my $error = $!; | ||
| 502 | my $fh_obj = Sidef::Types::Glob::FileHandle->new($fh, $self); | ||
| 503 | |||
| 504 | if (defined $fh_ref) { | ||
| 505 | ${$fh_ref} = $fh_obj; | ||
| 506 | |||
| 507 | return $success | ||
| 508 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 509 | : do { | ||
| 510 | defined($err_ref) && do { ${$err_ref} = Sidef::Types::String::String->new($error) }; | ||
| 511 | (Sidef::Types::Bool::Bool::FALSE); | ||
| 512 | }; | ||
| 513 | } | ||
| 514 | |||
| 515 | $success ? $fh_obj : undef; | ||
| 516 | } | ||
| 517 | |||
| 518 | sub symlink { | ||
| 519 | ref($_[0]) || shift(@_); | ||
| 520 | my ($self, $newfile) = @_; | ||
| 521 | CORE::symlink($self, $newfile) | ||
| 522 | ? Sidef::Types::Bool::Bool::TRUE | ||
| 523 | : Sidef::Types::Bool::Bool::FALSE; | ||
| 524 | } | ||
| 525 | |||
| 526 | sub link { | ||
| 527 | ref($_[0]) || shift(@_); | ||
| 528 | my ($self, $newfile) = @_; | ||
| 529 | CORE::link($self, $newfile) | ||
| 530 | ? Sidef::Types::Bool::Bool::TRUE | ||
| 531 | : Sidef::Types::Bool::Bool::FALSE; | ||
| 532 | } | ||
| 533 | |||
| 534 | sub touch { | ||
| 535 | ref($_[0]) || shift(@_); | ||
| 536 | my ($self, @args) = @_; | ||
| 537 | Sidef::Types::Glob::File::open($self, '>>', @args); | ||
| 538 | } | ||
| 539 | |||
| 540 | *make = \&touch; | ||
| 541 | *mkfile = \&touch; | ||
| 542 | *create = \&touch; | ||
| 543 | |||
| 544 | sub open_r { | ||
| 545 | ref($_[0]) || shift(@_); | ||
| 546 | my ($self, @args) = @_; | ||
| 547 | Sidef::Types::Glob::File::open($self, '<:utf8', @args); | ||
| 548 | } | ||
| 549 | |||
| 550 | *open_read = \&open_r; | ||
| 551 | |||
| 552 | sub open_w { | ||
| 553 | ref($_[0]) || shift(@_); | ||
| 554 | my ($self, @args) = @_; | ||
| 555 | Sidef::Types::Glob::File::open($self, '>:utf8', @args); | ||
| 556 | } | ||
| 557 | |||
| 558 | *open_write = \&open_w; | ||
| 559 | |||
| 560 | sub open_a { | ||
| 561 | ref($_[0]) || shift(@_); | ||
| 562 | my ($self, @args) = @_; | ||
| 563 | Sidef::Types::Glob::File::open($self, '>>:utf8', @args); | ||
| 564 | } | ||
| 565 | |||
| 566 | *open_append = \&open_a; | ||
| 567 | |||
| 568 | sub open_rw { | ||
| 569 | ref($_[0]) || shift(@_); | ||
| 570 | my ($self, @args) = @_; | ||
| 571 | Sidef::Types::Glob::File::open($self, '+<:utf8', @args); | ||
| 572 | } | ||
| 573 | |||
| 574 | *open_read_write = \&open_rw; | ||
| 575 | |||
| 576 | sub open_arw { | ||
| 577 | ref($_[0]) || shift(@_); | ||
| 578 | my ($self, @args) = @_; | ||
| 579 | Sidef::Types::Glob::File::open($self, '+>>:utf8', @args); | ||
| 580 | } | ||
| 581 | |||
| 582 | *open_append_read_write = \&open_arw; | ||
| 583 | |||
| 584 | sub opendir { | ||
| 585 | ref($_[0]) || shift(@_); | ||
| 586 | my ($self, @args) = @_; | ||
| 587 | Sidef::Types::Glob::Dir->new("$self")->open(@args); | ||
| 588 | } | ||
| 589 | |||
| 590 | sub sysopen { | ||
| 591 | ref($_[0]) || shift(@_); | ||
| 592 | my ($self, $var_ref, $mode, $perm) = @_; | ||
| 593 | |||
| 594 | my $success = sysopen(my $fh, "$self", "$mode", $perm // 0666); | ||
| 595 | |||
| 596 | if ($success) { | ||
| 597 | $$var_ref = Sidef::Types::Glob::FileHandle->new($fh, $self); | ||
| 598 | } | ||
| 599 | |||
| 600 | $success | ||
| 601 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 602 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 603 | } | ||
| 604 | |||
| 605 | sub stat { | ||
| 606 | ref($_[0]) || shift(@_); | ||
| 607 | my ($self) = @_; | ||
| 608 | Sidef::Types::Glob::Stat->stat("$self", $self); | ||
| 609 | } | ||
| 610 | |||
| 611 | sub lstat { | ||
| 612 | ref($_[0]) || shift(@_); | ||
| 613 | my ($self) = @_; | ||
| 614 | Sidef::Types::Glob::Stat->lstat("$self", $self); | ||
| 615 | } | ||
| 616 | |||
| 617 | sub chown { | ||
| 618 | ref($_[0]) || shift(@_); | ||
| 619 | my ($self, $uid, $gid) = @_; | ||
| 620 | CORE::chown($uid, $gid, "$self") | ||
| 621 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 622 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 623 | } | ||
| 624 | |||
| 625 | sub chmod { | ||
| 626 | ref($_[0]) || shift(@_); | ||
| 627 | my ($self, $permission) = @_; | ||
| 628 | CORE::chmod($permission, "$self") | ||
| 629 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 630 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 631 | } | ||
| 632 | |||
| 633 | sub utime { | ||
| 634 | ref($_[0]) || shift(@_); | ||
| 635 | my ($self, $atime, $mtime) = @_; | ||
| 636 | CORE::utime($atime, $mtime, "$self") | ||
| 637 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 638 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 639 | } | ||
| 640 | |||
| 641 | sub truncate { | ||
| 642 | ref($_[0]) || shift(@_); | ||
| 643 | my ($self, $length) = @_; | ||
| 644 | CORE::truncate("$self", $length // 0) | ||
| 645 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 646 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 647 | } | ||
| 648 | |||
| 649 | sub unlink { | ||
| 650 | my ($self, @args) = @_; | ||
| 651 | |||
| 652 | if (ref($self)) { | ||
| 653 | CORE::unlink("$self") | ||
| 654 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 655 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 656 | } | ||
| 657 | else { | ||
| 658 | Sidef::Types::Number::Number::_set_int(CORE::unlink(@args)); | ||
| 659 | } | ||
| 660 | } | ||
| 661 | |||
| 662 | sub delete { | ||
| 663 | my ($self, @args) = @_; | ||
| 664 | |||
| 665 | if (ref($self)) { | ||
| 666 | my $file = "$self"; | ||
| 667 | CORE::unlink($file) || return Sidef::Types::Bool::Bool::FALSE; | ||
| 668 | 1 while CORE::unlink($file); | ||
| 669 | return Sidef::Types::Bool::Bool::TRUE; | ||
| 670 | } | ||
| 671 | else { | ||
| 672 | my $count = 0; | ||
| 673 | |||
| 674 | foreach my $arg (@args) { | ||
| 675 | my $file = "$arg"; | ||
| 676 | CORE::unlink($file) || next; | ||
| 677 | 1 while CORE::unlink($file); | ||
| 678 | ++$count; | ||
| 679 | } | ||
| 680 | |||
| 681 | Sidef::Types::Number::Number::_set_int($count); | ||
| 682 | } | ||
| 683 | } | ||
| 684 | |||
| 685 | *remove = \&delete; | ||
| 686 | |||
| 687 | sub split { | ||
| 688 | ref($_[0]) || shift(@_); | ||
| 689 | my ($self) = @_; | ||
| 690 | Sidef::Types::Array::Array->new([map { Sidef::Types::String::String->new($_) } File::Spec->splitdir("$self")]); | ||
| 691 | } | ||
| 692 | |||
| 693 | sub splitpath { | ||
| 694 | ref($_[0]) || shift(@_); | ||
| 695 | my ($self) = @_; | ||
| 696 | Sidef::Types::Array::Array->new([map { Sidef::Types::String::String->new($_) } File::Spec->splitpath("$self")]); | ||
| 697 | } | ||
| 698 | |||
| 699 | sub dump { | ||
| 700 | my ($self) = @_; | ||
| 701 | Sidef::Types::String::String->new('File(' . ${Sidef::Types::String::String->new("$self")->dump} . ')'); | ||
| 702 | } | ||
| 703 | |||
| 704 | sub to_str { | ||
| 705 | my ($self) = @_; | ||
| 706 | Sidef::Types::String::String->new($$self); | ||
| 707 | } | ||
| 708 | |||
| 709 | *to_s = \&to_str; | ||
| 710 | |||
| 711 | 1 |