Skip to content

Commit

Permalink
cp: Support -aRv options
Browse files Browse the repository at this point in the history
  • Loading branch information
benlau committed Aug 5, 2016
1 parent 9ca87cb commit e48af1d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,13 @@ Examples
cp("*.txt", "/tmp");

cp("/tmp/123.txt", "456.txt");


Options

-a Same as -R options.

-R If source_file designates a directory, cp copies the directory
and the entire subtree connected at that point.

-v Cause cp to be verbose, showing files as they are copied.
34 changes: 28 additions & 6 deletions qtshell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,10 @@ bool QtShell::mkdir(const QString &options, const QString &path)
return dir.mkpath(path);
}

static bool _cp(QString source, QString target, bool verbose = false) {
static bool _cp(QString source, QString target,
bool recursive = false,
bool verbose = false) {

if (source.isEmpty() || target.isEmpty()) {
qWarning() << "cp(const QString &source, const QString &target)";
return false;
Expand All @@ -332,20 +335,36 @@ static bool _cp(QString source, QString target, bool verbose = false) {
QFileInfo targetInfo(target);

foreach (QFileInfo file, files) {
if (file.isDir()) {
qWarning() << QString("cp: %1 is a directory (not copied)").arg(file.fileName());
continue;
}

QString targetFile = target;

if (targetInfo.isDir()) {
targetFile = target + "/" + file.fileName();
}

if (file.isDir()) {

if (!recursive) {
qWarning() << QString("cp: %1 is a directory (not copied)").arg(file.fileName());
res = false;
} else {

QtShell::mkdir(targetFile);
QDir nextDir(file.absoluteFilePath());

if (nextDir.entryList().size() > 2) { // except "." && ".."
if (!_cp(file.absoluteFilePath() + "/*", targetFile, recursive, verbose)) {
res = false;
}
}
}
continue;
}

if (verbose) {
qDebug().noquote() << QString("%1 -> %2").arg(file.absoluteFilePath()).arg(targetFile);
}

if (!QFile::copy(file.absoluteFilePath(), targetFile)) {
qWarning() << QString("cp: %1: Failed to copy to %2").arg(file.fileName()).arg(target);
res = false;
Expand All @@ -364,15 +383,18 @@ bool QtShell::cp(const QString& options, const QString& source , const QString &

QCommandLineParser parser;
parser.addOption(QCommandLineOption("v"));
parser.addOption(QCommandLineOption("R"));
parser.addOption(QCommandLineOption("a"));

if (!parser.parse(QStringList() << "cp" << options)) {
qWarning() << QString("cp: %1").arg(parser.errorText());
return false;
}

bool recursive = parser.isSet("R") || parser.isSet("a");
bool verbose = parser.isSet("v");

return _cp(source, target, verbose);
return _cp(source, target, recursive, verbose);

}

Expand Down
1 change: 1 addition & 0 deletions qtshell.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace QtShell {
bool mkdir(const QString &options, const QString &path);

bool cp(const QString& source , const QString &target);

bool cp(const QString& options, const QString& source , const QString &target);


Expand Down
13 changes: 13 additions & 0 deletions tests/qtshellunittests/qtshelltests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,18 @@ void QtShellTests::test_cp()
QVERIFY(cp(":/*.cpp", "target"));
QCOMPARE(find("target",QStringList() << "*.cpp").size(), 1);

/* Test -r / -a */

mkdir("src/a");
mkdir("src/b");
touch("src/b/b1.txt");
touch("src/b/b2.txt");
rm("-rf", "target");
mkdir("target");

QVERIFY(cp("-av","src/*","target"));

QStringList files = find("target");
QCOMPARE(files.size(), 8);
}

0 comments on commit e48af1d

Please sign in to comment.