根据/proc/partitions获取插入的U盘设备名称

根据/proc/partitions获取插入的U盘设备名称1 在/proc/partitions中存放着U盘的设备名称,如sda,sdb2等,以sd开头。major主设备号,比如一个U盘有3个分区,主设备名为sda,major为8,分区的major也为8,minor则为分区号,sda1,sda2,minor值为1,2sda的minor为0,name即为设备名,连接/dev./设备名,就可以挂载到一个目录。blocks表示物理设备逻辑块

大家好,又见面了,我是你们的朋友全栈君。

1   在/proc/partitions中存放着U盘的设备名称,如sda,sdb2等,以sd开头。

major 主设备号,比如一个U盘有3个分区,主设备名为sda,major为8,分区的major也为8 (可能所有的U盘插进来,major都是8),minor则为分区号,sda1,sda2,minor值为1 ,2

sda的minor为0,name即为设备名,连接/dev./设备名,就可以挂载到一个目录。

blocks表示物理设备逻辑块的大小

根据/proc/partitions获取插入的U盘设备名称

2,代码

sign_primacy函数标记首要设备

read_proc_partition读取/proc/partitions

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct  parititions_info_s
{
	unsigned long nMajor;
	unsigned long nMinor;
	unsigned long nBlocks;
	char chDeviceName[50];
	int  nPrimacyDeviceFlag;
}parititions_info_t;

int sign_primacy(parititions_info_t *pstuPartitionsInfo,int nDeviceNum)

{
	int i = 0;
	int j = 0;
	char chDeviceNameLastSign[100] = {0};
	if((NULL == pstuPartitionsInfo) ||
		(nDeviceNum <= 0))
	{
		printf("\nparam error\n");
		return -1;
	}

	for(i = 0; i < nDeviceNum - 1;i++)
	{
		if((0 != strlen(chDeviceNameLastSign)) &&
			(NULL != strstr(pstuPartitionsInfo[i].chDeviceName,chDeviceNameLastSign)))
		{
			continue;
		}
		
		for(j = i + 1 ; j < nDeviceNum; j++)
		{
			if(pstuPartitionsInfo[i].nMajor != pstuPartitionsInfo[j].nMajor) //major相同才比较
			{
				break;
			}
			if(NULL != strstr(pstuPartitionsInfo[j].chDeviceName, pstuPartitionsInfo[i].chDeviceName))
			{
				 pstuPartitionsInfo[i].nPrimacyDeviceFlag = 1;

				 memset(chDeviceNameLastSign,0,sizeof(chDeviceNameLastSign));
				 strncpy(chDeviceNameLastSign,pstuPartitionsInfo[i].m_chDeviceName,sizeof(chDeviceNameLastSign) - 1);

				 printf("\n zzh_test primacy  chDeviceName=%s,nMajor[%lu] minor=%lu Flag = %d\n",
				 	pstuPartitionsInfo[i].chDeviceName,
				 	pstuPartitionsInfo[i].nMajor,
				 	pstuPartitionsInfo[i].nMinor,
				 	pstuPartitionsInfo[i].nPrimacyDeviceFlag);

				 i = j;
				 
				 break;
			}
		}
	}

	return 0;
}

int read_proc_partition(parititions_info_t * pstuPartitionsInfo,int nMaxNum,int *pnNum)
{
	FILE *fp = NULL;
	int ret = -1;
	int i = 0;
	int nSscanfNum = 0;
	unsigned long nMajor = 0;
	unsigned long nMinor = 0;
	unsigned long nBlocks = 0;
	char chDeviceName[50] = {0};
	char chBuffer[1024] = {0};
	int nDeviceNum = 0; 
	int nFindDeviceFlag = 0;
	
	if(NULL == pstuPartitionsInfo ||
		NULL == pnNum)
	{
		printf("\n param error \n");
		return -1;
	}

	fp = fopen("/proc/partitions","r");

	if (NULL == fp)
	{
		printf("\n fopen /proc/partitions failed \n");
		return -1;
	}

	
	while(1)
	{
		nFindDeviceFlag = 0;
		memset(chBuffer,0,sizeof(chBuffer));
		if(NULL == fgets(chBuffer,sizeof(chBuffer),fp))
		{
			break;
		}
		memset(chDeviceName,0,sizeof(chDeviceName));
		nSscanfNum = sscanf(chBuffer ," %lu %lu %lu %[^\n]",&nMajor,&nMinor,&nBlocks,chDeviceName);

		if(4 != nSscanfNum)
		{
			continue;
		}

		if(0 != strncmp(chDeviceName,"sd",strlen("sd")))
		{
			continue;
		}

		memset(&pstuPartitionsInfo[nDeviceNum], 0, sizeof(parititions_info_t));
		strncpy(pstuPartitionsInfo[nDeviceNum].chDeviceName,chDeviceName,sizeof(pstuPartitionsInfo[nDeviceNum].chDeviceName) -1);
		pstuPartitionsInfo[nDeviceNum].nMajor = nMajor;
		pstuPartitionsInfo[nDeviceNum].nMinor = nMinor; 
		pstuPartitionsInfo[nDeviceNum].nBlocks = nBlocks;

		printf("\n  nMajor[%lu],   nMinor[%lu],    nBlocks[%lu],    chDeviceName[%s] \n",nMajor,nMinor,nBlocks,chDeviceName);
		//find device 
		nDeviceNum++;

		if(nDeviceNum >= nMaxNum)//最多处理nMaxNum个
		{
			fclose(fp);
			*pnNum = nDeviceNum;
			 sign_primacy( pstuPartitionsInfo,nDeviceNum);
			return 0;
		}
	}
	fclose(fp);

	*pnNum = nDeviceNum;
	if(nDeviceNum > 0)
	{	
		sign_primacy( pstuPartitionsInfo,nDeviceNum);
	}
	
	
	return 0;
}

int main()
{
	parititions_info_t stuPartitionsInfo[30] = {0};
	int nMaxNum = sizeof(stuPartitionsInfo)/sizeof(stuPartitionsInfo[0]);
	int nNum = 0;

	read_proc_partition( stuPartitionsInfo, nMaxNum, &nNum);

	printf("\n Num[%d] \n",nNum);
}

运行结果:

根据/proc/partitions获取插入的U盘设备名称

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/153301.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号