我有一个基于 linux 的设备,它使用 QT 框架运行 c++ 代码。使用 QProcess 不是一个选项,因为我们没有编译 QT 来支持它。
我无法tar.gz
使用 execl() 创建存档。
它返回-1(失败)并且错误是"No such file or directory"
代码示例:
std::string applicationPathWithName = "/bin/busybox";
QString dataDirectory("/opt/appl/data/");
QString archiveName = QString("AswLogs.tar.gz");
char* applName;
applName = new char [applicationPathWithName.size() + 1];
strcpy(applName, applicationPathWithName.c_str());
itsFlmFileManagerPtr->writeInFile(eFlmFileTypes_LogFile, data); //This creates logs.txt successfully
pid_t pid = fork();
QString command = QString("tar -czvf %1%2 %3logs.txt").arg(dataDirectory).arg(archiveName).arg(dataDirectory);
if(0 == pid)
{
INFO("Pid is 0");
int execStatus = 0;
execStatus = execl(applName, applName, command.toStdString().c_str(), (char*)NULL);
INFO("Execl is done, execStatus= " << execStatus);
std::string errorStr = strerror(errno);
INFO("Error: " << errorStr);
_exit(EXIT_FAILURE);
}
else if (pid < 0)
{
INFO("Failed to fork");
}
else
{
INFO("pid=" << pid);
int status;
if(wait(&status) == -1)
{
INFO("Wait child error");
}
INFO("Resume from fork");
}
输出:
PID=877
PID 为 0
执行完毕,execStatus= -1
错误:没有这样的文件或目录
从叉子恢复
权限:
日志.txt 666 | 忙箱 755
如何获取更多错误详细信息或这里有什么问题?
Edit:
所以,过了一会儿,我试着只做 .tar 存档,它奏效了。然后我尝试进行 .gz 压缩,它也有效。
解决方案:因此,至少在我的情况下,解决方案是分两步执行 tar.gz(需要两个过程):
execl("/bin/busybox", "/bin/busybox", "tar", "-cvf", "/opt/appl/data/logs.tar", "/opt/appl/data/logs.txt", (char*) NULL);
execl("/bin/busybox", "/bin/busybox", "gzip", "/opt/appl/data/logs.tar", (char*) NULL);