Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Commit

Permalink
[os] single quoted params for commands executed in shell
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Zalevskiy <denis.zalevskiy@jolla.com>
  • Loading branch information
Denis Zalevskiy committed Oct 18, 2014
1 parent 7dc7eec commit c0e4370
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
2 changes: 2 additions & 0 deletions include/qtaround/os.hpp
Expand Up @@ -24,6 +24,8 @@

namespace qtaround { namespace os {

QString singleQuoted(QString const &);

using subprocess::Process;

namespace path {
Expand Down
40 changes: 37 additions & 3 deletions src/os.cpp
Expand Up @@ -18,6 +18,36 @@

namespace qtaround { namespace os {

#define QS_(str) QStringLiteral(str)
namespace {

const QChar squote{QLatin1Char('\'')};
const QChar bslash{QLatin1Char('\\')};
const QString escaped_squote{QS_("\"'\"")};

}

QString singleQuoted(QString const &v)
{
QStringList parts;
QStringRef ref(&v);

auto appendSquoted = [&parts](QStringRef const &ref) {
if (ref.length())
parts.push_back(squote + ref.toString() + squote);
};
for (auto pos = ref.indexOf(squote); pos != -1; pos = ref.indexOf(squote)) {
appendSquoted(ref.left(pos));
parts.push_back(escaped_squote);
ref = ref.mid(pos + 1);
if (!ref.length())
break;
}
if (ref.length())
appendSquoted(ref);
return parts.length() ? parts.join(QS_("")) : QS_("''");
}

namespace path {

QStringList split(QString const &p)
Expand Down Expand Up @@ -200,11 +230,15 @@ QList<QVariantMap> mount()

QString mountpoint(QString const &path)
{
QStringList commands = {"df -P " + path, "tail -1", "awk '{ print $NF; }'"};
if (!path::exists(path))
return "";

QStringList commands = {"df -P " + singleQuoted(path)
, "tail -1", "awk '{ print $NF; }'"};
QStringList options = {"-c", commands.join(" | ")};
auto data = subprocess::check_output("sh", options);
auto res = str(data).split("\n")[0];
debug::info("Mountpoint for", path, "=", path);
debug::info("Mountpoint for", path, "=", res);
return res;
}

Expand Down Expand Up @@ -273,7 +307,7 @@ class BtrFs {

QVariantMap df()
{
QStringList cmd_options = {"-c", "btrfs fi df " + path};
QStringList cmd_options = {"-c", "btrfs fi df " + singleQuoted(path)};
auto out = str(subprocess::check_output("sh", cmd_options));
auto data = filterEmpty(out.trimmed().split("\n"));

Expand Down
18 changes: 18 additions & 0 deletions tests/os.cpp
Expand Up @@ -88,9 +88,27 @@ enum test_ids {
tid_du
};

#define DQ "\""
#define SQ "'"
#define QDQ SQ DQ SQ
#define QSQ DQ SQ DQ

template<> template<>
void object::test<tid_basic>()
{
ensure_eq(AT, os::singleQuoted(""), "''");
ensure_eq(AT, os::singleQuoted("a"), "'a'");
ensure_eq(AT, os::singleQuoted("'"), QSQ);
ensure_eq(AT, os::singleQuoted("''"), QSQ QSQ);
ensure_eq(AT, os::singleQuoted("\"'"), QDQ QSQ);
ensure_eq(AT, os::singleQuoted("'\""), QSQ QDQ);
ensure_eq(AT, os::singleQuoted("a'"), "'a'" QSQ);
ensure_eq(AT, os::singleQuoted("'b"), QSQ "'b'");
ensure_eq(AT, os::singleQuoted("'c'"), QSQ "'c'" QSQ);
ensure_eq(AT, os::singleQuoted("d'd"), "'d'" QSQ "'d'");
ensure_eq(AT, os::singleQuoted("ee'"), "'ee'" QSQ);
ensure_eq(AT, os::singleQuoted("'ff"), QSQ "'ff'");

auto home = os::home();
ensure_ne(AT, home.size(), 0);
ensure_eq(AT, home, os::environ("HOME"));
Expand Down

0 comments on commit c0e4370

Please sign in to comment.