2015-02-07 00:32:25 +00:00
|
|
|
/*************************************************************************
|
|
|
|
* Copyright (C) 2009, 2010 by Volker Lanz <vl@fidra.de> *
|
2016-03-02 18:33:40 +00:00
|
|
|
* Copyright (C) 2016 by Andrius Štikonas <andrius@stikonas.eu> *
|
2015-02-07 00:32:25 +00:00
|
|
|
* *
|
|
|
|
* 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 3 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. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
|
|
|
|
*************************************************************************/
|
2010-01-15 18:08:01 +00:00
|
|
|
|
2010-01-16 20:21:42 +00:00
|
|
|
#include "gui/editmountpointdialogwidget.h"
|
|
|
|
#include "gui/editmountoptionsdialog.h"
|
2010-01-15 18:08:01 +00:00
|
|
|
|
2016-01-12 00:12:49 +00:00
|
|
|
#include <core/partition.h>
|
|
|
|
#include <core/mountentry.h>
|
2010-01-16 20:21:42 +00:00
|
|
|
|
2016-01-12 00:12:49 +00:00
|
|
|
#include <fs/filesystem.h>
|
2016-05-24 16:35:39 +01:00
|
|
|
#include <fs/luks.h>
|
2010-01-16 20:21:42 +00:00
|
|
|
|
2014-05-13 23:53:37 +01:00
|
|
|
#include <KLocalizedString>
|
2014-05-18 21:41:48 +01:00
|
|
|
#include <KMessageBox>
|
2016-08-23 16:40:16 +01:00
|
|
|
#include <KMountPoint>
|
2010-01-15 18:08:01 +00:00
|
|
|
|
2016-08-23 16:40:16 +01:00
|
|
|
#include <QDebug>
|
2010-01-16 20:21:42 +00:00
|
|
|
#include <QFile>
|
2014-05-18 21:41:48 +01:00
|
|
|
#include <QFileDialog>
|
2010-01-16 20:21:42 +00:00
|
|
|
#include <QPointer>
|
2016-08-23 16:40:16 +01:00
|
|
|
#include <QString>
|
2010-01-15 18:08:01 +00:00
|
|
|
|
2010-01-16 20:21:42 +00:00
|
|
|
#include <mntent.h>
|
|
|
|
#include <blkid/blkid.h>
|
|
|
|
|
|
|
|
static QString findBlkIdDevice(const QString& token, const QString& value)
|
2010-01-15 18:08:01 +00:00
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
blkid_cache cache;
|
|
|
|
QString rval;
|
2010-01-15 18:08:01 +00:00
|
|
|
|
2016-04-14 19:18:36 +01:00
|
|
|
if (blkid_get_cache(&cache, nullptr) == 0) {
|
2015-07-13 15:17:41 +01:00
|
|
|
if (char* c = blkid_evaluate_tag(token.toLocal8Bit().constData(), value.toLocal8Bit().constData(), &cache)) {
|
|
|
|
rval = QString::fromLocal8Bit(c);
|
|
|
|
free(c);
|
|
|
|
}
|
2010-01-16 20:21:42 +00:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
blkid_put_cache(cache);
|
|
|
|
}
|
2010-01-16 20:21:42 +00:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
return rval;
|
2010-01-16 20:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EditMountPointDialogWidget::EditMountPointDialogWidget(QWidget* parent, const Partition& p) :
|
2015-07-13 15:17:41 +01:00
|
|
|
QWidget(parent),
|
|
|
|
m_Partition(p)
|
2010-01-16 20:21:42 +00:00
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
readMountpoints(QStringLiteral("/etc/fstab"));
|
|
|
|
|
|
|
|
setupUi(this);
|
|
|
|
|
2016-05-24 16:35:39 +01:00
|
|
|
m_deviceNode = partition().deviceNode();
|
|
|
|
if (partition().roles().has(PartitionRole::Luks) && partition().fileSystem().type() != FileSystem::Luks) {
|
|
|
|
const FS::luks* luksFs = dynamic_cast<const FS::luks*>(&partition().fileSystem());
|
2016-09-04 23:34:16 +01:00
|
|
|
m_deviceNode = luksFs->mapperName();
|
2016-05-24 16:35:39 +01:00
|
|
|
}
|
|
|
|
labelName().setText(m_deviceNode);
|
2015-07-13 15:17:41 +01:00
|
|
|
labelType().setText(partition().fileSystem().name());
|
|
|
|
|
2016-05-24 16:35:39 +01:00
|
|
|
if (mountPoints().find(m_deviceNode) == mountPoints().end())
|
|
|
|
mountPoints()[m_deviceNode] = new MountEntry(m_deviceNode, QString(), partition().fileSystem().name(), QStringList(), 0, 0, MountEntry::deviceNode);
|
2015-07-13 15:17:41 +01:00
|
|
|
|
2016-05-24 16:35:39 +01:00
|
|
|
MountEntry* entry = mountPoints()[m_deviceNode];
|
2015-07-13 15:17:41 +01:00
|
|
|
|
|
|
|
Q_ASSERT(entry);
|
|
|
|
|
|
|
|
if (entry) {
|
|
|
|
editPath().setText(entry->path);
|
|
|
|
|
|
|
|
spinDumpFreq().setValue(entry->dumpFreq);
|
|
|
|
spinPassNumber().setValue(entry->passNumber);
|
|
|
|
|
|
|
|
switch (entry->identifyType) {
|
|
|
|
case MountEntry::uuid:
|
|
|
|
radioUUID().setChecked(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MountEntry::label:
|
|
|
|
radioLabel().setChecked(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
radioDeviceNode().setChecked(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
boxOptions()[QStringLiteral("ro")] = m_CheckReadOnly;
|
|
|
|
boxOptions()[QStringLiteral("users")] = m_CheckUsers;
|
|
|
|
boxOptions()[QStringLiteral("noauto")] = m_CheckNoAuto;
|
|
|
|
boxOptions()[QStringLiteral("noatime")] = m_CheckNoAtime;
|
|
|
|
boxOptions()[QStringLiteral("nodiratime")] = m_CheckNoDirAtime;
|
|
|
|
boxOptions()[QStringLiteral("sync")] = m_CheckSync;
|
|
|
|
boxOptions()[QStringLiteral("noexec")] = m_CheckNoExec;
|
|
|
|
boxOptions()[QStringLiteral("relatime")] = m_CheckRelAtime;
|
|
|
|
|
|
|
|
setupOptions(entry->options);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (partition().fileSystem().uuid().isEmpty()) {
|
|
|
|
radioUUID().setEnabled(false);
|
|
|
|
if (radioUUID().isChecked())
|
|
|
|
radioDeviceNode().setChecked(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (partition().fileSystem().label().isEmpty()) {
|
|
|
|
radioLabel().setEnabled(false);
|
|
|
|
if (radioLabel().isChecked())
|
|
|
|
radioDeviceNode().setChecked(true);
|
|
|
|
}
|
2016-07-02 12:12:56 +01:00
|
|
|
|
|
|
|
connect(m_ButtonMore, &QPushButton::clicked, this, &EditMountPointDialogWidget::buttonMoreClicked);
|
|
|
|
connect(m_ButtonSelect, &QPushButton::clicked, this, &EditMountPointDialogWidget::buttonSelectClicked);
|
2010-01-15 18:08:01 +00:00
|
|
|
}
|
|
|
|
|
2010-01-16 20:21:42 +00:00
|
|
|
EditMountPointDialogWidget::~EditMountPointDialogWidget()
|
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
qDeleteAll(mountPoints().values());
|
2010-01-16 20:21:42 +00:00
|
|
|
}
|
|
|
|
|
2010-01-15 18:08:01 +00:00
|
|
|
void EditMountPointDialogWidget::setupOptions(const QStringList& options)
|
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
QStringList optTmpList;
|
2010-01-15 18:08:01 +00:00
|
|
|
|
2016-08-09 14:57:33 +01:00
|
|
|
for (const auto &o : options) {
|
2016-08-08 19:39:20 +01:00
|
|
|
if (boxOptions().find(o) != boxOptions().end())
|
|
|
|
boxOptions()[o]->setChecked(true);
|
|
|
|
else
|
|
|
|
optTmpList.append(o);
|
|
|
|
}
|
2010-01-15 18:08:01 +00:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
m_Options = optTmpList.join(QStringLiteral(","));
|
2010-01-15 18:08:01 +00:00
|
|
|
}
|
|
|
|
|
2016-07-02 12:12:56 +01:00
|
|
|
void EditMountPointDialogWidget::buttonSelectClicked(bool)
|
2010-01-15 18:08:01 +00:00
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
const QString s = QFileDialog::getExistingDirectory(this, editPath().text());
|
|
|
|
if (!s.isEmpty())
|
|
|
|
editPath().setText(s);
|
2010-01-15 18:08:01 +00:00
|
|
|
}
|
|
|
|
|
2016-07-02 12:12:56 +01:00
|
|
|
void EditMountPointDialogWidget::buttonMoreClicked(bool)
|
2010-01-15 18:08:01 +00:00
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
QPointer<EditMountOptionsDialog> dlg = new EditMountOptionsDialog(this, m_Options.split(QStringLiteral(",")));
|
2010-01-16 20:21:42 +00:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
if (dlg->exec() == QDialog::Accepted)
|
|
|
|
setupOptions(dlg->options());
|
2010-01-16 20:21:42 +00:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
delete dlg;
|
2010-01-15 18:08:01 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 14:57:33 +01:00
|
|
|
QStringList EditMountPointDialogWidget::options() const
|
2010-01-15 18:08:01 +00:00
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
QStringList optList = m_Options.split(QStringLiteral(","), QString::SkipEmptyParts);
|
2010-01-15 18:08:01 +00:00
|
|
|
|
2016-09-01 00:44:33 +01:00
|
|
|
const auto keys = boxOptions().keys();
|
|
|
|
for (const auto &s : keys)
|
2016-08-08 19:39:20 +01:00
|
|
|
if (boxOptions()[s]->isChecked())
|
|
|
|
optList.append(s);
|
2010-01-15 18:08:01 +00:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
return optList;
|
2010-01-15 18:08:01 +00:00
|
|
|
}
|
|
|
|
|
2010-01-16 20:21:42 +00:00
|
|
|
bool EditMountPointDialogWidget::readMountpoints(const QString& filename)
|
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
FILE* fp = setmntent(filename.toLocal8Bit().constData(), "r");
|
|
|
|
|
2016-04-14 19:18:36 +01:00
|
|
|
if (fp == nullptr) {
|
2015-07-13 15:17:41 +01:00
|
|
|
KMessageBox::sorry(this,
|
|
|
|
xi18nc("@info", "Could not open mount point file <filename>%1</filename>.", filename),
|
2016-07-18 02:21:14 +01:00
|
|
|
xi18nc("@title:window", "Error while reading mount points"));
|
2015-07-13 15:17:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-14 19:18:36 +01:00
|
|
|
struct mntent* mnt = nullptr;
|
2015-07-13 15:17:41 +01:00
|
|
|
|
2016-04-14 19:18:36 +01:00
|
|
|
while ((mnt = getmntent(fp)) != nullptr) {
|
2015-07-13 15:17:41 +01:00
|
|
|
QString device = QString::fromUtf8(mnt->mnt_fsname);
|
|
|
|
MountEntry::IdentifyType type = MountEntry::deviceNode;
|
|
|
|
|
|
|
|
if (device.startsWith(QStringLiteral("UUID="))) {
|
|
|
|
type = MountEntry::uuid;
|
|
|
|
device = findBlkIdDevice(QStringLiteral("UUID"), QString(device).remove(QStringLiteral("UUID=")));
|
|
|
|
} else if (device.startsWith(QStringLiteral("LABEL="))) {
|
|
|
|
type = MountEntry::label;
|
|
|
|
device = findBlkIdDevice(QStringLiteral("LABEL"), QString(device).remove(QStringLiteral("LABEL=")));
|
|
|
|
} else if (device.startsWith(QStringLiteral("/")))
|
|
|
|
device = QFile::symLinkTarget(device);
|
|
|
|
|
|
|
|
if (!device.isEmpty()) {
|
|
|
|
QString mountPoint = QString::fromUtf8(mnt->mnt_dir);
|
|
|
|
mountPoints()[device] = new MountEntry(mnt, type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
endmntent(fp);
|
|
|
|
|
|
|
|
return true;
|
2010-01-16 20:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void writeEntry(QFile& output, const MountEntry* entry)
|
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
Q_ASSERT(entry);
|
2010-01-16 20:21:42 +00:00
|
|
|
|
2016-04-14 19:18:36 +01:00
|
|
|
if (entry == nullptr)
|
2015-07-13 15:17:41 +01:00
|
|
|
return;
|
2010-01-16 20:21:42 +00:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
if (entry->path.isEmpty())
|
|
|
|
return;
|
2010-01-16 22:31:39 +00:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
QTextStream s(&output);
|
2010-01-16 22:16:04 +00:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
s << entry->name << "\t"
|
|
|
|
<< entry->path << "\t"
|
|
|
|
<< entry->type << "\t"
|
|
|
|
<< (entry->options.size() > 0 ? entry->options.join(QStringLiteral(",")) : QStringLiteral("defaults")) << "\t"
|
|
|
|
<< entry->dumpFreq << "\t"
|
|
|
|
<< entry->passNumber << "\n";
|
2010-01-16 20:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EditMountPointDialogWidget::acceptChanges()
|
|
|
|
{
|
2016-04-14 19:18:36 +01:00
|
|
|
MountEntry* entry = nullptr;
|
2010-01-16 20:21:42 +00:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
if (mountPoints().find(labelName().text()) == mountPoints().end()) {
|
|
|
|
qWarning() << "could not find device " << labelName().text() << " in mount points.";
|
|
|
|
return false;
|
|
|
|
}
|
2010-01-16 20:21:42 +00:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
entry = mountPoints()[labelName().text()];
|
2010-01-16 22:16:04 +00:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
entry->dumpFreq = spinDumpFreq().value();
|
|
|
|
entry->passNumber = spinPassNumber().value();
|
|
|
|
entry->path = editPath().text();
|
|
|
|
entry->options = options();
|
2010-01-16 22:28:52 +00:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
if (radioUUID().isChecked() && !partition().fileSystem().uuid().isEmpty())
|
|
|
|
entry->name = QStringLiteral("UUID=") + partition().fileSystem().uuid();
|
|
|
|
else if (radioLabel().isChecked() && !partition().fileSystem().label().isEmpty())
|
|
|
|
entry->name = QStringLiteral("LABEL=") + partition().fileSystem().label();
|
|
|
|
else
|
2016-05-24 16:35:39 +01:00
|
|
|
entry->name = m_deviceNode;
|
2010-01-16 20:21:42 +00:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
return true;
|
2010-01-16 20:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EditMountPointDialogWidget::writeMountpoints(const QString& filename)
|
2010-01-15 18:08:01 +00:00
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
bool rval = true;
|
|
|
|
const QString newFilename = QStringLiteral("%1.new").arg(filename);
|
|
|
|
QFile out(newFilename);
|
|
|
|
|
|
|
|
if (!out.open(QFile::ReadWrite | QFile::Truncate)) {
|
|
|
|
qWarning() << "could not open output file " << newFilename;
|
|
|
|
rval = false;
|
|
|
|
} else {
|
2016-09-01 00:44:33 +01:00
|
|
|
const auto mp = mountPoints();
|
|
|
|
for (const auto &me : mp)
|
2016-08-08 19:39:20 +01:00
|
|
|
writeEntry(out, me);
|
2015-07-13 15:17:41 +01:00
|
|
|
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
const QString bakFilename = QStringLiteral("%1.bak").arg(filename);
|
|
|
|
QFile::remove(bakFilename);
|
|
|
|
|
|
|
|
if (QFile::exists(filename) && !QFile::rename(filename, bakFilename)) {
|
|
|
|
qWarning() << "could not rename " << filename << " to " << bakFilename;
|
|
|
|
rval = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rval && !QFile::rename(newFilename, filename)) {
|
|
|
|
qWarning() << "could not rename " << newFilename << " to " << filename;
|
|
|
|
rval = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rval)
|
|
|
|
KMessageBox::sorry(this,
|
|
|
|
xi18nc("@info", "Could not save mount points to file <filename>%1</filename>.", filename),
|
2016-07-18 02:21:14 +01:00
|
|
|
xi18nc("@title:window", "Error While Saving Mount Points"));
|
2015-07-13 15:17:41 +01:00
|
|
|
|
|
|
|
return rval;
|
2010-01-15 18:08:01 +00:00
|
|
|
}
|