Skip to content

Commit

Permalink
src:add delete file feature
Browse files Browse the repository at this point in the history
Signed-off-by: xiaoming <[email protected]>
  • Loading branch information
QQxiaoming committed Aug 7, 2023
1 parent 7ff89a7 commit 96790f4
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 35 deletions.
16 changes: 16 additions & 0 deletions src/fsviewmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
19 changes: 19 additions & 0 deletions src/fsviewmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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:
Expand All @@ -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;
};

Expand All @@ -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:
Expand All @@ -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;
};

Expand Down
64 changes: 43 additions & 21 deletions src/qfsviewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<QModelIndex(QModelIndex,QString &)> 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 {
Expand Down Expand Up @@ -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<QModelIndex(QModelIndex,QString &)> 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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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<QModelIndex(QModelIndex,QString &)> 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 {
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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<QModelIndex(QModelIndex,QString &)> 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 {
Expand All @@ -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;
Expand Down
22 changes: 9 additions & 13 deletions src/treemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,7 @@ QVariant TreeModel::data(const QModelIndex &index, int role) const
if ( !index.isValid() ) { return QVariant() ; }

TreeItem *p = static_cast<TreeItem *>(index.internalPointer()) ;

enum fs_entity_type {
UNKNOWN = 0,
REG_FILE,
DIR,
CHARDEV,
BLOCKDEV,
FIFO,
SOCKET,
SYMLINK,
LAST
};

struct fs_index {
int type;
int column;
Expand Down Expand Up @@ -181,14 +170,21 @@ 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; }

TreeItem *p = static_cast<TreeItem *>(index.internalPointer()) ;

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
Expand Down
13 changes: 12 additions & 1 deletion src/treemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 96790f4

Please sign in to comment.