Access directory content. Read and write to a file system directory.
Directory | Access directory content. |
Copyright | This program is free software. |
Files | |
C-kern/ | Header file of Directory. |
C-kern/ | Linux specific implementation Directory Linux. |
Types | |
struct directory_t | Export opaque directory_t to read/write a directory in the file system. |
Enumerations | |
filetype_e | Encodes the type of the file the filename refers to. |
Functions | |
test | |
unittest_io_directory | Test reading and writing content from and to directories. |
directory_t | |
lifetime | |
new_directory | Opens directory stream for read access. |
newtemp_directory | Creates a temporary directory read / writeable by the user and opens it for reading. |
delete_directory | Closes open directory stream. |
query | |
trypath_directory | Checks that file_path refers to an existing file or directory. |
io_directory | Returns iochannel_t (file descriptor) of the opened directory. |
filesize_directory | Returns the filesize of a file with path »file_path«. |
path_directory | Returns absolute real path of directory dir in path. |
read | |
next_directory | Reads the next directory entry and returns its name and type (ftype could be NULL). |
gofirst_directory | Sets the current read position to the begin of the directory stream. |
write | |
makedirectory_directory | Creates directory with directory_path relative to dir. |
makefile_directory | Creates a new file with file_path relative to dir. |
removedirectory_directory | Removes the empty directory with directory_path relative to dir. |
removefile_directory | Removes the file with file_path relative to dir. |
inline implementation | |
Macros | |
io_directory | Implements directory_t.io_directory. |
This program is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
© 2011 Jörg Seebohn
Header file of Directory.
Linux specific implementation Directory Linux.
typedef struct directory_t directory_t
Export opaque directory_t to read/write a directory in the file system.
Can also be used to represent a position in the file system. Other file system operations can take an argument of type directory_t and resolve relative paths against it.
In case the directory is set to 0 relative paths are resolved by using the current working directory as starting point.
Encodes the type of the file the filename refers to.
test | |
unittest_io_directory | Test reading and writing content from and to directories. |
lifetime | |
new_directory | Opens directory stream for read access. |
newtemp_directory | Creates a temporary directory read / writeable by the user and opens it for reading. |
delete_directory | Closes open directory stream. |
query | |
trypath_directory | Checks that file_path refers to an existing file or directory. |
io_directory | Returns iochannel_t (file descriptor) of the opened directory. |
filesize_directory | Returns the filesize of a file with path »file_path«. |
path_directory | Returns absolute real path of directory dir in path. |
read | |
next_directory | Reads the next directory entry and returns its name and type (ftype could be NULL). |
gofirst_directory | Sets the current read position to the begin of the directory stream. |
write | |
makedirectory_directory | Creates directory with directory_path relative to dir. |
makefile_directory | Creates a new file with file_path relative to dir. |
removedirectory_directory | Removes the empty directory with directory_path relative to dir. |
removefile_directory | Removes the file with file_path relative to dir. |
int new_directory( /*out*/directory_t ** dir, const char * dir_path, const directory_t * relative_to/*0 = > current working directory*/ )
Opens directory stream for read access. If relative_to is NULL then the dir_path is relative to the current working directory else it is considered relative to relative_to. If dir_path is absolute it does not matter what value relative_to has.
To open the current working directory dir_path can be set to either “.” or “”.
0 | Directory stream is open and dir is valid (in case of error dir is not changed). |
EACCES | Permission denied. |
EMFILE | Too many file descriptors in use by process. |
ENFILE | Too many files are currently open in the system. |
ENOENT | Directory does not exist, or name is an empty string. |
ENOMEM | Insufficient memory to complete the operation. |
ENOTDIR | name is not a directory. |
int newtemp_directory( /*out*/directory_t ** dir, const char * name_prefix )
Creates a temporary directory read / writeable by the user and opens it for reading. The name of the directory is prefixed with name_prefix and it is created in the temporary system directory. The name of the created directory is returned in parameter dir_path. Set this parameter to 0 if you do not need the name.
int trypath_directory( const directory_t * dir/*0 = > current working directory*/, const char * const file_path )
Checks that file_path refers to an existing file or directory. file_path is considered relative to dir or to the current working directory if dir is null. If file_path is absolute the value of dir does not matter.
0 | file_path refers to an existing file or directory. |
ENOENT | file_path does not refer to an existing file system entry. No error log entry is written. |
sys_iochannel_t io_directory( const directory_t * dir )
Returns iochannel_t (file descriptor) of the opened directory. Do not free it after usage else dir can not be accessed any more.
int filesize_directory( const directory_t * dir/*0 = > current working directory*/, const char * file_path, /*out*/off_t * file_size )
Returns the filesize of a file with path »file_path«. file_path is considered relative to dir. If dir is NULL then it is considered relative to the current working directory. If file_path is absolute the value of dir does not matter.
int path_directory( const directory_t * dir, /*ret*/struct wbuffer_t * path )
Returns absolute real path of directory dir in path. If dir was initializated with a path containing symlinks the returned path will differ from the one used during initialization. The returned path contains at least ‘/’ as first character. A trailing ‘\0’ is not appended at the end of the returned string. If wbuffer_t wraps a cstring_t a ‘\0’ byte is added automatically else do not forget to add your own if you use C string functions.
int next_directory( directory_t * dir, /*out*/const char ** name, /*out*/filetype_e * ftype )
Reads the next directory entry and returns its name and type (ftype could be NULL).
name | On success a pointer to the file name is returned. Name should never be freed. It is valid until any other function on dir is called. If there is no next entry name is set to NULL. |
ftype | On success and if ftype != NULL it contains the type of file. |
int gofirst_directory( directory_t * dir )
Sets the current read position to the begin of the directory stream. The next call to next_directory returns the first entry again.
int makedirectory_directory( directory_t * dir/*0 = > current working directory*/, const char * directory_path )
Creates directory with directory_path relative to dir. If dir is 0 the directory_path is relative to the current working directory. If directory_path is absolute then the parameter dir does not matter.
int makefile_directory( directory_t * dir/*0 = > current working directory*/, const char * file_path, off_t file_length )
Creates a new file with file_path relative to dir. If dir is 0 the file_path is relative to the current working directory. If file_path is absolute then the parameter dir does not matter.
int removedirectory_directory( directory_t * dir/*0 = > current working directory*/, const char * directory_path )
Removes the empty directory with directory_path relative to dir. If dir is 0 the directory_path is relative to the current working directory. If directory_path is absolute then the parameter dir does not matter.
int removefile_directory( directory_t * dir/*0 = > current working directory*/, const char * file_path )
Removes the file with file_path relative to dir. If dir is 0 the file_path is relative to the current working directory. If file_path is absolute then the parameter dir does not matter.
Macros | |
io_directory | Implements directory_t.io_directory. |
#define io_directory( dir ) (dirfd((DIR*)CONST_CAST(directory_t, dir)))
Implements directory_t.io_directory.
Export opaque directory_t to read/write a directory in the file system.
typedef struct directory_t directory_t
Test reading and writing content from and to directories.
int unittest_io_directory( void )
Opens directory stream for read access.
int new_directory( /*out*/directory_t ** dir, const char * dir_path, const directory_t * relative_to/*0 = > current working directory*/ )
Creates a temporary directory read / writeable by the user and opens it for reading.
int newtemp_directory( /*out*/directory_t ** dir, const char * name_prefix )
Closes open directory stream.
int delete_directory( directory_t ** dir )
Checks that file_path refers to an existing file or directory.
int trypath_directory( const directory_t * dir/*0 = > current working directory*/, const char * const file_path )
Returns iochannel_t (file descriptor) of the opened directory.
sys_iochannel_t io_directory( const directory_t * dir )
Describes an input/output channel like a file or network connection.
typedef sys_iochannel_t iochannel_t
Returns the filesize of a file with path »file_path«.
int filesize_directory( const directory_t * dir/*0 = > current working directory*/, const char * file_path, /*out*/off_t * file_size )
Returns absolute real path of directory dir in path.
int path_directory( const directory_t * dir, /*ret*/struct wbuffer_t * path )
Reads the next directory entry and returns its name and type (ftype could be NULL).
int next_directory( directory_t * dir, /*out*/const char ** name, /*out*/filetype_e * ftype )
Sets the current read position to the begin of the directory stream.
int gofirst_directory( directory_t * dir )
Creates directory with directory_path relative to dir.
int makedirectory_directory( directory_t * dir/*0 = > current working directory*/, const char * directory_path )
Creates a new file with file_path relative to dir.
int makefile_directory( directory_t * dir/*0 = > current working directory*/, const char * file_path, off_t file_length )
Removes the empty directory with directory_path relative to dir.
int removedirectory_directory( directory_t * dir/*0 = > current working directory*/, const char * directory_path )
Removes the file with file_path relative to dir.
int removefile_directory( directory_t * dir/*0 = > current working directory*/, const char * file_path )
Implements directory_t.io_directory.
#define io_directory( dir ) (dirfd((DIR*)CONST_CAST(directory_t, dir)))
Supports construction of return values of unknown size.
struct wbuffer_t
Dynamically growing C string with trailing ‘\0’ byte.
struct cstring_t