• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- FileAction.cpp ----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <fcntl.h>
10 
11 #include "lldb/Host/FileAction.h"
12 #include "lldb/Host/PosixApi.h"
13 #include "lldb/Utility/Stream.h"
14 
15 using namespace lldb_private;
16 
17 // FileAction member functions
18 
FileAction()19 FileAction::FileAction()
20     : m_action(eFileActionNone), m_fd(-1), m_arg(-1), m_file_spec() {}
21 
Clear()22 void FileAction::Clear() {
23   m_action = eFileActionNone;
24   m_fd = -1;
25   m_arg = -1;
26   m_file_spec.Clear();
27 }
28 
GetPath() const29 llvm::StringRef FileAction::GetPath() const { return m_file_spec.GetCString(); }
30 
GetFileSpec() const31 const FileSpec &FileAction::GetFileSpec() const { return m_file_spec; }
32 
Open(int fd,const FileSpec & file_spec,bool read,bool write)33 bool FileAction::Open(int fd, const FileSpec &file_spec, bool read,
34                       bool write) {
35   if ((read || write) && fd >= 0 && file_spec) {
36     m_action = eFileActionOpen;
37     m_fd = fd;
38     if (read && write)
39       m_arg = O_NOCTTY | O_CREAT | O_RDWR;
40     else if (read)
41       m_arg = O_NOCTTY | O_RDONLY;
42     else
43       m_arg = O_NOCTTY | O_CREAT | O_WRONLY;
44     m_file_spec = file_spec;
45     return true;
46   } else {
47     Clear();
48   }
49   return false;
50 }
51 
Close(int fd)52 bool FileAction::Close(int fd) {
53   Clear();
54   if (fd >= 0) {
55     m_action = eFileActionClose;
56     m_fd = fd;
57   }
58   return m_fd >= 0;
59 }
60 
Duplicate(int fd,int dup_fd)61 bool FileAction::Duplicate(int fd, int dup_fd) {
62   Clear();
63   if (fd >= 0 && dup_fd >= 0) {
64     m_action = eFileActionDuplicate;
65     m_fd = fd;
66     m_arg = dup_fd;
67   }
68   return m_fd >= 0;
69 }
70 
Dump(Stream & stream) const71 void FileAction::Dump(Stream &stream) const {
72   stream.PutCString("file action: ");
73   switch (m_action) {
74   case eFileActionClose:
75     stream.Printf("close fd %d", m_fd);
76     break;
77   case eFileActionDuplicate:
78     stream.Printf("duplicate fd %d to %d", m_fd, m_arg);
79     break;
80   case eFileActionNone:
81     stream.PutCString("no action");
82     break;
83   case eFileActionOpen:
84     stream.Printf("open fd %d with '%s', OFLAGS = 0x%x", m_fd,
85                   m_file_spec.GetCString(), m_arg);
86     break;
87   }
88 }
89