Implement udf::labelValidator() for validating UDF label
This would rapidly simplify UDF code if all checks are done by caller. Also checks for MIN_UDF_BLOCKS/MAX_UDF_BLOCKS are removed as they should be covered by udf::minCapacity() and udf::maxCapacity().
This commit is contained in:
parent
58f3d09d71
commit
9cc6596cb4
|
@ -24,6 +24,7 @@
|
|||
#include <KLocalizedString>
|
||||
|
||||
#include <QRegularExpression>
|
||||
#include <QRegularExpressionValidator>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
|
||||
|
@ -33,7 +34,7 @@ constexpr qint64 MIN_UDF_BLOCKS = 282;
|
|||
constexpr qint64 MAX_UDF_BLOCKS = ((1ULL << 32) - 1);
|
||||
|
||||
FileSystem::CommandSupportType udf::m_Create = FileSystem::cmdSupportNone;
|
||||
bool udf::m_OnlyAsciiLabel = false;
|
||||
QRegularExpressionValidator udf::m_LabelValidator;
|
||||
|
||||
udf::udf(qint64 firstsector, qint64 lastsector, qint64 sectorsused, const QString& label) :
|
||||
FileSystem(firstsector, lastsector, sectorsused, label, FileSystem::Udf)
|
||||
|
@ -44,9 +45,22 @@ void udf::init()
|
|||
{
|
||||
m_Create = findExternal(QStringLiteral("mkudffs"), {}, 1) ? cmdSupportFileSystem : cmdSupportNone;
|
||||
|
||||
// Detect old mkudffs prior to version 1.1 by lack of --label option
|
||||
ExternalCommand cmd(QStringLiteral("mkudffs"), { QStringLiteral("--help") });
|
||||
m_OnlyAsciiLabel = cmd.run(-1) && !cmd.output().contains(QStringLiteral("--label"));
|
||||
if (m_Create == cmdSupportFileSystem) {
|
||||
// Detect old mkudffs prior to version 1.1 by lack of --label option
|
||||
ExternalCommand cmd(QStringLiteral("mkudffs"), { QStringLiteral("--help") });
|
||||
bool oldMkudffsVersion = cmd.run(-1) && !cmd.output().contains(QStringLiteral("--label"));
|
||||
|
||||
if (oldMkudffsVersion) {
|
||||
// Mkudffs from udftools prior to version 1.1 damages the label if it
|
||||
// contains non-ASCII characters. Therefore do not allow a label with
|
||||
// such characters with old versions of mkudffs.
|
||||
m_LabelValidator.setRegularExpression(QRegularExpression(QStringLiteral("[\\x{0001}-\\x{007F}]{0,126}")));
|
||||
} else {
|
||||
// UDF label can only contain 126 bytes, either 126 ISO-8859-1
|
||||
// (Latin 1) characters or 63 UCS-2BE characters.
|
||||
m_LabelValidator.setRegularExpression(QRegularExpression(QStringLiteral("[\\x{0001}-\\x{00FF}]{0,126}|[\\x{0001}-\\x{FFFF}]{0,63}")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool udf::supportToolFound() const
|
||||
|
@ -71,7 +85,12 @@ qint64 udf::maxCapacity() const
|
|||
|
||||
qint64 udf::maxLabelLength() const
|
||||
{
|
||||
return 126; // and only 63 if label contains character above U+FF
|
||||
return 126;
|
||||
}
|
||||
|
||||
QValidator* udf::labelValidator(QObject *parent)
|
||||
{
|
||||
return &m_LabelValidator;
|
||||
}
|
||||
|
||||
bool udf::create(Report& report, const QString& deviceNode)
|
||||
|
@ -81,46 +100,17 @@ bool udf::create(Report& report, const QString& deviceNode)
|
|||
|
||||
bool udf::createWithLabel(Report& report, const QString& deviceNode, const QString& label)
|
||||
{
|
||||
// mkudffs from udftools prior to 1.1 does not check for partition limits and crashes
|
||||
if (length() > MAX_UDF_BLOCKS) {
|
||||
report.line() << xi18nc("@info:status", "Partition is too large");
|
||||
return false;
|
||||
}
|
||||
if (length() < MIN_UDF_BLOCKS) {
|
||||
report.line() << xi18nc("@info:status", "Partition is too small");
|
||||
return false;
|
||||
}
|
||||
|
||||
// It is not possible to create UDF filesystem without a label or with empty label
|
||||
// When --lvid or --vid option is not specified, mkudffs use sane default
|
||||
QStringList labelArgs;
|
||||
if (!label.isEmpty()) {
|
||||
const QRegularExpression nonAsciiRegExp = QRegularExpression(QStringLiteral("[^\\x{0000}-\\x{007F}]"));
|
||||
const QRegularExpression nonLatin1RegExp = QRegularExpression(QStringLiteral("[^\\x{0000}-\\x{00FF}]"));
|
||||
|
||||
// Mkudffs from udftools prior to version 1.1 damages the label if it
|
||||
// contains non-ASCII characters. Therefore do not allow a label with
|
||||
// such characters with old versions of mkudffs.
|
||||
if (m_OnlyAsciiLabel && label.contains(nonAsciiRegExp)) {
|
||||
report.line() << xi18nc("@info:status", "mkudffs prior to version 1.1 does not support non-ASCII characters in the label");
|
||||
return false;
|
||||
}
|
||||
|
||||
// NOTE: According to the OSTA specification, UDF supports only strings
|
||||
// encoded in 8-bit or 16-bit OSTA Compressed Unicode format. They are
|
||||
// equivalent to ISO-8859-1 (Latin 1) and UCS-2BE respectively.
|
||||
// Conversion from UTF-8 passed on the command line to OSTA format is done
|
||||
// by mkudffs. Strictly speaking UDF does not support UTF-16 as the UDF
|
||||
// specification was created before the introduction of UTF-16, but lots
|
||||
// of UDF tools are able to decode UTF-16 including UTF-16 Surrogate pairs
|
||||
// outside the BMP (Basic Multilingual Plane).
|
||||
//
|
||||
// The Volume Identifier (--vid) can only contain 30 bytes, either 30
|
||||
// ISO-8859-1 (Latin 1) characters or 15 UCS-2BE characters. Store the
|
||||
// most characters possible in the Volume Identifier. Either up to 15
|
||||
// UCS-2BE characters when a character needing 16-bit encoding is found in
|
||||
// the first 15 characters, or up to 30 characters when a character
|
||||
// needing 16-bit encoding is found in the second 15 characters.
|
||||
const QRegularExpression nonLatin1RegExp = QRegularExpression(QStringLiteral("[^\\x{0000}-\\x{00FF}]"));
|
||||
QString shortLabel = label.left(30);
|
||||
int firstNonLatin1Pos = shortLabel.indexOf(nonLatin1RegExp);
|
||||
if (firstNonLatin1Pos != -1 && firstNonLatin1Pos < 15)
|
||||
|
@ -128,18 +118,22 @@ bool udf::createWithLabel(Report& report, const QString& deviceNode, const QStri
|
|||
else if (firstNonLatin1Pos != -1 && firstNonLatin1Pos < 30)
|
||||
shortLabel = shortLabel.left(firstNonLatin1Pos);
|
||||
|
||||
// UDF Logical Volume Identifier (--lvid) represents the label, but blkid
|
||||
// (from util-linux) prior to version v2.26 reads the Volume Identifier
|
||||
// (--vid). Therefore for compatibility reasons store the label in both
|
||||
// locations.
|
||||
labelArgs << QStringLiteral("--lvid=") + label;
|
||||
labelArgs << QStringLiteral("--vid=") + shortLabel;
|
||||
}
|
||||
|
||||
// mkudffs from udftools prior to 1.1 is not able to detect logical (sector) size
|
||||
// and UDF block size must match logical sector size of underlying media
|
||||
QStringList cmdArgs;
|
||||
cmdArgs << QStringLiteral("--utf8");
|
||||
// TODO: Add GUI option for choosing different optical disks and UDF revision
|
||||
// For now format as UDF revision 2.01 for hard disk media type
|
||||
cmdArgs << QStringLiteral("--media-type=hd");
|
||||
cmdArgs << QStringLiteral("--udfrev=0x201");
|
||||
// mkudffs from udftools prior to 1.1 is not able to detect logical (sector) size
|
||||
// and UDF block size must match logical sector size of underlying media
|
||||
cmdArgs << QStringLiteral("--blocksize=") + QString::number(sectorSize());
|
||||
cmdArgs << labelArgs;
|
||||
cmdArgs << deviceNode;
|
||||
|
|
|
@ -70,12 +70,13 @@ public:
|
|||
qint64 minCapacity() const override;
|
||||
qint64 maxCapacity() const override;
|
||||
qint64 maxLabelLength() const override;
|
||||
QValidator* labelValidator(QObject *parent = nullptr) const override;
|
||||
SupportTool supportToolName() const override;
|
||||
bool supportToolFound() const override;
|
||||
|
||||
public:
|
||||
static CommandSupportType m_Create;
|
||||
static bool m_OnlyAsciiLabel;
|
||||
static QRegularExpressionValidator m_LabelValidator;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue