macOS/iOS 文件屬性的獲取

前言

在ios獲取mac中我們獲取文件屬性一般是通過使用NSFileManger對(duì)象類獲取,但是它在訪問macOS Big Sur上面,訪問桌面或者下載等文件的時(shí)候,需要用戶授權(quán)。所以我嘗試使用其底層函數(shù)來實(shí)現(xiàn)文件屬性的獲取。

stat函數(shù) 獲取文件屬性

inclue <sys/stat.h>

是unix/linux系統(tǒng)定義文件狀態(tài)所在的偽標(biāo)準(zhǔn)頭文件。

含有類型與函數(shù):

__uint16_t      st_dev;         /* inode's device */
ino_t           st_ino;         /* inode's number */
mode_t          st_mode;        /* inode protection mode */
nlink_t         st_nlink;       /* number of hard links */
__uint16_t      st_uid;         /* user ID of the file's owner */
__uint16_t      st_gid;         /* group ID of the file's group */
__uint16_t      st_rdev;        /* device type */
__int32_t       st_size;        /* file size, in bytes */
struct  timespec st_atimespec;  /* time of last access */
struct  timespec st_mtimespec;  /* time of last data modification */
struct  timespec st_ctimespec;  /* time of last file status change */
__int32_t       st_blksize;     /* optimal blocksize for I/O */
__int32_t       st_blocks;      /* blocks allocated for file */
__uint32_t      st_flags;       /* user defined flags for file */
__uint32_t      st_gen;         /* file generation number */

int     chmod(const char *, mode_t) __DARWIN_ALIAS(chmod);
int     fchmod(int, mode_t) __DARWIN_ALIAS(fchmod);
int     fstat(int, struct stat *) __DARWIN_INODE64(fstat);
int     lstat(const char *, struct stat *) __DARWIN_INODE64(lstat);
int     mkdir(const char *, mode_t);
int     mkfifo(const char *, mode_t);
int     stat(const char *, struct stat *) __DARWIN_INODE64(stat);
int     mknod(const char *, mode_t, dev_t);
mode_t  umask(mode_t);

#if __DARWIN_C_LEVEL >= 200809L
int     fchmodat(int, const char *, mode_t, int) __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0);
int     fstatat(int, const char *, struct stat *, int) __DARWIN_INODE64(fstatat) __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0);
int     mkdirat(int, const char *, mode_t) __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0);
int     futimens(int __fd, const struct timespec __times[2]) __API_AVAILABLE(macosx(10.13), ios(11.0), tvos(11.0), watchos(4.0));
int     utimensat(int __fd, const char *__path, const struct timespec __times[2],
    int __flag) __API_AVAILABLE(macosx(10.13), ios(11.0), tvos(11.0), watchos(4.0));
#endif

使用stat函數(shù)最多的可能是ls-l命令,用其可以獲得有關(guān)一個(gè)文件的所有信息。

一般頭文件在/usr/include下面,這里是標(biāo)準(zhǔn)C程序頭文件,如果你的頭文件前加了 <sys/*>,那說明這是系統(tǒng)調(diào)用函數(shù)頭文件,其在/usr/include/sys下面。

函數(shù)都是獲取文件(普通文件,目錄,管道,socket,字符,塊()的屬性。函數(shù)原型#include <sys/stat.h>

int stat(const char *restrict pathname, struct stat *restrict buf);提供文件名字,獲取文件對(duì)應(yīng)屬性。
int fstat(int filedes, struct stat *buf);通過文件描述符獲取文件對(duì)應(yīng)的屬性。
int lstat(const char *restrict pathname, struct stat *restrict buf);連接文件描述命,獲取文件屬性。
文件對(duì)應(yīng)的屬性

struct stat {
    dev_t           st_dev;         /* [XSI] 設(shè)備號(hào)碼 */
    ino_t           st_ino;         /* [XSI] inode節(jié)點(diǎn)號(hào) */
    mode_t          st_mode;        /* [XSI] 文件對(duì)應(yīng)的種類:文件,目錄等 */
    nlink_t         st_nlink;       /* [XSI] 文件的連接數(shù) */
    uid_t           st_uid;         /* [XSI] 文件所有者 */
    gid_t           st_gid;         /* [XSI] 文件所有者對(duì)應(yīng)的組 */
    dev_t           st_rdev;        /* [XSI] 設(shè)備ID */
#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
    struct  timespec st_atimespec;  /* 文件最后被訪問的時(shí)間 */
    struct  timespec st_mtimespec;  /* 文件內(nèi)容最后被修改的時(shí)間 */
    struct  timespec st_ctimespec;  /* 文件狀態(tài)改變時(shí)間 */
#else
    time_t          st_atime;       /* [XSI] 文件最后被訪問的時(shí)間 */
    long            st_atimensec;   /* 最后一次訪問的 nsec */
    time_t          st_mtime;       /* [XSI] 文件內(nèi)容最后被修改的時(shí)間 */
    long            st_mtimensec;   /* 文件內(nèi)容最后被修改的時(shí)間 nsec */
    time_t          st_ctime;       /* [XSI] 文件狀態(tài)改變時(shí)間 */
    long            st_ctimensec;   /* 文件狀態(tài)改變時(shí)間 nsec */
#endif
    off_t           st_size;        /* [XSI] 普通文件,對(duì)應(yīng)的文件字節(jié)數(shù) */
    blkcnt_t        st_blocks;      /* [XSI] 為文件分配的塊 */
    blksize_t       st_blksize;     /* [XSI] 文件內(nèi)容對(duì)應(yīng)的塊大小 */
    __uint32_t      st_flags;       /* user defined flags for file */
    __uint32_t      st_gen;         /* file generation number */
    __int32_t       st_lspare;      /* RESERVED: DO NOT USE! */
    __int64_t       st_qspare[2];   /* RESERVED: DO NOT USE! */
};

示例:

#import <sys/stat.h>
int main() {\
    struct stat buf;
    **stat("/etc/hosts", &buf);
    printf("/etc/hosts file size = %d\n", buf.st_size);
}

通過mdls命令行獲取文件屬性

 mdls /Users/xxxx/Desktop/文件讀取測(cè)試/test221.bundle 

_kMDItemDisplayNameWithExtensions      = "test221.bundle"            //文件擴(kuò)展名
kMDItemContentCreationDate             = 2021-08-30 07:15:22 +0000   //文件內(nèi)容創(chuàng)建時(shí)間
kMDItemContentCreationDate_Ranking     = 2021-08-30 00:00:00 +0000
kMDItemContentModificationDate         = 2021-09-10 07:52:30 +0000   //文件內(nèi)容修改時(shí)間
kMDItemContentModificationDate_Ranking = 2021-09-10 00:00:00 +0000
kMDItemContentType                     = "com.apple.generic-bundle"  //文件內(nèi)容類型
kMDItemContentTypeTree                 = (
    "com.apple.generic-bundle",
    "com.apple.bundle",
    "public.directory",
    "public.item",
    "com.apple.package"
)
kMDItemDateAdded                       = 2021-09-10 11:26:13 +0000 //文件添加時(shí)間
kMDItemDateAdded_Ranking               = 2021-09-10 00:00:00 +0000
kMDItemDisplayName                     = "test221.bundle"
kMDItemDocumentIdentifier              = 0
kMDItemFSContentChangeDate             = 2021-09-10 07:52:30 +0000 //內(nèi)容修改時(shí)間
kMDItemFSCreationDate                  = 2021-08-30 07:15:22 +0000
kMDItemFSCreatorCode                   = ""
kMDItemFSFinderFlags                   = 0
kMDItemFSHasCustomIcon                 = (null)
kMDItemFSInvisible                     = 0=
kMDItemFSIsExtensionHidden             = 0
kMDItemFSIsStationery                  = (null)
kMDItemFSLabel                         = 0
kMDItemFSName                          = "test221.bundle"
kMDItemFSNodeCount                     = 8
kMDItemFSOwnerGroupID                  = 20
kMDItemFSOwnerUserID                   = 501
kMDItemFSSize                          = 278040
kMDItemFSTypeCode                      = ""
kMDItemInterestingDate_Ranking         = 2021-09-10 00:00:00 +0000  //文件種類
kMDItemKind                            = "捆綁包"
kMDItemLogicalSize                     = 278040                     //文件大小 字節(jié)
kMDItemPhysicalSize                    = 303104

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容