2

我的程序通过 TCP 套接字接收可执行二进制文件。

我需要将此文件作为可执行程序保存到硬盘中。文件已成功接收,但问题是默认文件属性设置为不可执行。

如何在 Ubuntu 中将文件的属性更改为 C 中的可执行文件?

谢谢, 问候, 机器人

4

3 回答 3

6

怎么样int chmod(const char *path, mode_t mode)int fchmod(int fd, mode_t mode) ?

apropos chmod
man 2 chmod

最基本的例子:

#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]){

   char * fpath = "/path/to/binary";
   int ret=0;
   if(ret = chmod(fpath, S_IRUSR|S_IXUSR) < 0){
      perror("chmod failed");
      exit(1);
   }

   printf("chmod ok\n");
   exit(0);
}
于 2012-05-26T04:33:24.330 回答
3

你是如何创建和编写文件的?如果您知道它将是可执行的,只需首先使用正确的模式制作文件。

int fd = open("path/to/file", O_WRONLY | O_CREAT, 0777);

除非umask是剥离可执行位(常用值是0022并且0002保留可执行位单独),path/to/file否则将创建最初可执行的。

于 2012-05-26T05:09:58.647 回答
2

您可以使用 更改文件模式chmod。阅读手册页(man 2 chmod)了解详细信息(与 shell 命令大致相同chmod)。

于 2012-05-26T04:32:39.803 回答