qBittorrent
abstractfilestorage.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2020 Vladimir Golovnev <glassez@yandex.ru>
4  *
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.
9  *
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.
14  *
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.
18  *
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.
27  */
28 
29 #include "abstractfilestorage.h"
30 
31 #include <QDir>
32 #include <QHash>
33 #include <QVector>
34 
35 #include "base/exceptions.h"
36 #include "base/utils/fs.h"
37 
38 #if defined(Q_OS_WIN)
39 const Qt::CaseSensitivity CASE_SENSITIVITY {Qt::CaseInsensitive};
40 #else
41 const Qt::CaseSensitivity CASE_SENSITIVITY {Qt::CaseSensitive};
42 #endif
43 
44 namespace
45 {
46  bool areSameFileNames(QString first, QString second)
47  {
48  return QString::compare(first, second, CASE_SENSITIVITY) == 0;
49  }
50 }
51 
52 void BitTorrent::AbstractFileStorage::renameFile(const QString &oldPath, const QString &newPath)
53 {
54  if (!Utils::Fs::isValidFileSystemName(oldPath, true))
55  throw RuntimeError {tr("The old path is invalid: '%1'.").arg(oldPath)};
56  if (!Utils::Fs::isValidFileSystemName(newPath, true))
57  throw RuntimeError {tr("The new path is invalid: '%1'.").arg(newPath)};
58 
59  const QString oldFilePath = Utils::Fs::toUniformPath(oldPath);
60  if (oldFilePath.endsWith(QLatin1Char {'/'}))
61  throw RuntimeError {tr("Invalid file path: '%1'.").arg(oldFilePath)};
62 
63  const QString newFilePath = Utils::Fs::toUniformPath(newPath);
64  if (newFilePath.endsWith(QLatin1Char {'/'}))
65  throw RuntimeError {tr("Invalid file path: '%1'.").arg(newFilePath)};
66  if (QDir().isAbsolutePath(newFilePath))
67  throw RuntimeError {tr("Absolute path isn't allowed: '%1'.").arg(newFilePath)};
68 
69  int renamingFileIndex = -1;
70  for (int i = 0; i < filesCount(); ++i)
71  {
72  const QString path = filePath(i);
73 
74  if ((renamingFileIndex < 0) && areSameFileNames(path, oldFilePath))
75  renamingFileIndex = i;
76  else if (areSameFileNames(path, newFilePath))
77  throw RuntimeError {tr("The file already exists: '%1'.").arg(newFilePath)};
78  }
79 
80  if (renamingFileIndex < 0)
81  throw RuntimeError {tr("No such file: '%1'.").arg(oldFilePath)};
82 
83  renameFile(renamingFileIndex, newFilePath);
84 }
85 
86 void BitTorrent::AbstractFileStorage::renameFolder(const QString &oldPath, const QString &newPath)
87 {
88  if (!Utils::Fs::isValidFileSystemName(oldPath, true))
89  throw RuntimeError {tr("The old path is invalid: '%1'.").arg(oldPath)};
90  if (!Utils::Fs::isValidFileSystemName(newPath, true))
91  throw RuntimeError {tr("The new path is invalid: '%1'.").arg(newPath)};
92 
93  const auto cleanFolderPath = [](const QString &path) -> QString
94  {
95  const QString uniformPath = Utils::Fs::toUniformPath(path);
96  return (uniformPath.endsWith(QLatin1Char {'/'}) ? uniformPath : uniformPath + QLatin1Char {'/'});
97  };
98 
99  const QString oldFolderPath = cleanFolderPath(oldPath);
100  const QString newFolderPath = cleanFolderPath(newPath);
101  if (QDir().isAbsolutePath(newFolderPath))
102  throw RuntimeError {tr("Absolute path isn't allowed: '%1'.").arg(newFolderPath)};
103 
104  QVector<int> renamingFileIndexes;
105  renamingFileIndexes.reserve(filesCount());
106 
107  for (int i = 0; i < filesCount(); ++i)
108  {
109  const QString path = filePath(i);
110 
111  if (path.startsWith(oldFolderPath, CASE_SENSITIVITY))
112  renamingFileIndexes.append(i);
113  else if (path.startsWith(newFolderPath, CASE_SENSITIVITY))
114  throw RuntimeError {tr("The folder already exists: '%1'.").arg(newFolderPath)};
115  }
116 
117  if (renamingFileIndexes.isEmpty())
118  throw RuntimeError {tr("No such folder: '%1'.").arg(oldFolderPath)};
119 
120  for (const int index : renamingFileIndexes)
121  {
122  const QString newFilePath = newFolderPath + filePath(index).mid(oldFolderPath.size());
123  renameFile(index, newFilePath);
124  }
125 }
const Qt::CaseSensitivity CASE_SENSITIVITY
void renameFolder(const QString &oldPath, const QString &newPath)
virtual void renameFile(int index, const QString &name)=0
virtual int filesCount() const =0
virtual QString filePath(int index) const =0
bool isValidFileSystemName(const QString &name, bool allowSeparators=false)
Definition: fs.cpp:248
QString toUniformPath(const QString &path)
Definition: fs.cpp:69
bool areSameFileNames(QString first, QString second)