2015-05-28 23:08:29 +05:30
|
|
|
// Licensed under the Apache-2.0 license. See README.md for details.
|
|
|
|
|
|
|
|
#include "FileSystem.h"
|
|
|
|
|
|
|
|
#include <QDir>
|
2018-02-11 05:59:43 +05:30
|
|
|
#include <QFile>
|
2015-05-28 23:08:29 +05:30
|
|
|
#include <QSaveFile>
|
|
|
|
#include <QFileInfo>
|
2015-10-05 05:17:27 +05:30
|
|
|
#include <QDebug>
|
|
|
|
#include <QUrl>
|
2016-01-05 12:02:52 +05:30
|
|
|
#include <QStandardPaths>
|
2018-02-11 05:59:43 +05:30
|
|
|
#include <QTextStream>
|
2015-05-28 23:08:29 +05:30
|
|
|
|
2018-02-11 05:05:56 +05:30
|
|
|
#if defined Q_OS_WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#include <string>
|
|
|
|
#include <sys/utime.h>
|
2018-02-11 05:59:43 +05:30
|
|
|
#include <winnls.h>
|
|
|
|
#include <shobjidl.h>
|
|
|
|
#include <objbase.h>
|
|
|
|
#include <objidl.h>
|
|
|
|
#include <shlguid.h>
|
|
|
|
#include <shlobj.h>
|
2018-02-11 05:05:56 +05:30
|
|
|
#else
|
|
|
|
#include <utime.h>
|
|
|
|
#endif
|
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
namespace FS {
|
|
|
|
|
2015-05-28 23:08:29 +05:30
|
|
|
void ensureExists(const QDir &dir)
|
|
|
|
{
|
|
|
|
if (!QDir().mkpath(dir.absolutePath()))
|
|
|
|
{
|
2017-01-17 03:12:22 +05:30
|
|
|
throw FileSystemException("Unable to create folder " + dir.dirName() + " (" +
|
2015-05-28 23:08:29 +05:30
|
|
|
dir.absolutePath() + ")");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
void write(const QString &filename, const QByteArray &data)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
|
|
|
ensureExists(QFileInfo(filename).dir());
|
|
|
|
QSaveFile file(filename);
|
|
|
|
if (!file.open(QSaveFile::WriteOnly))
|
|
|
|
{
|
|
|
|
throw FileSystemException("Couldn't open " + filename + " for writing: " +
|
|
|
|
file.errorString());
|
|
|
|
}
|
|
|
|
if (data.size() != file.write(data))
|
|
|
|
{
|
|
|
|
throw FileSystemException("Error writing data to " + filename + ": " +
|
|
|
|
file.errorString());
|
|
|
|
}
|
|
|
|
if (!file.commit())
|
|
|
|
{
|
|
|
|
throw FileSystemException("Error while committing data to " + filename + ": " +
|
|
|
|
file.errorString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
QByteArray read(const QString &filename)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
|
|
|
QFile file(filename);
|
|
|
|
if (!file.open(QFile::ReadOnly))
|
|
|
|
{
|
|
|
|
throw FileSystemException("Unable to open " + filename + " for reading: " +
|
|
|
|
file.errorString());
|
|
|
|
}
|
|
|
|
const qint64 size = file.size();
|
|
|
|
QByteArray data(int(size), 0);
|
|
|
|
const qint64 ret = file.read(data.data(), size);
|
|
|
|
if (ret == -1 || ret != size)
|
|
|
|
{
|
|
|
|
throw FileSystemException("Error reading data from " + filename + ": " +
|
|
|
|
file.errorString());
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
2015-10-05 05:17:27 +05:30
|
|
|
|
2016-11-17 08:39:24 +05:30
|
|
|
bool updateTimestamp(const QString& filename)
|
|
|
|
{
|
2018-02-11 05:05:56 +05:30
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
std::wstring filename_utf_16 = filename.toStdWString();
|
|
|
|
return (_wutime64(filename_utf_16.c_str(), nullptr) == 0);
|
|
|
|
#else
|
|
|
|
QByteArray filenameBA = QFile::encodeName(filename);
|
|
|
|
return (utime(filenameBA.data(), nullptr) == 0);
|
|
|
|
#endif
|
2016-11-17 08:39:24 +05:30
|
|
|
}
|
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
bool ensureFilePathExists(QString filenamepath)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
|
|
|
QFileInfo a(filenamepath);
|
|
|
|
QDir dir;
|
|
|
|
QString ensuredPath = a.path();
|
|
|
|
bool success = dir.mkpath(ensuredPath);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
bool ensureFolderPathExists(QString foldernamepath)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
|
|
|
QFileInfo a(foldernamepath);
|
|
|
|
QDir dir;
|
|
|
|
QString ensuredPath = a.filePath();
|
|
|
|
bool success = dir.mkpath(ensuredPath);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
bool copy::operator()(const QString &offset)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
|
|
|
//NOTE always deep copy on windows. the alternatives are too messy.
|
|
|
|
#if defined Q_OS_WIN32
|
2015-10-10 09:25:55 +05:30
|
|
|
m_followSymlinks = true;
|
2015-10-05 05:17:27 +05:30
|
|
|
#endif
|
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
auto src = PathCombine(m_src.absolutePath(), offset);
|
|
|
|
auto dst = PathCombine(m_dst.absolutePath(), offset);
|
2015-10-05 05:17:27 +05:30
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
QFileInfo currentSrc(src);
|
|
|
|
if (!currentSrc.exists())
|
|
|
|
return false;
|
2015-10-05 05:17:27 +05:30
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
if(!m_followSymlinks && currentSrc.isSymLink())
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
2015-10-10 09:25:55 +05:30
|
|
|
qDebug() << "creating symlink" << src << " - " << dst;
|
|
|
|
if (!ensureFilePathExists(dst))
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
2015-10-10 09:25:55 +05:30
|
|
|
qWarning() << "Cannot create path!";
|
|
|
|
return false;
|
2015-10-05 05:17:27 +05:30
|
|
|
}
|
2015-10-10 09:25:55 +05:30
|
|
|
return QFile::link(currentSrc.symLinkTarget(), dst);
|
|
|
|
}
|
|
|
|
else if(currentSrc.isFile())
|
|
|
|
{
|
|
|
|
qDebug() << "copying file" << src << " - " << dst;
|
|
|
|
if (!ensureFilePathExists(dst))
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
2015-10-10 09:25:55 +05:30
|
|
|
qWarning() << "Cannot create path!";
|
|
|
|
return false;
|
2015-10-05 05:17:27 +05:30
|
|
|
}
|
2015-10-10 09:25:55 +05:30
|
|
|
return QFile::copy(src, dst);
|
|
|
|
}
|
|
|
|
else if(currentSrc.isDir())
|
|
|
|
{
|
|
|
|
qDebug() << "recursing" << offset;
|
|
|
|
if (!ensureFolderPathExists(dst))
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
2015-10-10 09:25:55 +05:30
|
|
|
qWarning() << "Cannot create path!";
|
|
|
|
return false;
|
2015-10-05 05:17:27 +05:30
|
|
|
}
|
2015-10-10 09:25:55 +05:30
|
|
|
QDir currentDir(src);
|
|
|
|
for(auto & f : currentDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System))
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
2015-10-10 09:25:55 +05:30
|
|
|
auto inner_offset = PathCombine(offset, f);
|
|
|
|
// ignore and skip stuff that matches the blacklist.
|
|
|
|
if(m_blacklist && m_blacklist->matches(inner_offset))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(!operator()(inner_offset))
|
|
|
|
{
|
2016-06-10 03:17:17 +05:30
|
|
|
qWarning() << "Failed to copy" << inner_offset;
|
2015-10-10 09:25:55 +05:30
|
|
|
return false;
|
|
|
|
}
|
2015-10-05 05:17:27 +05:30
|
|
|
}
|
|
|
|
}
|
2015-10-10 09:25:55 +05:30
|
|
|
else
|
|
|
|
{
|
|
|
|
qCritical() << "Copy ERROR: Unknown filesystem object:" << src;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2015-10-05 05:17:27 +05:30
|
|
|
}
|
2015-10-10 09:25:55 +05:30
|
|
|
|
|
|
|
bool deletePath(QString path)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
|
|
|
bool OK = true;
|
|
|
|
QDir dir(path);
|
|
|
|
|
|
|
|
if (!dir.exists())
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
auto allEntries = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden |
|
|
|
|
QDir::AllDirs | QDir::Files,
|
|
|
|
QDir::DirsFirst);
|
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
for(auto & info: allEntries)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
|
|
|
#if defined Q_OS_WIN32
|
|
|
|
QString nativePath = QDir::toNativeSeparators(info.absoluteFilePath());
|
|
|
|
auto wString = nativePath.toStdWString();
|
|
|
|
DWORD dwAttrs = GetFileAttributesW(wString.c_str());
|
|
|
|
// Windows: check for junctions, reparse points and other nasty things of that sort
|
|
|
|
if(dwAttrs & FILE_ATTRIBUTE_REPARSE_POINT)
|
|
|
|
{
|
|
|
|
if (info.isFile())
|
|
|
|
{
|
|
|
|
OK &= QFile::remove(info.absoluteFilePath());
|
|
|
|
}
|
|
|
|
else if (info.isDir())
|
|
|
|
{
|
|
|
|
OK &= dir.rmdir(info.absoluteFilePath());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// We do not trust Qt with reparse points, but do trust it with unix symlinks.
|
|
|
|
if(info.isSymLink())
|
|
|
|
{
|
|
|
|
OK &= QFile::remove(info.absoluteFilePath());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else if (info.isDir())
|
|
|
|
{
|
|
|
|
OK &= deletePath(info.absoluteFilePath());
|
|
|
|
}
|
|
|
|
else if (info.isFile())
|
|
|
|
{
|
|
|
|
OK &= QFile::remove(info.absoluteFilePath());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OK = false;
|
|
|
|
qCritical() << "Delete ERROR: Unknown filesystem object:" << info.absoluteFilePath();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OK &= dir.rmdir(dir.absolutePath());
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-08 12:32:27 +05:30
|
|
|
QString PathCombine(const QString & path1, const QString & path2)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
|
|
|
if(!path1.size())
|
|
|
|
return path2;
|
|
|
|
if(!path2.size())
|
|
|
|
return path1;
|
|
|
|
return QDir::cleanPath(path1 + QDir::separator() + path2);
|
|
|
|
}
|
|
|
|
|
2017-09-08 12:32:27 +05:30
|
|
|
QString PathCombine(const QString & path1, const QString & path2, const QString & path3)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
|
|
|
return PathCombine(PathCombine(path1, path2), path3);
|
|
|
|
}
|
|
|
|
|
2017-09-08 12:32:27 +05:30
|
|
|
QString PathCombine(const QString & path1, const QString & path2, const QString & path3, const QString & path4)
|
|
|
|
{
|
|
|
|
return PathCombine(PathCombine(path1, path2, path3), path4);
|
|
|
|
}
|
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
QString AbsolutePath(QString path)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
|
|
|
return QFileInfo(path).absolutePath();
|
|
|
|
}
|
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
QString ResolveExecutable(QString path)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
|
|
|
if (path.isEmpty())
|
|
|
|
{
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
if(!path.contains('/'))
|
|
|
|
{
|
|
|
|
path = QStandardPaths::findExecutable(path);
|
|
|
|
}
|
|
|
|
QFileInfo pathInfo(path);
|
|
|
|
if(!pathInfo.exists() || !pathInfo.isExecutable())
|
|
|
|
{
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
return pathInfo.absoluteFilePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalize path
|
|
|
|
*
|
2017-01-17 03:12:22 +05:30
|
|
|
* Any paths inside the current folder will be normalized to relative paths (to current)
|
2015-10-05 05:17:27 +05:30
|
|
|
* Other paths will be made absolute
|
|
|
|
*/
|
2015-10-10 09:25:55 +05:30
|
|
|
QString NormalizePath(QString path)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
|
|
|
QDir a = QDir::currentPath();
|
|
|
|
QString currentAbsolute = a.absolutePath();
|
|
|
|
|
|
|
|
QDir b(path);
|
|
|
|
QString newAbsolute = b.absolutePath();
|
|
|
|
|
|
|
|
if (newAbsolute.startsWith(currentAbsolute))
|
|
|
|
{
|
|
|
|
return a.relativeFilePath(newAbsolute);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return newAbsolute;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString badFilenameChars = "\"\\/?<>:*|!";
|
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
QString RemoveInvalidFilenameChars(QString string, QChar replaceWith)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
|
|
|
for (int i = 0; i < string.length(); i++)
|
|
|
|
{
|
|
|
|
if (badFilenameChars.contains(string[i]))
|
|
|
|
{
|
|
|
|
string[i] = replaceWith;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
QString DirNameFromString(QString string, QString inDir)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
|
|
|
int num = 0;
|
|
|
|
QString baseName = RemoveInvalidFilenameChars(string, '-');
|
|
|
|
QString dirName;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if(num == 0)
|
|
|
|
{
|
|
|
|
dirName = baseName;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dirName = baseName + QString::number(num);;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's over 9000
|
|
|
|
if (num > 9000)
|
|
|
|
return "";
|
|
|
|
num++;
|
|
|
|
} while (QFileInfo(PathCombine(inDir, dirName)).exists());
|
|
|
|
return dirName;
|
|
|
|
}
|
|
|
|
|
2017-01-17 03:12:22 +05:30
|
|
|
// Does the folder path contain any '!'? If yes, return true, otherwise false.
|
2015-10-05 05:17:27 +05:30
|
|
|
// (This is a problem for Java)
|
2015-10-10 09:25:55 +05:30
|
|
|
bool checkProblemticPathJava(QDir folder)
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
|
|
|
QString pathfoldername = folder.absolutePath();
|
|
|
|
return pathfoldername.contains("!", Qt::CaseInsensitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Win32 crap
|
|
|
|
#if defined Q_OS_WIN
|
|
|
|
|
|
|
|
bool called_coinit = false;
|
|
|
|
|
|
|
|
HRESULT CreateLink(LPCSTR linkPath, LPCSTR targetPath, LPCSTR args)
|
|
|
|
{
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
if (!called_coinit)
|
|
|
|
{
|
|
|
|
hres = CoInitialize(NULL);
|
|
|
|
called_coinit = true;
|
|
|
|
|
|
|
|
if (!SUCCEEDED(hres))
|
|
|
|
{
|
|
|
|
qWarning("Failed to initialize COM. Error 0x%08X", hres);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IShellLink *link;
|
|
|
|
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink,
|
|
|
|
(LPVOID *)&link);
|
|
|
|
|
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
|
|
|
IPersistFile *persistFile;
|
|
|
|
|
|
|
|
link->SetPath(targetPath);
|
|
|
|
link->SetArguments(args);
|
|
|
|
|
|
|
|
hres = link->QueryInterface(IID_IPersistFile, (LPVOID *)&persistFile);
|
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
|
|
|
WCHAR wstr[MAX_PATH];
|
|
|
|
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, linkPath, -1, wstr, MAX_PATH);
|
|
|
|
|
|
|
|
hres = persistFile->Save(wstr, TRUE);
|
|
|
|
persistFile->Release();
|
|
|
|
}
|
|
|
|
link->Release();
|
|
|
|
}
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-10-10 09:25:55 +05:30
|
|
|
QString getDesktopDir()
|
2015-10-05 05:17:27 +05:30
|
|
|
{
|
|
|
|
return QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cross-platform Shortcut creation
|
2015-10-10 09:25:55 +05:30
|
|
|
bool createShortCut(QString location, QString dest, QStringList args, QString name,
|
2015-10-05 05:17:27 +05:30
|
|
|
QString icon)
|
|
|
|
{
|
|
|
|
#if defined Q_OS_LINUX
|
|
|
|
location = PathCombine(location, name + ".desktop");
|
|
|
|
|
|
|
|
QFile f(location);
|
|
|
|
f.open(QIODevice::WriteOnly | QIODevice::Text);
|
|
|
|
QTextStream stream(&f);
|
|
|
|
|
|
|
|
QString argstring;
|
|
|
|
if (!args.empty())
|
|
|
|
argstring = " '" + args.join("' '") + "'";
|
|
|
|
|
|
|
|
stream << "[Desktop Entry]"
|
|
|
|
<< "\n";
|
|
|
|
stream << "Type=Application"
|
|
|
|
<< "\n";
|
|
|
|
stream << "TryExec=" << dest.toLocal8Bit() << "\n";
|
|
|
|
stream << "Exec=" << dest.toLocal8Bit() << argstring.toLocal8Bit() << "\n";
|
|
|
|
stream << "Name=" << name.toLocal8Bit() << "\n";
|
|
|
|
stream << "Icon=" << icon.toLocal8Bit() << "\n";
|
|
|
|
|
|
|
|
stream.flush();
|
|
|
|
f.close();
|
|
|
|
|
|
|
|
f.setPermissions(f.permissions() | QFileDevice::ExeOwner | QFileDevice::ExeGroup |
|
|
|
|
QFileDevice::ExeOther);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
#elif defined Q_OS_WIN
|
|
|
|
// TODO: Fix
|
|
|
|
// QFile file(PathCombine(location, name + ".lnk"));
|
|
|
|
// WCHAR *file_w;
|
|
|
|
// WCHAR *dest_w;
|
|
|
|
// WCHAR *args_w;
|
|
|
|
// file.fileName().toWCharArray(file_w);
|
|
|
|
// dest.toWCharArray(dest_w);
|
|
|
|
|
|
|
|
// QString argStr;
|
|
|
|
// for (int i = 0; i < args.count(); i++)
|
|
|
|
// {
|
|
|
|
// argStr.append(args[i]);
|
|
|
|
// argStr.append(" ");
|
|
|
|
// }
|
|
|
|
// argStr.toWCharArray(args_w);
|
|
|
|
|
|
|
|
// return SUCCEEDED(CreateLink(file_w, dest_w, args_w));
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
qWarning("Desktop Shortcuts not supported on your platform!");
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
2016-01-05 12:02:52 +05:30
|
|
|
}
|