WebUI: migrate to fetch API
[qBittorrent.git] / src / base / bittorrent / abstractfilestorage.cpp
blobccd501c5b179d491ac94dcc7468c2038fe312107
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2020 Vladimir Golovnev <glassez@yandex.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "abstractfilestorage.h"
31 #include <QDir>
32 #include <QHash>
33 #include <QList>
35 #include "base/exceptions.h"
36 #include "base/path.h"
37 #include "base/utils/fs.h"
39 void BitTorrent::AbstractFileStorage::renameFile(const Path &oldPath, const Path &newPath)
41 if (!oldPath.isValid())
42 throw RuntimeError(tr("The old path is invalid: '%1'.").arg(oldPath.toString()));
43 if (!newPath.isValid())
44 throw RuntimeError(tr("The new path is invalid: '%1'.").arg(newPath.toString()));
45 if (newPath.isAbsolute())
46 throw RuntimeError(tr("Absolute path isn't allowed: '%1'.").arg(newPath.toString()));
48 int renamingFileIndex = -1;
49 for (int i = 0; i < filesCount(); ++i)
51 const Path path = filePath(i);
53 if ((renamingFileIndex < 0) && (path == oldPath))
54 renamingFileIndex = i;
55 else if (path == newPath)
56 throw RuntimeError(tr("The file already exists: '%1'.").arg(newPath.toString()));
59 if (renamingFileIndex < 0)
60 throw RuntimeError(tr("No such file: '%1'.").arg(oldPath.toString()));
62 renameFile(renamingFileIndex, newPath);
65 void BitTorrent::AbstractFileStorage::renameFolder(const Path &oldFolderPath, const Path &newFolderPath)
67 if (!oldFolderPath.isValid())
68 throw RuntimeError(tr("The old path is invalid: '%1'.").arg(oldFolderPath.toString()));
69 if (!newFolderPath.isValid())
70 throw RuntimeError(tr("The new path is invalid: '%1'.").arg(newFolderPath.toString()));
71 if (newFolderPath.isAbsolute())
72 throw RuntimeError(tr("Absolute path isn't allowed: '%1'.").arg(newFolderPath.toString()));
74 QList<int> renamingFileIndexes;
75 renamingFileIndexes.reserve(filesCount());
77 for (int i = 0; i < filesCount(); ++i)
79 const Path path = filePath(i);
81 if (path.hasAncestor(oldFolderPath))
82 renamingFileIndexes.append(i);
83 else if (path.hasAncestor(newFolderPath))
84 throw RuntimeError(tr("The folder already exists: '%1'.").arg(newFolderPath.toString()));
87 if (renamingFileIndexes.isEmpty())
88 throw RuntimeError(tr("No such folder: '%1'.").arg(oldFolderPath.toString()));
90 for (const int index : renamingFileIndexes)
92 const Path newFilePath = newFolderPath / oldFolderPath.relativePathOf(filePath(index));
93 renameFile(index, newFilePath);