OSDN Git Service

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