Determine if files and directories exist under Linux

Source: Internet
Author: User

1. Preface

Work involves file systems, and sometimes it is necessary to determine whether files and directories exist. I combined apue fourth chapter file and directory, summed up how to correctly determine the existence of files and directories, easy to query later.

2. Stat Series function

The stat function is used to return the structure information associated with a file. The STAT series functions have three cases, corresponding to file names, file descriptors, and symbolic link files. The stat structure describes the properties of the file, mainly including the type of file, file size, and so on. The detailed stat structure is as follows:

1structStat {2 mode_t St_mode;//File type & Mode (permissions)3 ino_t St_ino;//I-node number (serial number)4 dev_t St_dev;//Device Number (filesystem)5 dev_t St_rdev;//Device number for specials files6 nlink_t St_nlink;//Number of links7 uid_t St_uid;//User ID of Owner8 gid_t St_gid;//Group ID of Owner9 off_t st_size;// size in bytes, for regular Files10 time_t st_atime; // time of last Access11 time_t st_ Mtime // time of last modification 12 time_t st_ctime; // time of last file status change 13 long st_blksize; // best I/O block size14 Span style= "COLOR: #0000ff" >long st_blocks; // number of 512-byte blocks Allocated15};                

We can get information such as file type and file size by Stat. File types are: Plain files, directory files, block special files, character special files, FIFO, sockets, and symbolic links. To determine if a file or directory exists by using the Stat series function, when the stat function is executed, it is necessary to further determine whether the file is a normal file or a directory file if it exists.

Stat Series Function Error returns-1, error code exists in errno, errno value is as follows:

1, ENOENT parameter file_name the specified file does not exist 2, Enotdir path directory exists but not the real directory 3, eloop to open the file has too many symbolic connection problems, the upper limit is 16 symbolic connection 4, efault parameter buf is invalid pointer, Pointing to a memory space that cannot exist 5, eaccess Access file denied 6, Enomem core memory is less than 7, enametoolong parameter file_name path name is too long

3. Access functions

Access functions are tested by the actual user ID and the actual group. The function prototypes are:

#include <unistd.h>int access (int mode); 

Mode value:

F_OK test file is present

R_OK Test Read Permissions

W_OK Test Write permissions

X_OK Test Execution permissions

To correctly determine whether a file exists is with an access function, the implementation is as follows:

4. Oepndir function

The Opendir function is used to open the file directory, successfully returning a pointer, and error returning null. The implementation is as follows:

Determine if files and directories exist under Linux

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.