Skip to content

Commit 8ec6137

Browse files
committed
PreserverPermissions: plugin for unix that preserves original file permissions
1 parent 6f923d3 commit 8ec6137

4 files changed

Lines changed: 237 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
2+
<CodeBlocks_project_file>
3+
<FileVersion major="1" minor="6" />
4+
<Project>
5+
<Option title="PreservePermissions" />
6+
<Option pch_mode="2" />
7+
<Option compiler="gcc" />
8+
<Build>
9+
<Target title="default">
10+
<Option output="PermissionsPreserver.so" prefix_auto="0" extension_auto="0" />
11+
<Option type="3" />
12+
<Option compiler="gcc" />
13+
<Option host_application="codeblocks" />
14+
<Option run_host_application_in_terminal="0" />
15+
<Compiler>
16+
<Add option="-ansi" />
17+
<Add option="-g" />
18+
<Add option="`pkg-config --cflags codeblocks`" />
19+
<Add option="`wx-config --cflags`" />
20+
<Add option="-fPIC" />
21+
</Compiler>
22+
<Linker>
23+
<Add option="`pkg-config --libs codeblocks`" />
24+
<Add option="`wx-config --libs`" />
25+
</Linker>
26+
<ExtraCommands>
27+
<Add after="zip -j9 PermissionsPreserver.zip manifest.xml" />
28+
<Add after="zip -j9 PermissionsPreserver.cbplugin PermissionsPreserver.so PermissionsPreserver.zip" />
29+
</ExtraCommands>
30+
</Target>
31+
</Build>
32+
<Unit filename="permissions_preserver.cpp" />
33+
<Unit filename="permissions_preserver.h" />
34+
<Unit filename="manifest.xml" />
35+
<Extensions>
36+
<code_completion />
37+
<envvars />
38+
<debugger />
39+
</Extensions>
40+
</Project>
41+
</CodeBlocks_project_file>

PreservePermissions/manifest.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
2+
<CodeBlocks_plugin_manifest_file>
3+
<SdkVersion major="1" minor="20" release="0" />
4+
<Plugin name="PermissionsPreserver">
5+
<Value title="Permissions Preserver" />
6+
<Value version="0.1" />
7+
<Value description="Preserves the permissions of files that are saved by Code::Blocks" />
8+
<Value author="Damien Moore" />
9+
<Value authorEmail="damienlmoore@gmail.com" />
10+
<Value authorWebsite="https://github.com/spillz/codeblocks-python" />
11+
<Value thanksTo="" />
12+
<Value license="GPL" />
13+
</Plugin>
14+
</CodeBlocks_plugin_manifest_file>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <sdk.h> // Code::Blocks SDK
2+
#include <editorbase.h>
3+
#include <configurationpanel.h>
4+
#include "Permissions_Preserver.h"
5+
6+
// Register the plugin with Code::Blocks.
7+
// We are using an anonymous namespace so we don't litter the global one.
8+
namespace
9+
{
10+
PluginRegistrant<PermissionsPreserver> reg(_T("PermissionsPreserver"));
11+
}
12+
13+
14+
15+
// constructor
16+
PermissionsPreserver::PermissionsPreserver()
17+
{
18+
// Make sure our resources are available.
19+
// In the generated boilerplate code we have no resources but when
20+
// we add some, it will be nice that this code is in place already ;)
21+
if(!Manager::LoadResource(_T("PermissionsPreserver.zip")))
22+
{
23+
NotifyMissingFile(_T("PermissionsPreserver.zip"));
24+
}
25+
}
26+
27+
// destructor
28+
PermissionsPreserver::~PermissionsPreserver()
29+
{
30+
}
31+
32+
void PermissionsPreserver::OnAttach()
33+
{
34+
// do whatever initialization you need for your plugin
35+
// NOTE: after this function, the inherited member variable
36+
// m_IsAttached will be TRUE...
37+
// You should check for it in other functions, because if it
38+
// is FALSE, it means that the application did *not* "load"
39+
// (see: does not need) this plugin...
40+
41+
// register event sinks
42+
Manager* pm = Manager::Get();
43+
pm->RegisterEventSink(cbEVT_EDITOR_BEFORE_SAVE, new cbEventFunctor<PermissionsPreserver, CodeBlocksEvent>(this, &PermissionsPreserver::OnEditorBeforeSave));
44+
pm->RegisterEventSink(cbEVT_EDITOR_SAVE, new cbEventFunctor<PermissionsPreserver, CodeBlocksEvent>(this, &PermissionsPreserver::OnEditorSave));
45+
46+
}
47+
48+
void PermissionsPreserver::OnRelease(bool appShutDown)
49+
{
50+
// do de-initialization for your plugin
51+
// if appShutDown is true, the plugin is unloaded because Code::Blocks is being shut down,
52+
// which means you must not use any of the SDK Managers
53+
// NOTE: after this function, the inherited member variable
54+
// m_IsAttached will be FALSE...
55+
}
56+
57+
void PermissionsPreserver::OnEditorBeforeSave(CodeBlocksEvent& event)
58+
{
59+
EditorBase *ed = event.GetEditor();
60+
m_filename = ed->GetFilename();
61+
struct stat s;
62+
if (stat(m_filename.mb_str(wxConvUTF8),&s) == 0)
63+
m_permissions = s.st_mode;
64+
else
65+
{
66+
m_permissions = 0;
67+
m_filename = wxEmptyString;
68+
}
69+
event.Skip();
70+
}
71+
72+
void PermissionsPreserver::OnEditorSave(CodeBlocksEvent& event)
73+
{
74+
EditorBase *ed = event.GetEditor();
75+
wxString filename = ed->GetFilename();
76+
//m_filename will be empty if original permissions could not be established
77+
//(e.g. because file does not exist), so no chmod will occur in this case
78+
if (filename == m_filename)
79+
chmod(m_filename.mb_str(wxConvUTF8),m_permissions);
80+
event.Skip();
81+
}
82+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/***************************************************************
2+
* Name: PreservePermissions
3+
* Purpose: Code::Blocks plugin
4+
* Author: Damien Moore (damienlmoore@gmail.com)
5+
* Created: 2014-03-29
6+
* Copyright: Damien Moore
7+
* License: GPL
8+
**************************************************************/
9+
10+
#ifndef PERMISSIONS_PRESERVER_H_INCLUDED
11+
#define PERMISSIONS_PRESERVER_H_INCLUDED
12+
13+
// For compilers that support precompilation, includes <wx/wx.h>
14+
#include <wx/wxprec.h>
15+
16+
#ifndef WX_PRECOMP
17+
#include <wx/wx.h>
18+
#endif
19+
20+
#include <cbplugin.h> // for "class cbPlugin"
21+
22+
#include <sys/stat.h>
23+
24+
25+
class PermissionsPreserver : public cbPlugin
26+
{
27+
public:
28+
/** Constructor. */
29+
PermissionsPreserver();
30+
/** Destructor. */
31+
virtual ~PermissionsPreserver();
32+
33+
34+
/** This method is called by Code::Blocks and is used by the plugin
35+
* to add any menu items it needs on Code::Blocks's menu bar.\n
36+
* It is a pure virtual method that needs to be implemented by all
37+
* plugins. If the plugin does not need to add items on the menu,
38+
* just do nothing ;)
39+
* @param menuBar the wxMenuBar to create items in
40+
*/
41+
virtual void BuildMenu(wxMenuBar* menuBar){}
42+
43+
/** This method is called by Code::Blocks core modules (EditorManager,
44+
* ProjectManager etc) and is used by the plugin to add any menu
45+
* items it needs in the module's popup menu. For example, when
46+
* the user right-clicks on a project file in the project tree,
47+
* ProjectManager prepares a popup menu to display with context
48+
* sensitive options for that file. Before it displays this popup
49+
* menu, it asks all attached plugins (by asking PluginManager to call
50+
* this method), if they need to add any entries
51+
* in that menu. This method is called.\n
52+
* If the plugin does not need to add items in the menu,
53+
* just do nothing ;)
54+
* @param type the module that's preparing a popup menu
55+
* @param menu pointer to the popup menu
56+
* @param data pointer to FileTreeData object (to access/modify the file tree)
57+
*/
58+
virtual void BuildModuleMenu(const ModuleType type, wxMenu* menu, const FileTreeData* data = 0){}
59+
60+
/** This method is called by Code::Blocks and is used by the plugin
61+
* to add any toolbar items it needs on Code::Blocks's toolbar.\n
62+
* It is a pure virtual method that needs to be implemented by all
63+
* plugins. If the plugin does not need to add items on the toolbar,
64+
* just do nothing ;)
65+
* @param toolBar the wxToolBar to create items on
66+
* @return The plugin should return true if it needed the toolbar, false if not
67+
*/
68+
virtual bool BuildToolBar(wxToolBar* toolBar){ return false; }
69+
protected:
70+
/** Any descendent plugin should override this virtual method and
71+
* perform any necessary initialization. This method is called by
72+
* Code::Blocks (PluginManager actually) when the plugin has been
73+
* loaded and should attach in Code::Blocks. When Code::Blocks
74+
* starts up, it finds and <em>loads</em> all plugins but <em>does
75+
* not</em> activate (attaches) them. It then activates all plugins
76+
* that the user has selected to be activated on start-up.\n
77+
* This means that a plugin might be loaded but <b>not</b> activated...\n
78+
* Think of this method as the actual constructor...
79+
*/
80+
virtual void OnAttach();
81+
82+
/** Any descendent plugin should override this virtual method and
83+
* perform any necessary de-initialization. This method is called by
84+
* Code::Blocks (PluginManager actually) when the plugin has been
85+
* loaded, attached and should de-attach from Code::Blocks.\n
86+
* Think of this method as the actual destructor...
87+
* @param appShutDown If true, the application is shutting down. In this
88+
* case *don't* use Manager::Get()->Get...() functions or the
89+
* behaviour is undefined...
90+
*/
91+
virtual void OnRelease(bool appShutDown);
92+
private:
93+
void OnEditorBeforeSave(CodeBlocksEvent& event);
94+
void OnEditorSave(CodeBlocksEvent& event);
95+
wxString m_filename;
96+
mode_t m_permissions;
97+
98+
};
99+
100+
#endif // PERMISSIONS_PRESERVER_H_INCLUDED

0 commit comments

Comments
 (0)