Skip to content
This repository was archived by the owner on Feb 27, 2023. It is now read-only.

Commit a585c86

Browse files
procountXECDesign
authored andcommitted
Shorten parition labels acc to fstype (#460)
1 parent 995960e commit a585c86

File tree

2 files changed

+84
-18
lines changed

2 files changed

+84
-18
lines changed

recovery/multiimagewritethread.cpp

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -449,21 +449,7 @@ bool MultiImageWriteThread::processImage(OsInfo *image)
449449
return false;
450450
}
451451
}
452-
if (label.size() > 15)
453-
{
454-
label.clear();
455-
}
456-
else if (!isLabelAvailable(label))
457-
{
458-
for (int i=0; i<10; i++)
459-
{
460-
if (isLabelAvailable(label+QByteArray::number(i)))
461-
{
462-
label = label+QByteArray::number(i);
463-
break;
464-
}
465-
}
466-
}
452+
467453
QByteArray partdevice = p->partitionDevice();
468454

469455
if (fstype == "raw")
@@ -660,6 +646,84 @@ bool MultiImageWriteThread::processImage(OsInfo *image)
660646
return true;
661647
}
662648

649+
QString MultiImageWriteThread::shorten(QString example, int maxLabelLen)
650+
{
651+
QString test;
652+
if (example.size()<=maxLabelLen)
653+
{
654+
return(example);
655+
}
656+
example.replace("_","#");
657+
example.replace("-","#");
658+
example.replace(" ","#");
659+
660+
QStringList parts = example.split("#", QString::SkipEmptyParts);
661+
int numParts = qMin(3, parts.count());
662+
int r;
663+
int len;
664+
int l1,l2;
665+
int rem;
666+
switch (numParts)
667+
{
668+
case 3:
669+
len=parts.last().size();
670+
r=qMin((maxLabelLen-4),len);
671+
rem = maxLabelLen -r-2;
672+
l2 = rem/2;
673+
l1 = rem-l2;
674+
test= parts.first().left(l1)+"_"+parts.at(1).right(l2)+"_"+parts.last().left(r);
675+
break;
676+
677+
case 2:
678+
len=parts.last().size();
679+
r=qMin(maxLabelLen-2, len);
680+
test = parts.first().left(maxLabelLen-r-1) + "_" + parts.last().left(r);
681+
break;
682+
683+
default:
684+
test = parts.first();
685+
test=test.left(maxLabelLen);
686+
break;
687+
}
688+
return(test);
689+
}
690+
691+
QByteArray MultiImageWriteThread::makeLabelUnique(QByteArray label, int maxLabelLen)
692+
{
693+
if (label.size() > maxLabelLen)
694+
{ //restrict to maximum size
695+
label = shorten(label, maxLabelLen).toAscii();
696+
}
697+
698+
if (!isLabelAvailable(label))
699+
{
700+
if (label.size() == maxLabelLen)
701+
{ //Make room for extra digit
702+
label = label.left(maxLabelLen-1);
703+
}
704+
for (int i=0; i<10; i++)
705+
{
706+
if (isLabelAvailable(label+QByteArray::number(i)))
707+
{
708+
label = label+QByteArray::number(i);
709+
return(label);
710+
}
711+
}
712+
//Let's add some more now that we can have 56 OSes on a USB installed!
713+
for (char c='A'; c<='Z'; c++)
714+
{
715+
if (isLabelAvailable(label+c))
716+
{
717+
label = label+c;
718+
return(label);
719+
}
720+
}
721+
//No hope if we get to here
722+
label="";
723+
}
724+
return (label);
725+
}
726+
663727
bool MultiImageWriteThread::mkfs(const QByteArray &device, const QByteArray &fstype, const QByteArray &label, const QByteArray &mkfsopt)
664728
{
665729
QString cmd;
@@ -669,23 +733,23 @@ bool MultiImageWriteThread::mkfs(const QByteArray &device, const QByteArray &fst
669733
cmd = "/sbin/mkfs.fat ";
670734
if (!label.isEmpty())
671735
{
672-
cmd += "-n "+label+" ";
736+
cmd += "-n "+makeLabelUnique(label, 11)+" ";
673737
}
674738
}
675739
else if (fstype == "ext4")
676740
{
677741
cmd = "/usr/sbin/mkfs.ext4 ";
678742
if (!label.isEmpty())
679743
{
680-
cmd += "-L "+label+" ";
744+
cmd += "-L "+makeLabelUnique(label, 16)+" ";
681745
}
682746
}
683747
else if (fstype == "ntfs")
684748
{
685749
cmd = "/sbin/mkfs.ntfs --fast ";
686750
if (!label.isEmpty())
687751
{
688-
cmd += "-L "+label+" ";
752+
cmd += "-L "+makeLabelUnique(label, 32)+" ";
689753
}
690754
}
691755

recovery/multiimagewritethread.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class MultiImageWriteThread : public QThread
2020
protected:
2121
virtual void run();
2222
bool processImage(OsInfo *image);
23+
QString shorten(QString example, int maxLabelLen);
24+
QByteArray makeLabelUnique(QByteArray label, int maxLabelLen);
2325
bool mkfs(const QByteArray &device, const QByteArray &fstype = "ext4", const QByteArray &label = "", const QByteArray &mkfsopt = "");
2426
bool dd(const QString &imagePath, const QString &device);
2527
bool partclone_restore(const QString &imagePath, const QString &device);

0 commit comments

Comments
 (0)