CMPlatform/src/UNIX/FileAccess.cpp

70 lines
2.0 KiB
C++
Raw Normal View History

2019-08-23 10:54:57 +08:00
#include<hgl/io/FileAccess.h>
2020-01-23 22:06:04 +08:00
#include<hgl/log/LogInfo.h>
2019-08-23 10:54:57 +08:00
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<errno.h>
namespace hgl
{
namespace io
{
bool FileAccess::CreateTemp()
{
char template_filename[HGL_MAX_PATH]="/tmp/cm/XXXXXX";
fp=mkstemps(template_filename,HGL_MAX_PATH);
if(fp==-1)
return(false);
filename=template_filename;
2020-09-11 17:05:29 +08:00
mode=FileOpenMode::Create;
2019-08-23 10:54:57 +08:00
return(true);
}
int OpenFile(const os_char *fn,FileOpenMode fom)
{
int fp;
2020-09-11 17:05:29 +08:00
if(fom==FileOpenMode::Create )fp=hgl_open64(fn,O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);else
if(fom==FileOpenMode::CreateTrunc )fp=hgl_open64(fn,O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);else
if(fom==FileOpenMode::OnlyRead )fp=hgl_open64(fn,O_RDONLY );else
if(fom==FileOpenMode::OnlyWrite )fp=hgl_open64(fn,O_WRONLY );else
if(fom==FileOpenMode::ReadWrite )fp=hgl_open64(fn,O_RDWR );else
if(fom==FileOpenMode::Append )fp=hgl_open64(fn,O_APPEND );else
2019-08-23 10:54:57 +08:00
{
2020-09-11 17:05:29 +08:00
LOG_ERROR(OS_TEXT("UNIX,FileAccess,OpenFile(")+OSString(fn)+OS_TEXT(" mode error: "+OSString::valueOf(uint(fom))));
2019-08-23 10:54:57 +08:00
RETURN_ERROR(-1);
}
if(fp==-1)
{
2020-06-18 03:35:55 +08:00
LOG_ERROR(OS_TEXT("UNIX,FileAccess,OpenFile(")+OSString(fn)+OS_TEXT(") open return error: "+OSString::valueOf(errno)));
2019-08-23 10:54:57 +08:00
}
return fp;
}
void CloseFile(int fp)
{
close(fp);
}
int64 FileAccess::Read(int64 offset,void *buf,int64 size)
{
if(!CanRead())return(-1);
return hgl_pread64(fp,buf,size,offset);
}
int64 FileAccess::Write(int64 offset,const void *buf,int64 size)
{
if(!CanWrite())return(-1);
return hgl_pwrite64(fp,buf,size,offset);
}
}//namespace io
}//namespace hgl