Commit ee3ac998 authored by Michael Reeves's avatar Michael Reeves

break parent/data union always init data.

parent d7507c80
...@@ -48,6 +48,7 @@ class FileAccess::Data ...@@ -48,6 +48,7 @@ class FileAccess::Data
{ {
reset(); reset();
} }
void reset() void reset()
{ {
m_url = QUrl(); m_url = QUrl();
...@@ -85,39 +86,36 @@ class FileAccess::Data ...@@ -85,39 +86,36 @@ class FileAccess::Data
FileAccess::FileAccess(const QString& name, bool bWantToWrite) FileAccess::FileAccess(const QString& name, bool bWantToWrite)
{ {
m_pData = nullptr; m_pData = nullptr;
m_bUseData = false; createData();
setFile(name, bWantToWrite); setFile(name, bWantToWrite);
} }
FileAccess::FileAccess() FileAccess::FileAccess()
{ {
m_bUseData = false;
m_bExists = false; m_bExists = false;
m_bFile = false; m_bFile = false;
m_bDir = false; m_bDir = false;
m_bSymLink = false; m_bSymLink = false;
m_bWritable = false; m_bWritable = false;
m_bHidden = false; m_bHidden = false;
m_pParent = nullptr; m_pData = nullptr;
m_size = 0; m_size = 0;
createData();
} }
FileAccess::FileAccess(const FileAccess& other) FileAccess::FileAccess(const FileAccess& other)
{ {
m_pData = nullptr; createData();
m_bUseData = false;
*m_pData = *other.m_pData;
*this = other; *this = other;
} }
void FileAccess::createData() void FileAccess::createData()
{ {
if(d() == nullptr) if(d() == nullptr)
{
FileAccess* pParent = m_pParent; // backup because in union with m_pData
m_pData = new Data(); m_pData = new Data();
m_bUseData = true;
m_pData->m_pParent = pParent;
}
} }
FileAccess& FileAccess::operator=(const FileAccess& other) FileAccess& FileAccess::operator=(const FileAccess& other)
...@@ -132,33 +130,20 @@ FileAccess& FileAccess::operator=(const FileAccess& other) ...@@ -132,33 +130,20 @@ FileAccess& FileAccess::operator=(const FileAccess& other)
m_bWritable = other.m_bWritable; m_bWritable = other.m_bWritable;
m_bHidden = other.m_bHidden; m_bHidden = other.m_bHidden;
if(other.m_bUseData)
{
createData();
*m_pData = *other.m_pData; *m_pData = *other.m_pData;
}
else
{
if(m_bUseData)
{
delete m_pData;
}
m_bUseData = false;
m_pParent = other.parent(); // should be 0 anyway
}
return *this; return *this;
} }
FileAccess::~FileAccess() FileAccess::~FileAccess()
{ {
if(m_bUseData)
{
if(!d()->m_localCopy.isEmpty()) if(!d()->m_localCopy.isEmpty())
{ {
removeTempFile(d()->m_localCopy); removeTempFile(d()->m_localCopy);
} }
if(m_pData != nullptr)
delete m_pData; delete m_pData;
}
} }
static QString nicePath(const QFileInfo& fi) static QString nicePath(const QFileInfo& fi)
...@@ -183,15 +168,9 @@ void FileAccess::setFile(const QFileInfo& fi, FileAccess* pParent) ...@@ -183,15 +168,9 @@ void FileAccess::setFile(const QFileInfo& fi, FileAccess* pParent)
m_filePath = pParent == nullptr ? fi.absoluteFilePath() : nicePath(fi.filePath()); // remove "./" at start m_filePath = pParent == nullptr ? fi.absoluteFilePath() : nicePath(fi.filePath()); // remove "./" at start
m_bSymLink = fi.isSymLink(); m_bSymLink = fi.isSymLink();
if(m_bSymLink)
{
createData(); createData();
}
if(m_bUseData)
d()->m_pParent = pParent; d()->m_pParent = pParent;
else
m_pParent = pParent;
if(parent() || d()) // if a parent is specified then we arrive here because of listing a directory if(parent() || d()) // if a parent is specified then we arrive here because of listing a directory
{ {
...@@ -209,7 +188,7 @@ void FileAccess::setFile(const QFileInfo& fi, FileAccess* pParent) ...@@ -209,7 +188,7 @@ void FileAccess::setFile(const QFileInfo& fi, FileAccess* pParent)
#endif #endif
} }
if(d() != nullptr) if(d()->m_url.isEmpty())
{ {
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
// On some windows machines in a network this takes very long. // On some windows machines in a network this takes very long.
...@@ -258,14 +237,8 @@ void FileAccess::setFile(const QString& name, bool bWantToWrite) ...@@ -258,14 +237,8 @@ void FileAccess::setFile(const QString& name, bool bWantToWrite)
m_size = 0; m_size = 0;
m_modificationTime = QDateTime(); m_modificationTime = QDateTime();
if(d() != nullptr) createData();
{
d()->reset(); d()->reset();
d()->m_pParent = nullptr;
}
else
m_pParent = nullptr;
// Note: Checking if the filename-string is empty is necessary for Win95/98/ME. // Note: Checking if the filename-string is empty is necessary for Win95/98/ME.
// The isFile() / isDir() queries would cause the program to crash. // The isFile() / isDir() queries would cause the program to crash.
// (This is a Win95-bug which has been corrected only in WinNT/2000/XP.) // (This is a Win95-bug which has been corrected only in WinNT/2000/XP.)
...@@ -310,7 +283,6 @@ void FileAccess::setFile(const QString& name, bool bWantToWrite) ...@@ -310,7 +283,6 @@ void FileAccess::setFile(const QString& name, bool bWantToWrite)
} }
else else
{ {
createData();
d()->m_url = url; d()->m_url = url;
d()->m_name = d()->m_url.fileName(); d()->m_name = d()->m_url.fileName();
d()->m_bLocal = false; d()->m_bLocal = false;
...@@ -328,7 +300,7 @@ void FileAccess::setFile(const QString& name, bool bWantToWrite) ...@@ -328,7 +300,7 @@ void FileAccess::setFile(const QString& name, bool bWantToWrite)
void FileAccess::addPath(const QString& txt) void FileAccess::addPath(const QString& txt)
{ {
if(d() != nullptr && d()->m_url.isValid()) if(d() != nullptr && d()->m_url.isEmpty() && d()->m_url.isValid())
{ {
QUrl url = d()->m_url.adjusted(QUrl::StripTrailingSlash); QUrl url = d()->m_url.adjusted(QUrl::StripTrailingSlash);
d()->m_url.setPath(url.path() + '/' + txt); d()->m_url.setPath(url.path() + '/' + txt);
...@@ -488,7 +460,7 @@ qint64 FileAccess::size() const ...@@ -488,7 +460,7 @@ qint64 FileAccess::size() const
QUrl FileAccess::url() const QUrl FileAccess::url() const
{ {
if(d() != nullptr) if(!m_filePath.isEmpty())
return d()->m_url; return d()->m_url;
else else
{ {
...@@ -512,7 +484,7 @@ bool FileAccess::isReadable() const ...@@ -512,7 +484,7 @@ bool FileAccess::isReadable() const
// On some windows machines in a network this takes very long to find out and it's not so important anyway. // On some windows machines in a network this takes very long to find out and it's not so important anyway.
return true; return true;
#else #else
if(d() != nullptr) if(!d()->m_url.isEmpty() && !d()->m_url.isLocalFile())
return d()->m_bReadable; return d()->m_bReadable;
else else
return QFileInfo(absoluteFilePath()).isReadable(); return QFileInfo(absoluteFilePath()).isReadable();
...@@ -521,7 +493,7 @@ bool FileAccess::isReadable() const ...@@ -521,7 +493,7 @@ bool FileAccess::isReadable() const
bool FileAccess::isWritable() const bool FileAccess::isWritable() const
{ {
if(parent() || d()) if(parent() || !(d()->m_url.isEmpty() || d()->m_url.isLocalFile()))
return m_bWritable; return m_bWritable;
else else
return QFileInfo(absoluteFilePath()).isWritable(); return QFileInfo(absoluteFilePath()).isWritable();
...@@ -533,7 +505,7 @@ bool FileAccess::isExecutable() const ...@@ -533,7 +505,7 @@ bool FileAccess::isExecutable() const
// On some windows machines in a network this takes very long to find out and it's not so important anyway. // On some windows machines in a network this takes very long to find out and it's not so important anyway.
return true; return true;
#else #else
if(d() != nullptr) if(!(d()->m_url.isEmpty() || d()->m_url.isLocalFile()))
return d()->m_bExecutable; return d()->m_bExecutable;
else else
return QFileInfo(absoluteFilePath()).isExecutable(); return QFileInfo(absoluteFilePath()).isExecutable();
...@@ -542,7 +514,7 @@ bool FileAccess::isExecutable() const ...@@ -542,7 +514,7 @@ bool FileAccess::isExecutable() const
bool FileAccess::isHidden() const bool FileAccess::isHidden() const
{ {
if(parent() || d()) if(parent() || !(d()->m_url.isEmpty() || d()->m_url.isLocalFile()))
return m_bHidden; return m_bHidden;
else else
return QFileInfo(absoluteFilePath()).isHidden(); return QFileInfo(absoluteFilePath()).isHidden();
...@@ -550,7 +522,7 @@ bool FileAccess::isHidden() const ...@@ -550,7 +522,7 @@ bool FileAccess::isHidden() const
QString FileAccess::readLink() const QString FileAccess::readLink() const
{ {
if(d() != nullptr) if(!(d()->m_url.isEmpty() || d()->m_url.isLocalFile()))
return d()->m_linkTarget; return d()->m_linkTarget;
else else
return QString(); return QString();
...@@ -579,7 +551,7 @@ QString FileAccess::absoluteFilePath() const ...@@ -579,7 +551,7 @@ QString FileAccess::absoluteFilePath() const
// Just the name-part of the path, without parent directories // Just the name-part of the path, without parent directories
QString FileAccess::fileName() const QString FileAccess::fileName() const
{ {
if(d() != nullptr) if(!(d()->m_url.isEmpty() || d()->m_url.isLocalFile()))
return d()->m_name; return d()->m_name;
else if(parent()) else if(parent())
return m_filePath; return m_filePath;
...@@ -603,26 +575,17 @@ QString FileAccess::filePath() const ...@@ -603,26 +575,17 @@ QString FileAccess::filePath() const
FileAccess* FileAccess::parent() const FileAccess* FileAccess::parent() const
{ {
if(m_bUseData)
return d()->m_pParent; return d()->m_pParent;
else
return m_pParent;
} }
FileAccess::Data* FileAccess::d() FileAccess::Data* FileAccess::d()
{ {
if(m_bUseData)
return m_pData; return m_pData;
else
return nullptr;
} }
const FileAccess::Data* FileAccess::d() const const FileAccess::Data* FileAccess::d() const
{ {
if(m_bUseData)
return m_pData; return m_pData;
else
return nullptr;
} }
QString FileAccess::prettyAbsPath() const QString FileAccess::prettyAbsPath() const
...@@ -687,7 +650,7 @@ static bool interruptableReadFile(QFile& f, void* pDestBuffer, qint64 maxLength) ...@@ -687,7 +650,7 @@ static bool interruptableReadFile(QFile& f, void* pDestBuffer, qint64 maxLength)
bool FileAccess::readFile(void* pDestBuffer, qint64 maxLength) bool FileAccess::readFile(void* pDestBuffer, qint64 maxLength)
{ {
if(d() != nullptr && !d()->m_localCopy.isEmpty()) if(!(d()->m_url.isEmpty() || d()->m_url.isLocalFile()) && !(d()->m_url.isEmpty() || d()->m_url.isLocalFile()) && !d()->m_localCopy.isEmpty())
{ {
QFile f(d()->m_localCopy); QFile f(d()->m_localCopy);
if(f.open(QIODevice::ReadOnly)) if(f.open(QIODevice::ReadOnly))
...@@ -878,11 +841,8 @@ QString FileAccess::getStatusText() ...@@ -878,11 +841,8 @@ QString FileAccess::getStatusText()
void FileAccess::setStatusText(const QString& s) void FileAccess::setStatusText(const QString& s)
{ {
if(!s.isEmpty() || d() != nullptr)
{
createData(); createData();
d()->m_statusText = s; d()->m_statusText = s;
}
} }
QString FileAccess::cleanPath(const QString& path) // static QString FileAccess::cleanPath(const QString& path) // static
......
...@@ -107,11 +107,8 @@ private: ...@@ -107,11 +107,8 @@ private:
const Data* d() const; const Data* d() const;
void createData(); void createData();
union Data* m_pData = nullptr;
{
Data* m_pData;
FileAccess* m_pParent;
};
QString m_filePath; // might be absolute or relative if m_pParent!=0 QString m_filePath; // might be absolute or relative if m_pParent!=0
qint64 m_size; qint64 m_size;
QDateTime m_modificationTime; QDateTime m_modificationTime;
...@@ -121,7 +118,7 @@ private: ...@@ -121,7 +118,7 @@ private:
bool m_bExists : 1; bool m_bExists : 1;
bool m_bWritable : 1; bool m_bWritable : 1;
bool m_bHidden : 1; bool m_bHidden : 1;
bool m_bUseData : 1; bool m_bReserved : 1;
friend class FileAccessJobHandler; friend class FileAccessJobHandler;
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment