bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: awk inplace overrides symlinks


From: Ed Morton
Subject: Re: awk inplace overrides symlinks
Date: Fri, 9 Feb 2024 08:31:22 -0600
User-agent: Mozilla Thunderbird

Not saying gawk needs to behave the same way of course but FWIW sed -i and perl -i both also replace the link with a regular file:

   $ rm link; echo 'foo' > file; ln -s file link; cat link; ls -l file link
   foo
   -rw-r--r-- 1 name name 4 Feb  9 08:28 file
   lrwxrwxrwx 1 name name 4 Feb  9 08:28 link -> file

   $ sed -i 's/foo/bar/' link

   $ cat link; ls -l file link
   bar
   -rw-r--r-- 1 name name 4 Feb  9 08:28 file
   -rw-r--r-- 1 name name 4 Feb  9 08:28 link

   $ rm link; echo 'foo' > file; ln -s file link; cat link; ls -l file link
   foo
   -rw-r--r-- 1 name name 4 Feb  9 08:28 file
   lrwxrwxrwx 1 name name 4 Feb  9 08:28 link -> file

   $ perl -pi -e 's/foo/bar/' link

   $ cat link; ls -l file link
   bar
   -rw-r--r-- 1 name name 4 Feb  9 08:28 file
   -rw-r--r-- 1 name name 4 Feb  9 08:28 link

so maybe there's something to be said for consistency across the various tools?

Regards,

    Ed.

On 2/9/2024 7:47 AM, Andrew J. Schorr wrote:
On Fri, Feb 09, 2024 at 05:21:05AM +0000, Avid Seeker wrote:
On Friday, February 9th, 2024 at 02:16, Andrew J. 
Schorr<aschorr@telemetry-investments.com>  wrote:

Hi,

On Fri, Feb 09, 2024 at 12:28:54AM +0000, Avid Seeker wrote:

`touch file ln -s file link awk -i inplace '{print "newline"}' link`

Results in replacing `link` with a regular file.

Thanks for the bug report. The code currently calls stat() to check whether
it's working on a regular file, and craps out if it isn't. So arguably we
should be using lstat() and failing in this case instead of stomping on the
symlink. Or do you expect the code to chase the link and replace the symlink
destination file? I think the latter is probably too much to ask...

Regards,
Andy
It's more expected for the program edits the original file. Consider how
editors like emacs don't complain when editing a symlink. In the end, the
symlink chasing is done by `readlink` function.
It could be a recursive symlink, so we'd actually need to use
realpath, not readlink. But it's not quite clear to me that this
behavior is within the scope of what the inplace extension is
supposed to do. One could debate it...

Regards,
Andy



reply via email to

[Prev in Thread] Current Thread [Next in Thread]