2010-01-15 18:08:01 +00:00
|
|
|
/***************************************************************************
|
2010-01-16 20:21:42 +00:00
|
|
|
* Copyright (C) 2009,2010 by Volker Lanz <vl@fidra.de> *
|
2010-01-15 18:08:01 +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 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. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
2010-01-16 20:21:42 +00:00
|
|
|
#include "gui/editmountpointdialogwidget.h"
|
|
|
|
#include "gui/editmountoptionsdialog.h"
|
2010-01-15 18:08:01 +00:00
|
|
|
|
2010-01-16 20:21:42 +00:00
|
|
|
#include "core/partition.h"
|
2010-06-01 23:07:13 +01:00
|
|
|
#include "core/mountentry.h"
|
2010-01-16 20:21:42 +00:00
|
|
|
|
|
|
|
#include "fs/filesystem.h"
|
|
|
|
|
2014-05-13 23:53:37 +01:00
|
|
|
#include <KLocalizedString>
|
2010-01-16 20:21:42 +00:00
|
|
|
#include <kfiledialog.h>
|
|
|
|
#include <klineedit.h>
|
|
|
|
#include <kmountpoint.h>
|
|
|
|
#include <kmessagebox.h>
|
2010-01-15 18:08:01 +00:00
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
#include <QWidget>
|
2010-01-16 20:21:42 +00:00
|
|
|
#include <QFile>
|
|
|
|
#include <QPointer>
|
2014-05-13 14:47:35 +01:00
|
|
|
#include <QDebug>
|
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
|
|
|
{
|
2010-01-16 20:21:42 +00:00
|
|
|
blkid_cache cache;
|
|
|
|
QString rval;
|
2010-01-15 18:08:01 +00:00
|
|
|
|
2010-01-16 20:21:42 +00:00
|
|
|
if (blkid_get_cache(&cache, NULL) == 0)
|
2010-01-15 18:08:01 +00:00
|
|
|
{
|
2010-01-16 20:21:42 +00:00
|
|
|
if (char* c = blkid_evaluate_tag(token.toLocal8Bit(), value.toLocal8Bit(), &cache))
|
|
|
|
{
|
|
|
|
rval = c;
|
|
|
|
free(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
blkid_put_cache(cache);
|
2010-01-15 18:08:01 +00:00
|
|
|
}
|
2010-01-16 20:21:42 +00:00
|
|
|
|
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
EditMountPointDialogWidget::EditMountPointDialogWidget(QWidget* parent, const Partition& p) :
|
2010-01-16 22:16:04 +00:00
|
|
|
QWidget(parent),
|
|
|
|
m_Partition(p)
|
2010-01-16 20:21:42 +00:00
|
|
|
{
|
|
|
|
readMountpoints("/etc/fstab");
|
|
|
|
|
|
|
|
setupUi(this);
|
|
|
|
|
2010-01-16 22:16:04 +00:00
|
|
|
labelName().setText(partition().deviceNode());
|
|
|
|
labelType().setText(partition().fileSystem().name());
|
2010-01-16 20:21:42 +00:00
|
|
|
|
2010-01-16 22:16:04 +00:00
|
|
|
if (mountPoints().find(partition().deviceNode()) == mountPoints().end())
|
|
|
|
mountPoints()[partition().deviceNode()] = new MountEntry(partition().deviceNode(), QString(), partition().fileSystem().name(), QStringList(), 0, 0, MountEntry::deviceNode);
|
2010-01-16 20:21:42 +00:00
|
|
|
|
2010-01-16 22:16:04 +00:00
|
|
|
MountEntry* entry = mountPoints()[partition().deviceNode()];
|
2010-01-16 20:21:42 +00:00
|
|
|
|
|
|
|
Q_ASSERT(entry);
|
|
|
|
|
|
|
|
if (entry)
|
2010-01-15 18:08:01 +00:00
|
|
|
{
|
2010-01-16 20:21:42 +00:00
|
|
|
editPath().setText(entry->path);
|
|
|
|
|
|
|
|
spinDumpFreq().setValue(entry->dumpFreq);
|
|
|
|
spinPassNumber().setValue(entry->passNumber);
|
|
|
|
|
2010-01-16 22:16:04 +00:00
|
|
|
switch(entry->identifyType)
|
|
|
|
{
|
|
|
|
case MountEntry::uuid:
|
|
|
|
radioUUID().setChecked(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MountEntry::label:
|
|
|
|
radioLabel().setChecked(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
radioDeviceNode().setChecked(true);
|
|
|
|
}
|
|
|
|
|
2010-01-16 20:21:42 +00:00
|
|
|
boxOptions()["ro"] = m_CheckReadOnly;
|
|
|
|
boxOptions()["users"] = m_CheckUsers;
|
|
|
|
boxOptions()["noauto"] = m_CheckNoAuto;
|
|
|
|
boxOptions()["noatime"] = m_CheckNoAtime;
|
|
|
|
boxOptions()["nodiratime"] = m_CheckNoDirAtime;
|
|
|
|
boxOptions()["sync"] = m_CheckSync;
|
|
|
|
boxOptions()["noexec"] = m_CheckNoExec;
|
|
|
|
boxOptions()["relatime"] = m_CheckRelAtime;
|
|
|
|
|
|
|
|
setupOptions(entry->options);
|
2010-01-15 18:08:01 +00:00
|
|
|
}
|
2010-01-16 22:16:04 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2010-01-15 18:08:01 +00:00
|
|
|
}
|
|
|
|
|
2010-01-16 20:21:42 +00:00
|
|
|
EditMountPointDialogWidget::~EditMountPointDialogWidget()
|
|
|
|
{
|
|
|
|
qDeleteAll(mountPoints().values());
|
|
|
|
}
|
|
|
|
|
2010-01-15 18:08:01 +00:00
|
|
|
void EditMountPointDialogWidget::setupOptions(const QStringList& options)
|
|
|
|
{
|
|
|
|
QStringList optTmpList;
|
|
|
|
|
2010-01-16 20:21:42 +00:00
|
|
|
foreach (const QString& o, options)
|
2010-01-15 18:08:01 +00:00
|
|
|
if (boxOptions().find(o) != boxOptions().end())
|
|
|
|
boxOptions()[o]->setChecked(true);
|
|
|
|
else
|
|
|
|
optTmpList.append(o);
|
|
|
|
|
|
|
|
m_Options = optTmpList.join(",");
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditMountPointDialogWidget::on_m_ButtonSelect_clicked(bool)
|
|
|
|
{
|
|
|
|
const QString s = KFileDialog::getExistingDirectory(KUrl(editPath().text()), this);
|
|
|
|
if (!s.isEmpty())
|
|
|
|
editPath().setText(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditMountPointDialogWidget::on_m_ButtonMore_clicked(bool)
|
|
|
|
{
|
2010-01-16 20:21:42 +00:00
|
|
|
QPointer<EditMountOptionsDialog> dlg = new EditMountOptionsDialog(this, m_Options.split(','));
|
|
|
|
|
2014-05-17 03:06:58 +01:00
|
|
|
if (dlg->exec() == QDialog::Accepted)
|
2010-01-16 20:21:42 +00:00
|
|
|
setupOptions(dlg->options());
|
|
|
|
|
|
|
|
delete dlg;
|
2010-01-15 18:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList EditMountPointDialogWidget::options()
|
|
|
|
{
|
|
|
|
QStringList optList = m_Options.split(',', QString::SkipEmptyParts);
|
|
|
|
|
|
|
|
foreach (const QString& s, boxOptions().keys())
|
|
|
|
if (boxOptions()[s]->isChecked())
|
|
|
|
optList.append(s);
|
|
|
|
|
|
|
|
return optList;
|
|
|
|
}
|
|
|
|
|
2010-01-16 20:21:42 +00:00
|
|
|
bool EditMountPointDialogWidget::readMountpoints(const QString& filename)
|
|
|
|
{
|
|
|
|
FILE* fp = setmntent(filename.toLocal8Bit(), "r");
|
|
|
|
|
|
|
|
if (fp == NULL)
|
|
|
|
{
|
|
|
|
KMessageBox::sorry(this,
|
|
|
|
i18nc("@info", "Could not open mount point file <filename>%1</filename>.", filename),
|
|
|
|
i18nc("@title:window", "Error while reading mount points"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-16 22:16:04 +00:00
|
|
|
struct mntent* mnt = NULL;
|
2010-01-16 20:21:42 +00:00
|
|
|
|
2010-01-16 22:16:04 +00:00
|
|
|
while ((mnt = getmntent(fp)) != NULL)
|
2010-01-16 20:21:42 +00:00
|
|
|
{
|
2010-01-16 22:16:04 +00:00
|
|
|
QString device = mnt->mnt_fsname;
|
|
|
|
MountEntry::IdentifyType type = MountEntry::deviceNode;
|
2010-01-16 20:21:42 +00:00
|
|
|
|
|
|
|
if (device.startsWith("UUID="))
|
2010-01-16 22:16:04 +00:00
|
|
|
{
|
|
|
|
type = MountEntry::uuid;
|
2010-01-16 20:21:42 +00:00
|
|
|
device = findBlkIdDevice("UUID", QString(device).remove("UUID="));
|
2010-01-16 22:16:04 +00:00
|
|
|
}
|
2010-01-16 20:21:42 +00:00
|
|
|
else if (device.startsWith("LABEL="))
|
2010-01-16 22:16:04 +00:00
|
|
|
{
|
|
|
|
type = MountEntry::label;
|
2010-01-16 20:21:42 +00:00
|
|
|
device = findBlkIdDevice("LABEL", QString(device).remove("LABEL="));
|
2010-01-16 22:16:04 +00:00
|
|
|
}
|
2010-08-08 13:36:05 +01:00
|
|
|
else if (device.startsWith('/'))
|
2010-05-26 17:52:20 +01:00
|
|
|
device = QFile::symLinkTarget(device);
|
2010-01-16 20:21:42 +00:00
|
|
|
|
|
|
|
if (!device.isEmpty())
|
|
|
|
{
|
2010-01-16 22:16:04 +00:00
|
|
|
QString mountPoint = mnt->mnt_dir;
|
|
|
|
mountPoints()[device] = new MountEntry(mnt, type);
|
2010-01-16 20:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
endmntent(fp);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void writeEntry(QFile& output, const MountEntry* entry)
|
|
|
|
{
|
|
|
|
Q_ASSERT(entry);
|
|
|
|
|
|
|
|
if (entry == NULL)
|
|
|
|
return;
|
|
|
|
|
2010-01-16 22:31:39 +00:00
|
|
|
if (entry->path.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2010-01-16 20:21:42 +00:00
|
|
|
QTextStream s(&output);
|
2010-01-16 22:16:04 +00:00
|
|
|
|
2010-01-16 20:21:42 +00:00
|
|
|
s << entry->name << "\t"
|
|
|
|
<< entry->path << "\t"
|
|
|
|
<< entry->type << "\t"
|
|
|
|
<< (entry->options.size() > 0 ? entry->options.join(",") : "defaults") << "\t"
|
|
|
|
<< entry->dumpFreq << "\t"
|
|
|
|
<< entry->passNumber << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditMountPointDialogWidget::acceptChanges()
|
|
|
|
{
|
2010-01-16 22:16:04 +00:00
|
|
|
MountEntry* entry = NULL;
|
2010-01-16 20:21:42 +00:00
|
|
|
|
|
|
|
if (mountPoints().find(labelName().text()) == mountPoints().end())
|
|
|
|
{
|
2014-05-13 14:47:35 +01:00
|
|
|
qWarning() << "could not find device " << labelName().text() << " in mount points.";
|
2010-01-16 20:21:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-16 22:28:52 +00:00
|
|
|
entry = mountPoints()[labelName().text()];
|
2010-01-16 22:16:04 +00:00
|
|
|
|
2010-01-16 22:28:52 +00:00
|
|
|
entry->dumpFreq = spinDumpFreq().value();
|
|
|
|
entry->passNumber = spinPassNumber().value();
|
|
|
|
entry->path = editPath().text();
|
|
|
|
entry->options = options();
|
|
|
|
|
|
|
|
if (radioUUID().isChecked() && !partition().fileSystem().uuid().isEmpty())
|
|
|
|
entry->name = "UUID=" + partition().fileSystem().uuid();
|
|
|
|
else if (radioLabel().isChecked() && !partition().fileSystem().label().isEmpty())
|
|
|
|
entry->name = "LABEL=" + partition().fileSystem().label();
|
|
|
|
else
|
|
|
|
entry->name = partition().deviceNode();
|
2010-01-16 20:21:42 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditMountPointDialogWidget::writeMountpoints(const QString& filename)
|
2010-01-15 18:08:01 +00:00
|
|
|
{
|
2010-01-16 20:21:42 +00:00
|
|
|
bool rval = true;
|
|
|
|
const QString newFilename = QString("%1.new").arg(filename);
|
|
|
|
QFile out(newFilename);
|
|
|
|
|
|
|
|
if (!out.open(QFile::ReadWrite | QFile::Truncate))
|
|
|
|
{
|
2014-05-13 14:47:35 +01:00
|
|
|
qWarning() << "could not open output file " << newFilename;
|
2010-01-16 20:21:42 +00:00
|
|
|
rval = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach (const MountEntry* me, mountPoints())
|
|
|
|
writeEntry(out, me);
|
|
|
|
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
const QString bakFilename = QString("%1.bak").arg(filename);
|
|
|
|
QFile::remove(bakFilename);
|
|
|
|
|
2010-05-17 16:14:19 +01:00
|
|
|
if (QFile::exists(filename) && !QFile::rename(filename, bakFilename))
|
2010-01-16 20:21:42 +00:00
|
|
|
{
|
2014-05-13 14:47:35 +01:00
|
|
|
qWarning() << "could not rename " << filename << " to " << bakFilename;
|
2010-01-16 20:21:42 +00:00
|
|
|
rval = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rval && !QFile::rename(newFilename, filename))
|
|
|
|
{
|
2014-05-13 14:47:35 +01:00
|
|
|
qWarning() << "could not rename " << newFilename << " to " << filename;
|
2010-01-16 20:21:42 +00:00
|
|
|
rval = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rval)
|
|
|
|
KMessageBox::sorry(this,
|
|
|
|
i18nc("@info", "Could not save mount points to file <filename>%1</filename>.", filename),
|
2010-03-11 16:34:48 +00:00
|
|
|
i18nc("@title:window", "Error While Saving Mount Points"));
|
2010-01-15 18:08:01 +00:00
|
|
|
|
2010-01-16 20:21:42 +00:00
|
|
|
return rval;
|
2010-01-15 18:08:01 +00:00
|
|
|
}
|