【问题标题】:System command failing on Linux C++Linux C++ 上的系统命令失败
【发布时间】:2013-05-02 04:22:57
【问题描述】:

在我的程序中,我将一个可执行文件从一个位置复制到另一个位置,然后执行复制的文件。执行复制的文件时,我收到“权限被拒绝”错误。但是如果我重新启动我的程序,那么文件就会毫无问题地执行。有人可以帮我解决这个问题吗?下面的代码很简单,但演示了问题。

void copyFile(string _from, string _to)
{
    std::ifstream  src(_from.c_str());
    std::ofstream  dst(_to.c_str());

    dst << src.rdbuf();
}

int main()
{
    string original("./exe_file");
    string dest_file("./exe_dir/exefile");

    system("./exe_dir/exefile");  //Fails on first run because exe_dir does not exist.

    //mkdir and copy the file.
    mkdir("./exe_dir",S_IRWXO | S_IRWXU | S_IRWXG);
    copyFile(original, dest_file);

    //Open the file and close it again to flush the attribute cache.
    int fd = open(dest_file.c_str(),O_RDONLY);
    close(fd);

    //The line below fails with system error code 2 (Permission denied) on exefile.
    return system("./exe_dir/exefile");
{

在执行程序之前,我在原始文件上使用了“chmod 777 exe_file”,并且在运行该程序后,目标也具有相同的访问权限。我可以手动执行它就好了。并且程序的每次后续运行都是成功的。为什么第一次运行就失败了?

【问题讨论】:

  • 如果exe_dir在您调用system时存在,那么mkdir如何工作?
  • 最初不存在。它将在第一次运行之后。在这种情况下,mkdir 将在第二次运行时失败,但不会影响执行,因为它只会返回一个被忽略的错误代码。
  • 复制的exe文件没有执行权限。执行 chmod 或任何最适合您的方法。
  • 我试过chmod(dest_file.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH)。我还尝试在执行系统命令之前统计文件并刷新属性缓存。我正在尽我所能确保权限正确,但仍然失败。
  • 以上链接(umask):en.wikipedia.org/wiki/Umask

标签: c++ command system


【解决方案1】:

Coderz,不知道您在使用 IDE 时遇到了什么问题,但这对我来说很好。

#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cstdlib>

using namespace std;

void copyFile(string _from, string _to)
{
    std::ifstream  src(_from.c_str());
    std::ofstream  dst(_to.c_str());

    dst << src.rdbuf();
}

int main()
{
    string original("./exe_file");
    string dest_file("./exe_dir/exefile");

    system("./exe_dir/exefile");

    if (mkdir("./exe_dir", S_IRWXO | S_IRWXU | S_IRWXG))
        perror("mkdir");

    copyFile(original, dest_file);

    if (chmod("./exe_dir/exefile", S_IRWXU | S_IRWXG | S_IRWXO) == -1)
        perror("chmod");

    return system("./exe_dir/exefile");
}

注意 exe_file 是一个简单的 Hello World 二进制文件,结果是

sh: 1: ./exe_dir/exefile: not found
Hello World

复制的文件在哪里

-rwxrwxrwx  1 duck duck 18969 May  9 19:51 exefile

目录内

drwxrwxr-x 2 duck duck   4096 May  9 19:51 exe_dir

【讨论】:

  • 是的,这也是我所拥有的。在调试模式下,一切都很好。但是如果我构建一个发布版本,那么它会失败并显示错误代码 2。我也尝试使用 boost libs,但我仍然遇到同样的问题。它以前有效,但不再有效。我没有改变盒子上的任何东西,所以这真的很奇怪。
  • 感谢您的解决方案!当我添加 chmod 命令时,一切正常。
【解决方案2】:

你应该关闭你创建的文件。

cplusplus.com: std::ifstream::close

【讨论】:

  • "请注意,当 ifstream 对象被销毁时,任何打开的文件都会自动关闭。"当srcdst 在函数存在的情况下超出范围时,文件将被关闭。
  • 这里我不确定,dstofstream,而不是 ifstream
  • 流超出范围时将关闭。但是我还是添加了一个 close() 命令。没有成功。我尝试了所有用于复制此处列出的文件的选项:http://stackoverflow.com/questions/10195343/copy-a-file-in-an-sane-safe-and-efficient-way,但仍然无法正常工作。
猜你喜欢
  • 2011-09-24
  • 1970-01-01
  • 1970-01-01
  • 2013-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-05
相关资源
最近更新 更多