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 <QCryptographicHash>
26 #include <QDirIterator>
27 #include <QFileInfo>
28 #include "log.h"
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  {
39  hash.addData(chunk);
40  }
41  f.close();
42  return hash.result();
43  }
44  return QByteArray();
45 }
46 
47 QString FileUtils::cdUpUntilExists(QString path)
48 {
49  static const int SANITY_LOOP_LIMIT = 1000;
50  QFileInfo fileInfo(QDir::cleanPath(path));
51  for (int i = 0; i < SANITY_LOOP_LIMIT; ++i)
52  {
53  if (fileInfo.filePath().endsWith("/.."))
54  return QString();
55  if (fileInfo.exists() || fileInfo.isRoot())
56  return fileInfo.filePath();
57  fileInfo = QDir::cleanPath(fileInfo.filePath() + "/..");
58  }
59  return QString();
60 }
61 
62 Qt::CaseSensitivity FileUtils::comparisonSensitivity()
63 {
64 #if defined(Q_OS_WIN32)
65  return Qt::CaseInsensitive;
66 #else
67  return Qt::CaseSensitive;
68 #endif
69 }
70 
71 bool FileUtils::containsPath(const QStringList &candidates, const QString &path)
72 {
73  foreach (const QString &candidate, candidates)
74  {
75  if (QFileInfo(candidate) == QFileInfo(path))
76  {
77  return true;
78  }
79  }
80  return false;
81 }
82 
83 bool FileUtils::rmAllFiles(const QString& dirPath,
84  const QStringList & nameFilters)
85 {
86  QDirIterator it(dirPath, nameFilters, QDir::Files);
87  bool bAllSuccess = true;
88  while (it.hasNext())
89  {
90  QString path = it.next();
91  QFile f(path);
92  if (!f.remove())
93  {
94  bAllSuccess = false;
95  gLog << Log::tr("Failed to remove: %1").arg(path);
96  }
97  }
98  return bAllSuccess;
99 }
static bool containsPath(const QStringList &candidates, const QString &path)
Uses QFileInfo::operator== to see if &#39;path&#39; is on &#39;candidates&#39; list.
Definition: fileutils.cpp:71
static bool rmAllFiles(const QString &dirPath, const QStringList &nameFilters=QStringList())
Deletes all files in specified directory.
Definition: fileutils.cpp:83
static QString cdUpUntilExists(QString path)
Moves upwards the path until it finds the path that exists.
Definition: fileutils.cpp:47