Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileOutputStream.cpp 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #include "FileOutputStream.h"
  21. namespace water {
  22. int64 water_fileSetPosition (void* handle, int64 pos);
  23. //==============================================================================
  24. FileOutputStream::FileOutputStream (const File& f, const size_t bufferSizeToUse)
  25. : file (f),
  26. fileHandle (nullptr),
  27. status (Result::ok()),
  28. currentPosition (0),
  29. bufferSize (bufferSizeToUse),
  30. bytesInBuffer (0)
  31. {
  32. if (buffer.malloc(jmax (bufferSizeToUse, (size_t) 16)))
  33. openHandle();
  34. else
  35. status = Result::fail ("Allocation failure");
  36. }
  37. FileOutputStream::~FileOutputStream()
  38. {
  39. flushBuffer();
  40. closeHandle();
  41. }
  42. int64 FileOutputStream::getPosition()
  43. {
  44. return currentPosition;
  45. }
  46. bool FileOutputStream::setPosition (int64 newPosition)
  47. {
  48. if (newPosition != currentPosition)
  49. {
  50. flushBuffer();
  51. currentPosition = water_fileSetPosition (fileHandle, newPosition);
  52. }
  53. return newPosition == currentPosition;
  54. }
  55. bool FileOutputStream::flushBuffer()
  56. {
  57. bool ok = true;
  58. if (bytesInBuffer > 0)
  59. {
  60. ok = (writeInternal (buffer, bytesInBuffer) == (ssize_t) bytesInBuffer);
  61. bytesInBuffer = 0;
  62. }
  63. return ok;
  64. }
  65. void FileOutputStream::flush()
  66. {
  67. flushBuffer();
  68. flushInternal();
  69. }
  70. bool FileOutputStream::write (const void* const src, const size_t numBytes)
  71. {
  72. jassert (src != nullptr && ((ssize_t) numBytes) >= 0);
  73. if (bytesInBuffer + numBytes < bufferSize)
  74. {
  75. memcpy (buffer + bytesInBuffer, src, numBytes);
  76. bytesInBuffer += numBytes;
  77. currentPosition += (int64) numBytes;
  78. }
  79. else
  80. {
  81. if (! flushBuffer())
  82. return false;
  83. if (numBytes < bufferSize)
  84. {
  85. memcpy (buffer + bytesInBuffer, src, numBytes);
  86. bytesInBuffer += numBytes;
  87. currentPosition += (int64) numBytes;
  88. }
  89. else
  90. {
  91. const ssize_t bytesWritten = writeInternal (src, numBytes);
  92. if (bytesWritten < 0)
  93. return false;
  94. currentPosition += (int64) bytesWritten;
  95. return bytesWritten == (ssize_t) numBytes;
  96. }
  97. }
  98. return true;
  99. }
  100. bool FileOutputStream::writeRepeatedByte (uint8 byte, size_t numBytes)
  101. {
  102. jassert (((ssize_t) numBytes) >= 0);
  103. if (bytesInBuffer + numBytes < bufferSize)
  104. {
  105. memset (buffer + bytesInBuffer, byte, numBytes);
  106. bytesInBuffer += numBytes;
  107. currentPosition += (int64) numBytes;
  108. return true;
  109. }
  110. return OutputStream::writeRepeatedByte (byte, numBytes);
  111. }
  112. #ifdef CARLA_OS_WIN
  113. void FileOutputStream::openHandle()
  114. {
  115. HANDLE h = CreateFile (file.getFullPathName().toUTF8(), GENERIC_WRITE, FILE_SHARE_READ, 0,
  116. OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  117. if (h != INVALID_HANDLE_VALUE)
  118. {
  119. LARGE_INTEGER li;
  120. li.QuadPart = 0;
  121. li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_END);
  122. if (li.LowPart != INVALID_SET_FILE_POINTER)
  123. {
  124. fileHandle = (void*) h;
  125. currentPosition = li.QuadPart;
  126. return;
  127. }
  128. }
  129. status = getResultForLastError();
  130. }
  131. void FileOutputStream::closeHandle()
  132. {
  133. CloseHandle ((HANDLE) fileHandle);
  134. }
  135. ssize_t FileOutputStream::writeInternal (const void* bufferToWrite, size_t numBytes)
  136. {
  137. if (fileHandle != nullptr)
  138. {
  139. DWORD actualNum = 0;
  140. if (! WriteFile ((HANDLE) fileHandle, bufferToWrite, (DWORD) numBytes, &actualNum, 0))
  141. status = getResultForLastError();
  142. return (ssize_t) actualNum;
  143. }
  144. return 0;
  145. }
  146. void FileOutputStream::flushInternal()
  147. {
  148. if (fileHandle != nullptr)
  149. if (! FlushFileBuffers ((HANDLE) fileHandle))
  150. status = getResultForLastError();
  151. }
  152. #else
  153. void FileOutputStream::openHandle()
  154. {
  155. if (file.exists())
  156. {
  157. const int f = open (file.getFullPathName().toUTF8(), O_RDWR, 00644);
  158. if (f != -1)
  159. {
  160. currentPosition = lseek (f, 0, SEEK_END);
  161. if (currentPosition >= 0)
  162. {
  163. fileHandle = fdToVoidPointer (f);
  164. }
  165. else
  166. {
  167. status = getResultForErrno();
  168. close (f);
  169. }
  170. }
  171. else
  172. {
  173. status = getResultForErrno();
  174. }
  175. }
  176. else
  177. {
  178. const int f = open (file.getFullPathName().toUTF8(), O_RDWR + O_CREAT, 00644);
  179. if (f != -1)
  180. fileHandle = fdToVoidPointer (f);
  181. else
  182. status = getResultForErrno();
  183. }
  184. }
  185. void FileOutputStream::closeHandle()
  186. {
  187. if (fileHandle != 0)
  188. {
  189. close (getFD (fileHandle));
  190. fileHandle = 0;
  191. }
  192. }
  193. ssize_t FileOutputStream::writeInternal (const void* const data, const size_t numBytes)
  194. {
  195. ssize_t result = 0;
  196. if (fileHandle != 0)
  197. {
  198. result = ::write (getFD (fileHandle), data, numBytes);
  199. if (result == -1)
  200. status = getResultForErrno();
  201. }
  202. return result;
  203. }
  204. void FileOutputStream::flushInternal()
  205. {
  206. if (fileHandle != 0)
  207. {
  208. if (fsync (getFD (fileHandle)) == -1)
  209. status = getResultForErrno();
  210. }
  211. }
  212. #endif
  213. }