MentOS  0.8.0
The Mentoring Operating System
Macros | Functions
namei.c File Reference

Implementation of functions fcntl() and open(). More...

Macros

#define __DEBUG_HEADER__   "[NAMEI ]"
 Change header.
 
#define __DEBUG_LEVEL__   LOGLEVEL_NOTICE
 Set log level.
 
#define APPEND_PATH_SEPARATOR(buffer, buflen)
 Appends the path with a "/" as separator. More...
 
#define APPEND_PATH(buffer, token)
 Appends the path. More...
 

Functions

int sys_unlink (const char *path)
 Delete a name and possibly the file it refers to. More...
 
int sys_mkdir (const char *path, mode_t mode)
 Creates a new directory at the given path. More...
 
int sys_rmdir (const char *path)
 Removes the given directory. More...
 
int sys_creat (const char *path, mode_t mode)
 Creates a new file or rewrite an existing one. More...
 
int sys_symlink (const char *linkname, const char *path)
 Creates a symbolic link. More...
 
int sys_readlink (const char *path, char *buffer, size_t bufsize)
 Read the symbolic link, if present. More...
 
char * realpath (const char *path, char *buffer, size_t buflen)
 Return the canonicalized absolute pathname. More...
 
static int __is_a_link (const char *path)
 Determines if the path points to a link. More...
 
static int __get_link_content (const char *path, char *buffer, size_t buflen)
 Returns the content of the link. More...
 
int __resolve_path (const char *path, char *abspath, size_t buflen, int flags, int link_depth)
 Resolve the path by following all symbolic links. More...
 
int resolve_path (const char *path, char *buffer, size_t buflen, int flags)
 

Detailed Description

Implementation of functions fcntl() and open().

Macro Definition Documentation

◆ APPEND_PATH

#define APPEND_PATH (   buffer,
  token 
)
Value:
{ \
strncat(buffer, token, strlen(token)); \
}
size_t strlen(const char *s)
Returns the length of the string s.
Definition: string.c:496

Appends the path.

◆ APPEND_PATH_SEPARATOR

#define APPEND_PATH_SEPARATOR (   buffer,
  buflen 
)
Value:
{ \
if (buffer[strnlen(buffer, buflen) - 1] != '/') { strncat(buffer, "/", buflen); } \
}
size_t strnlen(const char *s, size_t count)
Returns the number of characters inside s, excluding the terminating null byte ('\0'),...
Definition: string.c:503
char * strncat(char *s1, const char *s2, size_t n)
Appends a copy of the string src to the string dst, up to n bytes.
Definition: string.c:332

Appends the path with a "/" as separator.

Function Documentation

◆ __get_link_content()

static int __get_link_content ( const char *  path,
char *  buffer,
size_t  buflen 
)
inlinestatic

Returns the content of the link.

Parameters
paththe path to the file.
bufferthe buffer where we store the link content.
buflenlength of the buffer.
Returns
the length of the link, or a negative value on error.

◆ __is_a_link()

static int __is_a_link ( const char *  path)
inlinestatic

Determines if the path points to a link.

Parameters
paththe path to the file.
Returns
1 if it is a link, 0 otherwise.

◆ __resolve_path()

int __resolve_path ( const char *  path,
char *  abspath,
size_t  buflen,
int  flags,
int  link_depth 
)

Resolve the path by following all symbolic links.

Parameters
paththe path to resolve.
abspaththe buffer where the resolved path is stored.
buflenthe size of the provided resolved_path buffer.
flagsthe flags controlling how the path is resolved.
link_depththe current link depth.
Returns
-errno on fail, 1 on success.

◆ realpath()

char* realpath ( const char *  path,
char *  buffer,
size_t  buflen 
)

Return the canonicalized absolute pathname.

Parameters
paththe path we are canonicalizing.
bufferwhere we will store the canonicalized path.
buflenthe size of the buffer.
Returns
If there is no error, realpath() returns a pointer to the buffer. Otherwise, it returns NULL, the contents of the array buffer are undefined, and errno is set to indicate the error.

◆ sys_creat()

int sys_creat ( const char *  path,
mode_t  mode 
)

Creates a new file or rewrite an existing one.

Parameters
pathpath to the file.
modemode for file creation.
Returns
file descriptor number, -1 otherwise and errno is set to indicate the error.

It is equivalent to: open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)

◆ sys_mkdir()

int sys_mkdir ( const char *  path,
mode_t  mode 
)

Creates a new directory at the given path.

Parameters
pathThe path of the new directory.
modeThe permission of the new directory.
Returns
Returns a negative value on failure.

◆ sys_readlink()

int sys_readlink ( const char *  path,
char *  buffer,
size_t  bufsize 
)

Read the symbolic link, if present.

Parameters
paththe file for which we want to read the symbolic link information.
bufferthe buffer where we will store the symbolic link path.
bufsizethe size of the buffer.
Returns
The number of read characters on success, -1 otherwise and errno is set to indicate the error.

◆ sys_rmdir()

int sys_rmdir ( const char *  path)

Removes the given directory.

Parameters
pathThe path to the directory to remove.
Returns
Returns a negative value on failure.

◆ sys_symlink()

int sys_symlink ( const char *  linkname,
const char *  path 
)

Creates a symbolic link.

Parameters
linknamethe name of the link.
paththe entity it is linking to.
Returns
0 on success, a negative number if fails and errno is set.

◆ sys_unlink()

int sys_unlink ( const char *  path)

Delete a name and possibly the file it refers to.

Parameters
pathA pathname for a file.
Returns
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.