Add a function to write fstab file.

CCBUG: 305469
This commit is contained in:
Andrius Štikonas 2017-09-16 20:40:46 +01:00
parent 8719fb0bf3
commit baec157314
2 changed files with 54 additions and 0 deletions

View File

@ -22,9 +22,11 @@
#include <blkid/blkid.h>
#include <QChar>
#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QRegularExpression>
#include <QTextStream>
static void parseFsSpec(const QString& m_fsSpec, FstabEntryType& m_entryType, QString& m_deviceNode);
static QString findBlkIdDevice(const QString& token, const QString& value);
@ -92,6 +94,8 @@ FstabEntryList readFstabEntries( const QString& fstabPath )
}
fstabFile.close();
if (fstabEntries.last().entryType() == comment && fstabEntries.last().comment().isEmpty())
fstabEntries.removeLast();
}
return fstabEntries;
@ -148,3 +152,52 @@ static void parseFsSpec(const QString& m_fsSpec, FstabEntryType& m_entryType, QS
m_deviceNode = m_fsSpec;
}
}
static void writeEntry(QFile& output, const FstabEntry& entry)
{
QTextStream s(&output);
if (entry.entryType() == FstabEntryType::comment) {
s << entry.comment() << "\n";
return;
}
s << entry.fsSpec() << "\t"
<< entry.mountPoint() << "\t"
<< entry.type() << "\t"
<< (entry.options().size() > 0 ? entry.options().join(QLatin1Char(',')) : QStringLiteral("defaults")) << "\t"
<< entry.dumpFreq() << "\t"
<< entry.passNumber() << "\t"
<< entry.comment() << "\n";
}
bool writeMountpoints(const FstabEntryList fstabEntries, const QString& filename)
{
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 {
for (const auto &e : fstabEntries)
writeEntry(out, e);
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;
}
}
return rval;
}

View File

@ -94,5 +94,6 @@ typedef QList<FstabEntry> FstabEntryList;
LIBKPMCORE_EXPORT FstabEntryList readFstabEntries(const QString& fstabPath = QStringLiteral("/etc/fstab"));
LIBKPMCORE_EXPORT QStringList possibleMountPoints(const QString& deviceNode, const QString& fstabPath = QStringLiteral("/etc/fstab"));
LIBKPMCORE_EXPORT bool writeMountpoints(const FstabEntryList fstabEntries, const QString& filename = QStringLiteral("/etc/fstab"));
#endif