Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2025 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #include "file.h"
23 :
24 : #include <cstring>
25 :
26 : #include "ioretcodes.h"
27 :
28 : #include "log.h"
29 :
30 : using namespace muse;
31 : using namespace muse::io;
32 :
33 5423 : File::File(const path_t& filePath)
34 5423 : : m_filePath(filePath)
35 : {
36 10846 : setMeta("file_path", m_filePath.toStdString());
37 5423 : }
38 :
39 5445 : File::~File()
40 : {
41 5411 : close();
42 5445 : }
43 :
44 0 : const path_t& File::filePath() const
45 : {
46 0 : return m_filePath;
47 : }
48 :
49 4958 : bool File::exists() const
50 : {
51 4958 : return fileSystem()->exists(m_filePath);
52 : }
53 :
54 1855 : bool File::exists(const path_t& filePath)
55 : {
56 1855 : return fileSystem()->exists(filePath);
57 : }
58 :
59 1185 : bool File::remove(const path_t& filePath)
60 : {
61 1185 : return fileSystem()->remove(filePath);
62 : }
63 :
64 0 : bool File::copy(const path_t& src, const path_t& dst, bool replace)
65 : {
66 0 : return fileSystem()->copy(src, dst, replace);
67 : }
68 :
69 242 : Ret File::readFile(const io::path_t& filePath, ByteArray& out)
70 : {
71 242 : bool ok = fileSystem()->readFile(filePath, out);
72 242 : return make_ret(ok ? Err::NoError : Err::FSReadError);
73 : }
74 :
75 3500 : Ret File::writeFile(const io::path_t& filePath, const ByteArray& data)
76 : {
77 3500 : return fileSystem()->writeFile(filePath, data);
78 : }
79 :
80 0 : bool File::setPermissionsAllowedForAll(const path_t& filePath)
81 : {
82 0 : return fileSystem()->setPermissionsAllowedForAll(filePath);
83 : }
84 :
85 0 : bool File::remove()
86 : {
87 0 : return fileSystem()->remove(m_filePath);
88 : }
89 :
90 4571 : bool File::doOpen(OpenMode m)
91 : {
92 4571 : if (m == IODevice::WriteOnly) {
93 6 : Ret ret = fileSystem()->isWritable(m_filePath);
94 6 : if (!ret) {
95 0 : setError(ret.code(), ret.text());
96 : }
97 :
98 6 : return ret;
99 6 : }
100 :
101 4565 : if (!exists()) {
102 8 : if (m == OpenMode::ReadOnly) {
103 16 : setError(int(Err::FSReadError), "Opening non-existent file called on a read-only mode");
104 8 : return false;
105 : } else {
106 0 : return true;
107 : }
108 : }
109 :
110 4557 : m_data = ByteArray();
111 4557 : Ret ret = fileSystem()->readFile(m_filePath, m_data);
112 4557 : if (!ret) {
113 0 : setError(ret.code(), ret.text());
114 0 : return false;
115 : }
116 :
117 4557 : return true;
118 4557 : }
119 :
120 324127 : size_t File::dataSize() const
121 : {
122 324127 : return m_data.size();
123 : }
124 :
125 103437 : const uint8_t* File::rawData() const
126 : {
127 103437 : return m_data.constData();
128 : }
129 :
130 7 : bool File::resizeData(size_t size)
131 : {
132 7 : m_data.resize(size);
133 7 : return true;
134 : }
135 :
136 8 : size_t File::writeData(const uint8_t* data, size_t len)
137 : {
138 8 : std::memcpy(m_data.data() + pos(), data, len);
139 8 : Ret ret = fileSystem()->writeFile(m_filePath, m_data);
140 8 : if (!ret) {
141 0 : setError(ret.code(), ret.text());
142 0 : return 0;
143 : }
144 8 : return len;
145 8 : }
|