88
99#include " linglong/api/types/v1/Generators.hpp"
1010#include " linglong/api/types/v1/LayerInfo.hpp"
11- #include " linglong/utils/command/ cmd.h"
12- #include " linglong/utils/command/env .h"
11+ #include " linglong/utils/cmd.h"
12+ #include " linglong/utils/file .h"
1313#include " linglong/utils/log/log.h"
1414
1515#include < QDataStream>
@@ -28,8 +28,7 @@ LayerPackager::LayerPackager()
2828 // maybe refactor on later
2929 auto ret = this ->initWorkDir ();
3030 if (!ret) {
31- qCritical () << " init work dir failed" ;
32- Q_ASSERT (false );
31+ LogE (" init work dir failed" );
3332 }
3433}
3534
@@ -48,16 +47,15 @@ utils::error::Result<void> LayerPackager::initWorkDir()
4847 // 优先使用环境变量LINGLONG_TMPDIR指定的目录,默认为/var/tmp,避免/tmp是tmpfs内存不足
4948 auto uuid = QUuid::createUuid ().toString (QUuid::Id128);
5049 auto dirName = " linglong-layer-workdir-" + uuid.toStdString ();
51- auto tmpDir = utils::command::getEnv (" LINGLONG_TMPDIR" );
52- auto dirPath = std::filesystem::path (tmpDir. value_or ( " /var/tmp" ) ) / dirName;
50+ auto * tmpDir = std::getenv (" LINGLONG_TMPDIR" );
51+ auto dirPath = std::filesystem::path (tmpDir ? tmpDir : " /var/tmp" ) / dirName;
5352 auto ret = this ->mkdirDir (dirPath);
5453 if (!ret.has_value ()) {
5554 // 如果/var/tmp目录无权限创建,则使用临时目录
5655 dirPath = std::filesystem::temp_directory_path () / dirName;
5756 ret = this ->mkdirDir (dirPath);
5857 if (!ret) {
59- qCritical () << " failed to set work dir" << ret.error ().message ();
60- Q_ASSERT (false );
58+ LogE (" failed to set work dir: {}" , ret.error ());
6159 }
6260 }
6361 this ->workDir = dirPath;
@@ -84,16 +82,15 @@ utils::error::Result<void> LayerPackager::mkdirDir(const std::string &path) noex
8482LayerPackager::~LayerPackager ()
8583{
8684 if (this ->isMounted ) {
87- auto ret = utils::command:: Cmd (" fusermount" )
85+ auto ret = utils::Cmd (" fusermount" )
8886 .exec ({ " -z" , " -u" , (this ->workDir / " unpack" ).string ().c_str () });
8987 if (!ret) {
90- qWarning () << " failed to umount " << ( this -> workDir / " unpack " ). c_str ()
91- << " , please umount it manually " ;
88+ LogW ( " failed to umount {}, please umount it manually " ,
89+ ( this -> workDir / " unpack " ). string ()) ;
9290 }
9391 }
9492 if (!std::filesystem::remove_all (this ->workDir )) {
9593 LogE (" failed to remove {}" , this ->workDir );
96- Q_ASSERT (false );
9794 }
9895}
9996
@@ -151,23 +148,21 @@ LayerPackager::pack(const LayerDir &dir, const QString &layerFilePath) const
151148
152149 // compress data with erofs
153150 const auto &compressedFilePath = this ->workDir / " tmp.erofs" ;
154- const auto &ignoreRegex = QString{ " --exclude-regex=minified*" };
155151 // 使用-b统一指定block size为4096(2^12), 避免不同系统的兼容问题
156152 // loongarch64默认使用(16384)2^14, 在x86和arm64不受支持, 会导致无法推包
157- auto ret = utils::command:: Cmd (" mkfs.erofs" )
158- .exec ({ " -z" + compressor,
159- " -b4096" ,
160- compressedFilePath.string (). c_str (),
161- ignoreRegex ,
162- dir.absolutePath () });
153+ auto ret = utils::Cmd (" mkfs.erofs" )
154+ .exec (std::vector<std::string> { " -z" + compressor. toStdString () ,
155+ " -b4096" ,
156+ compressedFilePath.string (),
157+ " --exclude-regex=minified* " ,
158+ dir.absolutePath (). toStdString () });
163159 if (!ret) {
164160 return LINGLONG_ERR (ret);
165161 }
166162
167- ret = utils::command::Cmd (" sh" ).exec (
168- { " -c" , QString (" cat %1 >> %2" ).arg (compressedFilePath.string ().c_str (), layerFilePath) });
169- if (!ret) {
170- LINGLONG_ERR (ret);
163+ auto res = utils::concatFile (compressedFilePath, layerFilePath.toStdString ());
164+ if (!res) {
165+ return LINGLONG_ERR (res);
171166 }
172167
173168 auto result = LayerFile::New (layerFilePath);
@@ -242,8 +237,10 @@ utils::error::Result<LayerDir> LayerPackager::unpack(LayerFile &file)
242237 }
243238 fuseOffset = " 0" ;
244239 }
245- auto ret = utils::command::Cmd (" erofsfuse" )
246- .exec ({ " --offset=" + fuseOffset, fdPath, unpackDir.absolutePath () });
240+ auto ret = utils::Cmd (" erofsfuse" )
241+ .exec ({ " --offset=" + fuseOffset.toStdString (),
242+ fdPath.toStdString (),
243+ unpackDir.absolutePath ().toStdString () });
247244 if (!ret) {
248245 return LINGLONG_ERR (ret);
249246 }
@@ -252,15 +249,16 @@ utils::error::Result<LayerDir> LayerPackager::unpack(LayerFile &file)
252249 }
253250 // 判断fsck.erofs命令是否存在,fsck.erofs是erofs-utils的命令,可用于解压erofs文件
254251 // 在旧版本中fsck.erofs不支持offset参数,所以需要提前将erofs文件复制到临时目录
255- auto erofsFscExistsRet = utils::command:: Cmd (" fsck.erofs" ).exists ();
252+ auto erofsFscExistsRet = utils::Cmd (" fsck.erofs" ).exists ();
256253 if (erofsFscExistsRet) {
257254 fdPath = (this ->workDir / " layer.erofs" ).string ().c_str ();
258255 auto ret = this ->copyFile (file, fdPath.toStdString (), *offset);
259256 if (!ret) {
260257 return LINGLONG_ERR (ret);
261258 }
262- auto cmdRet = utils::command::Cmd (" fsck.erofs" )
263- .exec ({ " --extract=" + unpackDir.absolutePath (), fdPath });
259+ auto cmdRet =
260+ utils::Cmd (" fsck.erofs" )
261+ .exec ({ " --extract=" + unpackDir.absolutePath ().toStdString (), fdPath.toStdString () });
264262 if (!cmdRet) {
265263 return LINGLONG_ERR (cmdRet);
266264 }
@@ -278,7 +276,7 @@ void LayerPackager::setCompressor(const QString &compressor) noexcept
278276
279277utils::error::Result<bool > LayerPackager::checkErofsFuseExists () const
280278{
281- return utils::command:: Cmd (" erofsfuse" ).exists ();
279+ return utils::Cmd (" erofsfuse" ).exists ();
282280}
283281
284282} // namespace linglong::package
0 commit comments