Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions src/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ using namespace std::chrono_literals;

#define UPDATE_WATCHED_DIRECTORIES_INTERVAL 30s

namespace {

}

namespace appimagelauncher::daemon {

Q_LOGGING_CATEGORY(daemonCat, "appimagelauncher.daemon")
Expand All @@ -27,7 +23,7 @@ namespace appimagelauncher::daemon {
// to watch these
QObject::connect(_watcher, &FileSystemWatcher::newDirectoriesToWatch, this, [this](const QDirSet& newDirs) {
if (newDirs.empty()) {
qDebug() << "No new directories to watch detected";
qCDebug(daemonCat) << "No new directories to watch detected";
} else {
qCInfo(daemonCat) << "Discovered new directories to watch, integrating existing AppImages initially";

Expand All @@ -44,7 +40,7 @@ namespace appimagelauncher::daemon {
// a good example for this situation is a removable drive that has been unplugged from the computer
QObject::connect(_watcher, &FileSystemWatcher::directoriesToWatchDisappeared, this, [](const QDirSet& disappearedDirs) {
if (disappearedDirs.empty()) {
qDebug() << "No directories disappeared";
qCDebug(daemonCat) << "No directories disappeared";
} else {
qCInfo(daemonCat) << "Directories to watch disappeared, unintegrating AppImages formerly found in there";

Expand All @@ -54,12 +50,6 @@ namespace appimagelauncher::daemon {
}
});


// search directories to watch once initially
// we *have* to do this even though we connect this signal above, as the first update occurs in the constructor
// and we cannot connect signals before construction has finished for obvious reasons
initialSearchForAppImages(_watcher->directories());

// (re-)integrate all AppImages at once
_worker->executeDeferredOperations();

Expand Down Expand Up @@ -87,6 +77,11 @@ namespace appimagelauncher::daemon {
// initial search for AppImages; if AppImages are found, they will be integrated, unless they already are
qCInfo(daemonCat) << "Searching for existing AppImages";

if (dirsToSearch.empty()) {
qCWarning(daemonCat) << "No directories to search provided initially, skipping";
return;
}

for (const auto& dir : dirsToSearch) {
if (!dir.exists()) {
qCDebug(daemonCat) << "Directory " << dir.path() << " does not exist, skipping";
Expand Down Expand Up @@ -124,6 +119,14 @@ namespace appimagelauncher::daemon {
}

bool Daemon::startWatching() {
// make sure the watched directories list is up to date
_watcher->updateWatchedDirectories(watchedDirectories());

// search directories to watch once initially
// we *have* to do this even though we connect this signal above, as the first update occurs in the constructor
// and we cannot connect signals before construction has finished for obvious reasons
initialSearchForAppImages(_watcher->directories());

return _watcher->startWatching();
}

Expand Down
28 changes: 19 additions & 9 deletions src/daemon/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ int main(int argc, char* argv[]) {
// make sure shared won't try to use the UI
setenv("_FORCE_HEADLESS", "1", 1);

// improve default logging format
qSetMessagePattern("[%{type}] %{category}: %{message}");

QCommandLineParser parser;
parser.setApplicationDescription(
QObject::tr(
Expand All @@ -77,6 +80,15 @@ int main(int argc, char* argv[]) {
throw std::runtime_error("could not add Qt command line option for some reason");
}

QCommandLineOption debugOption(
"debug",
QObject::tr("Enable debug logging")
);

if (!parser.addOption(debugOption)) {
throw std::runtime_error("could not add Qt command line option for some reason");
}

QCoreApplication app(argc, argv);

{
Expand All @@ -90,6 +102,12 @@ int main(int argc, char* argv[]) {
// parse arguments
parser.process(app);

if (!parser.isSet(debugOption)) {
QLoggingCategory::setFilterRules("*.debug=false");
} else {
QLoggingCategory::setFilterRules("*.debug=true");
}

auto* daemon = new Daemon(&app);

// this option is for debugging the
Expand All @@ -105,15 +123,7 @@ int main(int argc, char* argv[]) {
std::cout << "Failed to clean up old desktop integration resources" << std::endl;
}

auto watchedDirectoriesStringList = [daemon]() {
QStringList rv;
for (const auto& dir : daemon->watchedDirectories()) {
rv << dir.path();
}
return rv;
}();

qInfo() << "Watching directories:" << watchedDirectoriesStringList;
qInfo() << "Watching directories:" << daemon->watchedDirectories();

if (!daemon->startWatching()) {
std::cerr << "Could not start watching directories" << std::endl;
Expand Down
21 changes: 11 additions & 10 deletions src/fswatcher/filesystemwatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <unistd.h>

// library includes
#include <QDebug>
#include <QDir>
#include <QMutex>
#include <QTimer>
Expand All @@ -30,6 +29,8 @@ namespace {

namespace appimagelauncher::daemon {

Q_LOGGING_CATEGORY(fswCat, "appimagelauncher.daemon.filesystemwatcher")

class FileSystemWatcher::PrivateData {
public:
enum EVENT_TYPES {
Expand Down Expand Up @@ -111,18 +112,18 @@ namespace appimagelauncher::daemon {
bool startWatching(const QDir& directory) {
static const auto mask = fileChangeEvents | fileRemovalEvents;

qDebug() << "start watching directory " << directory;
qCDebug(fswCat) << "start watching directory " << directory;

if (!directory.exists()) {
qDebug() << "Warning: directory " << directory.absolutePath() << " does not exist, skipping";
qCDebug(fswCat) << "Warning: directory " << directory.absolutePath() << " does not exist, skipping";
return true;
}

const int watchFd = inotify_add_watch(inotifyFd, directory.absolutePath().toStdString().c_str(), mask);

if (watchFd == -1) {
const auto error = errno;
std::cerr << "Failed to start watching: " << strerror(error) << std::endl;
qCCritical(fswCat) << "Failed to start watching: " << strerror(error);
return false;
}

Expand Down Expand Up @@ -161,11 +162,11 @@ namespace appimagelauncher::daemon {
// therefore, we can remove the file descriptor from the map in any case
watchFdMap.erase(watchFd);

qDebug() << "stop watching watchfd " << watchFd;
qCDebug(fswCat) << "stop watching watchfd " << watchFd;

if (inotify_rm_watch(inotifyFd, watchFd) == -1) {
const auto error = errno;
std::cerr << "Failed to stop watching: " << strerror(error) << std::endl;
qCCritical(fswCat) << "Failed to stop watching: " << strerror(error);
return false;
}

Expand All @@ -180,7 +181,7 @@ namespace appimagelauncher::daemon {
const auto watchFd = pair.first;

if (!stopWatching(watchFd)) {
std::cerr << "Warning: Failed to stop watching on file descriptor " << watchFd << std::endl;
qCCritical(fswCat) << "Warning: Failed to stop watching on file descriptor " << watchFd;
}
}

Expand Down Expand Up @@ -233,7 +234,7 @@ namespace appimagelauncher::daemon {
QMutexLocker lock{d->mutex};

if (d->isRunning) {
qDebug() << "tried to start file system watcher while it's running already";
qCDebug(fswCat) << "tried to start file system watcher while it's running already";
return true;
}
}
Expand All @@ -255,7 +256,7 @@ namespace appimagelauncher::daemon {
QMutexLocker lock{d->mutex};

if (!d->isRunning) {
qDebug() << "tried to stop file system watcher while stopped";
qCDebug(fswCat) << "tried to stop file system watcher while stopped";
return true;
}
}
Expand Down Expand Up @@ -298,7 +299,7 @@ namespace appimagelauncher::daemon {
// therefore we use a simple linear search to remove non-existing directories
for (auto it = watchedDirectories.begin(); it != watchedDirectories.end(); ++it) {
if (!it->exists()) {
std::cout << "Directory " << it->path().toStdString() << " does not exist, skipping" << std::endl;
qCDebug(fswCat) << "Directory " << it->path() << " does not exist, skipping";
it = watchedDirectories.erase(it);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/fswatcher/filesystemwatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// library includes
#include <QDir>
#include <QObject>
#include <QLoggingCategory>
#include <QSet>
#include <QString>
#include <QThread>
Expand All @@ -17,6 +18,8 @@

namespace appimagelauncher::daemon {

Q_DECLARE_LOGGING_CATEGORY(fswCat)

class FileSystemWatcherError : public std::runtime_error {
public:
explicit FileSystemWatcherError(const QString& message) : std::runtime_error(message.toStdString().c_str()) {};
Expand Down
2 changes: 1 addition & 1 deletion src/shared/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
add_library(shared STATIC shared.h shared.cpp types.h)
add_library(shared STATIC shared.h shared.cpp types.h types.cpp)
target_link_libraries(shared PUBLIC PkgConfig::glib Qt5::Core Qt5::Widgets Qt5::DBus libappimage translationmanager trashbin)
if(ENABLE_UPDATE_HELPER)
target_link_libraries(shared PUBLIC libappimageupdate)
Expand Down
9 changes: 9 additions & 0 deletions src/shared/types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "types.h"

QDebug operator<<(QDebug debug, const QDirSet& set) {
QDebugStateSaver saver(debug);
for (const auto& item : set) {
debug << item;
}
return debug;
}
4 changes: 4 additions & 0 deletions src/shared/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <set>

// library headers
#include <QDebug>
#include <QDir>

struct QDirComparator {
Expand All @@ -14,3 +15,6 @@ struct QDirComparator {
};

typedef std::set<QDir, QDirComparator> QDirSet;


QDebug operator<<(QDebug debug, const QDirSet &set);