Cleanup
[carla.git] / source / modules / water / files / TemporaryFile.cpp
blob76a23561461fa9fa093451f5d465dc21e7daa6d8
1 /*
2 ==============================================================================
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017-2023 Filipe Coelho <falktx@falktx.com>
8 Permission is granted to use this software under the terms of the ISC license
9 http://www.isc.org/downloads/software-support-policy/isc-license/
11 Permission to use, copy, modify, and/or distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 OF THIS SOFTWARE.
23 ==============================================================================
26 #include "TemporaryFile.h"
27 #include "../maths/Random.h"
29 #include "CarlaTimeUtils.hpp"
31 namespace water {
33 static File createTempFile (const File& parentDirectory, String name,
34 const String& suffix, const int optionFlags)
36 if ((optionFlags & TemporaryFile::useHiddenFile) != 0)
37 name = "." + name;
39 return parentDirectory.getNonexistentChildFile (name, suffix, (optionFlags & TemporaryFile::putNumbersInBrackets) != 0);
42 TemporaryFile::TemporaryFile (const String& suffix, const int optionFlags)
43 : temporaryFile (createTempFile (File::getSpecialLocation (File::tempDirectory),
44 "temp_" + String::toHexString (Random::getSystemRandom().nextInt()),
45 suffix, optionFlags))
49 TemporaryFile::TemporaryFile (const File& target, const int optionFlags)
50 : temporaryFile (createTempFile (target.getParentDirectory(),
51 target.getFileNameWithoutExtension()
52 + "_temp" + String::toHexString (Random::getSystemRandom().nextInt()),
53 target.getFileExtension(), optionFlags)),
54 targetFile (target)
56 // If you use this constructor, you need to give it a valid target file!
57 wassert (targetFile != File());
60 TemporaryFile::TemporaryFile (const File& target, const File& temporary)
61 : temporaryFile (temporary), targetFile (target)
65 TemporaryFile::~TemporaryFile()
67 if (! deleteTemporaryFile())
69 /* Failed to delete our temporary file! The most likely reason for this would be
70 that you've not closed an output stream that was being used to write to file.
72 If you find that something beyond your control is changing permissions on
73 your temporary files and preventing them from being deleted, you may want to
74 call TemporaryFile::deleteTemporaryFile() to detect those error cases and
75 handle them appropriately.
77 wassertfalse;
81 //==============================================================================
82 bool TemporaryFile::overwriteTargetFileWithTemporary() const
84 // This method only works if you created this object with the constructor
85 // that takes a target file!
86 wassert (targetFile != File());
88 if (temporaryFile.exists())
90 // Have a few attempts at overwriting the file before giving up..
91 for (int i = 5; --i >= 0;)
93 if (temporaryFile.replaceFileIn (targetFile))
94 return true;
96 carla_msleep (100);
99 else
101 // There's no temporary file to use. If your write failed, you should
102 // probably check, and not bother calling this method.
103 wassertfalse;
106 return false;
109 bool TemporaryFile::deleteTemporaryFile() const
111 // Have a few attempts at deleting the file before giving up..
112 for (int i = 5; --i >= 0;)
114 if (temporaryFile.deleteFile())
115 return true;
117 carla_msleep (50);
120 return false;