xref: /haiku/src/bin/bfs_tools/lib/Inode.h (revision 220d04022750f40f8bac8f01fa551211e28d04f2)
1 /*
2  * Copyright 2001-2008, pinc Software. All Rights Reserved.
3  */
4 #ifndef INODE_H
5 #define INODE_H
6 
7 
8 #include <SupportDefs.h>
9 
10 #include "Disk.h"
11 
12 class BPlusTree;
13 class Directory;
14 
15 
16 class Inode {
17 	public:
18 		Inode(Disk* disk, bfs_inode* inode, bool ownBuffer = true);
19 		Inode(const Inode &inode);
20 		virtual ~Inode();
21 
22 		status_t SetTo(bfs_inode *inode);
23 		virtual status_t InitCheck();
24 
25 		bool			IsFile() const { return S_ISREG(fInode->mode); }
26 		bool			IsDirectory() const { return S_ISDIR(fInode->mode); }
27 		bool			IsSymlink() const { return S_ISLNK(fInode->mode); }
28 		bool			IsIndex() const { return S_ISINDEX(fInode->mode); }
29 		bool			IsAttribute() const
30 							{ return (fInode->mode & S_ATTR) != 0; }
31 		bool			IsAttributeDirectory() const
32 							{ return (fInode->mode & S_ATTR_DIR) != 0; }
33 		bool			IsRoot() const { return BlockRun() == fDisk->Root(); }
34 
35 		int32			Mode() const { return fInode->mode; }
36 		int32			Flags() const { return fInode->flags; }
37 		off_t			Size() const { return fInode->data.size; }
38 
39 		off_t			Offset() const
40 							{ return fDisk->ToOffset(BlockRun()); }
41 		off_t			Block() const { return fDisk->ToBlock(BlockRun()); }
42 		const block_run& BlockRun() const { return fBlockRun; }
43 		block_run		Parent() const { return fInode->parent; }
44 		block_run		Attributes() const { return fInode->attributes; }
45 
46 		const bfs_inode* InodeBuffer() const { return fInode; }
47 		status_t		CopyBuffer();
48 
49 		void			ReleaseBuffer();
50 		status_t		AcquireBuffer();
51 		void			BufferClobbered();
52 
53 		void			SetParent(const block_run& run);
54 		void			SetBlockRun(const block_run& run);
55 		void			SetMode(uint32 mode);
56 
57 		status_t		SetName(const char* name);
58 		const char*		Name() const;
59 		status_t		GetNextSmallData(small_data** smallData);
60 
61 		status_t		RewindAttributes();
62 		status_t		GetNextAttribute(char* name, uint32* type, void** data,
63 							size_t* length);
64 
65 		class Source;
66 		const char*		Path(Inode::Source* source = NULL);
67 		virtual status_t CopyTo(const char* path, bool fullPath = true,
68 							Inode::Source* source = NULL);
69 		status_t		CopyAttributesTo(BNode* node);
70 
71 		static Inode*	Factory(Disk* disk, bfs_inode* inode,
72 							bool ownBuffer = true);
73 		static Inode*	Factory(Disk* disk, block_run run);
74 		static Inode*	Factory(Disk* disk, Inode* inode,
75 							bool copyBuffer = true);
76 
77 		static Inode*	EmptyInode(Disk* disk,const char* name, int32 mode);
78 
79 		class Source {
80 			public:
81 				virtual Inode *InodeAt(block_run run) = 0;
82 		};
83 
84 	protected:
85 		static bool	_LowMemory();
86 		void		_Unset();
87 		status_t	_FindPath(Inode::Source *source = NULL);
88 
89 		Disk		*fDisk;
90 		bfs_inode	*fInode;
91 		bool		fOwnBuffer;
92 		char		*fPath;
93 		block_run	fBlockRun;
94 		int32		fRefCount;
95 
96 		small_data	*fCurrentSmallData;
97 		Directory	*fAttributes;
98 		void		*fAttributeBuffer;
99 };
100 
101 
102 class DataStream : public Inode, public BPositionIO {
103 	public:
104 		DataStream(Disk *disk, bfs_inode *inode, bool ownBuffer = true);
105 		DataStream(const Inode &inode);
106 		~DataStream();
107 
108 		status_t			FindBlockRun(off_t pos);
109 
110 		virtual ssize_t		ReadAt(off_t pos, void *buffer, size_t size);
111 		virtual ssize_t		WriteAt(off_t pos, const void *buffer, size_t size);
112 
113 		virtual off_t		Seek(off_t position, uint32 seek_mode);
114 		virtual off_t		Position() const;
115 
116 		virtual status_t	SetSize(off_t size);
117 
118 	private:
119 		int32		fCurrent;
120 		int32		fLevel;
121 		block_run	fRun;
122 		off_t		fRunFileOffset;
123 		off_t		fRunBlockEnd;
124 		off_t		fPosition;
125 };
126 
127 
128 class File : public DataStream {
129 	public:
130 		File(Disk *disk, bfs_inode *inode, bool ownBuffer = true);
131 		File(const Inode &inode);
132 		~File();
133 
134 		virtual status_t	InitCheck();
135 		virtual status_t	CopyTo(const char *path, bool fullPath = true,
136 								Inode::Source *source = NULL);
137 };
138 
139 
140 class Attribute : public File {
141 	public:
142 		Attribute(Disk *disk, bfs_inode *inode, bool ownBuffer = true);
143 		Attribute(const Inode &inode);
144 		~Attribute();
145 
146 		virtual status_t	InitCheck();
147 		virtual status_t	CopyTo(const char *path, bool fullPath = true,
148 								Inode::Source *source = NULL);
149 
150 		uint32 Type() const { return fInode->type; }
151 };
152 
153 
154 class Directory : public DataStream {
155 	public:
156 		Directory(Disk *disk, bfs_inode *inode, bool ownBuffer = true);
157 		Directory(const Inode &inode);
158 		~Directory();
159 
160 		virtual status_t	InitCheck();
161 		virtual status_t	CopyTo(const char *path, bool fullPath = true,
162 								Inode::Source *source = NULL);
163 
164 		virtual status_t	Rewind();
165 		virtual status_t	GetNextEntry(char *name, block_run *run);
166 		virtual status_t	GetNextEntry(block_run *run);
167 
168 		virtual status_t	Contains(const block_run *run);
169 		virtual status_t	Contains(const Inode *inode);
170 		virtual status_t	FindEntry(const char *name, block_run *run);
171 
172 		virtual status_t	AddEntry(Inode *inode);
173 
174 		status_t			GetTree(BPlusTree **tree);
175 
176 	private:
177 		virtual status_t	CreateTree();
178 
179 		BPlusTree	*fTree;
180 };
181 
182 
183 class Symlink : public Inode {
184 	public:
185 		Symlink(Disk *disk, bfs_inode *inode, bool ownBuffer = true);
186 		Symlink(const Inode &inode);
187 		~Symlink();
188 
189 		virtual status_t	InitCheck();
190 		virtual status_t	CopyTo(const char *path, bool fullPath = true,
191 								Inode::Source *source = NULL);
192 
193 		status_t			LinksTo(char *to, size_t maxLength);
194 
195 	private:
196 		char	*fTo;
197 };
198 
199 #endif	/* INODE_H */
200