Fix fs::unread (Linux)

Detach from fs::lock
Handle existing file case (unlink it).
This commit is contained in:
Nekotekina 2021-02-02 00:57:51 +03:00
parent 2005c89baa
commit 78bad361ab

View File

@ -1099,13 +1099,22 @@ fs::file::file(const std::string& path, bs_t<open_mode> mode)
if (mode & fs::append) flags |= O_APPEND; if (mode & fs::append) flags |= O_APPEND;
if (mode & fs::create) flags |= O_CREAT; if (mode & fs::create) flags |= O_CREAT;
if (mode & fs::trunc && !(mode & (fs::lock + fs::unread))) flags |= O_TRUNC; if (mode & fs::trunc && !(mode & fs::lock)) flags |= O_TRUNC;
if (mode & fs::excl) flags |= O_EXCL; if (mode & fs::excl) flags |= O_EXCL;
int perm = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; int perm = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
if (mode & fs::write && mode & fs::unread) if (mode & fs::write && mode & fs::unread)
{ {
if (!(mode & (fs::excl + fs::lock)) && mode & fs::trunc)
{
// Alternative to truncation for "unread" flag (TODO)
if (mode & fs::create)
{
::unlink(path.c_str());
}
}
perm = 0; perm = 0;
} }
@ -1117,14 +1126,14 @@ fs::file::file(const std::string& path, bs_t<open_mode> mode)
return; return;
} }
if (mode & fs::write && mode & (fs::lock + fs::unread) && ::flock(fd, LOCK_EX | LOCK_NB) != 0) if (mode & fs::write && mode & fs::lock && ::flock(fd, LOCK_EX | LOCK_NB) != 0)
{ {
g_tls_error = errno == EWOULDBLOCK ? fs::error::acces : to_error(errno); g_tls_error = errno == EWOULDBLOCK ? fs::error::acces : to_error(errno);
::close(fd); ::close(fd);
return; return;
} }
if (mode & fs::trunc && mode & (fs::lock + fs::unread) && mode & fs::write) if (mode & fs::trunc && mode & fs::lock && mode & fs::write)
{ {
// Postpone truncation in order to avoid using O_TRUNC on a locked file // Postpone truncation in order to avoid using O_TRUNC on a locked file
ensure(::ftruncate(fd, 0) == 0); ensure(::ftruncate(fd, 0) == 0);