1 // DirItem.h 2 // 3 // Copyright (c) 2003, Ingo Weinhold (bonefish@cs.tu-berlin.de) 4 // 5 // This program is free software; you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation; either version 2 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with this program; if not, write to the Free Software 17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 // 19 // You can alternatively use *this file* under the terms of the the MIT 20 // license included in this package. 21 22 #ifndef DIR_ITEM_H 23 #define DIR_ITEM_H 24 25 #include <string.h> 26 27 #include "Block.h" 28 #include "Debug.h" 29 #include "endianess.h" 30 #include "Item.h" 31 #include "String.h" 32 33 // DirEntry 34 /*! 35 \class DirEntry 36 \brief Represents the on-disk structure for a directory entry. 37 38 A DirEntry has an offset identifying it uniquely in the list of 39 entries, and it knows about the dir and object ID of the actual 40 entry. Note, that the dir ID is note necessarily the object ID of 41 the parent directory! The DirEntry also knows the relative 42 location of its name in the dir item. 43 */ 44 class DirEntry : private reiserfs_de_head { 45 public: 46 DirEntry() {} 47 48 uint32 GetOffset() const { return le2h(deh_offset); } 49 uint32 GetDirID() const { return le2h(deh_dir_id); } 50 uint32 GetObjectID() const { return le2h(deh_objectid); } 51 uint16 GetLocation() const { return le2h(deh_location); } 52 uint16 GetState() const { return le2h(deh_state); } 53 54 bool IsVisible() const { return (GetState() & (1 << DEH_Visible)); } 55 bool IsHidden() const { return !IsVisible(); } 56 57 void Dump() 58 { 59 PRINT((" dir entry\n")); 60 PRINT((" offset: %lu\n", GetOffset())); 61 PRINT((" dir ID: %lu\n", GetDirID())); 62 PRINT((" object ID: %lu\n", GetObjectID())); 63 PRINT((" location: %hu\n", GetLocation())); 64 PRINT((" state: %hx\n", GetState())); 65 } 66 } _PACKED; 67 68 // DirItem 69 /*! 70 \class DirItem 71 \brief Provides access to the on-disk dir item structure. 72 73 A dir item consists of an array of DirEntrys and the names of these 74 entries. Note, that in general the names are not null terminated. 75 EntryNameAt() returns the length of the name. 76 */ 77 class DirItem : public Item { 78 public: 79 DirItem() : Item() {} 80 DirItem(LeafNode *node, ItemHeader *header) 81 : Item(node, header) {} 82 83 DirEntry *EntryAt(int32 index) const 84 { 85 DirEntry *entry = NULL; 86 if (index >= 0 && index < GetEntryCount()) 87 entry = (DirEntry*)GetData() + index; 88 return entry; 89 } 90 91 const char *EntryNameAt(int32 index, size_t *nameLen = NULL) const 92 { 93 const char *name = NULL; 94 if (DirEntry *entry = EntryAt(index)) { 95 // check the name location 96 uint32 location = entry->GetLocation(); 97 if (location < GetEntryNameSpaceOffset() || location > GetLen()) { 98 // bad location 99 FATAL(("WARNING: bad dir entry %ld in item %ld on node %Ld: " 100 "the entry's name location is %lu, which is outside the" 101 "entry name space (%lu - %u)!\n", index, GetIndex(), 102 fNode->GetNumber(), location, GetEntryNameSpaceOffset(), 103 GetLen())); 104 } else { 105 // get the name 106 name = (char*)((uint8*)GetData() + location); 107 } 108 if (name && nameLen) { 109 size_t maxLength = 0; 110 if (index == 0) 111 maxLength = fHeader->GetLen() - entry->GetLocation(); 112 else { 113 maxLength = EntryAt(index -1)->GetLocation() 114 - entry->GetLocation(); 115 } 116 *nameLen = strnlen(name, maxLength); 117 } 118 } 119 return name; 120 } 121 122 status_t GetEntryNameAt(int32 index, char *buffer, size_t bufferSize) 123 { 124 status_t error = (buffer && index >= 0 && index < GetEntryCount() 125 ? B_OK : B_BAD_VALUE); 126 if (error == B_OK) { 127 size_t nameLen = 0; 128 const char *name = EntryNameAt(index, &nameLen); 129 if (name && nameLen > 0) { 130 if (nameLen + 1 <= bufferSize) { 131 strncpy(buffer, name, nameLen); 132 buffer[nameLen] = 0; 133 } else // buffer too small 134 error = B_BAD_VALUE; 135 } else // bad name 136 error = B_BAD_DATA; 137 } 138 return error; 139 } 140 141 int32 IndexOfName(const char *name) const 142 { 143 int32 count = GetEntryCount(); 144 size_t len = strlen(name); 145 for (int32 i = 0; i < count; i++) { 146 size_t nameLen = 0; 147 const char *itemName = EntryNameAt(i, &nameLen); 148 if (name && nameLen == len && !strncmp(name, itemName, len)) { 149 return i; 150 } 151 } 152 return -1; 153 } 154 155 status_t Check() const 156 { 157 // the base class version checks the location of the item 158 status_t error = Item::Check(); 159 // check whether the entry headers can possibly fit into the item 160 if (error == B_OK) { 161 if (GetEntryNameSpaceOffset() > GetLen()) { 162 FATAL(("WARNING: bad dir item %ld on node %Ld: the item has " 163 "len %u and can thus impossibly contain %u entry " 164 "headers!\n", GetIndex(), fNode->GetNumber(), GetLen(), 165 GetEntryCount())); 166 return B_BAD_DATA; 167 } 168 } 169 return error; 170 } 171 172 private: 173 uint32 GetEntryNameSpaceOffset() const 174 { return GetEntryCount() * sizeof(DirEntry); } 175 }; 176 177 #endif // DIR_ITEM_H 178