fileutils.cpp
1 //------------------------------------------------------------------------------
2 // fileutils.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library 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 GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2012 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "fileutils.h"
24 
25 #include "log.h"
26 #include <QCryptographicHash>
27 #include <QDirIterator>
28 #include <QFileInfo>
29 
30 QByteArray FileUtils::md5(const QString &path)
31 {
32  QFile f(path);
33  if (f.open(QIODevice::ReadOnly))
34  {
35  QCryptographicHash hash(QCryptographicHash::Md5);
36  QByteArray chunk = f.read(1024 * 1024);
37  for (; !chunk.isEmpty(); chunk = f.read(1024 * 1024))
38  hash.addData(chunk);
39  f.close();
40  return hash.result();
41  }
42  return QByteArray();
43 }
44 
45 QString FileUtils::cdUpUntilExists(QString path)
46 {
47  static const int SANITY_LOOP_LIMIT = 1000;
48  QFileInfo fileInfo(QDir::cleanPath(path));
49  for (int i = 0; i < SANITY_LOOP_LIMIT; ++i)
50  {
51  if (fileInfo.filePath().endsWith("/.."))
52  return QString();
53  if (fileInfo.exists() || fileInfo.isRoot())
54  return fileInfo.filePath();
55  fileInfo = QDir::cleanPath(fileInfo.filePath() + "/..");
56  }
57  return QString();
58 }
59 
60 Qt::CaseSensitivity FileUtils::comparisonSensitivity()
61 {
62  #if defined(Q_OS_WIN32)
63  return Qt::CaseInsensitive;
64  #else
65  return Qt::CaseSensitive;
66  #endif
67 }
68 
69 bool FileUtils::containsPath(const QStringList &candidates, const QString &path)
70 {
71  for (const QString &candidate : candidates)
72  {
73  if (QFileInfo(candidate) == QFileInfo(path))
74  return true;
75  }
76  return false;
77 }
78 
79 bool FileUtils::rmAllFiles(const QString &dirPath,
80  const QStringList &nameFilters)
81 {
82  QDirIterator it(dirPath, nameFilters, QDir::Files);
83  bool bAllSuccess = true;
84  while (it.hasNext())
85  {
86  QString path = it.next();
87  QFile f(path);
88  if (!f.remove())
89  {
90  bAllSuccess = false;
91  gLog << Log::tr("Failed to remove: %1").arg(path);
92  }
93  }
94  return bAllSuccess;
95 }