0

我有一个基于 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);

4

1 回答 1

0

我不知道这是什么平台或编译器,但通常不可能将整个命令行传递给 execl()。如果我理解正确,你正在运行这样的东西:

execl ("/bin/busybox", "/bin/busybox", "tar -czvf blah blah", null);

但总的来说你需要

execl ("/bin/busybox", "/bin/busybox", "tar", "-czvf", "blah", "blah", null);

也就是说,您需要将命令行解析为其各个参数。在您描述的情况下,这应该很容易,因为您已经知道各个参数是什么。

我认为问题在于 /bin/busybox 启动,但是当它试图将“tar -czvf blah blah”解释为要运行的小程序的名称时会阻塞。

顺便说一句——而且可能不相关——busybox“tar”默认情况下不会在内部处理 gzip 压缩,除非你在构建时启用了这个特性。

于 2017-08-27T08:53:18.537 回答