The Linux stat function gets the file attributes (file size, creation time, etc., judging ordinary files or directories, etc.) __oracle

Source: Internet
Author: User
Tags error code lstat

Attention

1. Some platforms do not support Stat64,


2. Get the type of file size off_t,


The off_t type in Linux is a 32-bit long int (4 bytes) by default.


The file may overflow when it is too large.


It is recommended that GCC compile-time plus-d_file_offset_bits=64 option,


The off_t will be a 8-byte type.


3. Get the file size, many people think of the C language Ftell function, but the function


Limitations are large, especially for large files. )


(1) Definition of function


Table header file: #include <sys/stat.h>
#include <unistd.h>
Definition function: int stat (const char *file_name, struct stat *buf);

Function Description: Obtain the file information by filename filename, and save it in the structural body stat referred to by buf
Return value: Successful execution returns 0, failure returns-1, error code is stored in errno
Error code:
Enoent parameter file_name The specified file does not exist
The directory in the Enotdir path exists but is not a true directory
Eloop to open the file has too many symbolic connection problems, the upper limit is 16 symbolic connection
The Efault parameter buf to an invalid pointer, pointing to an unreachable memory space
Eaccess is denied when accessing files
Enomem Core memory is low
The path name for the Enametoolong parameter file_name is too long



(2) Stat structural body


struct STAT {
dev_t St_dev; Device number of the file
ino_t St_ino; Node
mode_t St_mode; Types of files and permissions to access
nlink_t St_nlink; The number of hard connections to the file, the newly created file value is 1
uid_t St_uid; User ID
gid_t St_gid; Group ID
dev_t St_rdev; (device type) If this file is a device file, its device number
off_t st_size; Number of File bytes (file size)
unsigned long st_blksize; Block Size (file system I/o buffer size)
unsigned long st_blocks; Number of blocks
time_t St_atime; Last Access time
time_t St_mtime; Last modification time
time_t St_ctime; Last change time (refers to the attribute)
};
St_mode, as previously described, defines the following several situations:
S_ifmt 0170000 file type of bit mask
S_ifsock 0140000 Scoket
S_iflnk 0120000 Symbolic Connection
S_ifreg 1,000,001-like file
S_IFBLK 0060000 block Unit
S_ifdir 0040000 Directory
S_IFCHR 0020000 character device
S_ififo 0010000 Advanced First Out
S_isuid 04000 file (set User-id on execution) bit
S_isgid 02000 file (set Group-id on execution) bit
Sticky bit of s_isvtx 01000 files
S_IRUSR (s_iread) 00400 file owner with readable permission
S_IWUSR (s_iwrite) 00200 file owner with writable permission
S_IXUSR (s_iexec) 00100 file owner with executable permissions
S_IRGRP 00040 user group with Read permission
S_IWGRP 00020 user group with writable permission
S_IXGRP 00010 user group with executable permissions
S_iroth 00004 Other users have Read permission
S_iwoth 00002 Other users have writable permissions
S_ixoth 00001 Other users have executable permissions
The above file types define macro definitions that check these types in POSIX:
S_islnk (St_mode) determines whether a symbolic connection
S_isreg (St_mode) is a general file
S_isdir (St_mode) is a directory
S_ISCHR (St_mode) is a character appliance file
S_ISBLK (S3E) is advanced first out
S_issock (St_mode) is a socket
If a directory has a sticky bit (s_isvtx), it means that the file in this directory can only be deleted or renamed by that file owner, this directory owner, or root.

The most likely use of the stat function is the ls-l command, which allows you to get all the information about a file.



(3) Other related functions



Functions are the properties of getting files (normal files, directories, pipes, sockets, characters, blocks ().
Function prototypes
#include <sys/stat.h>
int stat (const char *restrict pathname, struct stat *restrict);
Provides the file name and gets the corresponding property of the file.


int fstat (int filedes, struct stat *buf);
Gets the corresponding property of the file through the file descriptor.


int Lstat (const char *restrict pathname, struct stat *restrict);
Provides the file name and gets the file properties.


Main differences:


Open file descriptor received by Fstat (int fd,struct stat *)


The pathname received by the stat (char *filename,struct stat *) should be noted for processing symbolic links, but handling the files that the symbolic links point to.

The pathname received by the Lstat (char *filename,struct stat *) should be noted for its ability to handle symbolic links, but to handle the symbolic link itself (its own) file.




#include <stdio.h> #include <errno.h> #include <string.h> #include <dirent.h> #include < errno.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/stat.h> #include <time.h
> #if 1 typedef struct STAT stat_t;  #define STATFUNC (x,y) stat (x,y) #else typedef struct STAT64 stat_t;
   Some platforms do not support #define STATFUNC (x,y) stat64 (x,y) #endif int func (void) {char *filename = "BINXU.RMVB";
   int nret = 0;
   stat_t s;
   time_t ttimetmp = 0;
   struct TM stutimetmp;
   memset (&s, 0, sizeof (stat_t));
   nret = Statfunc (filename, &s); if (nret) {printf ("Stat%s failed!
       Error_code:%s ", filename, strerror (errno));
   return-1; printf (\ s.st_size=%d,s.st_ctime=%d,s.st_mtime=%d,s.st_atime=%d \ n), S.ST_SIZE,S.ST_CTIME,S.ST_MTIME,S.S

   T_atime); if (s.st_size >0) {printf ("\ n size ========%0.3lf KB%0.3lf M%0.3lf G ======\n", s.st_size/1024.0, S.st_size/1024.0/1024.0,s.st_size/1024.0/1024.0/1024.0);
   } memset (&stutimetmp,0,sizeof (struct TM));
   Ttimetmp = (s.st_ctime);
   struct TM *gmtime_r (const time_t *TIMEP, struct TM *result);
 
	(void *) Gmtime_r (&ttimetmp,&stutimetmp); printf ("\ n Create time**%d-%d-%d%d:%d:%d **\n", Stutimetmp.tm_year + 1900, Stutimetmp.tm_mon + 1, stutimetmp.tm_m
    
   Day, Stutimetmp.tm_hour, Stutimetmp.tm_min, stutimetmp.tm_sec);
return 0;



 int main () {func ();}






The time value of the file property that stat gets is UTC time.


Look at file properties under Windows, Time +8 The Beijing time zone is just right.





(4) using stat attribute to determine whether the folder belongs to the same mounted directory or whether it is the same device


#include <stdio.h> #include <errno.h> #include <string.h> #include <dirent.h> #include < errno.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/stat.h> #include <time.h
> #if 1 typedef struct STAT stat_t;  #define STATFUNC (x,y) stat (x,y) #else typedef struct STAT64 stat_t;
   Some platforms do not support #define STATFUNC (x,y) stat64 (x,y) #endif int func (char *pchfilename) {int nret = 0;
   stat_t Stustat;
   
   dev_t Mountdevice;
   if (NULL = = Pchfilename | | 0 = strlen (pchfilename)) {return-1;
   } memset (&stustat, 0, sizeof (stat_t));
   Nret = Statfunc (Pchfilename, &stustat); if (nret) {printf ("Stat%s failed!
       Error_code:%s ", Pchfilename, Strerror (errno));
   return-1;

	printf ("\n\n**************filepath[%s]***************", pchfilename);
   
   printf ("\ n (stustat.st_mode & s_ifmt) = = S_ifblk is%d", (Stustat.st_mode & s_ifmt) = = s_ifblk); if (Stustat.st_mode &S_IFMT) = = s_ifblk) {mountdevice = Stustat.st_rdev;////(device type) If this file is a device file, its device number} else {mountdevice = Stusta     T.st_dev;
	
   File's device number} printf ("\ St_rdev[%llu] st_dev[%llu] st_ino[%lu]\n", Stustat.st_rdev,stustat.st_dev,stustat.st_ino);
return 0;
	int main () {func ("/share/111/");
	Func ("/share/111/222/");
	
	Func ("/");
	Func ("/dev/sda1");
	Func ("/dev/sda2");

Func ("/dev/sda5");

 }



Belonging to the device, such as/DEV/SDA1, (Stustat.st_mode & s_ifmt) = = = S_ifblk condition is true, if the comparison is the same device, you should compare the St_rdev property

belong to the folder, (Stustat.st_mode & s_ifmt) = = = S_ifblk condition is false, to determine whether the same mount point, you should compare St_dev properties.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.