1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
|
/*
* Copyright (C) by Erik Verbruggen <erik@verbruggen.consulting>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QScopeGuard>
#include <iostream>
#include <vector>
#include "common/utility.h"
#include "libsync/filesystem.h"
#ifdef Q_OS_WIN
#include "common/utility_win.h"
#endif
/*
* IMPORTANT: the commands below do NOT read any data from files when modifying them!
*
* When a file is dehydrated, reading will cause a re-hydration (download), and many tests check
* for the number of network requests.
*/
using namespace std;
namespace {
bool writeToFile(std::string_view command, const QString &fileName, QIODevice::OpenMode mode, const QByteArray &data)
{
#ifndef Q_OS_WIN
QFile f(fileName);
if (!f.open(mode)) {
cerr << "Error: cannot open file '" << qPrintable(fileName) << "' for " << command << " command: "
<< qPrintable(f.errorString()) << endl;
return false;
}
const auto written = f.write(data);
if (mode & QFile::Append) {
if (!f.seek(f.size())) {
cerr << "Error: cannot seek to EOF in '" << qPrintable(fileName) << "' for " << command << " command" << endl;
return false;
}
}
if (written != data.size()) {
cerr << "Error: wrote " << written << " bytes to '" << qPrintable(fileName) << "' instead of requested " << data.size() << " bytes" << endl;
return false;
}
f.close();
#else
// Qt bug: [INSERT BUG HERE]
// When opening a cloud file results in a cfapi error, Qt does not report an error.
// Writes will succeed but not be committed to the file.
// Reads will always return 0
const QFileInfo info(fileName);
DWORD creation = OPEN_ALWAYS;
if (mode & QIODevice::Truncate) {
creation = TRUNCATE_EXISTING;
if (!info.exists()) {
cerr << "Error: truncating a non existing file '" << qPrintable(fileName) << "' for " << command << " command" << endl;
return false;
}
} else if (mode & QIODevice::Append) {
creation = OPEN_EXISTING;
if (!info.exists()) {
cerr << "Error: appending to non existing file '" << qPrintable(fileName) << "' for " << command << " command" << endl;
return false;
}
}
auto handle = CreateFileW(reinterpret_cast<const wchar_t *>(fileName.utf16()),
GENERIC_WRITE,
FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
{},
creation,
FILE_ATTRIBUTE_NORMAL,
nullptr);
if (handle == INVALID_HANDLE_VALUE) {
const auto error = OCC::Utility::formatWinError(GetLastError());
cerr << "Error: cannot open file '" << qPrintable(fileName) << "' for " << command << " command: "
<< qPrintable(error) << endl;
return false;
}
// cleanup the handle when leaving the scope
auto close = qScopeGuard([handle] {
CloseHandle(handle);
});
if (mode & QFile::Append) {
LARGE_INTEGER pos;
pos.QuadPart = info.size();
if (SetFilePointer(handle, pos.LowPart, &pos.HighPart, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
cerr << "Error: cannot seek to EOF in '" << qPrintable(fileName) << "' for " << command << " command" << endl;
return false;
}
}
DWORD bytesWritten;
if (!WriteFile(handle, data.constData(), data.size(), &bytesWritten, nullptr) || bytesWritten != data.size()) {
const auto error = OCC::Utility::formatWinError(GetLastError());
cerr << "Error: wrote " << bytesWritten << " bytes to '" << qPrintable(fileName) << "' instead of requested " << data.size() << " bytes. Error: " << qPrintable(error) << endl;
return false;
}
#endif
return true;
}
}
/**
* @brief The abstract Command class. You know, from the pattern.
*/
class Command
{
public:
Command(const QString &fileName)
: _fileName(fileName)
{
}
virtual ~Command() = 0;
virtual bool execute(QDir &rootDir) const = 0;
protected:
static QString parseFileName(QStringListIterator &it)
{
return it.next();
}
const QString _fileName;
};
Command::~Command() { }
class SetMtimeCommand : public Command
{
public:
static constexpr string_view name = "mtime";
SetMtimeCommand(const QString &fileName, qlonglong secs)
: Command(fileName)
, _secs(secs)
{
}
~SetMtimeCommand() override { }
bool execute(QDir &rootDir) const override
{
auto modTime = QDateTime::fromSecsSinceEpoch(_secs);
cerr << name << ", file: " << qPrintable(_fileName) << ", secs: " << _secs << endl;
return OCC::FileSystem::setModTime(rootDir.filePath(_fileName), OCC::Utility::qDateTimeToTime_t(modTime));
}
static Command *parse(QStringListIterator &it)
{
QString fileName = parseFileName(it);
if (fileName.isEmpty()) {
cerr << "Error: invalid filename for " << name << " command" << endl;
return {};
}
QString secsStr = it.next();
bool ok = false;
auto secs = secsStr.toLongLong(&ok);
if (!ok) {
cerr << "Error: '" << qPrintable(secsStr) << "' is not a valid number (" << name << ")" << endl;
return {};
}
return new SetMtimeCommand(fileName, secs);
}
private:
const qlonglong _secs;
};
class SetContentsCommand : public Command
{
public:
static constexpr string_view name = "contents";
SetContentsCommand(const QString &fileName, qlonglong count, char ch)
: Command(fileName)
, _count(count)
, _ch(ch)
{
}
~SetContentsCommand() override { }
bool execute(QDir &rootDir) const override
{
cerr << name << endl;
int count = _count == -1 ? 32 : _count;
return writeToFile(name, rootDir.filePath(_fileName), QIODevice::WriteOnly | QIODevice::Truncate, QByteArray(count, _ch));
}
static Command *parse(QStringListIterator &it)
{
QString fileName = parseFileName(it);
if (fileName.isEmpty()) {
cerr << "Error: invalid filename for " << name << " command" << endl;
return {};
}
QString countStr = it.next();
bool ok = false;
auto count = countStr.toLongLong(&ok);
if (!ok) {
cerr << "Error: '" << qPrintable(countStr) << "' is not a valid number (" << name << ")" << endl;
return {};
}
QString charStr = it.next();
if (charStr.size() != 1) {
cerr << "Error: content for " << name << " command should be 1 character in size" << endl;
}
return new SetContentsCommand(fileName, count, charStr.at(0).toLatin1());
}
private:
const qlonglong _count;
const char _ch;
};
class RenameCommand : public Command
{
public:
static constexpr string_view name = "rename";
RenameCommand(const QString &fileName, const QString &newName)
: Command(fileName)
, _newName(newName)
{
}
~RenameCommand() override { }
bool execute(QDir &rootDir) const override
{
cerr << name << endl;
if (!rootDir.exists(_fileName)) {
cerr << "File does not exist: " << qPrintable(rootDir.absoluteFilePath(_fileName)) << endl;
return false;
}
bool success = rootDir.rename(_fileName, _newName);
if (!success) {
cerr << "Rename of " << qPrintable(_fileName) << " failed" << endl;
}
return success;
}
static Command *parse(QStringListIterator &it)
{
QString fileName = parseFileName(it);
if (fileName.isEmpty()) {
cerr << "Error: invalid filename for " << name << " command" << endl;
return {};
}
QString newName = it.next();
if (newName.isEmpty()) {
cerr << "Error: invalid new name for " << name << " command" << endl;
return {};
}
return new RenameCommand(fileName, newName);
}
private:
const QString _newName;
};
class AppendByteCommand : public Command
{
public:
static constexpr string_view name = "appendbyte";
AppendByteCommand(const QString &fileName, char ch)
: Command(fileName)
, _ch(ch)
{
}
~AppendByteCommand() override { }
bool execute(QDir &rootDir) const override
{
cerr << name << endl;
if (_ch == '\0') {
cerr << "Error: appending a NUL byte is probably a failure somewhere else." << endl;
return false;
}
cerr << ".... file: " << qPrintable(_fileName) << ", byte: " << _ch << endl;
return writeToFile(name, rootDir.filePath(_fileName), QIODevice::WriteOnly | QIODevice::Append, QByteArray(1, _ch));
;
}
static Command *parse(QStringListIterator &it)
{
QString fileName = parseFileName(it);
if (fileName.isEmpty()) {
cerr << "Error: invalid filename for " << name << " command" << endl;
return {};
}
QString charStr = it.next();
if (charStr.size() != 1) {
cerr << "Error: content for " << name << " command should be 1 character in size" << endl;
}
return new AppendByteCommand(fileName, charStr.at(0).toLatin1());
}
private:
const char _ch;
};
class InsertCommand : public Command
{
public:
static constexpr string_view name = "insert";
InsertCommand(const QString &fileName, qlonglong count, char ch)
: Command(fileName)
, _count(count)
, _ch(ch)
{
}
~InsertCommand() override { }
bool execute(QDir &rootDir) const override
{
cerr << name << " '" << qPrintable(_fileName) << "' with "
<< _count << " " << _ch << " characters" << endl;
if (QFileInfo::exists(rootDir.filePath(_fileName))) {
cerr << "Error: file '" << qPrintable(_fileName) << "' for " << name << " command already exists" << endl;
return false;
}
return writeToFile(name, rootDir.filePath(_fileName), QIODevice::WriteOnly, QByteArray(_count, _ch));
}
static Command *parse(QStringListIterator &it)
{
QString fileName = parseFileName(it);
if (fileName.isEmpty()) {
cerr << "Error: invalid filename for " << name << " command" << endl;
return {};
}
QString countStr = it.next();
bool ok = false;
auto count = countStr.toLongLong(&ok);
if (!ok) {
cerr << "Error: '" << qPrintable(countStr) << "' is not a valid number (" << name << ")" << endl;
return {};
}
QString charStr = it.next();
if (charStr.size() != 1) {
cerr << "Error: content for " << name << " command should be 1 character in size" << endl;
}
return new InsertCommand(fileName, count, charStr.at(0).toLatin1());
}
private:
const qlonglong _count;
const char _ch;
};
class RemoveCommand : public Command
{
public:
static constexpr string_view name = "remove";
RemoveCommand(const QString &fileName)
: Command(fileName)
{
}
~RemoveCommand() override { }
bool execute(QDir &rootDir) const override
{
cerr << name << endl;
QFileInfo fi(rootDir.filePath(_fileName));
if (fi.isFile()) {
return rootDir.remove(_fileName);
} else {
return QDir(fi.filePath()).removeRecursively();
}
}
static Command *parse(QStringListIterator &it)
{
QString fileName = parseFileName(it);
if (fileName.isEmpty()) {
cerr << "Error: invalid filename for " << name << " command" << endl;
return {};
}
return new RemoveCommand(fileName);
}
};
class MkdirCommand : public Command
{
public:
static constexpr string_view name = "mkdir";
MkdirCommand(const QString &fileName)
: Command(fileName)
{
}
~MkdirCommand() override { }
bool execute(QDir &rootDir) const override
{
cerr << name << " " << qPrintable(_fileName) << endl;
return rootDir.mkdir(_fileName);
}
static Command *parse(QStringListIterator &it)
{
QString fileName = parseFileName(it);
if (fileName.isEmpty()) {
cerr << "Error: invalid directory name for " << name << " command" << endl;
return {};
}
return new MkdirCommand(fileName);
}
};
/**
* @brief parseArguments Creates a list of commands to be executed.
*
* Each command's `parse` method reads the required arguments from the command-line by using the
* iterator.
*
* @param it The iterator over the argument list
* @return a list of commands to execute
*/
vector<Command *> parseArguments(QStringListIterator &it)
{
map<string_view, function<Command *(QStringListIterator &)>> parserFunctions = {
{ SetMtimeCommand::name, SetMtimeCommand::parse },
{ SetContentsCommand::name, SetContentsCommand::parse },
{ RenameCommand::name, RenameCommand::parse },
{ AppendByteCommand::name, AppendByteCommand::parse },
{ InsertCommand::name, InsertCommand::parse },
{ RemoveCommand::name, RemoveCommand::parse },
{ MkdirCommand::name, MkdirCommand::parse },
};
vector<Command *> commands;
while (it.hasNext()) {
const QString option = it.next();
auto pf = parserFunctions.find(option.toStdString());
if (pf == parserFunctions.end()) {
cerr << "Error: unknown command '" << qPrintable(option) << "'" << endl;
return {};
}
if (auto cmd = (pf->second)(it)) {
commands.emplace_back(cmd);
} else {
return {};
}
}
return commands;
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QStringListIterator it(app.arguments());
if (it.hasNext()) {
// skip program name
it.next();
}
QDir rootDir(it.next());
const auto commands = parseArguments(it);
if (commands.empty()) {
return -1;
}
cerr << "Starting executing commands in '" << qPrintable(rootDir.absolutePath()) << "' ...:" << endl;
for (const auto &cmd : commands) {
cerr << ".. Executing command: ";
if (!cmd->execute(rootDir)) {
return -2;
}
cerr << ".. command done." << endl;
}
cerr << "Successfully executed all commands." << endl;
qDeleteAll(commands);
return 0;
}
|