Directory

Access directory content.  Read and write to a file system directory.

Summary
DirectoryAccess directory content.
CopyrightThis program is free software.
Files
C-kern/api/io/filesystem/directory.hHeader file of Directory.
C-kern/platform/Linux/io/directory.cLinux specific implementation Directory Linux.
Types
struct directory_tExport opaque directory_t to read/write a directory in the file system.
Enumerations
filetype_eEncodes the type of the file the filename refers to.
Functions
test
unittest_io_directoryTest reading and writing content from and to directories.
directory_t
lifetime
new_directoryOpens directory stream for read access.
newtemp_directoryCreates a temporary directory read / writeable by the user and opens it for reading.
delete_directoryCloses open directory stream.
query
trypath_directoryChecks that file_path refers to an existing file or directory.
io_directoryReturns iochannel_t (file descriptor) of the opened directory.
filesize_directoryReturns the filesize of a file with path »file_path«.
path_directoryReturns absolute real path of directory dir in path.
read
next_directoryReads the next directory entry and returns its name and type (ftype could be NULL).
gofirst_directorySets the current read position to the begin of the directory stream.
write
makedirectory_directoryCreates directory with directory_path relative to dir.
makefile_directoryCreates a new file with file_path relative to dir.
removedirectory_directoryRemoves the empty directory with directory_path relative to dir.
removefile_directoryRemoves the file with file_path relative to dir.
inline implementation
Macros
io_directoryImplements directory_t.io_directory.

Copyright

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.

Author

© 2011 Jörg Seebohn

Files

C-kern/api/io/filesystem/directory.h

Header file of Directory.

C-kern/platform/Linux/io/directory.c

Linux specific implementation Directory Linux.

Types

struct directory_t

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.

Enumerations

filetype_e

Encodes the type of the file the filename refers to.

ftUnknownUnknown file type.
ftBlockDeviceA block device special file.  For example a hard disk.
ftCharacterDeviceA character device special file.  For example a keyboard.
ftDirectoryDirectory type.
ftNamedPipeNamed pipe type.
ftSymbolicLinkA symbolic link.  This value is returned from <next_directory> only if the file the symbolic link refers to does not exist.  If it exists the type of the linked file is returned.
ftRegularFileA normal data file.
ftSocketA unix system socket.  *

Functions

Summary
test
unittest_io_directoryTest reading and writing content from and to directories.

test

unittest_io_directory

int unittest_io_directory(void)

Test reading and writing content from and to directories.

directory_t

Summary
lifetime
new_directoryOpens directory stream for read access.
newtemp_directoryCreates a temporary directory read / writeable by the user and opens it for reading.
delete_directoryCloses open directory stream.
query
trypath_directoryChecks that file_path refers to an existing file or directory.
io_directoryReturns iochannel_t (file descriptor) of the opened directory.
filesize_directoryReturns the filesize of a file with path »file_path«.
path_directoryReturns absolute real path of directory dir in path.
read
next_directoryReads the next directory entry and returns its name and type (ftype could be NULL).
gofirst_directorySets the current read position to the begin of the directory stream.
write
makedirectory_directoryCreates directory with directory_path relative to dir.
makefile_directoryCreates a new file with file_path relative to dir.
removedirectory_directoryRemoves the empty directory with directory_path relative to dir.
removefile_directoryRemoves the file with file_path relative to dir.

lifetime

new_directory

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.

Current working directory

To open the current working directory dir_path can be set to either “.” or “”.

Returns

0Directory stream is open and dir is valid (in case of error dir is not changed).
EACCESPermission denied.
EMFILEToo many file descriptors in use by process.
ENFILEToo many files are currently open in the system.
ENOENTDirectory does not exist, or name is an empty string.
ENOMEMInsufficient memory to complete the operation.
ENOTDIRname is not a directory.

newtemp_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.

delete_directory

int delete_directory(directory_t **dir)

Closes open directory stream.  Frees allocated memory.  Calling free twice is safe.  Calling it without a preceding init results in undefined behaviour.

query

trypath_directory

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.

Returns

0file_path refers to an existing file or directory.
ENOENTfile_path does not refer to an existing file system entry.  No error log entry is written.

io_directory

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.

filesize_directory

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.

path_directory

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.

read

next_directory

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).

Parameters

nameOn 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.
ftypeOn success and if ftype != NULL it contains the type of file.

gofirst_directory

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.

write

makedirectory_directory

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.

makefile_directory

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.

removedirectory_directory

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.

removefile_directory

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.

inline implementation

Macros

io_directory

#define io_directory(dir) (dirfd((DIR*)CONST_CAST(directory_t, dir)))

Implements directory_t.io_directory.

Access directory content.
Implements Directory.
typedef struct directory_t directory_t
Export opaque directory_t to read/write a directory in the file system.
int unittest_io_directory(void)
Test reading and writing content from and to directories.
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.
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.
int delete_directory(directory_t **dir)
Closes open directory stream.
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.
sys_iochannel_t io_directory(const directory_t *dir)
Returns iochannel_t (file descriptor) of the opened directory.
typedef sys_iochannel_t iochannel_t
Describes an input/output channel like a file or network connection.
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«.
int path_directory(const directory_t *dir,
/*ret*/struct wbuffer_t *path)
Returns absolute real path of directory dir in path.
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).
int gofirst_directory(directory_t *dir)
Sets the current read position to the begin of the directory stream.
int makedirectory_directory(directory_t *dir/*0 = > current working directory*/,
const char *directory_path  )
Creates directory with directory_path relative to dir.
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.
int removedirectory_directory(directory_t *dir/*0 = > current working directory*/,
const char *directory_path  )
Removes the empty directory with directory_path relative to dir.
int removefile_directory(directory_t *dir/*0 = > current working directory*/,
const char *file_path  )
Removes the file with file_path relative to dir.
#define io_directory(dir) (dirfd((DIR*)CONST_CAST(directory_t, dir)))
Implements directory_t.io_directory.
struct wbuffer_t
Supports construction of return values of unknown size.
struct cstring_t
Dynamically growing C string with trailing ‘\0’ byte.
Close