简体   繁体   中英

Unix Executable file get changed to Document while transferring through sockets

I am on Mac OSX. I have written two programs to transfer a "Unix Executable file" from one place to another(server program to client program). Here is the code:

Server Side code:

fileDescriptor = open(reqMsg.filename, O_RDONLY);
if (fileDescriptor > 0)
    while ((msgLength = read(fileDescriptor, content, MESSAGESIZE)) > 0)
        send(data.remoteSocketDes, content, msgLength, 0);

Client Side code:

fileDescriptor = open(data.filename, O_CREAT|O_WRONLY);
if (fileDescriptor > 0)
    while ((msgLength = recv(localSocketDes, content, MESSAGESIZE, 0)) > 0)
        write(fileDescriptor, content, msgLength);

The size and content of the file is exactly same but the type of file is changed from "Unix Executable file" to "Document". Eventually I am not able to execute it from the place where it has been copied.

Note: the code working properly fine for ".txt" files.

The "executable" property of a file is a function of its permissions, not its content. Simply copying its contents into another file will not preserve this property.

If you want to preserve this, you will need to serialize the permissions of the original file (which you can obtain using stat() on the file), send them to the client, and apply them to the new file (using chmod() , or by passing the permissions to open() ).

If preserving file metadata such as this is important, you may also want to consider preserving the creation and modification times and extended attributes of the file as well.

Under Unix (and variants like OS X), the determining factor whether you can execute a command from the shell prompt is whether the file you are trying to execute has the execute bit set (and you have permissions to execute it).

To handle this from the command prompt, you would just use chmod +x {file} , but if you know the incoming file is an executable (possibly by testing in sender first and sending that information through the socket in a preamble), you could code your receiving program to set the execute bit if appropriate by using chmod() :

chmod( data.filename, permission)

Where you calculate permission by or'ing the execute bits with the default umask, like so:

mode_t mask = umask (0);
umask (mask);
mode_t permission = mask | S_IXUSR | S_IXGRP | S_IXOTH;

Note that this will set the permissions to world, group, and owner writable, which might not be desirable. Also note that you MUST call umask the second time, since the first call is destructive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM