1

In my program I am copying an executable file from one location to another, and then execute the copied file. When the copied file is executed I get a "permission denied" error. But if I restart my program then the file gets executed without a problem. Can someone please help me with the problem? The code below is simple, but demonstrates the problem.

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");
{

I used 'chmod 777 exe_file' on the original file before executing the program, and after running this program the destination also has the same access rights. I can execute it manually just fine. And every subsequent run of the program is successful. Why does it fail on the first run?

Coderz
  • 11
  • 1
  • 2
  • if `exe_dir` exists when you call `system` then how does `mkdir` work? – Duck May 08 '13 at 03:38
  • Initially it does not exist. It will after the first run. In this case the mkdir will fail on the second run, but not have an effect on the execution since it will just return with an error code which is ignored. – Coderz May 08 '13 at 03:42
  • 1
    The copied exefile does not have execute permissions. Do a chmod or whatever method is best for you. – Duck May 08 '13 at 03:58
  • I tried `chmod(dest_file.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH)`. I also tried to stat the file and flush the attribute cache before doing the system command. I am doing everything I can to make sure the permissions is correct, but still it fails. – Coderz May 08 '13 at 04:07
  • Link for the above (`umask`): http://en.wikipedia.org/wiki/Umask – Fabian Tamp May 08 '13 at 04:22
  • Added umask(0) before copying the file which creates the file with full permissions (-rwxrwxrwx) by default. Then I still do the chmod and the open/close/stat for cache flush. No success. – Coderz May 08 '13 at 04:42
  • Whenever my code executes in DEBUG mode, then it is all fine. But when it executes in RELEASE mode, then it fails. Maybe I should give the DEBUG executable to the client. :) – Coderz May 08 '13 at 05:05
  • chmod +x ./exe_dir/exefile? – DarkWanderer May 08 '13 at 15:48

2 Answers2

0

You should close file you've created.

See cplusplus.com: std::ifstream::close

mtt
  • 106
  • 5
  • 1
    "Note that any open file is automatically closed when the ifstream object is destroyed." When `src` and `dst` go out of scope when the function exists, the files are closed. – Fabian Tamp May 08 '13 at 04:23
  • I'm not sure here, `dst` is `ofstream`, not `ifstream`. – mtt May 08 '13 at 04:25
  • The streams get closed when going out of scope. But I added a close() command anyway. No success. I tried all the options for copying files listed here: [http://stackoverflow.com/questions/10195343/copy-a-file-in-an-sane-safe-and-efficient-way](http://stackoverflow.com/questions/10195343/copy-a-file-in-an-sane-safe-and-efficient-way) and still not working. – Coderz May 08 '13 at 04:40
0

Coderz, no idea what problems you are experiencing with your IDE but this works fine for me.

#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");
}

Note that exe_file is a simple Hello World binary and the results are

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

where the copied file is

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

within directory

drwxrwxr-x 2 duck duck   4096 May  9 19:51 exe_dir
Duck
  • 26,924
  • 5
  • 64
  • 92
  • Yes, that is also what I have. In debug mode, all is fine. But if I build a release version, then it fails with error code 2. I tried using boost libs as well, and still I have the same issue. It worked before, but not any more. I did not change anything on the box, so this is really something strange. – Coderz May 11 '13 at 10:02
  • Thanks for the solution! When I added the chmod command it all worked fine. – Coderz Nov 15 '18 at 07:36