-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileOp.pm
More file actions
2777 lines (2087 loc) · 84.4 KB
/
Copy pathFileOp.pm
File metadata and controls
2777 lines (2087 loc) · 84.4 KB
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
package Win32::FileOp;
use vars qw($VERSION);
$VERSION = '0.14.1';
use Win32::API;
use File::Find;
use File::Path;
use File::DosGlob qw(glob);
use Cwd;
use strict ;# 'vars';
use Carp;
#http://Jenda.Krynicky.cz/
use Data::Lazy;
use Win32::AbsPath qw(Relative2Absolute RelativeToAbsolute);
require Exporter;
@Win32::FileOp::ISA = qw(Exporter);
$Win32::FileOp::BufferSize = 65534;
my @FOF_flags = qw(
FOF_SILENT FOF_RENAMEONCOLLISION FOF_NOCONFIRMATION FOF_ALLOWUNDO
FOF_FILESONLY FOF_SIMPLEPROGRESS FOF_NOCONFIRMMKDIR FOF_NOERRORUI
FOF_NOCOPYSECURITYATTRIBS FOF_MULTIDESTFILES FOF_CREATEPROGRESSDLG
);
my @OFN_flags = qw(
OFN_READONLY OFN_OVERWRITEPROMPT OFN_HIDEREADONLY OFN_NOCHANGEDIR OFN_SHOWHELP
OFN_ENABLEHOOK OFN_ENABLETEMPLATE OFN_ENABLETEMPLATEHANDLE OFN_NOVALIDATE
OFN_ALLOWMULTISELECT OFN_EXTENSIONDIFFERENT OFN_PATHMUSTEXIST OFN_FILEMUSTEXIST
OFN_CREATEPROMPT OFN_SHAREAWARE OFN_NOREADONLYRETURN OFN_NOTESTFILECREATE
OFN_NONETWORKBUTTON OFN_NOLONGNAMES OFN_EXPLORER OFN_NODEREFERENCELINKS
OFN_LONGNAMES OFN_SHAREFALLTHROUGH OFN_SHARENOWARN OFN_SHAREWARN
);
my @BIF_flags = qw(
BIF_RETURNONLYFSDIRS BIF_DONTGOBELOWDOMAIN BIF_STATUSTEXT BIF_RETURNFSANCESTORS
BIF_BROWSEFORCOMPUTER BIF_BROWSEFORPRINTER BIF_BROWSEINCLUDEFILES
);
my @CSIDL_flags = qw(
CSIDL_DESKTOP CSIDL_PROGRAMS CSIDL_CONTROLS CSIDL_PRINTERS CSIDL_PERSONAL
CSIDL_FAVORITES CSIDL_STARTUP CSIDL_RECENT CSIDL_SENDTO CSIDL_BITBUCKET
CSIDL_STARTMENU CSIDL_DESKTOPDIRECTORY CSIDL_DRIVES CSIDL_NETWORK CSIDL_NETHOOD
CSIDL_FONTS CSIDL_TEMPLATES CSIDL_COMMON_STARTMENU CSIDL_COMMON_PROGRAMS
CSIDL_COMMON_STARTUP CSIDL_COMMON_DESKTOPDIRECTORY CSIDL_APPDATA CSIDL_PRINTHOOD
);
my @CONNECT_flags = qw(
CONNECT_UPDATE_PROFILE CONNECT_UPDATE_RECENT CONNECT_TEMPORARY CONNECT_INTERACTIVE
CONNECT_PROMPT CONNECT_NEED_DRIVE CONNECT_REFCOUNT CONNECT_REDIRECT CONNECT_LOCALDRIVE
CONNECT_CURRENT_MEDIA CONNECT_DEFERRED CONNECT_RESERVED
);
my @SW_flags = qw(
SW_HIDE SW_MAXIMIZE SW_MINIMIZE SW_RESTORE SW_SHOW
SW_SHOWDEFAULT SW_SHOWMAXIMIZED SW_SHOWMINIMIZED
SW_SHOWMINNOACTIVE SW_SHOWNA SW_SHOWNOACTIVATE SW_SHOWNORMAL
);
@Win32::FileOp::EXPORT = (
qw( Recycle RecycleConfirm RecycleConfirmEach RecycleEx
Delete DeleteConfirm DeleteConfirmEach DeleteEx
Copy CopyConfirm CopyConfirmEach CopyEx
Move MoveConfirm MoveConfirmEach MoveEx
MoveAtReboot DeleteAtReboot MoveFile MoveFileEx CopyFile
FillInDir UpdateDir
FindInPATH FindInPath Relative2Absolute RelativeToAbsolute
AddToRecentDocs EmptyRecentDocs
ReadINISectionKeys ReadINISections
WriteToINI WriteToWININI ReadINI ReadWININI DeleteFromINI DeleteFromWININI
OpenDialog SaveAsDialog BrowseForFolder
recycle
DesktopHandle GetDesktopHandle WindowHandle GetWindowHandle
Compress Uncompress UnCompress Compressed SetCompression GetCompression CompressedSize CompressDir UncompressDir UnCompressDir
Map Connect Unmap Disconnect Mapped
Subst Unsubst Substed SubstDev
GetLargeFileSize GetDiskFreeSpace ShellExecute
),
@FOF_flags,
@OFN_flags,
@BIF_flags,
@CSIDL_flags,
@SW_flags
);
# FOF_CONFIRMMOUSE FOF_WANTMAPPINGHANDLE
*Win32::FileOp::EXPORT_OK = [@Win32::FileOp::EXPORT, @CONNECT_flags];
%Win32::FileOp::EXPORT_TAGS = (
INI => [qw( ReadINISectionKeys ReadINISections WriteToINI WriteToWININI ReadINI ReadWININI DeleteFromINI DeleteFromWININI )],
DIALOGS => [qw( OpenDialog SaveAsDialog BrowseForFolder),
@OFN_flags, @BIF_flags, @CSIDL_flags],
_DIALOGS => [@OFN_flags, @BIF_flags, @CSIDL_flags],
HANDLES => [qw( DesktopHandle GetDesktopHandle WindowHandle GetWindowHandle )],
BASIC => [qw(
Delete DeleteConfirm DeleteConfirmEach DeleteEx
Copy CopyConfirm CopyConfirmEach CopyEx
Move MoveConfirm MoveConfirmEach MoveEx
MoveAtReboot DeleteAtReboot MoveFile MoveFileEx CopyFile
),
@FOF_flags],
_BASIC => [@FOF_flags],
RECENT => [qw(AddToRecentDocs EmptyRecentDocs)],
DIRECTORY => [qw(UpdateDir FillInDir)],
COMPRESS => [qw(Compress Uncompress UnCompress Compressed SetCompression GetCompression CompressedSize CompressDir UncompressDir UnCompressDir)],
MAP => [qw(Map Connect Unmap Disconnect Mapped)],
_MAP => \@CONNECT_flags,
SUBST => [qw(Subst Unsubst Substed SubstDev)],
EXECUTE => ['ShellExecute', @SW_flags],
_EXECUTE => \@SW_flags,
);
use vars qw($ReadOnly $DesktopHandle $fileop $ProgressTitle);
$Win32::FileOp::DesktopHandle = 0;
$Win32::FileOp::WindowHandle = 0;
sub Win32::FileOp::GetDesktopHandle;
sub Win32::FileOp::GetWindowHandle;
$Win32::FileOp::fileop = 0;
$Win32::FileOp::ProgressTitle = '';
sub FO_MOVE () { 0x01 }
sub FO_COPY () { 0x02 }
sub FO_DELETE () { 0x03 }
sub FO_RENAME () { 0x04 }
sub FOF_CREATEPROGRESSDLG () { 0x0000 } # default
sub FOF_MULTIDESTFILES () { 0x0001 } # more than one dest for files
#sub FOF_CONFIRMMOUSE () { 0x0002 } # not implemented
sub FOF_SILENT () { 0x0004 } # don't create progress/report
sub FOF_RENAMEONCOLLISION () { 0x0008 } # rename if coliding
sub FOF_NOCONFIRMATION () { 0x0010 } # Don't prompt the user.
#sub FOF_WANTMAPPINGHANDLE () { 0x0020 } # Fill in FILEOPSTRUCT.hNameMappings
sub FOF_ALLOWUNDO () { 0x0040 } # recycle bin instead of delete
sub FOF_FILESONLY () { 0x0080 } # on *.*, do only files
sub FOF_SIMPLEPROGRESS () { 0x0100 } # means don't show names of files
sub FOF_NOCONFIRMMKDIR () { 0x0200 } # don't confirm making needed dirs
sub FOF_NOERRORUI () { 0x0400 } # don't put up error UI
sub FOF_NOCOPYSECURITYATTRIBS () { 0x0800 } # dont copy file Security Attributes
sub MOVEFILE_REPLACE_EXISTING () { 0x00000001 }
sub MOVEFILE_COPY_ALLOWED () { 0x00000002 }
sub MOVEFILE_DELAY_UNTIL_REBOOT () { 0x00000004 }
sub OFN_READONLY () { 0x00000001}
sub OFN_OVERWRITEPROMPT () { 0x00000002}
sub OFN_HIDEREADONLY () { 0x00000004}
sub OFN_NOCHANGEDIR () { 0x00000008}
sub OFN_SHOWHELP () { 0x00000010}
sub OFN_ENABLEHOOK () { #0x00000020;
carp "OFN_ENABLEHOOK not implemented" }
sub OFN_ENABLETEMPLATE () { #0x00000040;
carp "OFN_ENABLEHOOK not implemented" }
sub OFN_ENABLETEMPLATEHANDLE () { #0x00000080;
carp "OFN_ENABLEHOOK not implemented" }
sub OFN_NOVALIDATE () { 0x00000100}
sub OFN_ALLOWMULTISELECT () { 0x00000200}
sub OFN_EXTENSIONDIFFERENT () { 0x00000400}
sub OFN_PATHMUSTEXIST () { 0x00000800}
sub OFN_FILEMUSTEXIST () { 0x00001000}
sub OFN_CREATEPROMPT () { 0x00002000}
sub OFN_SHAREAWARE () { 0x00004000}
sub OFN_NOREADONLYRETURN () { 0x00008000}
sub OFN_NOTESTFILECREATE () { 0x00010000}
sub OFN_NONETWORKBUTTON () { 0x00020000}
sub OFN_NOLONGNAMES () { 0x00040000} # // force no long names for 4.x modules
#if(WINVER >() { 0x0400)
sub OFN_EXPLORER () { 0x00080000} # // new look commdlg
sub OFN_NODEREFERENCELINKS () { 0x00100000}
sub OFN_LONGNAMES () { 0x00200000} # // force long names for 3.x modules
sub OFN_SHAREFALLTHROUGH () { 2}
sub OFN_SHARENOWARN () { 1}
sub OFN_SHAREWARN () { 0}
sub BIF_RETURNONLYFSDIRS () { 0x0001 } #// For finding a folder to start document searching
sub BIF_DONTGOBELOWDOMAIN () { 0x0002 } #// For starting the Find Computer
sub BIF_STATUSTEXT () { 0x0004 } # Includes a status area in the dialog box.
# The callback function can set the status text
# by sending messages to the dialog box.
sub BIF_RETURNFSANCESTORS () { 0x0008 }
sub BIF_BROWSEFORCOMPUTER () { 0x1000 } #// Browsing for Computers.
sub BIF_BROWSEFORPRINTER () { 0x2000 } #// Browsing for Printers
sub BIF_BROWSEINCLUDEFILES () { 0x4000 } #// Browsing for Everything
#BIF_BROWSEFORCOMPUTER Only returns computers. If the user selects
#anything other than a computer, the OK button is grayed.
#BIF_BROWSEFORPRINTER Only returns printers. If the user selects
#anything other than a printer, the OK button is grayed.
#BIF_DONTGOBELOWDOMAIN Does not include network folders below the
#domain level in the tree view control.
#BIF_RETURNFSANCESTORS Only returns file system ancestors. If the user
#selects anything other than a file system ancestor, the OK button is
#grayed.
#BIF_RETURNONLYFSDIRS Only returns file system directories. If the
#user selects folders that are not part of the file system, the OK button
#is grayed.
#BIF_STATUSTEXT Includes a status area in the dialog box. The callback
#function can set the status text by sending messages to the dialog box.
sub CSIDL_DESKTOP () { 0x0000 }
sub CSIDL_PROGRAMS () { 0x0002 }
sub CSIDL_CONTROLS () { 0x0003 }
sub CSIDL_PRINTERS () { 0x0004 }
sub CSIDL_PERSONAL () { 0x0005 }
sub CSIDL_FAVORITES () { 0x0006 }
sub CSIDL_STARTUP () { 0x0007 }
sub CSIDL_RECENT () { 0x0008 }
sub CSIDL_SENDTO () { 0x0009 }
sub CSIDL_BITBUCKET () { 0x000a }
sub CSIDL_STARTMENU () { 0x000b }
sub CSIDL_DESKTOPDIRECTORY () { 0x0010 }
sub CSIDL_DRIVES () { 0x0011 }
sub CSIDL_NETWORK () { 0x0012 }
sub CSIDL_NETHOOD () { 0x0013 }
sub CSIDL_FONTS () { 0x0014 }
sub CSIDL_TEMPLATES () { 0x0015 }
sub CSIDL_COMMON_STARTMENU () { 0x0016 }
sub CSIDL_COMMON_PROGRAMS () { 0x0017 }
sub CSIDL_COMMON_STARTUP () { 0x0018 }
sub CSIDL_COMMON_DESKTOPDIRECTORY () { 0x0019 }
sub CSIDL_APPDATA () { 0x001a }
sub CSIDL_PRINTHOOD () { 0x001b }
#=rem
#sub FILE_SHARE_READ () { 0x00000001 }
#sub FILE_SHARE_WRITE () { 0x00000002 }
#sub FILE_SHARE_DELETE () { 0x00000004 }
#
#sub FILE_FLAG_WRITE_THROUGH () { 0x80000000 }
#sub FILE_FLAG_OVERLAPPED () { 0x40000000 }
#sub FILE_FLAG_NO_BUFFERING () { 0x20000000 }
#sub FILE_FLAG_RANDOM_ACCESS () { 0x10000000 }
#sub FILE_FLAG_SEQUENTIAL_SCAN () { 0x08000000 }
#sub FILE_FLAG_DELETE_ON_CLOSE () { 0x04000000 }
#sub FILE_FLAG_BACKUP_SEMANTICS () { 0x02000000 }
#sub FILE_FLAG_POSIX_SEMANTICS () { 0x01000000 }
#
#
#sub CREATE_NEW () { 1 }
#sub CREATE_ALWAYS () { 2 }
#sub OPEN_EXISTING () { 3 }
#sub OPEN_ALWAYS () { 4 }
#sub TRUNCATE_EXISTING () { 5 }
#=cut
sub DDD_RAW_TARGET_PATH () { 0x00000001 }
sub DDD_REMOVE_DEFINITION () { 0x00000002 }
sub DDD_EXACT_MATCH_ON_REMOVE () { 0x00000004 }
sub DDD_NO_BROADCAST_SYSTEM () { 0x00000008 }
sub CONNECT_UPDATE_PROFILE () {0x00000001}
sub CONNECT_UPDATE_RECENT () {0x00000002}
sub CONNECT_TEMPORARY () {0x00000004}
sub CONNECT_INTERACTIVE () {0x00000008}
sub CONNECT_PROMPT () {0x00000010}
sub CONNECT_NEED_DRIVE () {0x00000020}
sub CONNECT_REFCOUNT () {0x00000040}
sub CONNECT_REDIRECT () {0x00000080}
sub CONNECT_LOCALDRIVE () {0x00000100}
sub CONNECT_CURRENT_MEDIA () {0x00000200}
sub CONNECT_DEFERRED () {0x00000400}
sub CONNECT_RESERVED () {0xFF000000}
sub SW_HIDE () { 0 }
sub SW_SHOWNORMAL () { 1 }
sub SW_NORMAL () { 1 }
sub SW_SHOWMINIMIZED () { 2 }
sub SW_SHOWMAXIMIZED () { 3 }
sub SW_MAXIMIZE () { 3 }
sub SW_SHOWNOACTIVATE () { 4 }
sub SW_SHOW () { 5 }
sub SW_MINIMIZE () { 6 }
sub SW_SHOWMINNOACTIVE () { 7 }
sub SW_SHOWNA () { 8 }
sub SW_RESTORE () { 9 }
sub SW_SHOWDEFAULT () { 10 }
sub SW_FORCEMINIMIZE () { 11 }
sub SW_MAX () { 11 }
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
tie $Win32::FileOp::fileop, 'Data::Lazy', sub {
new Win32::API("shell32", "SHFileOperation", ['P'], 'I')
or
die "new Win32::API::SHFileOperation: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::copyfile, 'Data::Lazy', sub {
new Win32::API("KERNEL32", "CopyFile", [qw(P P I)], 'I')
or
die "new Win32::API::CopyFile: $!\n";
}, &LAZY_READONLY;
tie $Win32::FileOp::movefileexDel, 'Data::Lazy', sub {
new Win32::API("KERNEL32", "MoveFileEx", ['P','L','N'], 'I')
or
die "new Win32::API::MoveFileEx for delete: $!\n";
}, &LAZY_READONLY;
tie $Win32::FileOp::movefileex, 'Data::Lazy', sub {
new Win32::API("KERNEL32", "MoveFileEx", ['P','P','N'], 'I')
or
die "new Win32::API::MoveFileEx: $!\n";
}, &LAZY_READONLY;
tie $Win32::FileOp::SHAddToRecentDocs, 'Data::Lazy', sub {
new Win32::API("shell32", "SHAddToRecentDocs", ['I','P'], 'I')
or
die "new Win32::API::SHAddToRecentDocs: $!\n";
}, &LAZY_READONLY;
tie $Win32::FileOp::writeINI, 'Data::Lazy', sub {
new Win32::API("KERNEL32", "WritePrivateProfileString", [qw(P P P P)], 'I')
or
die "new Win32::API::WritePrivateProfileString: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::writeWININI, 'Data::Lazy', sub {
new Win32::API("KERNEL32", "WriteProfileString", [qw(P P P)], 'I')
or
die "new Win32::API::WriteProfileString: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::deleteINI, 'Data::Lazy', sub {
new Win32::API("KERNEL32", "WritePrivateProfileString", [qw(P P L P)], 'I')
or
die "new Win32::API::WritePrivateProfileString for delete: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::deleteWININI, 'Data::Lazy', sub {
new Win32::API("KERNEL32", "WriteProfileString", [qw(P P L)], 'I')
or
die "new Win32::API::WriteProfileString for delete: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::readINI, 'Data::Lazy', sub {
new Win32::API("KERNEL32", "GetPrivateProfileString", [qw(P P P P N P)], 'N')
or
die "new Win32::API::GetPrivateProfileString: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::readWININI, 'Data::Lazy', sub {
new Win32::API("KERNEL32", "GetProfileString", [qw(P P P P N)], 'N')
or
die "new Win32::API::GetProfileString: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::GetOpenFileName, 'Data::Lazy', sub {
new Win32::API("comdlg32", "GetOpenFileName", ['P'], 'N')
or
die "new Win32::API::GetOpenFileName: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::GetSaveFileName, 'Data::Lazy', sub {
new Win32::API("comdlg32", "GetSaveFileName", ['P'], 'N')
or
die "new Win32::API::GetSaveFileName: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::CommDlgExtendedError, 'Data::Lazy', sub {
new Win32::API("comdlg32", "CommDlgExtendedError", [], 'N')
or
die "new Win32::API::CommDlgExtendedError: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::CreateFile, 'Data::Lazy', sub {
new Win32::API( "kernel32", "CreateFile", [qw(P N N P N N P)], 'N')
or
die "new Win32::API::CreateFile: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::CloseHandle, 'Data::Lazy', sub {
new Win32::API( "kernel32", "CloseHandle", ['N'], 'N')
or
die "new Win32::API::CloseHandle: $!\n"
};
tie $Win32::FileOp::GetFileSize, 'Data::Lazy', sub {
new Win32::API( "kernel32", "GetFileSize", ['N','P'], 'N')
or
die "new Win32::API::GetFileSize: $!\n"
};
tie $Win32::FileOp::GetDiskFreeSpaceEx, 'Data::Lazy', sub {
new Win32::API( "kernel32", "GetDiskFreeSpaceEx", ['P','P','P','P'], 'N')
or
die "new Win32::API::GetDiskFreeSpaceEx: $!\n"
};
tie $Win32::FileOp::DeviceIoControl, 'Data::Lazy', sub {
new Win32::API( "kernel32", "DeviceIoControl", ['N', 'N', 'P', 'N', 'P', 'N', 'P', 'P'], 'N')
or
die "new Win32::API::DeviceIoControl: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::SHBrowseForFolder, 'Data::Lazy', sub {
new Win32::API("shell32", "SHBrowseForFolder", ['P'], 'N')
or
die "new Win32::API::SHBrowseForFolder: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::SHGetPathFromIDList, 'Data::Lazy', sub {
new Win32::API("shell32", "SHGetPathFromIDList", ['N','P'], 'I')
or
die "new Win32::API::SHGetPathFromIDList: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::SHGetSpecialFolderLocation, 'Data::Lazy', sub {
new Win32::API("shell32", "SHGetSpecialFolderLocation", ['N','I','P'], 'I')
or
die "new Win32::API::SHGetSpecialFolderLocation: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::GetFileVersionInfoSize, 'Data::Lazy', sub {
new Win32::API( "version", "GetFileVersionInfoSize", ['P', 'P'], 'N')
or
die "new Win32::API::GetFileVersionInfoSize: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::GetFileVersionInfo, 'Data::Lazy', sub {
new Win32::API( "version", "GetFileVersionInfo", ['P', 'N', 'N', 'P'], 'N')
or
die "new Win32::API::GetFileVersionInfo: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::GetCompressedFileSize, 'Data::Lazy', sub {
new Win32::API("kernel32", "GetCompressedFileSize", ['P','P'], 'L')
or
die "new Win32::API::GetCompressedFileSize: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::VerQueryValue, 'Data::Lazy', sub {
new Win32::API( "version", "VerQueryValue", ['P', 'P', 'P', 'P'], 'N')
or
die "new Win32::API::VerQueryValue: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::WNetAddConnection3, 'Data::Lazy', sub {
new Win32::API("mpr.dll", "WNetAddConnection3", ['L','P','P','P','L'], 'L')
or
die "new Win32::API::WNetAddConnection3: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::WNetGetConnection, 'Data::Lazy', sub {
new Win32::API("mpr.dll", "WNetGetConnection", ['P','P','P'], 'L')
or
die "new Win32::API::WNetGetConnection: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::WNetCancelConnection2, 'Data::Lazy', sub {
new Win32::API("mpr.dll", "WNetCancelConnection2", ['P','L','I'], 'L')
or
die "new Win32::API::WNetCancelConnection2: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::GetLogicalDrives, 'Data::Lazy', sub {
new Win32::API("kernel32.dll", "GetLogicalDrives", [], 'N')
or
die "new Win32::API::GetLogicalDrives: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::QueryDosDevice, 'Data::Lazy', sub {
new Win32::API("kernel32.dll", "QueryDosDevice", ['P','P','L'], 'L')
or
die "new Win32::API::QueryDosDevice: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::DefineDosDevice, 'Data::Lazy', sub {
new Win32::API("kernel32.dll", "DefineDosDevice", ['L','P','P'],'I')
or
die "new Win32::API::DefineDosDevice: $!\n"
}, &LAZY_READONLY;
tie $Win32::FileOp::ShellExecute, 'Data::Lazy', sub {
new Win32::API("shell32", "ShellExecute", ['N','P','P','P','P','N'], 'I')
or
die "new Win32::API::ShellExecute: $!\n"
}, &LAZY_READONLY;
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub ShellExecute {
my ($operation, $file, $params, $dir, $show, $handle) = @_;
if (@_ == 1) { #ShellExecute( $file)
$file = $operation;
$operation = undef;
} elsif (ref $file) { #ShellExecute( $file, {options})
($params, $file, $operation) = ($file, $operation, undef);
}
if (ref $params) {
$params = { map {lc($_) => $params->{$_}} keys %$params}; # lowercase the keys
$show = $params->{show};
$dir = $params->{dir};
$handle = $params->{handle};
$params = $params->{params};
}
if (defined $show) {
$show+=0;
} else {
$show = SW_SHOWDEFAULT;
}
$handle = Win32::FileOp::GetWindowHandle unless defined $handle;
my $result = $Win32::FileOp::ShellExecute->Call( $handle, $operation, $file, $params, $dir, $show);
return $result > 32;
}
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub Recycle {
&DeleteEx (@_, FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_SILENT |
FOF_NOERRORUI);
}
sub RecycleConfirm { &DeleteEx (@_, FOF_ALLOWUNDO); }
sub RecycleEx { my $opt = pop; $opt |= FOF_ALLOWUNDO; &DeleteEx (@_, $opt); }
sub Delete {
&DeleteEx (@_, FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI);
}
sub DeleteConfirm { &DeleteEx (@_, FOF_CREATEPROGRESSDLG); }
sub DeleteEx {
undef $Win32::FileOp::Error;
my $options = pop;
my ($opstruct, $filename);
my @files = map {if (/[*?]/) {glob($_)} elsif (-e $_) {$_} else {()}} @_; # since we change the names, make a copy of the list
return undef unless @files;
# pass all files at once, join them by \0 and end by \0\0
# fix to full paths
Relative2Absolute @files;
$filename = join "\0", @files;
$filename .= "\0\0"; # double term the filename
# pack fileop structure (really more like lLppIilP)
# sizeof args = l4, L4, p4, p4, I4, i4, l4, P4 = 32 bytes
if ($Win32::FileOp::ProgressTitle and $options & FOF_SIMPLEPROGRESS) {
$Win32::FileOp::ProgressTitle .= "\0" unless $Win32::FileOp::ProgressTitle =~ /\0$/;
$opstruct = pack ('LLpLILC2p', Win32::FileOp::GetWindowHandle, FO_DELETE,
$filename, 0, $options, 0, 0,0, $Win32::FileOp::ProgressTitle);
} else {
$opstruct = pack ('LLpLILLL', Win32::FileOp::GetWindowHandle, FO_DELETE,
$filename, 0, $options, 0, 0, 0);
}
# call delete SHFileOperation with structure
unless ($Win32::FileOp::fileop->Call($opstruct)) {
return 1;
} else {
$! = Win32::GetLastError();
return undef;
}
}
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub RecycleConfirmEach { &_DeleteConfirmEach (@_, FOF_ALLOWUNDO); }
sub DeleteConfirmEach { &_DeleteConfirmEach (@_, FOF_CREATEPROGRESSDLG); }
sub _DeleteConfirmEach {
undef $Win32::FileOp::Error;
my $options = pop;
return undef unless @_;
my $res = 0;
my ($filename,$opstruct);
while (defined($filename = shift)) {
Relative2Absolute $filename;
$filename .= "\0\0"; # double term the filename
my $was = -e $filename;
if ($Win32::FileOp::ProgressTitle and $options & FOF_SIMPLEPROGRESS) {
$Win32::FileOp::ProgressTitle .= "\0" unless $Win32::FileOp::ProgressTitle =~ /\0$/;
$opstruct = pack ('LLpLILC2p', Win32::FileOp::GetWindowHandle, FO_DELETE,
$filename, 0, $options, 0, 0,0, $Win32::FileOp::ProgressTitle);
} else {
$opstruct = pack ('LLpLILLL', Win32::FileOp::GetWindowHandle, FO_DELETE,
$filename, 0, $options, 0, 0, 0);
}
# call delete SHFileOperation with structure
unless ($Win32::FileOp::fileop->Call($opstruct)) {
$res++ if ($was and !-e $filename);
} else {
$! = Win32::GetLastError();
}
}
$res;
}
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub Copy {
&_MoveOrCopyEx (@_, FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR |
FOF_SILENT # | FOF_NOERRORUI
, FO_COPY);
}
sub CopyConfirm { &_MoveOrCopyEx (@_, FOF_CREATEPROGRESSDLG, FO_COPY); }
*CopyConfirmEach = \&CopyConfirm;
sub CopyEx { &_MoveOrCopyEx (@_, FO_COPY); }
sub Move {
&_MoveOrCopyEx (@_, FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_SILENT # | FOF_NOERRORUI
, FO_MOVE);
}
sub MoveConfirm { &_MoveOrCopyEx (@_, FOF_CREATEPROGRESSDLG, FO_MOVE); }
*MoveConfirmEach = \&MoveConfirm;
sub MoveEx { &_MoveOrCopyEx (@_, FO_MOVE); }
sub _MoveOrCopyEx {
undef $Win32::FileOp::Error;
my $func = pop;
my $options = pop;
my ($opstruct, $filename, $hash, $res, $from, $to);
if (@_ % 2) { die "Wrong number of arguments to Win32::FileOp::CopyEx!\n" };
my $i = 0;
while (defined ($from = $_[$i++]) and defined ($to = $_[$i++])) {
# fix to full paths
if (UNIVERSAL::isa($from, "ARRAY")) {
my @files = map {
my $s = $_;
Relative2Absolute $s;
$s;
} @$from;
$from = join "\0", @files;
} else {
Relative2Absolute $from;
$from =~ s#/#\\#g;
# if to ends in slash, get filename from from
if ($to =~ m{[\\/]$} and $to !~ /^\w:\\$/) {
my $tmp = $from;
$tmp =~ s#^.*[\\/](.*?)$#$1#;
$to .= $tmp;
}
$to .= '\\' if $to =~ /:$/;
}
$from .= "\0\0"; # double term the filename
my $options = $options;
if (UNIVERSAL::isa($to, "ARRAY")) {
my $strto='';
foreach (@$to) {
$strto .= RelativeToAbsolute($_) . "\0";
}
$to = $strto;
$options |= FOF_MULTIDESTFILES;
} else {
Relative2Absolute($to);
}
$to .= "\0\0"; # double term the filename
$to =~ s#/#\\#g;
if ($Win32::FileOp::ProgressTitle and $options & FOF_SIMPLEPROGRESS) {
$Win32::FileOp::ProgressTitle .= "\0" unless $Win32::FileOp::ProgressTitle =~ /\0$/;
$opstruct = pack ('LLppILC2p', Win32::FileOp::GetWindowHandle, $func,
$from, $to, $options, 0, 0,0, $Win32::FileOp::ProgressTitle);
} else {
$opstruct = pack ('LLppILLL', Win32::FileOp::GetWindowHandle, $func,
$from, $to, $options, 0, 0, 0);
}
unless ($Win32::FileOp::fileop->Call($opstruct)) {
$res++;
} else {
$! = Win32::GetLastError();
return undef;
}
}
$res;
}
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub MoveFile {
MoveFileEx(@_,MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED);
}
sub MoveAtReboot {
if (Win32::IsWinNT) {
MoveFileEx(@_,MOVEFILE_REPLACE_EXISTING | MOVEFILE_DELAY_UNTIL_REBOOT);
} else {
undef $Win32::FileOp::Error;
my @a;
my $i=0;
while ($_[$i]) {
$a[$i+1]= Win32::GetShortPathName $_[$i];
($a[$i]= $_[$i+1]) =~ s#^(.*)([/\\].*?)$#Win32::GetShortPathName($1).$2#e;
$i+=2;
}
Relative2Absolute(@a);
WriteToINI($ENV{WINDIR}.'\\wininit.ini','Rename',@a);
}
}
sub CopyFile {
undef $Win32::FileOp::Error;
my ($from,$to);
while (defined($from = shift) and defined($to = shift)) {
# Relative2Absolute($to,$from);
$to .= "\0";
$from .= "\0";
$Win32::FileOp::copyfile->Call($from,$to, 0);
}
}
sub DeleteAtReboot {
undef $Win32::FileOp::Error;
if (Win32::IsWinNT) {
my $file;
while (defined($file = shift)) {
Relative2Absolute($file);
$Win32::FileOp::movefileexDel->Call($file, 0, MOVEFILE_DELAY_UNTIL_REBOOT);
}
} else {
my @a;
foreach (@_) {
my $tmp=$_;
Relative2Absolute($tmp);
$tmp = Win32::GetShortPathName $tmp;
push @a, 'NUL', $tmp;
}
WriteToINI($ENV{WINDIR}.'\\wininit.ini','Rename',@a);
}
1;
}
sub MoveFileEx {
undef $Win32::FileOp::Error;
my $options = pop;
my ($from,$to);
while (defined($from = shift) and defined($to = shift)) {
Relative2Absolute($to,$from);
$to .= "\0";
$from .= "\0";
$Win32::FileOp::movefileex->Call($from,$to, $options);
}
}
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub UpdateDir {
undef $Win32::FileOp::Error;
local ($Win32::FileOp::from,$Win32::FileOp::to,$Win32::FileOp::callback) = @_;
-d $Win32::FileOp::from or return undef;
-d $Win32::FileOp::to or File::Path::mkpath $Win32::FileOp::to, 0777 or return undef;
Relative2Absolute($Win32::FileOp::to);
my $olddir = cwd;
chdir $Win32::FileOp::from;
find(\&_UpdateDir, '.');
chdir $olddir;
}
sub _UpdateDir {
undef $Win32::FileOp::Error;
my $fullto = "$Win32::FileOp::to\\$File::Find::dir\\$_";
$fullto =~ s#/#\\#g;
$fullto =~ s#\\\.\\#\\#;
if (-d $_) {
return if /^\.\.?$/ or -d $fullto;
mkdir $fullto, 0777;
} else {
my $age = -M($fullto);
if (! -e($fullto) or $age > -M($_)) {
if (! defined $Win32::FileOp::callback or &$Win32::FileOp::callback()) {
CopyFile $_, $fullto;
}
}
}
}
sub FillInDir {
undef $Win32::FileOp::Error;
local ($Win32::FileOp::from,$Win32::FileOp::to,$Win32::FileOp::callback) = @_;
-d $Win32::FileOp::from or return undef;
-d $Win32::FileOp::to or File::Path::mkpath $Win32::FileOp::to, 0777 or return undef;
Relative2Absolute($Win32::FileOp::to);
my $olddir = cwd;
chdir $Win32::FileOp::from;
find(\&_FillInDir, '.');
chdir $olddir;
}
sub _FillInDir {
my $fullto = "$Win32::FileOp::to\\$File::Find::dir\\$_";
$fullto =~ s#/#\\#g;
$fullto =~ s#\\\.\\#\\#;
if (-d $_) {
return if /^\.\.?$/ or -d $fullto;
mkdir $fullto, 0777;
} else {
if (! -e($fullto)) {
if (! defined $Win32::FileOp::callback or &$Win32::FileOp::callback()) {
CopyFile $_, $fullto;
}
}
}
}
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub AddToRecentDocs {
undef $Win32::FileOp::Error;
my $file;
my $res=0;
while (defined($file = shift)) {
next unless -e $file;
Relative2Absolute($file);
$file .= "\0";
$Win32::FileOp::SHAddToRecentDocs->Call(2,$file);
$res++;
}
$res;
}
sub EmptyRecentDocs {
undef $Win32::FileOp::Error;
my $x = 0;
$Win32::FileOp::SHAddToRecentDocs->Call(2,$x);
}
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub WriteToINI {
undef $Win32::FileOp::Error;
my $INI = RelativeToAbsolute(shift());$INI .= "\0";
my $section = shift;$section .= "\0";
my ($name,$value);
while (defined($name = shift) and defined($value = shift)) {
$name .= "\0";$value .= "\0";
$Win32::FileOp::writeINI->Call($section,$name,$value,$INI)
or return undef;
}
1;
}
sub WriteToWININI {
undef $Win32::FileOp::Error;
my $section = shift;$section .= "\0";
my ($name,$value);
while (defined($name = shift) and defined($value = shift)) {
$name .= "\0";$value .= "\0";
$Win32::FileOp::writeWININI->Call($section,$name,$value)
or return undef;
}
1;
}
sub DeleteFromINI {
undef $Win32::FileOp::Error;
my $INI = RelativeToAbsolute(shift());$INI .= "\0";
my $section = shift;$section .= "\0";
my $name;
while (defined($name = shift)) {
$name .= "\0";
$Win32::FileOp::deleteINI->Call($section,$name,0,$INI)
or return undef;
}
1;
}
sub DeleteFromWININI {
undef $Win32::FileOp::Error;
my $section = shift;$section .= "\0";
my $name;
while (defined($name = shift)) {
$name .= "\0";
$Win32::FileOp::deleteWININI->Call($section,$name,0)
or return undef;
}
1;
}
sub ReadINI {
undef $Win32::FileOp::Error;
my $INI = RelativeToAbsolute(shift());$INI .= "\0";
my $section = shift;$section .= "\0";
my $name = shift;$name .= "\0";
my $default = shift;$default .= "\0";
my $value = _ReadINI($section,$name,$default,$INI);
$value =~ s/\0.*$// or return;
return $value;
}
# MTY hack : Michael Yamada <myamada@gj.com>
sub ReadINISectionKeys {
undef $Win32::FileOp::Error;
my $INI = RelativeToAbsolute(shift());$INI='win.ini' unless $INI;$INI .= "\0";
my $section = shift;$section .= "\0";
my $name = 0; # pass null to API
my $default = "\0";
my @values;
@values = split(/\0/,_ReadINI($section,$name,$default,$INI));
@{$_[0]} = @values if (UNIVERSAL::isa($_[0], "ARRAY"));
return wantarray() ? @values : (@values ? \@values : undef);
}
# END MTY Hack
sub ReadINISections {
undef $Win32::FileOp::Error;
my $INI = RelativeToAbsolute(shift());$INI='win.ini' unless $INI;$INI .= "\0";
my $section = 0; # pass null to API
my $name = 0;
my $default = "\0";
my @values;
@values = split(/\0/,_ReadINI($section,$name,$default,$INI));
@{$_[0]} = @values if (UNIVERSAL::isa($_[0], "ARRAY"));
return wantarray() ? @values : (@values ? \@values : undef);
}
sub ReadWININI {
undef $Win32::FileOp::Error;
my $section = shift;$section .= "\0";
my $name = shift;$name .= "\0";
my $default = shift;$default .= "\0";
my $value = "\0" x 2048;
$Win32::FileOp::readWININI->Call($section,$name,$default,$value,256)
or return undef;
$value =~ s/\0.*$// or return;
return $value;
}
sub _ReadINI { # $section, $name, $default, $INI
my $size = 10;#24;
my $value = "\0" x $size; # large buffer to accomodate many keys
my $retsize = $size-2;
while ($size-$retsize <=2) {
$size*=2;$value = "\0" x $size;
$retsize = $Win32::FileOp::readINI->Call($_[0],$_[1],$_[2],$value,$size,$_[3])
or return '';
}
return $value;
}
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub OpenDialog {
OpenOrSaveDialog($Win32::FileOp::GetOpenFileName,@_);
}