HFS

"Server must do the best, client must assume the worst."

Files CCore/inc/net/HFSBase.h CCore/src/net/HFSBase.cpp

Files CCore/inc/net/HFSClientDevice.h CCore/src/net/HFSClientDevice.cpp

Files CCore/inc/net/HFSFileSystemDevice.h CCore/src/net/HFSFileSystemDevice.cpp

HFS is a host file system PTP service. Its service id is 2. The service has 14 functions with ids 1-14. These functions perform file input-output operations and file system operations according General file operations. To identify an opened file the FileId is used.


const ServiceIdType ServiceId = 2 ;
   
const FunctionIdType FunctionId_Open        =  1 ;
const FunctionIdType FunctionId_Read        =  2 ;
const FunctionIdType FunctionId_Write       =  3 ;
const FunctionIdType FunctionId_Close       =  4 ;

const FunctionIdType FunctionId_GetFileType =  5 ;
const FunctionIdType FunctionId_GetFileList =  6 ;
const FunctionIdType FunctionId_CreateFile  =  7 ;
const FunctionIdType FunctionId_DeleteFile  =  8 ;
const FunctionIdType FunctionId_CreateDir   =  9 ;
const FunctionIdType FunctionId_DeleteDir   = 10 ;
const FunctionIdType FunctionId_Rename      = 11 ;
const FunctionIdType FunctionId_Remove      = 12 ;
const FunctionIdType FunctionId_Exec        = 13 ;
const FunctionIdType FunctionId_Exec2       = 14 ;
const FunctionIdType FunctionId_GetFileUpdateTime = 15 ;

Service data types and constants are:


using FileLenType = uint64 ; // == GenFile def

using CmpFileTimeType = uint64 ; // == GenFile def

const unsigned MaxPathLen      =  512 ; // == GenFile def

const FlagType OpenFlag_Read       = 0x01 ; // == GenFile def
const FlagType OpenFlag_Write      = 0x02 ; // == GenFile def
const FlagType OpenFlag_Create     = 0x10 ; // == GenFile def
const FlagType OpenFlag_Erase      = 0x20 ; // == GenFile def
const FlagType OpenFlag_New        = 0x40 ; // == GenFile def
const FlagType OpenFlag_AutoDelete = 0x80 ; // == GenFile def
   
const FlagType FileType_none = 0 ; // == GenFile def
const FlagType FileType_file = 1 ; // == GenFile def
const FlagType FileType_dir  = 2 ; // == GenFile def
   
struct Path
 {
  LenType len : len<=MaxPathLen ;
  uint8 path[len];
 };
    
struct FileId
 {
  uint32 slot;
  uint64 number;
  uint64 clock;
 }; 
 
const unsigned DeltaReadLen    =   24 ;
const unsigned MaxReadDataLen  = 1416 ; // MaxInfoLen-DeltaReadLen
const unsigned DeltaWriteLen   =   40 ;
const unsigned MaxWriteDataLen = 1400 ; // MaxInfoLen-DeltaWriteLen

const ErrorIdType ErrorBase = 100 ; // GenFile error code + ErrorBase

Mostly they have the same values and meaning as General file data and constants.

Function Open has the following input/output:


struct OpenInput
 {
  FlagType open_flags;
  Path path;
 };
    
struct OpenOutput
 {
  FileId file_id;
  FileLenType file_len;
 }; 

This function opens the given file with the given set of open flags. The file id is returned with the file length.

Function Read has the following input/output:


struct ReadInput
 {
  FileId file_id;
  FileLenType off;
  LenType len;
 };
   
struct ReadOutput
 {
  FileLenType off;
  LenType len : len<=MaxReadDataLen ;
  uint8 data[len];
 };

This function reads the file data from the specified file position. Server may ether truncate or completely fail the operation if the required range continues past the end of the file. The output data length may also be less than the input length, if the input length is too high (exceeds the packet length based limit).

Function Write has the following input/output:


struct WriteInput
 {
  FileId file_id;
  FileLenType off;
  LenType len : len<=MaxWriteDataLen ;
  uint8 data[len];
 };
    
struct WriteOutput
 {
  FileLenType file_len;
 }; 

This function writes the data to the file at the specified file position. The new file length is returned.

Function Close has the following input/output:


struct CloseInput
 {
  BoolType preserve_file;
  FileId file_id;
 };
    
struct CloseOutput
 {
 }; 

This function closes the opened file. The flag preserve_file cancels the open flag OpenFlag_AutoDelete.

Function GetFileType has the following input/output:


struct GetFileTypeInput
 {
  Path path;
 };
    
struct GetFileTypeOutput
 {
  FlagType type;
 }; 

This function returns the file type.

Function GetFileList has the following input/output:


struct GetFileListInput
 {
  Path path;
 };
    
struct GetFileListOutput
 {
  FileId file_id;
  FileLenType file_len;
 }; 

This function opens a virtual temporary file with the file list of the given directory. The file id and the file length is returned. Then usual file operations can be used. File is opened for reading. The file has number of lines, each line starts with a file or directory name, then space, then 'f' for a file and 'd' for a directory. End of line symbol is '\n'.

Function CreateFile has the following input/output:


struct CreateFileInput
 {
  Path path;
 };
    
struct CreateFileOutput
 {
 }; 

This function creates the empty file with the given name.

Function DeleteFile has the following input/output:


struct DeleteFileInput
 {
  Path path;
 };
    
struct DeleteFileOutput
 {
 }; 

This function deletes the given file.

Function CreateDir has the following input/output:


struct CreateDirInput
 {
  Path path;
 };
    
struct CreateDirOutput
 {
 }; 

This function creates the directory.

Function DeleteDir has the following input/output:


struct DeleteDirInput
 {
  BoolType recursive;
  Path path;
 };
    
struct DeleteDirOutput
 {
 }; 

This function deletes the directory (recursive or not).

Function Rename has the following input/output:


struct RenameInput
 {
  BoolType allow_overwrite;
  Path old_path;
  Path new_path;
 };
    
struct RenameOutput
 {
 }; 

This function renames files or directories.

Function Remove has the following input/output:


struct RemoveInput
 {
  Path path;
 };
    
struct RemoveOutput
 {
 }; 

This function removes the file or directory.

Function Exec has the following input/output:


struct ExecInput
 {
  Path dir;
  Path program;
  Path arg;
 };

struct ExecOutput
 {
 };

This function executes a program at the specified working directory with the given arguments.

Function Exec2 has the following input/output:


struct Exec2Input
 {
  Path dir;
  Path program;
 };

struct Exec2Output
 {
  FileId file_id;
 };

This function is to execute a program at the specified working directory with the given arguments. To setup arguments a temporary virtual empty file is created. The file id is returned. Then usual file operations can be used. File is opened for reading and writing. When the file is closed if the preserve_file flag is set, then the program is started.

Function GetFileUpdateTime has the following input/output:


struct GetFileUpdateTimeInput
 {
  Path path;
 };

struct GetFileUpdateTimeOutput
 {
  CmpFileTimeType file_time;
 };

The file txt/cpp/HFS.txt.cpp contains this service definition.

The file net/HFSBase.h contains this service definition in C++ in the namespace HFS.

ClientDevice

This class is withing the namespace HFS. It is a HFS client class. You probably should not use its methods directly. The best way is to create a FileSystemDevice object an register it, then use AsyncFiles classes.


class ClientDevice : public ObjBase
 {
   ....

  public: 
  
   explicit ClientDevice(StrLen ptp_dev_name);
   
   virtual ~ClientDevice();
   
   PTP::ClientDevice * getPTPDevice() const;
   
   // open
   
   PacketFormat getOpenFormat() const;
   
   void open(Packet<uint8,OpenExt> packet,FlagType open_flags);
   
   void open(Packet<uint8,OpenExt> packet,StrLen file_name,FlagType open_flags);
   
   // read
   
   ulen getMaxReadLen() const; // always > 0 , <= MaxReadLen
   
   void read(Packet<uint8,ReadExt> packet,const FileId &file_id,FileLenType off,ulen len);
   
   // write
   
   PacketFormat getWriteFormat() const; // always max_data <= MaxWriteLen
   
   void write(Packet<uint8,WriteExt> packet,const FileId &file_id,FileLenType off);
   
   // close
   
   void close(Packet<uint8,CloseExt> packet,const FileId &file_id,bool preserve_file=false);
   
   // getFileType
   
   void getFileType(Packet<uint8,GetFileTypeExt> packet,StrLen path);
   
   // getFileUpdateTime

   void getFileUpdateTime(Packet<uint8,GetFileUpdateTimeExt> packet,StrLen path);

   // getFileList
   
   void getFileList(Packet<uint8,GetFileListExt> packet,StrLen dir_name);
   
   // createFile
   
   void createFile(Packet<uint8,CreateFileExt> packet,StrLen file_name);
   
   // deleteFile
   
   void deleteFile(Packet<uint8,DeleteFileExt> packet,StrLen file_name);
   
   // createDir
   
   void createDir(Packet<uint8,CreateDirExt> packet,StrLen dir_name);
   
   // deleteDir
   
   void deleteDir(Packet<uint8,DeleteDirExt> packet,StrLen dir_name,bool recursive);
   
   // rename
   
   void rename(Packet<uint8,RenameExt> packet,StrLen old_path,StrLen new_path,bool allow_overwrite);
   
   // remove
   
   void remove(Packet<uint8,RemoveExt> packet,StrLen path);
   
   // exec
   
   void exec(Packet<uint8,ExecExt> packet,StrLen dir,StrLen program,StrLen arg);
   
   // exec2
   
   void exec2(Packet<uint8,Exec2Ext> packet,StrLen dir,StrLen program);
 };

Constructor takes a PTP client device object name as the argument.

getPTPDevice() returns a pointer to the PTP ClientDevice being used.

Each function is performed with a packet with the proper Ext extension.

FileSystemDevice

This class is located in the namespace HFS. It implements async file and file system interfaces over a PTP HFS service.


class FileSystemDevice : public ObjBase , public Sys::AsyncFileDevice , 
                                          public Sys::AsyncFileSystemDevice
 {
   ....

  public:
  
   explicit FileSystemDevice(StrLen hfs_dev_name);
   
   virtual ~FileSystemDevice();
   
   // Sys::AsyncFileDevice
   
   ....

   // Sys::AsyncFileSystemDevice
   
   ....
 };

Constructor takes an HFS client device object name as the argument.

Below is an example for an HCore target:


Net::AsyncUDPEndpointDevice udp(Net::PTPClientUDPort,Net::UDPoint(127,0,0,1,Net::PTPServerUDPort));
  
ObjMaster udp_master(udp,"udp");
  
Net::PTP::ClientDevice ptp("udp");
  
ObjMaster ptp_master(ptp,"ptp");
  
Net::AsyncUDPEndpointDevice::StartStop start_stop(udp);
  
ptp.support_guarded();

Net::HFS::ClientDevice hfs("ptp");

ObjMaster hfs_master(hfs,"hfs"); 

Net::HFS::FileSystemDevice fs("hfs");

ObjMaster fs_master(fs,"host");

....

PrintAsyncFile out("host:/file_name.txt");

....