macOS/iOS 文件属性的获取

1,218 阅读3分钟

前言

在ios获取mac中我们获取文件属性一般是通过使用NSFileManger对象类获取,但是它在访问macOS Big Sur上面,访问桌面或者下载等文件的时候,需要用户授权。所以我尝试使用其底层函数来实现文件属性的获取。

stat函数 获取文件属性

#inclue <sys/stat.h> 是unix/linux系统定义文件状态所在的伪标准头文件。

含有类型与函数:

__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函数最多的可能是ls-l命令,用其可以获得有关一个文件的所有信息。

一般头文件在/usr/include下面,这里是标准C程序头文件,如果你的头文件前加了 <sys/*>,那说明这是系统调用函数头文件,其在/usr/include/sys下面。

函数都是获取文件(普通文件,目录,管道,socket,字符,块()的属性。函数原型#include <sys/stat.h>

int stat(const char *restrict pathname, struct stat *restrict buf);提供文件名字,获取文件对应属性。
int fstat(int filedes, struct stat *buf);通过文件描述符获取文件对应的属性。
int lstat(const char *restrict pathname, struct stat *restrict buf);连接文件描述命,获取文件属性。
文件对应的属性

struct stat {
    dev_t           st_dev;         /* [XSI] 设备号码 */
    ino_t           st_ino;         /* [XSI] inode节点号 */
    mode_t          st_mode;        /* [XSI] 文件对应的种类:文件,目录等 */
    nlink_t         st_nlink;       /* [XSI] 文件的连接数 */
    uid_t           st_uid;         /* [XSI] 文件所有者 */
    gid_t           st_gid;         /* [XSI] 文件所有者对应的组 */
    dev_t           st_rdev;        /* [XSI] 设备ID */
#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
    struct  timespec st_atimespec;  /* 文件最后被访问的时间 */
    struct  timespec st_mtimespec;  /* 文件内容最后被修改的时间 */
    struct  timespec st_ctimespec;  /* 文件状态改变时间 */
#else
    time_t          st_atime;       /* [XSI] 文件最后被访问的时间 */
    long            st_atimensec;   /* 最后一次访问的 nsec */
    time_t          st_mtime;       /* [XSI] 文件内容最后被修改的时间 */
    long            st_mtimensec;   /* 文件内容最后被修改的时间 nsec */
    time_t          st_ctime;       /* [XSI] 文件状态改变时间 */
    long            st_ctimensec;   /* 文件状态改变时间 nsec */
#endif
    off_t           st_size;        /* [XSI] 普通文件,对应的文件字节数 */
    blkcnt_t        st_blocks;      /* [XSI] 为文件分配的块 */
    blksize_t       st_blksize;     /* [XSI] 文件内容对应的块大小 */
    __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/文件读取测试/test221.bundle 

_kMDItemDisplayNameWithExtensions      = "test221.bundle"            //文件扩展名
kMDItemContentCreationDate             = 2021-08-30 07:15:22 +0000   //文件内容创建时间
kMDItemContentCreationDate_Ranking     = 2021-08-30 00:00:00 +0000
kMDItemContentModificationDate         = 2021-09-10 07:52:30 +0000   //文件内容修改时间
kMDItemContentModificationDate_Ranking = 2021-09-10 00:00:00 +0000
kMDItemContentType                     = "com.apple.generic-bundle"  //文件内容类型
kMDItemContentTypeTree                 = (
    "com.apple.generic-bundle",
    "com.apple.bundle",
    "public.directory",
    "public.item",
    "com.apple.package"
)
kMDItemDateAdded                       = 2021-09-10 11:26:13 +0000 //文件添加时间
kMDItemDateAdded_Ranking               = 2021-09-10 00:00:00 +0000
kMDItemDisplayName                     = "test221.bundle"
kMDItemDocumentIdentifier              = 0
kMDItemFSContentChangeDate             = 2021-09-10 07:52:30 +0000 //内容修改时间
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                     //文件大小 字节
kMDItemPhysicalSize                    = 303104