OSDN Git Service

Fix bugs of corruption on resuming downloading files larger than 4GB.
[ffftp/ffftp.git] / getput.c
1 /*=============================================================================\r
2 *\r
3 *                                                       ダウンロード/アップロード\r
4 *\r
5 ===============================================================================\r
6 / Copyright (C) 1997-2007 Sota. All rights reserved.\r
7 /\r
8 / Redistribution and use in source and binary forms, with or without \r
9 / modification, are permitted provided that the following conditions \r
10 / are met:\r
11 /\r
12 /  1. Redistributions of source code must retain the above copyright \r
13 /     notice, this list of conditions and the following disclaimer.\r
14 /  2. Redistributions in binary form must reproduce the above copyright \r
15 /     notice, this list of conditions and the following disclaimer in the \r
16 /     documentation and/or other materials provided with the distribution.\r
17 /\r
18 / THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR \r
19 / IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES \r
20 / OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. \r
21 / IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, \r
22 / INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, \r
23 / BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF \r
24 / USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON \r
25 / ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
26 / (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF \r
27 / THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
28 /============================================================================*/\r
29 \r
30 /* このソースは一部、WS_FTP Version 93.12.05 のソースを参考にしました。 */\r
31 /* スレッドの作成/終了に関して、樋口殿作成のパッチを組み込みました。 */\r
32 \r
33 /*\r
34         一部、高速化のためのコード追加 by H.Shirouzu at 2002/10/02\r
35 */\r
36 \r
37 #define STRICT\r
38 #include <stdio.h>\r
39 #include <stdlib.h>\r
40 #include <stdarg.h>\r
41 #include <string.h>\r
42 #include <mbstring.h>\r
43 #include <time.h>\r
44 #include <winsock.h>\r
45 #include <windowsx.h>\r
46 #include <commctrl.h>\r
47 #include <process.h>\r
48 \r
49 #include "common.h"\r
50 #include "resource.h"\r
51 \r
52 #define SET_BUFFER_SIZE\r
53 \r
54 /* Add by H.Shirouzu at 2002/10/02 */\r
55 #undef BUFSIZE\r
56 #define BUFSIZE                 (32 * 1024)\r
57 #define SOCKBUF_SIZE    (256 * 1024)\r
58 /* End */\r
59 \r
60 #ifdef DISABLE_TRANSFER_NETWORK_BUFFERS\r
61 #undef BUFSIZE\r
62 #define BUFSIZE                 (64 * 1024)     // RWIN値以下で充分な大きさが望ましいと思われる。\r
63 #undef SET_BUFFER_SIZE\r
64 #endif\r
65 \r
66 #define TIMER_DISPLAY           1               /* 表示更新用タイマのID */\r
67 #define DISPLAY_TIMING          500             /* 表示更新時間 0.5秒 */\r
68 \r
69 #define ERR_MSG_LEN                     1024\r
70 \r
71 \r
72 /* 削除確認ダイアログの情報 */\r
73 typedef struct {\r
74         int Cur;\r
75         TRANSPACKET *Pkt;\r
76 } MIRRORDELETEINFO;\r
77 \r
78 \r
79 /*===== プロトタイプ =====*/\r
80 \r
81 static void DispTransPacket(TRANSPACKET *Pkt);\r
82 static void EraseTransFileList(void);\r
83 static ULONG WINAPI TransferThread(void *Dummy);\r
84 static int MakeNonFullPath(TRANSPACKET *Pkt, char *CurDir, char *Tmp);\r
85 static int DownLoadNonPassive(TRANSPACKET *Pkt, int *CancelCheckWork);\r
86 static int DownLoadPassive(TRANSPACKET *Pkt, int *CancelCheckWork);\r
87 static int DownLoadFile(TRANSPACKET *Pkt, SOCKET dSkt, int CreateMode, int *CancelCheckWork);\r
88 static void DispDownloadFinishMsg(TRANSPACKET *Pkt, int iRetCode);\r
89 static int DispUpDownErrDialog(int ResID, HWND hWnd, char *Fname);\r
90 static BOOL CALLBACK UpDownErrorDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);\r
91 static int SetDownloadResume(TRANSPACKET *Pkt, int ProcMode, LONGLONG Size, int *Mode, int *CancelCheckWork);\r
92 static BOOL CALLBACK NoResumeWndProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam);\r
93 static int DoUpLoad(SOCKET cSkt, TRANSPACKET *Pkt);\r
94 static int UpLoadNonPassive(TRANSPACKET *Pkt);\r
95 static int UpLoadPassive(TRANSPACKET *Pkt);\r
96 static int UpLoadFile(TRANSPACKET *Pkt, SOCKET dSkt);\r
97 // 同時接続対応\r
98 //static int TermCodeConvAndSend(TERMCODECONVINFO *tInfo, SOCKET Skt, char *Data, int Size, int Ascii);\r
99 static int TermCodeConvAndSend(int ThreadCount, TERMCODECONVINFO *tInfo, SOCKET Skt, char *Data, int Size, int Ascii);\r
100 static void DispUploadFinishMsg(TRANSPACKET *Pkt, int iRetCode);\r
101 static int SetUploadResume(TRANSPACKET *Pkt, int ProcMode, LONGLONG Size, int *Mode);\r
102 static LRESULT CALLBACK TransDlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam);\r
103 static void DispTransferStatus(HWND hWnd, int End, TRANSPACKET *Pkt);\r
104 static void DispTransFileInfo(TRANSPACKET *Pkt, char *Title, int SkipButton, int Info);\r
105 static int GetAdrsAndPort(char *Str, char *Adrs, int *Port, int Max);\r
106 static int IsSpecialDevice(char *Fname);\r
107 static int MirrorDelNotify(int Cur, int Notify, TRANSPACKET *Pkt);\r
108 static BOOL CALLBACK MirrorDeleteDialogCallBack(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam);\r
109 static void SetErrorMsg(char *fmt, ...);\r
110 // 同時接続対応\r
111 static char* GetErrMsg();\r
112 \r
113 /*===== ローカルなワーク =====*/\r
114 \r
115 // 同時接続対応\r
116 //static HANDLE hTransferThread;\r
117 static HANDLE hTransferThread[MAX_DATA_CONNECTION];\r
118 static int fTransferThreadExit = FALSE;\r
119 \r
120 static HANDLE hRunMutex;                                /* 転送スレッド実行ミューテックス */\r
121 static HANDLE hListAccMutex;                    /* 転送ファイルアクセス用ミューテックス */\r
122 \r
123 static int TransFiles = 0;                              /* 転送待ちファイル数 */\r
124 static TRANSPACKET *TransPacketBase = NULL;     /* 転送ファイルリスト */\r
125 // 同時接続対応\r
126 static TRANSPACKET *NextTransPacketBase = NULL;\r
127 \r
128 // 同時接続対応\r
129 //static int Canceled;          /* 中止フラグ YES/NO */\r
130 static int Canceled[MAX_DATA_CONNECTION];               /* 中止フラグ YES/NO */\r
131 static int ClearAll;            /* 全て中止フラグ YES/NO */\r
132 \r
133 static int ForceAbort;          /* 転送中止フラグ */\r
134                                                         /* このフラグはスレッドを終了させるときに使う */\r
135 \r
136 // 同時接続対応\r
137 //static LONGLONG AllTransSizeNow;      /* 今回の転送で転送したサイズ */\r
138 //static time_t TimeStart;      /* 転送開始時間 */\r
139 static LONGLONG AllTransSizeNow[MAX_DATA_CONNECTION];   /* 今回の転送で転送したサイズ */\r
140 static time_t TimeStart[MAX_DATA_CONNECTION];   /* 転送開始時間 */\r
141 \r
142 static int KeepDlg = NO;        /* 転送中ダイアログを消さないかどうか (YES/NO) */\r
143 static int MoveToForeground = NO;               /* ウインドウを前面に移動するかどうか (YES/NO) */\r
144 \r
145 static char CurDir[FMAX_PATH+1] = { "" };\r
146 // 同時接続対応\r
147 //static char ErrMsg[ERR_MSG_LEN+7];\r
148 static char ErrMsg[MAX_DATA_CONNECTION+1][ERR_MSG_LEN+7];\r
149 static DWORD ErrMsgThreadId[MAX_DATA_CONNECTION+1];\r
150 static HANDLE hErrMsgMutex;\r
151 \r
152 // 同時接続対応\r
153 static int WaitForMainThread = NO;\r
154 \r
155 /*===== 外部参照 =====*/\r
156 \r
157 /* 設定値 */\r
158 extern int SaveTimeStamp;\r
159 extern int RmEOF;\r
160 // extern int TimeOut;\r
161 extern int FwallType;\r
162 extern int MirUpDelNotify;\r
163 extern int MirDownDelNotify;\r
164 extern int FolderAttr;\r
165 extern int FolderAttrNum;\r
166 \r
167 \r
168 /*----- ファイル転送スレッドを起動する ----------------------------------------\r
169 *\r
170 *       Parameter\r
171 *               なし\r
172 *\r
173 *       Return Value\r
174 *               なし\r
175 *----------------------------------------------------------------------------*/\r
176 \r
177 int MakeTransferThread(void)\r
178 {\r
179         DWORD dwID;\r
180         int i;\r
181 \r
182         hListAccMutex = CreateMutex( NULL, FALSE, NULL );\r
183         hRunMutex = CreateMutex( NULL, TRUE, NULL );\r
184         // 同時接続対応\r
185         hErrMsgMutex = CreateMutex( NULL, FALSE, NULL );\r
186 \r
187         ClearAll = NO;\r
188         ForceAbort = NO;\r
189 \r
190         fTransferThreadExit = FALSE;\r
191         // 同時接続対応\r
192 //      hTransferThread = (HANDLE)_beginthreadex(NULL, 0, TransferThread, 0, 0, &dwID);\r
193 //      if (hTransferThread == NULL)\r
194 //              return(FFFTP_FAIL); /* XXX */\r
195         for(i = 0; i < MAX_DATA_CONNECTION; i++)\r
196         {\r
197                 hTransferThread[i] = (HANDLE)_beginthreadex(NULL, 0, TransferThread, (void*)i, 0, &dwID);\r
198                 if(hTransferThread[i] == NULL)\r
199                         return FFFTP_FAIL;\r
200         }\r
201 \r
202         return(FFFTP_SUCCESS);\r
203 }\r
204 \r
205 \r
206 /*----- ファイル転送スレッドを終了する ----------------------------------------\r
207 *\r
208 *       Parameter\r
209 *               なし\r
210 *\r
211 *       Return Value\r
212 *               なし\r
213 *----------------------------------------------------------------------------*/\r
214 \r
215 void CloseTransferThread(void)\r
216 {\r
217         int i;\r
218         // 同時接続対応\r
219 //      Canceled = YES;\r
220         for(i = 0; i < MAX_DATA_CONNECTION; i++)\r
221                 Canceled[i] = YES;\r
222         ClearAll = YES;\r
223         ForceAbort = YES;\r
224 \r
225         fTransferThreadExit = TRUE;\r
226         // 同時接続対応\r
227 //      while(WaitForSingleObject(hTransferThread, 10) == WAIT_TIMEOUT)\r
228 //      {\r
229 //              BackgrndMessageProc();\r
230 //              Canceled = YES;\r
231 //      }\r
232 //      CloseHandle(hTransferThread);\r
233         for(i = 0; i < MAX_DATA_CONNECTION; i++)\r
234         {\r
235                 while(WaitForSingleObject(hTransferThread[i], 10) == WAIT_TIMEOUT)\r
236                 {\r
237                         BackgrndMessageProc();\r
238                         Canceled[i] = YES;\r
239                 }\r
240                 CloseHandle(hTransferThread[i]);\r
241         }\r
242 \r
243         ReleaseMutex( hRunMutex );\r
244 \r
245         CloseHandle( hListAccMutex );\r
246         CloseHandle( hRunMutex );\r
247         // 同時接続対応\r
248         CloseHandle( hErrMsgMutex );\r
249         return;\r
250 }\r
251 \r
252 \r
253 /*----- 転送するファイル情報をリストに追加する --------------------------------\r
254 *\r
255 *       Parameter\r
256 *               TRANSPACKET *Pkt : 転送ファイル情報\r
257 *               TRANSPACKET **Base : リストの先頭\r
258 *\r
259 *       Return Value\r
260 *               int ステータス\r
261 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
262 *----------------------------------------------------------------------------*/\r
263 \r
264 int AddTmpTransFileList(TRANSPACKET *Pkt, TRANSPACKET **Base)\r
265 {\r
266         TRANSPACKET *Pos;\r
267         TRANSPACKET *Prev;\r
268         int Sts;\r
269 \r
270         Sts = FFFTP_FAIL;\r
271         if((Pos = malloc(sizeof(TRANSPACKET))) != NULL)\r
272         {\r
273                 memcpy(Pos, Pkt, sizeof(TRANSPACKET));\r
274                 Pos->Next = NULL;\r
275 \r
276                 if(*Base == NULL)\r
277                         *Base = Pos;\r
278                 else\r
279                 {\r
280                         Prev = *Base;\r
281                         while(Prev->Next != NULL)\r
282                                 Prev = Prev->Next;\r
283                         Prev->Next = Pos;\r
284                 }\r
285                 Sts = FFFTP_SUCCESS;\r
286         }\r
287         return(Sts);\r
288 }\r
289 \r
290 \r
291 /*----- 転送するファイル情報リストをクリアする --------------------------------\r
292 *\r
293 *       Parameter\r
294 *               TRANSPACKET **Base : リストの先頭\r
295 *\r
296 *       Return Value\r
297 *               なし\r
298 *----------------------------------------------------------------------------*/\r
299 \r
300 void EraseTmpTransFileList(TRANSPACKET **Base)\r
301 {\r
302         TRANSPACKET *Pos;\r
303         TRANSPACKET *Next;\r
304 \r
305         Pos = *Base;\r
306         while(Pos != NULL)\r
307         {\r
308                 Next = Pos->Next;\r
309                 free(Pos);\r
310                 Pos = Next;\r
311         }\r
312         *Base = NULL;\r
313         return;\r
314 }\r
315 \r
316 \r
317 /*----- 転送するファイル情報リストから1つの情報を取り除く --------------------\r
318 *\r
319 *       Parameter\r
320 *               TRANSPACKET *Pkt : 転送ファイル情報\r
321 *               TRANSPACKET **Base : リストの先頭\r
322 *\r
323 *       Return Value\r
324 *               int ステータス\r
325 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
326 *----------------------------------------------------------------------------*/\r
327 \r
328 int RemoveTmpTransFileListItem(TRANSPACKET **Base, int Num)\r
329 {\r
330         TRANSPACKET *Pos;\r
331         TRANSPACKET *Prev;\r
332         int Sts;\r
333 \r
334         Sts = FFFTP_FAIL;\r
335         Pos = *Base;\r
336         if(Num == 0)\r
337         {\r
338                 *Base = Pos->Next;\r
339                 free(Pos);\r
340                 Sts = FFFTP_SUCCESS;\r
341         }\r
342         else\r
343         {\r
344                 while(Pos != NULL)\r
345                 {\r
346                         Prev = Pos;\r
347                         Pos = Pos->Next;\r
348                         if(--Num == 0)\r
349                         {\r
350                                 Prev->Next = Pos->Next;\r
351                                 free(Pos);\r
352                                 Sts = FFFTP_SUCCESS;\r
353                                 break;\r
354                         }\r
355                 }\r
356         }\r
357         return(Sts);\r
358 }\r
359 \r
360 \r
361 /*----- 転送するファイル情報を転送ファイルリストに登録する --------------------\r
362 *\r
363 *       Parameter\r
364 *               TRANSPACKET *Pkt : 転送ファイル情報\r
365 *\r
366 *       Return Value\r
367 *               なし\r
368 *----------------------------------------------------------------------------*/\r
369 \r
370 void AddTransFileList(TRANSPACKET *Pkt)\r
371 {\r
372         DispTransPacket(Pkt);\r
373 \r
374         // 同時接続対応\r
375 //      WaitForSingleObject(hListAccMutex, INFINITE);\r
376         while(WaitForSingleObject(hListAccMutex, 0) == WAIT_TIMEOUT)\r
377         {\r
378                 WaitForMainThread = YES;\r
379                 BackgrndMessageProc();\r
380                 Sleep(1);\r
381         }\r
382 \r
383         if(AddTmpTransFileList(Pkt, &TransPacketBase) == FFFTP_SUCCESS)\r
384         {\r
385                 if((strncmp(Pkt->Cmd, "RETR", 4) == 0) ||\r
386                    (strncmp(Pkt->Cmd, "STOR", 4) == 0))\r
387                 {\r
388                         TransFiles++;\r
389                         PostMessage(GetMainHwnd(), WM_CHANGE_COND, 0, 0);\r
390                 }\r
391         }\r
392         // 同時接続対応\r
393         if(NextTransPacketBase == NULL)\r
394                 NextTransPacketBase = TransPacketBase;\r
395         ReleaseMutex(hListAccMutex);\r
396         // 同時接続対応\r
397         WaitForMainThread = NO;\r
398 \r
399         return;\r
400 }\r
401 \r
402 \r
403 /*----- 転送ファイル情報を転送ファイルリストに追加する ------------------------\r
404 *\r
405 *       Parameter\r
406 *               TRANSPACKET *Pkt : 転送ファイル情報\r
407 *               TRANSPACKET **Base : リストの先頭\r
408 *\r
409 *       Return Value\r
410 *               なし\r
411 *\r
412 *       Note\r
413 *               Pkt自体をリストに連結する\r
414 *----------------------------------------------------------------------------*/\r
415 \r
416 void AppendTransFileList(TRANSPACKET *Pkt)\r
417 {\r
418         TRANSPACKET *Pos;\r
419 \r
420         // 同時接続対応\r
421 //      WaitForSingleObject(hListAccMutex, INFINITE);\r
422         while(WaitForSingleObject(hListAccMutex, 0) == WAIT_TIMEOUT)\r
423         {\r
424                 WaitForMainThread = YES;\r
425                 BackgrndMessageProc();\r
426                 Sleep(1);\r
427         }\r
428 \r
429         if(TransPacketBase == NULL)\r
430                 TransPacketBase = Pkt;\r
431         else\r
432         {\r
433                 Pos = TransPacketBase;\r
434                 while(Pos->Next != NULL)\r
435                         Pos = Pos->Next;\r
436                 Pos->Next = Pkt;\r
437         }\r
438         // 同時接続対応\r
439         if(NextTransPacketBase == NULL)\r
440                 NextTransPacketBase = TransPacketBase;\r
441 \r
442         while(Pkt != NULL)\r
443         {\r
444                 DispTransPacket(Pkt);\r
445 \r
446                 if((strncmp(Pkt->Cmd, "RETR", 4) == 0) ||\r
447                    (strncmp(Pkt->Cmd, "STOR", 4) == 0))\r
448                 {\r
449                         TransFiles++;\r
450                         PostMessage(GetMainHwnd(), WM_CHANGE_COND, 0, 0);\r
451                 }\r
452                 Pkt = Pkt->Next;\r
453         }\r
454 \r
455         ReleaseMutex(hListAccMutex);\r
456         // 同時接続対応\r
457         WaitForMainThread = NO;\r
458         return;\r
459 }\r
460 \r
461 \r
462 /*----- 転送ファイル情報を表示する --------------------------------------------\r
463 *\r
464 *       Parameter\r
465 *               TRANSPACKET *Pkt : 転送ファイル情報\r
466 *\r
467 *       Return Value\r
468 *               なし\r
469 *----------------------------------------------------------------------------*/\r
470 \r
471 static void DispTransPacket(TRANSPACKET *Pkt)\r
472 {\r
473         if((strncmp(Pkt->Cmd, "RETR", 4) == 0) || (strncmp(Pkt->Cmd, "STOR", 4) == 0))\r
474                 DoPrintf("TransList Cmd=%s : %s : %s", Pkt->Cmd, Pkt->RemoteFile, Pkt->LocalFile);\r
475         else if(strncmp(Pkt->Cmd, "R-", 2) == 0)\r
476                 DoPrintf("TransList Cmd=%s : %s", Pkt->Cmd, Pkt->RemoteFile);\r
477         else if(strncmp(Pkt->Cmd, "L-", 2) == 0)\r
478                 DoPrintf("TransList Cmd=%s : %s", Pkt->Cmd, Pkt->LocalFile);\r
479         else if(strncmp(Pkt->Cmd, "MKD", 3) == 0)\r
480         {\r
481                 if(strlen(Pkt->LocalFile) > 0)\r
482                         DoPrintf("TransList Cmd=%s : %s", Pkt->Cmd, Pkt->LocalFile);\r
483                 else\r
484                         DoPrintf("TransList Cmd=%s : %s", Pkt->Cmd, Pkt->RemoteFile);\r
485         }\r
486         else\r
487                 DoPrintf("TransList Cmd=%s", Pkt->Cmd);\r
488         return;\r
489 }\r
490 \r
491 \r
492 /*----- 転送ファイルリストをクリアする ----------------------------------------\r
493 *\r
494 *       Parameter\r
495 *               なし\r
496 *\r
497 *       Return Value\r
498 *               なし\r
499 *----------------------------------------------------------------------------*/\r
500 \r
501 static void EraseTransFileList(void)\r
502 {\r
503         TRANSPACKET *New;\r
504         TRANSPACKET *Next;\r
505         TRANSPACKET *NotDel;\r
506         TRANSPACKET Pkt;\r
507 \r
508         NotDel = NULL;\r
509 \r
510         // 同時接続対応\r
511 //      WaitForSingleObject(hListAccMutex, INFINITE);\r
512         while(WaitForSingleObject(hListAccMutex, 0) == WAIT_TIMEOUT)\r
513         {\r
514                 WaitForMainThread = YES;\r
515                 BackgrndMessageProc();\r
516                 Sleep(1);\r
517         }\r
518         New = TransPacketBase;\r
519         while(New != NULL)\r
520         {\r
521                 /* 最後の"BACKCUR"は必要なので消さない */\r
522                 if(strcmp(New->Cmd, "BACKCUR") == 0)\r
523                 {\r
524                         if(NotDel != NULL)\r
525                                 // 同時接続対応\r
526 //                              free(NotDel);\r
527                                 strcpy(NotDel->Cmd, "");\r
528                         NotDel = New;\r
529                         New = New->Next;\r
530                         // 同時接続対応\r
531 //                      NotDel->Next = NULL;\r
532                 }\r
533                 else\r
534                 {\r
535                         Next = New->Next;\r
536                         // 同時接続対応\r
537 //                      free(New);\r
538                         strcpy(New->Cmd, "");\r
539                         New = Next;\r
540                 }\r
541         }\r
542         TransPacketBase = NotDel;\r
543         // 同時接続対応\r
544         NextTransPacketBase = TransPacketBase;\r
545         TransFiles = 0;\r
546         PostMessage(GetMainHwnd(), WM_CHANGE_COND, 0, 0);\r
547         ReleaseMutex(hListAccMutex);\r
548         // 同時接続対応\r
549         WaitForMainThread = NO;\r
550 \r
551         strcpy(Pkt.Cmd, "GOQUIT");\r
552         AddTransFileList(&Pkt);\r
553         return;\r
554 }\r
555 \r
556 \r
557 /*----- 転送中ダイアログを消さないようにするかどうかを設定 --------------------\r
558 *\r
559 *       Parameter\r
560 *               int Sw : 転送中ダイアログを消さないかどうか (YES/NO)\r
561 *\r
562 *       Return Value\r
563 *               なし\r
564 *----------------------------------------------------------------------------*/\r
565 \r
566 void KeepTransferDialog(int Sw)\r
567 {\r
568         KeepDlg = Sw;\r
569         return;\r
570 }\r
571 \r
572 \r
573 /*----- 現在転送中かどうかを返す ----------------------------------------------\r
574 *\r
575 *       Parameter\r
576 *               なし\r
577 *\r
578 *       Return Value\r
579 *               int ステータス (YES/NO=転送中ではない)\r
580 *----------------------------------------------------------------------------*/\r
581 \r
582 int AskTransferNow(void)\r
583 {\r
584         return(TransPacketBase != NULL ? YES : NO);\r
585 }\r
586 \r
587 \r
588 /*----- 転送するファイルの数を返す --------------------------------------------\r
589 *\r
590 *       Parameter\r
591 *               なし\r
592 *\r
593 *       Return Value\r
594 *               int 転送するファイルの数\r
595 *----------------------------------------------------------------------------*/\r
596 \r
597 int AskTransferFileNum(void)\r
598 {\r
599         return(TransFiles);\r
600 }\r
601 \r
602 \r
603 /*----- 転送中ウインドウを前面に出す ------------------------------------------\r
604 *\r
605 *       Parameter\r
606 *               なし\r
607 *\r
608 *       Return Value\r
609 *               なし\r
610 *----------------------------------------------------------------------------*/\r
611 \r
612 void GoForwardTransWindow(void)\r
613 {\r
614         MoveToForeground = YES;\r
615         return;\r
616 }\r
617 \r
618 \r
619 /*----- 転送ソケットのカレントディレクトリ情報を初期化 ------------------------\r
620 *\r
621 *       Parameter\r
622 *               なし\r
623 *\r
624 *       Return Value\r
625 *               なし\r
626 *----------------------------------------------------------------------------*/\r
627 \r
628 void InitTransCurDir(void)\r
629 {\r
630         strcpy(CurDir, "");\r
631         return;\r
632 }\r
633 \r
634 \r
635 /*----- ファイル転送スレッドのメインループ ------------------------------------\r
636 *\r
637 *       Parameter\r
638 *               void *Dummy : 使わない\r
639 *\r
640 *       Return Value\r
641 *               なし\r
642 *----------------------------------------------------------------------------*/\r
643 \r
644 static ULONG WINAPI TransferThread(void *Dummy)\r
645 {\r
646         TRANSPACKET *Pos;\r
647         HWND hWndTrans;\r
648         char Tmp[FMAX_PATH+1];\r
649         int CwdSts;\r
650         int GoExit;\r
651 //      int Down;\r
652 //      int Up;\r
653         static int Down;\r
654         static int Up;\r
655         int DelNotify;\r
656         int ThreadCount;\r
657         SOCKET CmdSkt;\r
658         SOCKET NewCmdSkt;\r
659         SOCKET TrnSkt;\r
660         RECT WndRect;\r
661         int i;\r
662 \r
663         hWndTrans = NULL;\r
664         Down = NO;\r
665         Up = NO;\r
666         GoExit = NO;\r
667         DelNotify = NO;\r
668         // 同時接続対応\r
669         // ソケットは各転送スレッドが管理\r
670         ThreadCount = (int)Dummy;\r
671         CmdSkt = INVALID_SOCKET;\r
672         NewCmdSkt = INVALID_SOCKET;\r
673         TrnSkt = INVALID_SOCKET;\r
674         SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST);\r
675 \r
676         while((TransPacketBase != NULL) ||\r
677                   (WaitForSingleObject(hRunMutex, 200) == WAIT_TIMEOUT))\r
678         {\r
679                 if(fTransferThreadExit == TRUE)\r
680                         break;\r
681 \r
682                 if(WaitForMainThread == YES)\r
683                 {\r
684                         BackgrndMessageProc();\r
685                         Sleep(100);\r
686                         continue;\r
687                 }\r
688 \r
689 //              WaitForSingleObject(hListAccMutex, INFINITE);\r
690                 while(WaitForSingleObject(hListAccMutex, 0) == WAIT_TIMEOUT)\r
691                 {\r
692                         BackgrndMessageProc();\r
693                         Sleep(1);\r
694                 }\r
695 //              memset(ErrMsg, NUL, ERR_MSG_LEN+7);\r
696                 memset(GetErrMsg(), NUL, ERR_MSG_LEN+7);\r
697 \r
698 //              Canceled = NO;\r
699                 Canceled[ThreadCount] = NO;\r
700 \r
701                 while(TransPacketBase != NULL && strcmp(TransPacketBase->Cmd, "") == 0)\r
702                 {\r
703                         Pos = TransPacketBase;\r
704                         TransPacketBase = TransPacketBase->Next;\r
705                         free(Pos);\r
706                 }\r
707                 NewCmdSkt = AskCmdCtrlSkt();\r
708                 if(TransPacketBase && NewCmdSkt != INVALID_SOCKET && ThreadCount < AskMaxThreadCount())\r
709                 {\r
710                         if(TrnSkt == INVALID_SOCKET || NewCmdSkt != CmdSkt)\r
711                         {\r
712                                 ReleaseMutex(hListAccMutex);\r
713                                 ReConnectTrnSkt(&TrnSkt);\r
714 //                              WaitForSingleObject(hListAccMutex, INFINITE);\r
715                                 while(WaitForSingleObject(hListAccMutex, 0) == WAIT_TIMEOUT)\r
716                                 {\r
717                                         BackgrndMessageProc();\r
718                                         Sleep(1);\r
719                                 }\r
720                         }\r
721                 }\r
722                 else\r
723                 {\r
724                         if(TrnSkt != INVALID_SOCKET)\r
725                         {\r
726                                 ReleaseMutex(hListAccMutex);\r
727                                 DoClose(TrnSkt);\r
728                                 TrnSkt = INVALID_SOCKET;\r
729 //                              WaitForSingleObject(hListAccMutex, INFINITE);\r
730                                 while(WaitForSingleObject(hListAccMutex, 0) == WAIT_TIMEOUT)\r
731                                 {\r
732                                         BackgrndMessageProc();\r
733                                         Sleep(1);\r
734                                 }\r
735                         }\r
736                 }\r
737                 CmdSkt = NewCmdSkt;\r
738 //              if(TransPacketBase != NULL)\r
739                 if(TrnSkt != INVALID_SOCKET && NextTransPacketBase != NULL)\r
740                 {\r
741                         Pos = NextTransPacketBase;\r
742                         NextTransPacketBase = NextTransPacketBase->Next;\r
743                         // ディレクトリ操作は非同期で行わない\r
744 //                      ReleaseMutex(hListAccMutex);\r
745                         if(hWndTrans == NULL)\r
746                         {\r
747 //                              if((strncmp(TransPacketBase->Cmd, "RETR", 4) == 0) ||\r
748 //                                 (strncmp(TransPacketBase->Cmd, "STOR", 4) == 0) ||\r
749 //                                 (strncmp(TransPacketBase->Cmd, "MKD", 3) == 0) ||\r
750 //                                 (strncmp(TransPacketBase->Cmd, "L-", 2) == 0) ||\r
751 //                                 (strncmp(TransPacketBase->Cmd, "R-", 2) == 0))\r
752                                 if((strncmp(Pos->Cmd, "RETR", 4) == 0) ||\r
753                                    (strncmp(Pos->Cmd, "STOR", 4) == 0) ||\r
754                                    (strncmp(Pos->Cmd, "MKD", 3) == 0) ||\r
755                                    (strncmp(Pos->Cmd, "L-", 2) == 0) ||\r
756                                    (strncmp(Pos->Cmd, "R-", 2) == 0))\r
757                                 {\r
758                                         hWndTrans = CreateDialog(GetFtpInst(), MAKEINTRESOURCE(transfer_dlg), HWND_DESKTOP, (DLGPROC)TransDlgProc);\r
759                                         if(MoveToForeground == YES)\r
760                                                 SetForegroundWindow(hWndTrans);\r
761                                         ShowWindow(hWndTrans, SW_SHOWNOACTIVATE);\r
762                                         GetWindowRect(hWndTrans, &WndRect);\r
763                                         SetWindowPos(hWndTrans, NULL, WndRect.left, WndRect.top + (WndRect.bottom - WndRect.top) * ThreadCount - (WndRect.bottom - WndRect.top) * (AskMaxThreadCount() - 1) / 2, 0, 0, SWP_NOSIZE | SWP_NOZORDER);\r
764                                 }\r
765                         }\r
766 //                      TransPacketBase->hWndTrans = hWndTrans;\r
767                         Pos->hWndTrans = hWndTrans;\r
768                         Pos->ctrl_skt = TrnSkt;\r
769                         Pos->ThreadCount = ThreadCount;\r
770 \r
771                         if(hWndTrans != NULL)\r
772                         {\r
773                                 if(MoveToForeground == YES)\r
774                                 {\r
775                                         SetForegroundWindow(hWndTrans);\r
776                                         MoveToForeground = NO;\r
777                                 }\r
778                         }\r
779 \r
780                         if(hWndTrans != NULL)\r
781 //                              SendMessage(hWndTrans, WM_SET_PACKET, 0, (LPARAM)TransPacketBase);\r
782                                 SendMessage(hWndTrans, WM_SET_PACKET, 0, (LPARAM)Pos);\r
783 \r
784                         /* ダウンロード */\r
785 //                      if(strncmp(TransPacketBase->Cmd, "RETR", 4) == 0)\r
786                         if(strncmp(Pos->Cmd, "RETR", 4) == 0)\r
787                         {\r
788                                 // 一部TYPE、STOR(RETR)、PORT(PASV)を並列に処理できないホストがあるため\r
789 //                              ReleaseMutex(hListAccMutex);\r
790                                 /* 不正なパスを検出 */\r
791 //                              if(CheckPathViolation(TransPacketBase) == NO)\r
792                                 if(CheckPathViolation(Pos) == NO)\r
793                                 {\r
794                                         /* フルパスを使わないための処理 */\r
795 //                                      if(MakeNonFullPath(TransPacketBase, CurDir, Tmp) == FFFTP_SUCCESS)\r
796                                         if(MakeNonFullPath(Pos, CurDir, Tmp) == FFFTP_SUCCESS)\r
797                                         {\r
798 //                                              if(strncmp(TransPacketBase->Cmd, "RETR-S", 6) == 0)\r
799                                                 if(strncmp(Pos->Cmd, "RETR-S", 6) == 0)\r
800                                                 {\r
801                                                         /* サイズと日付を取得 */\r
802 //                                                      DoSIZE(TransPacketBase->RemoteFile, &TransPacketBase->Size);\r
803 //                                                      DoMDTM(TransPacketBase->RemoteFile, &TransPacketBase->Time);\r
804 //                                                      strcpy(TransPacketBase->Cmd, "RETR ");\r
805                                                         DoSIZE(TrnSkt, Pos->RemoteFile, &Pos->Size);\r
806                                                         DoMDTM(TrnSkt, Pos->RemoteFile, &Pos->Time);\r
807                                                         strcpy(Pos->Cmd, "RETR ");\r
808                                                 }\r
809 \r
810                                                 Down = YES;\r
811 //                                              if(DoDownLoad(AskTrnCtrlSkt(), TransPacketBase, NO) == 429)\r
812 //                                              {\r
813 //                                                      if(ReConnectTrnSkt() == FFFTP_SUCCESS)\r
814 //                                                              DoDownLoad(AskTrnCtrlSkt(), TransPacketBase, NO, &Canceled);\r
815                                                                 DoDownLoad(TrnSkt, Pos, NO, &Canceled[Pos->ThreadCount]);\r
816 //                                              }\r
817                                         }\r
818                                 }\r
819                                 // 一部TYPE、STOR(RETR)、PORT(PASV)を並列に処理できないホストがあるため\r
820                                 ReleaseMutex(hListAccMutex);\r
821                         }\r
822                         /* アップロード */\r
823 //                      else if(strncmp(TransPacketBase->Cmd, "STOR", 4) == 0)\r
824                         else if(strncmp(Pos->Cmd, "STOR", 4) == 0)\r
825                         {\r
826                                 // 一部TYPE、STOR(RETR)、PORT(PASV)を並列に処理できないホストがあるため\r
827 //                              ReleaseMutex(hListAccMutex);\r
828                                 /* フルパスを使わないための処理 */\r
829 //                              if(MakeNonFullPath(TransPacketBase, CurDir, Tmp) == FFFTP_SUCCESS)\r
830                                 if(MakeNonFullPath(Pos, CurDir, Tmp) == FFFTP_SUCCESS)\r
831                                 {\r
832                                         Up = YES;\r
833 //                                      if(DoUpLoad(AskTrnCtrlSkt(), TransPacketBase) == 429)\r
834 //                                      {\r
835 //                                              if(ReConnectTrnSkt() == FFFTP_SUCCESS)\r
836 //                                                      DoUpLoad(AskTrnCtrlSkt(), TransPacketBase);\r
837                                                         DoUpLoad(TrnSkt, Pos);\r
838 //                                      }\r
839                                 }\r
840                                 // 一部TYPE、STOR(RETR)、PORT(PASV)を並列に処理できないホストがあるため\r
841                                 ReleaseMutex(hListAccMutex);\r
842                         }\r
843                         /* フォルダ作成(ローカルまたはホスト) */\r
844 //                      else if(strncmp(TransPacketBase->Cmd, "MKD", 3) == 0)\r
845                         else if(strncmp(Pos->Cmd, "MKD", 3) == 0)\r
846                         {\r
847 //                              DispTransFileInfo(TransPacketBase, MSGJPN078, FALSE, YES);\r
848                                 DispTransFileInfo(Pos, MSGJPN078, FALSE, YES);\r
849 \r
850 //                              if(strlen(TransPacketBase->RemoteFile) > 0)\r
851                                 if(strlen(Pos->RemoteFile) > 0)\r
852                                 {\r
853                                         /* フルパスを使わないための処理 */\r
854                                         CwdSts = FTP_COMPLETE;\r
855 \r
856 //                                      strcpy(Tmp, TransPacketBase->RemoteFile);\r
857                                         strcpy(Tmp, Pos->RemoteFile);\r
858 //                                      if(ProcForNonFullpath(Tmp, CurDir, hWndTrans, 1) == FFFTP_FAIL)\r
859                                         if(ProcForNonFullpath(Tmp, CurDir, hWndTrans, (int)TrnSkt + 1) == FFFTP_FAIL)\r
860                                         {\r
861                                                 ClearAll = YES;\r
862                                                 CwdSts = FTP_ERROR;\r
863                                         }\r
864 \r
865                                         if(CwdSts == FTP_COMPLETE)\r
866                                         {\r
867                                                 Up = YES;\r
868 //                                              CommandProcTrn(NULL, "MKD %s", Tmp);\r
869                                                 CommandProcTrn(TrnSkt, NULL, "MKD %s", Tmp);\r
870                                                 /* すでにフォルダがある場合もあるので、 */\r
871                                                 /* ここではエラーチェックはしない */\r
872 \r
873                                         if(FolderAttr)\r
874 //                                              CommandProcTrn(NULL, "%s %03d %s", AskHostChmodCmd(), FolderAttrNum, Tmp);\r
875                                                 CommandProcTrn(TrnSkt, NULL, "%s %03d %s", AskHostChmodCmd(), FolderAttrNum, Tmp);\r
876                                         }\r
877                                 }\r
878 //                              else if(strlen(TransPacketBase->LocalFile) > 0)\r
879                                 else if(strlen(Pos->LocalFile) > 0)\r
880                                 {\r
881                                         Down = YES;\r
882 //                                      DoLocalMKD(TransPacketBase->LocalFile);\r
883                                         DoLocalMKD(Pos->LocalFile);\r
884                                 }\r
885                                 ReleaseMutex(hListAccMutex);\r
886                         }\r
887                         /* ディレクトリ作成(常にホスト側) */\r
888 //                      else if(strncmp(TransPacketBase->Cmd, "R-MKD", 5) == 0)\r
889                         else if(strncmp(Pos->Cmd, "R-MKD", 5) == 0)\r
890                         {\r
891 //                              DispTransFileInfo(TransPacketBase, MSGJPN079, FALSE, YES);\r
892                                 DispTransFileInfo(Pos, MSGJPN079, FALSE, YES);\r
893 \r
894                                 /* フルパスを使わないための処理 */\r
895 //                              if(MakeNonFullPath(TransPacketBase, CurDir, Tmp) == FFFTP_SUCCESS)\r
896                                 if(MakeNonFullPath(Pos, CurDir, Tmp) == FFFTP_SUCCESS)\r
897                                 {\r
898                                         Up = YES;\r
899 //                                      CommandProcTrn(NULL, "%s%s", TransPacketBase->Cmd+2, TransPacketBase->RemoteFile);\r
900                                         CommandProcTrn(TrnSkt, NULL, "%s%s", Pos->Cmd+2, Pos->RemoteFile);\r
901 \r
902                                         if(FolderAttr)\r
903 //                                              CommandProcTrn(NULL, "%s %03d %s", AskHostChmodCmd(), FolderAttrNum, TransPacketBase->RemoteFile);\r
904                                                 CommandProcTrn(TrnSkt, NULL, "%s %03d %s", AskHostChmodCmd(), FolderAttrNum, Pos->RemoteFile);\r
905                                 }\r
906                                 ReleaseMutex(hListAccMutex);\r
907                         }\r
908                         /* ディレクトリ削除(常にホスト側) */\r
909 //                      else if(strncmp(TransPacketBase->Cmd, "R-RMD", 5) == 0)\r
910                         else if(strncmp(Pos->Cmd, "R-RMD", 5) == 0)\r
911                         {\r
912 //                              DispTransFileInfo(TransPacketBase, MSGJPN080, FALSE, YES);\r
913                                 DispTransFileInfo(Pos, MSGJPN080, FALSE, YES);\r
914 \r
915 //                              DelNotify = MirrorDelNotify(WIN_REMOTE, DelNotify, TransPacketBase);\r
916                                 DelNotify = MirrorDelNotify(WIN_REMOTE, DelNotify, Pos);\r
917                                 if((DelNotify == YES) || (DelNotify == YES_ALL))\r
918                                 {\r
919                                         /* フルパスを使わないための処理 */\r
920 //                                      if(MakeNonFullPath(TransPacketBase, CurDir, Tmp) == FFFTP_SUCCESS)\r
921                                         if(MakeNonFullPath(Pos, CurDir, Tmp) == FFFTP_SUCCESS)\r
922                                         {\r
923                                                 Up = YES;\r
924 //                                              CommandProcTrn(NULL, "%s%s", TransPacketBase->Cmd+2, TransPacketBase->RemoteFile);\r
925                                                 CommandProcTrn(TrnSkt, NULL, "%s%s", Pos->Cmd+2, Pos->RemoteFile);\r
926                                         }\r
927                                 }\r
928                                 ReleaseMutex(hListAccMutex);\r
929                         }\r
930                         /* ファイル削除(常にホスト側) */\r
931 //                      else if(strncmp(TransPacketBase->Cmd, "R-DELE", 6) == 0)\r
932                         else if(strncmp(Pos->Cmd, "R-DELE", 6) == 0)\r
933                         {\r
934 //                              DispTransFileInfo(TransPacketBase, MSGJPN081, FALSE, YES);\r
935                                 DispTransFileInfo(Pos, MSGJPN081, FALSE, YES);\r
936 \r
937 //                              DelNotify = MirrorDelNotify(WIN_REMOTE, DelNotify, TransPacketBase);\r
938                                 DelNotify = MirrorDelNotify(WIN_REMOTE, DelNotify, Pos);\r
939                                 if((DelNotify == YES) || (DelNotify == YES_ALL))\r
940                                 {\r
941                                         /* フルパスを使わないための処理 */\r
942 //                                      if(MakeNonFullPath(TransPacketBase, CurDir, Tmp) == FFFTP_SUCCESS)\r
943                                         if(MakeNonFullPath(Pos, CurDir, Tmp) == FFFTP_SUCCESS)\r
944                                         {\r
945                                                 Up = YES;\r
946 //                                              CommandProcTrn(NULL, "%s%s", TransPacketBase->Cmd+2, TransPacketBase->RemoteFile);\r
947                                                 CommandProcTrn(TrnSkt, NULL, "%s%s", Pos->Cmd+2, Pos->RemoteFile);\r
948                                         }\r
949                                 }\r
950                                 ReleaseMutex(hListAccMutex);\r
951                         }\r
952                         /* ディレクトリ作成(常にローカル側) */\r
953 //                      else if(strncmp(TransPacketBase->Cmd, "L-MKD", 5) == 0)\r
954                         else if(strncmp(Pos->Cmd, "L-MKD", 5) == 0)\r
955                         {\r
956 //                              DispTransFileInfo(TransPacketBase, MSGJPN082, FALSE, YES);\r
957                                 DispTransFileInfo(Pos, MSGJPN082, FALSE, YES);\r
958 \r
959                                 Down = YES;\r
960 //                              DoLocalMKD(TransPacketBase->LocalFile);\r
961                                 DoLocalMKD(Pos->LocalFile);\r
962                                 ReleaseMutex(hListAccMutex);\r
963                         }\r
964                         /* ディレクトリ削除(常にローカル側) */\r
965 //                      else if(strncmp(TransPacketBase->Cmd, "L-RMD", 5) == 0)\r
966                         else if(strncmp(Pos->Cmd, "L-RMD", 5) == 0)\r
967                         {\r
968 //                              DispTransFileInfo(TransPacketBase, MSGJPN083, FALSE, YES);\r
969                                 DispTransFileInfo(Pos, MSGJPN083, FALSE, YES);\r
970 \r
971 //                              DelNotify = MirrorDelNotify(WIN_LOCAL, DelNotify, TransPacketBase);\r
972                                 DelNotify = MirrorDelNotify(WIN_LOCAL, DelNotify, Pos);\r
973                                 if((DelNotify == YES) || (DelNotify == YES_ALL))\r
974                                 {\r
975                                         Down = YES;\r
976 //                                      DoLocalRMD(TransPacketBase->LocalFile);\r
977                                         DoLocalRMD(Pos->LocalFile);\r
978                                 }\r
979                                 ReleaseMutex(hListAccMutex);\r
980                         }\r
981                         /* ファイル削除(常にローカル側) */\r
982 //                      else if(strncmp(TransPacketBase->Cmd, "L-DELE", 6) == 0)\r
983                         else if(strncmp(Pos->Cmd, "L-DELE", 6) == 0)\r
984                         {\r
985 //                              DispTransFileInfo(TransPacketBase, MSGJPN084, FALSE, YES);\r
986                                 DispTransFileInfo(Pos, MSGJPN084, FALSE, YES);\r
987 \r
988 //                              DelNotify = MirrorDelNotify(WIN_LOCAL, DelNotify, TransPacketBase);\r
989                                 DelNotify = MirrorDelNotify(WIN_LOCAL, DelNotify, Pos);\r
990                                 if((DelNotify == YES) || (DelNotify == YES_ALL))\r
991                                 {\r
992                                         Down = YES;\r
993 //                                      DoLocalDELE(TransPacketBase->LocalFile);\r
994                                         DoLocalDELE(Pos->LocalFile);\r
995                                 }\r
996                                 ReleaseMutex(hListAccMutex);\r
997                         }\r
998                         /* カレントディレクトリを設定 */\r
999 //                      else if(strcmp(TransPacketBase->Cmd, "SETCUR") == 0)\r
1000                         else if(strcmp(Pos->Cmd, "SETCUR") == 0)\r
1001                         {\r
1002                                 if(AskShareProh() == YES)\r
1003                                 {\r
1004 //                                      if(strcmp(CurDir, TransPacketBase->RemoteFile) != 0)\r
1005                                         if(strcmp(CurDir, Pos->RemoteFile) != 0)\r
1006                                         {\r
1007 //                                              if(CommandProcTrn(NULL, "CWD %s", TransPacketBase->RemoteFile)/100 != FTP_COMPLETE)\r
1008                                                 if(CommandProcTrn(TrnSkt, NULL, "CWD %s", Pos->RemoteFile)/100 != FTP_COMPLETE)\r
1009                                                 {\r
1010                                                         DispCWDerror(hWndTrans);\r
1011                                                         ClearAll = YES;\r
1012                                                 }\r
1013                                         }\r
1014                                 }\r
1015 //                              strcpy(CurDir, TransPacketBase->RemoteFile);\r
1016                                 strcpy(CurDir, Pos->RemoteFile);\r
1017                                 ReleaseMutex(hListAccMutex);\r
1018                         }\r
1019                         /* カレントディレクトリを戻す */\r
1020 //                      else if(strcmp(TransPacketBase->Cmd, "BACKCUR") == 0)\r
1021                         else if(strcmp(Pos->Cmd, "BACKCUR") == 0)\r
1022                         {\r
1023                                 if(AskShareProh() == NO)\r
1024                                 {\r
1025 //                                      if(strcmp(CurDir, TransPacketBase->RemoteFile) != 0)\r
1026 //                                              CommandProcTrn(NULL, "CWD %s", TransPacketBase->RemoteFile);\r
1027 //                                      strcpy(CurDir, TransPacketBase->RemoteFile);\r
1028                                         if(strcmp(CurDir, Pos->RemoteFile) != 0)\r
1029                                                 CommandProcTrn(TrnSkt, NULL, "CWD %s", Pos->RemoteFile);\r
1030                                         strcpy(CurDir, Pos->RemoteFile);\r
1031                                 }\r
1032                                 ReleaseMutex(hListAccMutex);\r
1033                         }\r
1034                         /* 自動終了のための通知 */\r
1035 //                      else if(strcmp(TransPacketBase->Cmd, "GOQUIT") == 0)\r
1036                         else if(strcmp(Pos->Cmd, "GOQUIT") == 0)\r
1037                         {\r
1038                                 ReleaseMutex(hListAccMutex);\r
1039                                 GoExit = YES;\r
1040                         }\r
1041                         else\r
1042                                 ReleaseMutex(hListAccMutex);\r
1043 \r
1044                         /*===== 1つの処理終わり =====*/\r
1045 \r
1046                         if(ForceAbort == NO)\r
1047                         {\r
1048 //                              WaitForSingleObject(hListAccMutex, INFINITE);\r
1049                                 while(WaitForSingleObject(hListAccMutex, 0) == WAIT_TIMEOUT)\r
1050                                 {\r
1051                                         BackgrndMessageProc();\r
1052                                         Sleep(1);\r
1053                                 }\r
1054                                 if(ClearAll == YES)\r
1055 //                                      EraseTransFileList();\r
1056                                 {\r
1057                                         for(i = 0; i < MAX_DATA_CONNECTION; i++)\r
1058                                                 Canceled[i] = YES;\r
1059                                         EraseTransFileList();\r
1060                                         Pos = NULL;\r
1061                                 }\r
1062                                 else\r
1063                                 {\r
1064 //                                      if((strncmp(TransPacketBase->Cmd, "RETR", 4) == 0) ||\r
1065 //                                         (strncmp(TransPacketBase->Cmd, "STOR", 4) == 0))\r
1066                                         if((strncmp(Pos->Cmd, "RETR", 4) == 0) ||\r
1067                                            (strncmp(Pos->Cmd, "STOR", 4) == 0))\r
1068                                         {\r
1069 //                                              TransFiles--;\r
1070                                                 if(TransFiles > 0)\r
1071                                                         TransFiles--;\r
1072                                                 PostMessage(GetMainHwnd(), WM_CHANGE_COND, 0, 0);\r
1073                                         }\r
1074 //                                      Pos = TransPacketBase;\r
1075 //                                      TransPacketBase = TransPacketBase->Next;\r
1076 //                                      free(Pos);\r
1077                                 }\r
1078                                 ClearAll = NO;\r
1079                                 ReleaseMutex(hListAccMutex);\r
1080 \r
1081                                 if(BackgrndMessageProc() == YES)\r
1082                                 {\r
1083                                         WaitForSingleObject(hListAccMutex, INFINITE);\r
1084                                         EraseTransFileList();\r
1085                                         ReleaseMutex(hListAccMutex);\r
1086                                 }\r
1087                         }\r
1088                         if(hWndTrans != NULL)\r
1089                                 SendMessage(hWndTrans, WM_SET_PACKET, 0, 0);\r
1090                         if(Pos != NULL)\r
1091                                 strcpy(Pos->Cmd, "");\r
1092                 }\r
1093 //              else\r
1094                 else if(TransPacketBase == NULL)\r
1095                 {\r
1096                         DelNotify = NO;\r
1097 \r
1098                         ReleaseMutex(hListAccMutex);\r
1099                         if(KeepDlg == NO)\r
1100                         {\r
1101                                 if(hWndTrans != NULL)\r
1102                                 {\r
1103                                         DestroyWindow(hWndTrans);\r
1104                                         hWndTrans = NULL;\r
1105 \r
1106 //                                      if(GoExit == YES)\r
1107 //                                      {\r
1108 //                                              SoundPlay(SND_TRANS);\r
1109 //\r
1110 //                                              if(AskAutoExit() == NO)\r
1111 //                                              {\r
1112 //                                                      if(Down == YES)\r
1113 //                                                              PostMessage(GetMainHwnd(), WM_REFRESH_LOCAL_FLG, 0, 0);\r
1114 //                                                      if(Up == YES)\r
1115 //                                                              PostMessage(GetMainHwnd(), WM_REFRESH_REMOTE_FLG, 0, 0);\r
1116 //                                              }\r
1117 //                                              Down = NO;\r
1118 //                                              Up = NO;\r
1119 //                                      }\r
1120                                 }\r
1121                         }\r
1122                         BackgrndMessageProc();\r
1123 //                      Sleep(1);\r
1124                         Sleep(100);\r
1125 \r
1126                         if(GoExit == YES)\r
1127                         {\r
1128                                 SoundPlay(SND_TRANS);\r
1129                                 if(AskAutoExit() == NO)\r
1130                                 {\r
1131                                         if(Down == YES)\r
1132                                                 PostMessage(GetMainHwnd(), WM_REFRESH_LOCAL_FLG, 0, 0);\r
1133                                         if(Up == YES)\r
1134                                                 PostMessage(GetMainHwnd(), WM_REFRESH_REMOTE_FLG, 0, 0);\r
1135                                 }\r
1136                                 Down = NO;\r
1137                                 Up = NO;\r
1138                                 PostMessage(GetMainHwnd(), WM_COMMAND, MAKEWPARAM(MENU_AUTO_EXIT, 0), 0);\r
1139                                 GoExit = NO;\r
1140                         }\r
1141                 }\r
1142                 else\r
1143                 {\r
1144                         ReleaseMutex(hListAccMutex);\r
1145                         if(hWndTrans != NULL)\r
1146                         {\r
1147                                 DestroyWindow(hWndTrans);\r
1148                                 hWndTrans = NULL;\r
1149                         }\r
1150                         BackgrndMessageProc();\r
1151                         if(ThreadCount < AskMaxThreadCount())\r
1152                                 Sleep(1);\r
1153                         else\r
1154                                 Sleep(100);\r
1155                 }\r
1156         }\r
1157         if(TrnSkt != INVALID_SOCKET)\r
1158                 DoClose(TrnSkt);\r
1159         return 0;\r
1160 }\r
1161 \r
1162 \r
1163 /*----- フルパスを使わないファイルアクセスの準備 ------------------------------\r
1164 *\r
1165 *       Parameter\r
1166 *               TRANSPACKET *Pkt : 転送パケット\r
1167 *               char *Cur : カレントディレクトリ\r
1168 *               char *Tmp : 作業用エリア\r
1169 *\r
1170 *       Return Value\r
1171 *               int ステータス(FFFTP_SUCCESS/FFFTP_FAIL)\r
1172 *\r
1173 *       Note\r
1174 *               フルパスを使わない時は、\r
1175 *                       このモジュール内で CWD を行ない、\r
1176 *                       Pkt->RemoteFile にファイル名のみ残す。(パス名は消す)\r
1177 *----------------------------------------------------------------------------*/\r
1178 \r
1179 // 同時接続対応\r
1180 static int MakeNonFullPath(TRANSPACKET *Pkt, char *Cur, char *Tmp)\r
1181 {\r
1182         int Sts;\r
1183 \r
1184 //      Sts = ProcForNonFullpath(Pkt->RemoteFile, Cur, Pkt->hWndTrans, 1);\r
1185         Sts = ProcForNonFullpath(Pkt->RemoteFile, Cur, Pkt->hWndTrans, (int)Pkt->ctrl_skt + 1);\r
1186         if(Sts == FFFTP_FAIL)\r
1187                 ClearAll = YES;\r
1188 \r
1189         return(Sts);\r
1190 }\r
1191 \r
1192 \r
1193 \r
1194 \r
1195 /*----- ダウンロードを行なう --------------------------------------------------\r
1196 *\r
1197 *       Parameter\r
1198 *               SOCKET cSkt : コントロールソケット\r
1199 *               TRANSPACKET *Pkt : 転送ファイル情報\r
1200 *               int DirList : ディレクトリリストのダウンロード(YES/NO)\r
1201 *\r
1202 *       Return Value\r
1203 *               int 応答コード\r
1204 *\r
1205 *       Note\r
1206 *               このモジュールは、ファイル一覧の取得などを行なう際にメインのスレッド\r
1207 *               からも呼ばれる。メインのスレッドから呼ばれる時は Pkt->hWndTrans == NULL。\r
1208 *----------------------------------------------------------------------------*/\r
1209 \r
1210 int DoDownLoad(SOCKET cSkt, TRANSPACKET *Pkt, int DirList, int *CancelCheckWork)\r
1211 {\r
1212         int iRetCode;\r
1213         char Reply[ERR_MSG_LEN+7];\r
1214 \r
1215         Pkt->ctrl_skt = cSkt;\r
1216         if(IsSpecialDevice(GetFileName(Pkt->LocalFile)) == YES)\r
1217         {\r
1218                 iRetCode = 500;\r
1219                 SetTaskMsg(MSGJPN085, GetFileName(Pkt->LocalFile));\r
1220                 DispDownloadFinishMsg(Pkt, iRetCode);\r
1221         }\r
1222         else if(Pkt->Mode != EXIST_IGNORE)\r
1223         {\r
1224                 if(Pkt->Type == TYPE_I)\r
1225                         Pkt->KanjiCode = KANJI_NOCNV;\r
1226 \r
1227                 iRetCode = command(Pkt->ctrl_skt, Reply, CancelCheckWork, "TYPE %c", Pkt->Type);\r
1228                 if(iRetCode/100 < FTP_RETRY)\r
1229                 {\r
1230                         if(Pkt->hWndTrans != NULL)\r
1231                         {\r
1232                                 // 同時接続対応\r
1233 //                              AllTransSizeNow = 0;\r
1234                                 AllTransSizeNow[Pkt->ThreadCount] = 0;\r
1235 \r
1236                                 if(DirList == NO)\r
1237                                         DispTransFileInfo(Pkt, MSGJPN086, TRUE, YES);\r
1238                                 else\r
1239                                         DispTransFileInfo(Pkt, MSGJPN087, FALSE, NO);\r
1240                         }\r
1241 \r
1242                         if(BackgrndMessageProc() == NO)\r
1243                         {\r
1244                                 if(AskPasvMode() != YES)\r
1245                                         iRetCode = DownLoadNonPassive(Pkt, CancelCheckWork);\r
1246                                 else\r
1247                                         iRetCode = DownLoadPassive(Pkt, CancelCheckWork);\r
1248                         }\r
1249                         else\r
1250                                 iRetCode = 500;\r
1251                 }\r
1252                 else\r
1253                         SetErrorMsg(Reply);\r
1254         }\r
1255         else\r
1256         {\r
1257                 DispTransFileInfo(Pkt, MSGJPN088, TRUE, YES);\r
1258                 SetTaskMsg(MSGJPN089, Pkt->RemoteFile);\r
1259                 iRetCode = 200;\r
1260         }\r
1261         return(iRetCode);\r
1262 }\r
1263 \r
1264 \r
1265 /*----- 通常モードでファイルをダウンロード ------------------------------------\r
1266 *\r
1267 *       Parameter\r
1268 *               TRANSPACKET *Pkt : 転送ファイル情報\r
1269 *\r
1270 *       Return Value\r
1271 *               int 応答コード\r
1272 *----------------------------------------------------------------------------*/\r
1273 \r
1274 static int DownLoadNonPassive(TRANSPACKET *Pkt, int *CancelCheckWork)\r
1275 {\r
1276         int iRetCode;\r
1277         int iLength;\r
1278         SOCKET data_socket = INVALID_SOCKET;   // data channel socket\r
1279         SOCKET listen_socket = INVALID_SOCKET; // data listen socket\r
1280         char Buf[1024];\r
1281         int CreateMode;\r
1282         struct sockaddr_in saSockAddr1;\r
1283         char Reply[ERR_MSG_LEN+7];\r
1284 \r
1285         if((listen_socket = GetFTPListenSocket(Pkt->ctrl_skt, CancelCheckWork)) != INVALID_SOCKET)\r
1286         {\r
1287                 if(SetDownloadResume(Pkt, Pkt->Mode, Pkt->ExistSize, &CreateMode, CancelCheckWork) == YES)\r
1288                 {\r
1289                         sprintf(Buf, "%s%s", Pkt->Cmd, Pkt->RemoteFile);\r
1290                         iRetCode = command(Pkt->ctrl_skt, Reply, CancelCheckWork, "%s", Buf);\r
1291                         if(iRetCode/100 == FTP_PRELIM)\r
1292                         {\r
1293                                 if(SocksGet2ndBindReply(listen_socket, &data_socket) == FFFTP_FAIL)\r
1294                                 {\r
1295                                         iLength = sizeof(saSockAddr1);\r
1296                                         data_socket = do_accept(listen_socket, (struct sockaddr *)&saSockAddr1, (int *)&iLength);\r
1297 \r
1298                                         if(shutdown(listen_socket, 1) != 0)\r
1299                                                 ReportWSError("shutdown listen", WSAGetLastError());\r
1300                                         listen_socket = DoClose(listen_socket);\r
1301 \r
1302                                         if(data_socket == INVALID_SOCKET)\r
1303                                         {\r
1304                                                 SetErrorMsg(MSGJPN280);\r
1305                                                 ReportWSError("accept", WSAGetLastError());\r
1306                                                 iRetCode = 500;\r
1307                                         }\r
1308                                         else\r
1309                                                 DoPrintf("Skt=%u : accept from %s port %u", data_socket, inet_ntoa(saSockAddr1.sin_addr), ntohs(saSockAddr1.sin_port));\r
1310                                 }\r
1311 \r
1312                                 if(data_socket != INVALID_SOCKET)\r
1313                                 {\r
1314                                         // 一部TYPE、STOR(RETR)、PORT(PASV)を並列に処理できないホストがあるため\r
1315                                         ReleaseMutex(hListAccMutex);\r
1316                                         // FTPS対応\r
1317 //                                      iRetCode = DownLoadFile(Pkt, data_socket, CreateMode, CancelCheckWork);\r
1318                                         if(IsSSLAttached(Pkt->ctrl_skt))\r
1319                                         {\r
1320                                                 if(AttachSSL(data_socket, Pkt->ctrl_skt))\r
1321                                                         iRetCode = DownLoadFile(Pkt, data_socket, CreateMode, CancelCheckWork);\r
1322                                                 else\r
1323                                                         iRetCode = 500;\r
1324                                         }\r
1325                                         else\r
1326                                                 iRetCode = DownLoadFile(Pkt, data_socket, CreateMode, CancelCheckWork);\r
1327 //                                      data_socket = DoClose(data_socket);\r
1328                                 }\r
1329                         }\r
1330                         else\r
1331                         {\r
1332                                 SetErrorMsg(Reply);\r
1333                                 SetTaskMsg(MSGJPN090);\r
1334                                 listen_socket = DoClose(listen_socket);\r
1335                                 iRetCode = 500;\r
1336                         }\r
1337                 }\r
1338                 else\r
1339                         iRetCode = 500;\r
1340         }\r
1341         else\r
1342         {\r
1343                 iRetCode = 500;\r
1344                 SetErrorMsg(MSGJPN279);\r
1345         }\r
1346         DispDownloadFinishMsg(Pkt, iRetCode);\r
1347 \r
1348         return(iRetCode);\r
1349 }\r
1350 \r
1351 \r
1352 /*----- Passiveモードでファイルをダウンロード ---------------------------------\r
1353 *\r
1354 *       Parameter\r
1355 *               TRANSPACKET *Pkt : 転送ファイル情報\r
1356 *\r
1357 *       Return Value\r
1358 *               int 応答コード\r
1359 *----------------------------------------------------------------------------*/\r
1360 \r
1361 static int DownLoadPassive(TRANSPACKET *Pkt, int *CancelCheckWork)\r
1362 {\r
1363         int iRetCode;\r
1364         SOCKET data_socket = INVALID_SOCKET;   // data channel socket\r
1365         char Buf[1024];\r
1366         int CreateMode;\r
1367         char Adrs[20];\r
1368         int Port;\r
1369         int Flg;\r
1370         char Reply[ERR_MSG_LEN+7];\r
1371 \r
1372         iRetCode = command(Pkt->ctrl_skt, Buf, CancelCheckWork, "PASV");\r
1373         if(iRetCode/100 == FTP_COMPLETE)\r
1374         {\r
1375                 if(GetAdrsAndPort(Buf, Adrs, &Port, 19) == FFFTP_SUCCESS)\r
1376                 {\r
1377                         if((data_socket = connectsock(Adrs, Port, MSGJPN091, CancelCheckWork)) != INVALID_SOCKET)\r
1378                         {\r
1379                                 // 変数が未初期化のバグ修正\r
1380                                 Flg = 1;\r
1381                                 if(setsockopt(data_socket, IPPROTO_TCP, TCP_NODELAY, (LPSTR)&Flg, sizeof(Flg)) == SOCKET_ERROR)\r
1382                                         ReportWSError("setsockopt", WSAGetLastError());\r
1383 \r
1384                                 if(SetDownloadResume(Pkt, Pkt->Mode, Pkt->ExistSize, &CreateMode, CancelCheckWork) == YES)\r
1385                                 {\r
1386                                         sprintf(Buf, "%s%s", Pkt->Cmd, Pkt->RemoteFile);\r
1387                                         iRetCode = command(Pkt->ctrl_skt, Reply, CancelCheckWork, "%s", Buf);\r
1388                                         if(iRetCode/100 == FTP_PRELIM)\r
1389                                         {\r
1390                                                 // 一部TYPE、STOR(RETR)、PORT(PASV)を並列に処理できないホストがあるため\r
1391                                                 ReleaseMutex(hListAccMutex);\r
1392                                                 // FTPS対応\r
1393 //                                              iRetCode = DownLoadFile(Pkt, data_socket, CreateMode, CancelCheckWork);\r
1394                                                 if(IsSSLAttached(Pkt->ctrl_skt))\r
1395                                                 {\r
1396                                                         if(AttachSSL(data_socket, Pkt->ctrl_skt))\r
1397                                                                 iRetCode = DownLoadFile(Pkt, data_socket, CreateMode, CancelCheckWork);\r
1398                                                         else\r
1399                                                                 iRetCode = 500;\r
1400                                                 }\r
1401                                                 else\r
1402                                                         iRetCode = DownLoadFile(Pkt, data_socket, CreateMode, CancelCheckWork);\r
1403 //                                              data_socket = DoClose(data_socket);\r
1404                                         }\r
1405                                         else\r
1406                                         {\r
1407                                                 SetErrorMsg(Reply);\r
1408                                                 SetTaskMsg(MSGJPN092);\r
1409                                                 data_socket = DoClose(data_socket);\r
1410                                                 iRetCode = 500;\r
1411                                         }\r
1412                                 }\r
1413                                 else\r
1414                                         iRetCode = 500;\r
1415                         }\r
1416                         else\r
1417                                 iRetCode = 500;\r
1418                 }\r
1419                 else\r
1420                 {\r
1421                         SetErrorMsg(MSGJPN093);\r
1422                         SetTaskMsg(MSGJPN093);\r
1423                         iRetCode = 500;\r
1424                 }\r
1425         }\r
1426         else\r
1427                 SetErrorMsg(Buf);\r
1428 \r
1429         DispDownloadFinishMsg(Pkt, iRetCode);\r
1430 \r
1431         return(iRetCode);\r
1432 }\r
1433 \r
1434 \r
1435 /*----- ダウンロードの実行 ----------------------------------------------------\r
1436 *\r
1437 *       Parameter\r
1438 *               TRANSPACKET *Pkt : 転送ファイル情報\r
1439 *               SOCKET dSkt : データソケット\r
1440 *               int CreateMode : ファイル作成モード (CREATE_ALWAYS/OPEN_ALWAYS)\r
1441 *\r
1442 *       Return Value\r
1443 *               int 応答コード\r
1444 *\r
1445 *       Note\r
1446 *               転送の経過表示は\r
1447 *                       ダイアログを出す(Pkt->hWndTrans!=NULL)場合、インターバルタイマで経過を表示する\r
1448 *                       ダイアログを出さない場合、このルーチンからDispDownloadSize()を呼ぶ\r
1449 *----------------------------------------------------------------------------*/\r
1450 \r
1451 static int DownLoadFile(TRANSPACKET *Pkt, SOCKET dSkt, int CreateMode, int *CancelCheckWork)\r
1452 {\r
1453         int iNumBytes;\r
1454         char Buf[BUFSIZE];\r
1455         char Buf2[BUFSIZE+3];\r
1456         HANDLE iFileHandle;\r
1457         SECURITY_ATTRIBUTES Sec;\r
1458         DWORD Writed;\r
1459         CODECONVINFO cInfo;\r
1460         int Continue;\r
1461 //      fd_set ReadFds;\r
1462 //      struct timeval Tout;\r
1463 //      struct timeval *ToutPtr;\r
1464         int iRetCode;\r
1465         int TimeOutErr;\r
1466         char TmpBuf[ONELINE_BUF_SIZE];\r
1467         DWORD dwFileAttributes;\r
1468 \r
1469 #ifdef SET_BUFFER_SIZE\r
1470 /* Add by H.Shirouzu at 2002/10/02 */\r
1471         int buf_size = SOCKBUF_SIZE;\r
1472         for ( ; buf_size > 0; buf_size /= 2)\r
1473                 if (setsockopt(dSkt, SOL_SOCKET, SO_RCVBUF, (char *)&buf_size, sizeof(buf_size)) == 0)\r
1474                         break;\r
1475 /* End */\r
1476 #endif\r
1477 \r
1478         // 念のため受信バッファを無効にする\r
1479 #ifdef DISABLE_TRANSFER_NETWORK_BUFFERS\r
1480         int buf_size = 0;\r
1481         setsockopt(dSkt, SOL_SOCKET, SO_RCVBUF, (char *)&buf_size, sizeof(buf_size));\r
1482 #endif\r
1483 \r
1484         Pkt->Abort = ABORT_NONE;\r
1485 \r
1486         Sec.nLength = sizeof(SECURITY_ATTRIBUTES);\r
1487         Sec.lpSecurityDescriptor = NULL;\r
1488         Sec.bInheritHandle = FALSE;\r
1489 \r
1490         dwFileAttributes = GetFileAttributes(Pkt->LocalFile);\r
1491         if (dwFileAttributes != INVALID_FILE_ATTRIBUTES && (dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {\r
1492                 // 読み取り専用\r
1493                 if (MessageBox(GetMainHwnd(), MSGJPN296, MSGJPN086, MB_YESNO) == IDYES) {\r
1494                         // 属性を解除\r
1495                         SetFileAttributes(Pkt->LocalFile, dwFileAttributes ^ FILE_ATTRIBUTE_READONLY);\r
1496                 }\r
1497         }\r
1498 \r
1499         if((iFileHandle = CreateFile(Pkt->LocalFile, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, &Sec, CreateMode, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE)\r
1500         {\r
1501                 // UTF-8対応\r
1502                 char Buf3[(BUFSIZE + 3) * 4];\r
1503                 CODECONVINFO cInfo2;\r
1504                 int ProcessedBOM = NO;\r
1505                 // 4GB超対応(kaokunさん提供)\r
1506                 DWORD High = 0;\r
1507                 if(CreateMode == OPEN_ALWAYS)\r
1508                         // 4GB超対応(kaokunさん提供)\r
1509 //                      SetFilePointer(iFileHandle, 0, 0, FILE_END);\r
1510                         SetFilePointer(iFileHandle, 0, &High, FILE_END);\r
1511 \r
1512                 if(Pkt->hWndTrans != NULL)\r
1513                 {\r
1514                         // 同時接続対応\r
1515 //                      TimeStart = time(NULL);\r
1516                         TimeStart[Pkt->ThreadCount] = time(NULL);\r
1517                         SetTimer(Pkt->hWndTrans, TIMER_DISPLAY, DISPLAY_TIMING, NULL);\r
1518                 }\r
1519 \r
1520                 InitCodeConvInfo(&cInfo);\r
1521                 cInfo.KanaCnv = Pkt->KanaCnv;\r
1522 \r
1523                 InitCodeConvInfo(&cInfo2);\r
1524                 cInfo2.KanaCnv = Pkt->KanaCnv;\r
1525 \r
1526                 /*===== ファイルを受信するループ =====*/\r
1527                 while((Pkt->Abort == ABORT_NONE) && (ForceAbort == NO))\r
1528                 {\r
1529 //                      FD_ZERO(&ReadFds);\r
1530 //                      FD_SET(dSkt, &ReadFds);\r
1531 //                      ToutPtr = NULL;\r
1532 //                      if(TimeOut != 0)\r
1533 //                      {\r
1534 //                              Tout.tv_sec = TimeOut;\r
1535 //                              Tout.tv_usec = 0;\r
1536 //                              ToutPtr = &Tout;\r
1537 //                      }\r
1538 //                      iNumBytes = select(0, &ReadFds, NULL, NULL, ToutPtr);\r
1539 //                      if(iNumBytes == SOCKET_ERROR)\r
1540 //                      {\r
1541 //                              ReportWSError("select", WSAGetLastError());\r
1542 //                              if(Pkt->Abort == ABORT_NONE)\r
1543 //                                      Pkt->Abort = ABORT_ERROR;\r
1544 //                              break;\r
1545 //                      }\r
1546 //                      else if(iNumBytes == 0)\r
1547 //                      {\r
1548 //                              SetErrorMsg(MSGJPN094);\r
1549 //                              SetTaskMsg(MSGJPN094);\r
1550 //                              Pkt->Abort = ABORT_ERROR;\r
1551 //                              break;\r
1552 //                      }\r
1553 \r
1554                         if((iNumBytes = do_recv(dSkt, Buf, BUFSIZE, 0, &TimeOutErr, CancelCheckWork)) <= 0)\r
1555                         {\r
1556                                 if(TimeOutErr == YES)\r
1557                                 {\r
1558                                         SetErrorMsg(MSGJPN094);\r
1559                                         SetTaskMsg(MSGJPN094);\r
1560                                         if(Pkt->hWndTrans != NULL)\r
1561                                                 ClearAll = YES;\r
1562                                         if(Pkt->Abort == ABORT_NONE)\r
1563                                                 Pkt->Abort = ABORT_ERROR;\r
1564                                 }\r
1565                                 else if(iNumBytes == SOCKET_ERROR)\r
1566                                 {\r
1567                                         if(Pkt->Abort == ABORT_NONE)\r
1568                                                 Pkt->Abort = ABORT_ERROR;\r
1569                                 }\r
1570                                 break;\r
1571                         }\r
1572 \r
1573                         /* 漢字コード変換 */\r
1574                         if(Pkt->KanjiCode != KANJI_NOCNV)\r
1575                         {\r
1576                                 cInfo.Str = Buf;\r
1577                                 cInfo.StrLen = iNumBytes;\r
1578                                 cInfo.Buf = Buf2;\r
1579                                 cInfo.BufSize = BUFSIZE+3;\r
1580                                 do\r
1581                                 {\r
1582                                         // ここで全てUTF-8へ変換する\r
1583                                         // TODO: SJIS以外も直接UTF-8へ変換\r
1584 //                                      if(Pkt->KanjiCode == KANJI_JIS)\r
1585 //                                              Continue = ConvJIStoSJIS(&cInfo);\r
1586 //                                      else\r
1587 //                                              Continue = ConvEUCtoSJIS(&cInfo);\r
1588                                         char Buf3[(BUFSIZE + 3) * 4];\r
1589                                         CODECONVINFO cInfo2;\r
1590                                         switch(Pkt->KanjiCode)\r
1591                                         {\r
1592                                         case KANJI_SJIS:\r
1593                                                 switch(Pkt->KanjiCodeDesired)\r
1594                                                 {\r
1595                                                 case KANJI_SJIS:\r
1596 //                                                      memcpy(Buf3, cInfo.Str, cInfo.StrLen);\r
1597 //                                                      cInfo2.OutLen = cInfo.StrLen;\r
1598 //                                                      Continue = NO;\r
1599                                                         // カナ変換のため\r
1600                                                         Continue = ConvSJIStoJIS(&cInfo);\r
1601                                                         cInfo2.Str = cInfo.Buf;\r
1602                                                         cInfo2.StrLen = cInfo.OutLen;\r
1603                                                         cInfo2.Buf = Buf3;\r
1604                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1605                                                         ConvJIStoSJIS(&cInfo2);\r
1606                                                         break;\r
1607                                                 case KANJI_JIS:\r
1608                                                         Continue = ConvSJIStoJIS(&cInfo);\r
1609                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1610                                                         cInfo2.OutLen = cInfo.OutLen;\r
1611                                                         break;\r
1612                                                 case KANJI_EUC:\r
1613                                                         Continue = ConvSJIStoEUC(&cInfo);\r
1614                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1615                                                         cInfo2.OutLen = cInfo.OutLen;\r
1616                                                         break;\r
1617                                                 case KANJI_UTF8N:\r
1618                                                         if(ProcessedBOM == NO)\r
1619                                                         {\r
1620                                                                 memcpy(Buf3, "\xEF\xBB\xBF", 3);\r
1621                                                                 cInfo2.OutLen = 3;\r
1622                                                                 Continue = YES;\r
1623                                                                 ProcessedBOM = YES;\r
1624                                                                 break;\r
1625                                                         }\r
1626                                                         Continue = ConvSJIStoUTF8N(&cInfo);\r
1627                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1628                                                         cInfo2.OutLen = cInfo.OutLen;\r
1629                                                         break;\r
1630                                                 }\r
1631                                                 break;\r
1632                                         case KANJI_JIS:\r
1633                                                 switch(Pkt->KanjiCodeDesired)\r
1634                                                 {\r
1635                                                 case KANJI_SJIS:\r
1636                                                         Continue = ConvJIStoSJIS(&cInfo);\r
1637                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1638                                                         cInfo2.OutLen = cInfo.OutLen;\r
1639                                                         break;\r
1640                                                 case KANJI_JIS:\r
1641 //                                                      memcpy(Buf3, cInfo.Str, cInfo.StrLen);\r
1642 //                                                      cInfo2.OutLen = cInfo.StrLen;\r
1643 //                                                      Continue = NO;\r
1644                                                         // カナ変換のため\r
1645                                                         Continue = ConvJIStoSJIS(&cInfo);\r
1646                                                         cInfo2.Str = cInfo.Buf;\r
1647                                                         cInfo2.StrLen = cInfo.OutLen;\r
1648                                                         cInfo2.Buf = Buf3;\r
1649                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1650                                                         ConvSJIStoJIS(&cInfo2);\r
1651                                                         break;\r
1652                                                 case KANJI_EUC:\r
1653                                                         Continue = ConvJIStoSJIS(&cInfo);\r
1654                                                         cInfo2.Str = cInfo.Buf;\r
1655                                                         cInfo2.StrLen = cInfo.OutLen;\r
1656                                                         cInfo2.Buf = Buf3;\r
1657                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1658                                                         ConvSJIStoEUC(&cInfo2);\r
1659                                                         break;\r
1660                                                 case KANJI_UTF8N:\r
1661                                                         if(ProcessedBOM == NO)\r
1662                                                         {\r
1663                                                                 memcpy(Buf3, "\xEF\xBB\xBF", 3);\r
1664                                                                 cInfo2.OutLen = 3;\r
1665                                                                 Continue = YES;\r
1666                                                                 ProcessedBOM = YES;\r
1667                                                                 break;\r
1668                                                         }\r
1669                                                         Continue = ConvJIStoSJIS(&cInfo);\r
1670                                                         cInfo2.Str = cInfo.Buf;\r
1671                                                         cInfo2.StrLen = cInfo.OutLen;\r
1672                                                         cInfo2.Buf = Buf3;\r
1673                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1674                                                         ConvSJIStoUTF8N(&cInfo2);\r
1675                                                         break;\r
1676                                                 }\r
1677                                                 break;\r
1678                                         case KANJI_EUC:\r
1679                                                 switch(Pkt->KanjiCodeDesired)\r
1680                                                 {\r
1681                                                 case KANJI_SJIS:\r
1682                                                         Continue = ConvEUCtoSJIS(&cInfo);\r
1683                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1684                                                         cInfo2.OutLen = cInfo.OutLen;\r
1685                                                         break;\r
1686                                                 case KANJI_JIS:\r
1687                                                         Continue = ConvEUCtoSJIS(&cInfo);\r
1688                                                         cInfo2.Str = cInfo.Buf;\r
1689                                                         cInfo2.StrLen = cInfo.OutLen;\r
1690                                                         cInfo2.Buf = Buf3;\r
1691                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1692                                                         ConvSJIStoJIS(&cInfo2);\r
1693                                                         break;\r
1694                                                 case KANJI_EUC:\r
1695 //                                                      memcpy(Buf3, cInfo.Str, cInfo.StrLen);\r
1696 //                                                      cInfo2.OutLen = cInfo.StrLen;\r
1697 //                                                      Continue = NO;\r
1698                                                         // カナ変換のため\r
1699                                                         Continue = ConvEUCtoSJIS(&cInfo);\r
1700                                                         cInfo2.Str = cInfo.Buf;\r
1701                                                         cInfo2.StrLen = cInfo.OutLen;\r
1702                                                         cInfo2.Buf = Buf3;\r
1703                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1704                                                         ConvSJIStoEUC(&cInfo2);\r
1705                                                         break;\r
1706                                                 case KANJI_UTF8N:\r
1707                                                         if(ProcessedBOM == NO)\r
1708                                                         {\r
1709                                                                 memcpy(Buf3, "\xEF\xBB\xBF", 3);\r
1710                                                                 cInfo2.OutLen = 3;\r
1711                                                                 Continue = YES;\r
1712                                                                 ProcessedBOM = YES;\r
1713                                                                 break;\r
1714                                                         }\r
1715                                                         Continue = ConvEUCtoSJIS(&cInfo);\r
1716                                                         cInfo2.Str = cInfo.Buf;\r
1717                                                         cInfo2.StrLen = cInfo.OutLen;\r
1718                                                         cInfo2.Buf = Buf3;\r
1719                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1720                                                         ConvSJIStoUTF8N(&cInfo2);\r
1721                                                         break;\r
1722                                                 }\r
1723                                                 break;\r
1724                                         case KANJI_UTF8N:\r
1725                                                 if(ProcessedBOM == NO)\r
1726                                                 {\r
1727                                                         if(memcmp(Buf, "\xEF\xBB\xBF", 3) == 0)\r
1728                                                         {\r
1729                                                                 cInfo.Str += 3;\r
1730                                                                 cInfo.StrLen -= 3;\r
1731                                                         }\r
1732                                                         cInfo2.OutLen = 0;\r
1733                                                         switch(Pkt->KanjiCodeDesired)\r
1734                                                         {\r
1735                                                         case KANJI_UTF8N:\r
1736                                                                 memcpy(Buf3, "\xEF\xBB\xBF", 3);\r
1737                                                                 cInfo2.OutLen = 3;\r
1738                                                                 break;\r
1739                                                         }\r
1740                                                         Continue = YES;\r
1741                                                         ProcessedBOM = YES;\r
1742                                                         break;\r
1743                                                 }\r
1744                                                 switch(Pkt->KanjiCodeDesired)\r
1745                                                 {\r
1746                                                 case KANJI_SJIS:\r
1747                                                         Continue = ConvUTF8NtoSJIS(&cInfo);\r
1748                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1749                                                         cInfo2.OutLen = cInfo.OutLen;\r
1750                                                         break;\r
1751                                                 case KANJI_JIS:\r
1752                                                         Continue = ConvUTF8NtoSJIS(&cInfo);\r
1753                                                         cInfo2.Str = cInfo.Buf;\r
1754                                                         cInfo2.StrLen = cInfo.OutLen;\r
1755                                                         cInfo2.Buf = Buf3;\r
1756                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1757                                                         ConvSJIStoJIS(&cInfo2);\r
1758                                                         break;\r
1759                                                 case KANJI_EUC:\r
1760                                                         Continue = ConvUTF8NtoSJIS(&cInfo);\r
1761                                                         cInfo2.Str = cInfo.Buf;\r
1762                                                         cInfo2.StrLen = cInfo.OutLen;\r
1763                                                         cInfo2.Buf = Buf3;\r
1764                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1765                                                         ConvSJIStoEUC(&cInfo2);\r
1766                                                         break;\r
1767                                                 case KANJI_UTF8N:\r
1768                                                         memcpy(Buf3, cInfo.Str, cInfo.StrLen);\r
1769                                                         cInfo2.OutLen = cInfo.StrLen;\r
1770                                                         Continue = NO;\r
1771                                                         break;\r
1772                                                 }\r
1773                                                 break;\r
1774                                         }\r
1775 //                                      if(WriteFile(iFileHandle, Buf2, cInfo.OutLen, &Writed, NULL) == FALSE)\r
1776                                         if(WriteFile(iFileHandle, Buf3, cInfo2.OutLen, &Writed, NULL) == FALSE)\r
1777                                                 Pkt->Abort = ABORT_DISKFULL;\r
1778                                 }\r
1779                                 while((Continue == YES) && (Pkt->Abort == ABORT_NONE));\r
1780                         }\r
1781                         else\r
1782                         {\r
1783                                 if(WriteFile(iFileHandle, Buf, iNumBytes, &Writed, NULL) == FALSE)\r
1784                                         Pkt->Abort = ABORT_DISKFULL;\r
1785                         }\r
1786 \r
1787                         Pkt->ExistSize += iNumBytes;\r
1788                         if(Pkt->hWndTrans != NULL)\r
1789                                 // 同時接続対応\r
1790 //                              AllTransSizeNow += iNumBytes;\r
1791                                 AllTransSizeNow[Pkt->ThreadCount] += iNumBytes;\r
1792                         else\r
1793                         {\r
1794                                 /* 転送ダイアログを出さない時の経過表示 */\r
1795                                 DispDownloadSize(Pkt->ExistSize);\r
1796                         }\r
1797 \r
1798                         if(BackgrndMessageProc() == YES)\r
1799                                 ForceAbort = YES;\r
1800                 }\r
1801 \r
1802                 /* 書き残したデータを書き込む */\r
1803                 if(Pkt->KanjiCode != KANJI_NOCNV)\r
1804                 {\r
1805                         cInfo.Buf = Buf2;\r
1806                         cInfo.BufSize = BUFSIZE+3;\r
1807                         FlushRestData(&cInfo);\r
1808                         switch(Pkt->KanjiCode)\r
1809                         {\r
1810                         case KANJI_SJIS:\r
1811                                 switch(Pkt->KanjiCodeDesired)\r
1812                                 {\r
1813                                 case KANJI_SJIS:\r
1814                                         // カナ変換のため\r
1815                                         cInfo2.Str = cInfo.Buf;\r
1816                                         cInfo2.StrLen = cInfo.OutLen;\r
1817                                         cInfo2.Buf = Buf3;\r
1818                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1819                                         ConvJIStoSJIS(&cInfo2);\r
1820                                         break;\r
1821                                 case KANJI_JIS:\r
1822                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1823                                         cInfo2.OutLen = cInfo.OutLen;\r
1824                                         break;\r
1825                                 case KANJI_EUC:\r
1826                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1827                                         cInfo2.OutLen = cInfo.OutLen;\r
1828                                         break;\r
1829                                 case KANJI_UTF8N:\r
1830                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1831                                         cInfo2.OutLen = cInfo.OutLen;\r
1832                                         break;\r
1833                                 }\r
1834                                 break;\r
1835                         case KANJI_JIS:\r
1836                                 switch(Pkt->KanjiCodeDesired)\r
1837                                 {\r
1838                                 case KANJI_SJIS:\r
1839                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1840                                         cInfo2.OutLen = cInfo.OutLen;\r
1841                                         break;\r
1842                                 case KANJI_JIS:\r
1843                                         // カナ変換のため\r
1844                                         cInfo2.Str = cInfo.Buf;\r
1845                                         cInfo2.StrLen = cInfo.OutLen;\r
1846                                         cInfo2.Buf = Buf3;\r
1847                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1848                                         ConvSJIStoJIS(&cInfo2);\r
1849                                         break;\r
1850                                 case KANJI_EUC:\r
1851                                         cInfo2.Str = cInfo.Buf;\r
1852                                         cInfo2.StrLen = cInfo.OutLen;\r
1853                                         cInfo2.Buf = Buf3;\r
1854                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1855                                         ConvSJIStoEUC(&cInfo2);\r
1856                                         break;\r
1857                                 case KANJI_UTF8N:\r
1858                                         cInfo2.Str = cInfo.Buf;\r
1859                                         cInfo2.StrLen = cInfo.OutLen;\r
1860                                         cInfo2.Buf = Buf3;\r
1861                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1862                                         ConvSJIStoUTF8N(&cInfo2);\r
1863                                         break;\r
1864                                 }\r
1865                                 break;\r
1866                         case KANJI_EUC:\r
1867                                 switch(Pkt->KanjiCodeDesired)\r
1868                                 {\r
1869                                 case KANJI_SJIS:\r
1870                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1871                                         cInfo2.OutLen = cInfo.OutLen;\r
1872                                         break;\r
1873                                 case KANJI_JIS:\r
1874                                         cInfo2.Str = cInfo.Buf;\r
1875                                         cInfo2.StrLen = cInfo.OutLen;\r
1876                                         cInfo2.Buf = Buf3;\r
1877                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1878                                         ConvSJIStoJIS(&cInfo2);\r
1879                                         break;\r
1880                                 case KANJI_EUC:\r
1881                                         // カナ変換のため\r
1882                                         cInfo2.Str = cInfo.Buf;\r
1883                                         cInfo2.StrLen = cInfo.OutLen;\r
1884                                         cInfo2.Buf = Buf3;\r
1885                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1886                                         ConvSJIStoEUC(&cInfo2);\r
1887                                         break;\r
1888                                 case KANJI_UTF8N:\r
1889                                         cInfo2.Str = cInfo.Buf;\r
1890                                         cInfo2.StrLen = cInfo.OutLen;\r
1891                                         cInfo2.Buf = Buf3;\r
1892                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1893                                         ConvSJIStoUTF8N(&cInfo2);\r
1894                                         break;\r
1895                                 }\r
1896                                 break;\r
1897                         case KANJI_UTF8N:\r
1898                                 switch(Pkt->KanjiCodeDesired)\r
1899                                 {\r
1900                                 case KANJI_SJIS:\r
1901                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1902                                         cInfo2.OutLen = cInfo.OutLen;\r
1903                                         break;\r
1904                                 case KANJI_JIS:\r
1905                                         cInfo2.Str = cInfo.Buf;\r
1906                                         cInfo2.StrLen = cInfo.OutLen;\r
1907                                         cInfo2.Buf = Buf3;\r
1908                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1909                                         ConvSJIStoJIS(&cInfo2);\r
1910                                         break;\r
1911                                 case KANJI_EUC:\r
1912                                         cInfo2.Str = cInfo.Buf;\r
1913                                         cInfo2.StrLen = cInfo.OutLen;\r
1914                                         cInfo2.Buf = Buf3;\r
1915                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1916                                         ConvSJIStoEUC(&cInfo2);\r
1917                                         break;\r
1918                                 case KANJI_UTF8N:\r
1919                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
1920                                         cInfo2.OutLen = cInfo.OutLen;\r
1921                                         break;\r
1922                                 }\r
1923                                 break;\r
1924                         }\r
1925 //                      if(WriteFile(iFileHandle, Buf2, cInfo.OutLen, &Writed, NULL) == FALSE)\r
1926                         if(WriteFile(iFileHandle, Buf3, cInfo2.OutLen, &Writed, NULL) == FALSE)\r
1927                                 Pkt->Abort = ABORT_DISKFULL;\r
1928                         cInfo2.Buf = Buf3;\r
1929                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
1930                         FlushRestData(&cInfo2);\r
1931                         if(WriteFile(iFileHandle, Buf3, cInfo2.OutLen, &Writed, NULL) == FALSE)\r
1932                                 Pkt->Abort = ABORT_DISKFULL;\r
1933                 }\r
1934 \r
1935                 /* グラフ表示を更新 */\r
1936                 if(Pkt->hWndTrans != NULL)\r
1937                 {\r
1938                         KillTimer(Pkt->hWndTrans, TIMER_DISPLAY);\r
1939                         DispTransferStatus(Pkt->hWndTrans, YES, Pkt);\r
1940                         // 同時接続対応\r
1941 //                      TimeStart = time(NULL) - TimeStart + 1;\r
1942                         TimeStart[Pkt->ThreadCount] = time(NULL) - TimeStart[Pkt->ThreadCount] + 1;\r
1943                 }\r
1944                 else\r
1945                 {\r
1946                         /* 転送ダイアログを出さない時の経過表示を消す */\r
1947                         DispDownloadSize(-1);\r
1948                 }\r
1949 \r
1950                 /* ファイルのタイムスタンプを合わせる */\r
1951                 if((SaveTimeStamp == YES) &&\r
1952                    ((Pkt->Time.dwLowDateTime != 0) || (Pkt->Time.dwHighDateTime != 0)))\r
1953                 {\r
1954                         SetFileTime(iFileHandle, &Pkt->Time, &Pkt->Time, &Pkt->Time);\r
1955                 }\r
1956 \r
1957                 CloseHandle(iFileHandle);\r
1958 \r
1959                 if(iNumBytes == SOCKET_ERROR)\r
1960                         ReportWSError("recv",WSAGetLastError());\r
1961         }\r
1962         else\r
1963         {\r
1964                 SetErrorMsg(MSGJPN095, Pkt->LocalFile);\r
1965                 SetTaskMsg(MSGJPN095, Pkt->LocalFile);\r
1966                 Pkt->Abort = ABORT_ERROR;\r
1967         }\r
1968 \r
1969 \r
1970         if(shutdown(dSkt, 1) != 0)\r
1971                 ReportWSError("shutdown", WSAGetLastError());\r
1972 \r
1973         DoClose(dSkt);\r
1974 \r
1975         if(ForceAbort == NO)\r
1976         {\r
1977                 /* Abortをホストに伝える */\r
1978                 if(Pkt->Abort != ABORT_NONE && iFileHandle != INVALID_HANDLE_VALUE)\r
1979                 {\r
1980                         SendData(Pkt->ctrl_skt, "\xFF\xF4\xFF", 3, MSG_OOB, CancelCheckWork);   /* MSG_OOBに注意 */\r
1981                         SendData(Pkt->ctrl_skt, "\xF2", 1, 0, CancelCheckWork);\r
1982                         command(Pkt->ctrl_skt, NULL, CancelCheckWork, "ABOR");\r
1983                 }\r
1984         }\r
1985 \r
1986         iRetCode = ReadReplyMessage(Pkt->ctrl_skt, Buf, 1024, CancelCheckWork, TmpBuf);\r
1987 \r
1988 //#pragma aaa\r
1989 //DoPrintf("##DOWN REPLY : %s", Buf);\r
1990 \r
1991         if(Pkt->Abort == ABORT_DISKFULL)\r
1992         {\r
1993                 SetErrorMsg(MSGJPN096);\r
1994                 SetTaskMsg(MSGJPN096);\r
1995         }\r
1996         if(iRetCode >= FTP_RETRY)\r
1997                 SetErrorMsg(Buf);\r
1998         if(Pkt->Abort != ABORT_NONE)\r
1999                 iRetCode = 500;\r
2000 \r
2001         return(iRetCode);\r
2002 }\r
2003 \r
2004 \r
2005 /*----- ダウンロード終了/中止時のメッセージを表示 ----------------------------\r
2006 *\r
2007 *       Parameter\r
2008 *               TRANSPACKET *Pkt : 転送ファイル情報\r
2009 *               int iRetCode : 応答コード\r
2010 *\r
2011 *       Return Value\r
2012 *               なし\r
2013 *----------------------------------------------------------------------------*/\r
2014 \r
2015 static void DispDownloadFinishMsg(TRANSPACKET *Pkt, int iRetCode)\r
2016 {\r
2017         char Fname[FMAX_PATH+1];\r
2018 \r
2019         if(ForceAbort == NO)\r
2020         {\r
2021                 if((iRetCode/100) >= FTP_CONTINUE)\r
2022                 {\r
2023                         strcpy(Fname, Pkt->RemoteFile);\r
2024 \r
2025 #if defined(HAVE_OPENVMS)\r
2026                         /* OpenVMSの場合、空ディレクトリへ移動すると550 File not foundになって\r
2027                          * エラーダイアログやエラーメッセージが出るので何もしない */\r
2028                         if (AskHostType() == HTYPE_VMS)\r
2029                                 return;\r
2030 #endif\r
2031 \r
2032                         if((strncmp(Pkt->Cmd, "NLST", 4) == 0) || (strncmp(Pkt->Cmd, "LIST", 4) == 0))\r
2033                         {\r
2034                                 SetTaskMsg(MSGJPN097);\r
2035                                 strcpy(Fname, MSGJPN098);\r
2036                         }\r
2037                         // 同時接続対応\r
2038 //                      else if((Pkt->hWndTrans != NULL) && (TimeStart != 0))\r
2039 //                              SetTaskMsg(MSGJPN099, TimeStart, Pkt->ExistSize/TimeStart);\r
2040                         else if((Pkt->hWndTrans != NULL) && (TimeStart[Pkt->ThreadCount] != 0))\r
2041                                 SetTaskMsg(MSGJPN099, TimeStart[Pkt->ThreadCount], Pkt->ExistSize/TimeStart[Pkt->ThreadCount]);\r
2042                         else\r
2043                                 SetTaskMsg(MSGJPN100);\r
2044 \r
2045                         if(Pkt->Abort != ABORT_USER)\r
2046                         {\r
2047                                 // 全て中止を選択後にダイアログが表示されるバグ対策\r
2048 //                              if(DispUpDownErrDialog(downerr_dlg, Pkt->hWndTrans, Fname) == NO)\r
2049                                 if(Canceled[Pkt->ThreadCount] == NO && ClearAll == NO && DispUpDownErrDialog(downerr_dlg, Pkt->hWndTrans, Fname) == NO)\r
2050                                         ClearAll = YES;\r
2051                         }\r
2052                 }\r
2053                 else\r
2054                 {\r
2055                         if((strncmp(Pkt->Cmd, "NLST", 4) == 0) || (strncmp(Pkt->Cmd, "LIST", 4) == 0))\r
2056                                 SetTaskMsg(MSGJPN101, Pkt->ExistSize);\r
2057                         // 同時接続対応\r
2058 //                      else if((Pkt->hWndTrans != NULL) && (TimeStart != 0))\r
2059 //                              SetTaskMsg(MSGJPN102, TimeStart, Pkt->ExistSize/TimeStart);\r
2060                         else if((Pkt->hWndTrans != NULL) && (TimeStart[Pkt->ThreadCount] != 0))\r
2061                                 // "0 B/S"と表示されるバグを修正\r
2062                                 // 原因は%dにあたる部分に64ビット値が渡されているため\r
2063 //                              SetTaskMsg(MSGJPN102, TimeStart[Pkt->ThreadCount], Pkt->ExistSize/TimeStart[Pkt->ThreadCount]);\r
2064                                 SetTaskMsg(MSGJPN102, (LONG)TimeStart[Pkt->ThreadCount], (LONG)(Pkt->ExistSize/TimeStart[Pkt->ThreadCount]));\r
2065                         else\r
2066                                 SetTaskMsg(MSGJPN103, Pkt->ExistSize);\r
2067                 }\r
2068         }\r
2069         return;\r
2070 }\r
2071 \r
2072 \r
2073 /*----- ダウンロード/アップロードエラーのダイアログを表示 --------------------\r
2074 *\r
2075 *       Parameter\r
2076 *               int RedID : ダイアログボックスのリソースID\r
2077 *               HWND hWnd : 書き込み中ダイアログのウインドウ\r
2078 *               char *Fname : ファイル名\r
2079 *\r
2080 *       Return Value\r
2081 *               int ステータス (YES=中止/NO=全て中止)\r
2082 *----------------------------------------------------------------------------*/\r
2083 \r
2084 static int DispUpDownErrDialog(int ResID, HWND hWnd, char *Fname)\r
2085 {\r
2086         if(hWnd == NULL)\r
2087                 hWnd = GetMainHwnd();\r
2088 \r
2089         SoundPlay(SND_ERROR);\r
2090         return(DialogBoxParam(GetFtpInst(), MAKEINTRESOURCE(ResID), hWnd, UpDownErrorDialogProc, (LPARAM)Fname));\r
2091 }\r
2092 \r
2093 \r
2094 /*----- ダウンロードエラー/アップロードエラーダイアログのコールバック --------\r
2095 *\r
2096 *       Parameter\r
2097 *               HWND hDlg : ウインドウハンドル\r
2098 *               UINT message : メッセージ番号\r
2099 *               WPARAM wParam : メッセージの WPARAM 引数\r
2100 *               LPARAM lParam : メッセージの LPARAM 引数\r
2101 *\r
2102 *       Return Value\r
2103 *               BOOL TRUE/FALSE\r
2104 *----------------------------------------------------------------------------*/\r
2105 \r
2106 static BOOL CALLBACK UpDownErrorDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)\r
2107 {\r
2108         switch (message)\r
2109         {\r
2110                 case WM_INITDIALOG :\r
2111                         SendDlgItemMessage(hDlg, UPDOWN_ERR_FNAME, WM_SETTEXT, 0, (LPARAM)lParam);\r
2112                         // 同時接続対応\r
2113 //                      SendDlgItemMessage(hDlg, UPDOWN_ERR_MSG, WM_SETTEXT, 0, (LPARAM)ErrMsg);\r
2114                         SendDlgItemMessage(hDlg, UPDOWN_ERR_MSG, WM_SETTEXT, 0, (LPARAM)GetErrMsg());\r
2115                         return(TRUE);\r
2116 \r
2117                 case WM_COMMAND :\r
2118                         switch(GET_WM_COMMAND_ID(wParam, lParam))\r
2119                         {\r
2120                                 case IDOK :\r
2121                                         EndDialog(hDlg, YES);\r
2122                                         break;\r
2123 \r
2124                                 case IDCANCEL :\r
2125                                         EndDialog(hDlg, NO);\r
2126                                         break;\r
2127                         }\r
2128                         return(TRUE);\r
2129         }\r
2130     return(FALSE);\r
2131 }\r
2132 \r
2133 \r
2134 /*----- ダウンロードのリジュームの準備を行う ----------------------------------\r
2135 *\r
2136 *       Parameter\r
2137 *               TRANSPACKET *Pkt : 転送ファイル情報\r
2138 *               iont ProcMode : 処理モード(EXIST_xxx)\r
2139 *               LONGLONG Size : ロード済みのファイルのサイズ\r
2140 *               int *Mode : ファイル作成モード (CREATE_xxxx)\r
2141 *\r
2142 *       Return Value\r
2143 *               int 転送を行うかどうか(YES/NO=このファイルを中止/NO_ALL=全て中止)\r
2144 *\r
2145 *       Note\r
2146 *               Pkt->ExistSizeのセットを行なう\r
2147 *----------------------------------------------------------------------------*/\r
2148 \r
2149 static int SetDownloadResume(TRANSPACKET *Pkt, int ProcMode, LONGLONG Size, int *Mode, int *CancelCheckWork)\r
2150 {\r
2151         int iRetCode;\r
2152         int Com;\r
2153         char Reply[ERR_MSG_LEN+7];\r
2154         char Tmp[40];\r
2155 \r
2156         Com = YES;\r
2157 \r
2158         Pkt->ExistSize = 0;\r
2159         *Mode = CREATE_ALWAYS;\r
2160 \r
2161         if(ProcMode == EXIST_RESUME)\r
2162         {\r
2163                 iRetCode = command(Pkt->ctrl_skt, Reply, CancelCheckWork, "REST %s", MakeNumString(Size, Tmp, FALSE));\r
2164                 if(iRetCode/100 < FTP_RETRY)\r
2165                 {\r
2166                         /* リジューム */\r
2167                         if(Pkt->hWndTrans != NULL)\r
2168                                 Pkt->ExistSize = Size;\r
2169                         *Mode = OPEN_ALWAYS;\r
2170                 }\r
2171                 else\r
2172                 {\r
2173                         Com = DialogBox(GetFtpInst(), MAKEINTRESOURCE(noresume_dlg), Pkt->hWndTrans, NoResumeWndProc);\r
2174                         if(Com != YES)\r
2175                         {\r
2176                                 if(Com == NO_ALL)               /* 全て中止 */\r
2177                                         ClearAll = YES;\r
2178                                 Pkt->Abort = ABORT_USER;\r
2179                         }\r
2180                 }\r
2181         }\r
2182         return(Com);\r
2183 }\r
2184 \r
2185 \r
2186 /*----- resumeエラーダイアログのコールバック ----------------------------------\r
2187 *\r
2188 *       Parameter\r
2189 *               HWND hDlg : ウインドウハンドル\r
2190 *               UINT message : メッセージ番号\r
2191 *               WPARAM wParam : メッセージの WPARAM 引数\r
2192 *               LPARAM lParam : メッセージの LPARAM 引数\r
2193 *\r
2194 *       Return Value\r
2195 *               BOOL TRUE/FALSE\r
2196 *----------------------------------------------------------------------------*/\r
2197 \r
2198 static BOOL CALLBACK NoResumeWndProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)\r
2199 {\r
2200         switch (iMessage)\r
2201         {\r
2202                 case WM_INITDIALOG :\r
2203                         return(TRUE);\r
2204 \r
2205                 case WM_COMMAND :\r
2206                         switch(GET_WM_COMMAND_ID(wParam, lParam))\r
2207                         {\r
2208                                 case IDOK :\r
2209                                         EndDialog(hDlg, YES);\r
2210                                         break;\r
2211 \r
2212                                 case IDCANCEL :\r
2213                                         EndDialog(hDlg, NO);\r
2214                                         break;\r
2215 \r
2216                                 case RESUME_CANCEL_ALL :\r
2217                                         EndDialog(hDlg, NO_ALL);\r
2218                                         break;\r
2219                         }\r
2220             return(TRUE);\r
2221         }\r
2222         return(FALSE);\r
2223 }\r
2224 \r
2225 \r
2226 \r
2227 /*----- アップロードを行なう --------------------------------------------------\r
2228 *\r
2229 *       Parameter\r
2230 *               SOCKET cSkt : コントロールソケット\r
2231 *               TRANSPACKET *Pkt : 転送ファイル情報\r
2232 *\r
2233 *       Return Value\r
2234 *               int 応答コード\r
2235 *----------------------------------------------------------------------------*/\r
2236 \r
2237 static int DoUpLoad(SOCKET cSkt, TRANSPACKET *Pkt)\r
2238 {\r
2239         int iRetCode;\r
2240         char Reply[ERR_MSG_LEN+7];\r
2241 \r
2242         Pkt->ctrl_skt = cSkt;\r
2243 \r
2244         if(Pkt->Mode != EXIST_IGNORE)\r
2245         {\r
2246                 if(CheckFileReadable(Pkt->LocalFile) == FFFTP_SUCCESS)\r
2247                 {\r
2248                         if(Pkt->Type == TYPE_I)\r
2249                                 Pkt->KanjiCode = KANJI_NOCNV;\r
2250 \r
2251                         // 同時接続対応\r
2252 //                      iRetCode = command(Pkt->ctrl_skt, Reply, &Canceled, "TYPE %c", Pkt->Type);\r
2253                         iRetCode = command(Pkt->ctrl_skt, Reply, &Canceled[Pkt->ThreadCount], "TYPE %c", Pkt->Type);\r
2254                         if(iRetCode/100 < FTP_RETRY)\r
2255                         {\r
2256                                 if(Pkt->Mode == EXIST_UNIQUE)\r
2257                                         strcpy(Pkt->Cmd, "STOU ");\r
2258 \r
2259                                 if(Pkt->hWndTrans != NULL)\r
2260                                         DispTransFileInfo(Pkt, MSGJPN104, TRUE, YES);\r
2261 \r
2262                                 if(BackgrndMessageProc() == NO)\r
2263                                 {\r
2264                                         if(AskPasvMode() != YES)\r
2265                                                 iRetCode = UpLoadNonPassive(Pkt);\r
2266                                         else\r
2267                                                 iRetCode = UpLoadPassive(Pkt);\r
2268                                 }\r
2269                                 else\r
2270                                         iRetCode = 500;\r
2271                         }\r
2272                         else\r
2273                                 SetErrorMsg(Reply);\r
2274 \r
2275                         /* 属性変更 */\r
2276                         if((Pkt->Attr != -1) && ((iRetCode/100) == FTP_COMPLETE))\r
2277                                 // 同時接続対応\r
2278 //                              command(Pkt->ctrl_skt, Reply, &Canceled, "%s %03X %s", AskHostChmodCmd(), Pkt->Attr, Pkt->RemoteFile);\r
2279                                 command(Pkt->ctrl_skt, Reply, &Canceled[Pkt->ThreadCount], "%s %03X %s", AskHostChmodCmd(), Pkt->Attr, Pkt->RemoteFile);\r
2280                 }\r
2281                 else\r
2282                 {\r
2283                         SetErrorMsg(MSGJPN105, Pkt->LocalFile);\r
2284                         SetTaskMsg(MSGJPN105, Pkt->LocalFile);\r
2285                         iRetCode = 500;\r
2286                         Pkt->Abort = ABORT_ERROR;\r
2287                         DispUploadFinishMsg(Pkt, iRetCode);\r
2288                 }\r
2289         }\r
2290         else\r
2291         {\r
2292                 DispTransFileInfo(Pkt, MSGJPN106, TRUE, YES);\r
2293                 SetTaskMsg(MSGJPN107, Pkt->LocalFile);\r
2294                 iRetCode = 200;\r
2295         }\r
2296         return(iRetCode);\r
2297 }\r
2298 \r
2299 \r
2300 /*----- 通常モードでファイルをアップロード ------------------------------------\r
2301 *\r
2302 *       Parameter\r
2303 *               TRANSPACKET *Pkt : 転送ファイル情報\r
2304 *\r
2305 *       Return Value\r
2306 *               int 応答コード\r
2307 *----------------------------------------------------------------------------*/\r
2308 \r
2309 static int UpLoadNonPassive(TRANSPACKET *Pkt)\r
2310 {\r
2311         int iRetCode;\r
2312         int iLength;\r
2313         SOCKET data_socket = INVALID_SOCKET;   // data channel socket\r
2314         SOCKET listen_socket = INVALID_SOCKET; // data listen socket\r
2315         char Buf[1024];\r
2316         struct sockaddr_in saSockAddr1;\r
2317         int Resume;\r
2318         char Reply[ERR_MSG_LEN+7];\r
2319 \r
2320         // 同時接続対応\r
2321 //      if((listen_socket = GetFTPListenSocket(Pkt->ctrl_skt, &Canceled)) != INVALID_SOCKET)\r
2322         if((listen_socket = GetFTPListenSocket(Pkt->ctrl_skt, &Canceled[Pkt->ThreadCount])) != INVALID_SOCKET)\r
2323         {\r
2324                 SetUploadResume(Pkt, Pkt->Mode, Pkt->ExistSize, &Resume);\r
2325                 if(Resume == NO)\r
2326                         sprintf(Buf, "%s%s", Pkt->Cmd, Pkt->RemoteFile);\r
2327                 else\r
2328                         sprintf(Buf, "%s%s", "APPE ", Pkt->RemoteFile);\r
2329 \r
2330                 // 同時接続対応\r
2331 //              iRetCode = command(Pkt->ctrl_skt, Reply, &Canceled, "%s", Buf);\r
2332                 iRetCode = command(Pkt->ctrl_skt, Reply, &Canceled[Pkt->ThreadCount], "%s", Buf);\r
2333                 if((iRetCode/100) == FTP_PRELIM)\r
2334                 {\r
2335                         if(SocksGet2ndBindReply(listen_socket, &data_socket) == FFFTP_FAIL)\r
2336                         {\r
2337                                 iLength=sizeof(saSockAddr1);\r
2338                                 data_socket = do_accept(listen_socket,(struct sockaddr *)&saSockAddr1, (int *)&iLength);\r
2339 \r
2340                                 if(shutdown(listen_socket, 1) != 0)\r
2341                                         ReportWSError("shutdown listen", WSAGetLastError());\r
2342                                 listen_socket = DoClose(listen_socket);\r
2343 \r
2344                                 if(data_socket == INVALID_SOCKET)\r
2345                                 {\r
2346                                         SetErrorMsg(MSGJPN280);\r
2347                                         ReportWSError("accept", WSAGetLastError());\r
2348                                         iRetCode = 500;\r
2349                                 }\r
2350                                 else\r
2351                                         DoPrintf("Skt=%u : accept from %s port %u", data_socket, inet_ntoa(saSockAddr1.sin_addr), ntohs(saSockAddr1.sin_port));\r
2352                         }\r
2353 \r
2354                         if(data_socket != INVALID_SOCKET)\r
2355                         {\r
2356                                 // 一部TYPE、STOR(RETR)、PORT(PASV)を並列に処理できないホストがあるため\r
2357                                 ReleaseMutex(hListAccMutex);\r
2358                                 // FTPS対応\r
2359 //                              iRetCode = UpLoadFile(Pkt, data_socket);\r
2360                                 if(IsSSLAttached(Pkt->ctrl_skt))\r
2361                                 {\r
2362                                         if(AttachSSL(data_socket, Pkt->ctrl_skt))\r
2363                                                 iRetCode = UpLoadFile(Pkt, data_socket);\r
2364                                         else\r
2365                                                 iRetCode = 500;\r
2366                                 }\r
2367                                 else\r
2368                                         iRetCode = UpLoadFile(Pkt, data_socket);\r
2369                                 data_socket = DoClose(data_socket);\r
2370                         }\r
2371                 }\r
2372                 else\r
2373                 {\r
2374                         SetErrorMsg(Reply);\r
2375                         SetTaskMsg(MSGJPN108);\r
2376                         listen_socket = DoClose(listen_socket);\r
2377                         iRetCode = 500;\r
2378                 }\r
2379         }\r
2380         else\r
2381         {\r
2382                 SetErrorMsg(MSGJPN279);\r
2383                 iRetCode = 500;\r
2384         }\r
2385         DispUploadFinishMsg(Pkt, iRetCode);\r
2386 \r
2387         return(iRetCode);\r
2388 }\r
2389 \r
2390 \r
2391 /*----- Passiveモードでファイルをアップロード ---------------------------------\r
2392 *\r
2393 *       Parameter\r
2394 *               TRANSPACKET *Pkt : 転送ファイル情報\r
2395 *\r
2396 *       Return Value\r
2397 *               int 応答コード\r
2398 *----------------------------------------------------------------------------*/\r
2399 \r
2400 static int UpLoadPassive(TRANSPACKET *Pkt)\r
2401 {\r
2402         int iRetCode;\r
2403         SOCKET data_socket = INVALID_SOCKET;   // data channel socket\r
2404         char Buf[1024];\r
2405         char Adrs[20];\r
2406         int Port;\r
2407         int Flg;\r
2408         int Resume;\r
2409         char Reply[ERR_MSG_LEN+7];\r
2410 \r
2411         // 同時接続対応\r
2412 //      iRetCode = command(Pkt->ctrl_skt, Buf, &Canceled, "PASV");\r
2413         iRetCode = command(Pkt->ctrl_skt, Buf, &Canceled[Pkt->ThreadCount], "PASV");\r
2414         if(iRetCode/100 == FTP_COMPLETE)\r
2415         {\r
2416                 if(GetAdrsAndPort(Buf, Adrs, &Port, 19) == FFFTP_SUCCESS)\r
2417                 {\r
2418                         // 同時接続対応\r
2419 //                      if((data_socket = connectsock(Adrs, Port, MSGJPN109, &Canceled)) != INVALID_SOCKET)\r
2420                         if((data_socket = connectsock(Adrs, Port, MSGJPN109, &Canceled[Pkt->ThreadCount])) != INVALID_SOCKET)\r
2421                         {\r
2422                                 // 変数が未初期化のバグ修正\r
2423                                 Flg = 1;\r
2424                                 if(setsockopt(data_socket, IPPROTO_TCP, TCP_NODELAY, (LPSTR)&Flg, sizeof(Flg)) == SOCKET_ERROR)\r
2425                                         ReportWSError("setsockopt", WSAGetLastError());\r
2426 \r
2427                                 SetUploadResume(Pkt, Pkt->Mode, Pkt->ExistSize, &Resume);\r
2428                                 if(Resume == NO)\r
2429                                         sprintf(Buf, "%s%s", Pkt->Cmd, Pkt->RemoteFile);\r
2430                                 else\r
2431                                         sprintf(Buf, "%s%s", "APPE ", Pkt->RemoteFile);\r
2432 \r
2433                                 // 同時接続対応\r
2434 //                              iRetCode = command(Pkt->ctrl_skt, Reply, &Canceled, "%s", Buf);\r
2435                                 iRetCode = command(Pkt->ctrl_skt, Reply, &Canceled[Pkt->ThreadCount], "%s", Buf);\r
2436                                 if(iRetCode/100 == FTP_PRELIM)\r
2437                                 {\r
2438                                         // 一部TYPE、STOR(RETR)、PORT(PASV)を並列に処理できないホストがあるため\r
2439                                         ReleaseMutex(hListAccMutex);\r
2440                                         // FTPS対応\r
2441 //                                      iRetCode = UpLoadFile(Pkt, data_socket);\r
2442                                         if(IsSSLAttached(Pkt->ctrl_skt))\r
2443                                         {\r
2444                                                 if(AttachSSL(data_socket, Pkt->ctrl_skt))\r
2445                                                         iRetCode = UpLoadFile(Pkt, data_socket);\r
2446                                                 else\r
2447                                                         iRetCode = 500;\r
2448                                         }\r
2449                                         else\r
2450                                                 iRetCode = UpLoadFile(Pkt, data_socket);\r
2451 \r
2452                                         data_socket = DoClose(data_socket);\r
2453                                 }\r
2454                                 else\r
2455                                 {\r
2456                                         SetErrorMsg(Reply);\r
2457                                         SetTaskMsg(MSGJPN110);\r
2458                                         data_socket = DoClose(data_socket);\r
2459                                         iRetCode = 500;\r
2460                                 }\r
2461                         }\r
2462                         else\r
2463                         {\r
2464                                 SetErrorMsg(MSGJPN281);\r
2465                                 iRetCode = 500;\r
2466                         }\r
2467                 }\r
2468                 else\r
2469                 {\r
2470                         SetErrorMsg(Buf);\r
2471                         SetTaskMsg(MSGJPN111);\r
2472                         iRetCode = 500;\r
2473                 }\r
2474         }\r
2475         else\r
2476                 SetErrorMsg(Buf);\r
2477 \r
2478         DispUploadFinishMsg(Pkt, iRetCode);\r
2479 \r
2480         return(iRetCode);\r
2481 }\r
2482 \r
2483 \r
2484 /*----- アップロードの実行 ----------------------------------------------------\r
2485 *\r
2486 *       Parameter\r
2487 *               TRANSPACKET *Pkt : 転送ファイル情報\r
2488 *               SOCKET dSkt : データソケット\r
2489 *\r
2490 *       Return Value\r
2491 *               int 応答コード\r
2492 *\r
2493 *       Note\r
2494 *               転送の経過表示は、インターバルタイマで経過を表示する\r
2495 *               転送ダイアログを出さないでアップロードすることはない\r
2496 *----------------------------------------------------------------------------*/\r
2497 \r
2498 static int UpLoadFile(TRANSPACKET *Pkt, SOCKET dSkt)\r
2499 {\r
2500         DWORD iNumBytes;\r
2501         HANDLE iFileHandle;\r
2502         SECURITY_ATTRIBUTES Sec;\r
2503         char Buf[BUFSIZE];\r
2504         char Buf2[BUFSIZE+3];\r
2505         CODECONVINFO cInfo;\r
2506         TERMCODECONVINFO tInfo;\r
2507         int Continue;\r
2508         char *EofPos;\r
2509         int iRetCode;\r
2510 #if 0\r
2511         int TimeOutErr;\r
2512 #endif\r
2513         char TmpBuf[ONELINE_BUF_SIZE];\r
2514         DWORD Low;\r
2515         DWORD High;\r
2516 \r
2517 #ifdef SET_BUFFER_SIZE\r
2518 /* Add by H.Shirouzu at 2002/10/02 */\r
2519         int buf_size = SOCKBUF_SIZE;\r
2520         for ( ; buf_size > 0; buf_size /= 2)\r
2521                 if (setsockopt(dSkt, SOL_SOCKET, SO_SNDBUF, (char *)&buf_size, sizeof(buf_size)) == 0)\r
2522                         break;\r
2523 /* End */\r
2524 #endif\r
2525 \r
2526         // 念のため送信バッファを無効にする\r
2527 #ifdef DISABLE_TRANSFER_NETWORK_BUFFERS\r
2528         int buf_size = 0;\r
2529         setsockopt(dSkt, SOL_SOCKET, SO_SNDBUF, (char *)&buf_size, sizeof(buf_size));\r
2530 #endif\r
2531 \r
2532         Pkt->Abort = ABORT_NONE;\r
2533 \r
2534         Sec.nLength = sizeof(SECURITY_ATTRIBUTES);\r
2535         Sec.lpSecurityDescriptor = NULL;\r
2536         Sec.bInheritHandle = FALSE;\r
2537 \r
2538         if((iFileHandle = CreateFile(Pkt->LocalFile, GENERIC_READ,\r
2539                 FILE_SHARE_READ|FILE_SHARE_WRITE, &Sec, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE)\r
2540         {\r
2541                 // UTF-8対応\r
2542                 char Buf3[(BUFSIZE + 3) * 4];\r
2543                 CODECONVINFO cInfo2;\r
2544                 int ProcessedBOM = NO;\r
2545                 if(Pkt->hWndTrans != NULL)\r
2546                 {\r
2547                         Low = GetFileSize(iFileHandle, &High);\r
2548                         Pkt->Size = MakeLongLong(High, Low);\r
2549 \r
2550                         High = (DWORD)HIGH32(Pkt->ExistSize);\r
2551                         Low = (DWORD)LOW32(Pkt->ExistSize);\r
2552                         SetFilePointer(iFileHandle, Low, &High, FILE_BEGIN);\r
2553 \r
2554                         // 同時接続対応\r
2555 //                      AllTransSizeNow = 0;\r
2556 //                      TimeStart = time(NULL);\r
2557                         AllTransSizeNow[Pkt->ThreadCount] = 0;\r
2558                         TimeStart[Pkt->ThreadCount] = time(NULL);\r
2559                         SetTimer(Pkt->hWndTrans, TIMER_DISPLAY, DISPLAY_TIMING, NULL);\r
2560                 }\r
2561 \r
2562                 InitCodeConvInfo(&cInfo);\r
2563                 cInfo.KanaCnv = Pkt->KanaCnv;\r
2564                 InitTermCodeConvInfo(&tInfo);\r
2565 \r
2566                 InitCodeConvInfo(&cInfo2);\r
2567                 cInfo2.KanaCnv = Pkt->KanaCnv;\r
2568 \r
2569                 /*===== ファイルを送信するループ =====*/\r
2570                 while((Pkt->Abort == ABORT_NONE) &&\r
2571                           (ForceAbort == NO) &&\r
2572                           (ReadFile(iFileHandle, Buf, BUFSIZE, &iNumBytes, NULL) == TRUE))\r
2573                 {\r
2574                         if(iNumBytes == 0)\r
2575                                 break;\r
2576 \r
2577                         /* EOF除去 */\r
2578                         EofPos = NULL;\r
2579                         if((RmEOF == YES) && (Pkt->Type == TYPE_A))\r
2580                         {\r
2581                                 if((EofPos = memchr(Buf, 0x1A, iNumBytes)) != NULL)\r
2582                                         iNumBytes = EofPos - Buf;\r
2583                         }\r
2584 \r
2585                         /* 漢字コード変換 */\r
2586                         if(Pkt->KanjiCode != KANJI_NOCNV)\r
2587                         {\r
2588                                 cInfo.Str = Buf;\r
2589                                 cInfo.StrLen = iNumBytes;\r
2590                                 cInfo.Buf = Buf2;\r
2591                                 cInfo.BufSize = BUFSIZE+3;\r
2592                                 do\r
2593                                 {\r
2594                                         // ここで全てUTF-8へ変換する\r
2595                                         // TODO: SJIS以外も直接UTF-8へ変換\r
2596 //                                      if(Pkt->KanjiCode == KANJI_JIS)\r
2597 //                                              Continue = ConvSJIStoJIS(&cInfo);\r
2598 //                                      else\r
2599 //                                              Continue = ConvSJIStoEUC(&cInfo);\r
2600                                         switch(Pkt->KanjiCodeDesired)\r
2601                                         {\r
2602                                         case KANJI_SJIS:\r
2603                                                 switch(Pkt->KanjiCode)\r
2604                                                 {\r
2605                                                 case KANJI_SJIS:\r
2606 //                                                      memcpy(Buf3, cInfo.Str, cInfo.StrLen);\r
2607 //                                                      cInfo2.OutLen = cInfo.StrLen;\r
2608 //                                                      Continue = NO;\r
2609                                                         // カナ変換のため\r
2610                                                         Continue = ConvSJIStoJIS(&cInfo);\r
2611                                                         cInfo2.Str = cInfo.Buf;\r
2612                                                         cInfo2.StrLen = cInfo.OutLen;\r
2613                                                         cInfo2.Buf = Buf3;\r
2614                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2615                                                         ConvJIStoSJIS(&cInfo2);\r
2616                                                         break;\r
2617                                                 case KANJI_JIS:\r
2618                                                         Continue = ConvSJIStoJIS(&cInfo);\r
2619                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2620                                                         cInfo2.OutLen = cInfo.OutLen;\r
2621                                                         break;\r
2622                                                 case KANJI_EUC:\r
2623                                                         Continue = ConvSJIStoEUC(&cInfo);\r
2624                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2625                                                         cInfo2.OutLen = cInfo.OutLen;\r
2626                                                         break;\r
2627                                                 case KANJI_UTF8N:\r
2628                                                         if(ProcessedBOM == NO)\r
2629                                                         {\r
2630                                                                 memcpy(Buf3, "\xEF\xBB\xBF", 3);\r
2631                                                                 cInfo2.OutLen = 3;\r
2632                                                                 Continue = YES;\r
2633                                                                 ProcessedBOM = YES;\r
2634                                                                 break;\r
2635                                                         }\r
2636                                                         Continue = ConvSJIStoUTF8N(&cInfo);\r
2637                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2638                                                         cInfo2.OutLen = cInfo.OutLen;\r
2639                                                         break;\r
2640                                                 }\r
2641                                                 break;\r
2642                                         case KANJI_JIS:\r
2643                                                 switch(Pkt->KanjiCode)\r
2644                                                 {\r
2645                                                 case KANJI_SJIS:\r
2646                                                         Continue = ConvJIStoSJIS(&cInfo);\r
2647                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2648                                                         cInfo2.OutLen = cInfo.OutLen;\r
2649                                                         break;\r
2650                                                 case KANJI_JIS:\r
2651 //                                                      memcpy(Buf3, cInfo.Str, cInfo.StrLen);\r
2652 //                                                      cInfo2.OutLen = cInfo.StrLen;\r
2653 //                                                      Continue = NO;\r
2654                                                         // カナ変換のため\r
2655                                                         Continue = ConvJIStoSJIS(&cInfo);\r
2656                                                         cInfo2.Str = cInfo.Buf;\r
2657                                                         cInfo2.StrLen = cInfo.OutLen;\r
2658                                                         cInfo2.Buf = Buf3;\r
2659                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2660                                                         ConvSJIStoJIS(&cInfo2);\r
2661                                                         break;\r
2662                                                 case KANJI_EUC:\r
2663                                                         Continue = ConvJIStoSJIS(&cInfo);\r
2664                                                         cInfo2.Str = cInfo.Buf;\r
2665                                                         cInfo2.StrLen = cInfo.OutLen;\r
2666                                                         cInfo2.Buf = Buf3;\r
2667                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2668                                                         ConvSJIStoEUC(&cInfo2);\r
2669                                                         break;\r
2670                                                 case KANJI_UTF8N:\r
2671                                                         if(ProcessedBOM == NO)\r
2672                                                         {\r
2673                                                                 memcpy(Buf3, "\xEF\xBB\xBF", 3);\r
2674                                                                 cInfo2.OutLen = 3;\r
2675                                                                 Continue = YES;\r
2676                                                                 ProcessedBOM = YES;\r
2677                                                                 break;\r
2678                                                         }\r
2679                                                         Continue = ConvJIStoSJIS(&cInfo);\r
2680                                                         cInfo2.Str = cInfo.Buf;\r
2681                                                         cInfo2.StrLen = cInfo.OutLen;\r
2682                                                         cInfo2.Buf = Buf3;\r
2683                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2684                                                         ConvSJIStoUTF8N(&cInfo2);\r
2685                                                         break;\r
2686                                                 }\r
2687                                                 break;\r
2688                                         case KANJI_EUC:\r
2689                                                 switch(Pkt->KanjiCode)\r
2690                                                 {\r
2691                                                 case KANJI_SJIS:\r
2692                                                         Continue = ConvEUCtoSJIS(&cInfo);\r
2693                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2694                                                         cInfo2.OutLen = cInfo.OutLen;\r
2695                                                         break;\r
2696                                                 case KANJI_JIS:\r
2697                                                         Continue = ConvEUCtoSJIS(&cInfo);\r
2698                                                         cInfo2.Str = cInfo.Buf;\r
2699                                                         cInfo2.StrLen = cInfo.OutLen;\r
2700                                                         cInfo2.Buf = Buf3;\r
2701                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2702                                                         ConvSJIStoJIS(&cInfo2);\r
2703                                                         break;\r
2704                                                 case KANJI_EUC:\r
2705 //                                                      memcpy(Buf3, cInfo.Str, cInfo.StrLen);\r
2706 //                                                      cInfo2.OutLen = cInfo.StrLen;\r
2707 //                                                      Continue = NO;\r
2708                                                         // カナ変換のため\r
2709                                                         Continue = ConvEUCtoSJIS(&cInfo);\r
2710                                                         cInfo2.Str = cInfo.Buf;\r
2711                                                         cInfo2.StrLen = cInfo.OutLen;\r
2712                                                         cInfo2.Buf = Buf3;\r
2713                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2714                                                         ConvSJIStoEUC(&cInfo2);\r
2715                                                         break;\r
2716                                                 case KANJI_UTF8N:\r
2717                                                         if(ProcessedBOM == NO)\r
2718                                                         {\r
2719                                                                 memcpy(Buf3, "\xEF\xBB\xBF", 3);\r
2720                                                                 cInfo2.OutLen = 3;\r
2721                                                                 Continue = YES;\r
2722                                                                 ProcessedBOM = YES;\r
2723                                                                 break;\r
2724                                                         }\r
2725                                                         Continue = ConvEUCtoSJIS(&cInfo);\r
2726                                                         cInfo2.Str = cInfo.Buf;\r
2727                                                         cInfo2.StrLen = cInfo.OutLen;\r
2728                                                         cInfo2.Buf = Buf3;\r
2729                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2730                                                         ConvSJIStoUTF8N(&cInfo2);\r
2731                                                         break;\r
2732                                                 }\r
2733                                                 break;\r
2734                                         case KANJI_UTF8N:\r
2735                                                 if(ProcessedBOM == NO)\r
2736                                                 {\r
2737                                                         if(memcmp(Buf, "\xEF\xBB\xBF", 3) == 0)\r
2738                                                         {\r
2739                                                                 cInfo.Str += 3;\r
2740                                                                 cInfo.StrLen -= 3;\r
2741                                                         }\r
2742                                                         cInfo2.OutLen = 0;\r
2743                                                         switch(Pkt->KanjiCode)\r
2744                                                         {\r
2745                                                         case KANJI_UTF8N:\r
2746                                                                 memcpy(Buf3, "\xEF\xBB\xBF", 3);\r
2747                                                                 cInfo2.OutLen = 3;\r
2748                                                                 break;\r
2749                                                         }\r
2750                                                         Continue = YES;\r
2751                                                         ProcessedBOM = YES;\r
2752                                                         break;\r
2753                                                 }\r
2754                                                 switch(Pkt->KanjiCode)\r
2755                                                 {\r
2756                                                 case KANJI_SJIS:\r
2757                                                         Continue = ConvUTF8NtoSJIS(&cInfo);\r
2758                                                         memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2759                                                         cInfo2.OutLen = cInfo.OutLen;\r
2760                                                         break;\r
2761                                                 case KANJI_JIS:\r
2762                                                         Continue = ConvUTF8NtoSJIS(&cInfo);\r
2763                                                         cInfo2.Str = cInfo.Buf;\r
2764                                                         cInfo2.StrLen = cInfo.OutLen;\r
2765                                                         cInfo2.Buf = Buf3;\r
2766                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2767                                                         ConvSJIStoJIS(&cInfo2);\r
2768                                                         break;\r
2769                                                 case KANJI_EUC:\r
2770                                                         Continue = ConvUTF8NtoSJIS(&cInfo);\r
2771                                                         cInfo2.Str = cInfo.Buf;\r
2772                                                         cInfo2.StrLen = cInfo.OutLen;\r
2773                                                         cInfo2.Buf = Buf3;\r
2774                                                         cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2775                                                         ConvSJIStoEUC(&cInfo2);\r
2776                                                         break;\r
2777                                                 case KANJI_UTF8N:\r
2778                                                         memcpy(Buf3, cInfo.Str, cInfo.StrLen);\r
2779                                                         cInfo2.OutLen = cInfo.StrLen;\r
2780                                                         Continue = NO;\r
2781                                                         break;\r
2782                                                 }\r
2783                                                 break;\r
2784                                         }\r
2785 \r
2786 //                                      if(TermCodeConvAndSend(&tInfo, dSkt, Buf2, cInfo.OutLen, Pkt->Type) == FFFTP_FAIL)\r
2787                                         if(TermCodeConvAndSend(Pkt->ThreadCount, &tInfo, dSkt, Buf3, cInfo2.OutLen, Pkt->Type) == FFFTP_FAIL)\r
2788                                         {\r
2789                                                 Pkt->Abort = ABORT_ERROR;\r
2790                                                         break;\r
2791                                         }\r
2792                                 }\r
2793                                 while(Continue == YES);\r
2794                         }\r
2795                         else\r
2796                         {\r
2797                                 // 同時接続対応\r
2798 //                              if(TermCodeConvAndSend(&tInfo, dSkt, Buf, iNumBytes, Pkt->Type) == FFFTP_FAIL)\r
2799                                 if(TermCodeConvAndSend(Pkt->ThreadCount, &tInfo, dSkt, Buf, iNumBytes, Pkt->Type) == FFFTP_FAIL)\r
2800                                         Pkt->Abort = ABORT_ERROR;\r
2801                         }\r
2802 \r
2803                         Pkt->ExistSize += iNumBytes;\r
2804                         if(Pkt->hWndTrans != NULL)\r
2805                                 // 同時接続対応\r
2806 //                              AllTransSizeNow += iNumBytes;\r
2807                                 AllTransSizeNow[Pkt->ThreadCount] += iNumBytes;\r
2808 \r
2809                         if(BackgrndMessageProc() == YES)\r
2810                                 ForceAbort = YES;\r
2811 \r
2812                         if(EofPos != NULL)\r
2813                                 break;\r
2814                 }\r
2815 \r
2816                 if((ForceAbort == NO) && (Pkt->Abort == ABORT_NONE))\r
2817                 {\r
2818                         /* 送り残したデータを送信 */\r
2819                         if(Pkt->KanjiCode != KANJI_NOCNV)\r
2820                         {\r
2821                                 cInfo.Buf = Buf2;\r
2822                                 cInfo.BufSize = BUFSIZE+3;\r
2823                                 FlushRestData(&cInfo);\r
2824                                 switch(Pkt->KanjiCodeDesired)\r
2825                                 {\r
2826                                 case KANJI_SJIS:\r
2827                                         switch(Pkt->KanjiCode)\r
2828                                         {\r
2829                                         case KANJI_SJIS:\r
2830                                                 // カナ変換のため\r
2831                                                 cInfo2.Str = cInfo.Buf;\r
2832                                                 cInfo2.StrLen = cInfo.OutLen;\r
2833                                                 cInfo2.Buf = Buf3;\r
2834                                                 cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2835                                                 ConvJIStoSJIS(&cInfo2);\r
2836                                                 break;\r
2837                                         case KANJI_JIS:\r
2838                                                 memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2839                                                 cInfo2.OutLen = cInfo.OutLen;\r
2840                                                 break;\r
2841                                         case KANJI_EUC:\r
2842                                                 memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2843                                                 cInfo2.OutLen = cInfo.OutLen;\r
2844                                                 break;\r
2845                                         case KANJI_UTF8N:\r
2846                                                 memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2847                                                 cInfo2.OutLen = cInfo.OutLen;\r
2848                                                 break;\r
2849                                         }\r
2850                                         break;\r
2851                                 case KANJI_JIS:\r
2852                                         switch(Pkt->KanjiCode)\r
2853                                         {\r
2854                                         case KANJI_SJIS:\r
2855                                                 memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2856                                                 cInfo2.OutLen = cInfo.OutLen;\r
2857                                                 break;\r
2858                                         case KANJI_JIS:\r
2859                                                 // カナ変換のため\r
2860                                                 cInfo2.Str = cInfo.Buf;\r
2861                                                 cInfo2.StrLen = cInfo.OutLen;\r
2862                                                 cInfo2.Buf = Buf3;\r
2863                                                 cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2864                                                 ConvSJIStoJIS(&cInfo2);\r
2865                                                 break;\r
2866                                         case KANJI_EUC:\r
2867                                                 cInfo2.Str = cInfo.Buf;\r
2868                                                 cInfo2.StrLen = cInfo.OutLen;\r
2869                                                 cInfo2.Buf = Buf3;\r
2870                                                 cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2871                                                 ConvSJIStoEUC(&cInfo2);\r
2872                                                 break;\r
2873                                         case KANJI_UTF8N:\r
2874                                                 cInfo2.Str = cInfo.Buf;\r
2875                                                 cInfo2.StrLen = cInfo.OutLen;\r
2876                                                 cInfo2.Buf = Buf3;\r
2877                                                 cInfo2.BufSize = (BUFSIZE + 3) * 4;\r
2878                                                 ConvSJIStoUTF8N(&cInfo2);\r
2879                                                 break;\r
2880                                         }\r
2881                                         break;\r
2882                                 case KANJI_EUC:\r
2883                                         switch(Pkt->KanjiCode)\r
2884                                         {\r
2885                                         case KANJI_SJIS:\r
2886                                                 memcpy(Buf3, cInfo.Buf, cInfo.OutLen);\r
2887                                                 cInfo2.OutLen = cInfo.OutLen;\r