LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-22-2009, 11:05 AM   #1
warrob
LQ Newbie
 
Registered: Aug 2005
Posts: 16

Rep: Reputation: 0
Question 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!!!

Last edited by warrob; 10-22-2009 at 11:08 AM.
 
Old 10-22-2009, 12:52 PM   #2
thegeek
Member
 
Registered: Oct 2009
Location: Amsterdam
Distribution: CentOS,Fedora,Puppy
Posts: 62

Rep: Reputation: 20
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
 
Old 10-22-2009, 02:13 PM   #3
warrob
LQ Newbie
 
Registered: Aug 2005
Posts: 16

Original Poster
Rep: Reputation: 0
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.
 
Old 10-22-2009, 07:29 PM   #4
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by warrob View Post
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.
 
Old 10-23-2009, 03:01 PM   #5
warrob
LQ Newbie
 
Registered: Aug 2005
Posts: 16

Original Poster
Rep: Reputation: 0
Thumbs down

Quote:
Originally Posted by lutusp View Post
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.
 
Old 10-23-2009, 03:38 PM   #6
thegeek
Member
 
Registered: Oct 2009
Location: Amsterdam
Distribution: CentOS,Fedora,Puppy
Posts: 62

Rep: Reputation: 20
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
 
Old 10-23-2009, 03:52 PM   #7
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by warrob View Post
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 View Post
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.
 
Old 10-24-2009, 03:31 AM   #8
warrob
LQ Newbie
 
Registered: Aug 2005
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by thegeek View Post
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!
 
Old 10-24-2009, 03:32 AM   #9
warrob
LQ Newbie
 
Registered: Aug 2005
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by lutusp View Post
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 ??
 
Old 10-24-2009, 05:45 AM   #10
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by warrob View Post
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.
 
Old 10-25-2009, 05:13 AM   #11
warrob
LQ Newbie
 
Registered: Aug 2005
Posts: 16

Original Poster
Rep: Reputation: 0
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?
 
  


Reply

Tags
attributes, permissions, umask



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange File Permissions tuftystick Linux - Newbie 3 08-25-2009 01:00 PM
Strange file behavior nc3b Linux - General 2 08-23-2006 01:27 PM
Strange file size and permissions under /usr/sbin phyrster Linux - Security 6 05-31-2006 08:26 AM
Strange file size and permissions in /usr/sbin phyrster Debian 2 05-28-2006 04:53 AM
Preserve file permissions when copying from NTFS to a Samba share? somedude Linux - Software 4 06-11-2004 09:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration