#include #endif

bool isFolderExists(const string& strFolder)

{

#ifndef _WIN32

DIR *pDir = opendir(strFolder.c_str());

if(pDir){

closedir(pDir);

return true;

}

#endif

return false;

}

bool isParentNode(const string& strNode)

{

#ifndef _WIN32

long nTotal = 0;

char cData[2048] = {0};

char cStram[2048] = {0};

sprintf(cStram, "lsblk -m /dev/%s", strNode.c_str());

FILE *fstream = popen(cStram, "r");

if(fstream){

while(fgets(cData, sizeof(cData), fstream)){

nTotal++;

}

pclose(fstream);

}

if(nTotal > 2){

return true;

}

#endif

return false;

}

void EnumDevicePath(const string& strDeviceName, string& strDiskMountPoint)

{

#ifndef _WIN32

char cData[4096] = {0};

char cStram[2048] = {0};

sprintf(cStram, "lsblk | grep %s | awk '{print $7}'", strDeviceName.c_str());

FILE *fstream = popen(cStram, "r");

if(fstream){

while(fgets(cData, sizeof(cData), fstream)){

long nLen = strlen(cData);

if(nLen > 0){

cData[nLen - 1] = 0;

strDiskMountPoint = cData;

}

}

pclose(fstream);

}

#endif

}

void MountDiskU(const string& strType)

{

#ifndef _WIN32

char cData[4096] = {0};

char cStram[2048] = {0};

sprintf(cStram, "lsblk | grep %s", strType.c_str());

FILE *fstream = popen(cStram, "r");

if(fstream){

while(fgets(cData, sizeof(cData), fstream)){

long nLen = strlen(cData);

if(nLen > 0){

cData[nLen - 1] = 0x00;

string strDiskID = "";

string strDiskMountPoint= "";

string strData = cData;

long nPos1 = strData.find(" ");

if(nPos1 != string::npos){

strDiskID = strData.substr(0, nPos1);

if(strDiskID.find("└─") != string::npos){

strDiskID = strDiskID.substr(strlen("└─"), strDiskID.size());

}

}

if(strDiskID.empty()){

continue;

}

if(strDiskID.find("sda") != string::npos){

continue;

}

if(isParentNode(strDiskID)){

continue;

}

EnumDevicePath(strDiskID, strDiskMountPoint);

if(strDiskMountPoint.size() > 0){}

else{

time_t tmNow = time(NULL);

struct tm *timeCurrent = localtime(&tmNow);

DIR *pDir = opendir("/opt/DiskU/");

if(pDir){

closedir(pDir);

}

else{

system("mkdir -p /opt/DiskU");

}

char cPath[2048] = {0};

sprintf(cPath, "/opt/DiskU/%s_%04d_%02d_%02d_%02d_%02d_%02d", strDiskID.c_str(), 1900 + timeCurrent->tm_year, timeCurrent->tm_mon + 1, timeCurrent->tm_mday, timeCurrent->tm_hour, timeCurrent->tm_min, timeCurrent->tm_sec);

if(!isFolderExists(cPath)){

mkdir(cPath, S_IRWXU | S_IRWXG | S_IRWXO);

}

char cCommand[2048] = {0};

sprintf(cCommand, "mount -o iocharset=utf8 /dev/%s %s", strDiskID.c_str(), cPath);

system(cCommand);

}

}

}

pclose(fstream);

}

#endif

}

bool FindDiskU(const string& strType, string& strPath, string& strID)

{

#ifndef _WIN32

char cData[4096] = {0};

char cStram[2048] = {0};

sprintf(cStram, "lsblk | grep %s", strType.c_str());

FILE *fstream = popen(cStram, "r");

if(fstream){

while(fgets(cData, sizeof(cData), fstream)){

long nLen = strlen(cData);

if(nLen > 0){

cData[nLen - 1] = 0x00;

string strDiskID = "";

string strDiskMountPoint= "";

string strData = cData;

long nPos1 = strData.find(" ");

if(nPos1 != string::npos){

strDiskID = strData.substr(0, nPos1);

if(strDiskID.find("└─") != string::npos){

strDiskID = strDiskID.substr(strlen("└─"), strDiskID.size());

}

}

if(strDiskID.empty()){

continue;

}

if(strDiskID.find("sda") != string::npos){

continue;

}

if(isParentNode(strDiskID)){

continue;

}

EnumDevicePath(strDiskID, strDiskMountPoint);

if(strDiskMountPoint.size() > 0){

strID = strDiskID;

strPath = strDiskMountPoint;

pclose(fstream);

return true;

}

}

}

pclose(fstream);

}

#endif

return false;

}

bool FindDiskU(string& strPath, string& strID)

{

#ifndef _WIN32

MountDiskU("part");

MountDiskU("disk");

if(FindDiskU("part", strPath, strID)){

return true;

}

if(FindDiskU("disk", strPath, strID)){

return true;

}

#endif

return false;

}

void RepairDiskU(const string& strPath, const string& strID)

{

#ifndef _WIN32

char cCommand[2048] = {0};

sprintf(cCommand, "umount %s", strPath.c_str());

system(cCommand);

memset(cCommand, 0, 2048);

sprintf(cCommand, "fsck -y %s", strID.c_str());

system(cCommand);

#endif

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐