AsyncFile Clients

Files CCore/inc/AsyncFile.h CCore/src/AsyncFile.cpp

Files CCore/inc/AsyncFileToMem.h CCore/src/AsyncFileToMem.cpp

There are several "client" classes to work with files and performes file system operations on async file and file system devices.

AsyncFile

AsyncFile can perform read/write operation on a file, using an async file device. An AsyncFile object has several parameters, affecting operations.

timeout is used by the open(), getWritePacket() methods.

final_timeout is used to complete all pending operations. By default it equals 3*timeout.

max_packets is the maximum number of executed in parallel operations.


class AsyncFile : public Funchor_nocopy
 {
   ....

  public: 
   
   // constructors
  
   explicit AsyncFile(MSec timeout=DefaultTimeout,
                      ulen max_packets=DefaultMaxPackets);
   
   explicit AsyncFile(StrLen file_name,
                      FileOpenFlags oflags,
                      MSec timeout=DefaultTimeout,
                      ulen max_packets=DefaultMaxPackets);

   AsyncFile(StrLen dev_name,StrLen dev_file_name,
             FileOpenFlags oflags,
             MSec timeout=DefaultTimeout,
             ulen max_packets=DefaultMaxPackets);

   ~AsyncFile();
   
   bool isOpened() const { return is_opened; }
   
   void setFinalTimeout(MSec t) { final_timeout=t; }
   
   // open/close
   
   FilePosType open(StrLen file_name,FileOpenFlags oflags); // return file_len
   
   FilePosType open(StrLen dev_name,StrLen dev_file_name,FileOpenFlags oflags); // return file_len

   void soft_close(FileMultiError &errout);
   
   void close();
   
   void preserveFile() { preserve_file=true; }
   
   void setLargeWriteLen(ulen large_write_len);
   
   void wait() { pset.wait(timeout); }
   
   void wait_final() { pset.wait(final_timeout); }
   
   void wait(MSec timeout) { pset.wait(timeout); }
   
   void cancel_and_wait() { pset.cancel_and_wait(); }
   
   // write
   
   struct WritePacket
    {
     Packet<uint8> packet;
     PtrLen<uint8> buf;
     
     WritePacket(Packet<uint8> packet_,PtrLen<uint8> buf_) : packet(packet_),buf(buf_) {}
    };
   
   WritePacket getWritePacket(); // non-null, not empty
   
   void write(FilePosType off,ulen len,Packet<uint8> packet) noexcept;
   
   // read
   
   void read_all(FilePosType off,uint8 *buf,ulen len);
   
   ulen getMaxReadLen() const { return max_read_len; }
   
   void read(FilePosType off,ulen len,PacketFunction complete_function); 
 };

The first constructor creates a closed object with the given parameters. The second and third constructors open the file.

isOpened() returns true, if the file is opened.

setFinalTimeout() sets the final_timeout to the given value. By default, constructors set this value to the triple of timeout. This extended timeout is used to wait for the completion of all pending operations in the read_all() and close() methods.

open() opens the file, if it was not opened. An exception is thrown in case of error. timeout is used for this operation. The return value is the file length. The file name can be given as the single argument "dev:path" or as two arguments.

soft_close() closes the file. Errors are reported using a FileMultiError object.

close() closes the file. An exception is thrown in case of error. Both soft_close() and close() methods takes a time to complete, because they waits for completion of pending operations first, using the final_timeout.

preserveFile() cancels the flag Open_AutoDelete.

wait() functions wait for pending operations completion, then cancel remaining operations and wait for completion.

cancel_and_wait() cancels all pending operations and waits for completion.

setLargeWriteLen() sets the large_write_len parameters. This value is used to allocate a larger data buffer for writing if the target device accepts packets with a large data length. The value is set, once the file is opened according the target device write format.

getWritePacket() allocates a packet for a write operation. It returns a data structure with two fields: packet is the packet and buf is a data range to be filled with data. This methods uses timeout to wait until a packet becomes available. An exception is thrown in case of error.

write() starts a write operation. The packet must be allocated by the getWritePacket() method. The len is the length of data, placed into the data buffer, returned by the getWritePacket(). off is the file offset where data will be written.

read_all() reads the file. This method waits first for all pending operation completion, using the final_timeout. This method starts a number of read operations to read the required data. It may continue a long time.

getMaxReadLen() returns the maximum read length for a single operation.

read() starts a single read operation. len must not exceed the getMaxReadLen(). The data can be retrieved in the complete_function:


void complete_function(PacketHeader *packet_)
 {
  Packet<uint8,Sys::AsyncFile::ReadBufExt> packet=packet_;
   
  Sys::AsyncFile::ReadBufExt *ext=packet.getExt();
   
  // FileError      ext->error -- error code
  // const uint8 *  ext->data -- data
  // ulen           ext->len -- data length

  ....   
   
  packet.popExt().complete();
 }

AsyncFileSystem

This class is built upon the Sys::AsyncFileSystem and can perform file system operations on an async file system device.


class AsyncFileSystem : public Funchor_nocopy
 {
   ....

  public:
   
   // constructors
  
   explicit AsyncFileSystem(StrLen dev_name,MSec timeout=DefaultTimeout,ulen max_packets=DefaultMaxPackets);
   
   ~AsyncFileSystem();
   
   // operations
   
   FileType getFileType(StrLen path);

   CmpFileTimeType getFileUpdateTime(StrLen path);
  
   FilePosType getFileList(StrLen dir_name,AsyncFile &file); // return file length
  
   void createFile(StrLen file_name);
  
   void deleteFile(StrLen file_name);
  
   void createDir(StrLen dir_name);
  
   void deleteDir(StrLen dir_name,bool recursive);
  
   void rename(StrLen old_path,StrLen new_path,bool allow_overwrite);
  
   void remove(StrLen path);
  
   void exec(StrLen dir,StrLen program,StrLen arg);
  
   void exec2(StrLen dir,StrLen program,AsyncFile &file);
   
   // static
   
   static FileType GetFileType(StrLen path,MSec timeout=DefaultTimeout);
  
   static CmpFileTimeType GetFileUpdateTime(StrLen path,MSec timeout=DefaultTimeout);

   static void CreateFile(StrLen file_name,MSec timeout=DefaultTimeout);
  
   static void DeleteFile(StrLen file_name,MSec timeout=DefaultTimeout);
  
   static void CreateDir(StrLen dir_name,MSec timeout=DefaultTimeout);
  
   static void DeleteDir(StrLen dir_name,bool recursive,MSec timeout=DefaultTimeout);
  
   static void Rename(StrLen old_path,StrLen new_path,bool allow_overwrite,MSec timeout=DefaultTimeout);
  
   static void Remove(StrLen path,MSec timeout=DefaultTimeout);
  
   static void Exec(StrLen dir,StrLen program,StrLen arg,MSec timeout=DefaultTimeout);
 };

Constructor takes the following arguments: dev_name is the device name, timeout is a total timeout for all class methods. The last constructor argument should have its default value.

Non-static class methods performs the standard list of file system operations. An exception is thrown in case of error. getFileList() and exec2() uses an external AsyncFile object to open a file.

Static methods do the following: they split the file name (or path), create a temporary AsyncFileSystem object and use it to perform the operation. If several file names are involved then its devices must be the same.

AsyncFileToMem

This class loads the given file from an async file device to the memory. It is derived from the class ToMemBase.


class AsyncFileToMem : public ToMemBase
 {
  public:

   explicit AsyncFileToMem(StrLen file_name,
                           ulen max_len=MaxULen,
                           MSec timeout=DefaultTimeout,
                           ulen max_packets=DefaultMaxPackets);

   AsyncFileToMem(StrLen dev_name,
                  StrLen dev_file_name,
                  ulen max_len=MaxULen,
                  MSec timeout=DefaultTimeout,
                  ulen max_packets=DefaultMaxPackets);

   ~AsyncFileToMem();
   
   // std move

   AsyncFileToMem(AsyncFileToMem &&obj) noexcept = default ;

   AsyncFileToMem & operator = (AsyncFileToMem &&obj) noexcept = default ;

   // swap/move objects
   
   void objSwap(AsyncFileToMem &obj);
   
   explicit AsyncFileToMem(ToMoveCtor<AsyncFileToMem> obj);
 };

The first constructor takes four arguments. It throws an exception in case of error. The constructor execution may take a time. The first argument file_name is the file name. The name is started with the device object name. The second argument max_len limits the image length, if the file is greater than this limit, an exception is thrown. Other arguments are propagated to the class AsyncFile, which is used to do the job.

The second constructor takes the device name and the file name as first two arguments.

AsyncFileToMem is std movable. The original object is nullified during the move.

AsyncFileToMem is swappable and movable.

PartAsyncFileToMem

PartAsyncFileToMem can be used to load parts of a file into a memory buffer. This class loads file parts sequentially.


class PartAsyncFileToMem : NoCopy
 {
   AsyncFile file;
   SimpleArray<uint8> buf;
   FilePosType off;
   FilePosType file_len;

  public:

   static const ulen DefaultBufLen = 64_KByte ;

   explicit PartAsyncFileToMem(StrLen file_name,ulen buf_len=DefaultBufLen,MSec timeout=DefaultTimeout,ulen max_packets=DefaultMaxPackets);

   PartAsyncFileToMem(StrLen dev_name,StrLen dev_file_name,ulen buf_len=DefaultBufLen,MSec timeout=DefaultTimeout,ulen max_packets=DefaultMaxPackets);

   ~PartAsyncFileToMem();

   FilePosType getFileLen() const { return file_len; }

   FilePosType getCurPos() const { return off; }

   FilePosType getRestLen() const { return file_len-off; }

   bool more() const { return off<file_len; }

   PtrLen<const uint8> read();
 };

Constructors arguments have the same meaning as the arguments of the AsyncFile constructors.

getFileLen() returns the file length.

getCurPos() returns the current position.

getRestLen() returns the length of the file part after the current position.

more() returns true, iff there is a room after the current position.

read() reads the next file part from the current position up to the end of the buffer or up to the end of the file. The part is stored in the internal buffer and the constant range of bytes is returned.

AsyncFileListToMem

This class loads the content of the given directory on an async file device to the memory. Then it can iterate through the list.


class AsyncFileListToMem : public ToMemBase
 {
   ....

  public:

   explicit AsyncFileListToMem(StrLen path,
                               ulen max_len=MaxULen,
                               MSec timeout=DefaultTimeout,
                               ulen max_packets=DefaultMaxPackets);

   AsyncFileListToMem(AsyncFileSystem &fs,
                      StrLen dev_path,
                      ulen max_len=MaxULen,
                      MSec timeout=DefaultTimeout,
                      ulen max_packets=DefaultMaxPackets);
   
   ~AsyncFileListToMem();
   
   bool next();
   
   StrLen getFileName() const;
   
   FileType getFileType() const;

   template <class FuncInit>
   void apply(FuncInit func_init); // func(StrLen file_name,FileType file_type)
 };

Constructor arguments are similar to the AsyncFileToMem class. The second constructor uses existing AsyncFileSystem object.

next() moves to the next file in the list. Initially the current file is "before the first".

getFileName() returns the name of the current file.

getFileType() is the type of the current file.

apply() applies the given functor on the file name list.