Bug 532006

Summary: sed -i of symbolic links create a file
Product: [Fedora] Fedora Reporter: Michele Marcionelli <michele.marcionelli>
Component: sedAssignee: Jiri Moskovcak <jmoskovc>
Status: CLOSED NOTABUG QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: high Docs Contact:
Priority: low    
Version: 11CC: dfediuck, jmoskovc, michele.marcionelli, pbonzini
Target Milestone: ---   
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2009-10-30 11:19:10 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Michele Marcionelli 2009-10-30 09:12:01 UTC
Description of problem:
when I apply "sed -i" on a symbolic link of a file (for instance /etc/grub.conf), sed will remove the symbolic link and create a file.

Version-Release number of selected component (if applicable):
sed-4.2.1-1.fc11.i586
sed-4.2.1-1.fc11.x86_64

How reproducible:
always

Steps to Reproduce:
root# echo "abc" > x1
root# ln -s x1 x2
root# sed -i 's/abc/cba/' x2
  
Actual results:

root# ls -l x?
-rw-r--r--. 1 root root 4 2009-10-30 10:06 x1
-rw-r--r--. 1 root root 4 2009-10-30 10:07 x2

root# cat x?
abc
cba


Expected results:

root# ls -l x?
-rw-r--r--. 1 root root 4 2009-10-30 10:06 x1
lrwxrwxrwx. 1 root root 2 2009-10-30 10:06 x2 -> x1

root# cat x?
abc
abc

Additional info:
none

Comment 1 Paolo Bonzini 2009-10-30 11:19:00 UTC
This is mentioned in i/usr/share/doc/sed-4.2.1/BUGS:


-i clobbers read-only files

  In short, `sed d -i' will let one delete the contents of
  a read-only file, and in general the `-i' option will let
  one clobber protected files.  This is not a bug, but rather a
  consequence of how the Unix filesystem works.

  The permissions on a file say what can happen to the data
  in that file, while the permissions on a directory say what can
  happen to the list of files in that directory.  `sed -i'
  will not ever open for writing  a file that is already on disk,
  rather, it will work on a temporary file that is finally renamed
  to the original name: if you rename or delete files, you're actually
  modifying the contents of the directory, so the operation depends on
  the permissions of the directory, not of the file).  For this same
  reason, sed will not let one use `-i' on a writeable file in a
  read-only directory, and will break hard or symbolic links when
  `-i' is used on such a file.


You can use --follow-symlinks to get the desired behavior.

Comment 2 Michele Marcionelli 2009-10-30 11:44:34 UTC
Ciao Paolo

but this is a new feature, isn't it? For instance I have not this problem with RHEL 5.4:

root# rpm -qa sed
sed-4.1.5-5.fc6

On RHEL 5.4 I have another problem: sed on a symbolic link works only with the full path:

root# sed -i 's/abc/cba/' x2
sed: ck_follow_symlink: couldn't lstat x/x1: No such file or directory

root# sed -i 's/abc/cba/' /tmp/sed/x2

and this without the "--follow-symlinks" option (which doesn't exist for this version):

root# ls -l x?
-rw-r--r-- 1 root root 4 Oct 30 12:31 x1
lrwxrwxrwx 1 root root 2 Oct 30 12:31 x2 -> x1

...
Thx,
Michele

Comment 3 Paolo Bonzini 2009-10-30 12:14:28 UTC
The RHEL bug is bug 490473.  I added you to the CC list there.

The Fedora behavior is "a new feature" indeed, or you could say it was a bug before in that it did not follow what the manual said.  It's a bit unfortunate either way, I agree.