KCoreAddons

klistopenfilesjob_unix.cpp
1 /*
2  This file is part of the KDE project
3 
4  SPDX-FileCopyrightText: 2010 Jacopo De Simoi <[email protected]>
5  SPDX-FileCopyrightText: 2014 Lukáš Tinkl <[email protected]>
6  SPDX-FileCopyrightText: 2016 Kai Uwe Broulik <[email protected]>
7  SPDX-FileCopyrightText: 2019 David Hallas <[email protected]>
8 
9  SPDX-License-Identifier: LGPL-2.0-only
10 */
11 
12 #include "klistopenfilesjob.h"
13 #include <QDir>
14 #include <QProcess>
15 #include <QRegularExpression>
16 #include <QVector>
17 
18 class KListOpenFilesJobPrivate
19 {
20 public:
21  KListOpenFilesJobPrivate(KListOpenFilesJob *Job, const QDir &Path)
22  : job(Job)
23  , path(Path)
24  {
26  lsofError(error);
27  });
28  QObject::connect(&lsofProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
29  [this](int exitCode, QProcess::ExitStatus exitStatus) {
30  lsofFinished(exitCode, exitStatus);
31  });
32  }
33  void start()
34  {
35  if (!path.exists()) {
36  emitResult(static_cast<int>(KListOpenFilesJob::Error::DoesNotExist),
37  QObject::tr("Path %1 doesn't exist").arg(path.path()));
38  return;
39  }
40  lsofProcess.start(QStringLiteral("lsof"), {QStringLiteral("-t"), QStringLiteral("+d"), path.path()});
41  }
42  KProcessList::KProcessInfoList getProcessInfoList() const
43  {
44  return processInfoList;
45  }
46 private:
47  void lsofError(QProcess::ProcessError processError)
48  {
49  emitResult(static_cast<int>(KListOpenFilesJob::Error::InternalError),
50  QObject::tr("Failed to execute `lsof' error code %1").arg(processError));
51  }
52  void lsofFinished(int, QProcess::ExitStatus)
53  {
54  if (hasEmittedResult) {
55  return;
56  }
57  const QString out(QString::fromLocal8Bit(lsofProcess.readAll()));
58 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
59  const QVector<QStringRef> pidList = out.splitRef(QRegularExpression(QStringLiteral("\\s+")), QString::SkipEmptyParts);
60 #else
61  const QVector<QStringRef> pidList = out.splitRef(QRegularExpression(QStringLiteral("\\s+")), Qt::SkipEmptyParts);
62 #endif
63  for (const auto &pidStr : pidList) {
64  qint64 pid = pidStr.toLongLong();
65  if (!pid) {
66  continue;
67  }
68  processInfoList << KProcessList::processInfo(pid);
69  }
70  job->emitResult();
71  }
72  void emitResult(int error, const QString& errorText)
73  {
74  if (hasEmittedResult) {
75  return;
76  }
77  job->setError(error);
78  job->setErrorText(errorText);
79  job->emitResult();
80  hasEmittedResult = true;
81  }
82 private:
83  KListOpenFilesJob *job;
84  const QDir path;
85  bool hasEmittedResult = false;
86  QProcess lsofProcess;
87  KProcessList::KProcessInfoList processInfoList;
88 };
89 
90 KListOpenFilesJob::KListOpenFilesJob(const QString& path)
91  : d(new KListOpenFilesJobPrivate(this, path))
92 {
93 }
94 
95 KListOpenFilesJob::~KListOpenFilesJob() = default;
96 
98 {
99  d->start();
100 }
101 
103 {
104  return d->getProcessInfoList();
105 }
106 
107 #include "moc_klistopenfilesjob.cpp"
void start() override
Starts the job asynchronously.
QString tr(const char *sourceText, const char *disambiguation, int n)
void errorOccurred(QProcess::ProcessError error)
QString fromLocal8Bit(const char *str, int size)
void finished(int exitCode)
Provides information about processes that have open files in a given path or subdirectory of path...
SkipEmptyParts
KProcessList::KProcessInfoList processInfoList() const
Returns the list of processes with open files for the requested path.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Sep 25 2020 23:03:07 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.