//打開目錄,周遊
std::string file, name;
DIR *pDir;
struct dirent* pDirent;
struct stat info;
std::cout<<path<<std::endl;
pDir = opendir(path.c_str());
if(pDir != NULL)
{
while(true)
{
pDirent = readdir(pDir);
if(pDirent == NULL)
break;
file = path + "/" + pDirent->d_name;
std::cout<<file<<std::endl;
if( stat(file.c_str(), &info) != 0)
{
LOG(ERROR)<<"Failed to obtain "<<pDirent->d_name<<"'s info !";
LOG(ERROR)<<"Reason : "<<strerror(errno);
std::cout<<"Read "<<pDirent->d_name<<"Failed"<<std::endl;
continue;
}
else if(S_ISREG(info.st_mode))
{
name = Convert_Path(file, "", '_');//取到IP
std::cout<<name<<std::endl;
if(Contain_Fail(file))
result.insert(make_pair(name,false));
else
result.insert(make_pair(name,true));
}
}
}
stat函數講解(轉)
表頭檔案: #include <sys/stat.h>
#include <unistd.h>
定義函數: int stat(const char *file_name, struct stat *buf);
函數說明: 通過檔案名filename擷取檔案資訊,并儲存在buf所指的結構體stat中
傳回值: 執行成功則傳回0,失敗傳回-1,錯誤代碼存于errno
錯誤代碼:
ENOENT 參數file_name指定的檔案不存在
ENOTDIR 路徑中的目錄存在但卻非真正的目錄
ELOOP 欲打開的檔案有過多符号連接配接問題,上限為16符号連接配接
EFAULT 參數buf為無效指針,指向無法存在的記憶體空間
EACCESS 存取檔案時被拒絕
ENOMEM 核心記憶體不足
ENAMETOOLONG 參數file_name的路徑名稱太長
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
int main() {
struct stat buf;
stat("/etc/hosts", &buf);
printf("/etc/hosts file size = %d/n", buf.st_size);
}
-----------------------------------------------------
struct stat {
dev_t st_dev; //檔案的裝置編号
ino_t st_ino; //節點
mode_t st_mode; //檔案的類型和存取的權限
nlink_t st_nlink; //連到該檔案的硬連接配接數目,剛建立的檔案值為1
uid_t st_uid; //使用者ID
gid_t st_gid; //組ID
dev_t st_rdev; //(裝置類型)若此檔案為裝置檔案,則為其裝置編号
off_t st_size; //檔案位元組數(檔案大小)
unsigned long st_blksize; //塊大小(檔案系統的I/O 緩沖區大小)
unsigned long st_blocks; //塊數
time_t st_atime; //最後一次通路時間
time_t st_mtime; //最後一次修改時間
time_t st_ctime; //最後一次改變時間(指屬性)
};
先前所描述的st_mode 則定義了下列數種情況:
S_IFMT 0170000 檔案類型的位遮罩
S_IFSOCK 0140000 scoket
S_IFLNK 0120000 符号連接配接
S_IFREG 0100000 一般檔案
S_IFBLK 0060000 區塊裝置
S_IFDIR 0040000 目錄
S_IFCHR 0020000 字元裝置
S_IFIFO 0010000 先進先出
S_ISUID 04000 檔案的(set user-id on execution)位
S_ISGID 02000 檔案的(set group-id on execution)位
S_ISVTX 01000 檔案的sticky位
S_IRUSR(S_IREAD) 00400 檔案所有者具可讀取權限
S_IWUSR(S_IWRITE)00200 檔案所有者具可寫入權限
S_IXUSR(S_IEXEC) 00100 檔案所有者具可執行權限
S_IRGRP 00040 使用者組具可讀取權限
S_IWGRP 00020 使用者組具可寫入權限
S_IXGRP 00010 使用者組具可執行權限
S_IROTH 00004 其他使用者具可讀取權限
S_IWOTH 00002 其他使用者具可寫入權限
S_IXOTH 00001 其他使用者具可執行權限
上述的檔案類型在POSIX中定義了檢查這些類型的宏定義:
S_ISLNK (st_mode) 判斷是否為符号連接配接
S_ISREG (st_mode) 是否為一般檔案
S_ISDIR (st_mode) 是否為目錄
S_ISCHR (st_mode) 是否為字元裝置檔案
S_ISBLK (s3e) 是否為先進先出
S_ISSOCK (st_mode) 是否為socket
若一目錄具有sticky位(S_ISVTX),則表示在此目錄下的檔案隻能被該檔案所有者、此目錄所有者或root來删除或改名。
-----------------------------------------------------
struct statfs {
long f_type; //檔案系統類型
long f_bsize; //塊大小
long f_blocks; //塊多少
long f_bfree; //空閑的塊
long f_bavail; //可用塊
long f_files; //總檔案節點
long f_ffree; //空閑檔案節點
fsid_t f_fsid; //檔案系統id
long f_namelen; //檔案名的最大長度
long f_spare[6]; //spare for later
};
stat、fstat和lstat函數(UNIX)
#include<sys/types.h>
#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);
連接配接檔案
三個函數的傳回:若成功則為0,若出錯則為-1
給定一個pathname,stat函數傳回一個與此命名檔案有關的資訊結構,fstat函數獲得已在描述符filedes上打開的檔案的有關資訊。lstat函數類似于stat,但是當命名的檔案是一個符号連接配接時,lstat傳回該符号連接配接的有關資訊,而不是由該符号連接配接引用的檔案的資訊。
第二個參數是個指針,它指向一個我們應提供的結構。這些函數填寫由buf指向的結構。該結構的實際定義可能随實作而有所不同,但其基本形式是:
struct stat{
mode_t st_mode;
ino_t st_ino;
dev_t st_rdev;
nlink_t st_nlink;
uid_t st_uid;
gid_t st_gid;
off_t st_size;
time_t st_atime;
time_t st_mtime;
time_t st_ctime;
long st_blksize;
long st_blocks;
};
注意,除最後兩個以外,其他各成員都為基本系統資料類型。我們将說明此結構的每個成員以了解檔案屬性。
使用stat函數最多的可能是ls-l指令,用其可以獲得有關一個檔案的所有資訊。
1 函數都是擷取檔案(普通檔案,目錄,管道,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); 連接配接檔案描述命,擷取檔案屬性。 2 檔案對應的屬性 struct stat {
mode_t st_mode; //檔案對應的模式,檔案,目錄等
ino_t st_ino; //inode節點号
dev_t st_dev; //裝置号碼
dev_t st_rdev; //特殊裝置号碼
nlink_t st_nlink; //檔案的連接配接數
uid_t st_uid; //檔案所有者
gid_t st_gid; //檔案所有者對應的組
off_t st_size; //普通檔案,對應的檔案位元組數
time_t st_atime; //檔案最後被通路的時間
time_t st_mtime; //檔案内容最後被修改的時間
time_t st_ctime; //檔案狀态改變時間
blksize_t st_blksize; //檔案内容對應的塊大小
blkcnt_t st_blocks; //偉建内容對應的塊數量
};
可以通過上面提供的函數,傳回一個結構體,儲存着檔案的資訊。