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  const QVector<QStringRef> pidList = out.splitRef(QRegularExpression(QStringLiteral("\\s+")), Qt::SkipEmptyParts);
59  for (const auto &pidStr : pidList) {
60  qint64 pid = pidStr.toLongLong();
61  if (!pid) {
62  continue;
63  }
64  processInfoList << KProcessList::processInfo(pid);
65  }
66  job->emitResult();
67  }
68  void emitResult(int error, const QString& errorText)
69  {
70  if (hasEmittedResult) {
71  return;
72  }
73  job->setError(error);
74  job->setErrorText(errorText);
75  job->emitResult();
76  hasEmittedResult = true;
77  }
78 private:
79  KListOpenFilesJob *job;
80  const QDir path;
81  bool hasEmittedResult = false;
82  QProcess lsofProcess;
83  KProcessList::KProcessInfoList processInfoList;
84 };
85 
86 KListOpenFilesJob::KListOpenFilesJob(const QString& path)
87  : d(new KListOpenFilesJobPrivate(this, path))
88 {
89 }
90 
91 KListOpenFilesJob::~KListOpenFilesJob() = default;
92 
94 {
95  d->start();
96 }
97 
99 {
100  return d->getProcessInfoList();
101 }
102 
103 #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-2021 The KDE developers.
Generated on Thu Jan 21 2021 23:04:45 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.