1 //===-- SBPlatform.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/API/SBPlatform.h"
11 #include "lldb/API/SBError.h"
12 #include "lldb/API/SBFileSpec.h"
13 #include "lldb/Core/ArchSpec.h"
14 #include "lldb/Core/Error.h"
15 #include "lldb/Host/File.h"
16 #include "lldb/Interpreter/Args.h"
17 #include "lldb/Target/Target.h"
18 #include "lldb/Target/Platform.h"
21 using namespace lldb_private;
23 //----------------------------------------------------------------------
24 // PlatformConnectOptions
25 //----------------------------------------------------------------------
26 struct PlatformConnectOptions {
27 PlatformConnectOptions(const char *url = NULL) :
30 m_rsync_remote_path_prefix(),
31 m_rsync_enabled(false),
32 m_rsync_omit_hostname_from_remote_path(false),
33 m_local_cache_directory ()
39 ~PlatformConnectOptions()
44 std::string m_rsync_options;
45 std::string m_rsync_remote_path_prefix;
47 bool m_rsync_omit_hostname_from_remote_path;
48 ConstString m_local_cache_directory;
51 //----------------------------------------------------------------------
52 // PlatformShellCommand
53 //----------------------------------------------------------------------
54 struct PlatformShellCommand {
55 PlatformShellCommand(const char *shell_command = NULL) :
60 m_timeout_sec(UINT32_MAX)
62 if (shell_command && shell_command[0])
63 m_command = shell_command;
66 ~PlatformShellCommand()
70 std::string m_command;
71 std::string m_working_dir;
75 uint32_t m_timeout_sec;
77 //----------------------------------------------------------------------
78 // SBPlatformConnectOptions
79 //----------------------------------------------------------------------
80 SBPlatformConnectOptions::SBPlatformConnectOptions (const char *url) :
81 m_opaque_ptr(new PlatformConnectOptions(url))
86 SBPlatformConnectOptions::SBPlatformConnectOptions(const SBPlatformConnectOptions &rhs) :
87 m_opaque_ptr(new PlatformConnectOptions())
89 *m_opaque_ptr = *rhs.m_opaque_ptr;
92 SBPlatformConnectOptions::~SBPlatformConnectOptions ()
98 SBPlatformConnectOptions::operator=(const SBPlatformConnectOptions &rhs)
100 *m_opaque_ptr = *rhs.m_opaque_ptr;
104 SBPlatformConnectOptions::GetURL()
106 if (m_opaque_ptr->m_url.empty())
108 return m_opaque_ptr->m_url.c_str();
112 SBPlatformConnectOptions::SetURL(const char *url)
115 m_opaque_ptr->m_url = url;
117 m_opaque_ptr->m_url.clear();
121 SBPlatformConnectOptions::GetRsyncEnabled()
123 return m_opaque_ptr->m_rsync_enabled;
127 SBPlatformConnectOptions::EnableRsync (const char *options,
128 const char *remote_path_prefix,
129 bool omit_hostname_from_remote_path)
131 m_opaque_ptr->m_rsync_enabled = true;
132 m_opaque_ptr->m_rsync_omit_hostname_from_remote_path = omit_hostname_from_remote_path;
133 if (remote_path_prefix && remote_path_prefix[0])
134 m_opaque_ptr->m_rsync_remote_path_prefix = remote_path_prefix;
136 m_opaque_ptr->m_rsync_remote_path_prefix.clear();
138 if (options && options[0])
139 m_opaque_ptr->m_rsync_options = options;
141 m_opaque_ptr->m_rsync_options.clear();
146 SBPlatformConnectOptions::DisableRsync ()
148 m_opaque_ptr->m_rsync_enabled = false;
152 SBPlatformConnectOptions::GetLocalCacheDirectory()
154 return m_opaque_ptr->m_local_cache_directory.GetCString();
158 SBPlatformConnectOptions::SetLocalCacheDirectory(const char *path)
161 m_opaque_ptr->m_local_cache_directory.SetCString(path);
163 m_opaque_ptr->m_local_cache_directory = ConstString();
166 //----------------------------------------------------------------------
167 // SBPlatformShellCommand
168 //----------------------------------------------------------------------
169 SBPlatformShellCommand::SBPlatformShellCommand (const char *shell_command) :
170 m_opaque_ptr(new PlatformShellCommand(shell_command))
174 SBPlatformShellCommand::SBPlatformShellCommand (const SBPlatformShellCommand &rhs) :
175 m_opaque_ptr(new PlatformShellCommand())
177 *m_opaque_ptr = *rhs.m_opaque_ptr;
180 SBPlatformShellCommand::~SBPlatformShellCommand()
186 SBPlatformShellCommand::Clear()
188 m_opaque_ptr->m_output = std::move(std::string());
189 m_opaque_ptr->m_status = 0;
190 m_opaque_ptr->m_signo = 0;
194 SBPlatformShellCommand::GetCommand()
196 if (m_opaque_ptr->m_command.empty())
198 return m_opaque_ptr->m_command.c_str();
202 SBPlatformShellCommand::SetCommand(const char *shell_command)
204 if (shell_command && shell_command[0])
205 m_opaque_ptr->m_command = shell_command;
207 m_opaque_ptr->m_command.clear();
211 SBPlatformShellCommand::GetWorkingDirectory ()
213 if (m_opaque_ptr->m_working_dir.empty())
215 return m_opaque_ptr->m_working_dir.c_str();
219 SBPlatformShellCommand::SetWorkingDirectory (const char *path)
222 m_opaque_ptr->m_working_dir = path;
224 m_opaque_ptr->m_working_dir.clear();
228 SBPlatformShellCommand::GetTimeoutSeconds ()
230 return m_opaque_ptr->m_timeout_sec;
234 SBPlatformShellCommand::SetTimeoutSeconds (uint32_t sec)
236 m_opaque_ptr->m_timeout_sec = sec;
240 SBPlatformShellCommand::GetSignal ()
242 return m_opaque_ptr->m_signo;
246 SBPlatformShellCommand::GetStatus ()
248 return m_opaque_ptr->m_status;
252 SBPlatformShellCommand::GetOutput ()
254 if (m_opaque_ptr->m_output.empty())
256 return m_opaque_ptr->m_output.c_str();
259 //----------------------------------------------------------------------
261 //----------------------------------------------------------------------
262 SBPlatform::SBPlatform () :
268 SBPlatform::SBPlatform (const char *platform_name) :
272 m_opaque_sp = Platform::Create (platform_name, error);
275 SBPlatform::~SBPlatform()
280 SBPlatform::IsValid () const
282 return m_opaque_sp.get() != NULL;
292 SBPlatform::GetName ()
294 PlatformSP platform_sp(GetSP());
296 return platform_sp->GetName().GetCString();
301 SBPlatform::GetSP () const
307 SBPlatform::SetSP (const lldb::PlatformSP& platform_sp)
309 m_opaque_sp = platform_sp;
313 SBPlatform::GetWorkingDirectory()
315 PlatformSP platform_sp(GetSP());
317 return platform_sp->GetWorkingDirectory().GetCString();
322 SBPlatform::SetWorkingDirectory(const char *path)
324 PlatformSP platform_sp(GetSP());
328 platform_sp->SetWorkingDirectory(ConstString(path));
330 platform_sp->SetWorkingDirectory(ConstString());
337 SBPlatform::ConnectRemote (SBPlatformConnectOptions &connect_options)
340 PlatformSP platform_sp(GetSP());
341 if (platform_sp && connect_options.GetURL())
344 args.AppendArgument(connect_options.GetURL());
345 sb_error.ref() = platform_sp->ConnectRemote(args);
349 sb_error.SetErrorString("invalid platform");
355 SBPlatform::DisconnectRemote ()
357 PlatformSP platform_sp(GetSP());
359 platform_sp->DisconnectRemote();
363 SBPlatform::IsConnected()
365 PlatformSP platform_sp(GetSP());
367 platform_sp->IsConnected();
372 SBPlatform::GetTriple()
374 PlatformSP platform_sp(GetSP());
377 ArchSpec arch(platform_sp->GetRemoteSystemArchitecture());
380 // Const-ify the string so we don't need to worry about the lifetime of the string
381 return ConstString(arch.GetTriple().getTriple().c_str()).GetCString();
388 SBPlatform::GetOSBuild()
390 PlatformSP platform_sp(GetSP());
394 if (platform_sp->GetOSBuildString(s))
398 // Const-ify the string so we don't need to worry about the lifetime of the string
399 return ConstString(s.c_str()).GetCString();
407 SBPlatform::GetOSDescription()
409 PlatformSP platform_sp(GetSP());
413 if (platform_sp->GetOSKernelDescription(s))
417 // Const-ify the string so we don't need to worry about the lifetime of the string
418 return ConstString(s.c_str()).GetCString();
426 SBPlatform::GetHostname ()
428 PlatformSP platform_sp(GetSP());
430 return platform_sp->GetHostname();
435 SBPlatform::GetOSMajorVersion ()
437 uint32_t major, minor, update;
438 PlatformSP platform_sp(GetSP());
439 if (platform_sp && platform_sp->GetOSVersion(major, minor, update))
446 SBPlatform::GetOSMinorVersion ()
448 uint32_t major, minor, update;
449 PlatformSP platform_sp(GetSP());
450 if (platform_sp && platform_sp->GetOSVersion(major, minor, update))
456 SBPlatform::GetOSUpdateVersion ()
458 uint32_t major, minor, update;
459 PlatformSP platform_sp(GetSP());
460 if (platform_sp && platform_sp->GetOSVersion(major, minor, update))
466 SBPlatform::Get (SBFileSpec &src,
470 PlatformSP platform_sp(GetSP());
473 sb_error.ref() = platform_sp->GetFile(src.ref(), dst.ref());
477 sb_error.SetErrorString("invalid platform");
483 SBPlatform::Put (SBFileSpec &src,
488 PlatformSP platform_sp(GetSP());
493 uint32_t permissions = src.ref().GetPermissions();
494 if (permissions == 0)
496 if (src.ref().GetFileType() == FileSpec::eFileTypeDirectory)
497 permissions = eFilePermissionsDirectoryDefault;
499 permissions = eFilePermissionsFileDefault;
502 sb_error.ref() = platform_sp->PutFile(src.ref(),
508 sb_error.ref().SetErrorStringWithFormat("'src' argument doesn't exist: '%s'", src.ref().GetPath().c_str());
513 sb_error.SetErrorString("invalid platform");
519 SBPlatform::Install (SBFileSpec &src,
523 PlatformSP platform_sp(GetSP());
528 sb_error.ref() = platform_sp->Install(src.ref(), dst.ref());
532 sb_error.ref().SetErrorStringWithFormat("'src' argument doesn't exist: '%s'", src.ref().GetPath().c_str());
537 sb_error.SetErrorString("invalid platform");
544 SBPlatform::Run (SBPlatformShellCommand &shell_command)
547 PlatformSP platform_sp(GetSP());
550 if (platform_sp->IsConnected())
552 const char *command = shell_command.GetCommand();
555 const char *working_dir = shell_command.GetWorkingDirectory();
556 if (working_dir == NULL)
558 working_dir = platform_sp->GetWorkingDirectory().GetCString();
560 shell_command.SetWorkingDirectory(working_dir);
562 sb_error.ref() = platform_sp->RunShellCommand(command,
564 &shell_command.m_opaque_ptr->m_status,
565 &shell_command.m_opaque_ptr->m_signo,
566 &shell_command.m_opaque_ptr->m_output,
567 shell_command.m_opaque_ptr->m_timeout_sec);
571 sb_error.SetErrorString("invalid shell command (empty)");
576 sb_error.SetErrorString("not connected");
581 sb_error.SetErrorString("invalid platform");
587 SBPlatform::MakeDirectory (const char *path, uint32_t file_permissions)
590 PlatformSP platform_sp(GetSP());
593 sb_error.ref() = platform_sp->MakeDirectory(path, file_permissions);
597 sb_error.SetErrorString("invalid platform");
603 SBPlatform::GetFilePermissions (const char *path)
605 PlatformSP platform_sp(GetSP());
608 uint32_t file_permissions = 0;
609 platform_sp->GetFilePermissions(path, file_permissions);
610 return file_permissions;
617 SBPlatform::SetFilePermissions (const char *path, uint32_t file_permissions)
620 PlatformSP platform_sp(GetSP());
623 sb_error.ref() = platform_sp->SetFilePermissions(path, file_permissions);
627 sb_error.SetErrorString("invalid platform");