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
17using std::string;
18
19struct stat;
20
21namespace BPrivate {
22
23// Descriptor
24struct 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
40struct 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
52struct 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
67struct 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
80struct 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
99private:
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
109struct 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
122Descriptor*	get_descriptor(int fd);
123int			add_descriptor(Descriptor *descriptor);
124status_t	delete_descriptor(int fd);
125bool		is_unknown_or_system_descriptor(int fd);
126
127}	// namespace BPrivate
128
129#endif	// FS_DESCRIPTORS_H
130