Opened 4 years ago

Last modified 11 months ago

#4360 defect new

filepath.FilePath.setContent doesn't preserve permissions

Reported by: therve Owned by:
Priority: high Milestone:
Component: core Keywords:
Cc: richard@…, keturn@… Branch:
Author: Launchpad Bug:


For example, if you set the content of an executable file, the executable bit is removed after calling setContent. It should probably try to keep the previous ones.

Attachments (1)

filepath-preserve-permissions-4360-1.patch (1.5 KB) - added by rwall 3 years ago.
A first attempt - next I will attempt to preserve owner and group

Download all attachments as: .zip

Change History (8)

comment:1 Changed 4 years ago by <automation>

  • Owner glyph deleted

comment:2 Changed 3 years ago by rwall

  • Cc richard@… added
  • Summary changed from FilePath.setContent doesn't preserve permissions to filepath.FilePath.setContent doesn't preserve permissions

I created duplicate ticket #4988 before exarkun pointed this one out to me. Adding my description here.

setContent creates, writes and then renames a temporary file over the top of the original file...but it doesn't apply permissions that were present on the original file.

Furthermore, the mode flags passed to, make the file executable.

This behaviour seems unexpected and is likely to confuse users of FilePath until they dig into the source code.

Seems like it should be fairly easy to read the mode from the original file and apply it to the new file.

>>> f = FilePath('foo')
>>> f.setContent('foo')
>>> f.restat()
>>> oct(f.statinfo.st_mode)
>>> f.chmod(0400)
>>> f.restat()
>>> oct(f.statinfo.st_mode)
>>> f.setContent('foo')
>>> f.restat()
>>> oct(f.statinfo.st_mode)

Changed 3 years ago by rwall

A first attempt - next I will attempt to preserve owner and group

comment:3 Changed 2 years ago by acapnotic

  • Cc keturn@… added

comment:4 Changed 2 years ago by exarkun

  • Priority changed from normal to high

comment:5 Changed 2 years ago by lewq

+1 for also retaining owner and group info.

comment:6 Changed 11 months ago by tom.prince

It might be nice if setContent optionally allowed you to specify the mode (for when creating a new file, for example).

comment:7 Changed 11 months ago by exarkun

Sounds like a tricky API.

Would the mode be ignored if setContent weren't creating a new file (can setContent even easily tell if it is creating a new file or not)? Or would it just be equivalent to using FilePath.chmod afterwards?

Note: See TracTickets for help on using tickets.