diff --git a/Utilities/File.cpp b/Utilities/File.cpp index e8fb333c09..daf6907860 100644 --- a/Utilities/File.cpp +++ b/Utilities/File.cpp @@ -1099,13 +1099,22 @@ fs::file::file(const std::string& path, bs_t mode) if (mode & fs::append) flags |= O_APPEND; 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; int perm = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; 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; } @@ -1117,14 +1126,14 @@ fs::file::file(const std::string& path, bs_t mode) 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); ::close(fd); 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 ensure(::ftruncate(fd, 0) == 0);