Exemplo n.º 1
0
// Download files from sourceforge svn web view page, preserve folder structure.
bool SvnSourceForge::DownloadFolder(
	const std::string & page_url,
	const std::string & folder_path,
	GameDownloader & download,
	std::ostream & error_output)
{
	// Download files.
	std::vector<std::string> folders;
	std::vector<std::string> files;
	if (!DownloadFolder(page_url, "", download, folders, files, error_output))
	{
		while (!files.empty())
		{
			const std::string temp_path = download.GetHttp().GetDownloadPath(files.back());
			PathManager::RemoveFile(temp_path);
			files.pop_back();
		}
		return false;
	}

	// Creare folders.
	PathManager::MakeDir(folder_path);
	while (!folders.empty())
	{
		PathManager::MakeDir(folder_path + folders.back());
		folders.pop_back();
	}

	// Copy files.
	while (!files.empty())
	{
		const std::string temp_path = download.GetHttp().GetDownloadPath(files.back());
		const std::string file_path = folder_path + files.back();
		PathManager::CopyFileTo(temp_path, file_path);
		PathManager::RemoveFile(temp_path);
		files.pop_back();
	}

	return true;
}
Exemplo n.º 2
0
int FtpClient::DownloadFolder ( char *dir, char *lDir )
{
    try
    {
        char localdir[4096];
        strcpy ( localdir, lDir );
        int error = ChangeDir ( dir );
        if ( error )
            cout << "Failed to change remote dir to "<<dir<<" , error="<<error<<endl;
        else cout << "\nDirectory was changed to "<<dir<<endl;
        strcat ( localdir, dir );
        if ( !mkdir ( localdir, 0777 )  || errno == EEXIST)
            cout << "\nLocal directory " << localdir << "created"<<endl;
        else
        {
            cout <<"\nLocal directory " << localdir << "wasn't created, err="<<errno<<endl;
            
        }
        string list[1000];
        int i = 0, j = 0, n = 0;
        if ( !List ( "this" ) )
        {
            while ( j < strlen ( Result ) )
            {
                //list[i] = new string;
                while ( Result[j] == '\r' || Result[j] == '\n' )
                    j++;
                while ( Result[j] != '\r' && Result[j] != '\n' && Result[j] != '\0' )
                {
                    list[i] += Result[j++];
                }
                i++;
                j++;
            }
            n = i;
        }
        else
        {
            return LIST_CMD_FAILED;
        }
        for ( i = 0; i < n; i++ )
        {
            int found;
            if ( list[i][0] == 'l' )
                continue;
            if ( list[i][0] == 'd' )
            { //folder sau fisier cu spatii
                found = list[i].find_last_of (' ');
                string folder = list[i].substr ( found+1 );
                if ( folder != "." && folder != ".." )
                {
                    char fd[256];
                    strcpy ( fd, dir );
                    if ( dir[strlen ( dir ) - 1] != '/' )
                        strcat ( fd, "/" );
                    strcat ( fd, folder.c_str() );
                    DownloadFolder ( fd, lDir );
                }
            }
            else
            {
                char seps[]   = " ";
                char *token;
                char file[512];
                strcpy ( file, list[i].c_str() );
                token = strtok ( file, seps ); //ia cate un cuvant
                string vector[20];
                int prop = 0;
                while ( token != NULL )
                {
                    vector[prop] = token; //punem cuvantul in vector
                    token = strtok ( NULL, seps );
                    prop++; //numarul de proprietati
                }
                bzero ( file, 512 );
                if ( prop > 9 )
                {
                    for ( int x = 8; x < prop; x++ )
                    {
                        strcat ( file, vector[x].c_str() );
                        strcat ( file, " " );
                    }
                    file[strlen ( file ) - 1] = '\0';
                }
                else	strcpy ( file, vector[prop-1].c_str() );
                char localPath[4096];
                strcpy ( localPath, localdir );
                if ( localdir[strlen ( localdir )-1] != '/' )
                    strcat ( localPath, "/" );
                strcat ( localPath, file );
                cout<<"\nDownloading file="<<file<<endl;
                int error = 0;
                error = DownloadFile ( file, localPath );
                if ( error == 0 )
                    cout << "\nFile was downloaded succesfully\n";
                else cout << "\nWe encountered an error while downloading the file, error="<<error<<"\n";
            }
        }

    }
    catch ( Exception ex )
    {
        cout << ex.Message << endl << ex.ErrorCode << endl;
        return 1;
    }
    return 0;
}
Exemplo n.º 3
0
// Download files and folders lists from sourceforge svn web view page
static bool DownloadFolder(
	const std::string & page_url,
	const std::string & folder,
	GameDownloader & download,
	std::vector<std::string> & folders,
	std::vector<std::string> & files,
	std::ostream & error_output)
{
	// Download folder page.
	if (!download(page_url))
	{
		error_output << "Download failed: " << page_url << std::endl;
		return false;
	}

	// Load folder page.
	const std::string page_path = download.GetHttp().GetDownloadPath(page_url);
	std::ifstream page(page_path.c_str());
	if (!page)
	{
		error_output << "File not found: " << page_path << std::endl;
		return false;
	}

	// Fast forward to the start of the list.
	Utils::SeekTo(page, "<ul>");

	// Loop through each entry.
	while (page)
	{
		Utils::SeekTo(page, "<li><a href=\"");
		const std::string name = Utils::SeekTo(page, "\">");

		if (name.empty() || name == "../" || name == "Sconscript")
			continue;

		// Download sub folder.
		if (*name.rbegin() == '/')
		{
			if (!DownloadFolder(page_url + name, folder + name, download,
								folders, files, error_output))
			{
				return false;
			}

			folders.push_back(folder + name);
			continue;
		}

		// Download file.
		const std::string file_url = page_url + name;
		if (!download(file_url))
		{
			error_output << "Download failed: " << file_url << std::endl;
			return false;
		}

		files.push_back(folder + name);
	}

	page.close();
	PathManager::RemoveFile(page_path);
	return true;
}