Linux C++ 获取U盘挂载路径

#include "DiskU.h"
#if (!defined _WIN32) && (!defined __ENVIRONMENT_MAC__)
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#endif

bool isFolderExists(const string& strFolder)
{
#if (!defined _WIN32) && (!defined __ENVIRONMENT_MAC__)
	DIR *pDir = opendir(strFolder.c_str());
	if(pDir){
		closedir(pDir);
		return true;
	}
#endif
	return false;
}

bool isParentNode(const string& strNode)
{
#if (!defined _WIN32) && (!defined __ENVIRONMENT_MAC__)
	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& strData, const string& strType, string& strDiskMountPoint)
{
#if (!defined _WIN32) && (!defined __ENVIRONMENT_MAC__)
    size_t nPos = strData.find(strType);
    if(nPos != string::npos){
        strDiskMountPoint = strData.substr(nPos + strlen(strType.c_str()) + 1, strData.size());
    }
#endif
}

void MountDiskU(const string& strType)
{
#if (!defined _WIN32) && (!defined __ENVIRONMENT_MAC__)
	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(cData, strType, 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)
{
#if (!defined _WIN32) && (!defined __ENVIRONMENT_MAC__)
	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(cData, strType, strDiskMountPoint);

				if(strDiskMountPoint.size() > 0){
					strID = strDiskID;
					strPath = strDiskMountPoint;
					pclose(fstream);
					return true;
				}
			}
		}
		pclose(fstream);
	}
#endif
	return false;
}


bool FindDiskU(string& strPath, string& strID)
{
#if (!defined _WIN32) && (!defined __ENVIRONMENT_MAC__)
	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)
{
#if (!defined _WIN32) && (!defined __ENVIRONMENT_MAC__)
	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
}

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值