2011-06-14 14:28:51 +04:00
|
|
|
/*
|
|
|
|
This file is part of the PhantomJS project from Ofi Labs.
|
|
|
|
|
2011-08-31 19:29:40 +04:00
|
|
|
Copyright (C) 2011 Ivan De Marino <ivan.de.marino@gmail.com>
|
2011-06-14 14:28:51 +04:00
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the <organization> nor the
|
|
|
|
names of its contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
|
|
|
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "filesystem.h"
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QDebug>
|
2011-07-22 03:29:25 +04:00
|
|
|
#include <QDateTime>
|
2011-06-14 14:28:51 +04:00
|
|
|
|
|
|
|
// File
|
|
|
|
// public:
|
2012-02-02 19:29:00 +04:00
|
|
|
File::File(QFile *openfile, QTextCodec *codec, QObject *parent) :
|
2011-06-14 14:28:51 +04:00
|
|
|
QObject(parent),
|
|
|
|
m_file(openfile)
|
|
|
|
{
|
|
|
|
m_fileStream.setDevice(m_file);
|
2012-02-02 19:29:00 +04:00
|
|
|
if ((QTextCodec *)NULL == codec) {
|
|
|
|
m_fileStream.setCodec(QTextCodec::codecForName("UTF-8"));
|
|
|
|
} else {
|
|
|
|
m_fileStream.setCodec(codec);
|
|
|
|
}
|
2011-06-14 14:28:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
File::~File()
|
|
|
|
{
|
|
|
|
this->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// public slots:
|
|
|
|
QString File::read()
|
|
|
|
{
|
|
|
|
if ( m_file->isReadable() ) {
|
|
|
|
return m_fileStream.readAll();
|
|
|
|
}
|
|
|
|
qDebug() << "File::read - " << "Couldn't read:" << m_file->fileName();
|
2011-06-28 16:24:24 +04:00
|
|
|
return QString();
|
2011-06-14 14:28:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool File::write(const QString &data)
|
|
|
|
{
|
|
|
|
if ( m_file->isWritable() ) {
|
|
|
|
m_fileStream << data;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
qDebug() << "File::write - " << "Couldn't write:" << m_file->fileName();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString File::readLine()
|
|
|
|
{
|
|
|
|
if ( m_file->isReadable() ) {
|
|
|
|
return m_fileStream.readLine();
|
|
|
|
}
|
|
|
|
qDebug() << "File::readLine - " << "Couldn't read:" << m_file->fileName();
|
2011-06-28 16:24:24 +04:00
|
|
|
return QString();
|
2011-06-14 14:28:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool File::writeLine(const QString &data)
|
|
|
|
{
|
|
|
|
if ( write(data) && write("\n") ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
qDebug() << "File::writeLine - " << "Couldn't write:" << m_file->fileName();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool File::atEnd() const
|
|
|
|
{
|
|
|
|
if ( m_file->isReadable() ) {
|
|
|
|
return m_fileStream.atEnd();
|
|
|
|
}
|
|
|
|
qDebug() << "File::atEnd - " << "Couldn't read:" << m_file->fileName();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void File::flush()
|
|
|
|
{
|
|
|
|
if ( m_file ) {
|
|
|
|
m_fileStream.flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void File::close()
|
|
|
|
{
|
|
|
|
flush();
|
|
|
|
if ( m_file ) {
|
|
|
|
m_file->close();
|
|
|
|
delete m_file;
|
|
|
|
m_file = NULL;
|
|
|
|
}
|
|
|
|
deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// FileSystem
|
|
|
|
// public:
|
|
|
|
FileSystem::FileSystem(QObject *parent) :
|
|
|
|
QObject(parent)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
// public slots:
|
2011-07-22 03:29:25 +04:00
|
|
|
|
|
|
|
// Attributes
|
|
|
|
int FileSystem::_size(const QString &path) const
|
|
|
|
{
|
|
|
|
QFileInfo fi(path);
|
|
|
|
if (fi.exists()) {
|
|
|
|
return fi.size();
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant FileSystem::lastModified(const QString &path) const
|
|
|
|
{
|
|
|
|
QFileInfo fi(path);
|
|
|
|
if (fi.exists()) {
|
|
|
|
return QVariant(fi.lastModified());
|
|
|
|
}
|
|
|
|
return QVariant(QDateTime());
|
|
|
|
}
|
|
|
|
|
2011-08-09 02:42:11 +04:00
|
|
|
// Tests
|
2011-06-14 14:28:51 +04:00
|
|
|
bool FileSystem::exists(const QString &path) const
|
|
|
|
{
|
|
|
|
return QFile::exists(path);
|
|
|
|
}
|
|
|
|
|
2011-06-28 02:12:46 +04:00
|
|
|
bool FileSystem::isDirectory(const QString &path) const
|
2011-06-14 14:28:51 +04:00
|
|
|
{
|
|
|
|
return QFileInfo(path).isDir();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileSystem::isFile(const QString &path) const
|
|
|
|
{
|
|
|
|
return QFileInfo(path).isFile();
|
|
|
|
}
|
|
|
|
|
2011-08-09 02:42:11 +04:00
|
|
|
bool FileSystem::isAbsolute(const QString &path) const {
|
|
|
|
return QFileInfo(path).isAbsolute();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileSystem::isExecutable(const QString &path) const {
|
|
|
|
return QFileInfo(path).isExecutable();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileSystem::isLink(const QString &path) const {
|
|
|
|
return QFileInfo(path).isSymLink();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileSystem::isReadable(const QString &path) const {
|
|
|
|
return QFileInfo(path).isReadable();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileSystem::isWritable(const QString &path) const {
|
|
|
|
return QFileInfo(path).isWritable();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Directory
|
2011-09-05 02:42:54 +04:00
|
|
|
bool FileSystem::_copyTree(const QString &source, const QString &destination) const {
|
|
|
|
QDir sourceDir(source);
|
|
|
|
QDir::Filters sourceDirFilter = QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Files | QDir::NoSymLinks | QDir::Drives;
|
|
|
|
|
|
|
|
if (sourceDir.exists()) {
|
|
|
|
// Make the destination directory if it doesn't exist already
|
|
|
|
if (!FileSystem::exists(destination) && !FileSystem::makeDirectory(destination)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(QFileInfo entry, sourceDir.entryInfoList(sourceDirFilter, QDir::DirsFirst)) {
|
|
|
|
if (entry.isDir()) {
|
|
|
|
if (!FileSystem::_copyTree(entry.absoluteFilePath(),
|
|
|
|
destination + "/" + entry.fileName())) { //< directory: recursive call
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!FileSystem::_copy(entry.absoluteFilePath(),
|
|
|
|
destination + "/" + entry.fileName())) { //< file: copy
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-28 02:12:46 +04:00
|
|
|
bool FileSystem::makeDirectory(const QString &path) const
|
|
|
|
{
|
|
|
|
return QDir().mkdir(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileSystem::makeTree(const QString &path) const
|
2011-06-14 14:28:51 +04:00
|
|
|
{
|
|
|
|
return QDir().mkpath(path);
|
|
|
|
}
|
|
|
|
|
2011-09-01 02:32:56 +04:00
|
|
|
bool FileSystem::_removeDirectory(const QString &path) const
|
2011-06-28 02:12:46 +04:00
|
|
|
{
|
|
|
|
return QDir().rmdir(path);
|
|
|
|
}
|
|
|
|
|
2011-09-01 02:32:56 +04:00
|
|
|
bool FileSystem::_removeTree(const QString &path) const
|
2011-06-28 02:12:46 +04:00
|
|
|
{
|
|
|
|
QDir dir(path);
|
2011-09-05 02:42:54 +04:00
|
|
|
QDir::Filters dirFilter = QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files;
|
2011-06-28 02:12:46 +04:00
|
|
|
|
|
|
|
if (dir.exists()) {
|
2011-09-05 02:42:54 +04:00
|
|
|
foreach(QFileInfo info, dir.entryInfoList(dirFilter, QDir::DirsFirst)) {
|
2011-06-28 02:12:46 +04:00
|
|
|
if (info.isDir()) {
|
2011-09-05 02:42:54 +04:00
|
|
|
if (!FileSystem::_removeTree(info.absoluteFilePath())) { //< directory: recursive call
|
|
|
|
return false;
|
|
|
|
}
|
2011-06-28 02:12:46 +04:00
|
|
|
} else {
|
2011-09-05 02:42:54 +04:00
|
|
|
if (!FileSystem::_remove(info.absoluteFilePath())) { //< file: remove
|
|
|
|
return false;
|
|
|
|
}
|
2011-06-28 02:12:46 +04:00
|
|
|
}
|
|
|
|
}
|
2011-09-05 02:42:54 +04:00
|
|
|
if (!FileSystem::_removeDirectory(path)) { //< delete the top tree directory
|
|
|
|
return false;
|
|
|
|
}
|
2011-06-28 02:12:46 +04:00
|
|
|
}
|
|
|
|
|
2011-09-05 02:42:54 +04:00
|
|
|
return true;
|
2011-06-28 02:12:46 +04:00
|
|
|
}
|
|
|
|
|
2011-06-14 14:28:51 +04:00
|
|
|
QStringList FileSystem::list(const QString &path) const
|
|
|
|
{
|
|
|
|
return QDir(path).entryList();
|
|
|
|
}
|
|
|
|
|
2011-08-02 03:47:43 +04:00
|
|
|
// Paths
|
|
|
|
QString FileSystem::separator() const
|
|
|
|
{
|
|
|
|
return QDir::separator();
|
|
|
|
}
|
|
|
|
|
2011-06-28 02:12:46 +04:00
|
|
|
QString FileSystem::workingDirectory() const
|
2011-06-14 14:28:51 +04:00
|
|
|
{
|
|
|
|
return QDir::currentPath();
|
|
|
|
}
|
|
|
|
|
2011-06-28 02:12:46 +04:00
|
|
|
bool FileSystem::changeWorkingDirectory(const QString &path) const
|
|
|
|
{
|
|
|
|
return QDir::setCurrent(path);
|
|
|
|
}
|
|
|
|
|
2011-08-02 03:47:43 +04:00
|
|
|
QString FileSystem::absolute(const QString &relativePath) const
|
2011-06-14 14:28:51 +04:00
|
|
|
{
|
2011-08-02 03:47:43 +04:00
|
|
|
return QFileInfo(relativePath).absoluteFilePath();
|
2011-06-14 14:28:51 +04:00
|
|
|
}
|
|
|
|
|
2012-02-02 19:29:00 +04:00
|
|
|
static inline QString getCharset(const QVariant &val) {
|
|
|
|
QVariant::Type type = val.type();
|
|
|
|
|
|
|
|
// val must be either a string or null/undefined.
|
|
|
|
if (QVariant::String != type && QVariant::Invalid != type) {
|
|
|
|
qDebug() << "FileSystem::open - " << "Charset must be a string!";
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString charset = val.toString();
|
|
|
|
|
|
|
|
// Default to UTF-8
|
|
|
|
if (charset.isEmpty()) {
|
|
|
|
charset = "UTF-8";
|
|
|
|
}
|
|
|
|
|
|
|
|
return charset;
|
|
|
|
}
|
|
|
|
|
2011-08-02 03:47:43 +04:00
|
|
|
// Files
|
2012-02-02 18:48:35 +04:00
|
|
|
QObject *FileSystem::_open(const QString &path, const QVariantMap &opts) const
|
2011-06-14 14:28:51 +04:00
|
|
|
{
|
|
|
|
File *f = NULL;
|
|
|
|
QFile *_f = new QFile(path);
|
|
|
|
QFile::OpenMode modeCode = QFile::NotOpen;
|
2012-02-02 18:48:35 +04:00
|
|
|
QVariant modeVar = opts["mode"];
|
|
|
|
|
|
|
|
// Ensure only strings
|
|
|
|
if (modeVar.type() != QVariant::String) {
|
|
|
|
qDebug() << "FileSystem::open - " << "Mode must be a string!" << modeVar;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString mode = modeVar.toString();
|
2011-06-14 14:28:51 +04:00
|
|
|
|
|
|
|
// Ensure only one "mode character" has been selected
|
|
|
|
if ( mode.length() != 1) {
|
|
|
|
qDebug() << "FileSystem::open - " << "Wrong Mode string length:" << mode;
|
2011-06-28 16:24:24 +04:00
|
|
|
return NULL;
|
2011-06-14 14:28:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the OpenMode
|
2011-06-28 02:12:46 +04:00
|
|
|
switch(mode[0].toAscii()) {
|
|
|
|
case 'r': case 'R': {
|
|
|
|
modeCode |= QFile::ReadOnly;
|
2011-06-28 16:24:24 +04:00
|
|
|
// Make sure there is something to read
|
2011-06-28 02:12:46 +04:00
|
|
|
if ( !_f->exists() ) {
|
|
|
|
qDebug() << "FileSystem::open - " << "Trying to read a file that doesn't exist:" << path;
|
2011-06-28 16:24:24 +04:00
|
|
|
return NULL;
|
2011-06-14 14:28:51 +04:00
|
|
|
}
|
2011-06-28 02:12:46 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'a': case 'A': case '+': {
|
|
|
|
modeCode |= QFile::Append;
|
|
|
|
// NOTE: no "break" here! This case will also execute the code for case 'w'.
|
|
|
|
}
|
|
|
|
case 'w': case 'W': {
|
|
|
|
modeCode |= QFile::WriteOnly;
|
2011-06-28 16:24:24 +04:00
|
|
|
// Make sure the file exists OR it can be created at the required path
|
2011-06-28 02:12:46 +04:00
|
|
|
if ( !_f->exists() && !makeTree(QFileInfo(path).dir().absolutePath()) ) {
|
|
|
|
qDebug() << "FileSystem::open - " << "Full path coulnd't be created:" << path;
|
2011-06-28 16:24:24 +04:00
|
|
|
return NULL;
|
2011-06-14 14:28:51 +04:00
|
|
|
}
|
2011-06-28 02:12:46 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
qDebug() << "FileSystem::open - " << "Wrong Mode:" << mode;
|
2011-06-28 16:24:24 +04:00
|
|
|
return NULL;
|
2011-06-28 02:12:46 +04:00
|
|
|
}
|
2011-06-14 14:28:51 +04:00
|
|
|
}
|
|
|
|
|
2012-02-02 19:29:00 +04:00
|
|
|
QString charset = getCharset(opts["charset"]);
|
|
|
|
QTextCodec *codec = QTextCodec::codecForName(charset.toAscii());
|
|
|
|
if ((QTextCodec *)NULL == codec) {
|
|
|
|
qDebug() << "FileSystem::open - " << "Unknown charset:" << charset;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-06-14 14:28:51 +04:00
|
|
|
// Try to Open
|
|
|
|
if ( _f->open(modeCode) ) {
|
2012-02-02 19:29:00 +04:00
|
|
|
f = new File(_f, codec);
|
2011-06-28 16:24:24 +04:00
|
|
|
if ( f ) {
|
|
|
|
return f;
|
|
|
|
}
|
2011-06-14 14:28:51 +04:00
|
|
|
}
|
|
|
|
|
2011-06-28 16:24:24 +04:00
|
|
|
// Return "NULL" if the file couldn't be opened as requested
|
2011-06-14 14:28:51 +04:00
|
|
|
qDebug() << "FileSystem::open - " << "Couldn't be opened:" << path;
|
2011-06-28 16:24:24 +04:00
|
|
|
return NULL;
|
2011-06-14 14:28:51 +04:00
|
|
|
}
|
2011-09-01 03:01:40 +04:00
|
|
|
|
|
|
|
bool FileSystem::_remove(const QString &path) const
|
|
|
|
{
|
|
|
|
return QFile::remove(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileSystem::_copy(const QString &source, const QString &destination) const {
|
|
|
|
return QFile(source).copy(destination);
|
|
|
|
}
|