-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathcommon2.c
2648 lines (2360 loc) · 83.6 KB
/
common2.c
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
// common2.c
// Copyright : 2024-07-25 Yutaka Sawada
// License : GPL
#ifndef _UNICODE
#define _UNICODE
#endif
#ifndef UNICODE
#define UNICODE
#endif
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600 // Windows Vista or later
#endif
#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <versionhelpers.h>
#include "common2.h"
// グローバル変数
wchar_t recovery_file[MAX_LEN]; // リカバリ・ファイルのパス
wchar_t base_dir[MAX_LEN]; // ソース・ファイルの基準ディレクトリ
wchar_t ini_path[MAX_LEN]; // 検査結果ファイルのパス
int base_len; // ソース・ファイルの基準ディレクトリの長さ
int recovery_limit; // 作成時はリカバリ・ファイルのサイズ制限
int first_num; // 作成時は最初のパリティ・ブロック番号、検査時は初めて見つけた数
int file_num; // ソース・ファイルの数
int entity_num; // 実体のあるファイルの数 (recovery set に含まれるファイル数)
int recovery_num; // リカバリ・ファイルの数
int source_num; // ソース・ブロックの数
int parity_num; // パリティ・ブロックの数
unsigned int block_size; // ブロック・サイズ
unsigned int split_size; // 分割サイズ
__int64 total_file_size; // 合計ファイル・サイズ
int switch_v; // 検査レベル 0=詳細検査, +1=簡易検査, +2=追加検査, 4=順列検査
// +8=子フォルダを無視, +16=検出後に保存
int switch_b; // バックアップを作るか
// 作成時はソース・ブロックの割合と基準サイズ
// 修復時 +1=別名にして残す, +2=ゴミ箱に入れる, +4=修復できなくても再構築
// +16=完全ならPAR2削除
// 可変長サイズの領域にファイル名を記録する
wchar_t *list_buf; // ソース・ファイルのリスト
int list_len; // ファイル・リストの文字数
int list_max; // ファイル・リストの最大文字数
wchar_t *recv_buf; // リカバリ・ファイルのリスト
int recv_len; // ファイル・リストの文字数
wchar_t *recv2_buf; // 有効なパケットを含むリカバリ・ファイルのリスト
int recv2_len; // ファイル・リストの文字数
wchar_t *list2_buf; // 指定された検査対象のファイル名のリスト
int list2_len;
int list2_max;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
unsigned int cp_output; // Console Output Code Page
// 指定された Code Page から UTF-16 に変換する
int cp_to_utf16(char *in, wchar_t *out, unsigned int cp)
{
int err = 0;
unsigned int dwFlags = MB_ERR_INVALID_CHARS; // 不適切な文字列を警告する
if (cp == CP_UTF8)
dwFlags = 0;
if (MultiByteToWideChar(cp, dwFlags, in, -1, out, MAX_LEN) == 0){
err = GetLastError();
if ((dwFlags != 0) && (err == ERROR_INVALID_FLAGS)){
//printf("MultiByteToWideChar, ERROR_INVALID_FLAGS\n");
if (MultiByteToWideChar(cp, 0, in, -1, out, MAX_LEN) != 0)
return 0;
err = GetLastError();
}
out[0] = 0;
return err;
}
return 0;
}
// Windown OS の UTF-16 から指定された Code Page に変換する
int utf16_to_cp(wchar_t *in, char *out, unsigned int cp)
{
unsigned int dwFlags = WC_NO_BEST_FIT_CHARS; // 似た文字への自動変換を行わない
if (cp == CP_UTF8)
dwFlags = 0;
if (WideCharToMultiByte(cp, dwFlags, in, -1, out, MAX_LEN * 3, NULL, NULL) == 0){
if ((dwFlags != 0) && (GetLastError() == ERROR_INVALID_FLAGS)){
//printf("WideCharToMultiByte, ERROR_INVALID_FLAGS\n");
if (WideCharToMultiByte(cp, 0, in, -1, out, MAX_LEN * 3, NULL, NULL) != 0)
return 0;
}
strcpy(out, "cannot encode");
return 1;
}
return 0;
}
// Windows OS の UTF-8 から UTF-16 に変換する
void utf8_to_utf16(char *in, wchar_t *out)
{
// UTF-16 <-> UTF-8 間では文字数以外のエラーは発生しない
if (MultiByteToWideChar(CP_UTF8, 0, in, -1, out, MAX_LEN) == 0)
wcscpy(out, L"too long");
}
// Windown OS の UTF-16 から UTF-8 に変換する
void utf16_to_utf8(wchar_t *in, char *out)
{
// UTF-16 <-> UTF-8 間では文字数以外のエラーは発生しない
WideCharToMultiByte(CP_UTF8, 0, in, -1, out, MAX_LEN * 3, NULL, NULL);
}
// 文字列が UTF-8 かどうかを判定する (0 = maybe UTF-8)
int check_utf8(unsigned char *text)
{
unsigned char c1;
int tail_len = 0; // UTF8-tail が何バイト続くか
while (*text != 0){
c1 = *text;
// 禁止 0xC0~0xC1,0xF5~0xFF
if ((c1 == 0xC0) || (c1 == 0xC1) || (c1 >= 0xF5))
return 1; // 禁止文字
if (tail_len == 0){ // 第1バイトなら
// 1バイト文字
if (c1 <= 0x7F){
tail_len = 0;
// 2バイト文字の第1バイト
} else if ((c1 >= 0xC2) && (c1 <= 0xDF)){
tail_len = 1;
// 3バイト文字の第1バイト
} else if ((c1 >= 0xE0) && (c1 <= 0xEF)){
tail_len = 2;
// 4バイト文字の第1バイト
} else if (c1 >= 0xF0){
tail_len = 3;
} else {
return 2; // 第1バイトとして不適当な文字
}
} else { // 第2バイト以後なら
if ((c1 >= 0x80) && (c1 <= 0xBF)){
tail_len--;
} else {
return 2; // 第2バイト以後として不適当な文字
}
}
text++; // 次の文字へ
}
return 0;
}
// ファイル・パスから、先頭にある "\\?\" を省いて、指定された Code Page に変換する
int path_to_cp(wchar_t *path, char *out, unsigned int cp)
{
unsigned int dwFlags = WC_NO_BEST_FIT_CHARS; // 似た文字への自動変換を行わない
if (cp == CP_UTF8)
dwFlags = 0;
if (wcsncmp(path, L"\\\\?\\", 4) == 0){ // "\\?\" を省く
path += 4;
if (wcsncmp(path, L"UNC\\", 4) == 0){ // "\\?\UNC" を省いて "\" を追加する
path += 3;
*out = '\\';
out += 1;
}
}
if (WideCharToMultiByte(cp, dwFlags, path, -1, out, MAX_LEN * 3, NULL, NULL) == 0){
if ((dwFlags != 0) && (GetLastError() == ERROR_INVALID_FLAGS)){
//printf("WideCharToMultiByte, ERROR_INVALID_FLAGS\n");
if (WideCharToMultiByte(cp, 0, path, -1, out, MAX_LEN * 3, NULL, NULL) != 0)
return 0;
}
strcpy(out, "cannot encode");
return 1;
}
return 0;
}
// UTF-16 のファイル・パスを画面出力用の Code Page を使って表示する
void printf_cp(unsigned char *format, wchar_t *path)
{
unsigned char buf[MAX_LEN * 3];
path_to_cp(path, buf, cp_output);
printf(format, buf);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// ファイルの offset バイト目から size バイトのデータを buf に読み込む
int file_read_data(
HANDLE hFileRead,
__int64 offset,
unsigned char *buf,
unsigned int size)
{
unsigned int rv;
// ファイルの位置を offsetバイト目にする
if (!SetFilePointerEx(hFileRead, *((PLARGE_INTEGER)&offset), NULL, FILE_BEGIN)){
print_win32_err();
return 1;
}
// size バイトを読み込む
if (!ReadFile(hFileRead, buf, size, &rv, NULL)){
print_win32_err();
return 1;
}
if (size != rv)
return 1; // 指定サイズを読み込めなかったらエラーになる
return 0;
}
// ファイルの offset バイト目に size バイトのデータを buf から書き込む
int file_write_data(
HANDLE hFileWrite,
__int64 offset,
unsigned char *buf,
unsigned int size)
{
unsigned int rv;
// ファイルの位置を offsetバイト目にする
if (!SetFilePointerEx(hFileWrite, *((PLARGE_INTEGER)&offset), NULL, FILE_BEGIN)){
print_win32_err();
return 1;
}
// size バイトを書き込む
if (!WriteFile(hFileWrite, buf, size, &rv, NULL)){
print_win32_err();
return 1;
}
return 0;
}
// ファイルの offset バイト目に size バイトの指定値を書き込む
int file_fill_data(
HANDLE hFileWrite,
__int64 offset,
unsigned char value,
unsigned int size)
{
unsigned char buf[IO_SIZE];
unsigned int rv, len;
// ファイルの位置を offsetバイト目にする
if (!SetFilePointerEx(hFileWrite, *((PLARGE_INTEGER)&offset), NULL, FILE_BEGIN)){
print_win32_err();
return 1;
}
// 指定された値で埋める
memset(buf, value, IO_SIZE);
while (size){
len = IO_SIZE;
if (size < IO_SIZE)
len = size;
size -= len;
if (!WriteFile(hFileWrite, buf, len, &rv, NULL)){
print_win32_err();
return 1;
}
}
return 0;
}
// ファイルの指定バイト目から size バイトのデータを別のファイルに書き込む
int file_copy_data(
HANDLE hFileRead,
__int64 offset_read,
HANDLE hFileWrite,
__int64 offset_write,
unsigned int size)
{
unsigned char buf[IO_SIZE];
unsigned int rv, len;
// ファイルの位置を offset_read バイト目にする
if (!SetFilePointerEx(hFileRead, *((PLARGE_INTEGER)&offset_read), NULL, FILE_BEGIN)){
print_win32_err();
return 1;
}
// ファイルの位置を offset_write バイト目にする
if (!SetFilePointerEx(hFileWrite, *((PLARGE_INTEGER)&offset_write), NULL, FILE_BEGIN)){
print_win32_err();
return 1;
}
// コピーする
while (size){
len = IO_SIZE;
if (size < IO_SIZE)
len = size;
size -= len;
if (!ReadFile(hFileRead, buf, len, &rv, NULL)){
print_win32_err();
return 1; // 指定サイズを読み込めなくてもエラーにしない
}
if (rv < len){
memset(buf + rv, 0, len - rv); // 足りなかった分は 0 で埋める
size = 0;
}
if (!WriteFile(hFileWrite, buf, len, &rv, NULL)){
print_win32_err();
return 1;
}
}
return 0;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// 修復中のテンポラリ・ファイルの名前を作る
// 末尾に _par.tmp を追加する (ADD_LEN 以下の文字数)
void get_temp_name(
wchar_t *file_path, // ファイル・パス
wchar_t *temp_path) // テンポラリ・ファイルのパス
{
wcscpy(temp_path, file_path);
wcscat(temp_path, L"_par.tmp"); // 末尾に追加する
}
// 作業用のゼロで埋められたテンポラリ・ファイルを作成する
int create_temp_file(
wchar_t *file_path, // ファイルのパス
__int64 file_size) // ファイルのサイズ
{
HANDLE hFile;
hFile = CreateFile(file_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE){
if (GetLastError() == ERROR_PATH_NOT_FOUND){ // Path not found (3)
make_dir(file_path); // 途中のフォルダが存在しないのなら作成する
hFile = CreateFile(file_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
}
if (hFile == INVALID_HANDLE_VALUE){
print_win32_err();
return 1;
}
}
// 本来のサイズにする
if (!SetFilePointerEx(hFile, *((PLARGE_INTEGER)&file_size), NULL, FILE_BEGIN)){
print_win32_err();
CloseHandle(hFile);
return 1;
}
if (!SetEndOfFile(hFile)){
print_win32_err();
CloseHandle(hFile);
return 1;
}
CloseHandle(hFile);
return 0;
}
#define FILE_WAIT_SEC 200 // ファイルアクセスの待ち時間 ms
#define FILE_WAIT_NUM 5
// 作業用のソース・ファイルを開く
HANDLE handle_temp_file(
wchar_t *file_name, // ソース・ファイル名
wchar_t *file_path) // 作業用、基準ディレクトリが入ってる
{
int rv, retry_time = FILE_WAIT_NUM;
HANDLE hFile;
get_temp_name(file_name, file_path + base_len);
// 作業ファイル内でデータをコピーできるよう、SHARE_READ にする
hFile = CreateFile(file_path, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
while (hFile == INVALID_HANDLE_VALUE){
rv = GetLastError();
if ((rv == 32) || // ERROR_SHARING_VIOLATION
(rv == 33)){ // ERROR_LOCK_VIOLATION
Sleep(FILE_WAIT_SEC); // 少し待ってから再挑戦する
hFile = CreateFile(file_path, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
} else {
break;
}
retry_time--;
if (retry_time == 0)
break;
}
if (hFile == INVALID_HANDLE_VALUE){
print_win32_err();
printf_cp("cannot open file, %s\n", file_path);
}
return hFile;
}
// 上書き用のソース・ファイルを開く
HANDLE handle_write_file(
wchar_t *file_name, // ソース・ファイル名
wchar_t *file_path, // 作業用、基準ディレクトリが入ってる
__int64 file_size) // 本来のファイルサイズ
{
__int64 now_size;
HANDLE hFile;
wcscpy(file_path + base_len, file_name);
// ソースファイル内でデータをコピーできるよう、SHARE_READ にする
// 既存ファイルの内容と属性を維持するため OPEN_ALWAYS にする
hFile = CreateFile(file_path, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE){
print_win32_err();
} else {
// 現在のファイルサイズを取得する
if (GetFileSizeEx(hFile, (PLARGE_INTEGER)&now_size) == 0){
print_win32_err();
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
// 大きすぎる場合は本来のサイズにする(小さい場合は自動的に埋められる)
} else if (now_size > file_size){
if (SetFilePointerEx(hFile, *((PLARGE_INTEGER)&file_size), NULL, FILE_BEGIN) == 0){
print_win32_err();
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
} else if (SetEndOfFile(hFile) == 0){
print_win32_err();
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
}
}
}
if (hFile == INVALID_HANDLE_VALUE)
printf_cp("cannot open file, %s\n", file_path);
return hFile;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// ファイル・パスがファイル・リスト上に既に存在するか調べる
int search_file_path(
wchar_t *list, // ファイル・リスト
int total_len, // ファイル・リストの文字数
wchar_t *search_file) // 検索するファイルのパス
{
int off = 0;
while (off < total_len){
if (_wcsicmp(list + off, search_file) == 0)
return 1;
// 次のファイル名の位置へ
//off += (wcslen(list + off) + 1);
while (list[off] != 0)
off++;
off++;
}
return 0;
}
// ファイル・リストの内容を並び替える
void sort_list(
wchar_t *list, // ファイル・リスト
int total_len) // ファイル・リストの文字数
{
wchar_t *work_buf;
int off1 = 0, off2, off3, work_off = 0;
// 作業バッファーを確保する
work_buf = (wchar_t *)calloc(total_len, 2);
if (work_buf == NULL)
return; // 並べ替え失敗
while (off1 < total_len){
if (list[off1] == 0){
break;
} else if (list[off1] == '*'){ // 既にコピー済なら
//off1 += (wcslen(list + off1) + 1);
while (list[off1] != 0)
off1++;
off1++;
continue;
}
off3 = off1;
//off2 = off3 + wcslen(list + off3) + 1; // 次の項目
off2 = off3;
while (list[off2] != 0)
off2++;
off2++;
while (off2 < total_len){
//printf("%S (%d), %S (%d)\n", list + off3, off3, list + off2, off2);
if (list[off2] != '*'){ // まだなら項目を比較する
//if (wcscmp(list + off3, list + off2) > 0) // QuickPar はこちらの順序
//if (CompareString(LOCALE_USER_DEFAULT, 0, list + off3, -1, list + off2, -1) > 2)
// Windows 7 以降では数値を認識できる SORT_DIGITSASNUMBERS = 0x00000008
if (CompareStringEx(LOCALE_NAME_USER_DEFAULT, 0x00000008, list + off3, -1, list + off2, -1, NULL, NULL, 0) > 2)
off3 = off2;
}
//off2 += (wcslen(list + off2) + 1); // 次の項目
while (list[off2] != 0)
off2++;
off2++;
}
// 順番にコピーしていく
//printf("get %S (%d)\n", list + off3, off3);
wcscpy(work_buf + work_off, list + off3);
work_off += ((int)wcslen(work_buf + work_off) + 1);
list[off3] = '*'; // コピーした印
if (off3 == off1){
//off1 += (wcslen(list + off1) + 1);
while (list[off1] != 0)
off1++;
off1++;
}
}
// 作業バッファーから戻す
memcpy(list, work_buf, total_len * 2);
free(work_buf);
}
// ソース・ファイルのリストに新しいファイル名を追加する
int add_file_path(wchar_t *filename) // 追加するファイル名
{
wchar_t *tmp_p;
int len;
len = (int)wcslen(filename);
if (list_len + len >= list_max){ // 領域が足りなくなるなら拡張する
list_max += ALLOC_LEN;
tmp_p = (wchar_t *)realloc(list_buf, list_max * 2);
if (tmp_p == NULL){
return 1;
} else {
list_buf = tmp_p;
}
}
wcscpy(list_buf + list_len, filename);
list_len += len + 1;
return 0;
}
// ファイル・リストから指定されたファイル・パスを取り除く
// 減らした後のファイル・リストの文字数を返す
int remove_file_path(
wchar_t *list, // ファイル・リスト
int total_len, // ファイル・リストの文字数
int file_off) // 取り除くファイル・パスの位置
{
int off, len;
if (file_off > total_len)
return total_len;
len = (int)wcslen(list + file_off) + 1; // 末尾のNULLを含める
if (len < total_len){
for (off = file_off; off < total_len - len; off++)
list[off] = list[off + len];
for (off = total_len - len; off < total_len; off++)
list[off] = 0;
return total_len - len;
} else {
for (off = 0; off < total_len; off++)
list[off] = 0;
return 0;
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// ファイル・パスからファイル名の位置を戻す
wchar_t * offset_file_name(wchar_t *file_path)
{
int i;
for (i = (int)wcslen(file_path) - 2; i >= 0; i--){
if ((file_path[i] == '\\') || (file_path[i] == '/'))
break;
}
i++;
return file_path + i;
}
// ファイル・パスからファイル名だけ取り出す
void get_file_name(
wchar_t *file_path, // ファイル・パス
wchar_t *file_name) // ファイル名
{
int i, len, find = -1;
len = (int)wcslen(file_path);
for (i = len - 2; i >= 0; i--){
if ((file_path[i] == '\\') || (file_path[i] == '/')){
find = i;
break;
}
}
find++;
for (i = find; i < len + 1; i++){
file_name[i - find] = file_path[i];
}
}
// ファイル・パスからディレクトリだけ取り出す、末尾は「\」か「/」
void get_base_dir(
wchar_t *file_path, // ファイル・パス
wchar_t *base_path) // ディレクトリ
{
int i, len, find = 0;
len = (int)wcslen(file_path);
if (len <= 1){
base_path[0] = 0;
return;
}
// 最初から末尾が「\」か「/」なら
if ((file_path[len - 1] == '\\') || (file_path[len - 1] == '/'))
len--; // それを取り除いてひとつ親のディレクトリを返す
for (i = len - 1; i >= 0; i--){
if (find == 0){
if ((file_path[i] == '\\') || (file_path[i] == '/')){
find = 1;
base_path[i] = file_path[i];
base_path[i + 1] = 0;
}
} else {
base_path[i] = file_path[i];
}
}
if (find == 0)
base_path[0] = 0;
}
// ディレクトリ記号の「\」を「/」に置換する
void unix_directory(wchar_t *path)
{
int i = 0;
while (path[i] != 0){
if (path[i] == '\\')
path[i] = '/';
i++;
}
}
// 絶対パスかどうかを判定する
int is_full_path(wchar_t *path)
{
if ((path[0] == 0) || (path[1] == 0))
return 0; // 2文字以下だと短すぎ
// 通常のドライブ指定「c:\」の形式
if ((path[1] == ':') && ((path[2] == '\\') || (path[2] == '/')))
return 1;
//「\\?\」が付くのは絶対パスだけ
// ネットワークの UNC パスなら「\\<server>\<share>」
if (((path[0] == '\\') || (path[0] == '/')) && ((path[1] == '\\') || (path[1] == '/')))
return 1;
return 0;
}
// ファイルのディレクトリ位置が同じかどうかを調べる
// フォルダには対応してないことに注意!
int compare_directory(wchar_t *path1, wchar_t *path2)
{
int len1, len2;
// パスの長さ
len1 = 0;
while (path1[len1] != 0)
len1++;
// フォルダの位置を調べたい時は末尾の「\」を無視する
// if (accept_folder != 0)
// len1--;
// サブ・ディレクトリの長さにする
while ((len1 > 0) && (path1[len1] != '\\'))
len1--;
// パスの長さ
len2 = 0;
while (path2[len2] != 0)
len2++;
// フォルダの位置を調べたい時は末尾の「\」を無視する
// if (accept_folder != 0)
// len2--;
// サブ・ディレクトリの長さにする
while ((len2 > 0) && (path2[len2] != '\\'))
len2--;
// サブ・ディレクトリの長さを比較する
if (len1 > len2)
return 1; // path1 が path2 より大きい
if (len1 < len2)
return -1; // path1 が path2 より小さい
// ディレクトリの長さが同じなら
return _wcsnicmp(path1, path2, len1);
}
// ワイルドカードを含む文字列をコピーする
// 出力文字数を返す (null文字を含む)、不正なパスなら 0 を返す
int copy_wild(wchar_t *dst, wchar_t *src)
{
wchar_t s;
int len = 0;
while ((src[0] == '\\') || (src[0] == '/')) // 先頭の「\」は無視する
src++;
do {
s = src[0];
if (s == '/'){ // ディレクトリ記号を統一する
s = '\\';
if ((src[1] == '\\') || (src[1] == '/'))
return 0;
} else if (s == '\\'){ // 「\\」はパスに含まれない
if ((src[1] == '\\') || (src[1] == '/'))
return 0;
} else if ((s >= 'A') && (s <= 'Z')){ // 大文字を小文字に変換する
s += 32;
} else if ((s == '*') && (src[1] == '*')){ // 三個以上の「*」を一個にする
while (src[2] == '*')
src++;
} else if ((s == '.') && (src[1] == '.')){ // 「..」はパスに含まれない
return 0;
}
dst[len++] = s;
src++;
} while (s != 0);
return len; // 末尾の null 文字を個数に含むことに注意
}
// ワイルドカード('*', '?')を使ってユニコードのパスを比較する
// 「*」と「?」はディレクトリ記号に一致しない
// 「**」はディレクトリ記号に一致する
int PathMatchWild(
wchar_t *text, // 比較する文字列
wchar_t *wild) // ワイルドカード (小文字にすること)
{
wchar_t next_char, *wild_prev, *wild_dir, *text_dir;
int stop_dir;
next_char = 0;
wild_prev = NULL;
wild_dir = NULL;
text_dir = text;
stop_dir = 0;
while (*text != 0){
//printf("%c, %c \n", *text, *wild);
if (*wild == '*'){
wild++;
if (*wild == '*'){ // 「**」
while (*wild == '*') // 二個より連続した「*」を無視する
wild++;
stop_dir = 0;
wild_dir = wild;
} else { // 「*」
stop_dir = 1;
}
if (*wild == 0){ // 「*」の次が無ければ一致とみなす
if (stop_dir){ // ディレクトリ記号が存在すれば不一致
while (*text != 0){
if (*text == '\\'){
stop_dir = 2;
break;
}
text++;
}
if (stop_dir == 2){
if (wild_dir != NULL){
stop_dir = 0;
wild_prev = wild_dir;
text = text_dir + 1;
wild = wild_dir;
next_char = *wild;
//printf("\n agian last: %c, %c \n", *text, next_char);
continue;
}
//printf("\n last * != include \\ \n");
return 0;
}
}
return 1;
} else {
next_char = *wild; // 「*」の次の文字
wild_prev = wild; // 最後の「*」の位置
//printf("next = %c, stop_dir = %d\n", next_char, stop_dir);
}
}
if ((*text == '\\') && (stop_dir)) // ディレクトリ記号が存在するなら再比較をしない
wild_prev = NULL;
if (next_char != 0){ // 「*」の後なら
if ((next_char == towlower(*text)) || ((*text != '\\') && (next_char == '?'))){
wild++;
next_char = 0;
text_dir = text;
}
if ((*text == '\\') && (stop_dir)) // ディレクトリ記号が存在するならワイルドカードを無効にする
next_char = 0;
text++;
} else if ((*wild == towlower(*text)) || ((*text != '\\') && (*wild == '?'))){ // 同じ文字なら
wild++;
text++;
} else { // 違う文字なら
if (wild_prev != NULL){ // 直前のワイルドカードから比較しなおす
wild = wild_prev;
next_char = *wild;
//printf("\n agian: %c, %c \n", *text, next_char);
} else if ((stop_dir != 0) && (wild_dir != NULL)){
stop_dir = 0;
wild_prev = wild_dir;
text = text_dir + 1;
wild = wild_dir;
next_char = *wild;
//printf("\n agian dir: %c, %c \n", *text, next_char);
} else{
//printf("\n diff: %c, %c \n", *text, *wild);
return 0; // それ以上一致しない
}
}
}
while (*wild == '*') // 残りの「*」を無視する
wild++;
if (*wild == 0) // ワイルドカードも終端なら一致とみなす
return 1;
return 0;
}
/*
int PathMatchWild1(
wchar_t *text, // 比較する文字列
wchar_t *wild) // ワイルドカード (小文字にすること)
{
wchar_t next_char, *wild_prev;
next_char = 0;
wild_prev = NULL;
while (*text != 0){
//printf("%c, %c \n", *text, *wild);
if (*wild == '*'){
wild++;
while (*wild == '*') // 連続した「*」を無視する
wild++;
if (*wild == 0){ // 「*」の次が無ければ一致とみなす
return 1;
} else {
next_char = *wild; // 「*」の次の文字
wild_prev = wild; // 最後の「*」の位置
}
}
if (next_char != 0){ // 「*」の後なら
if ((next_char == towlower(*text)) || (next_char == '?')){
wild++;
next_char = 0;
}
text++;
} else if ((*wild == towlower(*text)) || (*wild == '?')){ // 同じ文字なら
wild++;
text++;
} else { // 違う文字なら
if (wild_prev != NULL){ // 直前のワイルドカードから比較しなおす
wild = wild_prev;
next_char = *wild;
//printf("\n agian: %c, %c \n", *text, next_char);
} else{
return 0; // それ以上一致しない
}
}
}
while (*wild == '*') // 残りの「*」を無視する
wild++;
if (*wild == 0) // ワイルドカードも終端なら一致とみなす
return 1;
return 0;
}
*/
// ファイルのパスを除外リストと比較する
int exclude_path(wchar_t *path)
{
int off = 0, deny = 0;
deny = list2_max;
while (off < list2_len){
if (list2_buf[off++] == '+'){ // allow
if ((deny != 0) && (PathMatchWild(path, list2_buf + off) != 0)){
// printf_cp("allow: \"%s\"", path);
// printf_cp(" : \"%s\"\n", list2_buf + off);
deny = 0;
}
} else { // deny
if (PathMatchWild(path, list2_buf + off) != 0){
// printf_cp("deny : \"%s\"", path);
// printf_cp(" : \"%s\"\n", list2_buf + off);
return 1;
}
}
off += (int)wcslen(list2_buf + off) + 1;
}
// if ((list2_max != 0) && (deny != 0))
// printf_cp("not : \"%s\"\n", path);
return deny;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#define PREFIX_LEN 4 // 「\\?\」の長さ
// 相対パスを絶対パスに変換し、パスの先頭に "\\?\" を追加する
// 戻り値 : 0=エラー, 5~=新しいパスの長さ
int copy_path_prefix(
wchar_t *new_path, // 新しいパス
int max_len, // 新しいパスの最大長さ (末尾の null文字も含む)
wchar_t *src_path, // 元のパス (相対パスでもよい)
wchar_t *dir_path) // 相対パスの場合に基準となるディレクトリ (NULL ならカレント・ディレクトリ)
{
wchar_t tmp_path[MAX_LEN], *src_name, *new_name;
int len;
// 不正なファイル名を拒否する (検索記号は許可する)
if (src_path[0] == 0)
return 0;
if (wcspbrk(src_path, L"\"<>|")) // WinOS ではファイル名に「"<>|」の文字は使えない
return 0;
// 不適切な名前のファイルは、変換時に浄化されることがある
src_name = offset_file_name(src_path);
//wprintf(L"src_name = \"%s\"\n", src_name);
// 相対パスに対して基準となるディレクトリが指定されてるなら
if ((dir_path != NULL) && (is_full_path(src_path) == 0)){
// そのまま基準ディレクトリを連結して絶対パスにする
len = (int)wcslen(dir_path);
if (len + (int)wcslen(src_path) >= max_len)
return 0;
wcscpy(tmp_path, dir_path); // dir_path の末尾は「\」にしておくこと
wcscpy(tmp_path + len, src_path);
if (GetFullPathName(tmp_path, max_len, new_path, &new_name) == 0)
return 0;
} else { // カレント・ディレクトリを使う
// 相対パスから絶対パスに変換する (ディレクトリ記号も「\」に統一される)
if (GetFullPathName(src_path, max_len, new_path, &new_name) == 0)
return 0;
}
// 元のファイル名と比較して、浄化を取り消す
if (new_name == NULL)
new_name = offset_file_name(new_path);
//wprintf(L"new_name = \"%s\"\n", new_name);
if (_wcsicmp(src_name, new_name) != 0){