Jessica
Jessica

Reputation: 2035

copy a file and change its permissions in Linux with C

I copy a file by opening the source in read-only mode and the destination in write-only mode. It's simple and it works.

The problem is that sometimes I am copying a file that sits on an NFS drive, or other network drives and when in these cases the permissions get all screwed and SELinux complains. I then go and manually set the permissions of the files I just copied and it's OK. I can access them again (via ftp, web, etc).

Is there any way to copy a file and change the permissions to a certain user and group?

Code is appreciated. Thanks

EDIT:

would something like

open(argv[2], O_WRONLY | O_CREAT, 0666)

work?

Upvotes: 0

Views: 2116

Answers (2)

jim mcnamara
jim mcnamara

Reputation: 16379

For user and group setting changes use the chown() function. chmod() works on the st_mode values like protections and setuid, setgid, sticky bit.

Upvotes: 2

Maz
Maz

Reputation: 3375

According to http://www.delorie.com/gnu/docs/glibc/libc_290.html, there is a function:

int chmod (const char *filename, mode_t mode)

This should do what you want.

Upvotes: 0

Related Questions