diff --git a/src/fsviewmodel.cpp b/src/fsviewmodel.cpp index 6db4b1a..5bd9a6a 100644 --- a/src/fsviewmodel.cpp +++ b/src/fsviewmodel.cpp @@ -96,6 +96,11 @@ int Ext4FSViewModel::fs_remove_dir(QString path) { return 0; } +int Ext4FSViewModel::fs_remove_file(QString path) { + ext4_fremove(path.toStdString().c_str()); + return 0; +} + void Ext4FSViewModel::listFSAll(QString path, QModelIndex index) { const ext4_direntry *de; ext4_dir d; @@ -218,6 +223,11 @@ int FatFSFSViewModel::fs_remove_dir(QString path) { return 0; } +int FatFSFSViewModel::fs_remove_file(QString path) { + f_unlink(path.toStdString().c_str()); + return 0; +} + void FatFSFSViewModel::listFSAll(QString path, QModelIndex index) { FRESULT res; DIR dir; @@ -383,6 +393,12 @@ int Jffs2FSViewModel::fs_create_dir(QString path) { } int Jffs2FSViewModel::fs_remove_dir(QString path) { + Q_UNUSED(path); + return -1; +} + +int Jffs2FSViewModel::fs_remove_file(QString path) { + Q_UNUSED(path); return -1; } diff --git a/src/fsviewmodel.h b/src/fsviewmodel.h index 7c4d19b..d89a678 100644 --- a/src/fsviewmodel.h +++ b/src/fsviewmodel.h @@ -117,6 +117,21 @@ class FSViewModel return ret; } + int removeFileFSImg(QString dir_path) { + QFile fs_img(rootFSImgPath); + if(!fs_img.open(QIODevice::ReadWrite)) { + qWarning() << "open fs img failed"; + return -1; + } + uint8_t *addr = fs_img.map(offset,size); + fs_init(addr,size,false); + int ret = fs_remove_file(dir_path); + fs_deinit(addr,size,false); + fs_img.unmap(addr); + fs_img.close(); + return ret; + } + private: virtual int check_fs(uint8_t *addr) { Q_UNUSED(addr); return -1; } @@ -126,6 +141,7 @@ class FSViewModel virtual int fs_read_file(QString output, QFile &input) { Q_UNUSED(input); Q_UNUSED(output); return -1; } virtual int fs_create_dir(QString path) { Q_UNUSED(path); return -1; } virtual int fs_remove_dir(QString path) { Q_UNUSED(path); return -1; } + virtual int fs_remove_file(QString path) { Q_UNUSED(path); return -1; } virtual void listFSAll(QString path, QModelIndex index) { Q_UNUSED(path); Q_UNUSED(index); return; } public: @@ -150,6 +166,7 @@ class Ext4FSViewModel : public FSViewModel int fs_read_file(QString output, QFile &input) override; int fs_create_dir(QString path) override; int fs_remove_dir(QString path) override; + int fs_remove_file(QString path) override; void listFSAll(QString path, QModelIndex index) override; }; @@ -167,6 +184,7 @@ class FatFSFSViewModel : public FSViewModel int fs_read_file(QString output, QFile &input) override; int fs_create_dir(QString path) override; int fs_remove_dir(QString path) override; + int fs_remove_file(QString path) override; void listFSAll(QString path, QModelIndex index) override; private: @@ -187,6 +205,7 @@ class Jffs2FSViewModel : public FSViewModel int fs_read_file(QString output, QFile &input) override; int fs_create_dir(QString path) override; int fs_remove_dir(QString path) override; + int fs_remove_file(QString path) override; void listFSAll(QString path, QModelIndex index) override; }; diff --git a/src/qfsviewer.h b/src/qfsviewer.h index 353ce84..92eaaac 100644 --- a/src/qfsviewer.h +++ b/src/qfsviewer.h @@ -177,8 +177,9 @@ class FSViewWindow : public QTreeView QModelIndex tIndex = indexAt(viewport()->mapFromGlobal(event->globalPos())); if (tIndex.isValid() && m_idle) { int type = FSViewModel::FSView_UNKNOWN; + uint64_t size = 0; QString name; - mode->info(tIndex, type, name); + mode->info(tIndex, type, name, size); if((type == FSViewModel::FSView_REG_FILE) || (type == FSViewModel::FSView_DIR)) { //TODO: why this way crash? //QMenu *contextMenu = new QMenu(this); @@ -194,15 +195,17 @@ class FSViewWindow : public QTreeView [&,tIndex](void) { QString name; + uint64_t size = 0; int type = FSViewModel::FSView_UNKNOWN; - mode->info(tIndex, type, name); + mode->info(tIndex, type, name, size); QString path = name; std::function get_parent = [&](QModelIndex index, QString &name) -> QModelIndex { if(index.isValid() && index.parent().isValid()) { QString pname; + uint64_t size = 0; int type = FSViewModel::FSView_UNKNOWN; QModelIndex parent = index.parent(); - mode->info(parent, type, pname); + mode->info(parent, type, pname, size); name = (pname == "/")?(pname + name):(pname + "/" + name); return get_parent(parent, name); } else { @@ -247,15 +250,17 @@ class FSViewWindow : public QTreeView } QString name; + uint64_t size = 0; int type = FSViewModel::FSView_UNKNOWN; - mode->info(tIndex, type, name); + mode->info(tIndex, type, name, size); QString path = name; std::function get_parent = [&](QModelIndex index, QString &name) -> QModelIndex { if(index.isValid() && index.parent().isValid()) { QString pname; + uint64_t size = 0; int type = FSViewModel::FSView_UNKNOWN; QModelIndex parent = index.parent(); - mode->info(parent, type, pname); + mode->info(parent, type, pname, size); name = (pname == "/")?(pname + name):(pname + "/" + name); return get_parent(parent, name); } else { @@ -301,10 +306,10 @@ class FSViewWindow : public QTreeView } ); - QAction *pNew_Dir= new QAction(tr("New Dir"), this); - pNew_Dir->setIcon(QIcon(QFontIcon::icon(QChar(0xf0f6)))); - contextMenu->addAction(pNew_Dir); - connect(pNew_Dir,&QAction::triggered,this, + QAction *pCreate= new QAction(tr("Create"), this); + pCreate->setIcon(QIcon(QFontIcon::icon(QChar(0xf0f6)))); + contextMenu->addAction(pCreate); + connect(pCreate,&QAction::triggered,this, [&,tIndex](void) { int wret = QMessageBox::warning(this,"Warning","In principle, this software does not provide the function of modifying the disk image. If you use this function, please remember to back up your files, and this software does not guarantee the strict correctness of the import.\nPlease choose whether to continue.", QMessageBox::Yes, QMessageBox::No); @@ -313,15 +318,17 @@ class FSViewWindow : public QTreeView } QString name; + uint64_t size = 0; int type = FSViewModel::FSView_UNKNOWN; - mode->info(tIndex, type, name); + mode->info(tIndex, type, name, size); QString path = name; std::function get_parent = [&](QModelIndex index, QString &name) -> QModelIndex { if(index.isValid() && index.parent().isValid()) { QString pname; + uint64_t size = 0; int type = FSViewModel::FSView_UNKNOWN; QModelIndex parent = index.parent(); - mode->info(parent, type, pname); + mode->info(parent, type, pname, size); name = (pname == "/")?(pname + name):(pname + "/" + name); return get_parent(parent, name); } else { @@ -336,9 +343,14 @@ class FSViewWindow : public QTreeView path.replace("C:/",""); #endif } - QString fileName = QInputDialog::getText(this, tr("Enter Dir Name"), tr("Name")); - if (fileName.isEmpty()) + bool isOK = false; + QString fileName = QInputDialog::getText(this, tr("Enter Dir Name"), tr("Name"), QLineEdit::Normal, "", &isOK); + if (fileName.isEmpty()) { + if(isOK) { + QMessageBox::critical(this, tr("Error"), tr("Can't create dir!")); + } return; + } path = (path=="/")?(path+fileName):(path+"/"+fileName); QFileInfo info(this->windowTitle()); int ret = -1; @@ -358,10 +370,10 @@ class FSViewWindow : public QTreeView } } ); - QAction *pDel_Dir= new QAction(tr("Del Dir"), this); - pDel_Dir->setIcon(QIcon(QFontIcon::icon(QChar(0xf014)))); - contextMenu->addAction(pDel_Dir); - connect(pDel_Dir,&QAction::triggered,this, + QAction *pDelete= new QAction(tr("Delete"), this); + pDelete->setIcon(QIcon(QFontIcon::icon(QChar(0xf014)))); + contextMenu->addAction(pDelete); + connect(pDelete,&QAction::triggered,this, [&,tIndex](void) { int wret = QMessageBox::warning(this,"Warning","In principle, this software does not provide the function of modifying the disk image. If you use this function, please remember to back up your files, and this software does not guarantee the strict correctness of the import.\nPlease choose whether to continue.", QMessageBox::Yes, QMessageBox::No); @@ -370,19 +382,25 @@ class FSViewWindow : public QTreeView } QString name; + uint64_t size = 0; int type = FSViewModel::FSView_UNKNOWN; - mode->info(tIndex, type, name); - if(type != FSViewModel::FSView_DIR) { + mode->info(tIndex, type, name, size); + if(type != FSViewModel::FSView_DIR && type != FSViewModel::FSView_REG_FILE) { QMessageBox::critical(this, tr("Error"), tr("Unsupported operation!")); return; } + if(size != 0) { + QMessageBox::critical(this, tr("Error"), tr("Now only support delete empty dir!")); + return; + } QString path = name; std::function get_parent = [&](QModelIndex index, QString &name) -> QModelIndex { if(index.isValid() && index.parent().isValid()) { QString pname; + uint64_t size = 0; int type = FSViewModel::FSView_UNKNOWN; QModelIndex parent = index.parent(); - mode->info(parent, type, pname); + mode->info(parent, type, pname, size); name = (pname == "/")?(pname + name):(pname + "/" + name); return get_parent(parent, name); } else { @@ -393,7 +411,11 @@ class FSViewWindow : public QTreeView QFileInfo info(this->windowTitle()); int ret = -1; if(fsView) { - ret = fsView->removeDirFSImg(path); + if(type != FSViewModel::FSView_DIR ) { + ret = fsView->removeDirFSImg(path); + } else if(type != FSViewModel::FSView_REG_FILE) { + ret = fsView->removeFileFSImg(path); + } if(ret == 0) { resetView(); m_idle = false; diff --git a/src/treemodel.cpp b/src/treemodel.cpp index 0fbd01b..8356820 100644 --- a/src/treemodel.cpp +++ b/src/treemodel.cpp @@ -91,18 +91,7 @@ QVariant TreeModel::data(const QModelIndex &index, int role) const if ( !index.isValid() ) { return QVariant() ; } TreeItem *p = static_cast(index.internalPointer()) ; - - enum fs_entity_type { - UNKNOWN = 0, - REG_FILE, - DIR, - CHARDEV, - BLOCKDEV, - FIFO, - SOCKET, - SYMLINK, - LAST - }; + struct fs_index { int type; int column; @@ -181,7 +170,7 @@ QVariant TreeModel::data(const QModelIndex &index, int role) const return QVariant(); } -void TreeModel::info(const QModelIndex &index, int &type, QString &name) +void TreeModel::info(const QModelIndex &index, int &type, QString &name, uint64_t &size) { if ( !index.isValid() ) { return; } @@ -189,6 +178,13 @@ void TreeModel::info(const QModelIndex &index, int &type, QString &name) type = p->type(); name = p->data(); + if(type == REG_FILE) { + size = p->size(); + } else if(type == DIR) { + size = p->childCount(); + } else { + size = 0; + } } int TreeModel::rowCount(const QModelIndex &parent) const diff --git a/src/treemodel.h b/src/treemodel.h index 1b3a3d6..b68bb3e 100644 --- a/src/treemodel.h +++ b/src/treemodel.h @@ -36,11 +36,22 @@ class TreeModel : public QAbstractItemModel QModelIndex addTree(QString str, int type, uint64_t size, uint32_t timestamp, const QModelIndex &parent) ; void removeTree(QModelIndex &parent) ; - void info(const QModelIndex &index, int &type, QString &name); + void info(const QModelIndex &index, int &type, QString &name, uint64_t &size); void dumpTreeItems() ; private: + enum fs_entity_type { + UNKNOWN = 0, + REG_FILE, + DIR, + CHARDEV, + BLOCKDEV, + FIFO, + SOCKET, + SYMLINK, + LAST + }; void _dump(TreeItem *p, int tab) ; signals: