1 Star 0 Fork 4.9K

famoustang / docs

forked from OpenHarmony / docs 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
FS.md 245.71 KB
一键复制 编辑 原始数据 按行查看 历史
wenjun 提交于 2020-09-08 10:08 . add OpenHarmony 1.0 baseline

FS

Overview

Provides functions for file system, include file operations, directory operations and so on.

Since:

1.0

Version:

1.0

Summary

Files

File Name

Description

dirent.h

Provides functions and structures used for performing file system operations, including file read/write, directory traversal, and file system mounting.

fcntl.h

Declares structures and functions used for file and directory operations such as opening a file, creating a file, and allocating space.

ftw.h

Provides functions and structures related to file tree traversal.

glob.h

Provides functions and structures used to find path names matching a pattern and release the memory that was applied for during the search.

libgen.h

Provides functions related to file management, for example, obtaining directory and file names.

ioctl.h

Manipulates a device.

mount.h

Provides functions for mounting and unmounting a file system.

stat.h

Provides functions for file and directory operations.

statfs.h

Provides a function to obtain information about a file system.

statvfs.h

Provides a function and structure related to file system information operation, for example, viewing file system information.

utime.h

Provides the function and structure used for setting file access time and modification time.

Data Structures

Data Structure Name

Description

dirent

Defines the content of the directory.

FTW

Defines the file tree walker (FTW).

glob_t

Defines the search information.

stat

Defines the file information structure.

statvfs

Describes file system information.

utimbuf

Defines the file access time and modification time.

Macros

Macro Name and Value

Description

O_ACCMODE    (03|O_SEARCH)

Access mode.

O_RDONLY    00

Read-only.

O_WRONLY    01

Write-only.

O_RDWR    02

Read and write.

fallocate64    fallocate

Pre-allocates space to a file. See fallocate.

FTW_F    1

Regular file.

FTW_D    2

Directory.

FTW_DNR    3

Unreadable directory. Its subdirectories will not be traversed.

FTW_NS    4

The data of the stat structure cannot be obtained. The possible cause is that the permission is incorrect.

FTW_SL    5

Symbolic link.

FTW_DP   6

 

FTW_SLN    7

Symbolic link that names a non-existent file.

FTW_PHYS    1

Symbolic links that are not traversed. If not set, symbolic links are traversed by default.

FTW_MOUNT    2

Stay within the same file system.

FTW_CHDIR    4

chdir is called to go to the directory before reading the directory.

FTW_DEPTH    8

All subdirectories will be traversed before the directory itself.

GLOB_ERR    0x01

Return upon a read error.

GLOB_MARK    0x02

Append a slash (/) to each path, which corresponds to a directory.

GLOB_NOSORT    0x04

Do not sort the returned path names.

GLOB_DOOFFS    0x08

In the pglob parameter of the glob() function, reserve a string that has a length of pglob->gl_offs and ends with \0 at the beginning of the string list in pglob->pathv. That is, the newly matched string is appended to a position where the distance from the beginning of pglob->pathv is the length of pglob->gl_offs.

GLOB_NOCHECK    0x10

If no pattern matches, return the original pattern.

GLOB_APPEND    0x20

Append the results of this call to the results returned by a previous call to glob().

GLOB_NOESCAPE    0x40

Do not use a backslash () as an escape character.

GLOB_PERIOD    0x80

Allow a leading period to be matched by metacharacters.

GLOB_TILDE    0x1000

Carry out tilde expansion. If a tilde (~) is the only character in the pattern, or an initial tilde is followed immediately by a slash (/), then the home directory of the caller is substituted for the tilde.

GLOB_TILDE_CHECK    0x4000

Similar to GLOB_TILDE. The difference is that if the username is invalid, or the home directory cannot be determined, glob() returns GLOB_NOMATCH to indicate an error.

GLOB_NOSPACE    1

Memory insufficient.

GLOB_ABORTED    2

Read error.

GLOB_NOMATCH    3

No matching item is found.

GLOB_NOSYS    4

The function is not supported.

Typedefs

Typedef Name

Description

DIR

typedef struct __dirstream 

Opens the handle of the folder directory.

Functions

Function Name

Description

closedir (DIR *dirp)

int 

Closes a specified directory.

opendir (const char *dirname)

DIR

Opens a specified directory.

readdir (DIR *dirp)

struct dirent

Reads a specified directory.

readdir_r (DIR *__restrict dirp, struct dirent *__restrict entry, struct dirent **__restrict result)

int 

Reads a specified directory (thread-safe version).

rewinddir (DIR *dirp)

void 

Resets a directory stream read position.

alphasort (const struct dirent **a, const struct dirent **b)

int 

Sorts the directory entries in alphabetical order.

scandir (const char *dir, struct dirent ***namelist, int(*sel)(const struct dirent *), int(*compar)(const struct dirent **, const struct dirent **))

int 

Scans directory to find the target entries.

seekdir (DIR *dirp, long offset)

void 

Sets the position of the next readdir call in the directory stream.

telldir (DIR *dirp)

long 

Obtains the current position in the specified directory stream.

creat (const char *path, mode_t mode)

int 

Creates a file or rewrites an existing file.

open (const char *path, int oflags,...)

int 

Opens a file.

fallocate (int fd, int mode, off_t offset, off_t len)

int 

Pre-allocates space to a file.

fcntl (int fd, int cmd,...)

int 

Manipulates a specified file descriptor.

ftw (const char *path, int(*fn)(const char *file, const struct stat *sb, int flag), int depth)

int 

Traverses a file tree.

nftw (const char *path, int(*fn)(const char *file, const struct stat *sb, int flag, struct FTW *s), int fd_limit, int flags)

int 

Traverses a file tree.

glob (const char *__restrict pattern, int flags, int(*errfunc)(const char *, int), glob_t *__restrict pglob)

int 

Searches for path names that meet a specified pattern.

globfree (glob_t *pglob)

void 

Frees allocated memory from a call to glob().

dirname (char *path)

char * 

Obtains a directory name in a specified path.

basename (char *path)

char * 

Obtains a file name (excluding the directory) in a specified path.

ioctl (int fd, int cmd,...)

int 

Manipulates a device.

mount (const char *source, const char *target, const char *filesystemtype, unsigned long mountflags, const void *data)

int 

Mounts a file system.

umount (const char *target)

int 

Unmounts a file system.

umount2 (const char *target, int flags)

int 

Unmounts a file system.

stat (const char *__restrict path, struct stat *__restrict buf)

int 

Obtains file information.

fstat (int fd, struct stat *buf)

int 

Obtains file status information.

lstat (const char *__restrict path, struct stat *__restrict buf)

int 

Obtains file information.

mkdir (const char *pathname, mode_t mode)

int 

Creates a directory.

mkfifo (const char *name, mode_t mode)

int 

Creates a named pipe to implement inter-process communication.

mkdirat (int fd, const char *pathname, mode_t mode)

int 

Creates a directory.

chmod (const char *pathname, mode_t mode)

int 

Modifies permissions for a file.

umask (mode_t mode)

mode_t 

Sets umask for a process.

statfs (const char *path, struct statfs *buf)

int 

Obtains file system information of a file in a specified path.

statvfs (const char *__restrict path, struct statvfs *__restrict buf)

int 

Obtains the file system information.

utime (const char *filename, const struct utimbuf *times)

int 

Sets the access time and modification time of a file.

Details

Macro Definition Documentation

FTW_DP

#define FTW_DP   6

Description:

@briefDirectory. All subdirectories in this directory have been traversed.

Function Documentation

alphasort()

int alphasort (const struct [dirent](dirent.md) ** a, const struct [dirent](dirent.md) ** b )

Description:

Sorts the directory entries in alphabetical order.

This function is used to sort the two directory entries passed through the input parameters in alphabetical order. Generally, these parameters are passed in scandir as its last parameter.

Parameters:

Name

Description

a Indicates the pointer to directory entry a.
a Indicates the pointer to directory entry b.

Returns:

Returns an integer greater than, equal to, or less than 0, respectively, based on whether the name of the directory entry to which a points is lexically greater than, equal to, or less than the directory.

basename()

char* basename (char * path)

Description:

Obtains a file name (excluding the directory) in a specified path.

Parameters:

Name

Description

path Indicates the pointer to the path.

Returns:

Returns the file name if the operation is successful; returns NULL otherwise.

chmod()

int chmod (const char * pathname, mode_t mode )

Description:

Modifies permissions for a file.

Parameters:

Name

Description

pathname Indicates the pointer to the path of the file.
mode Indicates the new file permission.

Attention:

This function can be used only in the JFFS2 file system.

Returns:

Returns 0 if the operation is successful; returns -1 and sets errno to a value in the following table otherwise.

errno

Description

EINVAL

The pathname is a null pointer or an empty string.

EACCES

The permission bit of the file mode does not allow the requested access, or the search permission is denied on the path prefix directory.

EFAULT

Incorrect IP address.

ENAMETOOLONG

The path name length is greater than the value of NAME_MAX.

ENOENT

The directory component in the path name does not exist.

ENOMEM

Insufficient memory.

ENOTDIR

A component of the path prefix is the name of an existing file. The file is neither a directory nor a symbolic link of a directory.

EPERM

Operation not permitted or access denied because the access is prohibited or the directory is full.

EROFS

The physical drive is write-protected.

EEXIST

The file or directory already exists.

ENOSYS

The function is not supported.

closedir()

int closedir ([DIR](FS.md#ga0ebe68390948c14bb9d82987adbfc849) * dirp)

Description:

Closes a specified directory.

Parameters:

Name

Description

dirp Indicates the pointer to the structure representing the directory to close.

Returns:

Returns 0 if the directory is successfully closed; returns -1 and sets errno to the following value otherwise.

errno

Description

EBADF

dirp is not a valid opened directory stream.

creat()

int creat (const char * path, mode_t mode )

Description:

Creates a file or rewrites an existing file.

Parameters:

Name

Description

path Indicates the pointer to the name of the file to create or rewrite.
mode Indicates permissions on the file.

Returns:

Returns 0 if the file is created or rewritten successfully; returns -1 and sets errno to a value in the following table otherwise.

errno

Description

EINVAL

path points to an invalid path.

ENAMETOOLONG

The length of path is greater than the value of NAME_MAX.

ENXIO

Invalid inode or the inode is not a mount point.

EMFILE

All file descriptors that can be used by the process are opened.

EPERM

The file structure corresponding to the file descriptor cannot be obtained.

EACCES

Access denied by the permission bit of file mode

ENOMEM

The memory is insufficient.

ENODEV

The device does not exist.

EEXIST

O_CREAT and O_EXCL are set.

EIO

A hardware error occurs at the low-level disk I/O layer. As a result, the physical drive cannot work.

ENFILE

The number of opened files in the system has reached the maximum.

ENOTDIR

A component of the path prefix is the name of an existing file and is not a directory.

EBUSY

The operation is rejected according to the file sharing policy.

ENOTEMPTY

The directory is not empty.

ELOOP

Too many symbolic links are encountered.

EFAULT

The IP address is incorrect.

dirname()

char* dirname (char * path)

Description:

Obtains a directory name in a specified path.

Parameters:

Name

Description

path Indicates the pointer to the path.

Returns:

Returns the directory name if the operation is successful; returns NULL otherwise.

fallocate()

int fallocate (int fd, int mode, off_t offset, off_t len )

Description:

Pre-allocates space to a file.

Parameters:

Name

Description

fd Indicates the file descriptor.
mode Indicates the operation mode. Only FALLOC_FL_KEEP_SIZE is supported.
offset Indicates the offset of the file to allocate.
len Indicates the size of the disk space to be allocated to the file.

Attention:

This function can be used only in the FAT file system.

Returns:

Returns 0 if the operation is successful; returns -1 and sets errno to a value in the following table otherwise.

errno

Description

EINVAL

The value of len or offset is less than 0 or greater than that of INT_MAX, or the value of mode is not FALLOC_FL_KEEP_SIZE.

EBADF

fd is not a valid file descriptor, or the basic file system does not support this operation.

EAGAIN

The file list is null.

EACCES

Access denied by the authority bit of file mode

EIO

An I/O error occurs when data is read from or written to the file system.

EROFS

The physical drive is write-protected.

EPERM

The operation is not allowed or the access is rejected.

EBUSY

The operation is rejected according to the file sharing policy.

fcntl()

int fcntl (int fd, int cmd,  ... )

Description:

Manipulates a specified file descriptor.

Parameters:

Name

Description

fd Indicates the file descriptor.
cmd Indicates the command used to manipulate the file descriptor.

cmd

Description

F_DUPFD

Copy a new file descriptor (fd is a file).

F_GETFL

Obtain the status of the file specified by fd. (fd is a file or sockets are available.)

F_SETFL

Set the file descriptor status. (fd is a file or sockets are available.)

Returns:

Returns the value depending on cmd if the operation is successful; returns -1 and sets errno to a value in the following table otherwise.

errno

Description

EAGAIN

The operation is prohibited.

EBADF

fd is an invalid file descriptor.

EFAULT

Beyond the accessible address space.

EINVAL

The command specified by cmd cannot be recognized.

ENOLCK

Too many segment locks open, lock table is full, or a remote locking protocol failed.

EPERM

Attempt to clear the O_APPEND flag on a file that has the append-only attribute set.

fstat()

int fstat (int fd, struct [stat](stat.md) * buf )

Description:

Obtains file status information.

Parameters:

Name

Description

fd Indicates the file descriptor.
buf Indicates the pointer to a stat structure into which file information is placed.

Returns:

Returns 0 if file information is obtained successfully; returns -1 and sets errno to a value in the following table otherwise.

errno

Description

EBADF

fd is an invalid file descriptor.

EAGAIN

The file list is null.

EINVAL

Invalid parameter.

ENOENT

A component of path does not name an existing file or path is an empty string.

ENAMETOOLONG

The path name length is greater than the value of NAME_MAX.

ENOMEM

Insufficient memory.

ENOSYS

The function is not supported.

EACCES

Search permission is denied on a component of the path prefix.

EIO

An I/O error occurs when data is read from or written to the file system.

EROFS

The physical drive is write-protected.

ENOSPC

No available space on the device.

EISDIR

The named file is a directory.

EPERM

Operation not permitted or access denied because the access is prohibited or the directory is full.

EBUSY

The operation is rejected according to the file sharing policy.

EPROTO

The protocol in NFS is incorrect.

ENODEV

No such device.

EFAULT

Incorrect IP address.

ftw()

int ftw (const char * path, int(*)(const char *file, const struct [stat](stat.md) *sb, int flag) fn, int depth )

Description:

Traverses a file tree.

This function is used to traverse a file tree for search.

Parameters:

Name

Description

path Indicates the pointer to the file directory to traverse.
fn Indicates the pointer to the function to be called for the found entry. For the fn function, file identifies the path of the file relative to path, and sb indicates the pointer to the stat structure. flag indicates the flag. The values of flag are as follows:
depth Indicates the number of files that can be opened during traversal.

value

Description

FTW_F

Regular file

FTW_D

Directory

FTW_DNR

Unreadable directory. Its subdirectories will not be traversed.

FTW_SL

Symbolic link

FTW_NS

The data of the stat structure cannot be obtained. The possible cause is that the permission is incorrect.

Returns:

Returns 0 if the tree is traversed; returns the value that is returned by the fn() function if the traversal is interrupted; returns -1 if an error occurs.

glob()

int glob (const char *__restrict pattern, int flags, int(*)(const char *, int) errfunc, [glob_t](glob_t.md) *__restrict pglob )

Description:

Searches for path names that meet a specified pattern.

Parameters:

Name

Description

pattern Indicates the pattern you want to match.
flags Indicates the matching mode.
errfunc Indicates the pointer to the error processing callback function. If errfunc is not a null pointer, errfunc() is called when an error occurs (opendir(), readdir(), or stat() fails) during the search. If the function returns a non-zero value or GLOB_ERR is set, glob() terminates the entire search process.
pglob Indicates the pointer to the address storing the matched path names.

flags

Description

GLOB_ERR

GLOB_ERR

GLOB_MARK

GLOB_MARK

GLOB_NOSORT

GLOB_NOSORT

GLOB_DOOFFS

GLOB_DOOFFS

GLOB_NOCHECK

GLOB_NOCHECK

GLOB_APPEND

GLOB_APPEND

GLOB_NOESCAPE

GLOB_NOESCAPE

GLOB_PERIOD

GLOB_PERIOD

GLOB_TILDE

GLOB_TILDE

GLOB_TILDE_CHECK

GLOB_TILDE_CHECK

Returns:

Returns 0 on successful completion; returns GLOB_NOSPACE for running out of memory; returns GLOB_ABORTED for a read error; returns GLOB_NOMATCH for no match found.

globfree()

void globfree ([glob_t](glob_t.md) * pglob)

Description:

Frees allocated memory from a call to glob().

Parameters:

Name

Description

pglob Indicates the pointer to the address of memory dynamically allocated for a glob call.

ioctl()

int ioctl (int fd, int cmd,  ... )

Description:

Manipulates a device.

This function is used to perform specified operations on an input device file descriptor.

Parameters:

Name

Description

fd Indicates the file descriptor of the device (file-related file descriptors are not supported).
cmd Indicates the operation instruction for the device. You can define it based on the specific device.

Returns:

Returns 0 if the operation is successful; returns -1 and sets errno to a value in the following table otherwise.

errno

Description

EBADF

fd is an invalid file descriptor.

EFAULT

An inaccessible memory area is referenced.

EINVAL

The command request is invalid.

ENOTTY

fd is not associated with a character special device.

lstat()

int lstat (const char *__restrict path, struct [stat](stat.md) *__restrict buf )

Description:

Obtains file information.

Parameters:

Name

Description

path Indicates the file path.
buf Indicates the pointer to a stat structure into which file information is placed.

Returns:

Returns 0 if file information is obtained successfully; returns -1 and sets errno to a value in the following table otherwise.

errno

Description

EINVAL

Invalid parameter.

ENOENT

A component of path does not name an existing file or path is an empty string.

ENAMETOOLONG

The path name length is greater than the value of NAME_MAX.

ENOMEM

Insufficient memory.

ENOSYS

The function is not supported.

EAGAIN

The file list is null.

EACCES

The permission bit of the file mode does not allow the requested access, or the search permission is denied on the path prefix component.

EEXIST

The file or directory already exists.

EIO

An I/O error occurs when data is read from or written to the file system.

EROFS

The physical drive is write-protected.

ENOSPC

No available space on the device.

ENFILE

The number of opened files in the system has reached the maximum.

ENOTEMPTY

The directory is not empty.

EISDIR

The named file is a directory.

ENOTDIR

A component of the path prefix is the name of an existing file. The file is neither a directory nor a symbolic link of a directory.

EPERM

Operation not permitted or access denied because the access is prohibited or the directory is full.

EBUSY

The operation is rejected according to the file sharing policy.

EPROTO

The protocol in NFS is incorrect.

ENODEV

No such device.

EFAULT

Incorrect IP address.

mkdir()

int mkdir (const char * pathname, mode_t mode )

Description:

Creates a directory.

Parameters:

Name

Description

pathname Indicates the pointer to the name of the directory to create.
mode Indicates the directory permission.

Attention:

This function can NOT be used in the PROC file system.

Returns:

Returns 0 if the directory is created successfully; returns -1 and sets errno to a value in the following table otherwise.

errno

Description

EINVAL

The pathname is a null pointer or an empty string.

EACCES

The permission bit of the file mode does not allow the requested access, or the search permission is denied on the path prefix directory.

ENAMETOOLONG

The path name length is greater than the value of NAME_MAX.

ENOENT

The directory component in the path name does not exist.

EEXIST

The file or directory already exists.

EIO

An I/O error occurs when data is read from or written to the file system.

EROFS

The physical drive is write-protected.

ENOSPC

No available space on the device.

EPERM

Operation not permitted or access denied because the access is prohibited or the directory is full.

EBUSY

The operation is rejected according to the file sharing policy.

ENOSYS

The function is not supported.

EPROTO

The protocol in NFS is incorrect.

EFAULT

Incorrect IP address.

mkdirat()

int mkdirat (int fd, const char * pathname, mode_t mode )

Description:

Creates a directory.

Parameters:

Name

Description

fd Indicates the file descriptor of the target directory. (Currently, only AT_FDCWD is supported.)
pathname Indicates the pointer to the name of the directory to create.
mode Indicates the directory permission.

Attention:

This function can NOT be used in the PROC file system.

Returns:

Returns 0 if the directory is created successfully; returns -1 and sets errno to a value in the following table otherwise.

errno

Description

EINVAL

The pathname is a null pointer or an empty string.

EACCES

The permission bit of the file mode does not allow the requested access, or the search permission is denied on the path prefix directory.

ENAMETOOLONG

The path name length is greater than the value of NAME_MAX.

ENOENT

The directory component in the path name does not exist.

EEXIST

The file or directory already exists.

EIO

An I/O error occurs when data is read from or written to the file system.

EROFS

The physical drive is write-protected.

ENOSPC

No available space on the device.

EPERM

Operation not permitted or access denied because the access is prohibited or the directory is full.

EBUSY

The operation is rejected according to the file sharing policy.

ENOSYS

The function is not supported.

EPROTO

The protocol in NFS is incorrect.

EFAULT

Incorrect IP address.

mkfifo()

int mkfifo (const char * name, mode_t mode )

Description:

Creates a named pipe to implement inter-process communication.

Parameters:

Name

Description

name Indicates the pointer to the name of the pipe to create.
mode Not supported.

Attention:

Permission control is not supported. The number of pipes that can be created is not limited. Pipes can be created only in the /dev directory.

Returns:

Returns 0 if the pipe is created successfully; returns -1 and sets errno to a value in the following table otherwise.

errno

Description

EINVAL

Invalid input parameter (for example, empty address, file name too long or the file is not in the /dev directory).

EEXIST

The file already exists.

ENOMEM

Insufficient memory.

mount()

int mount (const char * source, const char * target, const char * filesystemtype, unsigned long mountflags, const void * data )

Description:

Mounts a file system.

Mounts a device (referenced by a file system name) to a specified directory.

Parameters:

Name

Description

source Indicates the pointer to the device to mount.
target Indicates the pointer to the path of the mount point.
filesystemtype Indicates the pointer to the file system type.
mountflags Indicates the mounting flag.
data Indicates the pointer to the mounting data.

Returns:

Returns 0 if the mounting is successful; returns -1 and sets errno to a value in the following table otherwise.

errno

Description

EACCES

A component of the path was not searchable.

EBUSY

source is already mounted.

EFAULT

One of the pointer parameters points outside the user address space.

EINVAL

source cannot be mounted.

ELOOP

Too many links encountered during path name resolution.

EMFILE

(Non-block device) Virtual device table is full.

ENAMETOOLONG

The path name length is greater than the value of MAXPATHLEN.

ENODEV

No such file system type is configured in the kernel.

ENOENT

A path name is null or has a nonexistent component.

ENOMEM

Insufficient memory.

ENOTBLK

source is not a block device.

ENOTDIR

target or the prefix of source is not a directory.

ENGIO

No such device.

EPERM

No permission.

nftw()

int nftw (const char * path, int(*)(const char *file, const struct [stat](stat.md) *sb, int flag, struct [FTW](FTW.md) *s) fn, int fd_limit, int flags )

Description:

Traverses a file tree.

This function is used to traverse a file tree. It has a similar effect to ftw except that it takes an additional parameter flags.

Parameters:

Name

Description

path Indicates the pointer to the file directory to traverse.
fn Indicates the pointer to the function to be called for the found entry. For the fn function, file identifies the path of the file relative to path, and sb indicates the pointer to the stat structure. flag indicates the flag. The values of flag are as follows: The fourth argument of fn() is a pointer to a FTW structure.
fd_limit Indicates the number of files that can be opened during traversal.
flags Indicates the traversal action. It is a bitwise inclusive-OR of zero or more of the following flags:

value

Description

FTW_F

Regular file

FTW_D

Directory

FTW_DNR

Unreadable directory. Its subdirectories will not be traversed.

FTW_SL

Symbolic link

FTW_NS

The data of the stat structure cannot be obtained. The possible cause is that the permission is incorrect.

FTW_DP

Directory. All subdirectories have been traversed.

FTW_SLN

Symbolic link that names a non-existent file

Returns:

Returns 0 if the tree is traversed; returns the value that is returned by the fn() function if the traversal is interrupted; returns -1 if an error occurs.

open()

int open (const char * path, int oflags,  ... )

Description:

Opens a file.

Parameters:

Name

Description

path Indicates the pointer to the name of the file to open.
oflags Indicates the file access mode. Available values are as follows:

oflags

Description

O_RDONLY

Read-only

O_WRONLY

Write-only

O_RDWR

Read and write

O_APPEND

If set, the file offset will be set to the end of the file prior to each write.

O_CREAT

If the file does not exist, it will be created.

O_LARGEFILE

A file exceeding 2 GB can be created.

O_TRUNC

If the file is opened successfully with O_WRONLY or O_RDWR, its length is truncated to 0.

O_EXCL

Used together with O_CREAT. This function fails if the file exists.

O_DIRECTORY

This flag must be added for opening a directory. Otherwise, the directory fails to be opened.

Attention:

This function can NOT be used in the PROC file system to create a file. This function is used to open a file. If the file does not exist, this function creates a file and opens it.

Returns:

Returns the file descriptor if the file is opened successfully; returns -1 and sets errno to a value in the following table otherwise.

errno

Description

EACCES

Search permission is denied on a component of the path prefix.

EINVAL

The path name format is invalid.

ENAMETOOLONG

The length of path is greater than the value of NAME_MAX.

ENOENT

The O_CREAT flag is not set and the named file does not exist.

ENXIO

Inode is invalid, not a normal character driver, or not a mount point.

EMFILE

All available file descriptors of the process are opened.

EPERM

The file structure corresponding to the file descriptor cannot be obtained.

EEXIST

O_CREAT and O_EXCL are set.

EIO

A hardware error occurs at the low-level disk I/O layer. As a result, the physical drive cannot work.

EROFS

The named file resides on a read-only file system, and either O_WRONLY, O_RDWR, O_CREAT (if file does not exist), or O_TRUNC is set in the oflags parameter.

ENOSPC

The directory or file system that would contain the new file cannot be expanded, the file does not exist, and O_CREAT is specified.

ENFILE

The number of opened files in the system has reached the maximum.

EISDIR

The named file is a directory, and oflags includes O_WRONLY, O_RDWR, or O_CREAT without O_DIRECTORY.

ENOTDIR

A component of the path prefix names an existing file that is not a directory.

EBUSY

The operation is rejected according to the file sharing policy.

ENODEV

The device does not exist.

ENOTEMPTY

The directory is not empty.

ELOOP

Too many symbolic links are encountered.

EFAULT

The IP address is incorrect.

opendir()

[DIR](FS.md#ga0ebe68390948c14bb9d82987adbfc849)* opendir (const char * dirname)

Description:

Opens a specified directory.

Parameters:

Name

Description

dirname Indicates the pointer to the name of directory to open.

Returns:

Returns the pointer to the DIR structure representing the directory if it is opened successfully; returns NULL and sets errno to a value in the following table otherwise.

errno

Description

EINVAL

The dirname is empty.

ENAMETOOLONG

The length of the path name string is longer than the value of NAME_MAX.

ENOENT

The path component does not exist.

ENOMEM

The storage memory is insufficient.

ENOTDIR

dirname is not a directory.

readdir()

struct [dirent](dirent.md)* readdir ([DIR](FS.md#ga0ebe68390948c14bb9d82987adbfc849) * dirp)

Description:

Reads a specified directory.

Parameters:

Name

Description

dirp Indicates the pointer to the directory structure.

Returns:

Returns the pointer to the dirent structure representing the directory if it is read successfully; returns NULL if reaching the end of the directory stream or the directory fails to be read. If the directory fails to be read, the corresponding error code is returned.

errno

Description

EACCES

This feature is not supported by the file system.

ENAMETOOLONG

The path name length is greater than the value of NAME_MAX.

ENOENT

The current position in the directory stream is invalid.

EBADF

dirp is not an opened directory stream.

readdir_r()

int readdir_r ([DIR](FS.md#ga0ebe68390948c14bb9d82987adbfc849) *__restrict dirp, struct [dirent](dirent.md) *__restrict entry, struct [dirent](dirent.md) **__restrict result )

Description:

Reads a specified directory (thread-safe version).

Parameters:

Name

Description

dirp Indicates the pointer to the directory structure.
entry Indicates the entry read from the dirp stream.
result Indicates the pointer to the returned buffer.

Returns:

Returns 0. If the directory fails to be read, errno is sets to a value in the following table.

errno

Description

EACCES

This feature is not supported by the file system.

ENAMETOOLONG

The path name length is greater than the value of NAME_MAX.

ENOENT

The current position in the directory stream is invalid.

EBADF

dirp is not an opened directory stream.

rewinddir()

void rewinddir ([DIR](FS.md#ga0ebe68390948c14bb9d82987adbfc849) * dirp)

Description:

Resets a directory stream read position.

This function is used to change the current read position of the specified directory stream to the original read position (that is, the start position after the directory stream is opened by opendir).

Parameters:

Name

Description

dirp Indicates the pointer to the directory stream whose read position needs to be reset.

errno

Description

EBADF

dirp is not an opened directory stream.

scandir()

int scandir (const char * dir, struct [dirent](dirent.md) *** namelist, int(*)(const struct [dirent](dirent.md) *) sel, int(*)(const struct [dirent](dirent.md) **, const struct [dirent](dirent.md) **) compar )

Description:

Scans directory to find the target entries.

Parameters:

Name

Description

dir Indicates the pointer to the directory to scan.
namelist Indicates the array storing the directory entries filtered by the function referenced by the sel parameter.
sel Indicates the filtering condition. You can customize the function referenced by this parameter. Value 0 is returned if the filtering condition is not met; a non-zero value is returned otherwise.
compar Indicates the pointer to the function for sorting the directory entries.

Returns:

Returns the number of entries copied to the namelist array if the scanning is successful; returns -1 and sets errno to a value in the following table otherwise.

errno

Description

EINVAL

dir is empty.

ENAMETOOLONG

The length of the path name string is greater than the value of NAME_MAX.

ENOENT

The directory component does not exist.

ENOMEM

The storage memory is insufficient.

ENOTDIR

dir is not a directory.

EACCES

This feature is not supported by the file system.

seekdir()

void seekdir ([DIR](FS.md#ga0ebe68390948c14bb9d82987adbfc849) * dirp, long offset )

Description:

Sets the position of the next readdir call in the directory stream.

Parameters:

Name

Description

dirp Indicates the pointer to the directory stream in which the position of the next readdir call needs to be set.
offset Indicates the offset of the position relative to the current position.

stat()

int [stat](stat.md) (const char *__restrict path, struct [stat](stat.md) *__restrict buf )

Description:

Obtains file information.

Parameters:

Name

Description

path Indicates the file path.
buf Indicates the pointer to a stat structure into which file information is placed.

Returns:

Returns 0 if file information is obtained successfully; returns -1 and sets errno to a value in the following table otherwise.

errno

Description

EINVAL

Invalid parameter.

ENOENT

A component of path does not name an existing file or path is an empty string.

ENAMETOOLONG

The path name length is greater than the value of NAME_MAX.

ENOMEM

Insufficient memory.

ENOSYS

The function is not supported.

EAGAIN

The file list is null.

EACCES

The permission bit of the file mode does not allow the requested access, or the search permission is denied on the path prefix component.

EEXIST

The file or directory already exists.

EIO

An I/O error occurs when data is read from or written to the file system.

EROFS

The physical drive is write-protected.

ENOSPC

No available space on the device.

ENFILE

The number of opened files in the system has reached the maximum.

ENOTEMPTY

The directory is not empty.

EISDIR

The named file is a directory.

ENOTDIR

A component of the path prefix is the name of an existing file. The file is neither a directory nor a symbolic link of a directory.

EPERM

Operation not permitted or access denied because the access is prohibited or the directory is full.

EBUSY

The operation is rejected according to the file sharing policy.

EPROTO

The protocol in NFS is incorrect.

ENODEV

No such device.

EFAULT

Incorrect IP address.

statfs()

int statfs (const char * path, struct statfs * buf )

Description:

Obtains file system information of a file in a specified path.

Parameters:

Name

Description

path Indicates the pointer to the name of the target file.
buf Indicates the pointer to a statfs structure that stores the information about the file system.

Returns:

Returns 0 if the operation is successful; returns -1 and sets errno to a value in the following table if the operation fails.

errno

Description

EACCES

Search permission is denied on a component of the path prefix.

EFAULT

buf or path points to an invalid address.

EINTR

The function call was interrupted by a signal.

EIO

An I/O error occurred while reading from the file system.

ELOOP

Too many symbolic links were found.

ENAMETOOLONG

path is too long.

ENOENT

The file referred to by path does not exist.

ENOMEM

Insufficient memory.

ENOSYS

The operation is not supported.

ENOTDIR

A component of the path prefix of path is not a directory.

EOVERFLOW

Information overflow

statvfs()

int [statvfs](statvfs.md) (const char *__restrict path, struct [statvfs](statvfs.md) *__restrict buf )

Description:

Obtains the file system information.

Parameters:

Name

Description

path Indicates the pointer to the path name of the file.
buf Indicates the pointer to the statvfs structure that stores the obtained file system information.

Returns:

Returns 0 if the file system information is obtained successfully; returns -1 and sets errno to a value in the following table otherwise.

errno

Description

EACCES

Search permission is denied on a component of the path prefix.

EFAULT

buf or path points to an invalid address.

EINTR

This call was interrupted by a signal.

EIO

An I/O error occurred while reading from the file system.

ELOOP

Too many symbolic links were found.

ENAMETOOLONG

path is too long.

ENOENT

The file specified by path does not exist.

ENOMEM

Insufficient memory.

ENOSYS

The file system does not support this call.

ENOTDIR

A component of the path prefix of path is not a directory.

EOVERFLOW

Information overflows.

telldir()

long telldir ([DIR](FS.md#ga0ebe68390948c14bb9d82987adbfc849) * dirp)

Description:

Obtains the current position in the specified directory stream.

Parameters:

Name

Description

dirp Indicates the pointer to the directory stream.

Returns:

Returns the current position in the directory stream if the operation is successful; returns -1 and sets errno to the value in the following table otherwise.

errno

Description

EBADF

drip is not an opened directory stream.

umask()

mode_t umask (mode_t mode)

Description:

Sets umask for a process.

This function sets the umask of the current process file and returns the previous value of the umask. umask is used to turn off permission bits corresponding to in the mode parameter.

Parameters:

Name

Description

mode Used to specify the umask value of a process.

Returns:

Returns the previous value of the umask. This function call always succeeds.

umount()

int umount (const char * target)

Description:

Unmounts a file system.

Parameters:

Name

Description

target Indicates the pointer to the path of the file system to unmount.

Returns:

Returns 0 if the file system is unmounted successfully; returns -1 and sets errno to a value in the following table otherwise.

errno

Description

EBUSY

target could not be unmounted because it is busy.

EFAULT

target points outside the user address space.

EINVAL

target is not a mount point.

ENAMETOOLONG

The path length is greater than the value of MAXPATHLEN.

ENOENT

A path name is null or has a nonexistent component.

ENOMEM

Insufficient memory.

EPERM

No permission.

umount2()

int umount2 (const char * target, int flags )

Description:

Unmounts a file system.

This function is used to unmount a file system. Currently, the value of flags can only be 0. In this case, this function is the same as umount().

Parameters:

Name

Description

target Indicates the pointer to the path of the file system to unmount.
flags Indicates the control flag. (Currently, the value of flags can only be 0. In this case, this function is the same as umount().)

Returns:

Returns 0 if the file system is unmounted successfully; returns -1 and sets errno to a value in the following table otherwise.

errno

Description

EBUSY

target could not be unmounted because it is busy.

EFAULT

target points outside the user address space.

EINVAL

target is not a mount point.

ENAMETOOLONG

The path length is greater than the value of MAXPATHLEN.

ENOENT

A path name is null or has a nonexistent component.

ENOMEM

Insufficient memory.

EPERM

No permission.

ENOSYS

The function call is not supported. (flags value is not 0.)

utime()

int utime (const char * filename, const struct [utimbuf](utimbuf.md) * times )

Description:

Sets the access time and modification time of a file.

Parameters:

Name

Description

filename Indicates the pointer to the name of the file whose access time and modification time need to be set.
times Indicates the pointer to the time to which the access time and modification time are set. If times is a null pointer, then the access time and modification time are set to the current time.

Returns:

Returns 0 if the access time and modification time are set successfully; returns -1 and sets errno to a value in the following table otherwise.

errno

Description

EINVAL

filename is null or times is invalid.

ENAMETOOLONG

The length of the component in the path name is greater than the value of NAME_MAX.

ENOENT

Some paths do not exist.

ENOSYS

This function is not supported.

EACCES

The file system is read-only.

ENOMEM

Insufficient memory.

EIO

A hardware error occurs at the underlying disk I/O layer.

1
https://gitee.com/famoustang/docs.git
git@gitee.com:famoustang/docs.git
famoustang
docs
docs
master

搜索帮助