10127ef0fSEd Maste //===-- FileAction.cpp ------------------------------------------*- C++ -*-===//
20127ef0fSEd Maste //
30127ef0fSEd Maste // The LLVM Compiler Infrastructure
40127ef0fSEd Maste //
50127ef0fSEd Maste // This file is distributed under the University of Illinois Open Source
60127ef0fSEd Maste // License. See LICENSE.TXT for details.
70127ef0fSEd Maste //
80127ef0fSEd Maste //===----------------------------------------------------------------------===//
90127ef0fSEd Maste
100127ef0fSEd Maste #include <fcntl.h>
110127ef0fSEd Maste
12435933ddSDimitry Andric #include "lldb/Host/PosixApi.h"
130127ef0fSEd Maste #include "lldb/Target/FileAction.h"
14*f678e45dSDimitry Andric #include "lldb/Utility/Stream.h"
150127ef0fSEd Maste
160127ef0fSEd Maste using namespace lldb_private;
170127ef0fSEd Maste
180127ef0fSEd Maste //----------------------------------------------------------------------------
190127ef0fSEd Maste // FileAction member functions
200127ef0fSEd Maste //----------------------------------------------------------------------------
210127ef0fSEd Maste
FileAction()22435933ddSDimitry Andric FileAction::FileAction()
23435933ddSDimitry Andric : m_action(eFileActionNone), m_fd(-1), m_arg(-1), m_file_spec() {}
240127ef0fSEd Maste
Clear()25435933ddSDimitry Andric void FileAction::Clear() {
260127ef0fSEd Maste m_action = eFileActionNone;
270127ef0fSEd Maste m_fd = -1;
280127ef0fSEd Maste m_arg = -1;
291c3bbb01SEd Maste m_file_spec.Clear();
300127ef0fSEd Maste }
310127ef0fSEd Maste
GetPath() const32435933ddSDimitry Andric llvm::StringRef FileAction::GetPath() const { return m_file_spec.GetCString(); }
331c3bbb01SEd Maste
GetFileSpec() const34435933ddSDimitry Andric const FileSpec &FileAction::GetFileSpec() const { return m_file_spec; }
350127ef0fSEd Maste
Open(int fd,const FileSpec & file_spec,bool read,bool write)36435933ddSDimitry Andric bool FileAction::Open(int fd, const FileSpec &file_spec, bool read,
37435933ddSDimitry Andric bool write) {
38435933ddSDimitry Andric if ((read || write) && fd >= 0 && file_spec) {
390127ef0fSEd Maste m_action = eFileActionOpen;
400127ef0fSEd Maste m_fd = fd;
410127ef0fSEd Maste if (read && write)
420127ef0fSEd Maste m_arg = O_NOCTTY | O_CREAT | O_RDWR;
430127ef0fSEd Maste else if (read)
440127ef0fSEd Maste m_arg = O_NOCTTY | O_RDONLY;
450127ef0fSEd Maste else
460127ef0fSEd Maste m_arg = O_NOCTTY | O_CREAT | O_WRONLY;
471c3bbb01SEd Maste m_file_spec = file_spec;
480127ef0fSEd Maste return true;
49435933ddSDimitry Andric } else {
500127ef0fSEd Maste Clear();
510127ef0fSEd Maste }
520127ef0fSEd Maste return false;
530127ef0fSEd Maste }
540127ef0fSEd Maste
Close(int fd)55435933ddSDimitry Andric bool FileAction::Close(int fd) {
560127ef0fSEd Maste Clear();
57435933ddSDimitry Andric if (fd >= 0) {
580127ef0fSEd Maste m_action = eFileActionClose;
590127ef0fSEd Maste m_fd = fd;
600127ef0fSEd Maste }
610127ef0fSEd Maste return m_fd >= 0;
620127ef0fSEd Maste }
630127ef0fSEd Maste
Duplicate(int fd,int dup_fd)64435933ddSDimitry Andric bool FileAction::Duplicate(int fd, int dup_fd) {
650127ef0fSEd Maste Clear();
66435933ddSDimitry Andric if (fd >= 0 && dup_fd >= 0) {
670127ef0fSEd Maste m_action = eFileActionDuplicate;
680127ef0fSEd Maste m_fd = fd;
690127ef0fSEd Maste m_arg = dup_fd;
700127ef0fSEd Maste }
710127ef0fSEd Maste return m_fd >= 0;
720127ef0fSEd Maste }
737aa51b79SEd Maste
Dump(Stream & stream) const74435933ddSDimitry Andric void FileAction::Dump(Stream &stream) const {
757aa51b79SEd Maste stream.PutCString("file action: ");
76435933ddSDimitry Andric switch (m_action) {
777aa51b79SEd Maste case eFileActionClose:
787aa51b79SEd Maste stream.Printf("close fd %d", m_fd);
797aa51b79SEd Maste break;
807aa51b79SEd Maste case eFileActionDuplicate:
817aa51b79SEd Maste stream.Printf("duplicate fd %d to %d", m_fd, m_arg);
827aa51b79SEd Maste break;
837aa51b79SEd Maste case eFileActionNone:
847aa51b79SEd Maste stream.PutCString("no action");
857aa51b79SEd Maste break;
867aa51b79SEd Maste case eFileActionOpen:
87435933ddSDimitry Andric stream.Printf("open fd %d with '%s', OFLAGS = 0x%x", m_fd,
88435933ddSDimitry Andric m_file_spec.GetCString(), m_arg);
897aa51b79SEd Maste break;
907aa51b79SEd Maste }
917aa51b79SEd Maste }
92