最近看到一個(gè)問題,如何獲取當(dāng)前系統(tǒng)的文件類型?
這個(gè)時(shí)候就要介紹下/proc/mounts
文件:這個(gè)文件以/etc/mtab
文件的格式給出當(dāng)前系統(tǒng)所安裝的文件系統(tǒng)信息。同時(shí)也能反映出任何手工安裝從而在/etc/mtab
文件中沒有包含的文件系統(tǒng)。
我們可以通過cat /proc/mounts
查看掛載文件系統(tǒng)的狀態(tài)。
root@firefly:~#?cat?/proc/mounts
sysfs?/sys?sysfs?rw,nosuid,nodev,noexec,relatime?0?0
proc?/proc?proc?rw,nosuid,nodev,noexec,relatime?0?0
udev?/dev?devtmpfs?rw,nosuid,relatime,size=1960196k,nr_inodes=490049,mode=755?0?0
devpts?/dev/pts?devpts?rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000?0?0
tmpfs?/run?tmpfs?rw,nosuid,noexec,relatime,size=393192k,mode=755?0?0
/dev/mmcblk1p7?/root-ro?ext4?ro,relatime,data=ordered?0?0
/dev/mmcblk1p8?/userdata?ext4?rw,relatime,data=ordered?0?0
overlayroot?/?overlay?rw,relatime,lowerdir=/root-ro,upperdir=/userdata/rootfs_overlay,workdir=/userdata/rootfs_overlay-workdir/_?0?0
securityfs?/sys/kernel/security?securityfs?rw,nosuid,nodev,noexec,relatime?0?0
tmpfs?/dev/shm?tmpfs?rw,nosuid,nodev?0?0
tmpfs?/run/lock?tmpfs?rw,nosuid,nodev,noexec,relatime,size=5120k?0?0
mounts文件包含6列
Device
-
- mount的設(shè)備
Mount Point
-
- 掛載點(diǎn),也就是掛載的路徑
File System Type
-
- 文件系統(tǒng)類型,如ext4、xfs等
Options
- 掛載選項(xiàng),包括讀寫權(quán)限等參數(shù)無用內(nèi)容,保持內(nèi)容和**/etc/fstab**格式一致無用內(nèi)容,保持內(nèi)容和**/etc/fstab**格式一致
下面寫個(gè)程序提取下設(shè)備名,掛載目錄,文件系統(tǒng)類型等參數(shù)。
#include?<stdio.h>
#include?<stdlib.h>
#include?<string.h>
#define?MOUNTS_INFO????????"/proc/mounts"
typedef?struct
{
????char?devName[32];
????char?dirName[16];
????char?fsType[16];
????char?authority[16];
}PartitionInfo;
int?getPartitionInfo(char?*pPartitionName,PartitionInfo?*pPartitionInfo)
{
????FILE?*fp?=?NULL;
????char?str[128]?=?{0};
????char?*p?=?NULL,*q?=?NULL,*pTmpStr?=?NULL;
????int?i?=?0;
????fp?=?fopen(MOUNTS_INFO,?"r");
????if?(fp?<?0)
????{
????????printf("open?%s?failedn",MOUNTS_INFO);
????????return?-1;
????}
????while?(NULL?!=?fgets(str,sizeof(str),fp))
????{
????????p?=?strstr(str,pPartitionName);
????????if?(NULL?!=?p)
????????{
????????????for(i?=?0;i?<?4;?i++)
????????????{
????????????????if?(0?==?i)
????????????????{
????????????????????q?=?strtok_r(str,"?",&pTmpStr);
????????????????????strcpy(pPartitionInfo->devName,q);
????????????????}
????????????????if?(1?==?i)
????????????????{
????????????????????q?=?strtok_r(p,"?",&pTmpStr);
????????????????????strcpy(pPartitionInfo->dirName,q);?
????????????????}??
????????????????if?(2?==?i)
????????????????{
????????????????????q?=?strtok_r(p,"?",&pTmpStr);
????????????????????strcpy(pPartitionInfo->fsType,q);?
????????????????}??
????????????????if?(3?==?i)
????????????????{
????????????????????q?=?strtok_r(p,",",&pTmpStr);
????????????????????strcpy(pPartitionInfo->authority,q);
????????????????}???
????????????????p?=?pTmpStr;?????????????????????????
????????????}????????
????????}
????}
????
????fclose(fp);
?return?0;
}
int?main()
{
????int?status?=?-1;
????PartitionInfo?partitionInfo;
????memset(&partitionInfo,0,sizeof(PartitionInfo));
????status?=?getPartitionInfo("/dev/pts",&partitionInfo);
????if(status?<?0)
????{
??printf("get??partition?info?failedn");
??return?-1;
????}
????
????printf("devName:%sn",partitionInfo.devName);
????printf("dirName:%sn",partitionInfo.dirName);
????printf("fsType:%sn",partitionInfo.fsType);
????printf("authority:%sn",partitionInfo.authority);
????
????return?0;
}
運(yùn)行結(jié)果
root@firefly:~/mnt#?./get_fsType?
devName:devpts
dirName:/dev/pts
fsType:devpts
authority:rw
閱讀全文