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

Commit

Permalink
Shorten parition labels acc to fstype (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
procount authored and XECDesign committed Mar 7, 2018
1 parent 995960e commit a585c86
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 18 deletions.
100 changes: 82 additions & 18 deletions recovery/multiimagewritethread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,21 +449,7 @@ bool MultiImageWriteThread::processImage(OsInfo *image)
return false;
}
}
if (label.size() > 15)
{
label.clear();
}
else if (!isLabelAvailable(label))
{
for (int i=0; i<10; i++)
{
if (isLabelAvailable(label+QByteArray::number(i)))
{
label = label+QByteArray::number(i);
break;
}
}
}

QByteArray partdevice = p->partitionDevice();

if (fstype == "raw")
Expand Down Expand Up @@ -660,6 +646,84 @@ bool MultiImageWriteThread::processImage(OsInfo *image)
return true;
}

QString MultiImageWriteThread::shorten(QString example, int maxLabelLen)
{
QString test;
if (example.size()<=maxLabelLen)
{
return(example);
}
example.replace("_","#");
example.replace("-","#");
example.replace(" ","#");

QStringList parts = example.split("#", QString::SkipEmptyParts);
int numParts = qMin(3, parts.count());
int r;
int len;
int l1,l2;
int rem;
switch (numParts)
{
case 3:
len=parts.last().size();
r=qMin((maxLabelLen-4),len);
rem = maxLabelLen -r-2;
l2 = rem/2;
l1 = rem-l2;
test= parts.first().left(l1)+"_"+parts.at(1).right(l2)+"_"+parts.last().left(r);
break;

case 2:
len=parts.last().size();
r=qMin(maxLabelLen-2, len);
test = parts.first().left(maxLabelLen-r-1) + "_" + parts.last().left(r);
break;

default:
test = parts.first();
test=test.left(maxLabelLen);
break;
}
return(test);
}

QByteArray MultiImageWriteThread::makeLabelUnique(QByteArray label, int maxLabelLen)
{
if (label.size() > maxLabelLen)
{ //restrict to maximum size
label = shorten(label, maxLabelLen).toAscii();
}

if (!isLabelAvailable(label))
{
if (label.size() == maxLabelLen)
{ //Make room for extra digit
label = label.left(maxLabelLen-1);
}
for (int i=0; i<10; i++)
{
if (isLabelAvailable(label+QByteArray::number(i)))
{
label = label+QByteArray::number(i);
return(label);
}
}
//Let's add some more now that we can have 56 OSes on a USB installed!
for (char c='A'; c<='Z'; c++)
{
if (isLabelAvailable(label+c))
{
label = label+c;
return(label);
}
}
//No hope if we get to here
label="";
}
return (label);
}

bool MultiImageWriteThread::mkfs(const QByteArray &device, const QByteArray &fstype, const QByteArray &label, const QByteArray &mkfsopt)
{
QString cmd;
Expand All @@ -669,23 +733,23 @@ bool MultiImageWriteThread::mkfs(const QByteArray &device, const QByteArray &fst
cmd = "/sbin/mkfs.fat ";
if (!label.isEmpty())
{
cmd += "-n "+label+" ";
cmd += "-n "+makeLabelUnique(label, 11)+" ";
}
}
else if (fstype == "ext4")
{
cmd = "/usr/sbin/mkfs.ext4 ";
if (!label.isEmpty())
{
cmd += "-L "+label+" ";
cmd += "-L "+makeLabelUnique(label, 16)+" ";
}
}
else if (fstype == "ntfs")
{
cmd = "/sbin/mkfs.ntfs --fast ";
if (!label.isEmpty())
{
cmd += "-L "+label+" ";
cmd += "-L "+makeLabelUnique(label, 32)+" ";
}
}

Expand Down
2 changes: 2 additions & 0 deletions recovery/multiimagewritethread.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class MultiImageWriteThread : public QThread
protected:
virtual void run();
bool processImage(OsInfo *image);
QString shorten(QString example, int maxLabelLen);
QByteArray makeLabelUnique(QByteArray label, int maxLabelLen);
bool mkfs(const QByteArray &device, const QByteArray &fstype = "ext4", const QByteArray &label = "", const QByteArray &mkfsopt = "");
bool dd(const QString &imagePath, const QString &device);
bool partclone_restore(const QString &imagePath, const QString &device);
Expand Down

0 comments on commit a585c86

Please sign in to comment.