In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
The method to determine the existence of a file under linux:
1. Stat series functions
The stat function is used to return structural information related to the file. There are three cases of stat series functions, corresponding to file names, file descriptors, and symbolic link files. The stat structure describes the attributes of a file, including the file type, file size, and so on. The detailed stat structure is as follows:
Struct stat {mode_t st_mode; / / file type & mode (permissions) ino_t st_ino; / / i-node number (serial number) dev_t st_dev; / / device number (filesystem) dev_t st_rdev; / / device number for specials files nlink_t st_nlink; / / number of links uid_t st_uid / / user ID of owner gid_t st_gid; / / group ID of owner off_t st_size; / / size in bytes, for regular files time_t st_atime; / / time of last access time_t st_mtime; / / time of last modification time_t st_ctime; / / time of last file status change long st_blksize; / / best I/O block size long st_blocks / / number of 512-byte blocks allocated}
We can get information such as file type and file size through stat. File types are: normal files, directory files, block special files, character special files, FIFO, sockets and symbolic links. If you want to determine whether a file or directory exists through stat series functions, when you execute the stat function, if the file exists, you need to further determine whether the file is a normal file or a directory file.
An error of stat series function returned-1. The error code is stored in errno. The value of errno is as follows:
1. The file specified by ENOENT parameter file_name does not exist.
2. The directory in the ENOTDIR path exists but is not a real directory
3. There are too many symbolic links in the files to be opened by ELOOP. The upper limit is 16 symbolic links.
4. The EFAULT parameter buf is an invalid pointer and points to memory space that cannot exist.
5. EACCESS was denied access to the file
6. ENOMEM core memory is insufficient.
7. The path name of the ENAMETOOLONG parameter file_name is too long.
2. Access function
The access function tests the access rights according to the actual user ID and the actual group. The function prototype is:
# include int access (const char * pathname, int mode)
Value of mode:
F_OK test whether the file exists
R_OK test read permission
W_OK test write permissions
X_OK test execution permissions
To correctly judge the existence of a file is to use the access function, which is implemented as follows:
3. Oepndir function
The opendir function is used to open the file directory and return a pointer successfully or NULL if an error occurs. The implementation is as follows:
These are the details of the methods to determine the existence of files under linux. Please pay attention to other related articles for more information.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.