Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Filename shortening if file is created by imager #945

Open
1 task done
paulober opened this issue Oct 9, 2024 · 4 comments
Open
1 task done

[BUG]: Filename shortening if file is created by imager #945

paulober opened this issue Oct 9, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@paulober
Copy link

paulober commented Oct 9, 2024

What happened?

Cloud-init network-config can't be created by imager only edited if already present in the imager because it would shorten the filename which results in an invalid filename for the cloud-init datasource.

Version

1.9.2 (Default)

What host operating system were you using?

Windows

Host OS Version

24H2

Selected OS

Raspberry Pi OS Bookworm Lite (64-bit)

Which Raspberry Pi Device are you using?

Raspberry Pi 5

What kind of storage device are you using?

microSD Card in a USB reader

OS Customisation

  • Yes, I was using OS Customisation when the bug occurred.

Relevant log output

if (createIfNotExist)
{
QByteArray shortFilename;
uint8_t shortFileNameChecksum = 0;
struct longfn_entry longEntry;
if (longFilename.count(".") == 1)
{
QList<QByteArray> fnParts = longFilename.toLatin1().toUpper().split('.');
shortFilename = fnParts[0].leftJustified(8, ' ', true)+fnParts[1].leftJustified(3, ' ', true);
}
else
{
shortFilename = longFilename.toLatin1().leftJustified(11, ' ', true);
}
/* Verify short file name has not been taken yet, and if not try inserting numbers into the name */
if (dirNameExists(shortFilename))
{
for (int i=0; i<100; i++)
{
shortFilename = shortFilename.left( (i < 10 ? 7 : 6) )+QByteArray::number(i)+shortFilename.right(3);
if (!dirNameExists(shortFilename))
{
break;
}
else if (i == 99)
{
throw std::runtime_error("Error finding available short filename");
}
}
}
for(int i = 0; i < shortFilename.length(); i++)
{
shortFileNameChecksum = ((shortFileNameChecksum & 1) ? 0x80 : 0) + (shortFileNameChecksum >> 1) + shortFilename[i];
}
QString longFilenameWithNull = longFilename + QChar::Null;
char *longFilenameStr = (char *) longFilenameWithNull.data();
int lfnFragments = (longFilenameWithNull.length()+12)/13;
int lenBytes = longFilenameWithNull.length()*2;
/* long file name directory entries are added in reverse order before the 8.3 entry */
for (int i = lfnFragments; i > 0; i--)
{
memset(&longEntry, 0xff, sizeof(longEntry));
longEntry.LDIR_Attr = ATTR_LONG_NAME;
longEntry.LDIR_Chksum = shortFileNameChecksum;
longEntry.LDIR_Ord = (i == lfnFragments) ? lfnFragments | LAST_LONG_ENTRY : lfnFragments;
longEntry.LDIR_FstClusLO = 0;
longEntry.LDIR_Type = 0;
size_t start = (i-1) * 26;
memcpy(longEntry.LDIR_Name1, longFilenameStr+start, qMin(lenBytes-start, sizeof(longEntry.LDIR_Name1)));
start += sizeof(longEntry.LDIR_Name1);
if (start < lenBytes)
{
memcpy(longEntry.LDIR_Name2, longFilenameStr+start, qMin(lenBytes-start, sizeof(longEntry.LDIR_Name2)));
start += sizeof(longEntry.LDIR_Name2);
if (start < lenBytes)
{
memcpy(longEntry.LDIR_Name3, longFilenameStr+start, qMin(lenBytes-start, sizeof(longEntry.LDIR_Name3)));
}
}
writeDirEntryAtCurrentPos((struct dir_entry *) &longEntry);
}
memset(entry, 0, sizeof(*entry));
memcpy(entry->DIR_Name, shortFilename.data(), sizeof(entry->DIR_Name));
entry->DIR_Attr = ATTR_ARCHIVE;
entry->DIR_CrtDate = QDateToFATdate( QDate::currentDate() );
entry->DIR_CrtTime = QTimeToFATtime( QTime::currentTime() );
writeDirEntryAtCurrentPos(entry);
/* Add an end-of-directory marker after our newly appended file */
struct dir_entry endOfDir = {0};
writeDirEntryAtCurrentPos(&endOfDir);
}

@paulober paulober added the bug Something isn't working label Oct 9, 2024
@paulober
Copy link
Author

paulober commented Oct 9, 2024

@tdewey-rpi Seems like it worked on other images because they already pre-created the modified files and therefore this part does not execute.

@lurch
Copy link
Contributor

lurch commented Oct 10, 2024

What filename is it supposed to have? What does it get shortened to?

@tdewey-rpi
Copy link
Collaborator

What filename is it supposed to have? What does it get shortened to?

In practice, we see Imager producing short filenames essentially all the time. This first manifested with user-data, and affects the default names for each of the cloud-init configuration files.

@maxnet
Copy link
Collaborator

maxnet commented Oct 10, 2024

In practice, we see Imager producing short filenames essentially all the time.

Pretty sure it did generate new files like "firstrun.sh" (lowercase) correctly in the past.
If storing long file name entries would not work at all, I would expect it to show up as FIRSTRUN.SH instead, as the 8.3 short name is stored in uppercase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants