Execute a filesystem command to create, delete or list a directory or to write out or read in a binary object to/from a file - as opposed to storing it in a repository.
| Return Type | Function name | Arguments |
|---|---|---|
| hzEcode | hdsExec::Filesys | (hzChain&,hzHttpEvent*,) |
Declared in file: hzDissemino.h
Defined in file : hdsExec.cpp
Function Logic:
Function body:
hzEcode hdsExec::Filesys (hzChain& errorReport)hzHttpEvent* pE,
{
// Execute a filesystem command to create, delete or list a directory or to write out or read in a binary object to/from a file - as opposed to storing it
// in a repository.
_hzfunc("hdsExec::Filesys") ;
FSTAT fs ; // File/directory status
hzAtom atom ; // For obtaing file content
hzChain content ; // The file content (usually file upload)
hzString resource ; // The directory/file to operate on
hzString m_Action ; // The command specifier (mkdir|rmdir|save|delete)
hzString m_Resource ; // Directory resource to operate on
hzString m_Content ; // Save command only - specifies data to be saved
hzEcode rc = E_OK ; // Return value
errorReport.Printf("Filesys action %s and resource %s\n", *m_Action, *m_Resource) ;
m_Action = m_pApp->m_ExecParams[m_FstParam] ;
m_Resource = m_pApp->m_ExecParams[m_FstParam] ;
m_Content = m_pApp->m_ExecParams[m_FstParam] ;
if (m_Action == "mkdir")
{
resource = m_pApp->ConvertText(m_Resource, pE) ;
rc = AssertDir(resource, 0777);
if (rc != E_OK)
errorReport.Printf("Could not assert dir %s error is %s\n", *resource, Err2Txt(rc)) ;
return rc ;
}
if (m_Action == "rmdir")
{
// Will work even if directory not empy
resource = m_pApp->ConvertText(m_Resource, pE) ;
rc = BlattDir(resource) ;
if (rc != E_OK)
errorReport.Printf("Could not blatt dir %s error is %s\n", *resource, Err2Txt(rc)) ;
return rc ;
}
if (m_Action == "save")
{
// Write out data to a file.
// Obtain file name
resource = m_pApp->ConvertText(m_Resource, pE) ;
// Obtain file content
rc = m_pApp->PcEntConv(atom, m_Content, pE) ;
content = atom.Chain() ;
if (!content.Size())
{
errorReport.Printf("No data to write to file %s\n", *resource) ;
return E_NODATA ;
}
ofstream os ; // Output stream
os.open(*resource) ;
if (os.fail())
{ errorReport.Printf("Could not open for write, file %s\n", *resource) ; return E_WRITEFAIL ; }
os << content ;
if (os.fail())
{ errorReport.Printf("Could not write, file %s\n", *resource) ; return E_WRITEFAIL ; }
os.close() ;
return E_OK ;
}
if (m_Action == "delete")
{
// Delete a file
resource = m_pApp->ConvertText(m_Resource, pE) ;
if (lstat(*resource, &fs) < 0)
{ errorReport.Printf("Action DELETE Failed: File %s not found\n", *resource) ; return E_NOTFOUND ; }
if (ISDIR(fs.st_mode))
{ errorReport.Printf("Action DELETE Failed: %s is a directory\n", *resource) ; return E_TYPE ; }
if (unlink(*resource) < 0)
{ errorReport.Printf("Action DELETE Failed: File %s could not be deleted\n", *resource) ; return E_WRITEFAIL ; }
return E_OK ;
}
// List entries in a directory
errorReport.Printf("Filesys command Failed: Action %s illegal\n", *m_Action) ;
return E_FORMAT ;
}