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.

122 lines
4.3KB

  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 "TemporaryFile.h"
  21. #include "../maths/Random.h"
  22. namespace water {
  23. static File createTempFile (const File& parentDirectory, String name,
  24. const String& suffix, const int optionFlags)
  25. {
  26. if ((optionFlags & TemporaryFile::useHiddenFile) != 0)
  27. name = "." + name;
  28. return parentDirectory.getNonexistentChildFile (name, suffix, (optionFlags & TemporaryFile::putNumbersInBrackets) != 0);
  29. }
  30. TemporaryFile::TemporaryFile (const String& suffix, const int optionFlags)
  31. : temporaryFile (createTempFile (File::getSpecialLocation (File::tempDirectory),
  32. "temp_" + String::toHexString (Random::getSystemRandom().nextInt()),
  33. suffix, optionFlags))
  34. {
  35. }
  36. TemporaryFile::TemporaryFile (const File& target, const int optionFlags)
  37. : temporaryFile (createTempFile (target.getParentDirectory(),
  38. target.getFileNameWithoutExtension()
  39. + "_temp" + String::toHexString (Random::getSystemRandom().nextInt()),
  40. target.getFileExtension(), optionFlags)),
  41. targetFile (target)
  42. {
  43. // If you use this constructor, you need to give it a valid target file!
  44. wassert (targetFile != File());
  45. }
  46. TemporaryFile::TemporaryFile (const File& target, const File& temporary)
  47. : temporaryFile (temporary), targetFile (target)
  48. {
  49. }
  50. TemporaryFile::~TemporaryFile()
  51. {
  52. if (! deleteTemporaryFile())
  53. {
  54. /* Failed to delete our temporary file! The most likely reason for this would be
  55. that you've not closed an output stream that was being used to write to file.
  56. If you find that something beyond your control is changing permissions on
  57. your temporary files and preventing them from being deleted, you may want to
  58. call TemporaryFile::deleteTemporaryFile() to detect those error cases and
  59. handle them appropriately.
  60. */
  61. wassertfalse;
  62. }
  63. }
  64. //==============================================================================
  65. bool TemporaryFile::overwriteTargetFileWithTemporary() const
  66. {
  67. // This method only works if you created this object with the constructor
  68. // that takes a target file!
  69. wassert (targetFile != File());
  70. if (temporaryFile.exists())
  71. {
  72. // Have a few attempts at overwriting the file before giving up..
  73. for (int i = 5; --i >= 0;)
  74. {
  75. if (temporaryFile.replaceFileIn (targetFile))
  76. return true;
  77. carla_msleep (100);
  78. }
  79. }
  80. else
  81. {
  82. // There's no temporary file to use. If your write failed, you should
  83. // probably check, and not bother calling this method.
  84. wassertfalse;
  85. }
  86. return false;
  87. }
  88. bool TemporaryFile::deleteTemporaryFile() const
  89. {
  90. // Have a few attempts at deleting the file before giving up..
  91. for (int i = 5; --i >= 0;)
  92. {
  93. if (temporaryFile.deleteFile())
  94. return true;
  95. carla_msleep (50);
  96. }
  97. return false;
  98. }
  99. }