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/bittorrent/common.h"
36 #include "base/exceptions.h"
37 #include "base/utils/fs.h"
38 
39 #if defined(Q_OS_WIN)
40 const Qt::CaseSensitivity CASE_SENSITIVITY {Qt::CaseInsensitive};
41 #else
42 const Qt::CaseSensitivity CASE_SENSITIVITY {Qt::CaseSensitive};
43 #endif
44 
45 namespace
46 {
areSameFileNames(QString first,QString second)47     bool areSameFileNames(QString first, QString second)
48     {
49         if (first.endsWith(QB_EXT, Qt::CaseInsensitive))
50             first.chop(QB_EXT.size());
51         if (second.endsWith(QB_EXT, Qt::CaseInsensitive))
52             second.chop(QB_EXT.size());
53         return QString::compare(first, second, CASE_SENSITIVITY) == 0;
54     }
55 }
56 
renameFile(const QString & oldPath,const QString & newPath)57 void BitTorrent::AbstractFileStorage::renameFile(const QString &oldPath, const QString &newPath)
58 {
59     if (!Utils::Fs::isValidFileSystemName(oldPath, true))
60         throw RuntimeError {tr("The old path is invalid: '%1'.").arg(oldPath)};
61     if (!Utils::Fs::isValidFileSystemName(newPath, true))
62         throw RuntimeError {tr("The new path is invalid: '%1'.").arg(newPath)};
63 
64     const QString oldFilePath = Utils::Fs::toUniformPath(oldPath);
65     if (oldFilePath.endsWith(QLatin1Char {'/'}))
66         throw RuntimeError {tr("Invalid file path: '%1'.").arg(oldFilePath)};
67 
68     const QString newFilePath = Utils::Fs::toUniformPath(newPath);
69     if (newFilePath.endsWith(QLatin1Char {'/'}))
70         throw RuntimeError {tr("Invalid file path: '%1'.").arg(newFilePath)};
71     if (QDir().isAbsolutePath(newFilePath))
72         throw RuntimeError {tr("Absolute path isn't allowed: '%1'.").arg(newFilePath)};
73 
74     int renamingFileIndex = -1;
75     for (int i = 0; i < filesCount(); ++i)
76     {
77         const QString path = filePath(i);
78 
79         if ((renamingFileIndex < 0) && areSameFileNames(path, oldFilePath))
80             renamingFileIndex = i;
81         else if (areSameFileNames(path, newFilePath))
82             throw RuntimeError {tr("The file already exists: '%1'.").arg(newFilePath)};
83     }
84 
85     if (renamingFileIndex < 0)
86         throw RuntimeError {tr("No such file: '%1'.").arg(oldFilePath)};
87 
88     const auto extAdjusted = [](const QString &path, const bool needExt) -> QString
89     {
90         if (path.endsWith(QB_EXT, Qt::CaseInsensitive) == needExt)
91             return path;
92 
93         return (needExt ? (path + QB_EXT) : (path.left(path.size() - QB_EXT.size())));
94     };
95 
96     renameFile(renamingFileIndex, extAdjusted(newFilePath, filePath(renamingFileIndex).endsWith(QB_EXT, Qt::CaseInsensitive)));
97 }
98 
renameFolder(const QString & oldPath,const QString & newPath)99 void BitTorrent::AbstractFileStorage::renameFolder(const QString &oldPath, const QString &newPath)
100 {
101     if (!Utils::Fs::isValidFileSystemName(oldPath, true))
102         throw RuntimeError {tr("The old path is invalid: '%1'.").arg(oldPath)};
103     if (!Utils::Fs::isValidFileSystemName(newPath, true))
104         throw RuntimeError {tr("The new path is invalid: '%1'.").arg(newPath)};
105 
106     const auto cleanFolderPath = [](const QString &path) -> QString
107     {
108         const QString uniformPath = Utils::Fs::toUniformPath(path);
109         return (uniformPath.endsWith(QLatin1Char {'/'}) ? uniformPath : uniformPath + QLatin1Char {'/'});
110     };
111 
112     const QString oldFolderPath = cleanFolderPath(oldPath);
113     const QString newFolderPath = cleanFolderPath(newPath);
114     if (QDir().isAbsolutePath(newFolderPath))
115         throw RuntimeError {tr("Absolute path isn't allowed: '%1'.").arg(newFolderPath)};
116 
117     QVector<int> renamingFileIndexes;
118     renamingFileIndexes.reserve(filesCount());
119 
120     for (int i = 0; i < filesCount(); ++i)
121     {
122         const QString path = filePath(i);
123 
124         if (path.startsWith(oldFolderPath, CASE_SENSITIVITY))
125             renamingFileIndexes.append(i);
126         else if (path.startsWith(newFolderPath, CASE_SENSITIVITY))
127             throw RuntimeError {tr("The folder already exists: '%1'.").arg(newFolderPath)};
128     }
129 
130     if (renamingFileIndexes.isEmpty())
131         throw RuntimeError {tr("No such folder: '%1'.").arg(oldFolderPath)};
132 
133     for (const int index : renamingFileIndexes)
134     {
135         const QString newFilePath = newFolderPath + filePath(index).mid(oldFolderPath.size());
136         renameFile(index, newFilePath);
137     }
138 }
139