LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   strange (to me) behavior of file permissions when copying file (http://www.linuxquestions.org/questions/linux-general-1/strange-to-me-behavior-of-file-permissions-when-copying-file-763745/)

warrob 10-22-2009 12:05 PM

strange (to me) behavior of file permissions when copying file
 
I wish that when copying a file, it has the attributes defined by umask. This sometimes happens, other times do not:

$ls -l /bin/cat
-rwxr-xr-x 1 root root 44072 11 set 13:18 /bin/cat
$umask 0002
$cp /bin/cat mycat ; ls -l mycat
-rwxr-xr-x 1 rob rob 44072 22 ott 17:51 mycat

The date have been updated, but the permissions don't (the group can't write). Now if I try with:

$umask 0077
$cp /bin/cat mycat ; ls -l mycat
-rwxr-xr-x 1 rob rob 44072 22 ott 17:52 mycat
$rm mycat
$cp /bin/cat mycat ; ls -l mycat
-rwx------ 1 rob rob 44072 22 ott 17:52 mycat

Worked! Note that I had to delete the file in order to have the right permissions set.
It seems that umask is followed only if it is more restrictive than the original file.

Is there any way to force umask for copied files?
I also tried with:
$rm mycat
$umask 0002
$cp --no-preserve=all /bin/cat mycat ; ls -l mycat
-rwxr-xr-x 1 rob rob 44072 22 ott 18:00 mycat

but it doesn't work either!!!

thegeek 10-22-2009 01:52 PM

Hi there, i agree your issues seem strange, but copy is not really the right command to install and setup file permissions in one command.
The install command is much better ...

[neil@f11 tmp]$ touch nhi
[neil@f11 tmp]$ install -m 777 nhi nhi2
[neil@f11 tmp]$ ls -l nhi*
-rw-rw-r-- 1 neil neil 0 2009-10-22 19:51 nhi
-rwxrwxrwx 1 neil neil 0 2009-10-22 19:51 nhi2

warrob 10-22-2009 03:13 PM

Using install is a nice workaround. I wish to understand if the behavior of umask is a bug or a programmers' choice. Moreover, I can not use install when using scp or a filemanager such as dolphin or konqueror.

There should be a way to automatically set the default permissions to new files, also when copied.

lutusp 10-22-2009 08:29 PM

Quote:

Originally Posted by warrob (Post 3728930)
Using install is a nice workaround. I wish to understand if the behavior of umask is a bug or a programmers' choice. Moreover, I can not use install when using scp or a filemanager such as dolphin or konqueror.

There should be a way to automatically set the default permissions to new files, also when copied.

This command --

Code:

$ cp -p path1 path2
-- preserves the original's permissions and modes. So does this:

Code:

$ scp -p path1 path2
from "man scp":

-p: Preserves modification times, access times, and modes from the original file.

warrob 10-23-2009 04:01 PM

Quote:

Originally Posted by lutusp (Post 3729222)
This command --

Code:

$ cp -p path1 path2
-- preserves the original's permissions and modes. So does this:

Code:

$ scp -p path1 path2
from "man scp":

-p: Preserves modification times, access times, and modes from the original file.

Maybe I wasn't clear enough.
This is what I DON'T want to do: I do not want to preserve permissions BUT I wish to use those from umask.

thegeek 10-23-2009 04:38 PM

Hey Warrob

It appears that this is not an issue with umask i think it is really cp that was holding you up, using --remove-destination works.

[root@f11 tmp]# cp /bin/cat /tmp/
[root@f11 tmp]# ls -l /tmp/cat
-rwxr-xr-x 1 root root 57096 2009-10-23 22:28 /tmp/cat

[root@f11 tmp]# umask 0077
[root@f11 tmp]# cp /bin/cat /tmp/
cp: overwrite `/tmp/cat'? y

[root@f11 tmp]# ls -l /tmp/cat
-rwxr-xr-x 1 root root 57096 2009-10-23 22:33 /tmp/cat

[root@f11 tmp]# man cp
[root@f11 tmp]# cp --remove-destination /bin/cat /tmp/
cp: overwrite `/tmp/cat'? y
[root@f11 tmp]# ls -l /tmp/cat
-rwx------ 1 root root 57096 2009-10-23 22:34 /tmp/cat

lutusp 10-23-2009 04:52 PM

Quote:

Originally Posted by warrob (Post 3730218)
Maybe I wasn't clear enough.
This is what I DON'T want to do: I do not want to preserve permissions BUT I wish to use those from umask.

But "umask" is not part of a transferred file's description. It has effect only when creating a new file. So always create a new file, never copy over an existing one. Simple (but risky).

So, to answer your original inquiry:

Quote:

Originally Posted by warrob (Post 3730218)
Is there any way to force umask for copied files?

No, it isn't. The "umask" property is not transferred with files, and is unknown to the file copying utilities.

Have you considered just setting permissions on all the copied files rather than trying to trick the system into behaving as though the files are newly created? This is safer than deleting then copying, for a reason that should be obvious.
  1. Perform a data transfer.
  2. Reset permissions on the target directory's contents (or on single files as they are copied).
This would be a better approach than deleting then copying.

warrob 10-24-2009 04:31 AM

Quote:

Originally Posted by thegeek (Post 3730252)
Hey Warrob

It appears that this is not an issue with umask i think it is really cp that was holding you up, using --remove-destination works.

...
...
...

You guys have not red the original post:
if I set umask 0077 , this will affect the copied file (even without --remove-destination) but with umask 0002 the original permissions are retained. That's weird to me!

warrob 10-24-2009 04:32 AM

Quote:

Originally Posted by lutusp (Post 3730266)
But "umask" is not part of a transferred file's description. It has effect only when creating a new file.

So why umask is applied if I set umask 0077 and it is not with umask 0002 ??

lutusp 10-24-2009 06:45 AM

Quote:

Originally Posted by warrob (Post 3730683)
So why umask is applied if I set umask 0077 and it is not with umask 0002 ??

Code:

$ man umask
umask [-p] [-S] [mode]

The user file-creation mask is set to mode. If mode begins with a digit, it is interpreted as an octal number; otherwise it is interpreted as a symbolic mode mask similar to that accepted by chmod(1). If mode is omitted, the current value of the mask is printed. The -S option causes the mask to be printed in symbolic form; the default output is an octal number. If the -p option is supplied, and mode is omitted, the output is in a form that may be reused as input. The return status is 0 if the mode was successfully changed or if no mode argument was supplied, and false otherwise.

Remember "umask" is a mask, not a value. Think about what that means.

In the example you chose, the copied file has to be created, which means it's a new file. And the source umask has no effect on the destination, which was my point. Your example shows that the destination umask sets default permissions on newly created files, which is what it's for.

warrob 10-25-2009 06:13 AM

Quote:

Remember "umask" is a mask, not a value. Think about what that means.

In the example you chose, the copied file has to be created, which means it's a new file. And the source umask has no effect on the destination, which was my point. Your example shows that the destination umask sets default permissions on newly created files, which is what it's for.
You are right. I'm still not sure in which way the mask is applied to the source-file permissions. I'm guessing if there exists a mask value that creates new files with permissions rwxrwxr-- , independently of the source-file permissions. My feeling is that probably doesn't exist.

Besides, any idea on why the --no-preserve option in cp does not work?


All times are GMT -5. The time now is 07:58 AM.