1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-09 23:34:20 +00:00

Fix 0 file descriptor handling on POSIX systems

This commit is contained in:
attila 2025-02-05 12:01:23 +01:00
parent da09b99bbf
commit 49b6dfb7c8
7 changed files with 80 additions and 25 deletions

View file

@ -0,0 +1,57 @@
/*
==============================================================================
This file is part of the JUCE framework.
Copyright (c) Raw Material Software Limited
JUCE is an open source framework subject to commercial or open source
licensing.
By downloading, installing, or using the JUCE framework, or combining the
JUCE framework with any other source code, object code, content or any other
copyrightable work, you agree to the terms of the JUCE End User Licence
Agreement, and all incorporated terms including the JUCE Privacy Policy and
the JUCE Website Terms of Service, as applicable, which will bind you. If you
do not agree to the terms of these agreements, we will not license the JUCE
framework to you, and you must discontinue the installation or download
process and cease use of the JUCE framework.
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
Or:
You may also use this code under the terms of the AGPLv3:
https://www.gnu.org/licenses/agpl-3.0.en.html
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
==============================================================================
*/
namespace juce::detail
{
#if JUCE_WINDOWS
using NativeFileHandle = void*;
#else
class NativeFileHandle
{
static constexpr int invalidFd = -1;
public:
int get() const noexcept { return fd; }
bool isValid() const noexcept { return 0 <= fd; }
void set (int newFd) noexcept { fd = newFd; }
void invalidate() noexcept { fd = invalidFd; }
private:
int fd = invalidFd;
};
#endif
} // namespace juce::detail

View file

@ -35,7 +35,7 @@
namespace juce
{
int64 juce_fileSetPosition (void* handle, int64 pos);
int64 juce_fileSetPosition (detail::NativeFileHandle handle, int64 pos);
//==============================================================================

View file

@ -89,7 +89,7 @@ public:
private:
//==============================================================================
const File file;
void* fileHandle = nullptr;
detail::NativeFileHandle fileHandle{};
int64 currentPosition = 0;
Result status { Result::ok() };

View file

@ -119,7 +119,7 @@ public:
private:
//==============================================================================
File file;
void* fileHandle = nullptr;
detail::NativeFileHandle fileHandle{};
Result status { Result::ok() };
int64 currentPosition = 0;
size_t bufferSize, bytesInBuffer = 0;

View file

@ -318,6 +318,7 @@ JUCE_END_IGNORE_WARNINGS_MSVC
#include "files/juce_File.h"
#include "files/juce_DirectoryIterator.h"
#include "files/juce_RangedDirectoryIterator.h"
#include "detail/juce_NativeFileHandle.h"
#include "files/juce_FileInputStream.h"
#include "files/juce_FileOutputStream.h"
#include "files/juce_FileSearchPath.h"

View file

@ -949,9 +949,9 @@ private:
void FileOutputStream::flushInternal()
{
if (fileHandle != nullptr)
if (fileHandle.isValid())
{
if (fsync (getFD (fileHandle)) == -1)
if (fsync (fileHandle.get()) == -1)
status = getResultForErrno();
// This stuff tells the OS to asynchronously update the metadata

View file

@ -241,9 +241,6 @@ namespace
{
return value == -1 ? getResultForErrno() : Result::ok();
}
int getFD (void* handle) noexcept { return (int) (pointer_sized_int) handle; }
void* fdToVoidPointer (int fd) noexcept { return (void*) (pointer_sized_int) fd; }
}
bool File::isDirectory() const
@ -442,9 +439,9 @@ Result File::createDirectoryInternal (const String& fileName) const
}
//==============================================================================
int64 juce_fileSetPosition (void* handle, int64 pos)
int64 juce_fileSetPosition (detail::NativeFileHandle handle, int64 pos)
{
if (handle != nullptr && lseek (getFD (handle), (off_t) pos, SEEK_SET) == pos)
if (handle.isValid() && lseek (handle.get(), (off_t) pos, SEEK_SET) == pos)
return pos;
return -1;
@ -455,24 +452,24 @@ void FileInputStream::openHandle()
auto f = open (file.getFullPathName().toUTF8(), O_RDONLY);
if (f != -1)
fileHandle = fdToVoidPointer (f);
fileHandle.set (f);
else
status = getResultForErrno();
}
FileInputStream::~FileInputStream()
{
if (fileHandle != nullptr)
close (getFD (fileHandle));
if (fileHandle.isValid())
close (fileHandle.get());
}
size_t FileInputStream::readInternal (void* buffer, size_t numBytes)
{
ssize_t result = 0;
if (fileHandle != nullptr)
if (fileHandle.isValid())
{
result = ::read (getFD (fileHandle), buffer, numBytes);
result = ::read (fileHandle.get(), buffer, numBytes);
if (result < 0)
{
@ -497,7 +494,7 @@ void FileOutputStream::openHandle()
if (currentPosition >= 0)
{
fileHandle = fdToVoidPointer (f);
fileHandle.set (f);
}
else
{
@ -515,7 +512,7 @@ void FileOutputStream::openHandle()
auto f = open (file.getFullPathName().toUTF8(), O_RDWR | O_CREAT, 00644);
if (f != -1)
fileHandle = fdToVoidPointer (f);
fileHandle.set (f);
else
status = getResultForErrno();
}
@ -523,19 +520,19 @@ void FileOutputStream::openHandle()
void FileOutputStream::closeHandle()
{
if (fileHandle != nullptr)
if (fileHandle.isValid())
{
close (getFD (fileHandle));
fileHandle = nullptr;
close (fileHandle.get());
fileHandle.invalidate();
}
}
ssize_t FileOutputStream::writeInternal (const void* data, size_t numBytes)
{
if (fileHandle == nullptr)
if (! fileHandle.isValid())
return 0;
auto result = ::write (getFD (fileHandle), data, numBytes);
auto result = ::write (fileHandle.get(), data, numBytes);
if (result == -1)
status = getResultForErrno();
@ -546,18 +543,18 @@ ssize_t FileOutputStream::writeInternal (const void* data, size_t numBytes)
#ifndef JUCE_ANDROID
void FileOutputStream::flushInternal()
{
if (fileHandle != nullptr && fsync (getFD (fileHandle)) == -1)
if (fileHandle.isValid() && fsync (fileHandle.get()) == -1)
status = getResultForErrno();
}
#endif
Result FileOutputStream::truncate()
{
if (fileHandle == nullptr)
if (! fileHandle.isValid())
return status;
flush();
return getResultForReturnValue (ftruncate (getFD (fileHandle), (off_t) currentPosition));
return getResultForReturnValue (ftruncate (fileHandle.get(), (off_t) currentPosition));
}
//==============================================================================