Skip to content

Commit

Permalink
feat: [ulnfs] change dlnfs to ulnfs
Browse files Browse the repository at this point in the history
[ulnfs] change dlnfs to ulnfs

Log: [ulnfs] change dlnfs to ulnfs
  • Loading branch information
pppanghu77 authored and max-lvs committed Aug 30, 2024
1 parent 659e934 commit 10aaf1b
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 25 deletions.
13 changes: 1 addition & 12 deletions assets/configs/org.deepin.dde.file-manager.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
"permissions":"readwrite",
"visibility":"private"
},
"dfm.mount.dlnfs": {
"dfm.mount.lnfs": {
"value":false,
"serial":0,
"flags":[],
Expand All @@ -123,17 +123,6 @@
"permissions":"readwrite",
"visibility":"private"
},
"dfm.mount.dlnfs.defaults": {
"value":["$HOME/Desktop", "$HOME/Music", "$HOME/Pictures", "$HOME/Downloads", "$HOME/Videos", "$HOME/Documents"],
"serial":0,
"flags":[],
"name":"Dirs that support long file names",
"name[zh_CN]":"启用长文件名扩展的目录",
"description[zh_CN]":"默认启用长文件名扩展的目录",
"description":"Dirs that support long file names by default.",
"permissions":"readwrite",
"visibility":"private"
},
"dfm.trash.allfiletotrash": {
"value":false,
"serial":0,
Expand Down
7 changes: 7 additions & 0 deletions assets/scripts/99dfm-ulnfs-automount
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
#
# SPDX-License-Identifier: GPL-3.0-or-later

echo "\ndfm_INFO: start main script to mount ulnfs at" $(date "+%Y-%m-%d %H:%M:%S.%3N")
[ -f /etc/deepin/dde-file-manager/dfm-ulnfs-automount ] && bash /etc/deepin/dde-file-manager/dfm-ulnfs-automount &
echo "dfm_INFO: end main script at" $(date "+%Y-%m-%d %H:%M:%S.%3N")
32 changes: 32 additions & 0 deletions assets/scripts/dfm-ulnfs-automount
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

# SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
#
# SPDX-License-Identifier: GPL-3.0-or-later

start_time=$(date "+%Y-%m-%d %H:%M:%S.%3N")
echo -e "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> started mounting ulnfs at [${start_time}]"
echo "dfm_INFO: user: $USER, uid: `id -u`, home: $HOME"

query_dconfig="dde-dconfig --get -a org.deepin.dde.file-manager -r org.deepin.dde.file-manager -k "
# obtain the config of ulnfs mount enable
ulnfs_enable=`$query_dconfig dfm.mount.lnfs`
echo "dfm_INFO: ulnfs mount status: $ulnfs_enable"

if [ "$ulnfs_enable" == "true" ]; then
default_paths=("$HOME")
echo "dfm_INFO: default mount paths: $default_paths"
gdbus call -y \
-d com.deepin.filemanager.daemon \
-o /com/deepin/filemanager/daemon/MountControl \
-m com.deepin.filemanager.daemon.MountControl.Mount \
"${default_paths}" \
"{'fsType': <'ulnfs'>}" \
-t 1

step_time=$(date "+%Y-%m-%d %H:%M:%S.%3N")
echo -e "dfm_INFO: finished mount ${path} at [${step_time}]\n"
fi

end_time=$(date "+%Y-%m-%d %H:%M:%S.%3N")
echo -e "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< finished mounting ulnfs at [${end_time}]\n"
4 changes: 2 additions & 2 deletions debian/dde-file-manager.install
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ usr/share/qt5/doc
usr/lib/systemd/system/ lib/systemd/
usr/lib/systemd/user/
etc/dbus-1/system.d/com.deepin.filemanager.daemon.conf
etc/X11/Xsession.d/99dfm-dlnfs-automount
etc/deepin/dde-file-manager/dfm-dlnfs-automount
etc/X11/Xsession.d/99dfm-ulnfs-automount
etc/deepin/dde-file-manager/dfm-ulnfs-automount
12 changes: 6 additions & 6 deletions src/dfm-base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,16 @@ FILE(GLOB SCHEMA_FILES ${AssetsPath}/gschema/*)
install(FILES ${SCHEMA_FILES} DESTINATION share/glib-2.0/schemas)
install(CODE "execute_process(COMMAND glib-compile-schemas ${CMAKE_INSTALL_PREFIX}/share/glib-2.0/schemas)")

set(DLNFS_SCRIPT ${AssetsPath}/scripts/dfm-dlnfs-automount)
install(FILES ${DLNFS_SCRIPT} DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/deepin/dde-file-manager)
set(LNFS_SCRIPT ${AssetsPath}/scripts/dfm-ulnfs-automount)
install(FILES ${LNFS_SCRIPT} DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/deepin/dde-file-manager)

set(DLNFS_SCRIPT_LAUNCHER ${AssetsPath}/scripts/99dfm-dlnfs-automount)
install(FILES ${DLNFS_SCRIPT_LAUNCHER} DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/X11/Xsession.d)
set(LNFS_SCRIPT_LAUNCHER ${AssetsPath}/scripts/99dfm-ulnfs-automount)
install(FILES ${LNFS_SCRIPT_LAUNCHER} DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/X11/Xsession.d)

set(DFM_DLNFS_SCRIPT_LAUNCHER
set(DFM_LNFS_SCRIPT_LAUNCHER
${AssetsPath}/scripts/dde-file-manager
${AssetsPath}/scripts/file-manager.sh)
install(FILES ${DFM_DLNFS_SCRIPT_LAUNCHER} DESTINATION bin)
install(FILES ${DFM_LNFS_SCRIPT_LAUNCHER} DESTINATION bin)

set(Mimetypes "${ShareDir}/mimetypes")
FILE(GLOB MIMETYPE_FILES ${AssetsPath}/mimetypes/*)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ inline constexpr char kErrorMessage[] { "errMsg" };

namespace MountFstypeSupportedField {
inline constexpr char kDlnFs[] { "dlnfs" };
inline constexpr char kUlnFs[] { "ulnfs" };
inline constexpr char kCifs[] { "cifs" };
inline constexpr char kCommon[] { "common" };
}
Expand All @@ -51,8 +52,8 @@ enum MountErrorCode { // NOTE(xust): use NAGATIVE value when return, avoid con
kMountNotExist,
kNotOwnerOfMount,

kDlnFsProcessNotExists,
kDlnMountMounted,
kLnFsProcessNotExists,
kLnMountMounted,
kFusermountProcessNotExists,

kUnhandledError = 1000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "mounthelpers/cifsmounthelper.h"
#include "mounthelpers/dlnfsmounthelper.h"
#include "mounthelpers/commonmounthelper.h"
#include "mounthelpers/ulnfsmounthelper.h"

#include <QFile>

Expand All @@ -28,7 +29,7 @@ MountControlDBus::MountControlDBus(QObject *parent)
QDBusConnection::systemBus().registerObject(kMountControlObjPath, this);
}

MountControlDBus::~MountControlDBus() { }
MountControlDBus::~MountControlDBus() {}

QVariantMap MountControlDBus::Mount(const QString &path, const QVariantMap &opts)
{
Expand Down Expand Up @@ -73,10 +74,12 @@ MountControlDBusPrivate::MountControlDBusPrivate(MountControlDBus *qq)
{
CifsMountHelper *cifsHelper = new CifsMountHelper(qq);
DlnfsMountHelper *dlnfsHelper = new DlnfsMountHelper(qq);
UlnfsMountHelper *ulnfsHelper = new UlnfsMountHelper(qq);
CommonMountHelper *commonHelper = new CommonMountHelper(qq);

cifsHelper->cleanMountPoint();
mountHelpers.insert(MountFstypeSupportedField::kCifs, cifsHelper);
mountHelpers.insert(MountFstypeSupportedField::kUlnFs, ulnfsHelper);
mountHelpers.insert(MountFstypeSupportedField::kDlnFs, dlnfsHelper);
mountHelpers.insert(MountFstypeSupportedField::kCommon, commonHelper);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ QVariantMap DlnfsMountHelper::mount(const QString &path, const QVariantMap &opts
// 1. check if dlnfs at `path` is already exist.
if (checkDlnfsExist(path))
return { { kResult, true },
{ kErrorCode, -kDlnMountMounted },
{ kErrorCode, -kLnMountMounted },
{ kErrorMessage, QString("dlnfs is already mounted at %1").arg(path) } };

// 2. check `dlnfs` process exist.
if (QStandardPaths::findExecutable(kDlnfs).isEmpty())
return { { kResult, false },
{ kErrorCode, -kDlnFsProcessNotExists },
{ kErrorCode, -kLnFsProcessNotExists },
{ kErrorMessage, "dlnfs do not exist" } };

// 3. mount dlnfs on `path`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "ulnfsmounthelper.h"

#include <QDataStream>
#include <QDebug>
#include <QFile>
#include <QProcess>
#include <QStandardPaths>

#include <libmount/libmount.h>

DAEMONPMOUNTCONTROL_USE_NAMESPACE

QVariantMap UlnfsMountHelper::mount(const QString &path, const QVariantMap &opts)
{
Q_UNUSED(opts) // not used yet and might be used in the future.
using namespace MountReturnField;

static constexpr char kUlnfs[] { "ulnfs" };

// 1. check if ulnfs/dlnfs at `path` is already exist.
if (checkLnfsExist(path))
return { { kResult, true },
{ kErrorCode, -kLnMountMounted },
{ kErrorMessage, QString("lnfs is already mounted at %1").arg(path) } };

// 2. check `ulnfs` is loaded.
if (!isModuleLoaded(kUlnfs) && !loadKernelModule(kUlnfs))
return { { kResult, false },
{ kErrorCode, -kLnFsProcessNotExists },
{ kErrorMessage, QString("ulnfs is not loaded.") } };

// 3. mount ulnfs on `path`
auto aPath = path.toLocal8Bit().constData();
int ret = ::mount(aPath, aPath, kUlnfs, 0, "");

fmInfo() << "ulnfs: mount result: " << ret << aPath;
if (ret == 0) {
return { { kMountPoint, aPath }, { kResult, true }, { kErrorCode, 0 } };
} else {
int errNum = errno;
QString errMsg = strerror(errNum);
return { { kResult, false },
{ kErrorMessage, errMsg },
{ kErrorCode, parseErrorCodeByMsg(ret) } };
}
}

QVariantMap UlnfsMountHelper::unmount(const QString &path, const QVariantMap &opts)
{
Q_UNUSED(opts)
using namespace MountReturnField;

// 1. check if ulnfs is already mounted at `path`
if (!checkLnfsExist(path)) {
fmDebug() << "dlnfs: is not mounted at" << path;
return { { kResult, true },
{ kErrorCode, -kMountNotExist },
{ kErrorMessage, QString("dlnfs is not mounted at %1").arg(path) } };
}

// 2. do unmount ulnfs

int ret = ::umount(path.toStdString().c_str());
int err = errno;
QString errMsg = strerror(errno);
if (ret != 0)
fmWarning() << "unmount failed: " << path << err << errMsg;

return { { kResult, ret == 0 }, { kErrorCode, err }, { kErrorMessage, errMsg } };
}

bool UlnfsMountHelper::isModuleLoaded(const QString &moduleName)
{
QProcess process;
process.start("lsmod");
process.waitForFinished();

QString output = process.readAllStandardOutput();
QStringList lines = output.split('\n');

foreach (const QString &line, lines) {
if (line.startsWith(moduleName)) {
return true;
}
}
return false;
}

bool UlnfsMountHelper::loadKernelModule(const QString &moduleName)
{
QProcess modprobeProcess;

modprobeProcess.start("modprobe", QStringList() << moduleName);

if (!modprobeProcess.waitForFinished()) {
qCritical("Failed to load module: %s", qPrintable(modprobeProcess.errorString()));
return false;
}

QByteArray output = modprobeProcess.readAllStandardOutput();
qDebug() << moduleName << "Module loaded successfully.";
return true;
}

bool UlnfsMountHelper::checkLnfsExist(const QString &path)
{
QFile mountsFile("/proc/mounts");
if (!mountsFile.open(QIODevice::ReadOnly)) {
qWarning() << "Failed to open /proc/mounts.";
return false;
}

QByteArray content = mountsFile.readAll();
QString fileContent(content);

QStringList lines = fileContent.split('\n');
for (const QString &line : lines) {
QStringList parts = line.split(' ');

if (parts.size() >= 3) {
QString mountPoint = parts[1];
QString fsType = parts[2];

if (mountPoint == path && fsType == "ulnfs") {
return true;
}
}
}

return false;
}

int UlnfsMountHelper::parseErrorCodeByMsg(int res)
{
if (res == 0)
return 0;
return -kUnhandledError;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef UlnfsMountHelper_H
#define UlnfsMountHelper_H

#include "abstractmounthelper.h"

DAEMONPMOUNTCONTROL_BEGIN_NAMESPACE
class UlnfsMountHelper : public AbstractMountHelper
{
public:
explicit UlnfsMountHelper(QDBusContext *context)
: AbstractMountHelper(context) {}

virtual QVariantMap mount(const QString &path, const QVariantMap &opts) override;
virtual QVariantMap unmount(const QString &path, const QVariantMap &opts) override;

private:
bool loadKernelModule(const QString &moduleName);
bool checkLnfsExist(const QString &path);
bool isModuleLoaded(const QString &path);
int parseErrorCodeByMsg(int res);
};
DAEMONPMOUNTCONTROL_END_NAMESPACE

#endif // UlnfsMountHelper_H

0 comments on commit 10aaf1b

Please sign in to comment.