2019-04-06 17:16:42 +02:00
# include <QFileInfo>
# include <QDebug>
# include <QSqlQuery>
# include <QSqlError>
# include <QDateTime>
# include <QMap>
# include <QTextStream>
# include <QException>
# include <QCommandLineParser>
# include <QMutex>
# include <QMutexLocker>
# include <QtConcurrent/QtConcurrentMap>
# include "commandadd.h"
2019-04-16 08:52:43 +02:00
# include "logger.h"
2019-04-06 17:16:42 +02:00
int CommandAdd : : handle ( QStringList arguments )
{
QCommandLineParser parser ;
parser . addOptions ( {
2019-04-30 23:43:31 +02:00
{ { " c " , " continue " } , " Continue adding files, don't exit on first error. If this option is not given, qss will exit asap, but it's possible that a few files will still be processed. "
" Set -t 1 to avoid this behavior, but processing will be slower. " } ,
2019-04-06 17:16:42 +02:00
{ { " a " , " all " } , " On error, no files should be added, even already processed ones " } ,
{ { " v " , " verbose " } , " Print skipped and added files " } ,
2019-04-16 08:52:43 +02:00
{ { " t " , " threads " } , " Number of threads to use. " , " threads " }
2019-04-06 17:16:42 +02:00
} ) ;
parser . addHelpOption ( ) ;
2019-04-06 23:27:56 +02:00
parser . addPositionalArgument ( " add " , " Add paths to the index " , " add [paths...] " ) ;
2019-04-06 17:16:42 +02:00
parser . process ( arguments ) ;
2019-04-16 08:52:43 +02:00
bool keepGoing = parser . isSet ( " continue " ) ;
bool verbose = parser . isSet ( " verbose " ) ;
2019-04-06 17:16:42 +02:00
if ( parser . isSet ( " all " ) )
{
throw QSSGeneralException ( " To be implemented " ) ;
}
if ( parser . isSet ( " threads " ) )
{
QString threadsCount = parser . value ( " threads " ) ;
QThreadPool : : globalInstance ( ) - > setMaxThreadCount ( threadsCount . toInt ( ) ) ;
}
QStringList files = parser . positionalArguments ( ) ;
if ( files . length ( ) = = 0 )
{
QTextStream stream ( stdin ) ;
while ( ! stream . atEnd ( ) )
{
QString path = stream . readLine ( ) ;
files . append ( path ) ;
}
}
2019-04-16 08:52:43 +02:00
FileSaver saver ( * this - > dbService ) ;
2019-04-26 22:46:33 +02:00
int numFilesCount = files . size ( ) ;
int processedFilesCount = saver . addFiles ( files . toVector ( ) , keepGoing , verbose ) ;
if ( processedFilesCount ! = numFilesCount )
2019-04-16 08:52:43 +02:00
{
2019-04-26 22:46:33 +02:00
Logger : : error ( ) < < " Errors occured while trying to add files to the database. Processed " < < processedFilesCount < < " out of " < < numFilesCount < < " files " < < endl ;
2019-04-16 08:52:43 +02:00
return 1 ;
}
2019-04-06 17:16:42 +02:00
return 0 ;
}
2019-04-16 08:52:43 +02:00