linux系统和windows系统检测磁盘大小报警

发布时间 2023-10-12 11:41:47作者: 一字千金

1.应用场景

需要往磁盘中存储图片和或者数据,需要检测磁盘大小,当小于5GB的时候提示用户,并停止存储图片和数据,避免磁盘塞满,无法启动系统或者操作电脑;

2.实现方法

用一个定时器去定时查询磁盘空间大小,linux系统,采用QProess执行命令,然后解析命令返回值,获取剩余的磁盘空间

void FaceMatch::SlotCheckDriverMemory()
{
	///磁盘名
	QString strpicPath = CConfig::instance()->GetResaultPath();
#ifdef OS_WINDOWS
	QString iDriver = strpicPath.left(3);
	LPCWSTR strDriver = (LPCWSTR)iDriver.utf16();
	ULARGE_INTEGER freeDiskSpaceAvailable, totalDiskSpace, totalFreeDiskSpace;
	///调用函数获取磁盘参数(单位为字节Byte)
	GetDiskFreeSpaceEx(strDriver, &freeDiskSpaceAvailable, &totalDiskSpace, &totalFreeDiskSpace);
	quint64 freeDB = totalFreeDiskSpace.QuadPart / (1024 * 1024 * 1024);
	m_VideoAnalysisTaskList->SlotSetStoreSpace(freeDB>=5);
	if (freeDB < 5)//space less than 5GB
	{
		if (m_bigThanFiveGB == true)
		{
			m_bigThanFiveGB = false;
			SlotError(-1, strpicPath + "磁盘空间不足5G,请在SystemConfig.xml配置文件切换磁盘,或者清除数据。");
		}
		else
		{
			m_bigThanFiveGB = false;
		}

	}
	else//space big than 5GB
	{
		if (m_bigThanFiveGB == false)//space
		{
			m_bigThanFiveGB = true;
			m_VideoAnalysisTaskList->StartNextWaitTask("struct");
			m_VideoAnalysisTaskList->StartNextWaitTask("gait");
			SlotError(0, strpicPath + QString("磁盘空间剩余%1,继续分析").arg(freeDB));
		}
	}
    
#else
	//获取保存的路径的
	QProcess process;
	process.start("df -h " + strpicPath);
	if (process.waitForFinished())
	{
		QString bytes = process.readAll();
        QString strLeftSize=bytes.split("\n").at(1);
        QStringList list=strLeftSize.split(" ");
        for(int i=0;i<list.size();)
        {
            if(list[i]==" "||list[i]=="")
            {
                list.removeAt(i);
            }
            else
            {
                i++;
            }
        }
        if(list.size()<4)
        {
             LOG_ERROR("Get store info error:%s ",bytes.toStdString().c_str());
             return;
        }
       strLeftSize=list[3];
       strLeftSize= strLeftSize.remove("G");
       int freeDB =strLeftSize.toInt();
        m_VideoAnalysisTaskList->SlotSetStoreSpace(freeDB >=5);
       if(freeDB<5)//space less than 5GB
       {
           if(m_bigThanFiveGB==true)
           {
               m_bigThanFiveGB=false;
              SlotError (-1, CConfig::instance()->GetResaultPath()+ "磁盘空间不足5G,请在SystemConfig.xml配置文件切换磁盘,或者清除数据。");
           }
           else
           {
                m_bigThanFiveGB=false;
           }

       }
       else//space big than 5GB
       {
           if(m_bigThanFiveGB==false)//space
           {
               m_bigThanFiveGB=true;
               m_VideoAnalysisTaskList->StartNextWaitTask("struct");
               m_VideoAnalysisTaskList->StartNextWaitTask("gait");
                SlotError (0, CConfig::instance()->GetResaultPath() + QString("磁盘空间剩余%1,继续分析").arg(freeDB));
           }
       }
	}
#endif
	

}