Windows将u盘烧录为ext4 linux磁盘类型并进行读写
介绍
项目中要求做一个跨平台的烧录u盘软件, windows/linux 将文件写入到烧录的u盘中作为一个arm系统启动盘,中间遇到很多坑故记录于此
有坑* windows去烧录ext4系统后,所有权限只有root有 并且没有x执行权限
需要的可以直接私聊我,收费
处理过程
我用了Qt作为开发工具进行开发,项目构建使用cmake,windows 使用powershell 而linux较简单,直接用bash启动shell脚本,由于开发过程比较繁琐,本项目进行收费下面是一些使用的部分代码
windows(核心,较麻烦)
//windows
QString exepath = "diskpart.exe";
QProcess process;
process.start(exepath);
bool res = process.waitForStarted();
if(!res){
emit signalMessageShow("Failed to start Disk Management tool!");
return false;
}
process.write("list disk\n");
process.closeWriteChannel();
process.waitForFinished();
QByteArray qba = process.readAllStandardOutput();
QTextCodec* pTextCodec = QTextCodec::codecForName("System");
assert(pTextCodec != nullptr);
QString info = pTextCodec->toUnicode(qba);
QTextCodec *mTextCodec = QTextCodec::codecForName("GBK");
qDebug()<< mTextCodec->toUnicode(info.toUtf8()).toStdString().c_str();
process.close();
QStringList strlist,typelist;
std::map<QString,int> map_disksize;
int npos=0;
while(info.indexOf(QStringLiteral("磁盘 "),npos) != -1){
int nindex = info.indexOf(QStringLiteral("磁盘 "),npos);
npos = nindex+4;
if(QStringLiteral("磁盘 #") != info.mid(nindex,4)){
strlist<<info.mid(nindex,4);
QString strsize = " 联机 931 ";
int ngb_index = info.indexOf("GB",npos);
if(ngb_index != -1 && ngb_index<strsize.size() *2 + nindex){
map_disksize[info.mid(nindex,4)] = info.mid(ngb_index-7,7).toInt();
}
}
}
std::vector<int> disk_number;
QStringList enddisk;
for (int i = 0;i<strlist.size();i++) {
QString str ="select disk "+QString::number(i)+ "\n detail disk\n";
process.start(exepath);
bool res = process.waitForStarted();
if(!res){
emit signalMessageShow("Failed to start Disk Management tool!");
return false;
}
process.write(str.toStdString().c_str());
process.closeWriteChannel();
process.waitForFinished();
qba = process.readAllStandardOutput();
info = pTextCodec->toUnicode(qba);
int nindex = info.indexOf(QStringLiteral("类型 :"),0);
QString strtype = info.mid(nindex+6,4);
nindex = info.indexOf(QStringLiteral("磁盘 ID:"),0);
int rindex = info.left(nindex-2).lastIndexOf("\r\n");
//拼硬盘名称
QString strname = info.mid(rindex+2,nindex-rindex-4);
if(strtype.indexOf("USB")!= -1){
enddisk<<strname;
disk_number.push_back(i);
}
process.close();
}
if(enddisk != m_disk_name || disk_number != m_disk_number){
emit signalMessageShow(QStringLiteral("磁盘顺序已发生改变,请先刷新后重新进行烧录"));
return false;
}
if(!diskToFromat(m_disk_number.at(ui->cb_upan->currentIndex()))){
return false;
}
return true;