Code::Blocks Forums

Developer forums (C::B DEVELOPMENT STRICTLY!) => Plugins development => Topic started by: dmoore on March 29, 2014, 10:23:18 pm

Title: Preserving file permissions
Post by: dmoore on March 29, 2014, 10:23:18 pm
I finally got irritated enough by my scripts losing their execute permissions after being saved by C::B that I wrote a little plugin to preserve them:

https://github.com/spillz/codeblocks-python/tree/master/PreservePermissions

Wasn't sure whether to do this as a plugin or as a patch to the save routine in the SDK, so it's a plugin for now.

Notes:
* Linux only, because frankly permissions generally aren't used much on windows by most users, but if someone wants to patch for windows that's fine too.
* Uses system calls stat (http://linux.die.net/man/2/stat) to get permissions and chmod (http://linux.die.net/man/2/chmod) to set them. It only works for regular save, not save as.
Title: Re: Preserving file permissions
Post by: oBFusCATed on March 30, 2014, 01:33:11 pm
I think it should be fixed in the SDK, no as plugin, because there is a changes for race conditions or sync issues.
Title: Re: Preserving file permissions
Post by: dmoore on March 30, 2014, 01:55:20 pm
I think it should be fixed in the SDK, no as plugin, because there is a changes for race conditions or sync issues.

I was under the impression that NotifyPlugins was blocking and therefore synchronous. So race issues no more or less likely as a plugin I would think. Isn't the existing code already susceptible to arace issues anyway (because access to the original file is never locked) which in practice are never an issue?

But I can show you what a patch to the SDK would look like...
Title: Re: Preserving file permissions
Post by: dmoore on March 30, 2014, 04:53:53 pm
Attached is the alternative of an SDK patch. Wasn't sure what #IFDEF to use, so used __WXGTK__ for now.

Contrary to what I said about "Save As", one potential flaw is that when you overwrite an existing file with Save As, the permissions will be the same as the file you are overwriting. I can't decide if that's good or bad...
Title: Re: Preserving file permissions
Post by: oBFusCATed on March 31, 2014, 08:37:42 am
A check for platform::unix would probably be better. But you probably need to check for osx, too. I'm not sure what will happen with a cygwin builds thought.
Also the preserve_permissions argument is not used anywhere in the function, nor passed to false. So it should probably not be added.
Title: Re: Preserving file permissions
Post by: dmoore on March 31, 2014, 02:54:03 pm
Thanks for taking a look.

Isn't the platform stuff for runtime checks? We need a compile time check.

I put the function arg in for the case that we don't want to preserve permissions for SaveAs - but need to add the code. Maybe a better way is to have a wxString permissions_from argument, which is the file to take permissions from?

Cygwin should be fine I would think. Does OS X have stat? (And that's __wxMAC__ or something anyway isn't it, which isn't in the patch)
Title: Re: Preserving file permissions
Post by: oBFusCATed on March 31, 2014, 11:20:39 pm
OSX is based on FreeBSD, so it is unix - thus most of the posix apis should work just fine.
But you're correct that the platform stuff is runtime only.

The wxString permissinsFrom parameter sounds fine to me.
Title: Re: Preserving file permissions
Post by: dmoore on April 12, 2014, 05:01:45 pm
Ping. Anyone have any thoughts on what is the right #ifdef to use to detect systems with stat?
Title: Re: Preserving file permissions
Post by: oBFusCATed on April 14, 2014, 12:19:50 am
Probably some of these: http://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor

I'd probably use __posix.
Title: Re: Preserving file permissions
Post by: dmoore on April 14, 2014, 12:40:52 am
Thanks for the links.

This is hilariously comprehensive: http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system

I saw answers like this in a couple of places: http://stackoverflow.com/a/16107549/748925 so maybe this is better than __posix?
Title: Re: Preserving file permissions
Post by: oBFusCATed on April 14, 2014, 01:09:56 am
You could probably add some global macro which could be checked if we have posix or not.