HCore SysFileSystem

Files CCore/inc/sys/SysFileSystem.h CCore/src/sys/SysFileSystem.cpp

This part provides OS backed file system operations. These operations comply with the general file operation set.


#ifndef CCore_inc_sys_SysFileSystem_h
#define CCore_inc_sys_SysFileSystem_h
 
#include <CCore/inc/GenFile.h>

namespace CCore {
namespace Sys {

/* classes */ 

struct FileSystem;

/* struct FileSystem */ 

struct FileSystem
 {
  // private

  ....

  // public
  
  struct TypeResult
   {
    FileType type;
    FileError error;
   };

  struct CmpTimeResult
   {
    CmpFileTimeType time;
    FileError error;
   };
  
  struct PathOfResult
   {
    StrLen path;
    FileError error;
   };
   
  struct DirCursor 
   {
    // private

    ....

    // public
   
    char file_name[MaxPathLen]; // file name only
    ulen len;
    FileType type;
    FileError error;
    
    void init(FileSystem *fs,StrLen dir_name) noexcept;
  
    void exit() noexcept;
    
    bool next() noexcept;
   };
 
  // public
  
  FileError init() noexcept;
  
  FileError exit() noexcept;
  
  TypeResult getFileType(StrLen path) noexcept;

  CmpTimeResult getFileUpdateTime(StrLen path) noexcept;
  
  FileError createFile(StrLen file_name) noexcept;
  
  FileError deleteFile(StrLen file_name) noexcept;
  
  FileError createDir(StrLen dir_name) noexcept;
  
  FileError deleteDir(StrLen dir_name,bool recursive) noexcept;
  
  FileError rename(StrLen old_path,StrLen new_path,bool allow_overwrite) noexcept;
  
  FileError remove(StrLen path) noexcept;
  
  FileError exec(StrLen dir,StrLen program,StrLen arg) noexcept;
  
  PathOfResult pathOf(StrLen path,char buf[MaxPathLen+1]) noexcept;
 };
 
} // namespace Sys
} // namespace CCore
 
#endif

To perform a file system operation you create an instance of the structure FileSystem and initialize it. Then you use this structure methods to do desired operations. Finally, you uninitialize the instance. Copying of the instances is prohibited. The private content of the structure is target-dependent. It's very likely, that the structure has no data fields. In such case its methods can be declared static. But you should not use them as such. This structure uses types and constants from the GenFile.h, this includes error codes.

init() initializes the instance. The error code is returned. If the object is successfully initialized, it must be uninitialized by the method exit().

exit() uninitialize the instance.

getFileType() returns the file type. The argument specifies a file or a directory. The return value is represented by the inner structure TypeResult. This structure has two fields: type and error. The type is the object type, the error is the error code.

getFileUpdateTime() returns the last modification time of the file or directory. The argument specifies a file or a directory. The return value is represented by the inner structure CmpTimeResult. This structure has two fields: time and error. The time is the requested time, the error is the error code.

createFile() creates the new empty file with the given name. The error code is returned.

deleteFile() deletes the file with the given name. The error code is returned.

createDir() creates the empty directory with the given name. The error code is returned.

deleteDir() deletes the directory with the given name. The error code is returned. The second argument is a recursion flag. If it is false, then the directory must be empty. Otherwise the directory is deleted with all its content, including nested subdirectories.

rename() renames or moves the file of directory. The error code is returned. The allow_overwrite flag allows overwriting the existing file.

remove() deletes ether the existing file or the existing empty directory. The error code is returned.

exec() is not a file system operation. It is an extra operation. This methods starts a new process. The dir is the working directory of the new process, the program is the executable file name, the arg is the command line.

pathOf() is another extra operation. This method builds a normalized full path of the file or directory. The buf is a storage buffer for the result. The resulting string is zero-terminated. The return value is represented by the inner structure PathOfResult. This structure has two fields: path and error. The path is the resulting path, the error is the error code.

DirCursor is the inner structure. This structure can be used to iterate over the content of a directory. Copying of the instances is prohibited. The private content of the structure is target-dependent.

file_name is the buffer for the file name. The only file part is stored here. The name is not zero-terminated.

len is the length of the file name.

type is the file type.

error contains the error code after method calls (except exit()).

init() initializes the instance. The field error will contain the error code. If the object is successfully initialized, it must be uninitialized by the method exit(). The iterator is set to the position "before the first".

exit() uninitializes the instance. No errors are returned.

next() moves to the next file (or directory) in the directory. The return value is true, if such file is found. The return value is false in case of error or if there is no more files. The field error will contain the error code. Fields file_name, len and type contain file information, if the file is found.