[*] UNIX: Use O_EXCL if available

[*] UNIX: Try to open readable dirs
This commit is contained in:
Reece Wilson 2023-11-11 13:25:05 +00:00
parent 151bb106fb
commit 9b74a623af

View File

@ -139,6 +139,8 @@ namespace Aurora::IO
this->bDirectIO = true;
}
int flags {};
switch (create.eMode)
{
case FS::EFileOpenMode::eRead:
@ -156,25 +158,56 @@ namespace Aurora::IO
if (create.bFailIfNonEmptyFile)
{
#if defined(O_EXCL)
flags = O_EXCL;
#else
if (AuFS::FileExists(pathex.c_str()))
{
SysPushErrorResourceExists("File {} already exists", create.path);
return false;
}
#endif
}
break;
}
};
flags |= create.eMode == FS::EFileOpenMode::eRead ? O_RDONLY : (O_RDWR | O_CREAT);
flags |= O_CLOEXEC;
flags |= this->bDirectIO ? O_DIRECT : 0;
iFileDescriptor = ::open(pathex.c_str(),
(create.eMode == FS::EFileOpenMode::eRead ? O_RDONLY : (O_RDWR | O_CREAT)) | O_CLOEXEC | (this->bDirectIO ? O_DIRECT : 0),
flags,
0664);
if (iFileDescriptor < 0)
{
SysPushErrorIO("Couldn't open file: {} ({}) {}", path, pathex, errno);
return false;
if (errno == EEXIST)
{
SysPushErrorResourceExists("File {} already exists", create.path);
return false;
}
if (errno == EISDIR)
{
flags &= ~(O_WRONLY | O_RDWR);
iFileDescriptor = ::open(pathex.c_str(),
flags,
0664);
}
if (errno == ENFILE)
{
SysPushErrorIOResourceFailure("low resources");
return false;
}
if (iFileDescriptor < 0)
{
SysPushErrorIO("Couldn't open file: {} ({}) {}", path, pathex, errno);
return false;
}
}
if (!FS::ApplyDumbAdvisoryLock(iFileDescriptor, create.eAdvisoryLevel))