-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileSystem.pm
1097 lines (706 loc) · 26.2 KB
/
FileSystem.pm
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
# Copyright (c) 1998-2003, Mikhael Goikhman
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#mod scriptman package General::FileSystem;
require 5.004;
use strict;
use vars qw(@ISA @EXPORT);
require Exporter;
@ISA = qw(Exporter);
=mod scriptman
@EXPORT = qw(
loadFile saveFile appendFile removeFile copyFile moveFile
makeDir makePath cleanDir removeDir copyDir moveDir
listFileNames findFile findExecutable
defaultDirPerm preserveStat parsePath getCwd
);
=cut
use vars qw($CACHE_FILE_NUM $cacheCounter @prevFileNames @prevFileContentRefs);
use vars qw($ENABLE_CACHE %NEVER_COPY_FILES %NEVER_REMOVE_FILES);
use vars qw($DEFAULT_DIR_PERM $PRESERVED_STAT);
use vars qw($DEBUG_ENABLED $ERROR_HANDLER $LOAD_FILE_DIRS $SAVE_FILE_DIR);
BEGIN {
$ENABLE_CACHE = 0; # this is risky for dynamical files
%NEVER_COPY_FILES = ( 'CVS' => 1, 'core' => 1 );
%NEVER_REMOVE_FILES = ( 'CVS' => 1 );
$DEFAULT_DIR_PERM = 0775;
$PRESERVED_STAT = 0;
# allow these constants to be set directly from outside
$ERROR_HANDLER ||= "warn"; # may be "die", "warn", "quiet" or CODE
$DEBUG_ENABLED ||= 0;
$LOAD_FILE_DIRS ||= [ "." ]; # for non fully qualified files only
$SAVE_FILE_DIR ||= "."; # for non fully qualified files only
}
# ----------------------------------------------------------------------------
=head1 NAME
General::FileSystem - file system specific functions
=head1 SYNOPSIS
use General::FileSystem "-die", "-debug"; # die on errors
eval {
makePath("/tmp/my-own/dir");
my $fileContentRef = loadFile("/etc/issue");
saveFile("/tmp/my-own/dir/issue", $fileContentRef);
# This is equivalent to the previous two lines, but optimized
copyFile("/etc/issue", "/tmp/my-own/dir/issue");
makeDir("/tmp/my-own/dir2", 0711);
copyFile("/etc/issue", "/tmp/my-own/dir2/issue");
moveFile("/tmp/my-own/dir2/issue", "/tmp/my-own/dir2/issue2");
removeFile("/tmp/my-own/dir2/issue2");
cleanDir("/tmp/my-own/dir2"); # no effect, it's empty already
removeDir("/tmp/my-own");
};
if ($@) {
print "File System Error: $@";
};
or just:
use General::FileSystem;
copyFile("origin.txt", "backup.txt");
=head1 DESCRIPTION
This package contains common file operation functions:
B<loadFile>, B<saveFile>, B<appendFile>, B<removeFile>, B<copyFile>, B<moveFile>,
B<makeDir>, B<makePath>, B<cleanDir>, B<removeDir>, B<copyDir>, B<moveDir>,
B<listFileNames>, B<findFile>, B<findExecutable>,
B<defaultDirPerm>, B<preserveStat>, B<parsePath>, B<getCwd>.
On fatal file system errors all functions call the error handler, that may
throw exception (die), issue a warning or quietly return undef.
You may control this by passing one of the arguments I<-die>, I<-warn>
or I<-quiet> in B<use> or by setting C<$ERROR_HANDLER> to one of these
values (don't specify a dash in this case).
=head1 REQUIREMENTS
L<Cwd>, L<File::Basename>, L<File::Copy>.
=head1 FUNCTIONS
=cut
# ============================================================================
use Cwd;
use File::Basename;
use File::Copy;
sub import ($;$) {
my $package = shift;
while (@_ && $_[0] =~ /^-/) {
local $_ = shift;
$ERROR_HANDLER = $1 if /^-(die|warn|quiet)$/i;
$DEBUG_ENABLED = $1 if /^-(debug)$/i;
}
$package->export_to_level(1, @_);
}
# private function
sub callErrorHandler ($) {
my $msg = shift;
die "$msg: [$!]\n" if $ERROR_HANDLER eq "die";
warn "$msg: [$!]\n" if $ERROR_HANDLER eq "warn";
return undef if $ERROR_HANDLER eq "quiet";
&$ERROR_HANDLER($msg) if ref($ERROR_HANDLER) eq "CODE";
return undef;
}
# private function
sub printLog ($) {
my $msg = shift;
return unless $DEBUG_ENABLED;
print STDERR "FileSystem: $msg\n";
}
# ----------------------------------------------------------------------------
=head2 loadFile
=over 4
=item usage
$contentRef = loadFile($fileName)
=item description
Loads file with given file-name from local filesystem.
=item parameters
* fileName - name of the file to be loaded.
=item returns
Reference to file content string on success, otherwise either dies or warns
and returns undef as configured.
=back
=cut
# ============================================================================
BEGIN {
$CACHE_FILE_NUM = 6;
$cacheCounter = -1;
@prevFileNames = ("", "", "", "", "", "");
@prevFileContentRefs = \("", "", "", "", "", "");
}
sub loadFile ($) {
my $fileName = shift;
foreach (@$LOAD_FILE_DIRS) {
if (-f "$_/$fileName") { $fileName = "$_/$fileName"; last; }
}
printLog("Loading file $fileName") if $DEBUG_ENABLED;
if ($ENABLE_CACHE) {
for (0 .. $CACHE_FILE_NUM-1) {
if ($fileName eq $prevFileNames[$_] && -r $fileName) {
printLog("getting from file cache") if $DEBUG_ENABLED;
return $prevFileContentRefs[$_];
}
}
}
open(FILE, "<$fileName") || return callErrorHandler("Can't open $fileName");
my $fileContent = join("", <FILE>);
close(FILE) || return callErrorHandler("Can't close $fileName");
if ($ENABLE_CACHE) {
$cacheCounter = ($cacheCounter+1) % $CACHE_FILE_NUM;
$prevFileNames[$cacheCounter] = $fileName;
$prevFileContentRefs[$cacheCounter] = \$fileContent;
}
return \$fileContent;
}
# ----------------------------------------------------------------------------
=head2 saveFile
=over 4
=item description
Saves file-content to local filesystem with given file-name.
=item usage
saveFile($fileName, \$fileContent);
=item parameters
* fileName - name of the file to be saved into
* fileContentRef - reference to file-content string
* createSubdirs - optional flag (default is 0 - don't create subdirs)
=item returns
C<1> on success, otherwise either dies or warns and returns undef as configured.
=back
=cut
# ============================================================================
sub saveFile ($$;$) {
my ($fileName, $fileContentRef, $createDirs) = @_;
if ($fileName !~ m=^[/\\]|\w:\\=) {
$fileName = "$SAVE_FILE_DIR/$fileName";
}
printLog("Saving file $fileName") if $DEBUG_ENABLED;
die("saveFile: No SCALAR ref parameter\n")
unless ref($fileContentRef) eq 'SCALAR';
if ($ENABLE_CACHE) {
for (0 .. $CACHE_FILE_NUM-1) {
$prevFileContentRefs[$_] = $fileContentRef
if $fileName eq $prevFileNames[$_];
}
}
if ($createDirs) {
my $dirName = dirname($fileName);
makePath($dirName) unless -d $dirName;
}
open(FILE, ">$fileName") || return callErrorHandler("Can't open $fileName");
print FILE $$fileContentRef;
close(FILE) || return callErrorHandler("Can't close $fileName");
return 1;
}
# ----------------------------------------------------------------------------
=head2 appendFile
=over 4
=item description
Appends file-append-content to local filesystem with given file-name.
=item usage
appendFile($fileName, \$fileAppendContent);
=item parameters
* fileName - name of the file to be saved into
* fileAppendContentRef - reference to file-append-content string
=item returns
C<1> on success, otherwise either dies or warns and returns undef as configured.
=back
=cut
# ============================================================================
sub appendFile ($$) {
my ($fileName, $fileAppendRef) = @_;
printLog("Append>>file $fileName") if $DEBUG_ENABLED;
if ($ENABLE_CACHE) {
for (0 .. $CACHE_FILE_NUM-1 && -r $fileName) {
${$prevFileContentRefs[$_]} .= $$fileAppendRef
if $fileName eq $prevFileNames[$_];
}
}
open(FILE, ">>$fileName") || return callErrorHandler("Can't append to $fileName");
print FILE $$fileAppendRef;
close(FILE) || return callErrorHandler("Can't close $fileName");
return 1;
}
# ----------------------------------------------------------------------------
=head2 removeFile
=over 4
=item description
Removes all files from given directory.
=item usage
removeFile($fileName);
=item parameters
* fileName - name of the file to be deleted
=item returns
C<1> on success, otherwise either dies or warns and returns undef as configured.
=back
=cut
# ============================================================================
sub removeFile ($;$) {
my $fileName = shift;
printLog("Removin file $fileName") if $DEBUG_ENABLED;
unlink($fileName) || return callErrorHandler("Can't unlink $fileName");
return 1;
}
# ----------------------------------------------------------------------------
=head2 makeDir
=over 4
=item description
Removes all files from given directory.
=item usage
makeDir($PREVIEW_DIR);
=item parameters
* directory to make
* optional creating dir permissions (default is $DEFAULT_DIR_PERM)
=item returns
C<1> on success, otherwise either dies or warns and returns undef as configured.
=back
=cut
# ============================================================================
sub makeDir ($;$) {
my $dirName = shift;
my $perm = shift || $DEFAULT_DIR_PERM;
printLog("Creating dir $dirName, " . sprintf("%o", $perm))
if $DEBUG_ENABLED;
mkdir($dirName, $perm) || return callErrorHandler("Can't mkdir $dirName");
return 1;
}
# ----------------------------------------------------------------------------
=head2 makePath
=over 4
=item description
Removes all files from given directory.
=item usage
makePath($PUBLISH_DIR);
=item parameters
* path to make
* optional creating dir permissions (default is $DEFAULT_DIR_PERM)
=item returns
C<1> on success, otherwise either dies or warns and returns undef as configured.
=back
=cut
# ============================================================================
sub makePath ($;$) {
my $dirName = shift;
my $perm = shift || $DEFAULT_DIR_PERM;
printLog("Making path $dirName, " . sprintf("%o", $perm))
if $DEBUG_ENABLED;
return 1 if -d $dirName;
my $parentDir = dirname($dirName);
local $DEBUG_ENABLED = 0;
&makePath($parentDir, $perm) unless -d $parentDir;
makeDir($dirName, $perm);
return 1;
}
# ----------------------------------------------------------------------------
=head2 copyFile
=over 4
=item description
Copies a file to another location.
=item usage
copyFile($from, $to);
=item parameters
* file name to copy from
* file name to copy to
=item returns
C<1> on success, otherwise either dies or warns and returns undef as configured.
=back
=cut
# ============================================================================
sub copyFile ($$) {
my ($srcFileName, $dstFileName) = @_;
printLog("Copying file $srcFileName to $dstFileName")
if $DEBUG_ENABLED;
# Must manage symbolic links somehow
# return if -l $srcFileName;
copy($srcFileName, $dstFileName)
or return callErrorHandler("Can't copy $srcFileName $dstFileName");
if ($PRESERVED_STAT) {
my ($device, $inode, $mode) = stat($srcFileName);
chmod($mode, $dstFileName);
}
return 1;
}
# ----------------------------------------------------------------------------
=head2 moveFile
=over 4
=item description
Moves (or renames) a file to another location.
=item usage
moveFile($from, $to);
=item parameters
* file name to move from
* file name to move to
=item returns
C<1> on success, otherwise either dies or warns and returns undef as configured.
=back
=cut
# ============================================================================
sub moveFile ($$) {
my ($srcFileName, $dstFileName) = @_;
printLog("Moving file $srcFileName to $dstFileName")
if $DEBUG_ENABLED;
move($srcFileName, $dstFileName)
or return callErrorHandler("Can't move $srcFileName $dstFileName");
return 1;
}
# ----------------------------------------------------------------------------
=head2 cleanDir
=over 4
=item description
Removes all files from given directory.
=item usage
cleanDir($PREVIEW_DIR);
=item parameters
* directory to clean
* optional flag:
0 - don't go recursively, unlink files in first level only
1 - recursively clean subdirs (default)
2 - unlink subdirs
3 - unlink given directory
=item returns
C<1> on success, otherwise either dies or warns and returns undef as configured.
=back
=cut
# ============================================================================
sub cleanDir ($;$) {
my $dirName = shift;
my $recursive = shift || 1;
die("cleanDir: Unsupported flag $recursive\n")
if $recursive > 3 || $recursive < 0;
printLog(($recursive != 3? "Cleaning": "Removing") . " dir $dirName "
. ["files only", "recursively files only", "recursively", "completely"]->[$recursive])
if $DEBUG_ENABLED;
local $DEBUG_ENABLED = 0;
my @subdirs = ();
my $fileNames = listFileNames($dirName);
# process files
foreach (@$fileNames) {
next if $NEVER_REMOVE_FILES{$_};
my $fileName = "$dirName/$_";
if (-d $fileName) { push @subdirs, $fileName; }
else { unlink("$fileName") || return callErrorHandler("Can't unlink $fileName"); }
}
# process subdirs
map {
cleanDir($_, $recursive);
rmdir($_) || return callErrorHandler("Can't unlink $_") if $recursive == 2;
} @subdirs if $recursive;
rmdir($dirName) || return callErrorHandler("Can't unlink $dirName") if $recursive == 3;
return 1;
}
# ----------------------------------------------------------------------------
=head2 removeDir
=over 4
=item description
Entirely removes given directory and its content (if any).
This is an alias to C<cleanDir(3)>.
=item usage
removeDir($TMP_DIR);
=item parameters
* directory to clean
=item returns
C<1> on success, otherwise either dies or warns and returns undef as configured.
=back
=cut
# ============================================================================
sub removeDir ($) {
my $dirName = shift;
return cleanDir($dirName, 3);
}
# ----------------------------------------------------------------------------
=head2 copyDir
=over 4
=item description
Recursively copies all files and subdirectories inside given directory
to new location.
Destination directory must not exist. Use: C<trap { removeDir($dest); };>
to remove it before copying.
=item usage
copyDir($dirFrom, $dirTo);
=item parameters
* source directory to copy
* destination directory to copy to (may not exist)
* optional creating dir permissions (default is $DEFAULT_DIR_PERM)
=item returns
C<1> on success, otherwise either dies or warns and returns undef as configured.
=back
=cut
# ============================================================================
sub copyDir ($$) {
my ($srcDirName, $dstDirName, $perm) = @_;
return callErrorHandler("Directory $srcDirName does not exist")
unless -d $srcDirName;
makeDir($dstDirName, $perm) unless -d $dstDirName;
printLog("Copying dir $srcDirName to $dstDirName recursively")
if $DEBUG_ENABLED;;
local $DEBUG_ENABLED = 0;
my $error = 0;
my @subdirs = ();
my $fileNames = listFileNames($srcDirName);
# process files
foreach (@$fileNames) {
next if $NEVER_COPY_FILES{$_};
my $srcFileName = "$srcDirName/$_";
my $dstFileName = "$dstDirName/$_";
if (-d $srcFileName) { push @subdirs, $_; }
elsif (-l $srcFileName) { next if "# We ignore links for now! TO FIX!" }
else { copyFile($srcFileName, $dstFileName) or $error = 1; }
}
# process subdirs
foreach (@subdirs) {
my $srcSubDirName = "$srcDirName/$_";
my $dstSubDirName = "$dstDirName/$_";
©Dir($srcSubDirName, $dstSubDirName) or $error = 1;
}
return callErrorHandler("Errors while copying some files/subdirs in $srcDirName to $dstDirName")
if $error;
return 1;
}
# ----------------------------------------------------------------------------
=head2 moveDir
=over 4
=item description
Moves (or actually renames) a directory to another location.
Destination directory must not exist. Use: C<trap { removeDir($dest); };>
to remove it before copying.
=item usage
moveDir($dirFrom, $dirTo);
=item parameters
* source directory to move from
* destination directory to move to (must not exist)
=item returns
C<1> on success, otherwise either dies or warns and returns undef as configured.
=back
=cut
# ============================================================================
sub moveDir ($$) {
my ($srcDirName, $dstDirName) = @_;
printLog("Moving dir $srcDirName to $dstDirName")
if $DEBUG_ENABLED;
rename($srcDirName, $dstDirName)
or return callErrorHandler("Can't rename $srcDirName $dstDirName");
return 1;
}
# ----------------------------------------------------------------------------
=head2 listFileNames
=over 4
=item description
Returns the file names in the given directory including all types of files
(regular, directory, link, other), not including '.' and '..' entries.
=item usage
# mini file lister
$dir = '/home/ftp';
foreach my $file (@{listFileNames($dir)}) {
print "File $file\n" if -f "$dir/$file";
print "Dir $file\n" if -d "$dir/$file";
}
=item parameters
* directory to list (or array ref of directories)
* optional flag, 1 means work recursively, the default is 0
=item returns
Array ref of scalars (file names) on success.
Otherwise either dies or warns and returns undef as configured.
=back
=cut
# ============================================================================
sub listFileNames ($;$) {
my $dirName = shift;
my $recursive = shift || 0;
if (ref($dirName) eq "ARRAY") {
my @files = ();
foreach (@$dirName) { push @files, &listFileNames($_); }
return \@files;
}
printLog("Listing dir $dirName") if $DEBUG_ENABLED;
opendir(DIR, $dirName) || return callErrorHandler("Can't opendir $dirName");
my @files = grep { $_ ne '.' && $_ ne '..' } readdir(DIR);
closedir(DIR) || return callErrorHandler("Can't closedir $dirName");
if ($recursive) {
my $i = 0;
for (; $i < @files; ) {
my $subdir = $files[$i];
if (-d "$dirName/$subdir") {
splice(@files, $i, 1, map { "$subdir/$_" }
@{&listFileNames("$dirName/$subdir")});
} else {
$i++;
}
}
}
return \@files;
}
# ----------------------------------------------------------------------------
=head2 findFile
=over 4
=item description
Searches for the given file in the given directories.
Returns the fully qualified file name.
=item usage
my $gtkrc = findFile(".gtkrc", [$home, "$home/.gnome"]);
=item parameters
* file name to search for
* array ref of directories to search in
=item returns
File name with full path if found, or undef if not found.
=back
=cut
# ============================================================================
sub findFile ($$) {
my $fileName = shift;
my $dirs = shift();
die "findFile: no dirs given\n" unless ref($dirs) eq "ARRAY";
foreach (@$dirs) {
my $filePath = "$_/$fileName";
return $filePath if -f $filePath;
}
return undef;
}
# ----------------------------------------------------------------------------
=head2 findExecutable
=over 4
=item description
Searches for the given executable file in the directories that are in the
environmebt variable $PATH or in the additional parameter.
Returns the fully qualified file name.
=item usage
my $gzipExe = findExecutable("gzip", ["/usr/gnu/bin", "/gnu/bin"]);
=item parameters
* file name to search for (only executables are tested)
* optional array ref of directories to search in
=item returns
File name with full path if found, or undef if not found.
=back
=cut
# ============================================================================
sub findExecutable ($;$) {
my $fileName = shift;
my $addDirs = shift;
my @dirs = split(":", $ENV{"PATH"} || "");
if (ref($addDirs) eq "ARRAY") {
push @dirs, @$addDirs;
}
foreach (@dirs) {
my $filePath = "$_/$fileName";
return $filePath if -x $filePath;
}
return undef;
}
# ----------------------------------------------------------------------------
=head2 defaultDirPerm
=over 4
=item description
This functions changes default directory permissions, used in
C<makeDir>, C<makePath>, C<copyDir> and C<moveDir> functions.
The default of this package is 0775.
If no parameters specified, the current value is returned.
=item usage
defaultDirPerm(0700);
=item parameters
* optional default directory permission (integer)
=item returns
Previous value.
=back
=cut
# ============================================================================
sub defaultDirPerm (;$) {
return if $^O =~ /Win|DOS/;
my $newValue = shift;
my $oldValue = $DEFAULT_DIR_PERM;
if (defined $newValue) {
printLog("defaultDirPerm = $newValue") if $DEBUG_ENABLED;
$DEFAULT_DIR_PERM = $newValue;
}
return $oldValue;
}
# ----------------------------------------------------------------------------
=head2 preserveStat
=over 4
=item description
This functions changes behavior of C<copyFile> and C<copyDir> functions.
If 0 is given as a parameter stats will not be preserved.
TODO: specify values for diferent preserves:
0 nothing
1 mode file mode (type and permissions)
2 uid numeric user ID of file's owner
4 gid numeric group ID of file's owner
8 atime last access time since the epoch
16 mtime last modify time since the epoch
32 ctime inode change time (NOT creation time!) since the epo
The default of this package is 0.
If no parameters specified, nothing is set (only current value is returned).
=item usage
preserveStat(1);
=item parameters
* optional flag (currently 0 or 1)
=item returns
Previous value.
=back
=cut
# ============================================================================
sub preserveStat (;$) {
return if $^O =~ /Win|DOS/;
my $newValue = shift;
my $oldValue = $PRESERVED_STAT;
if (defined $newValue) {