diff --git a/modules/juce_core/files/juce_File.cpp b/modules/juce_core/files/juce_File.cpp index 2202344a9c..598717e5aa 100644 --- a/modules/juce_core/files/juce_File.cpp +++ b/modules/juce_core/files/juce_File.cpp @@ -1009,6 +1009,19 @@ public: expect (mb[0] == '0'); } + { + expect (tempFile.getSize() == 10); + FileOutputStream fo (tempFile); + expect (fo.openedOk()); + + expect (fo.setPosition (7)); + expect (fo.truncate().wasOk()); + expect (tempFile.getSize() == 7); + fo.write ("789", 3); + fo.flush(); + expect (tempFile.getSize() == 10); + } + beginTest ("Memory-mapped files"); { diff --git a/modules/juce_core/files/juce_FileOutputStream.h b/modules/juce_core/files/juce_FileOutputStream.h index 870c3b14b4..6d078c11dc 100644 --- a/modules/juce_core/files/juce_FileOutputStream.h +++ b/modules/juce_core/files/juce_FileOutputStream.h @@ -80,6 +80,12 @@ public: */ bool openedOk() const noexcept { return status.wasOk(); } + /** Attempts to truncate the file to the current write position. + To truncate a file to a specific size, first use setPosition() to seek to the + appropriate location, and then call this method. + */ + Result truncate(); + //============================================================================== void flush(); int64 getPosition(); diff --git a/modules/juce_core/native/juce_posix_SharedCode.h b/modules/juce_core/native/juce_posix_SharedCode.h index 4fd41ec9d5..51f58b14e0 100644 --- a/modules/juce_core/native/juce_posix_SharedCode.h +++ b/modules/juce_core/native/juce_posix_SharedCode.h @@ -280,6 +280,11 @@ namespace { return value == -1 ? getResultForErrno() : Result::ok(); } + + int getFD (void* handle) noexcept + { + return (int) (pointer_sized_int) handle; + } } bool File::isDirectory() const @@ -399,10 +404,10 @@ Result File::createDirectoryInternal (const String& fileName) const return getResultForReturnValue (mkdir (fileName.toUTF8(), 0777)); } -//============================================================================== +//===================================================================== int64 juce_fileSetPosition (void* handle, int64 pos) { - if (handle != 0 && lseek ((int) (pointer_sized_int) handle, pos, SEEK_SET) == pos) + if (handle != 0 && lseek (getFD (handle), pos, SEEK_SET) == pos) return pos; return -1; @@ -424,7 +429,7 @@ void FileInputStream::closeHandle() { if (fileHandle != 0) { - close ((int) (pointer_sized_int) fileHandle); + close (getFD (fileHandle)); fileHandle = 0; } } @@ -435,7 +440,7 @@ size_t FileInputStream::readInternal (void* const buffer, const size_t numBytes) if (fileHandle != 0) { - result = ::read ((int) (pointer_sized_int) fileHandle, buffer, numBytes); + result = ::read (getFD (fileHandle), buffer, numBytes); if (result < 0) { @@ -488,7 +493,7 @@ void FileOutputStream::closeHandle() { if (fileHandle != 0) { - close ((int) (pointer_sized_int) fileHandle); + close (getFD (fileHandle)); fileHandle = 0; } } @@ -499,7 +504,7 @@ int FileOutputStream::writeInternal (const void* const data, const int numBytes) if (fileHandle != 0) { - result = ::write ((int) (pointer_sized_int) fileHandle, data, numBytes); + result = ::write (getFD (fileHandle), data, numBytes); if (result == -1) status = getResultForErrno(); @@ -511,10 +516,19 @@ int FileOutputStream::writeInternal (const void* const data, const int numBytes) void FileOutputStream::flushInternal() { if (fileHandle != 0) - if (fsync ((int) (pointer_sized_int) fileHandle) == -1) + if (fsync (getFD (fileHandle)) == -1) status = getResultForErrno(); } +Result FileOutputStream::truncate() +{ + if (fileHandle == 0) + return status; + + flush(); + return getResultForReturnValue (ftruncate (getFD (fileHandle), (off_t) currentPosition)); +} + //============================================================================== MemoryMappedFile::MemoryMappedFile (const File& file, MemoryMappedFile::AccessMode mode) : address (nullptr), diff --git a/modules/juce_core/native/juce_win32_Files.cpp b/modules/juce_core/native/juce_win32_Files.cpp index f9b30bab98..8dabf7db03 100644 --- a/modules/juce_core/native/juce_win32_Files.cpp +++ b/modules/juce_core/native/juce_win32_Files.cpp @@ -289,6 +289,16 @@ void FileOutputStream::flushInternal() status = WindowsFileHelpers::getResultForLastError(); } +Result FileOutputStream::truncate() +{ + if (fileHandle == nullptr) + return status; + + flush(); + return SetEndOfFile ((HANDLE) fileHandle) ? Result::ok() + : WindowsFileHelpers::getResultForLastError(); +} + //============================================================================== MemoryMappedFile::MemoryMappedFile (const File& file, MemoryMappedFile::AccessMode mode) : address (nullptr), diff --git a/modules/juce_gui_basics/layout/juce_Viewport.cpp b/modules/juce_gui_basics/layout/juce_Viewport.cpp index 7edd16eb4b..22c2d6ef91 100644 --- a/modules/juce_gui_basics/layout/juce_Viewport.cpp +++ b/modules/juce_gui_basics/layout/juce_Viewport.cpp @@ -185,7 +185,7 @@ void Viewport::updateVisibleArea() const bool canShowHBar = showHScrollbar && canShowAnyBars; const bool canShowVBar = showVScrollbar && canShowAnyBars; - bool hBarVisible, vBarVisible; + bool hBarVisible = false, vBarVisible = false; Rectangle contentArea; for (int i = 3; --i >= 0;)