Skip to content
Permalink
Browse files

bugfix getFileSizeOnDisk(Windows) and refactoring.

  • Loading branch information...
haraki committed Jun 26, 2019
1 parent 0e9ea8a commit 77208217da5d062a3d1777d914dc18eceaeefa6d
Showing with 197 additions and 56 deletions.
  1. +7 −1 doublefolderpanel.cpp
  2. +7 −2 farman.pro
  3. +0 −33 file.cpp
  4. +0 −2 file.h
  5. +7 −1 fileattributesdialog.cpp
  6. +3 −17 foldermodel.cpp
  7. +16 −0 misc.h
  8. +66 −0 win32.cpp
  9. +28 −0 win32.h
  10. +37 −0 xnix.cpp
  11. +26 −0 xnix.h
@@ -17,6 +17,12 @@
#include "selectbookmarkdialog.h"
#include "file.h"

#ifdef Q_OS_WIN
# include "win32.h"
#else
# include "xnix.h"
#endif

namespace Farman
{

@@ -698,7 +704,7 @@ void DoubleFolderPanel::onAttributes()
file.permissions(),
file.fileTime(QFile::FileBirthTime),
file.fileTime(QFile::FileModificationTime),
File::getFileSizeOnDisk(filePath),
getFileSizeOnDisk(filePath),
parentWidget());
if(dialog.exec() != QDialog::Accepted)
{
@@ -34,6 +34,7 @@ SOURCES += main.cpp\
selectbookmarkdialog.cpp \
sortdialog.cpp \
filterdialog.cpp \
win32.cpp \
workingdialog.cpp \
worker.cpp \
copyworker.cpp \
@@ -52,7 +53,8 @@ SOURCES += main.cpp\
textview.cpp \
QHexView/src/QHexView.cpp \
hexviewer.cpp \
file.cpp
file.cpp \
xnix.cpp

HEADERS += mainwindow.h \
bookmarkeditdialog.h \
@@ -65,10 +67,12 @@ HEADERS += mainwindow.h \
foldermodel.h \
folderview.h \
doublefolderpanel.h \
misc.h \
selectbookmarkdialog.h \
types.h \
sortdialog.h \
filterdialog.h \
win32.h \
workingdialog.h \
worker.h \
copyworker.h \
@@ -88,7 +92,8 @@ HEADERS += mainwindow.h \
textview.h \
QHexView/include/QHexView.h \
hexviewer.h \
file.h
file.h \
xnix.h

FORMS += mainwindow.ui \
bookmarkeditdialog.ui \
@@ -8,12 +8,6 @@
#include "settings.h"
#include "mainwindow.h"

#ifdef Q_OS_WIN
# include <Windows.h>
#else
# include <sys/stat.h>
#endif

namespace Farman
{

@@ -27,33 +21,6 @@ File::~File()
{
}

qint64 File::getFileSizeOnDisk(const QString& filePath)
{
#ifdef Q_OS_WIN
DWORD fileSizeH, fileSizeL;
fileSizeL = ::GetCompressedFileSize(filePath.toStdWString().c_str(), &fileSizeH);
if(fileSizeL == static_cast<DWORD>(-1))
{
return -1;
}

qDebug() << "fileSizeH : " << fileSizeH << ", fileSizeL : " << fileSizeL;

return (static_cast<qint64>(fileSizeH) << 32) | fileSizeL;
#else
struct stat statBuf;

if(lstat(filePath.toUtf8(), &statBuf) == -1)
{
return -1;
}

qDebug() << "st_size : " << statBuf.st_size << ", st_blocks : " << statBuf.st_blocks;

return statBuf.st_blocks * 512;
#endif
}

bool File::copyFile(const QStringList& srcPaths, const QString& dstDirPath)
{
CopyWorker* copyWorker = new CopyWorker(srcPaths, dstDirPath, false);
2 file.h
@@ -18,8 +18,6 @@ class File : public QObject
explicit File(MainWindow* mainWindow);
~File();

static qint64 getFileSizeOnDisk(const QString& filePath);

Q_SIGNALS:
void createNewFileFinished(const QString& filePath);

@@ -29,11 +29,17 @@ FileAttributesDialog::FileAttributesDialog(const QFileInfo& fileInfo,
if(fileInfo.isDir())
{
ui->informationFileSizeLabel->setText("- ");
ui->informationSizeOnDiskLabel->setText("- ");
}
else
{
ui->informationFileSizeLabel->setText(QString("%L1 ").arg(fileInfo.size()) + tr("Bytes"));
}
if(fileInfo.isDir() || fileSizeOnDisk < 0)
{
ui->informationSizeOnDiskLabel->setText("- ");
}
else
{
ui->informationSizeOnDiskLabel->setText(QString("%L1 ").arg(fileSizeOnDisk) + tr("Bytes"));
}

@@ -8,10 +8,7 @@
#include "foldermodel.h"
#include "settings.h"
#include "types.h"

#ifdef Q_OS_WIN
# include <Windows.h>
#endif
#include "win32.h"

namespace Farman
{
@@ -508,7 +505,7 @@ bool FolderModel::filterAcceptsRow(int source_row, const QModelIndex &source_par
}

#ifdef Q_OS_WIN
if(!(m_filterFlags & FilterFlag::System) && isWindowsSystemFile(cfi))
if(!(m_filterFlags & FilterFlag::System) && Win32::isSystemFile(cfi))
{
return false;
}
@@ -667,17 +664,6 @@ bool FolderModel::isDrive(const QModelIndex& index) const

return false;
}

bool FolderModel::isWindowsSystemFile(const QFileInfo& fileInfo) const
{
DWORD attrFlags = ::GetFileAttributes(fileInfo.filePath().toStdWString().c_str());
if(attrFlags != static_cast<DWORD>(-1) && (attrFlags & FILE_ATTRIBUTE_SYSTEM))
{
return true;
}

return false;
}
#endif

SectionType FolderModel::getSectionTypeFromColumn(int column) const
@@ -744,7 +730,7 @@ QBrush FolderModel::getTextBrush(const QModelIndex& index) const
}
}
#ifdef Q_OS_WIN
else if(fi.fileName() != ".." && isWindowsSystemFile(fi))
else if(fi.fileName() != ".." && isSystemFile(fi))
{
if(selected)
{
16 misc.h
@@ -0,0 +1,16 @@
#ifndef MISC_H
#define MISC_H

#include <QtGlobal>

namespace Farman
{

template <class T> T RoundUp(T n, quint64 m)
{
return (n / m) * m + (((n % m) != 0) ? m : 0);
}

} // namespace Farman

#endif // MISC_H
@@ -0,0 +1,66 @@
#include <QtGlobal>

#ifdef Q_OS_WIN

#include <Windows.h>
#include <QFileInfo>
#include <QStorageInfo>
#include <QDebug>
#include "win32.h"
#include "misc.h"

namespace Farman
{

namespace Win32
{

bool isSystemFile(const QFileInfo& fileInfo)
{
DWORD attrFlags = ::GetFileAttributes(fileInfo.filePath().toStdWString().c_str());
if(attrFlags != static_cast<DWORD>(-1) && (attrFlags & FILE_ATTRIBUTE_SYSTEM))
{
return true;
}

return false;
}

qint64 getFileSizeOnDisk(const QString& filePath)
{
DWORD sectorsPerCluster = 0;
DWORD bytesPerSector = 0;
DWORD freeClusters = 0;
DWORD totalClusters = 0;

if(!::GetDiskFreeSpace(QStorageInfo(filePath).rootPath().toStdWString().c_str(),
&sectorsPerCluster,
&bytesPerSector,
&freeClusters,
&totalClusters))
{
qDebug() << "GetDiskFreeSpace() error : " << ::GetLastError() << endl;
return -1;
}

DWORD clusterSize = sectorsPerCluster * bytesPerSector;

DWORD fileSizeH, fileSizeL;
fileSizeL = ::GetCompressedFileSize(filePath.toStdWString().c_str(), &fileSizeH);
if(fileSizeL == static_cast<DWORD>(-1))
{
return -1;
}

qint64 sizeOnDisk = RoundUp((static_cast<qint64>(fileSizeH) << 32) | fileSizeL, clusterSize);

qDebug() << "clusterSize : " << clusterSize << ", fileSizeH : " << fileSizeH << ", fileSizeL : " << fileSizeL << ", sizeOnDisk : " << sizeOnDisk;

return sizeOnDisk;
}

} // namespace Win32

} // namespace Farman

#endif
28 win32.h
@@ -0,0 +1,28 @@
#ifndef WIN32_H
#define WIN32_H

#include <QtGlobal>

#ifdef Q_OS_WIN

#include <QFileInfo>
#include <QString>

namespace Farman
{

namespace Win32
{

extern bool isSystemFile(const QFileInfo& fileInfo);
extern qint64 getFileSizeOnDisk(const QString& filePath);

} // namespace Win32

} // namespace Farman

using namespace Farman::Win32;

#endif

#endif // WIN32_H
@@ -0,0 +1,37 @@
#include <QtGlobal>

#ifndef Q_OS_WIN

#include <sys/stat.h>
#include <QFileInfo>
#include <QDebug>
#include "xnix.h"

namespace Farman
{

namespace Xnix
{

qint64 getFileSizeOnDisk(const QString& filePath)
{
struct stat statBuf;

if(lstat(filePath.toUtf8(), &statBuf) == -1)
{
return -1;
}

qDebug() << "st_size : " << statBuf.st_size << ", st_blocks : " << statBuf.st_blocks;

return statBuf.st_blocks * 512;
}

} // namespace Xnix

} // namespace Farman

#endif



26 xnix.h
@@ -0,0 +1,26 @@
#ifndef XNIX_H
#define XNIX_H

#include <QtGlobal>

#ifndef Q_OS_WIN

#include <QString>

namespace Farman
{

namespace Xnix
{

extern qint64 getFileSizeOnDisk(const QString& filePath);

} // namespace Xnix

} // namespace Farman

using namespace Farman::Xnix;

#endif

#endif // XNIX_H

0 comments on commit 7720821

Please sign in to comment.
You can’t perform that action at this time.