Code::Blocks  SVN r11506
sc_io.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
3  * http://www.gnu.org/licenses/lgpl-3.0.html
4  *
5  * $Revision: 9426 $
6  * $Id: sc_io.cpp 9426 2013-11-02 19:42:20Z alpha0010 $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/sdk/scripting/bindings/sc_io.cpp $
8  */
9 
10 #include <sdk_precomp.h>
11 #include <manager.h>
12 #include <macrosmanager.h>
13 #include <configmanager.h>
14 #include <annoyingdialog.h>
15 
16 #ifndef CB_PRECOMP
17  #include <globals.h>
18  #include <wx/string.h>
19  #include <wx/filedlg.h>
20 #endif
21 
22 #include <wx/filename.h>
23 #include <wx/utils.h>
24 
26 #include "sc_base_types.h"
27 
28 namespace ScriptBindings
29 {
30  namespace IOLib
31  {
32  // not exposed
33  bool SecurityAllows(const wxString& operation, const wxString& descr)
34  {
35  if (Manager::Get()->GetScriptingManager()->IsCurrentlyRunningScriptTrusted())
36  return true;
37 
38  if (Manager::Get()->GetConfigManager(_T("security"))->ReadBool(operation, false))
39  return true;
40 
41  ScriptSecurityWarningDlg dlg(Manager::Get()->GetAppWindow(), operation, descr);
42  if (dlg.ShowModal() != wxID_OK)
43  return false;
44 
45  ScriptSecurityResponse response = dlg.GetResponse();
46  switch (response)
47  {
48  case ssrAllow:
49  return true;
50 
51  case ssrAllowAll:
52  Manager::Get()->GetConfigManager(_T("security"))->Write(operation, true);
53  return true;
54 
55  case ssrTrust: // purposely fall through
58  return true;
59 
60  default:
61  return false;
62  }
63  return false;
64  }
65 
67  {
68  return wxGetCwd();
69  }
70 
71  void SetCwd(const wxString& dir)
72  {
74  }
75 
76  bool CreateDirRecursively(const wxString& full_path, int perms)
77  {
78  wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(full_path));
80  if (!SecurityAllows(_T("CreateDir"), fname.GetFullPath()))
81  return false;
83  }
84 
85  wxString ChooseDir(const wxString& message, const wxString& initialPath, bool showCreateDirButton)
86  {
87  return ChooseDirectory(nullptr, message, Manager::Get()->GetMacrosManager()->ReplaceMacros(initialPath), wxEmptyString, false, showCreateDirButton);
88  }
89 
90  bool RemoveDir(const wxString& src)
91  {
92  wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(src));
94  if (!SecurityAllows(_T("RemoveDir"), fname.GetFullPath()))
95  return false;
96  return wxRmdir(fname.GetFullPath());
97  }
98 
99  bool DirectoryExists(const wxString& dir)
100  {
101  wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(dir));
103  return wxDirExists(fname.GetFullPath());
104  }
105 
106  bool CopyFile(const wxString& src, const wxString& dst, bool overwrite)
107  {
108  wxFileName fname1(Manager::Get()->GetMacrosManager()->ReplaceMacros(src));
109  wxFileName fname2(Manager::Get()->GetMacrosManager()->ReplaceMacros(dst));
110  NormalizePath(fname1, wxEmptyString);
111  NormalizePath(fname2, wxEmptyString);
112  if (!SecurityAllows(_T("CopyFile"), wxString::Format(_T("%s -> %s"), src.c_str(), dst.c_str())))
113  return false;
114  if (!wxFileExists(fname1.GetFullPath())) return false;
115  return wxCopyFile(fname1.GetFullPath(),
116  fname2.GetFullPath(),
117  overwrite);
118  }
119 
120  bool RenameFile(const wxString& src, const wxString& dst)
121  {
122  wxFileName fname1(Manager::Get()->GetMacrosManager()->ReplaceMacros(src));
123  wxFileName fname2(Manager::Get()->GetMacrosManager()->ReplaceMacros(dst));
124  NormalizePath(fname1, wxEmptyString);
125  NormalizePath(fname2, wxEmptyString);
126  if (!SecurityAllows(_T("RenameFile"), wxString::Format(_T("%s -> %s"),
127  fname1.GetFullPath().c_str(), fname2.GetFullPath().c_str())))
128  return false;
129  if (!wxFileExists(fname1.GetFullPath())) return false;
130  return wxRenameFile(fname1.GetFullPath(),
131  fname2.GetFullPath());
132  }
133 
134  bool RemoveFile(const wxString& src)
135  {
136  wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(src));
138  if (!SecurityAllows(_T("RemoveFile"), fname.GetFullPath()))
139  return false;
140  if (!wxFileExists(fname.GetFullPath())) return false;
141  return wxRemoveFile(fname.GetFullPath());
142  }
143 
144  bool FileExists(const wxString& file)
145  {
146  wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(file));
148  return wxFileExists(fname.GetFullPath());
149  }
150 
151  wxString ChooseFile(const wxString& title, const wxString& defaultFile, const wxString& filter)
152  {
153  wxFileDialog dlg(nullptr,
154  title,
156  Manager::Get()->GetMacrosManager()->ReplaceMacros(defaultFile),
157  filter,
158  wxFD_OPEN | compatibility::wxHideReadonly);
159  PlaceWindow(&dlg);
160  if (dlg.ShowModal() == wxID_OK)
161  return dlg.GetPath();
162  return wxEmptyString;
163  }
164 
166  {
167  wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(filename));
169  wxFile f(fname.GetFullPath());
170  return cbReadFileContents(f);
171  }
172 
173  bool WriteFileContents(const wxString& filename, const wxString& contents)
174  {
175  wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(filename));
177  if (!SecurityAllows(_T("CreateFile"), fname.GetFullPath()))
178  return false;
179  wxFile f(fname.GetFullPath(), wxFile::write);
180  return cbWrite(f, contents);
181  }
182 
183  int Execute(const wxString& command)
184  {
185  if (!SecurityAllows(_T("Execute"), command))
186  return -1;
187  wxArrayString output;
188  return wxExecute(command, output, wxEXEC_NODISABLE);
189  }
190 
192  {
193  if (!SecurityAllows(_T("Execute"), command))
194  return wxEmptyString;
195 
196  wxArrayString output;
197  wxExecute(command, output, wxEXEC_NODISABLE);
198 
199  return GetStringFromArray(output, _T("\n"));
200  }
201 
202  wxString ExecuteAndGetOutputAndError(const wxString& command, bool prepend_error = true)
203  {
204  if (!SecurityAllows(_T("Execute"), command))
205  return wxEmptyString;
206 
207  wxArrayString output;
208  wxArrayString error;
209  wxExecute(command, output, error, wxEXEC_NODISABLE);
210 
211  wxString str_out;
212 
213  if ( prepend_error && !error.IsEmpty())
214  str_out += GetStringFromArray(error, _T("\n"));
215 
216  if (!output.IsEmpty())
217  str_out += GetStringFromArray(output, _T("\n"));
218 
219  if (!prepend_error && !error.IsEmpty())
220  str_out += GetStringFromArray(error, _T("\n"));
221 
222  return str_out;
223  }
224 
225 
226  } // namespace IOLib
227 } // namespace ScriptBindings
228 
229 namespace ScriptBindings
230 {
231  struct IONamespace {};
232 
233  void Register_IO()
234  {
235  SqPlus::SQClassDef<IONamespace>("IO").
236 
237  #ifndef NO_INSECURE_SCRIPTS
238  staticFunc(&IOLib::CreateDirRecursively, "CreateDirectory").
239  staticFunc(&IOLib::RemoveDir, "RemoveDirectory").
240  staticFunc(&IOLib::CopyFile, "CopyFile").
241  staticFunc(&IOLib::RenameFile, "RenameFile").
242  staticFunc(&IOLib::RemoveFile, "RemoveFile").
243  staticFunc(&IOLib::WriteFileContents, "WriteFileContents").
244  staticFunc(&IOLib::Execute, "Execute").
245  staticFunc(&IOLib::ExecuteAndGetOutput, "ExecuteAndGetOutput").
246  staticFunc(&IOLib::ExecuteAndGetOutputAndError, "ExecuteAndGetOutputAndError").
247  #endif // NO_INSECURE_SCRIPTS
248 
249  staticFunc(&IOLib::GetCwd, "GetCwd").
250  staticFunc(&IOLib::SetCwd, "SetCwd").
251 
252  staticFunc(&IOLib::DirectoryExists, "DirectoryExists").
253  staticFunc(&IOLib::ChooseDir, "SelectDirectory").
254  staticFunc(&IOLib::FileExists, "FileExists").
255  staticFunc(&IOLib::ChooseFile, "SelectFile").
256  staticFunc(&IOLib::ReadFileContents, "ReadFileContents");
257 
258  #ifndef NO_INSECURE_SCRIPTS
259  SqPlus::BindConstant(true, "allowInsecureScripts");
260  #else
261  SqPlus::BindConstant(false, "allowInsecureScripts");
262  #endif // NO_INSECURE_SCRIPTS
263  }
264 } // namespace ScriptBindings
bool wxRmdir(const wxString &dir, int flags=0)
int Execute(const wxString &command)
Definition: sc_io.cpp:183
bool wxRenameFile(const wxString &file1, const wxString &file2, bool overwrite=true)
virtual int ShowModal()
bool wxRemoveFile(const wxString &file)
ConfigManager * GetConfigManager(const wxString &name_space) const
Definition: manager.cpp:474
bool WriteFileContents(const wxString &filename, const wxString &contents)
Definition: sc_io.cpp:173
static Manager * Get()
Use Manager::Get() to get a pointer to its instance Manager::Get() is guaranteed to never return an i...
Definition: manager.cpp:182
wxString ExecuteAndGetOutput(const wxString &command)
Definition: sc_io.cpp:191
ScriptSecurityResponse
bool wxFileExists(const wxString &filename)
bool DirectoryExists(const wxString &dir)
Definition: sc_io.cpp:99
DLLIMPORT wxString ChooseDirectory(wxWindow *parent, const wxString &message=_("Select directory"), const wxString &initialPath=_T(""), const wxString &basePath=_T(""), bool askToMakeRelative=false, bool showCreateDirButton=false)
Definition: globals.cpp:639
DLLIMPORT bool NormalizePath(wxFileName &f, const wxString &base)
Definition: globals.cpp:942
wxCStrData c_str() const
bool wxDirExists(const wxString &dirname)
#define _T(string)
DLLIMPORT wxString GetStringFromArray(const wxArrayString &array, const wxString &separator=DEFAULT_ARRAY_SEP, bool SeparatorAtEnd=true)
Definition: globals.cpp:122
void TrustCurrentlyRunningScript(bool permanently)
Security function to trust a script.
DLLIMPORT wxString cbReadFileContents(wxFile &file, wxFontEncoding encoding=wxFONTENCODING_SYSTEM)
Reads a wxString from a non-unicode file. File must be open. File is closed automatically.
Definition: globals.cpp:697
void Write(const wxString &name, const wxString &value, bool ignoreEmpty=false)
wxString ReadFileContents(const wxString &filename)
Definition: sc_io.cpp:165
wxString ChooseFile(const wxString &title, const wxString &defaultFile, const wxString &filter)
Definition: sc_io.cpp:151
bool wxSetWorkingDirectory(const wxString &dir)
void Register_IO()
Definition: sc_io.cpp:233
bool wxCopyFile(const wxString &file1, const wxString &file2, bool overwrite=true)
bool IsEmpty() const
void SetCwd(const wxString &dir)
Definition: sc_io.cpp:71
virtual int ShowModal()
DLLIMPORT bool cbWrite(wxFile &file, const wxString &buff, wxFontEncoding encoding=wxFONTENCODING_SYSTEM)
Writes a wxString to a non-unicode file. File must be open. File is closed automatically.
Definition: globals.cpp:705
bool RemoveDir(const wxString &src)
Definition: sc_io.cpp:90
wxString wxEmptyString
virtual wxString GetPath() const
bool SecurityAllows(const wxString &operation, const wxString &descr)
Definition: sc_io.cpp:33
wxString GetCwd()
Definition: sc_io.cpp:66
DLLIMPORT void PlaceWindow(wxTopLevelWindow *w, cbPlaceDialogMode mode=pdlBest, bool enforce=false)
Definition: globals.cpp:1177
bool RemoveFile(const wxString &src)
Definition: sc_io.cpp:134
bool CreateDirRecursively(const wxString &full_path, int perms)
Definition: sc_io.cpp:76
ScriptingManager * GetScriptingManager() const
Definition: manager.cpp:469
wxString ExecuteAndGetOutputAndError(const wxString &command, bool prepend_error=true)
Definition: sc_io.cpp:202
wxString GetFullPath(wxPathFormat format=wxPATH_NATIVE) const
ScriptSecurityResponse GetResponse()
wxString ChooseDir(const wxString &message, const wxString &initialPath, bool showCreateDirButton)
Definition: sc_io.cpp:85
bool RenameFile(const wxString &src, const wxString &dst)
Definition: sc_io.cpp:120
static wxString Format(const wxString &format,...)
bool FileExists(const wxString &file)
Definition: sc_io.cpp:144
bool CopyFile(const wxString &src, const wxString &dst, bool overwrite)
Definition: sc_io.cpp:106
long wxExecute(const wxString &command, int flags=wxEXEC_ASYNC, wxProcess *callback=NULL, const wxExecuteEnv *env=NULL)
wxString wxGetCwd()