Subversion Repositories tpanel

Rev

Rev 126 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 126 Rev 137
Line 353... Line 353...
353
    }
353
    }
354
 
354
 
355
    return err;
355
    return err;
356
}
356
}
357
 
357
 
-
 
358
string TTPInit::getTmpFileName()
-
 
359
{
-
 
360
    DECL_TRACER("TTPInit::getTmpFileName()");
-
 
361
 
-
 
362
    const string alphanum =
-
 
363
            "0123456789"
-
 
364
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-
 
365
            "abcdefghijklmnopqrstuvwxyz";
-
 
366
 
-
 
367
    size_t stringLength = alphanum.length() - 1;
-
 
368
    std::string Str;
-
 
369
    char *tmp = getenv("TMP");
-
 
370
 
-
 
371
    if (!tmp)
-
 
372
        tmp = getenv("TEMP");
-
 
373
 
-
 
374
    if (!tmp)
-
 
375
        tmp = getenv("HOME");
-
 
376
    else
-
 
377
        tmp = (char *)"/tmp";
-
 
378
 
-
 
379
    Str.assign(tmp);
-
 
380
    Str.append("/");
-
 
381
 
-
 
382
    for(size_t i = 0; i < MAX_TMP_LEN; ++i)
-
 
383
        Str += alphanum[rand() % stringLength];
-
 
384
 
-
 
385
    // We create the file. YES, this is a security hole but in our case we have
-
 
386
    // no real alternative for now.
-
 
387
    try
-
 
388
    {
-
 
389
        std::ofstream tmpfile;
-
 
390
        tmpfile.open(Str, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
-
 
391
 
-
 
392
        if (!tmpfile.is_open())
-
 
393
        {
-
 
394
            MSG_ERROR("Error opening a temporary file!");
-
 
395
        }
-
 
396
        else
-
 
397
            tmpfile.flush();
-
 
398
 
-
 
399
        tmpfile.close();
-
 
400
    }
-
 
401
    catch (std::exception& e)
-
 
402
    {
-
 
403
        MSG_ERROR("Couldn't create a temporary file: " << e.what());
-
 
404
        return string();
-
 
405
    }
-
 
406
 
-
 
407
    return Str;
-
 
408
}
-
 
409
 
358
/**
410
/**
359
 * This methods checks if there exists a previous downloaded TP4 file. If this
411
 * This methods checks if there exists a previous downloaded TP4 file. If this
360
 * is the case, nothing happens.
412
 * is the case, nothing happens.
361
 * If there is no previous downloaded file it checks if there is one on the
413
 * If there is no previous downloaded file it checks if there is one on the
362
 * controller and downloads it if it exists. After successfull download the
414
 * controller and downloads it if it exists. After successfull download the
Line 434... Line 486...
434
vector<string>& TTPInit::getFileList(const string& filter)
486
vector<string>& TTPInit::getFileList(const string& filter)
435
{
487
{
436
    DECL_TRACER("TTPInit::getFileList(const string& filter)");
488
    DECL_TRACER("TTPInit::getFileList(const string& filter)");
437
 
489
 
438
    ftplib *ftp = new ftplib();
490
    ftplib *ftp = new ftplib();
-
 
491
    ftp->regLogging(bind(&TTPInit::logging, this, std::placeholders::_1, std::placeholders::_2));
439
 
492
 
440
    if (TConfig::getFtpPassive())
493
    if (TConfig::getFtpPassive())
441
        ftp->SetConnmode(ftplib::pasv);
494
        ftp->SetConnmode(ftplib::pasv);
442
    else
495
    else
443
        ftp->SetConnmode(ftplib::port);
496
        ftp->SetConnmode(ftplib::port);
Line 459... Line 512...
459
    {
512
    {
460
        delete ftp;
513
        delete ftp;
461
        return mDirList;
514
        return mDirList;
462
    }
515
    }
463
 
516
 
464
    string tmpFile = std::tmpnam(nullptr);
517
//    string tmpFile = std::tmpnam(nullptr);
-
 
518
    string tmpFile = getTmpFileName();
465
    MSG_DEBUG("Reading remote directory / into file " << tmpFile);
519
    MSG_DEBUG("Reading remote directory / into file " << tmpFile);
466
    ftp->Nlst(tmpFile.c_str(), "/");
520
    ftp->Nlst(tmpFile.c_str(), "/");
467
    ftp->Quit();
521
    ftp->Quit();
468
    delete ftp;
522
    delete ftp;
469
    mDirList.clear();
523
    mDirList.clear();
Line 477... Line 531...
477
 
531
 
478
        while (ifile.getline(buffer, sizeof(buffer)))
532
        while (ifile.getline(buffer, sizeof(buffer)))
479
        {
533
        {
480
            string buf = buffer;
534
            string buf = buffer;
481
            string fname = trim(buf);
535
            string fname = trim(buf);
-
 
536
            MSG_DEBUG("FTP line: " << buf << " (" << fname << ")");
482
 
537
 
483
            if (!filter.empty())
538
            if (!filter.empty())
484
            {
539
            {
485
                if (endsWith(toUpper(buf), uFilter))
540
                if (endsWith(toUpper(buf), uFilter))
486
                    mDirList.push_back(trim(fname));
541
                    mDirList.push_back(trim(fname));
Line 555... Line 610...
555
    }
610
    }
556
 
611
 
557
    return false;
612
    return false;
558
}
613
}
559
 
614
 
-
 
615
void TTPInit::logging(int level, const std::string &msg)
-
 
616
{
-
 
617
    switch(level)
-
 
618
    {
-
 
619
        case LOG_INFO:      MSG_INFO(msg); break;
-
 
620
        case LOG_WARNING:   MSG_WARNING(msg); break;
-
 
621
        case LOG_ERROR:     MSG_ERROR(msg); break;
-
 
622
        case LOG_TRACE:     MSG_TRACE(msg); break;
-
 
623
        case LOG_DEBUG:     MSG_DEBUG(msg); break;
-
 
624
    }
-
 
625
}
-
 
626
 
560
#ifdef __ANDROID__
627
#ifdef __ANDROID__
561
bool TTPInit::askPermissions()
628
bool TTPInit::askPermissions()
562
{
629
{
563
    DECL_TRACER("TTPInit::askPermissions()");
630
    DECL_TRACER("TTPInit::askPermissions()");
564
 
631