1 /* 2 * Copyright 2005-2011, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef FS_DESCRIPTORS_H 6 #define FS_DESCRIPTORS_H 7 8 #include <dirent.h> 9 10 #include <string> 11 12 #include <StorageDefs.h> 13 #include <SupportDefs.h> 14 15 #include "NodeRef.h" 16 17 using std::string; 18 19 struct stat; 20 21 namespace BPrivate { 22 23 // Descriptor 24 struct Descriptor { 25 int fd; 26 27 virtual ~Descriptor(); 28 29 virtual status_t Close() = 0; 30 virtual status_t Dup(Descriptor *&clone) = 0; 31 virtual status_t GetStat(bool traverseLink, struct stat *st) = 0; 32 33 virtual bool IsSystemFD() const; 34 virtual status_t GetPath(string& path) const; 35 36 virtual status_t GetNodeRef(NodeRef &ref); 37 }; 38 39 // FileDescriptor 40 struct FileDescriptor : Descriptor { 41 FileDescriptor(int fd); 42 virtual ~FileDescriptor(); 43 44 virtual status_t Close(); 45 virtual status_t Dup(Descriptor *&clone); 46 virtual status_t GetStat(bool traverseLink, struct stat *st); 47 48 virtual bool IsSystemFD() const; 49 }; 50 51 // DirectoryDescriptor 52 struct DirectoryDescriptor : Descriptor { 53 DIR *dir; 54 NodeRef ref; 55 56 DirectoryDescriptor(DIR *dir, const NodeRef &ref); 57 virtual ~DirectoryDescriptor(); 58 59 virtual status_t Close(); 60 virtual status_t Dup(Descriptor *&clone); 61 virtual status_t GetStat(bool traverseLink, struct stat *st); 62 63 virtual status_t GetNodeRef(NodeRef &ref); 64 }; 65 66 // SymlinkDescriptor 67 struct SymlinkDescriptor : Descriptor { 68 string path; 69 70 SymlinkDescriptor(const char *path); 71 72 virtual status_t Close(); 73 virtual status_t Dup(Descriptor *&clone); 74 virtual status_t GetStat(bool traverseLink, struct stat *st); 75 76 virtual status_t GetPath(string& path) const; 77 }; 78 79 // AttributeDescriptor 80 struct AttributeDescriptor : Descriptor { 81 AttributeDescriptor(int fileFD, 82 const char* attribute, uint32 type, 83 int openMode); 84 virtual ~AttributeDescriptor(); 85 86 status_t Init(); 87 status_t Write(off_t offset, const void* buffer, 88 size_t bufferSize); 89 90 int FileFD() const { return fFileFD; } 91 const char* Attribute() const { return fAttribute; } 92 uint32 Type() const { return fType; } 93 int OpenMode() const { return fOpenMode; } 94 95 virtual status_t Close(); 96 virtual status_t Dup(Descriptor*& clone); 97 virtual status_t GetStat(bool traverseLink, struct stat* st); 98 99 private: 100 int fFileFD; 101 char fAttribute[B_ATTR_NAME_LENGTH]; 102 uint32 fType; 103 int fOpenMode; 104 uint8* fData; 105 size_t fDataSize; 106 }; 107 108 // AttrDirDescriptor 109 struct AttrDirDescriptor : DirectoryDescriptor { 110 111 AttrDirDescriptor(DIR *dir, const NodeRef &ref); 112 virtual ~AttrDirDescriptor(); 113 114 virtual status_t Close(); 115 virtual status_t Dup(Descriptor *&clone); 116 virtual status_t GetStat(bool traverseLink, struct stat *st); 117 118 virtual status_t GetNodeRef(NodeRef &ref); 119 }; 120 121 122 Descriptor* get_descriptor(int fd); 123 int add_descriptor(Descriptor *descriptor); 124 status_t delete_descriptor(int fd); 125 bool is_unknown_or_system_descriptor(int fd); 126 127 } // namespace BPrivate 128 129 #endif // FS_DESCRIPTORS_H 130