2022-04-04 07:53:34 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: FileStream.Unix.cpp
|
|
|
|
Date: 2021-6-12
|
|
|
|
Author: Reece
|
|
|
|
***/
|
2021-09-06 10:58:08 +00:00
|
|
|
#define _FILE_OFFSET_BITS 64
|
|
|
|
#define _LARGEFILE64_SOURCE
|
|
|
|
|
2021-09-30 14:57:41 +00:00
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2021-09-06 10:58:08 +00:00
|
|
|
#include "FS.hpp"
|
|
|
|
#include "FileStream.Generic.hpp"
|
2022-04-13 16:20:23 +00:00
|
|
|
#include "FileAdvisory.Unix.hpp"
|
2021-09-06 10:58:08 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2023-10-20 10:37:52 +00:00
|
|
|
#include <sys/ioctl.h>
|
2021-09-06 10:58:08 +00:00
|
|
|
|
|
|
|
#if !defined(_AURUNTIME_GENERICFILESTREAM)
|
|
|
|
|
|
|
|
namespace Aurora::IO::FS
|
|
|
|
{
|
|
|
|
static const AuUInt64 kFileCopyBlock = 0x4000; // 16KiB
|
|
|
|
|
|
|
|
static bool PosixLseek63(int fd, AuUInt64 offset, int whence, AuUInt64 *pOffset)
|
|
|
|
{
|
|
|
|
if (offset > std::numeric_limits<off_t>::max())
|
|
|
|
{
|
|
|
|
SysPushErrorIO("int overflow exploit?");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(AURORA_IS_LINUX_DERIVED)
|
|
|
|
auto ret = lseek64(fd, offset, whence);
|
|
|
|
#elif defined(AURORA_IS_64BIT)
|
|
|
|
static_assert(std::numeric_limits<off_t>::max() >= std::numeric_limits<AuInt64>::max(), "unsupported posix os");
|
|
|
|
auto ret = lseek(fd, offset, whence);
|
|
|
|
#else
|
|
|
|
#error accient 32-bit posix operating systems require 64-bit file awareness
|
2021-10-02 16:07:33 +00:00
|
|
|
#endif
|
2021-09-06 10:58:08 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-04-17 14:51:37 +00:00
|
|
|
//SysPushErrorIO("PosixLseek63 IO Error: %i", ret);
|
2021-09-06 10:58:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pOffset)
|
|
|
|
{
|
|
|
|
*pOffset = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static AuUInt64 PosixGetOffset(int fd)
|
|
|
|
{
|
|
|
|
AuUInt64 ret;
|
2023-09-18 06:11:34 +00:00
|
|
|
if (!PosixLseek63(fd, 0, SEEK_CUR, &ret))
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-04-17 14:51:37 +00:00
|
|
|
bool PosixSetOffset(int fd, AuUInt64 offset)
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
|
|
|
return PosixLseek63(fd, offset, SEEK_SET, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static AuUInt64 PosixGetLength(int fd)
|
|
|
|
{
|
|
|
|
AuUInt64 ret {}, old {};
|
|
|
|
bool status {};
|
|
|
|
|
2023-09-18 06:11:34 +00:00
|
|
|
if (!PosixLseek63(fd, 0, SEEK_CUR, &old))
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = PosixLseek63(fd, 0, SEEK_END, &ret);
|
|
|
|
status &= PosixLseek63(fd, old, SEEK_SET, nullptr);
|
|
|
|
return status ? ret : 0;
|
|
|
|
}
|
|
|
|
|
2022-04-17 14:51:37 +00:00
|
|
|
bool PosixRead(int fd, void *buf, AuUInt32 count, AuUInt32 *pRead)
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
|
|
|
auto ret = read(fd, buf, count);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-04-04 07:53:34 +00:00
|
|
|
SysPushErrorIO("PosixRead IO Error: %i", ret);
|
2021-09-06 10:58:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pRead)
|
|
|
|
{
|
|
|
|
*pRead = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-04-06 01:24:38 +00:00
|
|
|
|
2022-04-17 14:51:37 +00:00
|
|
|
bool PosixWrite(int fd, const void *buf, AuUInt32 count, AuUInt32 *pWritten)
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
|
|
|
auto ret = write(fd, buf, count);
|
2023-09-18 06:11:34 +00:00
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-04-04 07:53:34 +00:00
|
|
|
SysPushErrorIO("PosixWrite IO Error: %i", ret);
|
2021-09-06 10:58:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pWritten)
|
|
|
|
{
|
|
|
|
*pWritten = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PosixFileStream::~PosixFileStream()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
bool PosixFileStream::Init(AuSPtr<IIOHandle> pHandle)
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2022-04-04 07:53:34 +00:00
|
|
|
AuCtorCode_t code;
|
2023-08-11 15:51:42 +00:00
|
|
|
this->pHandle_ = pHandle;
|
2022-04-04 07:53:34 +00:00
|
|
|
return true;
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt64 PosixFileStream::GetOffset()
|
|
|
|
{
|
2022-04-04 07:53:34 +00:00
|
|
|
AU_LOCK_GUARD(this->spinlock_);
|
2023-08-11 15:51:42 +00:00
|
|
|
|
2023-09-16 00:13:41 +00:00
|
|
|
int fd {};
|
|
|
|
if (auto pHandle = this->pHandle_)
|
2023-08-11 15:51:42 +00:00
|
|
|
{
|
2023-09-16 00:13:41 +00:00
|
|
|
if (!pHandle->IsFile())
|
|
|
|
{
|
|
|
|
SysPushErrorIOResourceRejected();
|
|
|
|
return 0;
|
|
|
|
}
|
2023-08-11 15:51:42 +00:00
|
|
|
|
2023-09-16 00:13:41 +00:00
|
|
|
if (auto opt = pHandle->GetOSHandleSafe())
|
|
|
|
{
|
|
|
|
fd = opt.Value();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SysPushErrorInvalidFd();
|
|
|
|
return 0;
|
|
|
|
}
|
2023-08-11 15:51:42 +00:00
|
|
|
}
|
2023-09-16 00:13:41 +00:00
|
|
|
else
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
|
|
|
SysPushErrorUninitialized();
|
2023-09-16 00:13:41 +00:00
|
|
|
return 0;
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
return PosixGetOffset(fd);
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
2022-03-22 03:53:08 +00:00
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
bool PosixFileStream::SetOffset(AuUInt64 offset)
|
|
|
|
{
|
2022-04-04 07:53:34 +00:00
|
|
|
AU_LOCK_GUARD(this->spinlock_);
|
2023-08-11 15:51:42 +00:00
|
|
|
|
2023-09-16 00:13:41 +00:00
|
|
|
int fd {};
|
|
|
|
if (auto pHandle = this->pHandle_)
|
2023-08-11 15:51:42 +00:00
|
|
|
{
|
2023-09-16 00:13:41 +00:00
|
|
|
if (!pHandle->IsFile())
|
|
|
|
{
|
|
|
|
SysPushErrorIOResourceRejected();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto opt = pHandle->GetOSHandleSafe())
|
|
|
|
{
|
|
|
|
fd = opt.Value();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SysPushErrorInvalidFd();
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-11 15:51:42 +00:00
|
|
|
}
|
2023-09-16 00:13:41 +00:00
|
|
|
else
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
|
|
|
SysPushErrorUninitialized();
|
2023-08-11 15:51:42 +00:00
|
|
|
return false;
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
2023-09-16 00:13:41 +00:00
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
return PosixSetOffset(fd, offset);
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt64 PosixFileStream::GetLength()
|
|
|
|
{
|
2022-04-04 07:53:34 +00:00
|
|
|
AU_LOCK_GUARD(this->spinlock_);
|
2023-08-11 15:51:42 +00:00
|
|
|
|
2023-09-16 00:13:41 +00:00
|
|
|
int fd {};
|
|
|
|
if (auto pHandle = this->pHandle_)
|
2023-08-11 15:51:42 +00:00
|
|
|
{
|
2023-09-16 00:13:41 +00:00
|
|
|
if (!pHandle->IsFile())
|
|
|
|
{
|
|
|
|
// Intentionally suppressed (for the moment)
|
|
|
|
// I don't want to fill error stacks up under paths that can handle char-devs by reading until EoS
|
|
|
|
// If we do, we could hit an always-throw on AuErrorStack-catch
|
|
|
|
// Some users are completely unaware they can access/test the underlying handle
|
|
|
|
// Returning zero will do
|
|
|
|
return 0;
|
|
|
|
}
|
2023-08-11 15:51:42 +00:00
|
|
|
|
2023-09-16 00:13:41 +00:00
|
|
|
if (auto opt = pHandle->GetOSHandleSafe())
|
|
|
|
{
|
|
|
|
fd = opt.Value();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SysPushErrorInvalidFd();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
|
|
|
SysPushErrorUninitialized();
|
2023-09-16 00:13:41 +00:00
|
|
|
return 0;
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
return PosixGetLength(fd);
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 18:55:22 +00:00
|
|
|
bool PosixFileStream::Read(const Memory::MemoryViewStreamWrite ¶meters)
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2022-04-04 07:53:34 +00:00
|
|
|
AU_LOCK_GUARD(this->spinlock_);
|
2022-08-02 12:29:01 +00:00
|
|
|
|
|
|
|
parameters.outVariable = 0;
|
2023-09-18 06:11:34 +00:00
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
if (!this->pHandle_)
|
|
|
|
{
|
|
|
|
SysPushErrorUninitialized();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-15 13:41:49 +00:00
|
|
|
int fd = (int)this->pHandle_->GetOSReadHandleSafe().ValueOr((AuUInt)-1);
|
2023-08-11 15:51:42 +00:00
|
|
|
if (fd == -1)
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2023-08-15 13:41:49 +00:00
|
|
|
SysPushErrorIOResourceRejected();
|
2021-09-06 10:58:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-10-20 10:37:52 +00:00
|
|
|
AuUInt uOffset {};
|
|
|
|
AuUInt64 uLength = parameters.length;
|
|
|
|
|
|
|
|
if (this->pHandle_->IsPipe())
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2023-10-20 10:37:52 +00:00
|
|
|
#if defined(FIONREAD)
|
|
|
|
int available {};
|
|
|
|
if (::ioctl(fd, FIONREAD, &available) < 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2023-10-20 10:37:52 +00:00
|
|
|
uLength = AuMin(uLength, available);
|
|
|
|
if (!uLength)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
while (uLength)
|
|
|
|
{
|
|
|
|
AuUInt32 uRead;
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2023-10-20 10:37:52 +00:00
|
|
|
if (!PosixRead(fd,
|
|
|
|
&reinterpret_cast<char *>(parameters.ptr)[uOffset],
|
|
|
|
AuMin(kFileCopyBlock, uLength),
|
|
|
|
&uRead))
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2023-10-20 10:37:52 +00:00
|
|
|
SysPushErrorNested("File Error: {}", this->path_);
|
2021-09-06 10:58:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-10-20 10:37:52 +00:00
|
|
|
if (uRead == 0)
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-10-20 10:37:52 +00:00
|
|
|
uOffset += uRead;
|
|
|
|
uLength -= uRead;
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
2023-10-20 10:37:52 +00:00
|
|
|
parameters.outVariable = uOffset;
|
2021-09-14 23:56:26 +00:00
|
|
|
return true;
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 18:55:22 +00:00
|
|
|
bool PosixFileStream::Write(const Memory::MemoryViewStreamRead ¶meters)
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2022-04-04 07:53:34 +00:00
|
|
|
AU_LOCK_GUARD(this->spinlock_);
|
|
|
|
|
2023-10-20 10:37:52 +00:00
|
|
|
parameters.outVariable = 0;
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
if (!this->pHandle_)
|
|
|
|
{
|
|
|
|
SysPushErrorUninitialized();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-09-16 00:13:41 +00:00
|
|
|
int fd = this->pHandle_->GetOSWriteHandleSafe().ValueOr((AuUInt)-1);
|
2023-08-11 15:51:42 +00:00
|
|
|
if (fd == -1)
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
|
|
|
SysPushErrorUninitialized();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-04 07:53:34 +00:00
|
|
|
AuUInt length = parameters.length;
|
2021-09-14 23:56:26 +00:00
|
|
|
AuUInt offset {0};
|
2021-09-06 10:58:08 +00:00
|
|
|
while (length)
|
|
|
|
{
|
|
|
|
AuUInt32 written;
|
|
|
|
|
2022-04-04 07:53:34 +00:00
|
|
|
int blockSize = AuMin(AuUInt(kFileCopyBlock), length);
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
if (!PosixWrite(fd, &reinterpret_cast<const char *>(parameters.ptr)[offset], blockSize, &written))
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2023-08-11 15:51:42 +00:00
|
|
|
SysPushErrorNested("File Error: {}", this->pHandle_->GetPath());
|
2021-09-14 23:56:26 +00:00
|
|
|
return false;
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (written != blockSize)
|
|
|
|
{
|
2021-10-02 16:07:33 +00:00
|
|
|
SysPushErrorNested("File Error: {}", path_);
|
2021-09-14 23:56:26 +00:00
|
|
|
break;
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
offset += written;
|
|
|
|
length -= written;
|
|
|
|
}
|
|
|
|
|
2021-09-14 23:56:26 +00:00
|
|
|
if (!offset)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
parameters.outVariable = offset;
|
|
|
|
return true;
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PosixFileStream::Close()
|
|
|
|
{
|
2023-08-11 15:51:42 +00:00
|
|
|
auto pHandle = this->pHandle_;
|
|
|
|
AuResetMember(this->pHandle_);
|
2022-04-04 07:53:34 +00:00
|
|
|
|
|
|
|
if (AuExchange(this->bMadeTemporary, false))
|
|
|
|
{
|
2023-08-11 15:51:42 +00:00
|
|
|
if (pHandle)
|
|
|
|
{
|
|
|
|
::unlink(pHandle->GetPath().c_str());
|
|
|
|
}
|
2022-04-04 07:53:34 +00:00
|
|
|
}
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PosixFileStream::Flush()
|
|
|
|
{
|
2023-09-16 00:13:41 +00:00
|
|
|
if (!this->pHandle_)
|
2023-08-11 15:51:42 +00:00
|
|
|
{
|
|
|
|
SysPushErrorUninitialized();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-16 00:13:41 +00:00
|
|
|
int fd = this->pHandle_->GetOSWriteHandleSafe().ValueOr((AuUInt)-1);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
SysPushErrorIOResourceRejected();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
::fsync(fd);
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
2022-04-04 07:53:34 +00:00
|
|
|
|
2022-03-22 03:53:08 +00:00
|
|
|
void PosixFileStream::WriteEoS()
|
|
|
|
{
|
2023-09-16 00:13:41 +00:00
|
|
|
if (!this->pHandle_)
|
2023-08-11 15:51:42 +00:00
|
|
|
{
|
|
|
|
SysPushErrorUninitialized();
|
|
|
|
return;
|
|
|
|
}
|
2023-09-16 00:13:41 +00:00
|
|
|
|
|
|
|
int fd = this->pHandle_->GetOSWriteHandleSafe().ValueOr((AuUInt)-1);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
SysPushErrorIOResourceRejected();
|
|
|
|
return;
|
|
|
|
}
|
2023-08-11 15:51:42 +00:00
|
|
|
|
2023-09-18 06:11:34 +00:00
|
|
|
::ftruncate(fd, PosixGetOffset(fd));
|
2022-04-04 07:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PosixFileStream::MakeTemporary()
|
|
|
|
{
|
|
|
|
this->bMadeTemporary = true;
|
2022-03-22 03:53:08 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
int PosixFileStream::GetUnixHandle()
|
2022-08-10 09:50:49 +00:00
|
|
|
{
|
2023-08-11 15:51:42 +00:00
|
|
|
return this->pHandle_ ?
|
|
|
|
(int)this->pHandle_->GetOSWriteHandleSafe().ValueOr(this->pHandle_->GetOSReadHandleSafe().ValueOr((AuUInt)-1)) :
|
|
|
|
-1;
|
2022-08-10 09:50:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
AuSPtr<IIOHandle> PosixFileStream::GetHandle()
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2023-08-11 15:51:42 +00:00
|
|
|
return this->pHandle_;
|
|
|
|
}
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
AUKN_SYM AuSPtr<IFileStream> OpenBlockingFileStreamFromHandle(AuSPtr<IIOHandle> pIOHandle)
|
|
|
|
{
|
|
|
|
auto pStream = AuMakeShared<PosixFileStream>();
|
|
|
|
if (!pStream)
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2023-08-11 15:51:42 +00:00
|
|
|
SysPushErrorMemory();
|
|
|
|
return nullptr;
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
pStream->Init(pIOHandle);
|
|
|
|
return pStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
static IFileStream *OpenNewEx(const AuString &path,
|
|
|
|
EFileOpenMode openMode,
|
|
|
|
EFileAdvisoryLockLevel lock,
|
|
|
|
bool bCheck)
|
|
|
|
{
|
|
|
|
try
|
2023-02-04 19:43:01 +00:00
|
|
|
{
|
2023-08-11 15:51:42 +00:00
|
|
|
auto pHandle = AuIO::IOHandleShared();
|
|
|
|
if (!pHandle)
|
2023-02-04 19:43:01 +00:00
|
|
|
{
|
2023-08-11 15:51:42 +00:00
|
|
|
SysPushErrorMemory();
|
|
|
|
return nullptr;
|
2023-02-04 19:43:01 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
AuIO::IIOHandle::HandleCreate createhandle(path);
|
|
|
|
createhandle.eAdvisoryLevel = lock;
|
|
|
|
createhandle.eMode = openMode;
|
|
|
|
createhandle.bFailIfNonEmptyFile = bCheck;
|
|
|
|
createhandle.bDirectIOMode = false;
|
|
|
|
createhandle.bAsyncHandle = false;
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
if (!pHandle->InitFromPath(createhandle))
|
|
|
|
{
|
|
|
|
SysPushErrorNested();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2022-04-13 16:20:23 +00:00
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
auto pStream = _new PosixFileStream();
|
|
|
|
if (!pStream)
|
2023-02-04 19:43:01 +00:00
|
|
|
{
|
2023-08-11 15:51:42 +00:00
|
|
|
SysPushErrorMemory();
|
|
|
|
return nullptr;
|
2023-02-04 19:43:01 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
pStream->Init(pHandle);
|
|
|
|
return pStream;
|
2022-04-04 07:53:34 +00:00
|
|
|
}
|
2023-08-11 15:51:42 +00:00
|
|
|
catch (...)
|
2022-04-04 07:53:34 +00:00
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-04 19:43:01 +00:00
|
|
|
AUKN_SYM IFileStream *CreateNew(const AuString &path)
|
|
|
|
{
|
|
|
|
return OpenNewEx(path, EFileOpenMode::eWrite, EFileAdvisoryLockLevel::eBlockReadWrite, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM void CreateRelease(IFileStream *that)
|
|
|
|
{
|
|
|
|
AuSafeDelete<PosixFileStream *>(that);
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM IFileStream *OpenNew(const AuString &path, EFileOpenMode openMode, EFileAdvisoryLockLevel lock)
|
|
|
|
{
|
|
|
|
return OpenNewEx(path, openMode, lock, false);
|
|
|
|
}
|
|
|
|
|
2022-04-06 01:24:38 +00:00
|
|
|
AUKN_SYM void OpenRelease(IFileStream *that)
|
|
|
|
{
|
|
|
|
AuSafeDelete<PosixFileStream *>(that);
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM IFileStream *OpenReadNew(const AuString &path, EFileAdvisoryLockLevel level)
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2023-02-04 19:43:01 +00:00
|
|
|
return OpenNewEx(path, EFileOpenMode::eRead, level, false);
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 01:24:38 +00:00
|
|
|
AUKN_SYM void OpenReadRelease(IFileStream * that)
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2022-03-22 03:53:08 +00:00
|
|
|
AuSafeDelete<PosixFileStream *>(that);
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 01:24:38 +00:00
|
|
|
AUKN_SYM IFileStream *OpenWriteNew(const AuString &path, EFileAdvisoryLockLevel level)
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2023-02-04 19:43:01 +00:00
|
|
|
return OpenNewEx(path, EFileOpenMode::eWrite, level, false);
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
2022-04-06 01:24:38 +00:00
|
|
|
|
|
|
|
AUKN_SYM void OpenWriteRelease(IFileStream *that)
|
2021-09-06 10:58:08 +00:00
|
|
|
{
|
2022-03-22 03:53:08 +00:00
|
|
|
AuSafeDelete<PosixFileStream *>(that);
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-02 16:07:33 +00:00
|
|
|
#endif
|