fsInternetResult fsFtpFiles::BuildList() { fsInternetResult ir = IR_SUCCESS; WIN32_FIND_DATA wfd; m_vFiles.clear (); if (m_bAbort) return IR_S_FALSE; HINTERNET hFind = FtpFindFirstFile (m_pServer->GetHandle (), NULL, &wfd, m_bReload ? INTERNET_FLAG_RELOAD : 0, NULL); if (hFind) { do { fsFileInfo file; if (_tcscmp (wfd.cFileName, _T(".")) == 0 || _tcscmp (wfd.cFileName, _T("..")) == 0) continue; file.strName = wfd.cFileName; file.uSize = wfd.nFileSizeLow; file.date = wfd.ftLastWriteTime; file.bAvailable = TRUE; file.bFolder = wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; if (file.strName == NULL) { InternetCloseHandle (hFind); return IR_OUTOFMEMORY; } m_vFiles.add (file); } while (InternetFindNextFile (hFind, &wfd) && m_bAbort == FALSE); if (::GetLastError () != ERROR_NO_MORE_FILES && m_bAbort == FALSE) ir = fsWinInetErrorToIR (); InternetCloseHandle (hFind); } if (m_bAbort) m_vFiles.clear (); return m_bAbort ? IR_S_FALSE : ir; }
bool FtpClient::NextFile(FtpFileInformation & info) { if (m_hFind == 0) return false; if (InternetFindNextFile(m_hFind, &m_findData) == FALSE) { CloseFind(); return false; } UpdateFileInformation(info); return true; }
bool Downloader::scanFtpDir(FtpDir *ftpDir, tstring destsubdir) { Url url(ftpDir->url); url.internetOptions = internetOptions; updateFileName(url.components.lpszUrlPath); if(!url.connect(internet)) { storeError(); return false; } if(!FtpSetCurrentDirectory(url.connection, url.components.lpszUrlPath)) { storeError(); return false; } list<tstring> dirs; WIN32_FIND_DATA fd; TRACE(_T("Scanning FTP dir %s:"), ftpDir->url.c_str()); HINTERNET handle = FtpFindFirstFile(url.connection, ftpDir->mask.c_str(), &fd, NULL, NULL); if(handle) { TRACE(_T(" (%s) %s"), (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? _T("D") : _T("F"), fd.cFileName); updateFileName(tstring(fd.cFileName)); if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { tstring dirname(fd.cFileName); if(!(dirname.compare(_T(".")) == 0) && !(dirname.compare(_T("..")) == 0)) dirs.push_back(dirname); } else { tstring fileUrl = addslash(ftpDir->url); tstring fileName = addbackslash(ftpDir->destdir); fileUrl += tstring(fd.cFileName); fileName += addbackslash(destsubdir); fileName += tstring(fd.cFileName); addFile(fileUrl, fileName, ((DWORDLONG)fd.nFileSizeHigh << 32) | fd.nFileSizeLow, ftpDir->compstr); } while(InternetFindNextFile(handle, &fd)) { TRACE(_T(" (%s) %s"), (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? _T("D") : _T("F"), fd.cFileName); updateFileName(tstring(fd.cFileName)); if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { tstring dirname(fd.cFileName); if(!(dirname.compare(_T(".")) == 0) && !(dirname.compare(_T("..")) == 0)) dirs.push_back(dirname); } else { tstring fileUrl = addslash(ftpDir->url); tstring fileName = addbackslash(ftpDir->destdir); fileUrl += tstring(fd.cFileName); fileName += addbackslash(destsubdir); fileName += tstring(fd.cFileName); addFile(fileUrl, fileName, ((DWORDLONG)fd.nFileSizeHigh << 32) | fd.nFileSizeLow, ftpDir->compstr); } } } url.disconnect(); if(ftpDir->recursive && !dirs.empty()) { for(list<tstring>::iterator i = dirs.begin(); i != dirs.end(); i++) { tstring dir = *i; tstring urlstr = addslash(ftpDir->url); urlstr += dir; FtpDir fdir(urlstr, ftpDir->mask, ftpDir->destdir, ftpDir->recursive, ftpDir->compstr); if(preserveFtpDirs) { tstring destdir(addbackslash(ftpDir->destdir)); destdir += addbackslash(destsubdir); destdir += dir; TRACE(_T("Creating directory %s"), destdir.c_str()); _tmkdir(destdir.c_str()); tstring subdir = addbackslash(destsubdir); subdir += dir; scanFtpDir(&fdir, subdir); } else scanFtpDir(&fdir); } } return true; }
void CSiteDir::ScanFTPFiles(BOOL bCheckExisting/* = FALSE*/) { HINTERNET hFindFile; WIN32_FIND_DATA wfd; TCHAR search[MAX_PATH]; wsprintf(search, "*.*", m_pathName); if (bCheckExisting) { // Assume that all files have been deleted UPOSITION pos = m_childList.GetHeadPosition(); while (pos) { CSiteItem* pItem = (CSiteItem*)m_childList.GetNext(pos); pItem->m_bFoundOnDisk = FALSE; } } if ((hFindFile = FtpFindFirstFile( m_pWebSite->m_hInternetFtp, search, &wfd, INTERNET_FLAG_RELOAD, 0/*dwContext*/)) != NULL/*INVALID_HANDLE_VALUE*/) { do { if (wfd.cFileName[0] != '.') { CSiteItem* pExistingItem = NULL; if (bCheckExisting) { pExistingItem = FileExists(wfd.cFileName, (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)? 1: 2); if (pExistingItem) { pExistingItem->m_bFoundOnDisk = TRUE; } } if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { CSiteDir* pDir; if (pExistingItem) { pDir = (CSiteDir*)pExistingItem; } else { pDir = new CSiteDir; pDir->m_pWebSite = m_pWebSite; _makepath(pDir->m_pathName.GetBuffer(512), NULL, m_pathName, wfd.cFileName, NULL); pDir->m_pathName.ReleaseBuffer(); // pDir->m_name = wfd.cFileName; pDir->m_wfd = wfd; pDir->m_bFoundOnDisk = TRUE; AddChildTail(pDir); pDir->AddSiteItemToDatabase(); } // pDir->ScanFiles(bCheckExisting); } else { if (pExistingItem) { CSiteFile* pFile = (CSiteFile*)pExistingItem; if (pFile->m_wfd.nFileSizeLow != wfd.nFileSizeLow || pFile->m_wfd.nFileSizeHigh != wfd.nFileSizeHigh || pFile->m_wfd.dwFileAttributes != wfd.dwFileAttributes || memcmp(&pFile->m_wfd.ftLastWriteTime, &wfd.ftLastWriteTime, sizeof(FILETIME)) || memcmp(&pFile->m_wfd.ftCreationTime, &wfd.ftCreationTime, sizeof(FILETIME))) { pFile->m_wfd = wfd; } } else { CSiteFile* pFile = new CSiteFile; pFile->m_pWebSite = m_pWebSite; pFile->m_bFoundOnDisk = TRUE; // pFile->m_name = wfd.cFileName; // ?? pFile->m_wfd = wfd; AddChildTail(pFile); } } } } while (InternetFindNextFile(hFindFile, &wfd)); InternetCloseHandle(hFindFile); } // DWORD lastError = GetLastError(); // if (lastError) WinError(lastError); if (bCheckExisting) { // Traverse all items, and remove the files that no longer exist on disk UPOSITION pos = m_childList.GetHeadPosition(); while (pos) { UPOSITION pos2 = pos; CSiteItem* pItem = (CSiteItem*)m_childList.GetNext(pos); if (!pItem->m_bFoundOnDisk) { // if (*pActive == pItem) *pActive = NULL; //pItem->RemoveFromDatabase(); pItem->m_parent->m_childList.RemoveAt(pos2); delete pItem; } } } }
int CzHttp::Exec( void ) { // 毎フレーム実行 // static char hdr[] = "Content-Type: application/x-www-form-urlencoded\r\n"; char req_name[1024]; char *name; BOOL res; switch( mode ) { case CZHTTP_MODE_REQUEST: // httpに接続 strcpy( req_name, req_url ); strcat( req_name, req_path ); hService = InternetOpenUrl( hSession, req_name, req_header, -1L, 0, INTERNET_FLAG_RELOAD ); if ( hService == NULL ) { SetError( "無効なURLが指定されました" ); break; } mode = CZHTTP_MODE_REQSEND; case CZHTTP_MODE_REQSEND: name = down_path; if ( name[0] == 0 ) name = req_path; fp = fopen( name, "wb"); if ( fp == NULL ) { SetError( "ダウンロードファイルが作成できません" ); break; } size = 0; mode = CZHTTP_MODE_DATAWAIT; case CZHTTP_MODE_DATAWAIT: { DWORD dwBytesRead = INETBUF_MAX; if ( InternetReadFile( hService, buf, INETBUF_MAX, &dwBytesRead ) == 0 ) { fclose( fp ); InternetCloseHandle( hService ); SetError( "ダウンロード中にエラーが発生しました" ); break; } if( dwBytesRead == 0 ) { mode = CZHTTP_MODE_DATAEND; break; } fwrite( buf, dwBytesRead, 1, fp ); size += dwBytesRead; break; } case CZHTTP_MODE_DATAEND: fclose( fp ); InternetCloseHandle( hService ); mode = CZHTTP_MODE_READY; break; case CZHTTP_MODE_VARREQUEST: strcpy( req_name, req_url2 ); strcat( req_name, req_path ); // HTTPに接続 hHttpSession = ::InternetConnectA( hSession, varserver, varport, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0 ); if ( hHttpSession == NULL ) { SetError( "無効なサーバーが指定されました" ); break; } // HTTP要求の作成 hHttpRequest = ::HttpOpenRequestA( hHttpSession, varstr, req_name, HTTP_VERSION, NULL, NULL, INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_UI, 0 ); if ( hHttpSession == NULL ) { SetError( "無効なURLが指定されました" ); break; } mode = CZHTTP_MODE_VARREQSEND; // FALL THROUGH case CZHTTP_MODE_VARREQSEND: // 作成したHTTP要求の発行 if ( postdata != NULL ) { char *additional_header = req_header != NULL ? req_header : ""; char *header = (char *)malloc( strlen(hdr) + strlen(additional_header) + 1 ); strcpy(header, hdr); strcat(header, additional_header); res = ::HttpSendRequestA( hHttpRequest, header, -1L, postdata, (int)strlen(postdata) ); free(header); } else { res = ::HttpSendRequestA( hHttpRequest, req_header, -1L, NULL, 0 ); } if ( res == false ) { InternetCloseHandle( hHttpSession ); SetError( "リクエストができませんでした" ); break; } /* { // 返されたコンテンツの長さを取得 DWORD dwSize = INETBUF_MAX; *buf = 0; res = HttpQueryInfo( hHttpRequest, HTTP_QUERY_CONTENT_LENGTH, buf, &dwSize, 0 ); if ( res == false ) { InternetCloseHandle( hHttpRequest ); InternetCloseHandle( hHttpSession ); SetError( "データサイズ取得ができませんでした" ); break; } varsize = atoi(buf); if ( varsize == 0 ) { InternetCloseHandle( hHttpRequest ); InternetCloseHandle( hHttpSession ); SetError( "ダウンロードできませんでした" ); break; } } */ varsize = 0x40000; ClearVarData(); vardata = (char *)malloc( varsize ); size = 0; mode = CZHTTP_MODE_VARDATAWAIT; // FALL THROUGH case CZHTTP_MODE_VARDATAWAIT: { DWORD dwBytesRead; int needed_size = size + INETBUF_MAX + 1; if ( needed_size > varsize ) { while ( needed_size > varsize ) { varsize *= 2; } vardata = (char *)realloc( vardata, varsize ); } if ( InternetReadFile( hHttpRequest, vardata+size, INETBUF_MAX, &dwBytesRead ) == 0 ) { InternetCloseHandle( hHttpRequest ); InternetCloseHandle( hHttpSession ); SetError( "ダウンロード中にエラーが発生しました" ); break; } size += dwBytesRead; if( dwBytesRead == 0 ) { mode = CZHTTP_MODE_VARDATAEND; vardata[size] = 0; break; } break; } case CZHTTP_MODE_VARDATAEND: InternetCloseHandle( hHttpRequest ); InternetCloseHandle( hHttpSession ); mode = CZHTTP_MODE_READY; break; case CZHTTP_MODE_INFOREQ: strcpy( req_name, req_url ); strcat( req_name, req_path ); hService = InternetOpenUrl( hSession, req_name, req_header, -1L, 0, 0 ); if ( hService == NULL ) { SetError( "無効なURLが指定されました" ); break; } mode = CZHTTP_MODE_INFORECV; case CZHTTP_MODE_INFORECV: { DWORD dwSize = INETBUF_MAX; buf[0] = 0; HttpQueryInfo( hService, HTTP_QUERY_RAW_HEADERS_CRLF, buf, &dwSize, 0 ); InternetCloseHandle( hService ); mode = CZHTTP_MODE_READY; break; } case CZHTTP_MODE_FTPREADY: return 2; case CZHTTP_MODE_FTPDIR: { char tx[512]; char dname[64]; char *fname; SYSTEMTIME t; fname = finddata.cFileName; if( finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { strcpy( dname, "<DIR>" ); } else { sprintf( dname, "%d", finddata.nFileSizeLow ); } FileTimeToSystemTime( &finddata.ftLastWriteTime,&t ); sprintf( tx, "\"%s\",%s,%4d/%02d/%02d,%02d:%02d:%02d\r\n", fname, dname, t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond ); AddFlexBuf( tx, (int)strlen(tx) ); if ( !InternetFindNextFile( hFtpEnum, &finddata ) ) { InternetCloseHandle( hFtpEnum ); mode = CZHTTP_MODE_FTPREADY; } break; } case CZHTTP_MODE_FTPREAD: break; case CZHTTP_MODE_FTPWRITE: break; case CZHTTP_MODE_FTPCMD: { char tx[1024]; DWORD dwBytesRead; if( InternetReadFile( hResponse, buf, 1024, &dwBytesRead ) ) { AddFlexBuf( tx, dwBytesRead ); } if ( dwBytesRead == 0 ) { InternetCloseHandle( hResponse ); mode = CZHTTP_MODE_FTPREADY; } break; } case CZHTTP_MODE_FTPRESULT: GetFtpResponse(); break; case CZHTTP_MODE_ERROR: return -1; default: return 1; } return 0; }
int PreProc_FtpDownloadFiles( char *downloadURL, char *tempLocation, int deleteFiles ) { if ( IsURL( downloadURL ) && !IsStopped() ) { char *p; long count=0, filenum; char server[128], path[256], name[64], passwd[32]; #if DEF_WINDOWS WIN32_FIND_DATA lpFindFileData; HINTERNET hFind, fs; char msg[256]; ExtractUserFromURL( downloadURL, server, name, passwd, path ); DateFixFilename( path, 0 ); OutDebugs( "Doing PreProcess Download %s...", path ); fs = (void*)FtpServerOpen( server, name, passwd ); if ( !fs ) { FtpServerOpenError( server ); return -1; } if ( fs ) { long flags = INTERNET_FLAG_NO_CACHE_WRITE; char newpath[512]; // ***************************************************************** // To be more friendly with various FTP servers, we need to break // up the filename from the directory that it is contained in. // We can then change to the directory prior to fetching the file. // The issue here is that some FTP servers can not get a file that // has path information in it (ie: /directory1/dir2/file.dat) // ***************************************************************** int iLen; for (iLen=mystrlen(path);iLen && path[iLen]!='/';--iLen) ; if (!iLen) // then there is no '/' { mystrcpy( newpath, path+1 ); } else { path[iLen] = NULL; // Set the '/' to a NULL so we have a path up to the name. if (!::FtpSetCurrentDirectory(fs,"/")) // Set it to root just to be sure. { // we have a problem, however there is no real way to action this. // so lets just hope that the fetch will still work. } if (!::FtpSetCurrentDirectory(fs,path)) { // again. // we have a problem, however there is no real way to action this. // so lets just hope that the fetch will still work. path[iLen] = '/'; } mystrcpy( newpath, path+iLen+1 ); } hFind = FtpFindFirstFile( fs, newpath, &lpFindFileData, flags , 0 ); if ( !hFind ){ unsigned long len = 512; FtpServerClose( fs ); OutDebugs( "%s Not Found....Trying root level path instead...", newpath ); fs = (void*)FtpServerOpen( server, name, passwd ); if ( fs ) { FtpGetCurrentDirectory( fs, newpath, &len ); strcat( newpath, path ); hFind = FtpFindFirstFile( fs, newpath, &lpFindFileData, flags , 0 ); } else FtpServerOpenError( server ); } if ( hFind ) OutDebugs( "Ftp File Found %s size = %d", lpFindFileData.cFileName, lpFindFileData.nFileSizeLow ); else { ErrorMsg( "Cannot open ftp file ...\n%s\nBecause %s", newpath, NetworkErr(NULL) ); FtpServerClose( fs ); return -1; } filenum = 0; while( hFind && !IsStopped() ) { long ret; char ftpfilename[256], localfilename[256]; if( hFind ) { ftpfilename[0] = 0; if ( !strchr( lpFindFileData.cFileName , '/' ) ){ // only if NEWPATH has a / in it copy it. if ( strchr( newpath , '/' ) ){ mystrcpy( ftpfilename, newpath ); p = strrchr( ftpfilename, '/'); if ( p ) *p = 0; } strcat( ftpfilename, "/" ); strcat( ftpfilename, lpFindFileData.cFileName ); } else mystrcpy( ftpfilename, lpFindFileData.cFileName ); // Figure out local file name if ( *tempLocation == 0 || GetFileAttributes( tempLocation ) != FILE_ATTRIBUTE_DIRECTORY ) { sprintf( msg, "%%TEMP%%\\%s", FileFromPath( ftpfilename,NULL ) ); ExpandEnvironmentStrings( msg, localfilename, 255 ); StatusSetID( IDS_STOREFILEINTEMP ); OutDebugs( "Using system temp location %s", localfilename ); } else { PathFromFullPath( tempLocation, localfilename ); if( strlen( localfilename ) ) { strcat ( localfilename, "\\" ); p = FileFromPath( ftpfilename,NULL ); if ( !p ) p = "temp.log"; strcat ( localfilename, p ); } else mystrcpy( localfilename, "temp.log" ); OutDebugs( "Using user temp location %s", localfilename ); } OutDebugs( "Trying to download %d byte file '%s' into '%s' ...", lpFindFileData.nFileSizeLow, ftpfilename, localfilename ); ret = FtpFileDownload( fs, downloadURL, localfilename, ftpfilename, lpFindFileData.nFileSizeLow ); if ( ret > 0 ) { if ( deleteFiles ){ // delete remote ftp file after downloading StatusSetID( IDS_DELETEFTP, ftpfilename ); FtpDelFile( fs, ftpfilename ); } AddFileToLogQ( localfilename, filenum++ ); } else { OutDebugs( "error downloading (%d)", ret ); ErrorMsg( "Cannot download file %s\n%s", NetworkErr(NULL) ); hFind = NULL; FtpServerClose( fs ); return -2; } //------------------------ } //if( hFind ) if ( hFind ) { if( InternetFindNextFile( hFind, &lpFindFileData ) == FALSE ) hFind = NULL; } } //while FtpServerClose( fs ); } #endif } return IsStopped(); }
// =============================================================== // —качивает файлы с FTP bool DownloadFiles ( HINTERNET hFtp, const std::wstring& rootDir ) { //printf( "Resolve directory %s\n", rootDir.c_str() ); char buf[1024] = {0}; std::vector< std::wstring > dirs; DWORD c = 1024; WIN32_FIND_DATAW fd = { 0 }, ft = { 0 }; if ( !FtpSetCurrentDirectoryW( hFtp, rootDir.c_str() ) ) wprintf( ( L"Directory " + rootDir + L" are not avaible. Goodbay\n").c_str() ); HINTERNET hSearch = FtpFindFirstFileW( hFtp, NULL, &fd, INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_RELOAD , 0 ); if ( hSearch ) { do { if( wcscmp( fd.cFileName, L"." ) == 0 || wcscmp( fd.cFileName, L".." ) == 0 ) continue; if ( fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) { dirs.push_back( fd.cFileName ); } else { // —равниваем дату найденного файла с тем что сейчас в списке if ( _waccess( fd.cFileName, 0 ) == -1 ) { wprintf( L"Loading file %s\n", fd.cFileName ); if( !FtpGetFileW( hFtp, fd.cFileName, fd.cFileName, FALSE, 0, 0, 0 ) ) return false; } else { HANDLE ftFile = FindFirstFileW( fd.cFileName, &ft ); if ( CompareFileTime ( &ft.ftLastWriteTime, &fd.ftLastWriteTime ) < 0 ) { wprintf( L"Loading file %s\n", fd.cFileName ); if ( !FtpGetFileW( hFtp, fd.cFileName, fd.cFileName, FALSE, 0, 0, 0 ) ) return false; } } } } while ( InternetFindNextFile ( hSearch, &fd ) ); InternetCloseHandle ( hSearch ); } for( size_t k=0; k < dirs.size(); k++ ) { CreateDirectoryW( dirs[ k ].c_str(), NULL ); SetCurrentDirectoryW( dirs[ k ].c_str() ); DownloadFiles( hFtp, dirs[ k ] ); } FtpSetCurrentDirectoryW( hFtp, L".." ); SetCurrentDirectoryW( L".." ); return true; } // DownloadFiles