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
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
|
#!/usr/bin/perl
# Unix SMB/CIFS implementation.
# Test suite for the tar backup mode of smbclient.
# Copyright (C) Aurélien Aptel 2013
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
=head1 NAME
C<test_smbclient_tarmode.pl> - Test for smbclient tar backup feature
=cut
use v5.10;
use strict;
use warnings;
use Archive::Tar;
use Data::Dumper;
use Digest::MD5 qw/md5_hex/;
use File::Path qw/make_path remove_tree/;
use File::Spec;
use File::Temp;
use Getopt::Long;
use Pod::Usage;
use Term::ANSIColor;
sub d {print Dumper @_;}
# DEFAULTS
# 'our' to make them available in the File package
our $USER = '';
our $PW = '';
our $HOST = '';
our $IP = '';
our $SHARE = '';
our $DIR = 'tar_test_dir';
our $LOCALPATH = '';
our $TMP = File::Temp->newdir();
our $BIN = 'smbclient';
our $SUBUNIT = 0;
my $SELECTED_TEST = '';
my $LIST_TEST = 0;
my @SMBARGS = ();
our $DEBUG = 0;
our $VERBOSE = 0;
my $MAN = 0;
my $HELP = 0;
my $CLEAN = 0;
# all tests
my @TESTS = (
# ['test helper', \&test_helper],
['create, normal files (no attributes)', \&test_creation_normal, 'normal'],
['create, normal nested files (no attributes)', \&test_creation_normal, 'nested'],
['create, normal files (interactive)', \&test_creation_normal, 'inter'],
['create, large file', \&test_creation_large_file],
['create, long path', \&test_creation_long_path],
['create, incremental with -g', \&test_creation_incremental, '-g'],
['create, incremental with tarmode', \&test_creation_incremental, 'tarmode inc'],
['create, reset archived files with -a', \&test_creation_reset, '-a'],
['create, reset archived files with tarmode', \&test_creation_reset, 'tarmode reset'],
['create, files newer than a file', \&test_creation_newer],
['create, combination of tarmode filter', \&test_creation_attr],
['create, explicit include', \&test_creation_include],
['create, explicit exclude', \&test_creation_exclude],
['create, include w/ filelist (F)', \&test_creation_list],
['create, wildcard simple', \&test_creation_wildcard_simple],
['create, regex', \&test_creation_regex],
['create, multiple backup in session', \&test_creation_multiple],
['extract, normal files', \&test_extraction_normal],
['extract, explicit include', \&test_extraction_include],
['extract, explicit exclude', \&test_extraction_exclude],
['extract, include w/ filelist (F)', \&test_extraction_list],
['extract, regex', \&test_extraction_regex],
);
=head1 SYNOPSIS
test_smbclient_tarmode.pl [options] -- [smbclient options]
Options:
-h, --help brief help message
--man full documentation
Environment:
-u, --user USER
-p, --password PW
-n, --name HOST (required)
-i, --ip IP
-s, --share SHARE (required)
-d, --dir PATH
sub-path to use on the share
-l, --local-path PATH (required)
path to the root of the samba share on the machine.
-b, --bin BIN
path to the smbclient binary to use
Test:
--list
list tests
--test N
--test A-B
--test A,B,D-F
only run certain tests (accept list and intervals of numbers)
-v, --verbose
be more verbose
--debug
print command and their output (also set -v)
--subunit
print output in subunit format
=cut
GetOptions('u|user=s' => \$USER,
'p|password=s' => \$PW,
'n|name=s' => \$HOST,
'i|ip=s' => \$IP,
's|share=s' => \$SHARE,
'd|dir=s' => \$DIR,
'l|local-path=s' => \$LOCALPATH,
'b|bin=s' => \$BIN,
'test=s' => \$SELECTED_TEST,
'list' => \$LIST_TEST,
'clean' => \$CLEAN,
'subunit' => \$SUBUNIT,
'debug' => \$DEBUG,
'v|verbose' => \$VERBOSE,
'h|help' => \$HELP,
'man' => \$MAN) or pod2usage(2);
pod2usage(0) if $HELP;
pod2usage(-exitval => 0, -verbose => 2) if $MAN;
list_test(), exit 0 if $LIST_TEST;
pod2usage(1) unless $HOST;
pod2usage(1) unless $SHARE;
pod2usage(1) unless $LOCALPATH;
if ($USER xor $PW) {
die "Need both user and password when one is provided\n";
}
elsif ($USER and $PW) {
push @SMBARGS, '-U'.$USER.'%'.$PW;
}
else {
push @SMBARGS, '-N';
}
if ($IP) {
push @SMBARGS, '-I', $IP;
}
# remaining arguments are passed to smbclient
push @SMBARGS, @ARGV;
# path to store the downloaded tarball
my $TAR = "$TMP/tarmode.tar";
#####
# SANITIZATION
# remove all final slashes from input paths
$LOCALPATH =~ s{[/\\]+$}{}g;
$SHARE =~ s{[/\\]+$}{}g;
$HOST =~ s{[/\\]+$}{}g;
$DIR =~ s{^\.[/\\]+$}{}g;
$DIR =~ s{[/\\]+$}{}g;
if (!-d $LOCALPATH) {
die "Local path '$LOCALPATH' is not a directory.\n";
}
if ($CLEAN) {
# clean the whole root first
remove_tree($LOCALPATH, { keep_root => 1 });
}
if ($DEBUG) {
$VERBOSE = 1;
}
#####
# RUN TESTS
my @selection = parse_test_string($SELECTED_TEST);
if ($SELECTED_TEST eq '') {
run_test(@TESTS);
} elsif (@selection > 0) {
run_test(@selection);
} else {
die "Test selection '$SELECTED_TEST' is invalid\n";
}
#################################
=head1 DOCUMENTATION
=head2 Defining a test
=over
=item * Create a function C<test_yourtest>
=item * Use the File module, documented below
=item * Use C<smb_tar>, C<smb_client>, C<check_tar> or C<check_remote>
=item * Return number of error
=item * Add function to C<@TESTS>
=back
The function must be placed in the C<@TESTS> list along with a short
description and optional arguments.
=cut
sub test_creation_newer {
my @files;
my $dt = 3000;
# create oldest file at - DT
my $oldest = File->new_remote('oldest');
$oldest->set_time(time - $dt);
# create limit file
my $limit = File->new_local("$TMP/limit");
# create newA file at + DT
my $newA = File->new_remote('newA');
$newA->set_time(time + $dt);
# create newB file at + DT
my $newB = File->new_remote('newB');
$newB->set_time(time + $dt);
# get files newer than limit_file
push @files, $newA, $newB;
smb_tar('', '-TcN', $limit->localpath, $TAR, $DIR);
return check_tar($TAR, \@files);
}
sub test_creation_attr {
my @attr = qw/r h s a/;
my @all;
my @inc;
my $err = 0;
# one normal file
my $f = File->new_remote("file-n.txt");
push @all, $f;
# combinations of attributes
for my $n (1..@attr) {
for (combine(\@attr, $n)) {
my @t = @$_;
my $fn = "file-" . join('+', @t) . ".txt";
my $f = File->new_remote($fn);
$f->set_attr(@t);
push @all, $f;
}
}
@inc = grep { !$_->attr('s') } @all;
smb_tar('tarmode nosystem', '-Tc', $TAR, $DIR);
$err += check_tar($TAR, \@inc);
@inc = grep { !$_->attr('h') } @all;
smb_tar('tarmode nohidden', '-Tc', $TAR, $DIR);
$err += check_tar($TAR, \@inc);
@inc = grep { !$_->attr_any('h', 's') } @all;
smb_tar('tarmode nohidden nosystem', '-Tc', $TAR, $DIR);
$err += check_tar($TAR, \@inc);
@inc = grep { $_->attr('a') && !$_->attr_any('h', 's') } @all;
smb_tar('tarmode inc nohidden nosystem', '-Tc', $TAR, $DIR);
$err += check_tar($TAR, \@inc);
# adjust attr so remote files can be deleted with deltree
File::walk(sub { $_->set_attr(qw/n r s h/) }, File::tree($DIR));
$err;
}
sub test_creation_reset {
my ($mode) = @_;
my @files;
my $n = 3;
for (1..$n) {
my $f = File->new_remote("file-$_");
$f->set_attr('a');
push @files, $f;
}
if ($mode =~ /reset/) {
smb_tar('tarmode full reset', '-Tc', $TAR, $DIR);
} else {
smb_tar('', '-Tca', $TAR, $DIR);
}
my $err = check_tar($TAR, \@files);
return $err if ($err > 0);
for my $f (File::list($DIR)) {
if ($f->{attr}{a}) {
printf " ! %s %s\n", $f->attr_str, $f->remotepath;
$err++;
}
}
return $err;
}
sub test_creation_large_file {
my $size = int(15e6); # 15MB
my $f = File->new_remote("fat.jpg", 0, $size);
smb_tar('', '-Tc', $TAR, $DIR);
return check_tar($TAR, [$f]);
}
sub test_creation_long_path {
my $d = "a"x130;
my @all;
for (qw( foo/a bar/b )) {
push @all, File->new_remote("$d/$_");
}
smb_tar('', '-Tc', $TAR, $DIR);
return check_tar($TAR, \@all);
}
sub test_creation_normal {
my ($mode) = @_;
my $prefix = ($mode =~ /nest/) ? "/foo/bar/bar/" : '';
my @files;
my $n = 5;
for (1..$n) {
my $f = File->new_remote($prefix."file-$_");
push @files, $f;
}
if ($mode =~ /inter/) {
smb_tar("tar c $TAR $DIR", '');
} else {
smb_tar('tarmode full', '-Tc', $TAR, $DIR);
}
return check_tar($TAR, \@files);
}
sub test_creation_incremental {
my ($mode) = @_;
my @files;
my $n = 10;
for (1..$n) {
my $f = File->new_remote("file-$_");
# set archive bit on ~half of them
if ($_ < $n/2) {
$f->set_attr('a');
push @files, $f;
}
else {
$f->set_attr((qw/n r s h/)[$_ % 4]);
}
}
if ($mode =~ /inc/) {
smb_tar('tarmode inc', '-Tc', $TAR, $DIR);
} else {
smb_tar('', '-Tcg', $TAR, $DIR);
}
my $res = check_tar($TAR, \@files);
# adjust attr so remote files can be deleted with deltree
File::walk(sub { $_->set_attr(qw/n r s h/) }, File::tree($DIR));
return $res
}
sub test_extraction_normal {
my @files;
my $n = 5;
for (1..$n) {
my $f = File->new_remote("file-$_");
push @files, $f;
}
# store
smb_tar('', '-Tc', $TAR, $DIR);
my $err = check_tar($TAR, \@files);
return $err if $err > 0;
reset_remote();
smb_tar('', '-Tx', $TAR);
check_remote($DIR, \@files);
}
sub test_extraction_include {
my @all_files;
my @inc_files;
for (qw(file_inc inc/b inc/c inc/dir/foo dir_ex/d zob)) {
my $f = File->new_remote($_);
push @all_files, $f;
push @inc_files, $f if /inc/;
}
# store
smb_tar('', '-Tc', $TAR, $DIR);
my $err = check_tar($TAR, \@all_files);
return $err if $err > 0;
reset_remote();
smb_tar('', '-TxI', $TAR, "$DIR/file_inc", "$DIR/inc");
check_remote($DIR, \@inc_files);
}
sub test_extraction_exclude {
my @all_files;
my @inc_files;
for (qw(file_exc exc/b exc/c exc/dir/foo dir_ex/d zob)) {
my $f = File->new_remote($_);
push @all_files, $f;
push @inc_files, $f if !/exc/;
}
# store
smb_tar('', '-Tc', $TAR, $DIR);
my $err = check_tar($TAR, \@all_files);
return $err if $err > 0;
reset_remote();
smb_tar('', '-TxX', $TAR, "$DIR/file_exc", "$DIR/exc");
check_remote($DIR, \@inc_files);
}
sub test_creation_include {
my @files;
for (qw(file_inc inc/b inc/c inc/dir/foo dir_ex/d zob)) {
my $f = File->new_remote($_);
push @files, $f if /inc/;
}
smb_tar('', '-TcI', $TAR, "$DIR/file_inc", "$DIR/inc");
return check_tar($TAR, \@files);
}
sub test_creation_exclude {
my @files;
for (qw(file_ex ex/b ex/c ex/dir/foo foo/bar zob)) {
my $f = File->new_remote($_);
push @files, $f if !/ex/;
}
smb_tar('', '-TcX', $TAR, "$DIR/file_ex", "$DIR/ex");
return check_tar($TAR, \@files);
}
sub test_creation_list {
my @inc_files;
for (qw(file_inc inc/b inc/c inc/dir/foo foo/bar zob)) {
my $f = File->new_remote($_);
push @inc_files, $f if /inc/;
}
my $flist = File->new_local("$TMP/list", file_list(@inc_files));
smb_tar('', '-TcF', $TAR, $flist->localpath);
return check_tar($TAR, \@inc_files);
}
sub test_creation_regex {
my @exts = qw(jpg exe);
my @dirs = ('', "$DIR/");
my @all = make_env(\@exts, \@dirs);
my $nb;
my @inc;
my $err = 0;
# EXCLUSION
# skip *.exe
@inc = grep { $_->remotepath !~ m{exe$} } @all;
smb_tar('', '-TcrX', $TAR, '*.exe');
$err += check_tar($TAR, \@inc);
# if the pattern is a path, it doesn't skip anything
smb_tar('', '-TcrX', $TAR, "$DIR/*.exe");
$err += check_tar($TAR, \@all);
smb_tar('', '-TcrX', $TAR, "$DIR/*");
$err += check_tar($TAR, \@all);
smb_tar('', '-TcrX', $TAR, "$DIR");
$err += check_tar($TAR, \@all);
# no paths => include everything
smb_tar('', '-TcrX', $TAR);
$err += check_tar($TAR, \@all);
# skip everything
smb_tar('', '-TcrX', $TAR, "*.*");
$err += check_tar($TAR, []);
smb_tar('', '-TcrX', $TAR, "*");
$err += check_tar($TAR, []);
# INCLUSION
# no paths => include everything
smb_tar('', '-Tcr', $TAR);
$err += check_tar($TAR, \@all);
# include everything
smb_tar('', '-Tcr', $TAR, '*');
$err += check_tar($TAR, \@all);
# include only .exe at root
@inc = grep { $_->remotepath =~ m{^[^/]+exe$}} @all;
smb_tar('', '-Tcr', $TAR, '*.exe');
$err += check_tar($TAR, \@inc);
# smb_tar('', '-Tcr', $TAR, "$DIR/*");
## in old version (bug?)
# $err += check_tar($TAR, []);
## in rewrite
# @inc = grep { $_->remotepath =~ /^$DIR/ } @all;
# $err += check_tar($TAR, \@inc);
$err;
}
sub test_creation_wildcard_simple {
my @exts = qw(jpg exe);
my @dirs = ('', "$DIR/");
my @all = make_env(\@exts, \@dirs);
my $nb;
my @inc;
my $err = 0;
@inc = grep { $_->remotepath =~ m{^[^/]+exe$} } @all;
smb_tar('', '-Tc', $TAR, "*.exe");
$err += check_tar($TAR, \@inc);
@inc = grep { $_->remotepath =~ m{$DIR/.+exe$} } @all;
smb_tar('', '-Tc', $TAR, "$DIR/*.exe");
$err += check_tar($TAR, \@inc);
$err;
}
# NOT USED
# helper to test tests
sub test_helper {
my @exts = qw(txt jpg exe);
my @dirs = ('', "$DIR/", "$DIR/dir/");
my @all = make_env(\@exts, \@dirs);
my $nb;
my $err = 0;
my @inc;
smb_tar('', '-Tc', $TAR);
return 1 if check_tar($TAR, \@all);
reset_remote();
my @exc = grep { $_->remotepath =~ m!/dir/.+exe!} @all;
@inc = grep { $_->remotepath !~ m!/dir/.+exe!} @all;
smb_tar('', '-TxXr', $TAR, "/$DIR/dir/*.exe");
$err += check_remote('/', \@all); # BUG: should be \@inc
reset_remote();
return 0;
}
sub test_creation_multiple {
my @exts = qw(jpg exe);
my @dirs = ('', "$DIR/");
my @all = make_env(\@exts, \@dirs);
my $nb;
my @inc;
my $err = 0;
my ($tarA, $tarB) = ("$TMP/a.tar", "$TMP/b.tar");
my @incA = grep { $_->remotepath =~ m{^[^/]+exe$} } @all;
my @incB = grep { $_->remotepath =~ m{^[^/]+jpg$} } @all;
my $flistA = File->new_local("$TMP/listA", file_list(@incA))->localpath;
my $flistB = File->new_local("$TMP/listB", file_list(@incB))->localpath;
smb_tar("tar cF $tarA $flistA ; tar cF $tarB $flistB ; quit");
$err += check_tar($tarA, \@incA);
$err += check_tar($tarB, \@incB);
$err;
}
sub test_extraction_regex {
my @exts = qw(txt jpg exe);
my @dirs = ('', "$DIR/", "$DIR/dir/");
my @all = make_env(\@exts, \@dirs);
my $nb;
my $err = 0;
my (@inc, @exc);
smb_tar('', '-Tc', $TAR);
return 1 if check_tar($TAR, \@all);
reset_remote();
# INCLUDE
# only include file at root
@inc = grep { $_->remotepath =~ m!exe!} @all;
smb_tar('', '-Txr', $TAR, "*.exe");
$err += check_remote('/', \@inc);
reset_remote();
@inc = grep { $_->remotepath =~ m!/dir/.+exe!} @all;
smb_tar('', '-Txr', $TAR, "/$DIR/dir/*.exe");
$err += check_remote('/', []); # BUG: should be \@inc
reset_remote();
# EXCLUDE
# exclude file not directly at root
@inc = grep { $_->remotepath =~ m!^[^/]+$!} @all;
@exc = grep { $_->remotepath !~ m!^[^/]+$!} @all;
smb_tar('', '-TxrX', $TAR, map {$_->remotepath} @exc);
$err += check_remote('/', \@all); # BUG: should be @inc...
reset_remote();
# exclude only $DIR/dir/*exe
@exc = grep { $_->remotepath =~ m!/dir/.+exe!} @all;
@inc = grep { $_->remotepath !~ m!/dir/.+exe!} @all;
smb_tar('', '-TxXr', $TAR, "/$DIR/dir/*.exe");
$err += check_remote('/', \@all); # BUG: should be \@inc
reset_remote();
$err;
}
sub test_extraction_wildcard {
my @exts = qw(txt jpg exe);
my @dirs = ('', "$DIR/", "$DIR/dir/");
my $nb;
my $err = 0;
for my $dir (@dirs) {
my @all;
$nb = 0;
for my $dir (@dirs) {
for (@exts) {
my $fn = $dir . "file$nb." . $_;
my $f = File->new_remote($fn, 'ABSPATH');
$f->delete_on_destruction(1);
push @all, $f;
$nb++;
}
}
my @inc;
my $ext = 'exe';
my $fn = $dir."file$nb.".$ext;
my $pattern = $dir.'*.'.$ext;
my $flist;
# with F
$flist = File->new_local("$TMP/list", "$pattern\n");
# store
my $re = '^'.$dir.'.*file';
@inc = grep { $dir eq '' or $_->remotepath =~ m{$re} } @all;
smb_tar('', '-Tc', $TAR, $dir);
$err += check_tar($TAR, \@inc);
reset_remote();
my $re2 = '^'.$dir.'file.+exe';
@inc = grep { $_->remotepath =~ /$re2/ } @all;
smb_tar('', '-TxrF', $TAR, $flist->localpath);
$err += check_remote($dir, \@inc);
reset_remote();
}
$err;
}
sub test_extraction_list {
my @inc_files;
my @all_files;
for (qw(file_inc inc/b inc/c inc/dir/foo foo/bar zob)) {
my $f = File->new_remote($_);
push @all_files, $f;
push @inc_files, $f if /inc/;
}
# store
smb_tar('', '-Tc', $TAR, $DIR);
my $err = check_tar($TAR, \@all_files);
return $err if $err > 0;
reset_remote();
my $flist = File->new_local("$TMP/list", file_list(@inc_files));
smb_tar('', '-TxF', $TAR, $flist->localpath);
return check_remote($DIR, \@inc_files);
}
#################################
# IMPLEMENTATION
=head2 Useful functions
Here are a list of useful functions and helpers to define tests.
=cut
# list test number and description
sub list_test {
my $i = 0;
for (@TESTS) {
my ($desc, $f, @args) = @$_;
printf "%2d.\t%s\n", $i++, $desc;
}
}
sub run_test {
if ($SUBUNIT) {
run_test_subunit(@_);
} else {
run_test_normal(@_);
}
}
sub run_test_normal {
for (@_) {
my ($desc, $f, @args) = @$_;
my $err;
reset_env();
say "TEST: $desc";
if ($VERBOSE) {
$err = $f->(@args);
} else {
# turn off STDOUT
open my $saveout, ">&STDOUT";
open STDOUT, '>', File::Spec->devnull();
$err = $f->(@args);
open STDOUT, ">&", $saveout;
}
print_res($err);
print "\n";
}
reset_env();
}
sub run_test_subunit {
for (@_) {
my ($desc, $f, @args) = @$_;
my $err;
my $str = '';
reset_env();
say "test: $desc";
# capture output in $buf
my $buf = '';
open my $handle, '>', \$buf;
select $handle;
# check for die() calls
eval {
$err = $f->(@args);
};
if ($@) {
$str = $@;
$err = 1;
}
close $handle;
# restore output
select STDOUT;
# result string is output + eventual exception message
$str = $buf.$str;
printf "%s: %s [\n%s]\n", ($err > 0 ? "failure" : "success"), $desc, $str;
}
reset_env();
}
sub parse_test_string {
my $s = shift;
my @tests = ();
if (!length($s)) {
return ();
}
for (split /,/, $s) {
if (/^\d+$/) {
if ($_ >= @TESTS) {
return ();
}
push @tests, $TESTS[$_];
}
elsif (/^(\d+)-(\d+)$/) {
my ($min, $max) = sort ($1, $2);
if ($max >= @TESTS) {
return ();
}
for ($min..$max) {
push @tests, $TESTS[$_];
}
}
else {
return ();
}
}
return @tests;
}
sub print_res {
my $err = shift;
if ($err) {
printf " RES: %s%d ERR%s\n", color('bold red'), $err, color 'reset';
} else {
printf " RES: %sOK%s\n", color('bold green'), color 'reset';
}
}
sub make_env {
my ($exts, $dirs) = @_;
my @all;
my $nb = 0;
for my $dir (@$dirs) {
for (@$exts) {
my $fn = $dir . "file$nb." . $_;
my $f = File->new_remote($fn, 'ABSPATH');
$f->delete_on_destruction(1);
push @all, $f;
$nb++;
}
}
@all;
}
=head3 C<combine ( \@set, $n )>
=head3 C<combine ( ['a', 'b', 'c'], 2 )>
Return a list of all possible I<n>-uplet (or combination of C<$n> element) of C<@set>.
=cut
sub combine {
my ($list, $n) = @_;
die "Insufficient list members" if $n > @$list;
return map [$_], @$list if $n <= 1;
my @comb;
for (my $i = 0; $i+$n <= @$list; $i++) {
my $val = $list->[$i];
my @rest = @$list[$i+1..$#$list];
push @comb, [$val, @$_] for combine(\@rest, $n-1);
}
return @comb;
}
=head3 C<reset_remote( )>
Remove all files in the server C<$DIR> (not root)
=cut
sub reset_remote {
# remove_tree($LOCALPATH . '/'. $DIR);
# make_path($LOCALPATH . '/'. $DIR);
my $DIR;
my @names;
my $name;
smb_client_cmd(0, '-c', "deltree ./*");
# Ensure all files are gone.
opendir(DIR,$LOCALPATH) or die "Can't open $LOCALPATH\n";
@names = readdir(DIR) or die "Unable to read $LOCALPATH\n";
closedir(DIR);
foreach $name (@names) {
next if ($name eq "."); # skip the current directory entry
next if ($name eq ".."); # skip the parent directory entry
die "$LOCALPATH not empty\n";
}
}
=head3 C<reset_tmp( )>
Remove all files in the temp directory C<$TMP>
=cut
sub reset_tmp {
remove_tree($TMP, { keep_root => 1 });
}
=head3 C<reset_env( )>
Remove both temp and remote (C<$DIR>) files
=cut
sub reset_env {
reset_tmp();
reset_remote();
}
=head3 C<file_list ( @files )>
Make a multiline string of all the files remote path, one path per line.
C<@files> must be a list of C<File> instance.
=cut
sub file_list {
my @files = @_;
my $s = '';
for (@files) {
$s .= $_->remotepath."\n";
}
return $s;
}
# remove leading "./"
sub remove_dot {
my $s = shift;
$s =~ s{^\./}{};
$s;
}
=head3 C<check_remote( $remotepath, \@files )>
Check if C<$remotepath> has B<exactly> all the C<@files>.
Print a summary on STDOUT.
C<@files> must be a list of C<File> instance.
=cut
sub check_remote {
my ($subpath, $files) = @_;
my (%done, %expected);
my (@less, @more, @diff);
for (@$files) {
my $fn = remove_dot($_->remotepath);
$expected{$fn} = $_;
$done{$fn} = 0;
}
my %remote;
File::walk(sub { $remote{remove_dot($_->remotepath)} = $_ }, File::tree($subpath));
for my $rfile (sort keys %remote) {
# files that shouldn't be there
if (!exists $expected{$rfile}) {
say " + $rfile";
push @more, $rfile;
next;
}
# same file multiple times
if ($done{$rfile} > 0) {
$done{$rfile}++;
push @more, $rfile;
printf " +%3d %s\n", $done{$rfile}, $rfile;
next;
}
$done{$rfile}++;
# different file
my $rmd5 = $remote{$rfile}->md5;
if ($expected{$rfile}->md5 ne $rmd5) {
say " ! $rfile ($rmd5)";
push @diff, $rfile;
next;
}
say " $rfile";
}
# file that should have been in tar
@less = grep { $done{$_} == 0 } sort keys %done;
for (@less) {
say " - $_";
}
# summary
printf("\t%d files, +%d, -%d, !%d\n",
scalar keys %done,
scalar @more,
scalar @less,
scalar @diff);
return (@more + @less + @diff); # nb of errors
}
=head3 C<check_tar( $path_to_tar, \@files )>
Check if the archive C<$path_to_tar> has B<exactly> all the C<@files>.
Print a summary on C<STDOUT>;
C<@files> must be a list of C<File> instance.
=cut
sub check_tar {
my ($tar, $files) = @_;
my %done;
my (@less, @more, @diff);
my %h;
if (!-f $tar) {
say "no tar file $tar";
return 1;
}
for (@$files) {
$h{$_->tarpath} = $_;
$done{$_->tarpath} = 0;
}
my $total = 0;
my $i = Archive::Tar->iter($tar, 1, {md5 => 1});
while (my $f = $i->()) {
if ($f->has_content) {
my $p = $f->full_path;
# we skip pseudo files of Pax format archives
next if ($p =~ m/\/PaxHeader/);
$total++;
$p =~ s{^\./+}{};
# file that shouldn't be there
if (!exists $done{$p}) {
push @more, $p;
say " + $p";
next;
}
# same file multiple times
if ($done{$p} > 0) {
$done{$p}++;
push @more, $p;
printf " +%3d %s\n", $done{$p}, $p;
next;
}
$done{$p}++;
# different file
my $md5 = $f->data;
if ($^V lt v5.16) {
$md5 = md5_hex($md5);
}
if ($md5 ne $h{$p}->md5) {
say " ! $p ($md5)";
push @diff, $p;
next;
}
say " $p";
}
}
# file that should have been in tar
@less = grep { $done{$_} == 0 } keys %done;
for (sort @less) {
say " - $_";
}
# summary
printf("\t%d files, +%d, -%d, !%d\n",
$total,
scalar @more,
scalar @less,
scalar @diff);
return (@more + @less + @diff); # nb of errors
}
=head3 C<smb_client_cmd( $will_die, @args)>
=head3 C<smb_client_cmd( 0, '-c', 'deltree', $somedir )>
Run smbclient with C<@args> passed as argument and return output.
Each element of C<@args> becomes one escaped argument of smbclient.
Host, share, user, password and the additional arguments provided on
the command-line are already inserted.
The output contains both the C<STDOUT> and C<STDERR>.
if C<$will_die> then Die if smbclient crashes or exits with an error code.
otherwise return output
=cut
sub smb_client_cmd {
my ($will_die, @args) = @_;
my $fullpath = "//$HOST/$SHARE";
my $cmd = sprintf("%s %s %s",
quotemeta($BIN),
quotemeta($fullpath),
join(' ', map {quotemeta} (@SMBARGS, @args)));
if ($DEBUG) {
my $tmp = $cmd;
$tmp =~ s{\\([./+-])}{$1}g;
say color('bold yellow'), $tmp, color('reset');
}
my $out = `$cmd 2>&1`;
my $err = $?;
my $errstr = '';
# handle abnormal exit
if ($err == -1) {
$errstr = "failed to execute $cmd: $!\n";
}
elsif ($err & 127) {
$errstr = sprintf "child died with signal %d (%s)\n", ($err & 127), $cmd;
}
elsif ($err >> 8) {
$errstr = sprintf "child exited with value %d (%s)\n", ($err >> 8), $cmd;
}
if ($DEBUG) {
say $out;
}
if ($err) {
if ($will_die) {
die "ERROR: $errstr";
} else {
say "ERROR: $errstr";
}
}
return $out;
}
=head3 C<smb_client ( @args )>
Run smbclient with C<@args> passed as argument and return output.
Each element of C<@args> becomes one escaped argument of smbclient.
Host, share, user, password and the additional arguments provided on
the command-line are already inserted.
The output contains both the C<STDOUT> and C<STDERR>.
Die if smbclient crashes or exits with an error code.
=cut
sub smb_client {
my (@args) = @_;
return smb_client_cmd(1, @args)
}
sub smb_cmd {
return smb_client('-c', join(' ', @_));
}
=head3 C<smb_tar( $cmd, @args )>
=head3 C<smb_tar( 'tarmode inc', '-Tc', $TAR, $DIR )>
Run C<$cmd> command and use C<@args> as argument and return output.
Wrapper around C<smb_client> for tar calls.
=cut
sub smb_tar {
my ($cmd, @rest) = @_;
printf " CMD: %s\n ARG: %s\n", $cmd, join(' ', @rest);
smb_client((length($cmd) ? ('-c', $cmd) : ()), @rest);
}
=head3 C<random( $min, $max )>
Return integer in C<[ $min ; $max ]>
=cut
sub random {
my ($min, $max) = @_;
($min, $max) = ($max, $min) if ($min > $max);
$min + int(rand($max - $min));
}
#################################
package File;
=head2 The File module
All the test should use the C<File> class. It has nice functions and
methods to deal with paths, to create random files, to list the
content of the server, to change attributes, etc.
There are 2 kinds of C<File>: remote and local.
=over
=item * Remote files are accessible on the server.
=item * Local files are not.
=back
Thus, some methods only works on remote files. If they do not make
sense for local ones, they always return undef.
=cut
use File::Basename;
use File::Path qw/make_path remove_tree/;
use Digest::MD5 qw/md5_hex/;
use Scalar::Util 'blessed';
=head3 Constructors
=head4 C<< File->new_remote($path [, $abs, $size]) >>
Creates a file accessible on the server at C<$DIR/$path> ie. not at the
root of the share and write C<$size> random bytes.
If no size is provided, a random size is chosen.
If you want to remove the automatic prefix C<$DIR>, set C<$abs> to 1.
The file is created without any DOS attributes.
If C<$path> contains non-existent directories, they are automatically
created.
=cut
sub new_remote {
my ($class, $path, $abs, $size) = @_;
my ($file, $dir) = fileparse($path);
$dir = '' if $dir eq './';
my $loc;
if ($abs) {
$loc = cleanpath($main::LOCALPATH.'/'.$dir);
} else {
$dir = cleanpath($main::DIR.'/'.$dir);
$loc = cleanpath($main::LOCALPATH.'/'.$dir);
}
make_path($loc);
my $self = bless {
'attr' => {qw/r 0 s 0 h 0 a 0 d 0 n 0/},
'dir' => $dir,
'name' => $file,
'md5' => create_file($loc.'/'.$file, $size),
'remote' => 1,
}, $class;
$self->set_attr();
$self;
}
=head4 C<< File->new_local($abs_path [, $data]) >>
Creates a file at C<$abs_path> with $data in it on the system.
If $data is not provided, fill it with random bytes.
=cut
sub new_local {
my ($class, $path, $data) = @_;
my ($file, $dir) = fileparse($path);
make_path($dir);
my $md5;
if (defined $data) {
open my $f, '>', $path or die "can't write in $path: $!";
print $f $data;
close $f;
$md5 = md5_hex($data);
} else {
$md5 = create_file($path);
}
my $self = {
'attr' => {qw/r 0 s 0 h 0 a 0 d 0 n 0/},
'dir' => $dir,
'name' => $file,
'md5' => $md5,
'remote' => 0,
};
bless $self, $class;
}
=head3 Methods
=head4 C<< $f->localpath >>
Return path on the system eg. F</opt/samba/share/test_tar_mode/file>
=cut
sub localpath {
my $s = shift;
if ($s->{remote}) {
return cleanpath($main::LOCALPATH.'/'.$s->remotepath);
}
else {
return cleanpath($s->{dir}.'/'.$s->{name});
}
}
=head4 C<< $f->remotepath >>
Return path on the server share.
Return C<undef> if the file is local.
=cut
sub remotepath {
my ($s) = @_;
return undef if !$s->{remote};
my $r = $s->{dir}.'/'.$s->{name};
$r =~ s{^/}{};
return cleanpath($r);
}
=head4 C<< $f->remotedir >>
Return the directory path of the file on the server.
Like C<< $f->remotepath >> but without the final file name.
=cut
sub remotedir {
my $s = shift;
return undef if !$s->{remote};
cleanpath($s->{dir});
}
=head4 C<< $f->tarpath >>
Return path as it would appear in a tar archive.
Like C<< $f->remotepath >> but prefixed with F<./>
=cut
sub tarpath {
my $s = shift;
return undef if !$s->{remote};
my $r = $s->remotepath;
$r =~ s{^\./+}{};
return $r;
}
=head4 C<< $f->delete_on_destruction( 0 ) >>
=head4 C<< $f->delete_on_destruction( 1 ) >>
By default, a C<File> is not deleted on the filesystem when it is not
referenced anymore in Perl memory.
When set to 1, the destructor unlink the file if it is not already removed.
If the C<File> created directories when constructed, it does not remove them.
=cut
sub delete_on_destruction {
my ($s, $delete) = @_;
$s->{delete_on_destruction} = $delete;
}
=head4 C<< $f->set_attr( ) >>
=head4 C<< $f->set_attr( 'a' ) >>
=head4 C<< $f->set_attr( 'a', 'r', 's', 'h' ) >>
Remove all DOS attributes and only set the one provided.
=cut
sub set_attr {
my ($s, @flags) = @_;
return undef if !$s->{remote};
$s->{attr} = {qw/r 0 s 0 h 0 a 0 d 0 n 0/};
for (@flags) {
$s->{attr}{lc($_)} = 1;
}
my $file = $s->{name};
my @args;
if ($s->remotedir) {
push @args, '-D', $s->remotedir;
}
main::smb_client(@args, '-c', qq{setmode "$file" -rsha});
if (@flags && $flags[0] !~ /n/i) {
main::smb_client(@args, '-c', qq{setmode "$file" +}.join('', @flags));
}
}
=head4 C<< $f->attr_any( 'a' ) >>
=head4 C<< $f->attr_any( 'a', 's', ... ) >>
Return 1 if the file has any of the DOS attributes provided.
=cut
sub attr_any {
my ($s, @flags) = @_;
for (@flags) {
return 1 if $s->{attr}{$_};
}
0;
}
=head4 C<< $f->attr( 'a' ) >>
=head4 C<< $f->attr( 'a', 's', ... ) >>
Return 1 if the file has all the DOS attributes provided.
=cut
sub attr {
my ($s, @flags) = @_;
for (@flags) {
return 0 if !$s->{attr}{$_};
}
1;
}
=head4 C<< $f->attr_str >>
Return DOS attributes as a compact string.
Read-only, hidden, system, archive => "rhsa"
=cut
sub attr_str {
my $s = shift;
return undef if !$s->{remote};
join('', map {$_ if $s->{attr}{$_}} qw/r h s a d n/);
}
=head4 C<< $f->set_time($t) >>
Set modification and access time of the file to C<$t>.
C<$t> must be in Epoch time (number of seconds since 1970/1/1).
=cut
sub set_time {
my ($s, $t) = @_;
utime $t, $t, $s->localpath;
}
=head4 C<< $f->md5 >>
Return md5 sum of the file.
The result is cached.
=cut
sub md5 {
my $s = shift;
if (!$s->{md5}) {
open my $h, '<', $s->localpath() or die "can't read ".$s->localpath.": $!";
binmode $h;
$s->{md5} = Digest::MD5->new->addfile($h)->hexdigest;
close $h;
}
return $s->{md5};
}
sub DESTROY {
my $s = shift;
if ($s->{delete_on_destruction} && -f $s->localpath) {
if ($main::DEBUG) {
say "DESTROY ".$s->localpath;
}
unlink $s->localpath;
}
}
=head3 Functions
=head4 C<< File::walk( \&function, @files) >>
=head4 C<< File::walk( sub { ... }, @files) >>
Iterate on file hierarchy in C<@files> and return accumulated results.
Use C<$_> in the sub to access the current C<File>.
The C<@files> must come from a call to the C<File::tree> function.
=cut
sub walk {
my $fun = \&{shift @_};
my @res;
for (@_) {
if ($_->{attr}{d}) {
push @res, walk($fun, @{$_->{content}});
} else {
push @res, $fun->($_);
}
}
return @res;
}
=head4 C<< File::list( $remotepath ) >>
Return list of file (C<File> instance) in C<$remotepath>.
C<$remotepath> must be a directory.
=cut
sub list {
my ($path) = @_;
$path ||= '/';
my @files;
my $out = main::smb_client('-D', $path, '-c', 'ls');
$path =~ s{^/}{};
for (split /\n/, $out) {
next if !/^ (.+?)\s+([AHSRDN]*)\s+(\d+)\s+(.+)/o;
my ($fn, $attr, $size, $date) = ($1, $2, $3, $4);
next if $fn =~ /^\.{1,2}$/;
push @files, bless {
'remote' => 1,
'dir' => $path,
'name' => $fn,
'size' => int($size),
'date' => $date,
'attr' => {
# list context returns something different than the
# boolean matching result => force scalar context
'a' => scalar ($attr =~ /A/),
'h' => scalar ($attr =~ /H/),
's' => scalar ($attr =~ /S/),
'r' => scalar ($attr =~ /R/),
'd' => scalar ($attr =~ /D/),
'n' => scalar ($attr =~ /N/),
},
}, 'File';
}
return @files;
}
=head4 C<< File::tree( $remotepath ) >>
Return recursive list of file in C<$remotepath>.
C<$remotepath> must be a directory.
Use C<File::walk()> to iterate over all the files.
=cut
sub tree {
my ($d) = @_;
my @files;
if (!defined $d) {
@files = list();
} elsif (blessed $d) {
@files = list($d->remotepath);
} else {
@files = list($d);
}
for my $f (@files) {
if ($f->{attr}{d}) {
$f->{content} = [tree($f)];
}
}
return @files;
}
# remove trailing or duplicated slash
sub cleanpath {
my $p = shift;
$p =~ s{/+}{/}g;
$p =~ s{/$}{};
$p;
}
# create random file at path local path $fn
sub create_file {
my ($fn, $size) = @_;
my $buf = '';
unlink $fn if -e $fn;
$size ||= main::random(512, 1024);
$size = int($size);
my $md5;
# try /dev/urandom, faster
if (-e '/dev/urandom') {
my $cmd = sprintf('head -c %d /dev/urandom | tee %s | md5sum',
$size, quotemeta($fn));
$md5 = (split / /, `$cmd`)[0];
} else {
open my $out, '>', $fn or die "can't open $fn: $!\n";
binmode $out;
for (1..$size) {
$buf .= pack('C', main::random(0, 256));
}
print $out $buf;
close $out;
$md5 = md5_hex($buf);
}
return $md5;
}
=head3 Examples
# create remote file in $DIR/foo/bar
my $f = File->new_remote("foo/bar/myfile");
say $f->localpath; # /opt/share/$DIR/foo/bar/myfile
say $f->remotepath; # $DIR/foo/bar/myfile
say $f->remotedir; # $DIR/foo/bar
# same but in root dir
my $f = File->new_remote("myfile", 1);
say $f->localpath; # /opt/share/myfile
say $f->remotepath; # myfile
say $f->remotedir; #
# create local random temp file in $TMP
my $f = File->new_local("$TMP/temp");
say $f->remotepath; # undef because it's not on the server
# same but file contains "hello"
my $f = File->new_local("$TMP/temp", "hello");
# list of files in $DIR (1 level)
for (File::list($DIR)) {
say $_->remotepath;
}
# list of all files in dir and subdir of $DIR
File::walk(sub { say $_->remotepath }, File::tree($DIR));
=cut
1;
|