Skip to content

Commit

Permalink
feat: add the permission management for files
Browse files Browse the repository at this point in the history
Use DCapManager class to unify privilege management on
whether to open files or not.

By registering a special file engine, the scope of action
after registration is global unless called the unregister
function manually.

By default, the application can still access some directories
and files with permissions after it is started, but when it
access files beyond the scope of these directories, it will
not get any information.

The default directory includes: Home path; Cache path; DSG path
XDG path etc.

Log:
Influence: null
Change-Id: I0fed6051addc56001b382fc34f73b046c0e4aca2
  • Loading branch information
Chen Bin authored and kegechen committed Aug 26, 2022
1 parent 066f34e commit 9cdc9e7
Show file tree
Hide file tree
Showing 14 changed files with 1,336 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/DtkCore/DCapDir
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "dcapfile.h"
1 change: 1 addition & 0 deletions include/DtkCore/DCapFile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "dcapfile.h"
1 change: 1 addition & 0 deletions include/DtkCore/DCapManager
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "dcapmanager.h"
105 changes: 105 additions & 0 deletions include/filesystem/dcapfile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#ifndef DCAPFILE_H
#define DCAPFILE_H

#include <dtkcore_global.h>

#include <DObject>
#include <QDir>
#include <QFile>

DCORE_BEGIN_NAMESPACE

class DCapFilePrivate;
class DCapFile : public QFile, public DObject
{
Q_OBJECT
D_DECLARE_PRIVATE(DCapFile)
Q_DISABLE_COPY(DCapFile)

public:
explicit DCapFile(QObject *parent = nullptr);
DCapFile(const QString &name, QObject *parent = nullptr);

~DCapFile() override;

void setFileName(const QString &name);

bool exists() const;
static bool exists(const QString &fileName);

#if QT_DEPRECATED_SINCE(5, 13)
D_DECL_DEPRECATED_X("Use QFile::symLinkTarget() instead")
QString readLink() const;
#endif
#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
QString symLinkTarget() const;
#endif
bool remove();
static bool remove(const QString &fileName);

#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
bool moveToTrash();
static bool moveToTrash(const QString &fileName, QString *pathInTrash = nullptr);
#endif

bool rename(const QString &newName);
static bool rename(const QString &oldName, const QString &newName);

bool link(const QString &newName);
static bool link(const QString &oldname, const QString &newName);

bool copy(const QString &newName);
static bool copy(const QString &fileName, const QString &newName);

bool open(OpenMode flags) override;

bool resize(qint64 sz) override;
static bool resize(const QString &filename, qint64 sz);

private:
bool open(FILE *f, OpenMode ioFlags, FileHandleFlags handleFlags=DontCloseHandle);
bool open(int fd, OpenMode ioFlags, FileHandleFlags handleFlags=DontCloseHandle);
};

class DCapDirPrivate;
class DCapDir : public QDir
{
public:
DCapDir(const DCapDir &);
DCapDir(const QString &path = QString());
DCapDir(const QString &path, const QString &nameFilter,
SortFlags sort = SortFlags(Name | IgnoreCase), Filters filter = AllEntries);
~DCapDir();

void setPath(const QString &path);

bool cd(const QString &dirName);

QStringList entryList(Filters filters = NoFilter, SortFlags sort = NoSort) const;
QStringList entryList(const QStringList &nameFilters, Filters filters = NoFilter,
SortFlags sort = NoSort) const;

QFileInfoList entryInfoList(Filters filters = NoFilter, SortFlags sort = NoSort) const;
QFileInfoList entryInfoList(const QStringList &nameFilters, Filters filters = NoFilter,
SortFlags sort = NoSort) const;

bool mkdir(const QString &dirName) const;
bool rmdir(const QString &dirName) const;
bool mkpath(const QString &dirPath) const;
bool rmpath(const QString &dirPath) const;
bool exists() const;
bool remove(const QString &fileName);
bool rename(const QString &oldName, const QString &newName);
bool exists(const QString &name) const;

private:
QSharedDataPointer<DCapDirPrivate> dd_ptr;
};

DCORE_END_NAMESPACE
Q_DECLARE_SHARED(DTK_CORE_NAMESPACE::DCapDir)
#endif // DCAPFILE_H
37 changes: 37 additions & 0 deletions include/filesystem/dcapmanager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#ifndef DCAPMANAGER_H
#define DCAPMANAGER_H

#include <DObject>

#include <QObject>

DCORE_BEGIN_NAMESPACE
class DCapManagerPrivate;
class DCapManager : public QObject, public DObject
{
Q_OBJECT
Q_DISABLE_COPY(DCapManager)
D_DECLARE_PRIVATE(DCapManager)
public:
static DCapManager *instance();
static void registerFileEngine();
static void unregisterFileEngine();

void appendPath(const QString &path);
void appendPaths(const QStringList &pathList);

void removePath(const QString &path);
void removePaths(const QStringList &paths);

QStringList paths() const;

protected:
explicit DCapManager();
};

DCORE_END_NAMESPACE
#endif // DCAPMANAGER_H
Loading

0 comments on commit 9cdc9e7

Please sign in to comment.