13

When I change files in cygwin bash with the sed command, the file gets locked.

Reproduce:

  1. Open cmd and cd to non-user directory (f.e. temp)
  2. echo aaa > test.txt
  3. Open in texteditor, add line, try to save => works
  4. %CYGWIN_HOME%\bin\bash -c "sed -i 's/aaa/bbb/' test.txt"
  5. In texteditor, add another line and try to save => "Access denied"

WTF?! How can I sed a file without locking, or release the file handles afterwards?

  • Windows security permissions appear to be completely screwed on file:

    Permissions before:

    permissions before (Image!)

    Permissions after:

    permissions after (Image!)

  • It works however in user directory (Desktop)

  • I checked the file with LockHunter but no process appears to lock the file
  • Cygwin FAQ mentions interferences with some programs. It tried uninstalling Antivir and Catalyst and rebooting.
  • Trying UnxTools (crashes), GnuWin32 (sed doesn't support inplace substitution), Interex (installer doesn't support W7 Pro) ... * sigh *
3
  • 2
    That just sounds like the screwy Windows philosophy 'must protect users from themselves', I see that all the time when trying to mix and match Windows and Cygwin/Linux files. Good luck.
    – shellter
    Feb 20, 2012 at 21:51
  • +1 lambdor. I checked this behavior in Cygwin and you're right. This is pretty weird. The file isn't locked (i.e. as in it isn't in use). The file permissions read only / read write permissions get modified.
    – BicycleDude
    Feb 20, 2012 at 21:59
  • I've used GnuWin32 sed on W7 64bit and had no trouble with in place substitution. It's sed 4.2.1 which I downloaded with the getGnuWin32 project. Feb 21, 2012 at 13:26

3 Answers 3

9

corresponding to Cygwin Mailinglist, use noacl for mounting

$ mount -o binary,posix=0,user,noacl C:/tmp /mnt/tmp

or directly in Windows ../cygwin/fstab before starting Cygwin bash

C:/tmp /mnt/tmp ntfs binary,posix=0,user,noacl 0 0
6

I had exactly the same trouble after using find in combination with grep and sed under cygwin to process a folder full of Java source code files automatically. Sed messed up the NTFS file permissions of my files, which was still persistent after a reboot, so I searched for an easy way to fix this.

I actually found an easy way to get the file permissions back to the old values. It can be done for a whole folder recursively, which is important to me, since I have modified so many files at once (several hundred). Sadly, I only know the german names for the menu entries, so I'm not sure how the menu entries (in the Windows Explorer) are called in correct english.

In german, you do: Rightclick on the parent folder of the folder where I used sed, "Eigenschaften" (context menu), "Sicherheit" (tab in the dialog), "Erweitert" (button), "Berechtigungen ändern..." (button with UAC prompt), now I just check both checkboxes (the upper one is set already, but the lower is not, but has to be). Now I click on "OK" for three times.

The actual change that is done by this is that the lower checkbox forces Windows to replace the current permissions for the folder contents recursively with the permissions of the folder you rightclicked, so all broken permissions are fixed. Worked like a charm for me, and is also really quick (only a few seconds for a lot of files).

Possible translations (guessed by me):

  • "Eigenschaften" -> "Properties"
  • "Sicherheit" -> "Security"
  • "Erweitert" -> "Advanced"
  • "Berechtigungen ändern..." -> "Change permissions..."
3

I ran my own set of tests, and, indeed, the permissions of the file do get set to r-xr-x--- after the sed command and subsequent cygwin commands will perceive this as a read only file.

C:\Temp>echo aaa > test.txt
C:\Temp>ls -l
total 1
----------+ 1 bikeguy ???????? 6 Feb 21 08:51 test.txt
C:\temp>sed -i 's/aaa/bbb/' test.txt
C:\temp>cat test.txt
bbb
C:\temp>ls -l test.txt
-r-xr-x---+ 1 bikeguy mkgroup-l-d 5 Feb 21 08:52 test.txt
C:\temp>

These -r-xr-x--- permissions do prevent subsequent writes by some software:

C:\temp>echo xxx > test.txt
Access is denied.
C:\temp>

So, the workaround would be to add an additional chmod on the file

C:\temp>chmod 777 test.txt
C:\temp>echo xxx > test.txt
C:\temp>
3
  • the weird thing is the first permissions mask '----------+ 1 bikeguy ????????' which I also run into using Cygwin/Windows. Then you write 'These -r-xr-x--- permissions do prevent subsequent writes by some software:' So maybe the 'some software' runs under a different group AND becuase there is no access thru 'other' permissions, that causes the problem? Thanks, this debugging has helped me with this problem. Good luck to all.
    – shellter
    Feb 20, 2012 at 22:36
  • Yeah, @shellter I noticed that too when I was researching the answer. I assumed that '-----------+' line to meant no security exists (so you have full access) whereas '-r-xr-x---+' means security exists where write access is missing. If I did a chmod 000 I will also get '----------+' but this time there is definitely security and I have no privileges to read or write this file.
    – BicycleDude
    Feb 21, 2012 at 2:07
  • I'm using git version 1.9.5.msysgit.1, I also see the same issue, I use the chmod 777 test.txt command to workaround this issue.
    – ollydbg23
    Oct 30, 2015 at 4:26

You must log in to answer this question.