LMMS
Loading...
Searching...
No Matches
TemporaryFile.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
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>
7
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/
10
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.
14
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.
22
23 ==============================================================================
24*/
25
26#include "TemporaryFile.h"
27#include "../maths/Random.h"
28
29#include "CarlaTimeUtils.hpp"
30
31namespace water {
32
33static File createTempFile (const File& parentDirectory, String name,
34 const String& suffix, const int optionFlags)
35{
36 if ((optionFlags & TemporaryFile::useHiddenFile) != 0)
37 name = "." + name;
38
39 return parentDirectory.getNonexistentChildFile (name, suffix, (optionFlags & TemporaryFile::putNumbersInBrackets) != 0);
40}
41
42TemporaryFile::TemporaryFile (const String& suffix, const int optionFlags)
43 : temporaryFile (createTempFile (File::getSpecialLocation (File::tempDirectory),
44 "temp_" + String::toHexString (Random::getSystemRandom().nextInt()),
45 suffix, optionFlags))
46{
47}
48
49TemporaryFile::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)
55{
56 // If you use this constructor, you need to give it a valid target file!
57 wassert (targetFile != File());
58}
59
60TemporaryFile::TemporaryFile (const File& target, const File& temporary)
61 : temporaryFile (temporary), targetFile (target)
62{
63}
64
66{
67 if (! deleteTemporaryFile())
68 {
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.
71
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.
76 */
78 }
79}
80
81//==============================================================================
83{
84 // This method only works if you created this object with the constructor
85 // that takes a target file!
86 wassert (targetFile != File());
87
88 if (temporaryFile.exists())
89 {
90 // Have a few attempts at overwriting the file before giving up..
91 for (int i = 5; --i >= 0;)
92 {
93 if (temporaryFile.replaceFileIn (targetFile))
94 return true;
95
96 carla_msleep (100);
97 }
98 }
99 else
100 {
101 // There's no temporary file to use. If your write failed, you should
102 // probably check, and not bother calling this method.
104 }
105
106 return false;
107}
108
110{
111 // Have a few attempts at deleting the file before giving up..
112 for (int i = 5; --i >= 0;)
113 {
114 if (temporaryFile.deleteFile())
115 return true;
116
117 carla_msleep (50);
118 }
119
120 return false;
121}
122
123}
Definition File.h:50
File getNonexistentChildFile(const String &prefix, const String &suffix, bool putNumbersInBrackets=true) const
Definition File.cpp:617
Definition Random.h:40
Definition String.h:48
~TemporaryFile()
Definition TemporaryFile.cpp:65
bool deleteTemporaryFile() const
Definition TemporaryFile.cpp:109
TemporaryFile(const String &suffix=String(), int optionFlags=0)
Definition TemporaryFile.cpp:42
const File targetFile
Definition TemporaryFile.h:165
@ putNumbersInBrackets
Definition TemporaryFile.h:80
@ useHiddenFile
Definition TemporaryFile.h:78
const File temporaryFile
Definition TemporaryFile.h:165
bool overwriteTargetFileWithTemporary() const
Definition TemporaryFile.cpp:82
register unsigned i
Definition inflate.c:1575
static const char * name
Definition pugl.h:1582
#define wassertfalse
#define wassert(expression)
Definition AudioSampleBuffer.h:33
static File createTempFile(const File &parentDirectory, String name, const String &suffix, const int optionFlags)
Definition TemporaryFile.cpp:33