struct stat
#include <sys/stat.h>
// some useful member
struct stat {
dev_t st_dev; /* [XSI] ID of device containing file */
ino_t st_ino; /* [XSI] File serial number */
mode_t st_mode; /* [XSI] Mode of file (see below) */
nlink_t st_nlink; /* [XSI] Number of hard links */
uid_t st_uid; /* [XSI] User ID of the file */
gid_t st_gid; /* [XSI] Group ID of the file */
dev_t st_rdev; /* [XSI] Device ID */
time_t st_atime; /* [XSI] Time of last access */
long st_atimensec; /* nsec of last access */
time_t st_mtime; /* [XSI] Last data modification time */
long st_mtimensec; /* last data modification nsec */
time_t st_ctime; /* [XSI] Time of last status change */
long st_ctimensec; /* nsec of last status change */
off_t st_size; /* [XSI] file size, in bytes */
blkcnt_t st_blocks; /* [XSI] blocks allocated for file */
blksize_t st_blksize; /* [XSI] optimal blocksize for I/O */
};
st_mode include some cases:
S_IFSOCK socket
S_IFLINK symbol link
S_IFDIR if dictionary
the difference between lstat/fstat/stat
int fstat(int filedes, struct stat* buf);
int stat(const char* path, struct stat* buf);
int lstat(const char* path, struct stat* buf);
fstat need a file descriptor, so we should use open at first;
lstat means “link + stat”, so it can find if this file is symbolic link file;
when stat encounter a symbolic link file, it just go to the file referred to by the link directly.
use stat()
#include <sys/stat.h>
#include <unistd.h>
int main(){
struct stat buf;
stat("/etc/hosts", &buf);
cout << "/etc/hosts file's size is " << buf.size << endl;
some function about st_mode
#define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK) /* block special */
#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) /* char special */
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) /* directory */
#define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) /* fifo or socket */
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) /* regular file */
#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) /* symbolic link */
#define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK) /* socket */
#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
#define S_ISWHT(m) (((m) & S_IFMT) == S_IFWHT) /* OBSOLETE: whiteout */
use it
int main(){
struct stat buf;
int res = stat("/etc/hosts", &buf);
if(0 != res){
cout < "error" << endl;
}
if(S_ISDIR(buf.st_mode)){
cout << "this file is a dic" << endl;
}
}
some flags in open()
/* open-only flags */
#define O_RDONLY 0x0000 /* open for reading only */
#define O_WRONLY 0x0001 /* open for writing only */
#define O_RDWR 0x0002 /* open for reading and writing */
#define O_ACCMODE 0x0003 /* mask for above modes */
#define O_NONBLOCK 0x00000004 /* no delay */
#define O_APPEND 0x00000008 /* set append mode */
#define O_SHLOCK 0x00000010 /* open with shared file lock */
#define O_EXLOCK 0x00000020 /* open with exclusive file lock */
#define O_ASYNC 0x00000040 /* signal pgrp when data ready */
#define O_FSYNC O_SYNC /* source compatibility: do not use */
#define O_NOFOLLOW 0x00000100 /* don't follow symlinks */
#define O_CREAT 0x00000200 /* create if nonexistant */
#define O_TRUNC 0x00000400 /* truncate to zero length */
#define O_EXCL 0x00000800 /* error if already exists */
#define O_SYMLINK 0x00200000 /* allow open of a symlink */
#define O_NOFOLLOW_ANY 0x20000000 /* no symlinks allowed in path */
use exemple
int main(){
int fileFd = open("/etc/hosts", O_RDONLY); // read only
}