File: Fs.pm

package info (click to toggle)
rex 1.14.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,012 kB
  • sloc: perl: 35,587; xml: 264; sh: 51; makefile: 13
file content (1210 lines) | stat: -rw-r--r-- 25,901 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#

=head1 NAME

Rex::Commands::Fs - File system commands

=head1 DESCRIPTION

With this module you can do file system tasks like creating directories, deleting or moving files, and more.

=head1 SYNOPSIS

 my @files = list_files "/etc";
 
 unlink("/tmp/file");
 
 rmdir("/tmp");
 mkdir("/tmp");
 
 my %stat = stat("/etc/passwd");
 
 my $link = readlink("/path/to/a/link");
 symlink("/source", "/dest");
 
 rename("oldname", "newname");
 
 chdir("/tmp");
 
 is_file("/etc/passwd");
 is_dir("/etc");
 is_writeable("/tmp");
 is_writable("/tmp");
 
 chmod 755, "/tmp";
 chown "user", "/tmp";
 chgrp "group", "/tmp";


=head1 EXPORTED FUNCTIONS

=cut

package Rex::Commands::Fs;

use v5.12.5;
use warnings;

our $VERSION = '1.14.1'; # VERSION

require Rex::Exporter;
use Data::Dumper;
use Fcntl;
use Rex::Helper::File::Spec;
use Rex::Helper::SSH2;
use Rex::Helper::Path;
use Rex::Commands;
use Rex::Interface::Fs;
use Rex::Interface::Exec;
use Rex::Interface::File;
use File::Basename;
use Rex::Commands::MD5;

use vars qw(@EXPORT);
use base qw(Rex::Exporter);

@EXPORT = qw(list_files ls
  unlink rm rmdir mkdir stat readlink symlink ln rename mv chdir cd cp
  chown chgrp chmod
  is_file is_dir is_readable is_writeable is_writable is_symlink
  df du
  mount umount
  glob);

use vars qw(%file_handles);

=head2 Changing content

These commands are supposed to change the contents of the file system.

=head3 symlink($from, $to)

This function will create a symbolic link from C<$from> to C<$to>.

 task "symlink", "server01", sub {
   symlink("/var/www/versions/1.0.0", "/var/www/html");
 };

=cut

sub symlink {
  my ( $from, $to ) = @_;
  $from = resolv_path($from);
  $to   = resolv_path($to);

  Rex::get_current_connection()->{reporter}
    ->report_resource_start( type => "symlink", name => $to );

  my $fs = Rex::Interface::Fs->create;
  if ( $fs->is_symlink($to) && $fs->readlink($to) eq $from ) {
    Rex::get_current_connection()->{reporter}->report( changed => 0, );
  }
  else {
    $fs->ln( $from, $to ) or die("Can't link $from -> $to");
    Rex::get_current_connection()->{reporter}
      ->report( changed => 1, message => "Symlink created: $from -> $to." );
  }

  Rex::get_current_connection()->{reporter}
    ->report_resource_end( type => "symlink", name => $to );

  return 1;
}

=head3 ln($from, $to)

C<ln> is an alias for C<symlink>

=cut

sub ln {
  &symlink(@_);
}

=head3 unlink($file)

This function will remove the given C<$file>.

 task "unlink", "server01", sub {
   unlink("/tmp/testfile");
 };

=cut

sub unlink {
  my @files = @_;

  my $f;
  if ( ref $files[0] eq "ARRAY" ) {
    $f = $files[0];
  }
  else {
    $f = \@files;
  }

  if ( scalar @{$f} == 1 ) {
    my $file = resolv_path $f->[0];
    my $fs   = Rex::Interface::Fs->create;

    Rex::get_current_connection()->{reporter}
      ->report_resource_start( type => "unlink", name => $file );

    if ( $fs->is_file($file) || $fs->is_symlink($file) ) {
      $fs->unlink($file);

      my $tmp_path = Rex::Config->get_tmp_dir;
      if ( $file !~ m/^\Q$tmp_path\E[\/\\][a-z]+\.tmp$/ ) { # skip tmp rex files
        Rex::get_current_connection()->{reporter}
          ->report( changed => 1, message => "File $file removed." );
      }
    }
    else {
      Rex::get_current_connection()->{reporter}->report( changed => 0, );
    }

    Rex::get_current_connection()->{reporter}
      ->report_resource_end( type => "unlink", name => $file );
  }
  else {
    &unlink($_) for @{$f};
  }

}

=head3 rm($file)

This is an alias for C<unlink>.

=cut

sub rm {
  &unlink(@_);
}

=head3 rmdir($dir)

This function will remove the given directory.

 task "rmdir", "server01", sub {
   rmdir("/tmp");
 };


With Rex-0.45 and newer, please use the L<file|Rex::Commands::File#file> resource instead.

 task "prepare", sub {
   file "/tmp",
     ensure => "absent";
 };

=cut

sub rmdir {
  my @dirs = @_;

  my $d;
  if ( ref $dirs[0] eq "ARRAY" ) {
    $d = $dirs[0];
  }
  else {
    $d = \@dirs;
  }

  if ( scalar @{$d} == 1 ) {
    my $dir = resolv_path $d->[0];
    my $fs  = Rex::Interface::Fs->create;

    Rex::get_current_connection()->{reporter}
      ->report_resource_start( type => "rmdir", name => $dir );

    if ( !$fs->is_dir($dir) && $dir !~ m/[\*\[]/ ) {
      Rex::get_current_connection()->{reporter}->report( changed => 0, );
    }
    else {
      $fs->rmdir($dir);
      if ( $fs->is_dir($dir) ) {
        die "Can't remove $dir.";
      }

      Rex::get_current_connection()->{reporter}
        ->report( changed => 1, message => "Directory $dir removed." );
    }

    Rex::get_current_connection()->{reporter}
      ->report_resource_end( type => "rmdir", name => $dir );
  }
  else {
    &rmdir($_) for @{$d};
  }
}

=head3 mkdir($newdir)

This function will create a new directory.

The following options are supported:

=over 4

=item * owner

=item * group

=item * mode

=item * on_change

=back

With Rex-0.45 and newer, please use the L<file|Rex::Commands::File#file> resource instead.

 task "prepare", sub {
   file "/tmp",
     ensure => "directory",
     owner  => "root",
     group  => "root",
     mode   => 1777;
 };

Direct usage:
 
 task "mkdir", "server01", sub {
   mkdir "/tmp";
 
   mkdir "/tmp",
     owner => "root",
     group => "root",
     mode => 1777;
 };

=cut

sub mkdir {
  Rex::Logger::debug("Creating directory $_[0]");
  my $dir = shift;
  $dir = resolv_path($dir);

  my $options = {@_};

  $options->{on_change} //= sub { };

  Rex::get_current_connection()->{reporter}
    ->report_resource_start( type => "mkdir", name => $dir );

  my $fs = Rex::Interface::Fs->create;

  my $not_created = 0;
  my %old_stat;
  my $changed = 0;

  if ( $fs->is_dir($dir) ) {
    $not_created = 1;
    %old_stat    = &stat($dir);
  }

  my $mode          = $options->{"mode"}          || 755;
  my $owner         = $options->{"owner"}         || "";
  my $group         = $options->{"group"}         || "";
  my $not_recursive = $options->{"not_recursive"} || 0;

  if ($not_recursive) {
    if ( !$fs->mkdir($dir) ) {
      Rex::Logger::debug("Can't create directory $dir");
      die("Can't create directory $dir");
    }

    &chown( $owner, $dir ) if $owner;
    &chgrp( $group, $dir ) if $group;
    &chmod( $mode, $dir )  if $mode;
  }
  else {
    if ( !Rex::Helper::File::Spec->file_name_is_absolute($dir) ) {
      $dir = Rex::Helper::File::Spec->rel2abs($dir);
    }

    my @directories = __splitdir($dir);
    my $path_so_far = shift @directories;

    for my $part (@directories) {
      $path_so_far = Rex::Helper::File::Spec->join( $path_so_far, $part );

      if ( !is_dir($path_so_far) && !is_file($path_so_far) ) {
        if ( !$fs->mkdir($path_so_far) ) {
          Rex::Logger::debug("Can't create directory $dir");
          die("Can't create directory $dir");
        }

        &chown( $owner, $path_so_far ) if $owner;
        &chgrp( $group, $path_so_far ) if $group;
        &chmod( $mode, $path_so_far )  if $mode;
      }
    }
  }

  my %new_stat = &stat($dir);

  if ( !$not_created ) {
    Rex::get_current_connection()->{reporter}
      ->report( changed => 1, message => "Directory created." );
    $changed = 1;
  }

  if ( %old_stat && $old_stat{uid} != $new_stat{uid} ) {
    Rex::get_current_connection()->{reporter}
      ->report( changed => 1, message => "Owner updated." );
    $changed = 1;
  }

  if ( %old_stat && $old_stat{gid} != $new_stat{gid} ) {
    Rex::get_current_connection()->{reporter}
      ->report( changed => 1, message => "Group updated." );
    $changed = 1;
  }

  if ( %old_stat && $old_stat{mode} ne $new_stat{mode} ) {
    Rex::get_current_connection()->{reporter}
      ->report( changed => 1, message => "Mode updated." );
    $changed = 1;
  }

  if ( $changed == 0 ) {
    Rex::get_current_connection()->{reporter}->report( changed => 0, );
  }
  else {
    $options->{on_change}->($dir);
  }

  Rex::get_current_connection()->{reporter}
    ->report_resource_end( type => "mkdir", name => $dir );

  return 1;
}

sub __splitdir {
  return Rex::Helper::File::Spec->splitdir(shift);
}

=head3 chown($owner, $path)

Change the owner of a file or a directory.

 chown "www-data", "/var/www/html";
 
 chown "www-data", "/var/www/html",
                recursive => 1;


This command will not be reported.

If you want to use reports, please use the L<file|Rex::Commands::File#file> resource instead.

=cut

sub chown {
  my ( $user, $file, @opts ) = @_;

  $file = resolv_path($file);
  my $fs = Rex::Interface::Fs->create;
  $fs->chown( $user, $file, @opts ) or die("Can't chown $file");
}

=head3 chgrp($group, $path)

Change the group of a file or a directory.

 chgrp "nogroup", "/var/www/html";
 
 chgrp "nogroup", "/var/www/html",
              recursive => 1;


This command will not be reported.

If you want to use reports, please use the L<file|Rex::Commands::File#file> resource instead.

=cut

sub chgrp {
  my ( $group, $file, @opts ) = @_;
  $file = resolv_path($file);

  my $fs = Rex::Interface::Fs->create;
  $fs->chgrp( $group, $file, @opts ) or die("Can't chgrp $file");
}

=head3 chmod($mode, $path)

Change the permissions of a file or a directory.

 chmod 755, "/var/www/html";
 
 chmod 755, "/var/www/html",
          recursive => 1;


This command will not be reported.

If you want to use reports, please use the L<file|Rex::Commands::File#file> resource instead.

=cut

sub chmod {
  my ( $mode, $file, @opts ) = @_;
  $file = resolv_path($file);

  my $fs = Rex::Interface::Fs->create;
  $fs->chmod( $mode, $file, @opts ) or die("Can't chmod $file");
}

=head3 rename($old, $new)

This function will rename C<$old> to C<$new>. Will return 1 on success and 0 on failure.

 task "rename", "server01", sub {
   rename("/tmp/old", "/tmp/new");
 };

=cut

sub rename {
  my ( $old, $new ) = @_;
  $old = resolv_path($old);
  $new = resolv_path($new);

  Rex::get_current_connection()->{reporter}
    ->report_resource_start( type => "rename", name => "$old -> $new" );

  my $fs = Rex::Interface::Fs->create;

  my $old_present = 0;
  if ( $fs->is_file($old) || $fs->is_dir($old) || $fs->is_symlink($old) ) {
    $old_present = 1;
  }

  Rex::Logger::debug("Renaming $old to $new");

  if ( !$fs->rename( $old, $new ) ) {
    Rex::Logger::info("Rename failed ($old -> $new)");
    die("Rename failed $old -> $new");
  }

  my $new_present = 0;
  if ( $fs->is_file($new) || $fs->is_dir($new) || $fs->is_symlink($new) ) {
    $new_present = 1;
  }

  my $old_absent = 0;
  if ( !( $fs->is_file($old) || $fs->is_dir($old) || $fs->is_symlink($old) ) ) {
    $old_absent = 1;
  }

  if ( $old_present == 1 && $new_present == 1 && $old_absent == 1 ) {
    Rex::get_current_connection()->{reporter}->report( changed => 1 );
  }
  else {
    Rex::get_current_connection()->{reporter}->report( changed => 0 );
  }

  Rex::get_current_connection()->{reporter}
    ->report_resource_end( type => "rename", name => "$old -> $new" );
}

=head3 mv($old, $new)

C<mv> is an alias for C<rename>.

=cut

sub mv {
  return &rename(@_);
}

=head3 cp($source, $destination)

C<cp> will copy C<$source> to C<$destination> recursively.

 task "cp", "server01", sub {
    cp("/var/www", "/var/www.old");
 };

=cut

sub cp {
  my ( $source, $dest ) = @_;

  $source = resolv_path($source);
  $dest   = resolv_path($dest);

  Rex::get_current_connection()->{reporter}
    ->report_resource_start( type => "cp", name => "$source -> $dest" );

  my $fs = Rex::Interface::Fs->create;

  my $new_present = 0;
  if ( $fs->is_file($source) && $fs->is_dir($dest) ) {
    $dest = "$dest/" . basename $source;
  }

  if ( $fs->is_file($dest) || $fs->is_dir($dest) || $fs->is_symlink($dest) ) {
    $new_present = 1;
  }

  if ( !$fs->cp( $source, $dest ) ) {
    die("Copy failed from $source to $dest");
  }

  if ( $new_present == 0 ) {
    Rex::get_current_connection()->{reporter}->report( changed => 1, );
  }
  else {
    Rex::get_current_connection()->{reporter}->report( changed => 0, );
  }

  Rex::get_current_connection()->{reporter}
    ->report_resource_end( type => "cp", name => "$source -> $dest" );
}

=head2 Not changing content

These commands should not change the contents of the file system.

=head3 list_files("/path");

This function lists all entries (files, directories, ...) in a given directory and returns them as an array.

 task "ls-etc", "server01", sub {
   my @tmp_files = grep { /\.tmp$/ } list_files("/etc");
 };

This command will not be reported.

=cut

sub list_files {
  my $path = shift;
  $path = resolv_path($path);

  my $fs  = Rex::Interface::Fs->create;
  my @ret = $fs->ls($path);

  return @ret;
}

=head3 ls($path)

Just an alias for C<list_files>.

=cut

sub ls {
  return list_files(@_);
}

=head3 stat($file)

This function will return a hash with the following information about a file or directory:

=over 4

=item mode

=item size

=item uid

=item gid

=item atime

=item mtime

=back

 task "stat", "server01", sub {
   my %file_stat = stat("/etc/passwd");
 };


This command will not be reported.

=cut

sub stat {
  my ($file) = @_;
  $file = resolv_path($file);
  my %ret;

  Rex::Logger::debug("Getting fs stat from $file");

  my $fs = Rex::Interface::Fs->create;

  # may return undef, so capture into a list first.
  my @stat = $fs->stat($file);
  die("Can't stat $file") if ( !defined $stat[0] && scalar @stat == 1 );

  if ( scalar @stat % 2 ) {
    Rex::Logger::debug( 'stat output: ' . join ', ', @stat );
    die('stat returned odd number of elements');
  }

  %ret = @stat;

  return %ret;
}

=head3 is_file($path)

This function tests if C<$path> is a file. Returns 1 if true, 0 if false.

 task "isfile", "server01", sub {
   if( is_file("/etc/passwd") ) {
     say "it is a file.";
   }
   else {
     say "hm, this is not a file.";
   }
 };

This command will not be reported.

=cut

sub is_file {
  my ($file) = @_;
  $file = resolv_path($file);

  my $fs = Rex::Interface::Fs->create;
  return $fs->is_file($file);
}

=head3 is_dir($path)

This function tests if C<$path> is a directory. Returns 1 if true, 0 if false.

 task "isdir", "server01", sub {
   if( is_dir("/etc") ) {
     say "it is a directory.";
   }
   else {
     say "hm, this is not a directory.";
   }
 };

This command will not be reported.

=cut

sub is_dir {
  my ($path) = @_;
  $path = resolv_path($path);

  my $fs = Rex::Interface::Fs->create;
  return $fs->is_dir($path);

}

=head3 is_symlink($path)

This function tests if C<$path> is a symbolic link. Returns 1 if true, 0 if false.

 task "issym", "server01", sub {
   if( is_symlink("/etc/foo.txt") ) {
     say "it is a symlink.";
   }
   else {
     say "hm, this is not a symlink.";
   }
 };

This command will not be reported.

=cut

sub is_symlink {
  my ($path) = @_;
  $path = resolv_path($path);

  my $fs = Rex::Interface::Fs->create;
  return $fs->is_symlink($path);
}

=head3 is_readable($path)

This function tests if C<$path> is readable. It returns 1 if true, 0 if false.

 task "readable", "server01", sub {
   if( is_readable("/etc/passwd") ) {
     say "passwd is readable";
   }
   else {
     say "not readable.";
   }
 };

This command will not be reported.

=cut

sub is_readable {
  my ($file) = @_;
  $file = resolv_path($file);
  Rex::Logger::debug("Checking if $file is readable");

  my $fs = Rex::Interface::Fs->create;
  return $fs->is_readable($file);
}

=head3 is_writable($path)

This function tests if C<$path> is writable. It returns 1 if true, 0 if false.

 task "writable", "server01", sub {
   if( is_writable("/etc/passwd") ) {
     say "passwd is writable";
   }
   else {
     say "not writable.";
   }
 };

This command will not be reported.

=cut

sub is_writable {
  my ($file) = @_;
  $file = resolv_path($file);
  Rex::Logger::debug("Checking if $file is writable");

  my $fs = Rex::Interface::Fs->create;
  return $fs->is_writable($file);
}

=head3 is_writeable($file)

This is only an alias for C<is_writable>.

=cut

sub is_writeable {
  is_writable(@_);
}

=head3 readlink($link)

If C<$link> is a symbolic link, returns the path it resolves to, and C<die()>s otherwise.

 task "islink", "server01", sub {
   my $link;
   eval {
     $link = readlink("/tmp/testlink");
   };
 
   say "this is a link" if($link);
 };

This command will not be reported.

=cut

sub readlink {
  my ($file) = @_;
  $file = resolv_path($file);
  Rex::Logger::debug("Reading link of $file");

  my $fs   = Rex::Interface::Fs->create;
  my $link = $fs->readlink($file);

  unless ($link) {
    Rex::Logger::debug("readlink: $file is not a link.");
    die("readlink: $file is not a link.");
  }

  return $link;
}

=head3 chdir($newdir)

This function will change the working directory to C<$newdir>. This function currently works only locally.

 task "chdir", "server01", sub {
   chdir("/tmp");
 };

This command will not be reported.

=cut

sub chdir {
  Rex::Logger::debug("chdir behaviour will be changed in the future.");
  CORE::chdir( $_[0] );
}

=head3 cd($newdir)

This is an alias of C<chdir>.

=cut

sub cd {
  &chdir( $_[0] );
}

=head3 df([$device])

This function returns a hash reference which reflects the output of C<df>.

 task "df", "server01", sub {
    my $df = df();
    my $df_on_sda1 = df("/dev/sda1");
 };

This command will not be reported.

=cut

sub df {
  my ($dev) = @_;

  my $ret = {};

  $dev ||= "";

  my $exec = Rex::Interface::Exec->create;
  my ( $out, $err ) = $exec->exec("df $dev 2>/dev/null");

  my @lines = split( /\r?\n/, $out );

  $ret = _parse_df(@lines);

  if ($dev) {
    if ( keys %$ret == 1 ) {
      ($dev) = keys %$ret;
    }
    return $ret->{$dev};
  }

  return $ret;
}

sub _parse_df {
  my @lines = @_;
  chomp @lines;

  my $ret = {};

  shift @lines;
  my $current_fs = "";

  for my $line (@lines) {
    my ( $fs, $size, $used, $free, $use_per, $mounted_on ) =
      split( /\s+/, $line, 6 );
    $current_fs = $fs if $fs;

    if ( !$size ) {
      next;
    }

    $ret->{$current_fs} = {
      size       => $size,
      used       => $used,
      free       => $free,
      used_perc  => $use_per,
      mounted_on => $mounted_on
    };
  }

  return $ret;
}

=head3 du($path)

Returns the disk usage of C<$path>.

 task "du", "server01", sub {
   say "size of /var/www: " . du("/var/www");
 };

This command will not be reported.

=cut

sub du {
  my ($path) = @_;
  $path = resolv_path($path);

  my $exec  = Rex::Interface::Exec->create;
  my @lines = $exec->exec("du -s $path");
  my ($du)  = ( $lines[0] =~ m/^(\d+)/ );

  return $du;
}

=head3 mount($device, $mount_point, @options)

Mount devices.

 task "mount", "server01", sub {
   mount "/dev/sda5", "/tmp";
   mount "/dev/sda6", "/mnt/sda6",
          ensure    => "present",
          type      => "ext3",
          options   => [qw/noatime async/],
          on_change => sub { say "device mounted"; };
   #
   # mount persistent with entry in /etc/fstab
 
   mount "/dev/sda6", "/mnt/sda6",
          ensure     => "persistent",
          type       => "ext3",
          options    => [qw/noatime async/],
          on_change  => sub { say "device mounted"; };
 
   # to umount a device
   mount "/dev/sda6", "/mnt/sda6",
          ensure => "absent";
 
 };

In order to be more aligned with C<mount> terminology, the previously used C<fs> option has been deprecated in favor of the C<type> option. The C<fs> option is still supported and works as previously, but Rex prints a warning if it is being used. There's also a warning if both C<fs> and C<type> options are specified, and in this case C<type> will be used.

=cut

sub mount {
  my ( $device, $mount_point, @options ) = @_;
  my $option = {@options};

  if ( defined $option->{fs} ) {
    Rex::Logger::info(
      'The `fs` option of the mount command has been deprecated in favor of the `type` option. Please update your task.',
      'warn'
    );

    if ( !defined $option->{type} ) {
      $option->{type} = $option->{fs};
    }
    else {
      Rex::Logger::info(
        'Both `fs` and `type` options have been specified for mount command. Preferring `type`.',
        'warn'
      );
    }
  }

  delete $option->{fs};

  Rex::get_current_connection()->{reporter}
    ->report_resource_start( type => "mount", name => "$mount_point" );

  $option->{ensure} ||= "present"; # default

  if ( $option->{ensure} eq "absent" ) {
    &umount(
      $mount_point,
      device    => $device,
      on_change =>
        ( exists $option->{on_change} ? $option->{on_change} : undef )
    );
  }
  else {
    if ( $option->{ensure} eq "persistent" ) {
      $option->{persistent} = 1;
    }

    my $changed = 0;
    my $exec    = Rex::Interface::Exec->create;

    my ( $m_out, $m_err ) = $exec->exec("mount");
    my @mounted = split( /\r?\n/, $m_out );
    my ($already_mounted) = grep { m/$device on $mount_point/ } @mounted;
    if ($already_mounted) {
      Rex::Logger::debug("Device ($device) already mounted on $mount_point.");
      $changed = 0;
    }

    my $cmd = sprintf(
      "mount %s %s %s %s",
      $option->{type} ? "-t " . $option->{type} : "", # file system
      $option->{"options"}
      ? " -o " . join( ",", @{ $option->{"options"} } )
      : "",
      $device,
      $mount_point
    );

    unless ($already_mounted) {
      $exec->exec($cmd);
      if ( $? != 0 ) { die("Mount failed of $mount_point"); }
      $changed = 1;
      Rex::get_current_connection()->{reporter}->report(
        changed => 1,
        message => "Device $device mounted on $mount_point."
      );
    }

    if ( exists $option->{persistent} ) {
      if ( !exists $option->{type} ) {

        # no fs given, so get it from mount output
        my ( $out, $err ) = $exec->exec("mount");
        my @output = split( /\r?\n/, $out );
        my ($line) = grep { /^$device/ } @output;
        my ( $_d, $_o, $_p, $_t, $fs_type ) = split( /\s+/, $line );
        $option->{type} = $fs_type;

        my ($_options) = ( $line =~ m/\((.+?)\)/ );
        $option->{options} = $_options;
      }

      my $fh = Rex::Interface::File->create;

      my $old_md5 = md5("/etc/fstab");

      if ( !$fh->open( "<", "/etc/fstab" ) ) {
        Rex::Logger::debug("Can't open /etc/fstab for reading.");
        die("Can't open /etc/fstab for reading.");
      }

      my $f       = Rex::FS::File->new( fh => $fh );
      my @content = $f->read_all;
      $f->close;

      my @new_content = grep { !/^$device\s/ } @content;

      $option->{options} ||= "defaults";

      if ( ref( $option->{options} ) eq "ARRAY" ) {
        my $mountops = join( ",", @{ $option->{"options"} } );
        if ( $option->{label} ) {
          push( @new_content,
                "LABEL="
              . $option->{label}
              . "\t$mount_point\t$option->{type}\t$mountops\t0 0\n" );
        }
        else {
          push( @new_content,
            "$device\t$mount_point\t$option->{type}\t$mountops\t0 0\n" );
        }
      }
      else {
        if ( $option->{label} ) {
          push( @new_content,
                "LABEL="
              . $option->{label}
              . "\t$mount_point\t$option->{type}\t$option->{options}\t0 0\n" );
        }
        else {
          push( @new_content,
            "$device\t$mount_point\t$option->{type}\t$option->{options}\t0 0\n"
          );
        }
      }

      $fh = Rex::Interface::File->create;

      if ( !$fh->open( ">", "/etc/fstab" ) ) {
        Rex::Logger::debug("Can't open /etc/fstab for writing.");
        die("Can't open /etc/fstab for writing.");
      }

      $f = Rex::FS::File->new( fh => $fh );
      $f->write( join( "\n", @new_content ) );
      $f->close;

      my $new_md5 = md5("/etc/fstab");

      if ( $new_md5 ne $old_md5 ) {
        Rex::get_current_connection()->{reporter}
          ->report( changed => 1, message => "File /etc/fstab updated." );
        $changed = 1;
      }
    }

    if ( $changed == 1 ) {
      if ( exists $option->{on_change} && ref $option->{on_change} eq "CODE" ) {
        $option->{on_change}->( $device, $mount_point );
      }
    }
  }

  Rex::get_current_connection()->{reporter}
    ->report_resource_end( type => "mount", name => "$mount_point" );
}

=head3 umount($mount_point)

Unmount device.

 task "umount", "server01", sub {
   umount "/tmp";
 };

=cut

sub umount {
  my ( $mount_point, %option ) = @_;

  my $device;

  if ( exists $option{device} ) {
    $device = $option{device};
  }

  my $exec = Rex::Interface::Exec->create;

  Rex::get_current_connection()->{reporter}
    ->report_resource_start( type => "umount", name => "$mount_point" );

  my $changed = 0;
  my ( $m_out, $m_err ) = $exec->exec("mount");
  my @mounted = split( /\r?\n/, $m_out );
  my $already_mounted;

  if ($device) {
    ($already_mounted) = grep { m/$device on $mount_point/ } @mounted;
  }
  else {
    ($already_mounted) = grep { m/on $mount_point/ } @mounted;
  }

  if ($already_mounted) {
    $exec->exec("umount $mount_point");
    if ( $? != 0 ) { die("Umount failed of $mount_point"); }
    $changed = 1;
  }

  if ($changed) {
    if ( exists $option{on_change} && ref $option{on_change} eq "CODE" ) {
      $option{on_change}->( $mount_point, %option );
    }
    Rex::get_current_connection()->{reporter}
      ->report( changed => 1, message => "Unmounted $mount_point." );
  }

  Rex::get_current_connection()->{reporter}
    ->report_resource_end( type => "umount", name => "$mount_point" );
}

=head3 glob($glob)

Returns the list of filename expansions for C<$glob> as L<Perl's built-in glob|https://perldoc.perl.org/functions/glob.html> would do.

 task "glob", "server1", sub {
   my @files_with_p = grep { is_file($_) } glob("/etc/p*");
 };

This command will not be reported.

=cut

sub glob {
  my ($glob) = @_;
  $glob = resolv_path($glob);

  my $fs = Rex::Interface::Fs->create;
  return $fs->glob($glob);
}

1;