forked from linuxdeepin/dde-file-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ulnfs] add to ulnfs Log: [ulnfs] add dlnfs to ulnfs Task: https://pms.uniontech.com/task-view-359649.html
- Loading branch information
1 parent
9353818
commit 85f818d
Showing
12 changed files
with
284 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#!/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.ulnfs` | ||
echo "dfm_INFO: ulnfs mount status: $ulnfs_enable" | ||
|
||
if [ "$ulnfs_enable" == "true" ]; then | ||
|
||
default_paths=`$query_dconfig dfm.mount.ulnfs.defaults` | ||
echo "dfm_INFO: default mount paths: $default_paths" | ||
|
||
formats='"[ " ]' | ||
for path in $default_paths | ||
do | ||
contains=$(echo $formats | grep -F "${path}") | ||
if [ "$contains" != "" ]; then | ||
continue | ||
fi | ||
|
||
# remove quotes and commas | ||
path=${path//'"'/} | ||
path=${path//','/} | ||
abs_path=$path | ||
|
||
env_path=`echo $path | grep -o '\$[^/]*'` # env_path = $HOME | ||
|
||
if [ "$env_path" != "" ]; then | ||
env_var=${env_path//'$'/} # env_var = HOME | ||
abs_env_path=`echo ${!env_var}` # abs_env_path = /home/$USER | ||
abs_path=${path/$env_path/$abs_env_path} #path = /home/xxxx/xxxx | ||
fi | ||
|
||
echo "dfm_INFO: ========================= $path [$abs_path]" | ||
if [ ! -d $abs_path ]; then | ||
echo "dfm_WARNING: $abs_path do not exist" | ||
continue | ||
fi | ||
|
||
gdbus call -y \ | ||
-d com.deepin.filemanager.daemon \ | ||
-o /com/deepin/filemanager/daemon/MountControl \ | ||
-m com.deepin.filemanager.daemon.MountControl.Mount \ | ||
"${abs_path}" \ | ||
"{'fsType': <'ulnfs'>}" \ | ||
-t 1 | ||
|
||
step_time=$(date "+%Y-%m-%d %H:%M:%S.%3N") | ||
echo -e "dfm_INFO: finished mount ${abs_path} at [${step_time}]\n" | ||
done | ||
|
||
fi | ||
|
||
end_time=$(date "+%Y-%m-%d %H:%M:%S.%3N") | ||
echo -e "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< finished mounting ulnfs at [${end_time}]\n" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
src/plugins/daemon/daemonplugin-mountcontrol/mounthelpers/ulnfsmounthelper.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
28 changes: 28 additions & 0 deletions
28
src/plugins/daemon/daemonplugin-mountcontrol/mounthelpers/ulnfsmounthelper.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |