Determine whether a directory exists in Linux, and create the directory if it does not exist

When manipulating file directories, we often consider the following functions:

1. Determine whether the file exists, and whether the file is writable/the directory exists

Under Linux:

#include<unistd.h>

int access(const char* pathname, int mode);

Parameter introduction:

Return value: success 0, failure -1

pathname is the path name of the file + file name

mode: Specify the role of access, the values ​​are as follows:

F_OK 值为0,判断文件是否存在
 
X_OK 值为1,判断对文件是可执行权限
 
W_OK 值为2,判断对文件是否有写权限
 
R_OK 值为4,判断对文件是否有读权限
 
注:后三种可以使用或“|”的方式,一起使用,如W_OK|R_OK

C++ (in standard C++):

Header file : <io.h>

Function prototype : int _access(const char *pathname, int mode);

Parameter : pathname is the file path or directory path, mode is the access authority (may be redefined by macro definitions that cannot be used in different systems)

Return value : If the file has the specified access permission, the function returns 0; if the file does not exist or cannot access the specified permission, it returns -1.

Remarks : When pathname is a file, the _access function determines whether the file exists and whether the file can be accessed in the mode specified by the mode value. When pathname is a directory, _access only determines whether the specified directory exists. In Windows NT and Windows 2000, all directories have only read and write permissions.

The value and meaning of mode are as follows:

00-only check if the file exists

02-Write permission

04-Read permission

06-read and write permissions

Note: Use int access(const char *pathname, int mode); to determine whether there is such a file or directory-it can’t tell whether it is a file or a directory

Use int stat(const char *file_name, struct stat *buf); to determine whether the file or directory exists; get st_mode, and then determine whether it is a directory file. 
    The stat() system call is used to see if it is successful. If it is unsuccessful, it does not exist. If it succeeds, it is determined whether the returned st_mode is a folder.

2. Determine whether the directory exists

You can use opendir to judge, this is a relatively simple way.

 #include <sys/types.h>
 #include <dirent.h>

DIR *opendir(const char *name);

3. The mkdir function under Linux, create a directory

Header file library:

#include <sys/stat.h>

#include <sys/types.h>

Function prototype:

int mkdir(const char *pathname, mode_t mode);

Function description:

The mkdir() function creates a directory named after the parameter pathname in mode, and mode defines the permissions of the newly created directory.

return value:

If the directory is created successfully, it returns 0; otherwise, it returns -1 and records the error in the global variable errno.

The values ​​of mode are as follows:

S_IRWXU 00700 permission, which means that the owner of the file has the permission to read, write and perform operations
S_IRUSR(S_IREAD) 00400 permission, which means that the owner of the file has readable permission
S_IWUSR(S_IWRITE) 00200 permission, which means that the owner of the file has writable permission
S_IXUSR (S_IEXEC) 00100 permission, which means that the owner of the file has execution permission
S_IRWXG 00070 permission, which means that the file user group has the permission to read, write and execute operations
S_IRGRP 00040 permission, which means that the file user group has readable permission
S_IWGRP 00020 permissions, which means that the file user group has writable permissions
S_IXGRP 00010 permission, which means that the file user group has execute permission
S_IRWXO 00007 authority, which means that other users have the authority to read, write and perform operations
S_IROTH 00004 permission, which means that other users have readable permissions
S_IWOTH 00002 permission, which means that other users have writable permission
S_IXOTH 00001 permission, which means that other users have execution permission
int32_t OpendirAndMkdir(const char * pathname)
{
	int ret = 0;
	DIR * mydir = NULL;
	if ((mydir = opendir(pathname)) == NULL)
	{
		ret = mkdir(pathname, 0755);
		if (ret != 0) return -1;

		printf("%s created sucess!/n", pathname);
	}
	else
	{
		printf("%s exist!/n", pathname);
	}
	return ret;
}

 

 

Guess you like

Origin blog.csdn.net/Swallow_he/article/details/109639047