Prettier formatting of fstab file.
This commit is contained in:
parent
2ed9953694
commit
1d195b00da
|
@ -21,6 +21,8 @@
|
||||||
#include "util/externalcommand.h"
|
#include "util/externalcommand.h"
|
||||||
#include "util/report.h"
|
#include "util/report.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
#if defined(Q_OS_LINUX)
|
#if defined(Q_OS_LINUX)
|
||||||
#include <blkid/blkid.h>
|
#include <blkid/blkid.h>
|
||||||
#endif
|
#endif
|
||||||
|
@ -34,6 +36,8 @@
|
||||||
|
|
||||||
static void parseFsSpec(const QString& m_fsSpec, FstabEntry::Type& m_entryType, QString& m_deviceNode);
|
static void parseFsSpec(const QString& m_fsSpec, FstabEntry::Type& m_entryType, QString& m_deviceNode);
|
||||||
static QString findBlkIdDevice(const char *token, const QString& value);
|
static QString findBlkIdDevice(const char *token, const QString& value);
|
||||||
|
static void writeEntry(QTextStream& s, const FstabEntry& entry, std::array<unsigned int, 4> columnWidth);
|
||||||
|
std::array<unsigned int, 4> fstabColumnWidth(const FstabEntryList& fstabEntries);
|
||||||
|
|
||||||
struct FstabEntryPrivate
|
struct FstabEntryPrivate
|
||||||
{
|
{
|
||||||
|
@ -142,6 +146,11 @@ const QStringList& FstabEntry::options() const
|
||||||
return d->m_options;
|
return d->m_options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const QString FstabEntry::optionsString() const
|
||||||
|
{
|
||||||
|
return options().size() > 0 ? options().join(QLatin1Char(',')) : QStringLiteral("defaults");
|
||||||
|
}
|
||||||
|
|
||||||
int FstabEntry::dumpFreq() const
|
int FstabEntry::dumpFreq() const
|
||||||
{
|
{
|
||||||
return d->m_dumpFreq;
|
return d->m_dumpFreq;
|
||||||
|
@ -232,28 +241,37 @@ static void parseFsSpec(const QString& m_fsSpec, FstabEntry::Type& m_entryType,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void writeEntry(QTextStream& s, const FstabEntry& entry)
|
|
||||||
|
// Used to nicely format fstab file
|
||||||
|
std::array<unsigned int, 4> fstabColumnWidth(const FstabEntryList& fstabEntries)
|
||||||
|
{
|
||||||
|
std::array<unsigned int, 4> columnWidth;
|
||||||
|
|
||||||
|
#define FIELD_WIDTH(x) 3 + std::max_element(fstabEntries.begin(), fstabEntries.end(), [](const FstabEntry& a, const FstabEntry& b) {return a.x().length() < b.x().length(); })->x().length();
|
||||||
|
|
||||||
|
columnWidth[0] = FIELD_WIDTH(fsSpec);
|
||||||
|
columnWidth[1] = FIELD_WIDTH(mountPoint);
|
||||||
|
columnWidth[2] = FIELD_WIDTH(type);
|
||||||
|
columnWidth[3] = FIELD_WIDTH(optionsString);
|
||||||
|
|
||||||
|
return columnWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void writeEntry(QTextStream& s, const FstabEntry& entry, std::array<unsigned int, 4> columnWidth)
|
||||||
{
|
{
|
||||||
if (entry.entryType() == FstabEntry::Type::comment) {
|
if (entry.entryType() == FstabEntry::Type::comment) {
|
||||||
s << entry.comment() << "\n";
|
s << entry.comment() << "\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString options;
|
s.setFieldAlignment(QTextStream::AlignLeft);
|
||||||
if (entry.options().size() > 0) {
|
s.setFieldWidth(columnWidth[0]);
|
||||||
options = entry.options().join(QLatin1Char(','));
|
s << entry.fsSpec()
|
||||||
if (options.isEmpty())
|
<< qSetFieldWidth(columnWidth[1]) << (entry.mountPoint().isEmpty() ? QStringLiteral("none") : entry.mountPoint())
|
||||||
options = QStringLiteral("defaults");
|
<< qSetFieldWidth(columnWidth[2]) << entry.type()
|
||||||
}
|
<< qSetFieldWidth(columnWidth[3]) << entry.optionsString() << qSetFieldWidth(0)
|
||||||
else
|
<< entry.dumpFreq() << " "
|
||||||
options = QStringLiteral("defaults");
|
<< entry.passNumber() << " "
|
||||||
|
|
||||||
s << entry.fsSpec() << "\t"
|
|
||||||
<< (entry.mountPoint().isEmpty() ? QStringLiteral("none") : entry.mountPoint()) << "\t"
|
|
||||||
<< entry.type() << "\t"
|
|
||||||
<< options << "\t"
|
|
||||||
<< entry.dumpFreq() << "\t"
|
|
||||||
<< entry.passNumber() << "\t"
|
|
||||||
<< entry.comment() << "\n";
|
<< entry.comment() << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -262,8 +280,10 @@ bool writeMountpoints(const FstabEntryList& fstabEntries, const QString& filenam
|
||||||
QString fstabContents;
|
QString fstabContents;
|
||||||
QTextStream out(&fstabContents);
|
QTextStream out(&fstabContents);
|
||||||
|
|
||||||
|
std::array<unsigned int, 4> columnWidth = fstabColumnWidth(fstabEntries);
|
||||||
|
|
||||||
for (const auto &e : fstabEntries)
|
for (const auto &e : fstabEntries)
|
||||||
writeEntry(out, e);
|
writeEntry(out, e, columnWidth);
|
||||||
|
|
||||||
ExternalCommand cmd;
|
ExternalCommand cmd;
|
||||||
return cmd.createFile(fstabContents.toLocal8Bit(), filename);
|
return cmd.createFile(fstabContents.toLocal8Bit(), filename);
|
||||||
|
|
|
@ -66,6 +66,11 @@ public:
|
||||||
*/
|
*/
|
||||||
const QStringList& options() const;
|
const QStringList& options() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the mount options associated with the file system
|
||||||
|
*/
|
||||||
|
const QString optionsString() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the fs_freq field of fstab entry
|
* @return the fs_freq field of fstab entry
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue