加入星計劃,您可以享受以下權(quán)益:

  • 創(chuàng)作內(nèi)容快速變現(xiàn)
  • 行業(yè)影響力擴(kuò)散
  • 作品版權(quán)保護(hù)
  • 300W+ 專業(yè)用戶
  • 1.5W+ 優(yōu)質(zhì)創(chuàng)作者
  • 5000+ 長期合作伙伴
立即加入
  • 正文
  • 相關(guān)推薦
  • 電子產(chǎn)業(yè)圖譜
申請入駐 產(chǎn)業(yè)圖譜

如何獲取系統(tǒng)下目錄的文件系統(tǒng)類型?

2023/04/16
1080
閱讀需 11 分鐘
加入交流群
掃碼加入
獲取工程師必備禮包
參與熱點(diǎn)資訊討論

最近看到一個問題,如何獲取當(dāng)前系統(tǒng)的文件類型?

這個時候就要介紹下/proc/mounts文件:這個文件以/etc/mtab文件的格式給出當(dāng)前系統(tǒng)所安裝的文件系統(tǒng)信息。同時也能反映出任何手工安裝從而在/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**格式一致

下面寫個程序提取下設(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

相關(guān)推薦

電子產(chǎn)業(yè)圖譜

本公眾號專注原創(chuàng)電子軟硬件相關(guān)教程,希望這些教程能夠幫助到大家!